Permalink
Browse files

Fix old default.png getting used when changing texture pack

  • Loading branch information...
UnknownShadow200 committed Dec 5, 2018
1 parent 7482123 commit 397f0f872af542a633278fa8440947564eb24627
Showing with 108 additions and 62 deletions.
  1. +34 −2 src/Drawer2D.c
  2. +3 −0 src/Drawer2D.h
  3. +0 −23 src/Game.c
  4. +0 −1 src/Game.h
  5. +0 −2 src/GameStructs.h
  6. +13 −3 src/Gui.c
  7. +4 −3 src/LScreens.c
  8. +34 −1 src/LWidgets.c
  9. +17 −15 src/Launcher.c
  10. +3 −3 src/Menus.c
  11. +0 −9 src/Screens.c
@@ -6,11 +6,18 @@
#include "ErrorHandler.h"
#include "Bitmap.h"
#include "Game.h"
#include "Event.h"
#include "Chat.h"
bool Drawer2D_BitmappedText;
bool Drawer2D_BlackTextShadows;
BitmapCol Drawer2D_Cols[DRAWER2D_MAX_COLS];
static char fontNameBuffer[STRING_SIZE];
String Drawer2D_FontName = String_FromArray(fontNameBuffer);
int Drawer2D_FontSize;
FontDesc Drawer2D_DefaultFont;
void DrawTextArgs_Make(struct DrawTextArgs* args, STRING_REF const String* text, const FontDesc* font, bool useShadow) {
args->Text = *text;
args->Font = *font;
@@ -29,7 +36,7 @@ void Drawer2D_MakeFont(FontDesc* desc, int size, int style) {
desc->Size = size;
desc->Style = style;
} else {
Font_Make(desc, &Game_FontName, size, style);
Font_Make(desc, &Drawer2D_FontName, size, style);
}
}
@@ -603,13 +610,38 @@ static void Drawer2D_Reset(void) {
}
}
static void Drawer2D_TextureChanged(void* obj, struct Stream* src, const String* name) {
Bitmap bmp;
ReturnCode res;
if (!String_CaselessEqualsConst(name, "default.png")) return;
if ((res = Png_Decode(&bmp, src))) {
Chat_LogError2(res, "decoding", name);
Mem_Free(bmp.Scan0);
} else {
Drawer2D_SetFontBitmap(&bmp);
Event_RaiseVoid(&ChatEvents_FontChanged);
}
}
static void Drawer2D_Init(void) {
Drawer2D_Reset();
Drawer2D_BitmappedText = Game_ClassicMode || !Options_GetBool(OPT_USE_CHAT_FONT, false);
Drawer2D_BlackTextShadows = Options_GetBool(OPT_BLACK_TEXT, false);
Options_Get(OPT_FONT_NAME, &Drawer2D_FontName, Font_DefaultName);
if (Game_ClassicMode) {
Drawer2D_FontName.length = 0;
String_AppendConst(&Drawer2D_FontName, Font_DefaultName);
}
/* TODO: Handle Arial font not working */
Event_RegisterEntry(&TextureEvents_FileChanged, NULL, Drawer2D_TextureChanged);
}
static void Drawer2D_Free(void) { Drawer2D_FreeFontBitmap(); }
static void Drawer2D_Free(void) {
Drawer2D_FreeFontBitmap();
Event_UnregisterEntry(&TextureEvents_FileChanged, NULL, Drawer2D_TextureChanged);
}
struct IGameComponent Drawer2D_Component = {
Drawer2D_Init, /* Init */
@@ -26,6 +26,9 @@ extern BitmapCol Drawer2D_Cols[DRAWER2D_MAX_COLS];
#define DRAWER2D_OFFSET 1
#define Drawer2D_GetCol(c) Drawer2D_Cols[(uint8_t)c]
/* Name of default font, defaults to Font_DefaultName. */
extern String Drawer2D_FontName;
/* Clamps the given rectangle to line inside the bitmap. */
/* Returns false if rectangle is completely outside bitmap's rectangle. */
bool Drawer2D_Clamp(Bitmap* bmp, int* x, int* y, int* width, int* height);
@@ -69,12 +69,10 @@ static int Game_TasksCount, entTaskI;
static char Game_UsernameBuffer[FILENAME_SIZE];
static char Game_MppassBuffer[STRING_SIZE];
static char Game_IPAddressBuffer[STRING_SIZE];
static char Game_FontNameBuffer[STRING_SIZE];
String Game_Username = String_FromArray(Game_UsernameBuffer);
String Game_Mppass = String_FromArray(Game_MppassBuffer);
String Game_IPAddress = String_FromArray(Game_IPAddressBuffer);
String Game_FontName = String_FromArray(Game_FontNameBuffer);
static struct IGameComponent* comps_head;
static struct IGameComponent* comps_tail;
@@ -329,16 +327,6 @@ static void Game_TextureChangedCore(void* obj, struct Stream* src, const String*
} else if (!Game_ChangeTerrainAtlas(&bmp)) {
Mem_Free(bmp.Scan0);
}
} else if (String_CaselessEqualsConst(name, "default.png")) {
res = Png_Decode(&bmp, src);
if (res) {
Chat_LogError2(res, "decoding", name);
Mem_Free(bmp.Scan0);
} else {
Drawer2D_SetFontBitmap(&bmp);
Event_RaiseVoid(&ChatEvents_FontChanged);
}
}
}
@@ -420,13 +408,6 @@ static void Game_LoadGuiOptions(void) {
Game_UseClassicOptions = Options_GetBool(OPT_CLASSIC_OPTIONS, false) || Game_ClassicMode;
Game_TabAutocomplete = Options_GetBool(OPT_TAB_AUTOCOMPLETE, false);
Options_Get(OPT_FONT_NAME, &Game_FontName, Font_DefaultName);
if (!Game_ClassicMode) {
Game_FontName.length = 0;
String_AppendConst(&Game_FontName, Font_DefaultName);
}
/* TODO: Handle Arial font not working */
}
void Game_Free(void* obj);
@@ -505,10 +486,6 @@ void Game_Load(void) {
}
Game_ExtractInitialTexturePack();
for (comp = comps_head; comp; comp = comp->Next) {
if (comp->Ready) comp->Ready();
}
entTaskI = ScheduledTask_Add(GAME_DEF_TICKS, Entities_Tick);
/* TODO: plugin dll support */
/* if (nonLoaded != null) {
@@ -53,7 +53,6 @@ extern bool Game_SmoothLighting;
extern bool Game_ChatLogging;
extern bool Game_AutoRotate;
extern bool Game_SmoothCamera;
extern String Game_FontName;
extern int Game_ChatLines;
extern bool Game_ClickableChat;
extern bool Game_HideGui;
@@ -18,8 +18,6 @@ struct IGameComponent {
void (*OnNewMap)(void);
/* Called to update the component's state when the user has finished loading a new map. */
void (*OnNewMapLoaded)(void);
/* Called when the texture pack has been loaded and all components have been initialised. */
void (*Ready)(void);
/* Next component in linked list of components. */
struct IGameComponent* Next;
};
@@ -83,6 +83,17 @@ bool Gui_Contains(int recX, int recY, int width, int height, int x, int y) {
return x >= recX && y >= recY && x < (recX + width) && y < (recY + height);
}
CC_NOINLINE static void Gui_RecreateScreen(struct Screen* screen) {
if (Gfx_LostContext || !screen) return;
Elem_Recreate(screen);
}
static void Gui_FontChanged(void* obj) {
Gui_RecreateScreen(Gui_Active);
Gui_RecreateScreen(Gui_Status);
Gui_RecreateScreen(Gui_HUD);
}
static void Gui_FileChanged(void* obj, struct Stream* stream, const String* name) {
if (String_CaselessEqualsConst(name, "gui.png")) {
Game_UpdateTexture(&Gui_GuiTex, stream, name, NULL);
@@ -94,12 +105,11 @@ static void Gui_FileChanged(void* obj, struct Stream* stream, const String* name
}
static void Gui_Init(void) {
Event_RegisterVoid(&ChatEvents_FontChanged, NULL, Gui_FontChanged);
Event_RegisterEntry(&TextureEvents_FileChanged, NULL, Gui_FileChanged);
Gui_Status = StatusScreen_MakeInstance();
Gui_HUD = HUDScreen_MakeInstance();
}
static void Gui_Ready(void) {
Elem_Init(Gui_Status);
Elem_Init(Gui_HUD);
}
@@ -113,6 +123,7 @@ static void Gui_Reset(void) {
}
static void Gui_Free(void) {
Event_UnregisterVoid(&ChatEvents_FontChanged, NULL, Gui_FontChanged);
Event_UnregisterEntry(&TextureEvents_FileChanged, NULL, Gui_FileChanged);
Gui_CloseActive();
Elem_TryFree(Gui_Status);
@@ -131,7 +142,6 @@ struct IGameComponent Gui_Component = {
Gui_Reset, /* Reset */
NULL, /* OnNewMap */
NULL, /* OnNewMapLoaded */
Gui_Ready
};
struct Screen* Gui_GetActiveScreen(void) {
@@ -85,7 +85,7 @@ CC_NOINLINE static void LScreen_Slider(struct LScreen* s, struct LSlider* w, int
/*########################################################################################################################*
*-------------------------------------------------------ChooseModeScreen--------------------------------------------------*
*#########################################################################################################################*/
static struct ChooseModeScreen {
/*static struct ChooseModeScreen {
LScreen_Layout
struct LWidget* Widgets[12];
struct LButton BtnEnhanced, BtnClassicHax, BtnClassic, BtnBack;
@@ -177,13 +177,13 @@ struct LScreen* ChooseModeScreen_MakeInstance(bool firstTime) {
s->Init = ChooseModeScreen_Init;
s->FirstTime = firstTime;
return (struct LScreen*)s;
}
}*/
/*########################################################################################################################*
*--------------------------------------------------------SettingsScreen---------------------------------------------------*
*#########################################################################################################################*/
static struct SettingsScreen {
/*static struct SettingsScreen {
LScreen_Layout
struct LWidget* Widgets[7];
struct LButton BtnUpdates, BtnMode, BtnColours, BtnBack;
@@ -245,3 +245,4 @@ struct LScreen* SettingsScreen_MakeInstance(void) {
s->Init = SettingsScreen_Init;
return (struct LScreen*)s;
}
*/
@@ -148,6 +148,15 @@ void LButton_SetText(struct LButton* w, const String* text, const FontDesc* font
/*########################################################################################################################*
*------------------------------------------------------InputWidget--------------------------------------------------------*
*#########################################################################################################################*/
CC_NOINLINE static void LInput_GetText(struct LInput* w, String* text) {
int i;
if (!w->Password) { *text = w->Text; return; }
for (i = 0; i < w->Text.length; i++) {
String_Append(text, '*');
}
}
CC_NOINLINE void LInput_Init(struct LInput* w, int width, int height, const char* hintText, const FontDesc* hintFont);
CC_NOINLINE void LInput_SetText(struct LInput* w, const String* text, const FontDesc* font);
CC_NOINLINE Rect2D LInput_MeasureCaret(struct LInput* w);
@@ -162,7 +171,31 @@ void LInput_AdvanceCaretPos(struct LInput* w, bool forwards) {
if (w->CaretPos < 0 || w->CaretPos >= w->Text.length) w->CaretPos = -1;
}
CC_NOINLINE void LInput_SetCaretToCursor(struct LInput* w, int x, int y);
void LInput_SetCaretToCursor(struct LInput* w, int x, int y) {
String text; char textBuffer[STRING_SIZE];
struct DrawTextArgs args;
int i, charX, charWidth;
String_InitArray(text, textBuffer);
LInput_GetText(w, &text);
x -= w->X; y -= w->Y;
DrawTextArgs_Make(&args, &text, &w->Font, true);
if (x >= Drawer2D_MeasureText(&args).Width) {
w->CaretPos = -1; return;
}
for (i = 0; i < text.length; i++) {
args.Text = String_UNSAFE_Substring(&text, 0, i);
charX = Drawer2D_MeasureText(&args).Width;
args.Text = String_UNSAFE_Substring(&text, i, 1);
charWidth = Drawer2D_MeasureText(&args).Width;
if (x >= charX && x < charX + charWidth) {
w->CaretPos = i; return;
}
}
}
bool LInput_Append(struct LInput* w, char c) {
if (w->TextFilter && !w->TextFilter(c)) return false;
@@ -34,8 +34,8 @@ void Launcher_ShowError(ReturnCode res, const char* place) {
/* TODO: FIX THESE STUBS!!! */
void Launcher_SecureSetOpt(const char* opt, const String* data, const String* key) { }
internal UpdateCheckTask checkTask;
/* TODO: FIX */
/* internal UpdateCheckTask checkTask; */
static bool fullRedraw, pendingRedraw;
static FontDesc logoFont;
@@ -69,9 +69,12 @@ void Launcher_SetScreen(struct LScreen* screen) {
screen->Init(screen);
/* for hovering over active button etc */
screen->MouseMove(screen, 0, 0);
/* TODO: FIX */
/* screen->MouseMove(screen, 0, 0); */
}
/* TODO FIX */
/*
static void Launcher_Init(void) {
BitmapCol col = BITMAPCOL_CONST(125, 125, 125, 255);
@@ -81,12 +84,9 @@ static void Launcher_Init(void) {
Window.Redraw += RedrawPending;
Keyboard.KeyDown += KeyDown;
Options_Load();
Options_Get(OPT_FONT_NAME, &Game_FontName, Font_DefaultName);
/* TODO: Handle Arial font not working */
Font_Make(&logoFont, &Game_FontName, 32, FONT_STYLE_NORMAL);
Font_Make(&Launcher_TitleFont, &Game_FontName, 16, FONT_STYLE_BOLD);
Font_Make(&Launcher_TextFont, &Game_FontName, 14, FONT_STYLE_NORMAL);
Font_Make(&logoFont, &Drawer2D_FontName, 32, FONT_STYLE_NORMAL);
Font_Make(&Launcher_TitleFont, &Drawer2D_FontName, 16, FONT_STYLE_BOLD);
Font_Make(&Launcher_TextFont, &Drawer2D_FontName, 14, FONT_STYLE_NORMAL);
Drawer2D_Cols['g'] = col;
Utils_EnsureDirectory("texpacks");
@@ -113,8 +113,10 @@ void Dispose() {
}
void Run() {
Options_Load();
Window = Factory.CreateWindow(640, 400, Program.AppName,
GraphicsMode.Default, DisplayDevice.Default);
Window_SetVisible(true);
Drawer2D_Component.Init();
Game_UpdateClientSize();
@@ -185,11 +187,7 @@ void Display() {
Window_DrawRaw(rec);
DirtyArea = Rectangle.Empty;
fullRedraw = false;
}
void KeyDown(Key key) {
if (IsShutdown(key)) Launcher_ShouldExit = true;
}
}*/
static bool Launcher_IsShutdown(Key key) {
if (key == KEY_F4 && Key_IsAltPressed()) return true;
@@ -202,6 +200,10 @@ static bool Launcher_IsShutdown(Key key) {
#endif
}
void KeyDown(Key key) {
if (Launcher_IsShutdown(key)) Launcher_ShouldExit = true;
}
/*########################################################################################################################*
*---------------------------------------------------------Colours/Skin----------------------------------------------------*
*#########################################################################################################################*/
@@ -308,7 +310,7 @@ static void Launcher_ProcessZipEntry(const String* path, struct Stream* data, st
res = Png_Decode(&bmp, data);
if (res) {
Launcher_ShowError(res, "decoding default.png"); return;
Launcher_ShowError(res, "decoding terrain.png"); return;
} else {
Drawer2D_SetFontBitmap(&bmp);
Launcher_LoadTextures(&bmp);
@@ -1431,8 +1431,8 @@ static void FontListScreen_EntryClick(void* screen, void* widget) {
int cur = s->CurrentIndex;
if (String_CaselessEqualsConst(&fontName, LIST_SCREEN_EMPTY)) return;
String_Copy(&Game_FontName, &fontName);
Options_Set(OPT_FONT_NAME, &fontName);
String_Copy(&Drawer2D_FontName, &fontName);
Options_Set(OPT_FONT_NAME, &fontName);
/* changing font recreates list menu */
Menu_HandleFontChange((struct Screen*)s);
@@ -1453,7 +1453,7 @@ static void FontListScreen_UpdateEntry(struct ListScreen* s, struct ButtonWidget
static void FontListScreen_Init(void* screen) {
struct ListScreen* s = screen;
ListScreen_Init(s);
ListScreen_Select(s, &Game_FontName);
ListScreen_Select(s, &Drawer2D_FontName);
}
static struct ScreenVTABLE FontListScreen_VTABLE = {
Oops, something went wrong.

0 comments on commit 397f0f8

Please sign in to comment.