Skip to content

Commit

Permalink
Tilemap render order fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nemene21 committed Jul 6, 2024
1 parent 6c0484e commit a795433
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 73 deletions.
140 changes: 72 additions & 68 deletions build/Makefile

Large diffs are not rendered by default.

Binary file added build/assets/images/tilemap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions src/framework/entities/tilemap_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Tilemap::Tilemap(Vector2 tilesize, std::string texture_path):
built_chunks {},
changed_chunks {},
tilesize {tilesize},
renderer {[this](float delta) {this->render(delta); }},
chunksize {16, 16} {

type_count = (texture.get()->width / tilesize.x) / 4;
}

Expand Down Expand Up @@ -268,7 +268,7 @@ void Tilemap::process(float delta) {
}

// Draws the tilemap (spatial partitioning and camera culling at play)
void Tilemap::draw(float delta) {
void Tilemap::render(float delta) {
Vector2 camera_pos {0, 0};
float max_dist = sqrt(tilesize.x*tilesize.x + tilesize.y*tilesize.y) +
sqrt(res.x*res.x + res.y*res.y);
Expand All @@ -294,7 +294,6 @@ void Tilemap::draw(float delta) {

if (built_chunks.find(chunk_pos) != built_chunks.end()) { // If chunk exists
TileDataVector &drawables = built_chunks[chunk_pos];

// For all tiles
for (auto& tile: drawables) {
Vector2 tile_pos {tile.pos.x * tilesize.x, tile.pos.y * tilesize.y};
Expand Down
6 changes: 5 additions & 1 deletion src/framework/entities/tilemap_entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
#include <entity.hpp>

#include <camera_component.hpp>
#include <tilemap_renderer.hpp>
#include <collider_component.hpp>
#include <area_component.hpp>
#include <drawables.hpp>
#include <fstream>
#include <json.hpp>
#include <tuple>
Expand Down Expand Up @@ -57,11 +59,12 @@ class Tilemap: public Entity {
public:
Vector2 tilesize;
Vector2 chunksize;
TilemapRenderer renderer;

Tilemap(Vector2 tilesize, std::string texture_path);

void process(float delta);
void draw(float delta);
void render(float delta);

/// @brief Set tile at x, y to type
/// @param x X coord of tile
Expand Down Expand Up @@ -91,4 +94,5 @@ class Tilemap: public Entity {
void load(std::string path);
};


#endif
7 changes: 7 additions & 0 deletions src/framework/entities/tilemap_renderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <tilemap_renderer.hpp>

TilemapRenderer::TilemapRenderer(std::function<void(float)> func): Drawable(), func {func} {}

void TilemapRenderer::draw() {
func(GetFrameTime());
}
14 changes: 14 additions & 0 deletions src/framework/entities/tilemap_renderer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef TILEMAP_RENDERER_H
#define TILEMAP_RENDERER_H
#include <drawables.hpp>
#include <functional>

class TilemapRenderer: public Drawable {
public:
TilemapRenderer(std::function<void(float)> func);

std::function<void(float)> func;
void draw();
};

#endif
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ int main() {

Framework::run();
return 0;
}
}

0 comments on commit a795433

Please sign in to comment.