diff --git a/engine.c b/engine.c index 803ff43b12..9329176246 100644 --- a/engine.c +++ b/engine.c @@ -56,7 +56,7 @@ void DeleteCustomEngineNames(void) uint i; StringID old; - for(i=0; i!=TOTAL_NUM_ENGINES; i++) { + for (i = 0; i != TOTAL_NUM_ENGINES; i++) { old = _engine_name_strings[i]; _engine_name_strings[i] = i + STR_8000_KIRBY_PAUL_TANK_STEAM; DeleteName(old); @@ -73,10 +73,10 @@ void LoadCustomEngineNames(void) static void SetupEngineNames(void) { - uint i; + StringID *name; - for(i=0; i!=TOTAL_NUM_ENGINES; i++) - _engine_name_strings[i] = STR_SV_EMPTY; + for (name = _engine_name_strings; name != endof(_engine_name_strings); name++) + *name = STR_SV_EMPTY; DeleteCustomEngineNames(); LoadCustomEngineNames(); @@ -200,7 +200,7 @@ void StartupEngines(void) AdjustAvailAircraft(); } -uint32 _engine_refit_masks[256]; +uint32 _engine_refit_masks[TOTAL_NUM_ENGINES]; // TODO: We don't support cargo-specific wagon overrides. Pretty exotic... ;-) --pasky @@ -214,7 +214,7 @@ struct WagonOverride { static struct WagonOverrides { int overrides_count; struct WagonOverride *overrides; -} _engine_wagon_overrides[256]; +} _engine_wagon_overrides[TOTAL_NUM_ENGINES]; void SetWagonOverrideSprites(byte engine, struct SpriteGroup *group, byte *train_id, int trains) @@ -260,12 +260,12 @@ static struct SpriteGroup *GetWagonOverrideSpriteSet(byte engine, byte overridin } -byte _engine_original_sprites[256]; +byte _engine_original_sprites[TOTAL_NUM_ENGINES]; // 0 - 28 are cargos, 29 is default, 30 is the advert (purchase list) // (It isn't and shouldn't be like this in the GRF files since new cargo types // may appear in future - however it's more convenient to store it like this in // memory. --pasky) -static struct SpriteGroup _engine_custom_sprites[256][NUM_CID]; +static struct SpriteGroup _engine_custom_sprites[TOTAL_NUM_ENGINES][NUM_CID]; void SetCustomEngineSprites(byte engine, byte cargo, struct SpriteGroup *group) { @@ -622,14 +622,22 @@ void TriggerVehicle(Vehicle *veh, enum VehicleTrigger trigger) DoTriggerVehicle(veh, trigger, 0, true); } - -static char *_engine_custom_names[256]; +static char *_engine_custom_names[TOTAL_NUM_ENGINES]; void SetCustomEngineName(int engine, const char *name) { _engine_custom_names[engine] = strdup(name); } +void UnInitNewgrEngines(void) +{ + char **i; + for (i = _engine_custom_names; i != endof(_engine_custom_names); i++) { + free(*i); + *i = NULL; + } +} + StringID GetCustomEngineName(int engine) { if (!_engine_custom_names[engine]) diff --git a/engine.h b/engine.h index 022af0ac72..c7ab673dc5 100644 --- a/engine.h +++ b/engine.h @@ -134,6 +134,7 @@ void LoadCustomEngineNames(void); void DeleteCustomEngineNames(void); bool IsEngineBuildable(uint engine, byte type); +void UnInitNewgrEngines(void); enum { NUM_NORMAL_RAIL_ENGINES = 54, diff --git a/sdl.c b/sdl.c index 73a849f7bb..9433faa73c 100644 --- a/sdl.c +++ b/sdl.c @@ -330,7 +330,7 @@ extern const char _openttd_revision[]; static bool CreateMainSurface(int w, int h) { SDL_Surface *newscreen; - char *caption; + char caption[50]; GetAvailableVideoMode(&w, &h); @@ -348,7 +348,7 @@ static bool CreateMainSurface(int w, int h) _sdl_screen = newscreen; InitPalette(); - caption = str_fmt("OpenTTD %s", _openttd_revision); + snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision); SDL_CALL SDL_WM_SetCaption(caption, caption); SDL_CALL SDL_ShowCursor(0); diff --git a/station_gui.c b/station_gui.c index 90a547c72e..702351c142 100644 --- a/station_gui.c +++ b/station_gui.c @@ -82,8 +82,7 @@ static void GlobalSortStationList(void) uint16 *i; // reset #-of stations to 0 because ++ is used for value-assignment - for (i = _num_station_sort; i != endof(_num_station_sort); i++) - *i = 0; + memset(_num_station_sort, 0, sizeof(_num_station_sort)); /* Create array for sorting */ _station_sort = realloc(_station_sort, GetStationPoolSize() * sizeof(_station_sort[0])); diff --git a/ttd.c b/ttd.c index 7b85a59f59..eeb0c24523 100644 --- a/ttd.c +++ b/ttd.c @@ -447,6 +447,11 @@ static void UnInitializeDynamicVariables(void) free(_industry_sort); } +static void UnInitializeGame(void) +{ + UnInitWindowSystem(); + UnInitNewgrEngines(); +} static void LoadIntroGame(void) { @@ -459,7 +464,7 @@ static void LoadIntroGame(void) LoadStringWidthTable(); // Setup main window - InitWindowSystem(); + ResetWindowSystem(); SetupColorsAndInitialWindow(); // Generate a world. @@ -699,6 +704,7 @@ int ttd_main(int argc, char* argv[]) /* Close all and any open filehandles */ FioCloseAll(); + UnInitializeGame(); return 0; } @@ -725,7 +731,7 @@ static void MakeNewGame(void) GfxLoadSprites(); // Reinitialize windows - InitWindowSystem(); + ResetWindowSystem(); LoadStringWidthTable(); SetupColorsAndInitialWindow(); @@ -757,7 +763,7 @@ static void MakeNewEditorWorld(void) GfxLoadSprites(); // Re-init the windowing system - InitWindowSystem(); + ResetWindowSystem(); // Create toolbars SetupColorsAndInitialWindow(); @@ -792,7 +798,7 @@ static void StartScenario(void) GfxLoadSprites(); // Reinitialize windows - InitWindowSystem(); + ResetWindowSystem(); LoadStringWidthTable(); SetupColorsAndInitialWindow(); @@ -1327,7 +1333,7 @@ bool AfterLoadGame(uint version) } // Initialize windows - InitWindowSystem(); + ResetWindowSystem(); SetupColorsAndInitialWindow(); w = FindWindowById(WC_MAIN_WINDOW, 0); diff --git a/window.c b/window.c index c65b69c7f9..a1e4edcefc 100644 --- a/window.c +++ b/window.c @@ -360,11 +360,10 @@ void AssignWidgetToWindow(Window *w, const Widget *widget) index++; } - w->widget = malloc(sizeof(Widget) * index); + w->widget = realloc(w->widget, sizeof(Widget) * index); memcpy(w->widget, widget, sizeof(Widget) * index); - } else { + } else w->widget = NULL; - } } Window *AllocateWindow( @@ -427,6 +426,7 @@ Window *AllocateWindow( w->vscroll.count = 0; w->hscroll.pos = 0; w->hscroll.count = 0; + w->widget = NULL; AssignWidgetToWindow(w, widget); w->resize.width = width; w->resize.height = height; @@ -672,6 +672,7 @@ Window *FindWindowFromPt(int x, int y) void InitWindowSystem(void) { IConsoleClose(); + memset(&_windows, 0, sizeof(_windows)); _last_window = _windows; memset(_viewports, 0, sizeof(_viewports)); @@ -679,12 +680,28 @@ void InitWindowSystem(void) _no_scroll = 0; } +void UnInitWindowSystem(void) +{ + Window *w; + // delete all malloced widgets + for (w = _windows; w != _last_window; w++) { + free(w->widget); + w->widget = NULL; + } +} + +void ResetWindowSystem(void) +{ + UnInitWindowSystem(); + InitWindowSystem(); +} + static void DecreaseWindowCounters(void) { Window *w; - for(w=_last_window; w != _windows;) { + for (w = _last_window; w != _windows;) { --w; // Unclick scrollbar buttons if they are pressed. if (w->flags4 & (WF_SCROLL_DOWN | WF_SCROLL_UP)) { @@ -694,7 +711,7 @@ static void DecreaseWindowCounters(void) CallWindowEventNP(w, WE_MOUSELOOP); } - for(w=_last_window; w != _windows;) { + for (w = _last_window; w != _windows;) { --w; if (w->flags4&WF_TIMEOUT_MASK && !(--w->flags4&WF_TIMEOUT_MASK)) { diff --git a/window.h b/window.h index 8259b28527..fde10cdf52 100644 --- a/window.h +++ b/window.h @@ -563,6 +563,8 @@ Window *AllocateWindowAutoPlace2( void DrawWindowViewport(Window *w); void InitWindowSystem(void); +void UnInitWindowSystem(void); +void ResetWindowSystem(void); int GetMenuItemIndex(Window *w, int x, int y); void MouseLoop(void); void UpdateWindows(void);