ClassiCube/Launcher2/LauncherWindow.Background.cs

149 lines
4.9 KiB
C#
Raw Normal View History

2016-06-11 15:29:45 +10:00
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using System.Drawing;
using System.IO;
using ClassicalSharp;
using ClassicalSharp.Textures;
using Launcher.Drawing;
2016-06-11 15:29:45 +10:00
namespace Launcher {
public sealed partial class LauncherWindow {
internal bool ClassicBackground = false;
bool fontPng, terrainPng;
internal void TryLoadTexturePack() {
fontPng = false; terrainPng = false;
Options.Load();
LauncherSkin.LoadFromOptions();
2016-11-27 14:50:45 +11:00
if (Options.Get("nostalgia-classicbg") != null)
ClassicBackground = Options.GetBool("nostalgia-classicbg", false);
2016-06-11 15:29:45 +10:00
else
2016-11-27 14:50:45 +11:00
ClassicBackground = Options.GetBool("mode-classic", false);
2016-06-11 15:29:45 +10:00
2016-11-27 14:50:45 +11:00
string texDir = Path.Combine(Program.AppDirectory, "texpacks");
string texPack = Options.Get(OptionsKey.DefaultTexturePack) ?? "default.zip";
texPack = Path.Combine(texDir, texPack);
2016-06-11 15:29:45 +10:00
2016-11-27 14:50:45 +11:00
if (!File.Exists(texPack))
texPack = Path.Combine(texDir, "default.zip");
if (!File.Exists(texPack)) return;
2016-06-11 15:29:45 +10:00
2016-11-27 14:50:45 +11:00
ExtractTexturePack(texPack);
if (!fontPng || !terrainPng) {
texPack = Path.Combine(texDir, "default.zip");
ExtractTexturePack(texPack);
2016-06-11 15:29:45 +10:00
}
}
2016-11-27 14:50:45 +11:00
void ExtractTexturePack(string texPack) {
using (Stream fs = new FileStream(texPack, FileMode.Open, FileAccess.Read, FileShare.Read)) {
ZipReader reader = new ZipReader();
2016-06-11 15:29:45 +10:00
reader.ShouldProcessZipEntry = (f) => f == "default.png" || f == "terrain.png";
reader.ProcessZipEntry = ProcessZipEntry;
2016-11-27 14:50:45 +11:00
reader.Extract(fs);
2016-06-11 15:29:45 +10:00
}
}
2016-11-27 14:50:45 +11:00
void ProcessZipEntry(string filename, byte[] data, ZipEntry entry) {
if (filename == "default.png") {
if (fontPng) return;
Bitmap bmp = Platform.ReadBmp32Bpp(Drawer, data);
2016-11-27 14:50:45 +11:00
Drawer.SetFontBitmap(bmp);
useBitmappedFont = !Options.GetBool(OptionsKey.ArialChatFont, false);
2016-06-11 15:29:45 +10:00
fontPng = true;
2016-11-27 14:50:45 +11:00
} else if (filename == "terrain.png") {
if (terrainPng) return;
Bitmap bmp = Platform.ReadBmp32Bpp(Drawer, data);
2016-11-27 14:50:45 +11:00
MakeClassicTextures(bmp);
bmp.Dispose();
2016-06-11 15:29:45 +10:00
terrainPng = true;
}
}
bool useBitmappedFont;
Bitmap terrainBmp;
FastBitmap terrainPixels;
const int tileSize = 48;
2016-11-27 14:50:45 +11:00
void MakeClassicTextures(Bitmap bmp) {
2016-06-11 15:29:45 +10:00
int elemSize = bmp.Width / 16;
2016-11-27 14:50:45 +11:00
Size size = new Size(tileSize, tileSize);
terrainBmp = Platform.CreateBmp(tileSize * 2, tileSize);
2016-11-27 14:50:45 +11:00
terrainPixels = new FastBitmap(terrainBmp, true, false);
2016-06-11 15:29:45 +10:00
// Precompute the scaled background
2016-11-27 14:50:45 +11:00
using (FastBitmap src = new FastBitmap(bmp, true, true)) {
BitmapDrawer.DrawScaled(src, terrainPixels, size,
new Rectangle(2 * elemSize, 0, elemSize, elemSize),
new Rectangle(tileSize, 0, tileSize, tileSize), 128, 64);
BitmapDrawer.DrawScaled(src, terrainPixels, size,
new Rectangle(1 * elemSize, 0, elemSize, elemSize),
new Rectangle(0, 0, tileSize, tileSize), 96, 96);
2016-06-11 15:29:45 +10:00
}
}
public void RedrawBackground() {
2016-11-27 14:50:45 +11:00
if (Framebuffer == null || (Framebuffer.Width != Width || Framebuffer.Height != Height)) {
if (Framebuffer != null)
2016-06-11 15:29:45 +10:00
Framebuffer.Dispose();
2016-11-27 14:50:45 +11:00
Framebuffer = platformDrawer.CreateFrameBuffer(Width, Height);
2016-06-11 15:29:45 +10:00
}
2016-11-27 14:50:45 +11:00
if (ClassicBackground && terrainPixels != null) {
using (FastBitmap bmp = LockBits()) {
ClearTile(0, 0, Width, 48, tileSize, bmp);
ClearTile(0, 48, Width, Height - 48, 0, bmp);
2016-06-11 15:29:45 +10:00
}
} else {
2016-11-27 14:50:45 +11:00
ResetArea(0, 0, Width, Height);
2016-06-11 15:29:45 +10:00
}
DrawTitle();
Dirty = true;
}
void DrawTitle() {
2016-11-27 14:50:45 +11:00
using (IDrawer2D drawer = Drawer) {
drawer.SetBitmap(Framebuffer);
2016-06-11 15:29:45 +10:00
drawer.UseBitmappedChat = (useBitmappedFont || ClassicBackground) && fontPng;
2016-11-27 14:50:45 +11:00
DrawTextArgs args = new DrawTextArgs("&eClassical&fSharp", logoFont, false);
2017-02-28 10:46:03 +11:00
Size size = drawer.MeasureSize(ref args);
2016-06-11 15:29:45 +10:00
int xStart = Width / 2 - size.Width / 2;
args.Text = "&0Classical&0Sharp";
2017-02-28 10:46:03 +11:00
drawer.DrawText(ref args, xStart + 4, 4);
2016-06-11 15:29:45 +10:00
args.Text = "&eClassical&fSharp";
2017-02-28 10:46:03 +11:00
drawer.DrawText(ref args, xStart, 0);
2016-06-11 15:29:45 +10:00
drawer.UseBitmappedChat = false;
}
}
/// <summary> Redraws the specified region with the background pixels. </summary>
2016-11-27 14:50:45 +11:00
public void ResetArea(int x, int y, int width, int height) {
using (FastBitmap dst = LockBits())
ResetArea(x, y, width, height, dst);
2016-06-11 15:29:45 +10:00
}
/// <summary> Redraws the specified region with the background pixels. </summary>
2016-11-27 14:50:45 +11:00
public void ResetArea(int x, int y, int width, int height, FastBitmap dst) {
if (ClassicBackground && terrainPixels != null) {
ClearTile(x, y, width, height, 0, dst);
2016-06-11 15:29:45 +10:00
} else {
FastColour col = LauncherSkin.BackgroundCol;
2016-11-27 14:50:45 +11:00
Gradient.Noise(dst, new Rectangle(x, y, width, height), col, 6);
2016-06-11 15:29:45 +10:00
}
}
2016-11-27 14:50:45 +11:00
void ClearTile(int x, int y, int width, int height, int srcX, FastBitmap dst) {
Rectangle srcRect = new Rectangle(srcX, 0, tileSize, tileSize);
Rectangle dstRect = new Rectangle(x, y, width, height);
BitmapDrawer.DrawTiled(terrainPixels, dst, srcRect, dstRect);
2016-06-11 15:29:45 +10:00
}
}
}