implement a proper image list allocation

This commit is contained in:
Ted John 2016-07-02 23:03:25 +01:00
parent 7124143b89
commit 3bfa747ce4
3 changed files with 101 additions and 24 deletions

View File

@ -68,6 +68,7 @@
<ClCompile Include="src\drawing\engines\opengl\TextureCache.cpp" />
<ClCompile Include="src\drawing\engines\SoftwareDrawingEngine.cpp" />
<ClCompile Include="src\drawing\font.c" />
<ClCompile Include="src\drawing\Image.cpp" />
<ClCompile Include="src\drawing\line.c" />
<ClCompile Include="src\drawing\NewDrawing.cpp" />
<ClCompile Include="src\drawing\Rain.cpp" />

100
src/drawing/Image.cpp Normal file
View File

@ -0,0 +1,100 @@
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
/*****************************************************************************
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
*
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* A full copy of the GNU General Public License can be found in licence.txt
*****************************************************************************/
#pragma endregion
#include <list>
#include "../core/Guard.hpp"
extern "C"
{
#include "drawing.h"
}
constexpr uint32 BASE_IMAGE_ID = 29294;
constexpr uint32 MAX_IMAGES = 262144;
constexpr uint32 INVALID_IMAGE_ID = UINT32_MAX;
struct ImageList
{
uint32 BaseId;
uint32 Count;
};
static bool _initialised = false;
static std::list<ImageList> _freeLists;
static uint32 AllocateImageList(uint32 count)
{
if (!_initialised)
{
_initialised = true;
_freeLists.clear();
_freeLists.push_back({ BASE_IMAGE_ID, MAX_IMAGES });
}
for (auto it = _freeLists.begin(); it != _freeLists.end(); it++)
{
ImageList imageList = *it;
if (imageList.Count >= count)
{
_freeLists.erase(it);
if (imageList.Count > count)
{
ImageList remainder = { imageList.BaseId + count,
imageList.Count - count };
_freeLists.push_back(remainder);
}
return imageList.BaseId;
}
}
return INVALID_IMAGE_ID;
}
static void FreeImageList(uint32 baseImageId, uint32 count)
{
Guard::Assert(_initialised);
// TODO validate that this was an allocated list
_freeLists.push_back({ baseImageId, count });
}
extern "C"
{
uint32 gfx_object_allocate_images(const rct_g1_element * images, uint32 count)
{
uint32 baseImageId = AllocateImageList(count);
if (baseImageId == INVALID_IMAGE_ID)
{
log_error("Reached maximum image limit.");
return INVALID_IMAGE_ID;
}
uint32 imageId = baseImageId;
for (uint32 i = 0; i < count; i++)
{
g1Elements[imageId] = images[i];
drawing_engine_invalidate_image(imageId);
imageId++;
}
return baseImageId;
}
void gfx_object_free_images(uint32 baseImageId, uint32 count)
{
FreeImageList(baseImageId, count);
}
}

View File

@ -173,30 +173,6 @@ int gfx_load_g2()
return 0;
}
static uint32 _nextImageId = 29294;
uint32 gfx_object_allocate_images(const rct_g1_element * images, uint32 count)
{
uint32 baseImageId = _nextImageId;
for (uint32 i = 0; i < count; i++) {
uint32 imageId = _nextImageId;
if (imageId >= 291438) {
log_error("Reached maximum image limit.");
break;
}
g1Elements[imageId] = images[i];
drawing_engine_invalidate_image(imageId);
_nextImageId++;
}
return baseImageId;
}
void gfx_object_free_images(uint32 baseImageId, uint32 count)
{
_nextImageId = 29294;
}
/**
* This function looks like it initialises the 0x009E3CE4 array which references sprites used for background / palette mixing or
* something. Further investigation is needed.