Created
February 3, 2020 21:32
-
-
Save tehsausage/2823ee19b0bcc60b8aeb14f180cf216d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "map_renderer.hpp" | |
#include "common.hpp" | |
#include "common_alsmart.hpp" | |
#include "draw_buffer.hpp" | |
#include "gfx_manager.hpp" | |
Map_Renderer::Map_Renderer(GFX_Manager& gfx) | |
: m_gfx(gfx) | |
{ } | |
Map_Renderer::~Map_Renderer() | |
{ } | |
void Map_Renderer::draw(Draw_Buffer& draw) | |
{ | |
if (!m_emf) | |
return; | |
auto&& gfx = m_gfx; | |
auto&& emf = m_emf.value().get(); | |
auto&& emfh = emf.header; | |
constexpr float ep = 0.0001f; // gap between depth of each tile on a layer | |
constexpr float epi = 0.00001f; // gap between each interleaved layer | |
struct LayerInfo | |
{ | |
int file; | |
int xoff; | |
int yoff; | |
bool center; | |
float depth; | |
}; | |
static constexpr LayerInfo layer_info[9] = { | |
{ 3, 0, 0, false, 1.0f - epi*0 }, // Ground | |
{ 4, -2, -2, true, 0.8f - epi*0 }, // Objects | |
{ 5, -2, -2, true, 0.7f - epi*0 }, // Overlay | |
{ 6, 0, -1, false, 0.8f - epi*1 }, // Down Wall | |
{ 6, 32, -1, false, 0.8f - epi*2 }, // Right Wall | |
{ 7, 0, -64, false, 0.7f - epi*1 }, // Roof | |
{ 3, 0, -32, false, 0.7f - epi*2 }, // Top | |
{ 22, -24, -12, false, 0.9f - epi*0 }, // Shadow | |
{ 5, -2, -2, true, 0.7f - epi*3 }, // Overlay 2 | |
}; | |
float depth = layer_info[0].depth; | |
auto next_depth = [&depth]() { return depth -= ep; }; | |
auto base_gfx = gfx.get_image(3, emfh.fill_tile); | |
for (int layer = 0; layer < 9; ++layer) | |
{ | |
if (layer == 7) | |
continue; | |
depth = layer_info[layer].depth; | |
for (int i = 0; i < emfh.width + emfh.height; ++i) | |
{ | |
int x, y; | |
if (i < emfh.height) | |
{ | |
x = 0; | |
y = i; | |
} | |
else | |
{ | |
x = i - emfh.height + 1; | |
y = emfh.height - 1; | |
} | |
for (next_depth(); y >= 0 && x < emfh.width; --y, ++x, next_depth()) | |
{ | |
int xoff = layer_info[layer].xoff; | |
int yoff = layer_info[layer].yoff; | |
bool center = layer_info[layer].center; | |
short tile = emf.gfx(x, y)[layer]; | |
int tilex = xoff + (x * 32) - (y * 32); | |
int tiley = yoff + (x * 16) + (y * 16); | |
if (layer == 0 && tile == -1) | |
{ | |
if (emfh.fill_tile != 0) | |
draw.draw(base_gfx, tilex, tiley, depth, 0); | |
} | |
if (tile > 0) | |
{ | |
auto tile_gfx = gfx.get_image(layer_info[layer].file, tile); | |
if (tile_gfx) | |
{ | |
int tile_w = al_get_bitmap_width(tile_gfx.get()); | |
int tile_h = al_get_bitmap_height(tile_gfx.get()); | |
if (center) | |
{ | |
tilex -= (tile_w / 2) - 32; | |
} | |
if (layer != 0) | |
tiley -= tile_h - 32; | |
draw.draw(tile_gfx, tilex, tiley, depth, 0); | |
} | |
else | |
{ | |
cio::out << "Could not load tile " << layer_info[layer].file << '/' << tile << cio::endl; | |
} | |
} | |
} | |
} | |
} | |
draw.sync(); | |
} | |
void Map_Renderer::set_map(Full_EMF& emf) | |
{ | |
m_emf = std::ref(emf); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment