Skip to content

Commit

Permalink
Tidy load_g1()
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjuk committed Jan 21, 2018
1 parent 368b5f3 commit 3a1691f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
43 changes: 21 additions & 22 deletions src/openloco/graphics/gfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,48 @@ 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;
loco_global_array<g1_element_t, LOCO_G1_ELEMENT_COUNT, 0x9E2424> _g1Elements;

drawpixelinfo_t& screen_dpi()
{
return _screen_dpi;
}

// 0x0044733C
void load_g1()
bool load_g1()
{
auto g1Path = environment::get_path(environment::path_id::g1);

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

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

if (file != NULL) {
if (fread(&header, 8, 1, file) == 1) {
if (file == nullptr)
{
return false;
}

// Read element headers
fread(_g1Elements, header.num_entries * sizeof(loco_g1_element), 1, file);
bool fileReadable = (fread(&header, 8, 1, file) == 1);

// Read element data
g1Buffer = malloc(header.total_size);
fread(g1Buffer, header.total_size, 1, file);
if (fileReadable)
{
// Read element headers
fread(_g1Elements, header.num_entries * sizeof(g1_element_t), 1, file);

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

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

// 0x00447485
Expand Down
6 changes: 3 additions & 3 deletions src/openloco/graphics/gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ namespace openloco::gfx

drawpixelinfo_t& screen_dpi();

struct loco_g1_header
struct g1_header_t
{
uint32_t num_entries;
uint32_t total_size;
};

struct loco_g1_element
struct g1_element_t
{
uint8_t * offset; // 0x00
int16_t width; // 0x04
Expand All @@ -42,7 +42,7 @@ namespace openloco::gfx
int16_t unused; // 0x0E
};

void load_g1();
bool load_g1();
void clear(drawpixelinfo_t &dpi, uint32_t fill);
void clear_single(drawpixelinfo_t &dpi, uint8_t paletteId);
void draw_string_494B3F(
Expand Down
5 changes: 4 additions & 1 deletion src/openloco/openloco.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ namespace openloco
scenariomgr::load_index(0);
progressbar::begin(string_ids::loading, 0);
progressbar::set_progress(60);
gfx::load_g1();
if (!gfx::load_g1())
{
throw std::runtime_error("Unable to load g1.dat.");
}
progressbar::set_progress(220);
call(0x004949BC);
progressbar::set_progress(235);
Expand Down

0 comments on commit 3a1691f

Please sign in to comment.