Skip to content

Commit

Permalink
rework load_g1()
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjuk committed Jan 21, 2018
1 parent 3a1691f commit 8a5ff8c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
39 changes: 28 additions & 11 deletions src/openloco/graphics/gfx.cpp
Expand Up @@ -16,7 +16,7 @@ namespace openloco::gfx
}

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

Expand All @@ -26,30 +26,47 @@ namespace openloco::gfx

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

if (file == nullptr)
try
{
return false;
}
if (file == nullptr)
{
throw std::exception("Opening g1 file failed.");
}

bool fileReadable = (fread(&header, 8, 1, file) == 1);
if (fread(&header, 8, 1, file) != 1)
{
throw std::exception("Reading g1 file header failed.");
}

if (fileReadable)
{
// Read element headers
fread(_g1Elements, header.num_entries * sizeof(g1_element_t), 1, file);
if (fread(_g1Elements, header.num_entries * sizeof(g1_element_t), 1, file) != 1)
{
throw std::exception("Reading g1 element headers failed.");
}

// Read element data
g1Buffer = malloc(header.total_size);
fread(g1Buffer, header.total_size, 1, file);
if (fread(g1Buffer, header.total_size, 1, file) != 1)
{
free(g1Buffer);
throw std::exception("Reading g1 elements failed.");
}

// Adjust memory offsets
for (uint32_t i = 0; i < header.num_entries; i++)
{
_g1Elements[i].offset += (int32_t)g1Buffer;
}
fclose(file);
}
catch (const std::exception &e)
{
if (file == nullptr)
{
fclose(file);
}
throw std::runtime_error(e.what());
}
fclose(file);
return fileReadable;
}

// 0x00447485
Expand Down
2 changes: 1 addition & 1 deletion src/openloco/graphics/gfx.h
Expand Up @@ -42,7 +42,7 @@ namespace openloco::gfx
int16_t unused; // 0x0E
};

bool load_g1();
void 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: 1 addition & 4 deletions src/openloco/openloco.cpp
Expand Up @@ -253,10 +253,7 @@ namespace openloco
scenariomgr::load_index(0);
progressbar::begin(string_ids::loading, 0);
progressbar::set_progress(60);
if (!gfx::load_g1())
{
throw std::runtime_error("Unable to load g1.dat.");
}
gfx::load_g1();
progressbar::set_progress(220);
call(0x004949BC);
progressbar::set_progress(235);
Expand Down

0 comments on commit 8a5ff8c

Please sign in to comment.