Skip to content

Commit

Permalink
Implement load_g1()
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjuk committed Jan 21, 2018
1 parent b57af38 commit afe4ae8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/openloco/graphics/gfx.cpp
@@ -1,4 +1,5 @@
#include "../interop/interop.hpp"
#include "../environment.h"
#include "../ui.h"
#include "gfx.h"

Expand All @@ -7,6 +8,7 @@ using namespace openloco::interop;
namespace openloco::gfx
{
loco_global<drawpixelinfo_t, 0x0050B884> _screen_dpi;
loco_global_array<loco_g1_element, LOCO_G1_ELEMENT_COUNT, 0x9E2424> _g1Elements;

drawpixelinfo_t& screen_dpi()
{
Expand All @@ -16,7 +18,39 @@ namespace openloco::gfx
// 0x0044733C
void load_g1()
{
call(0x0044733C);
auto g1Path = environment::get_path(environment::path_id::g1);

FILE * file;
gfx::loco_g1_header header;
void * g1Buffer;

file = fopen(g1Path.make_preferred().u8string().c_str(), "rb");

if (file != NULL) {
if (fread(&header, 8, 1, file) == 1) {

// Read element headers
fread(_g1Elements, header.num_entries * sizeof(loco_g1_element), 1, file);

// Read element data
g1Buffer = malloc(header.total_size);
fread(g1Buffer, header.total_size, 1, file);

fclose(file);

// Adjust memory offsets
for (uint32_t i = 0; i < header.num_entries; i++)
{
_g1Elements[i].offset += (int)g1Buffer;
}
}
fclose(file);
return;
}
else
{
throw std::exception("Unable to load g1.dat");
}
}

// 0x00447485
Expand Down
19 changes: 19 additions & 0 deletions src/openloco/graphics/gfx.h
Expand Up @@ -4,6 +4,8 @@
#include "../localisation/stringmgr.h"
#include "../openloco.h"

#define LOCO_G1_ELEMENT_COUNT 0x1A10

namespace openloco::gfx
{
#pragma pack(push, 1)
Expand All @@ -23,6 +25,23 @@ namespace openloco::gfx

drawpixelinfo_t& screen_dpi();

struct loco_g1_header
{
uint32_t num_entries;
uint32_t total_size;
};

struct loco_g1_element
{
uint8_t * offset; // 0x00
int16_t width; // 0x04
int16_t height; // 0x06
int16_t x_offset; // 0x08
int16_t y_offset; // 0x0A
uint16_t flags; // 0x0C
int16_t unused; // 0x0E
};

void load_g1();
void clear(drawpixelinfo_t &dpi, uint32_t fill);
void clear_single(drawpixelinfo_t &dpi, uint8_t paletteId);
Expand Down

0 comments on commit afe4ae8

Please sign in to comment.