diff --git a/src/lang/english.txt b/src/lang/english.txt index cdfd490c14..f9f18d450e 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -1602,6 +1602,8 @@ STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_ALGORITHM :{WHITE}... save STR_CONFIG_ERROR_INVALID_BASE_GRAPHICS_NOT_FOUND :{WHITE}... ignoring Base Graphics set '{RAW_STRING}': not found STR_CONFIG_ERROR_INVALID_BASE_SOUNDS_NOT_FOUND :{WHITE}... ignoring Base Sounds set '{RAW_STRING}': not found STR_CONFIG_ERROR_INVALID_BASE_MUSIC_NOT_FOUND :{WHITE}... ignoring Base Music set '{RAW_STRING}': not found +STR_CONFIG_ERROR_OUT_OF_MEMORY :{WHITE}Out of memory +STR_CONFIG_ERROR_SPRITECACHE_TOO_BIG :{WHITE}Allocating {BYTES} of spritecache failed. The spritecache was reduced to {BYTES}. This will reduce the performance of OpenTTD. To reduce memory requirements you can try to disable 32bpp graphics and/or zoom-in levels # Intro window STR_INTRO_CAPTION :{WHITE}OpenTTD {REV} diff --git a/src/rail_gui.cpp b/src/rail_gui.cpp index ed1bd0ac04..cee1903a04 100644 --- a/src/rail_gui.cpp +++ b/src/rail_gui.cpp @@ -1080,8 +1080,8 @@ public: if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) { DrawPixelInfo *old_dpi = _cur_dpi; _cur_dpi = &tmp_dpi; - if (!DrawStationTile(32, 16, _cur_railtype, AXIS_X, _railstation.station_class, _railstation.station_type)) { - StationPickerDrawSprite(32, 16, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 2); + if (!DrawStationTile(32, 28, _cur_railtype, AXIS_X, _railstation.station_class, _railstation.station_type)) { + StationPickerDrawSprite(32, 28, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 2); } _cur_dpi = old_dpi; } @@ -1092,8 +1092,8 @@ public: if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) { DrawPixelInfo *old_dpi = _cur_dpi; _cur_dpi = &tmp_dpi; - if (!DrawStationTile(32, 16, _cur_railtype, AXIS_Y, _railstation.station_class, _railstation.station_type)) { - StationPickerDrawSprite(32, 16, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 3); + if (!DrawStationTile(32, 28, _cur_railtype, AXIS_Y, _railstation.station_class, _railstation.station_type)) { + StationPickerDrawSprite(32, 28, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 3); } _cur_dpi = old_dpi; } @@ -1125,11 +1125,11 @@ public: } /* Set up a clipping area for the station preview. */ - if (FillDrawPixelInfo(&tmp_dpi, r.left + 1, r.top + 1, r.right - (r.left + 1) + 1, r.bottom - 1 - (r.top + 1) + 1)) { + if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) { DrawPixelInfo *old_dpi = _cur_dpi; _cur_dpi = &tmp_dpi; - if (!DrawStationTile(31, 29, _cur_railtype, _railstation.orientation, _railstation.station_class, type)) { - StationPickerDrawSprite(31, 29, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 2 + _railstation.orientation); + if (!DrawStationTile(32, 28, _cur_railtype, _railstation.orientation, _railstation.station_class, type)) { + StationPickerDrawSprite(32, 28, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 2 + _railstation.orientation); } _cur_dpi = old_dpi; } @@ -1354,9 +1354,9 @@ static const NWidgetPart _nested_station_builder_widgets[] = { NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_ORIENTATION, STR_NULL), SetPadding(1, 2, 0, 2), NWidget(NWID_HORIZONTAL), NWidget(NWID_SPACER), SetMinimalSize(7, 0), SetFill(1, 0), - NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAS_PLATFORM_DIR_X), SetMinimalSize(66, 48), SetFill(0, 0), SetDataTip(0x0, STR_STATION_BUILD_RAILROAD_ORIENTATION_TOOLTIP), EndContainer(), + NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAS_PLATFORM_DIR_X), SetMinimalSize(66, 60), SetFill(0, 0), SetDataTip(0x0, STR_STATION_BUILD_RAILROAD_ORIENTATION_TOOLTIP), EndContainer(), NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0), - NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAS_PLATFORM_DIR_Y), SetMinimalSize(66, 48), SetFill(0, 0), SetDataTip(0x0, STR_STATION_BUILD_RAILROAD_ORIENTATION_TOOLTIP), EndContainer(), + NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAS_PLATFORM_DIR_Y), SetMinimalSize(66, 60), SetFill(0, 0), SetDataTip(0x0, STR_STATION_BUILD_RAILROAD_ORIENTATION_TOOLTIP), EndContainer(), NWidget(NWID_SPACER), SetMinimalSize(7, 0), SetFill(1, 0), EndContainer(), NWidget(WWT_LABEL, COLOUR_DARK_GREEN, WID_BRAS_SHOW_NEWST_TYPE), SetMinimalSize(144, 11), SetDataTip(STR_ORANGE_STRING, STR_NULL), SetPadding(1, 2, 4, 2), diff --git a/src/spritecache.cpp b/src/spritecache.cpp index 8d6a63ec90..3833d500b7 100644 --- a/src/spritecache.cpp +++ b/src/spritecache.cpp @@ -13,6 +13,7 @@ #include "fileio_func.h" #include "spriteloader/grf.hpp" #include "gfx_func.h" +#include "error.h" #include "zoom_func.h" #include "settings_type.h" #include "blitter/factory.hpp" @@ -20,6 +21,7 @@ #include "core/mem_func.hpp" #include "table/sprites.h" +#include "table/strings.h" #include "table/palette_convert.h" /* Default of 4MB spritecache */ @@ -847,10 +849,43 @@ static void GfxInitSpriteCache() int bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth(); uint target_size = (bpp > 0 ? _sprite_cache_size * bpp / 8 : 1) * 1024 * 1024; - if (_spritecache_ptr == NULL || _allocated_sprite_cache_size != target_size) { - free(_spritecache_ptr); + /* Remember 'target_size' from the previous allocation attempt, so we do not try to reach the target_size multiple times in case of failure. */ + static uint last_alloc_attempt = 0; + + if (_spritecache_ptr == NULL || (_allocated_sprite_cache_size != target_size && target_size != last_alloc_attempt)) { + delete[] reinterpret_cast(_spritecache_ptr); + + last_alloc_attempt = target_size; _allocated_sprite_cache_size = target_size; - _spritecache_ptr = (MemBlock*)MallocT(_allocated_sprite_cache_size); + + do { + try { + /* Try to allocate 50% more to make sure we do not allocate almost all available. */ + _spritecache_ptr = reinterpret_cast(new byte[_allocated_sprite_cache_size + _allocated_sprite_cache_size / 2]); + } catch (std::bad_alloc &oom) { + _spritecache_ptr = NULL; + } + + if (_spritecache_ptr != NULL) { + /* Allocation succeeded, but we wanted less. */ + delete[] reinterpret_cast(_spritecache_ptr); + _spritecache_ptr = reinterpret_cast(new byte[_allocated_sprite_cache_size]); + } else if (_allocated_sprite_cache_size < 2 * 1024 * 1024) { + usererror("Cannot allocate spritecache"); + } else { + /* Try again to allocate half. */ + _allocated_sprite_cache_size >>= 1; + } + } while (_spritecache_ptr == NULL); + + if (_allocated_sprite_cache_size != target_size) { + DEBUG(misc, 0, "Not enough memory to allocate %d MiB of spritecache. Spritecache was reduced to %d MiB.", target_size / 1024 / 1024, _allocated_sprite_cache_size / 1024 / 1024); + + ErrorMessageData msg(STR_CONFIG_ERROR_OUT_OF_MEMORY, STR_CONFIG_ERROR_SPRITECACHE_TOO_BIG); + msg.SetDParam(0, target_size); + msg.SetDParam(1, _allocated_sprite_cache_size); + ScheduleErrorMessage(msg); + } } /* A big free block */