Fix compilation of sprite.cpp

This commit is contained in:
Ted John 2017-02-09 13:11:32 +00:00
parent 6a0f934a5f
commit b79e714740
6 changed files with 551 additions and 544 deletions

View File

@ -270,7 +270,6 @@ extern sint32 gPickupPeepX;
extern sint32 gPickupPeepY;
extern rct_g1_element *g1Elements;
extern rct_gx g2;
extern rct_drawpixelinfo gScreenDPI;
extern rct_drawpixelinfo gWindowDPI;

View File

@ -14,27 +14,37 @@
*****************************************************************************/
#pragma endregion
#include <memory>
#include "../common.h"
#include "../config.h"
#include "../core/FileStream.hpp"
#include "../core/Memory.hpp"
#include "../core/Util.hpp"
#include "../OpenRCT2.h"
#include "../rct2/addresses.h"
#include "../sprites.h"
extern "C"
{
#include "../config.h"
#include "../rct2/addresses.h"
#include "../util/util.h"
#include "drawing.h"
}
void *_g1Buffer = NULL;
rct_gx g2;
rct_gx csg;
extern "C"
{
static void * _g1Buffer = nullptr;
static rct_gx _g2;
static rct_gx _csg;
#ifdef NO_RCT2
rct_g1_element *g1Elements = NULL;
rct_g1_element * g1Elements = nullptr;
#else
rct_g1_element * g1Elements = RCT2_ADDRESS(RCT2_ADDRESS_G1_ELEMENTS, rct_g1_element);
#endif
static const uint32 fadeSprites[] = {
SPR_NONE,
static const uint32 FadeSprites[] =
{
(uint32)SPR_NONE,
SPR_FADE_1,
SPR_FADE_2,
SPR_FADE_3,
@ -44,23 +54,25 @@ static const uint32 fadeSprites[] = {
SPR_FADE_7,
};
static void read_and_convert_gxdat(SDL_RWops *file, size_t count, rct_g1_element *elements)
static void read_and_convert_gxdat(IStream * stream, size_t count, rct_g1_element *elements)
{
rct_g1_element_32bit *g1Elements32 = calloc(count, sizeof(rct_g1_element_32bit));
SDL_RWread(file, g1Elements32, count * sizeof(rct_g1_element_32bit), 1);
for (size_t i = 0; i < count; i++) {
auto g1Elements32 = stream->ReadArray<rct_g1_element_32bit>(count);
for (size_t i = 0; i < count; i++)
{
const auto src = &g1Elements32[i];
/* Double cast to silence compiler warning about casting to
* pointer from integer of mismatched length.
*/
elements[i].offset = (uint8*)(uintptr_t)g1Elements32[i].offset;
elements[i].width = g1Elements32[i].width;
elements[i].height = g1Elements32[i].height;
elements[i].x_offset = g1Elements32[i].x_offset;
elements[i].y_offset = g1Elements32[i].y_offset;
elements[i].flags = g1Elements32[i].flags;
elements[i].zoomed_offset = g1Elements32[i].zoomed_offset;
elements[i].offset = (uint8*)(uintptr_t)src->offset;
elements[i].width = src->width;
elements[i].height = src->height;
elements[i].x_offset = src->x_offset;
elements[i].y_offset = src->y_offset;
elements[i].flags = src->flags;
elements[i].zoomed_offset = src->zoomed_offset;
}
free(g1Elements32);
Memory::Free(g1Elements32);
}
/**
@ -69,12 +81,12 @@ static void read_and_convert_gxdat(SDL_RWops *file, size_t count, rct_g1_element
*/
bool gfx_load_g1()
{
log_verbose("loading g1 graphics");
log_verbose("gfx_load_g1()");
try
{
auto fs = FileStream(get_file_path(PATH_ID_G1), FILE_MODE_OPEN);
rct_g1_header header = fs.ReadValue<rct_g1_header>();
SDL_RWops *file = SDL_RWFromFile(get_file_path(PATH_ID_G1), "rb");
if (file != NULL) {
rct_g1_header header;
if (SDL_RWread(file, &header, 8, 1) == 1) {
/* number of elements is stored in g1.dat, but because the entry
* headers are static, this can't be variable until made into a
* dynamic array.
@ -83,32 +95,30 @@ bool gfx_load_g1()
// Read element headers
#ifdef NO_RCT2
g1Elements = calloc(324206, sizeof(rct_g1_element));
g1Elements = Memory::AllocateArray<rct_g1_element>(324206);
#endif
read_and_convert_gxdat(file, header.num_entries, g1Elements);
read_and_convert_gxdat(&fs, header.num_entries, g1Elements);
// Read element data
_g1Buffer = malloc(header.total_size);
SDL_RWread(file, _g1Buffer, header.total_size, 1);
SDL_RWclose(file);
_g1Buffer = fs.ReadArray<uint8>(header.total_size);
// Fix entry data offsets
for (uint32 i = 0; i < header.num_entries; i++)
{
g1Elements[i].offset += (uintptr_t)_g1Buffer;
}
return true;
}
SDL_RWclose(file);
}
catch (const Exception &)
{
log_fatal("Unable to load g1 graphics");
if (!gOpenRCT2Headless) {
if (!gOpenRCT2Headless)
{
platform_show_messagebox("Unable to load g1.dat. Your RollerCoaster Tycoon 2 path may be incorrectly set.");
}
return false;
}
}
void gfx_unload_g1()
{
@ -120,61 +130,64 @@ void gfx_unload_g1()
void gfx_unload_g2()
{
SafeFree(g2.elements);
SafeFree(_g2.elements);
SafeFree(_g2.data);
}
void gfx_unload_csg()
{
SafeFree(csg.elements);
SafeFree(_csg.elements);
SafeFree(_csg.data);
}
bool gfx_load_g2()
{
log_verbose("loading g2 graphics");
log_verbose("gfx_load_g2()");
char path[MAX_PATH];
platform_get_openrct_data_path(path, sizeof(path));
safe_strcat_path(path, "g2.dat", MAX_PATH);
SDL_RWops *file = SDL_RWFromFile(path, "rb");
if (file != NULL) {
if (SDL_RWread(file, &g2.header, 8, 1) == 1) {
// Read element headers
g2.elements = malloc(g2.header.num_entries * sizeof(rct_g1_element));
try
{
auto fs = FileStream(path, FILE_MODE_OPEN);
_g2.header = fs.ReadValue<rct_g1_header>();
read_and_convert_gxdat(file, g2.header.num_entries, g2.elements);
// Read element headers
_g2.elements = Memory::AllocateArray<rct_g1_element>(_g2.header.num_entries);
read_and_convert_gxdat(&fs, _g2.header.num_entries, _g2.elements);
// Read element data
g2.data = malloc(g2.header.total_size);
SDL_RWread(file, g2.data, g2.header.total_size, 1);
SDL_RWclose(file);
_g2.data = fs.ReadArray<uint8>(_g2.header.total_size);
// Fix entry data offsets
for (uint32 i = 0; i < g2.header.num_entries; i++)
g2.elements[i].offset += (uintptr_t)g2.data;
for (uint32 i = 0; i < _g2.header.num_entries; i++)
{
_g2.elements[i].offset += (uintptr_t)_g2.data;
}
return true;
}
SDL_RWclose(file);
}
catch (const Exception &)
{
log_fatal("Unable to load g2 graphics");
if (!gOpenRCT2Headless) {
if (!gOpenRCT2Headless)
{
platform_show_messagebox("Unable to load g2.dat");
}
}
return false;
}
bool gfx_load_csg()
{
if (str_is_null_or_empty(gConfigGeneral.rct1_path)) {
log_verbose("gfx_load_csg()");
if (str_is_null_or_empty(gConfigGeneral.rct1_path))
{
log_verbose(" unable to load CSG, RCT1 path not set");
return false;
}
bool success = false;
log_verbose("loading csg graphics");
char pathHeader[MAX_PATH];
safe_strcpy(pathHeader, gConfigGeneral.rct1_path, sizeof(pathHeader));
safe_strcat_path(pathHeader, "Data", sizeof(pathHeader));
@ -185,47 +198,34 @@ bool gfx_load_csg()
safe_strcat_path(pathData, "Data", sizeof(pathData));
safe_strcat_path(pathData, "csg1.1", sizeof(pathData));
SDL_RWops * fileHeader = SDL_RWFromFile(pathHeader, "rb");
SDL_RWops * fileData = SDL_RWFromFile(pathData, "rb");
if (fileHeader != NULL && fileData != NULL) {
SDL_RWseek(fileHeader, 0, RW_SEEK_END);
SDL_RWseek(fileData, 0, RW_SEEK_END);
size_t fileHeaderSize = SDL_RWtell(fileHeader);
size_t fileDataSize = SDL_RWtell(fileData);
SDL_RWseek(fileHeader, 0, RW_SEEK_SET);
SDL_RWseek(fileData, 0, RW_SEEK_SET);
try
{
auto fileHeader = FileStream(pathHeader, FILE_MODE_OPEN);
auto fileData = FileStream(pathData, FILE_MODE_OPEN);
size_t fileHeaderSize = fileHeader.GetLength();
size_t fileDataSize = fileData.GetLength();
csg.header.num_entries = (uint32)(fileHeaderSize / sizeof(rct_g1_element_32bit));
csg.header.total_size = (uint32)fileDataSize;
_csg.header.num_entries = (uint32)(fileHeaderSize / sizeof(rct_g1_element_32bit));
_csg.header.total_size = (uint32)fileDataSize;
// Read element headers
csg.elements = malloc(csg.header.num_entries * sizeof(rct_g1_element));
read_and_convert_gxdat(fileHeader, csg.header.num_entries, csg.elements);
_csg.elements = Memory::AllocateArray<rct_g1_element>(_csg.header.num_entries);
read_and_convert_gxdat(&fileHeader, _csg.header.num_entries, _csg.elements);
// Read element data
csg.data = malloc(csg.header.total_size);
SDL_RWread(fileData, csg.data, csg.header.total_size, 1);
_csg.data = fileData.ReadArray<uint8>(_csg.header.total_size);
// Fix entry data offsets
for (uint32 i = 0; i < csg.header.num_entries; i++) {
csg.elements[i].offset += (uintptr_t)csg.data;
for (uint32 i = 0; i < _csg.header.num_entries; i++)
{
_csg.elements[i].offset += (uintptr_t)_csg.data;
// RCT1 used zoomed offsets that counted from the beginning of the file, rather than from the current sprite.
csg.elements[i].zoomed_offset = i - (SPR_CSG_BEGIN + csg.elements[i].zoomed_offset);
_csg.elements[i].zoomed_offset = i - (SPR_CSG_BEGIN + _csg.elements[i].zoomed_offset);
}
success = true;
}
if (fileHeader != NULL) {
SDL_RWclose(fileHeader);
}
if (fileData != NULL) {
SDL_RWclose(fileData);
}
if (success) {
return true;
} else {
}
catch (const Exception &)
{
log_error("Unable to load csg graphics");
return false;
}
@ -237,12 +237,16 @@ bool gfx_load_csg()
*/
void sub_68371D()
{
for (sint32 i = 0; i < countof(fadeSprites); i++) {
const uint32 spriteId = fadeSprites[i];
if (spriteId == SPR_NONE) {
unk_9E3CE4[i] = NULL;
} else {
unk_9E3CE4[i] = g1Elements[fadeSprites[i]].offset;
for (size_t i = 0; i < Util::CountOf(FadeSprites); i++)
{
const uint32 spriteId = FadeSprites[i];
if (spriteId == (uint32)SPR_NONE)
{
unk_9E3CE4[i] = nullptr;
}
else
{
unk_9E3CE4[i] = g1Elements[FadeSprites[i]].offset;
}
}
}
@ -252,7 +256,8 @@ void sub_68371D()
* image.
* rct2: 0x0067A690
*/
static void FASTCALL gfx_bmp_sprite_to_buffer(uint8* palette_pointer, uint8* unknown_pointer, uint8* source_pointer, uint8* dest_pointer, rct_g1_element* source_image, rct_drawpixelinfo *dest_dpi, sint32 height, sint32 width, sint32 image_type){
static void FASTCALL gfx_bmp_sprite_to_buffer(uint8* palette_pointer, uint8* unknown_pointer, uint8* source_pointer, uint8* dest_pointer, rct_g1_element* source_image, rct_drawpixelinfo *dest_dpi, sint32 height, sint32 width, sint32 image_type)
{
uint16 zoom_level = dest_dpi->zoom_level;
uint8 zoom_amount = 1 << zoom_level;
uint32 dest_line_width = (dest_dpi->width / zoom_amount) + dest_dpi->pitch;
@ -260,7 +265,7 @@ static void FASTCALL gfx_bmp_sprite_to_buffer(uint8* palette_pointer, uint8* unk
// Image uses the palette pointer to remap the colours of the image
if (image_type & IMAGE_TYPE_REMAP){
assert(palette_pointer != NULL);
assert(palette_pointer != nullptr);
//image with remaps
for (; height > 0; height -= zoom_amount){
@ -284,7 +289,7 @@ static void FASTCALL gfx_bmp_sprite_to_buffer(uint8* palette_pointer, uint8* unk
//telling if it needs to be drawn not for colour. Colour provided
//by the palette pointer.
if (image_type & IMAGE_TYPE_TRANSPARENT){//Not tested
assert(palette_pointer != NULL);
assert(palette_pointer != nullptr);
for (; height > 0; height -= zoom_amount){
uint8* next_source_pointer = source_pointer + source_line_width;
uint8* next_dest_pointer = dest_pointer + dest_line_width;
@ -340,7 +345,7 @@ static void FASTCALL gfx_bmp_sprite_to_buffer(uint8* palette_pointer, uint8* unk
uint8* FASTCALL gfx_draw_sprite_get_palette(sint32 image_id, uint32 tertiary_colour) {
sint32 image_type = (image_id & 0xE0000000);
if (image_type == 0)
return NULL;
return nullptr;
if (!(image_type & IMAGE_TYPE_REMAP_2_PLUS)) {
uint8 palette_ref = (image_id >> 19) & 0xFF;
@ -400,7 +405,7 @@ void FASTCALL gfx_draw_sprite_software(rct_drawpixelinfo *dpi, sint32 image_id,
}
gfx_draw_sprite_palette_set_software(dpi, image_id, x, y, palette_pointer, NULL);
gfx_draw_sprite_palette_set_software(dpi, image_id, x, y, palette_pointer, nullptr);
}
/*
@ -420,15 +425,14 @@ void FASTCALL gfx_draw_sprite_palette_set_software(rct_drawpixelinfo *dpi, sint3
rct_g1_element *g1_source = gfx_get_g1_element(image_element);
if (dpi->zoom_level != 0 && (g1_source->flags & G1_FLAG_HAS_ZOOM_SPRITE)) {
rct_drawpixelinfo zoomed_dpi = {
.bits = dpi->bits,
.x = dpi->x >> 1,
.y = dpi->y >> 1,
.height = dpi->height>>1,
.width = dpi->width>>1,
.pitch = dpi->pitch,
.zoom_level = dpi->zoom_level - 1
};
rct_drawpixelinfo zoomed_dpi;
zoomed_dpi.bits = dpi->bits;
zoomed_dpi.x = dpi->x >> 1;
zoomed_dpi.y = dpi->y >> 1;
zoomed_dpi.height = dpi->height >> 1;
zoomed_dpi.width = dpi->width >> 1;
zoomed_dpi.pitch = dpi->pitch;
zoomed_dpi.zoom_level = dpi->zoom_level - 1;
gfx_draw_sprite_palette_set_software(&zoomed_dpi, image_type | (image_element - g1_source->zoomed_offset), x >> 1, y >> 1, palette_pointer, unknown_pointer);
return;
}
@ -549,7 +553,6 @@ void FASTCALL gfx_draw_sprite_palette_set_software(rct_drawpixelinfo *dpi, sint3
if (!(g1_source->flags & G1_FLAG_1)) {
gfx_bmp_sprite_to_buffer(palette_pointer, unknown_pointer, source_pointer, dest_pointer, g1_source, dpi, height, width, image_type);
}
return;
}
/**
@ -573,16 +576,16 @@ void FASTCALL gfx_draw_sprite_raw_masked_software(rct_drawpixelinfo *dpi, sint32
return;
}
width = min(imgMask->width, imgColour->width);
height = min(imgMask->height, imgColour->height);
width = Math::Min(imgMask->width, imgColour->width);
height = Math::Min(imgMask->height, imgColour->height);
x += imgMask->x_offset;
y += imgMask->y_offset;
left = max(dpi->x, x);
top = max(dpi->y, y);
right = min(dpi->x + dpi->width, x + width);
bottom = min(dpi->y + dpi->height, y + height);
left = Math::Max<sint32>(dpi->x, x);
top = Math::Max<sint32>(dpi->y, y);
right = Math::Min(dpi->x + dpi->width, x + width);
bottom = Math::Min(dpi->y + dpi->height, y + height);
width = right - left;
height = bottom - top;
@ -616,12 +619,17 @@ void FASTCALL gfx_draw_sprite_raw_masked_software(rct_drawpixelinfo *dpi, sint32
}
}
rct_g1_element *gfx_get_g1_element(sint32 image_id) {
if (image_id < SPR_G2_BEGIN) {
rct_g1_element * gfx_get_g1_element(sint32 image_id)
{
if (image_id < SPR_G2_BEGIN)
{
return &g1Elements[image_id];
}
if (image_id < SPR_CSG_BEGIN) {
return &g2.elements[image_id - SPR_G2_BEGIN];
if (image_id < SPR_CSG_BEGIN)
{
return &_g2.elements[image_id - SPR_G2_BEGIN];
}
return &csg.elements[image_id - SPR_CSG_BEGIN];
return &_csg.elements[image_id - SPR_CSG_BEGIN];
}
}

View File

@ -495,7 +495,7 @@ utf8 *platform_open_directory_browser(utf8 *title) {
return return_value;
}
void platform_show_messagebox(char *message) {
void platform_show_messagebox(const char * message) {
size_t size;
dialog_type dtype;
char cmd[MAX_PATH];

View File

@ -94,7 +94,7 @@ void platform_posix_sub_resolve_openrct_data_path(utf8 *out, size_t size) {
}
}
void platform_show_messagebox(char *message)
void platform_show_messagebox(const char * message)
{
@autoreleasepool
{

View File

@ -193,7 +193,7 @@ void platform_resolve_openrct_data_path();
void platform_get_openrct_data_path(utf8 *outPath, size_t outSize);
void platform_get_user_directory(utf8 *outPath, const utf8 *subDirectory, size_t outSize);
utf8* platform_get_username();
void platform_show_messagebox(utf8 *message);
void platform_show_messagebox(const utf8 * message);
bool platform_open_common_file_dialog(utf8 *outFilename, file_dialog_desc *desc, size_t outSize);
utf8 *platform_open_directory_browser(utf8 *title);
uint8 platform_get_locale_currency();

View File

@ -590,7 +590,7 @@ void platform_get_user_directory(utf8 *outPath, const utf8 *subDirectory, size_t
}
}
void platform_show_messagebox(utf8 *message)
void platform_show_messagebox(const utf8 * message)
{
MessageBoxA(windows_get_window_handle(), message, "OpenRCT2", MB_OK);
}