mirror of
https://github.com/NoelFB/tiny_link.git
synced 2025-07-01 19:45:27 +08:00
tilemap rendering!
This commit is contained in:
@ -24,6 +24,9 @@ Collider Collider::make_grid(int tile_size, int columns, int rows)
|
||||
collider.m_grid.columns = columns;
|
||||
collider.m_grid.rows = rows;
|
||||
collider.m_grid.cells = std::shared_ptr<bool[]>(new bool[columns * rows]);
|
||||
|
||||
memset(collider.m_grid.cells.get(), 0, sizeof(bool) * columns * rows);
|
||||
|
||||
return collider;
|
||||
}
|
||||
|
||||
@ -60,6 +63,13 @@ void Collider::set_cell(int x, int y, bool value)
|
||||
m_grid.cells[x + y * m_grid.columns] = value;
|
||||
}
|
||||
|
||||
void Collider::set_cells(int x, int y, int w, int h, bool value)
|
||||
{
|
||||
for (int tx = x; tx < x + w; tx++)
|
||||
for (int ty = y; ty < y + h; ty++)
|
||||
m_grid.cells[tx + ty * m_grid.columns] = value;
|
||||
}
|
||||
|
||||
bool Collider::check(uint32_t mask, Point offset) const
|
||||
{
|
||||
auto other = world()->first<Collider>();
|
||||
@ -143,5 +153,21 @@ bool TL::Collider::rect_to_rect(const Collider* a, const Collider* b, Point offs
|
||||
|
||||
bool TL::Collider::rect_to_grid(const Collider* a, const Collider* b, Point offset)
|
||||
{
|
||||
// get a relative rectangle to the grid
|
||||
RectI rect = a->m_rect + a->entity()->position + offset - b->entity()->position;
|
||||
|
||||
// get the cells the rectangle overlaps
|
||||
int left = Calc::clamp_int(Calc::floor(rect.x / (float)b->m_grid.tile_size), 0, b->m_grid.columns);
|
||||
int right = Calc::clamp_int(Calc::ceiling(rect.right() / (float)b->m_grid.tile_size), 0, b->m_grid.columns);
|
||||
int top = Calc::clamp_int(Calc::floor(rect.y / (float)b->m_grid.tile_size), 0, b->m_grid.rows);
|
||||
int bottom = Calc::clamp_int(Calc::ceiling(rect.bottom() / (float)b->m_grid.tile_size), 0, b->m_grid.rows);
|
||||
|
||||
// check each cell
|
||||
for (int x = left; x < right; x++)
|
||||
for (int y = top; y < bottom; y++)
|
||||
if (b->m_grid.cells[x + y * b->m_grid.columns])
|
||||
return true;
|
||||
|
||||
// all cells were empty
|
||||
return false;
|
||||
}
|
||||
|
@ -29,6 +29,7 @@ namespace TL
|
||||
void set_rect(const RectI& value);
|
||||
bool get_cell(int x, int y) const;
|
||||
void set_cell(int x, int y, bool value);
|
||||
void set_cells(int x, int y, int w, int h, bool value);
|
||||
|
||||
bool check(uint32_t mask, Point offset = Point::zero) const;
|
||||
bool overlaps(const Collider* other, Point offset = Point::zero) const;
|
||||
|
61
src/components/tilemap.cpp
Normal file
61
src/components/tilemap.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
#include "tilemap.h"
|
||||
|
||||
using namespace TL;
|
||||
|
||||
Tilemap::Tilemap()
|
||||
{
|
||||
}
|
||||
|
||||
Tilemap::Tilemap(int tile_width, int tile_height, int columns, int rows)
|
||||
{
|
||||
m_tile_width = tile_width;
|
||||
m_tile_height = tile_height;
|
||||
m_columns = columns;
|
||||
m_rows = rows;
|
||||
m_grid = std::shared_ptr<Subtexture[]>(new Subtexture[columns * rows]);
|
||||
}
|
||||
|
||||
int Tilemap::tile_width() const
|
||||
{
|
||||
return m_tile_width;
|
||||
}
|
||||
|
||||
int Tilemap::tile_height() const
|
||||
{
|
||||
return m_tile_height;
|
||||
}
|
||||
|
||||
int Tilemap::columns() const
|
||||
{
|
||||
return m_columns;
|
||||
}
|
||||
|
||||
int Tilemap::rows() const
|
||||
{
|
||||
return m_rows;
|
||||
}
|
||||
|
||||
void Tilemap::set_cell(int x, int y, const Subtexture* tex)
|
||||
{
|
||||
if (tex)
|
||||
m_grid[x + y * m_columns] = *tex;
|
||||
else
|
||||
m_grid[x + y * m_columns].texture.reset();
|
||||
}
|
||||
|
||||
void Tilemap::set_cells(int x, int y, int w, int h, const Subtexture* tex)
|
||||
{
|
||||
for (int tx = x; tx < x + w; tx++)
|
||||
for (int ty = y; ty < y + h; ty++)
|
||||
set_cell(tx, ty, tex);
|
||||
}
|
||||
|
||||
void Tilemap::render(Batch& batch)
|
||||
{
|
||||
for (int x = 0; x < m_columns; x ++)
|
||||
for (int y = 0; y < m_rows; y ++)
|
||||
if (m_grid[x + y * m_columns].texture)
|
||||
{
|
||||
batch.tex(m_grid[x + y * m_columns], Vec2(x * m_tile_width, y * m_tile_height));
|
||||
}
|
||||
}
|
32
src/components/tilemap.h
Normal file
32
src/components/tilemap.h
Normal file
@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#include "../world.h"
|
||||
#include <blah.h>
|
||||
#include <memory>
|
||||
|
||||
using namespace Blah;
|
||||
|
||||
namespace TL
|
||||
{
|
||||
class Tilemap : public Component
|
||||
{
|
||||
public:
|
||||
Tilemap();
|
||||
Tilemap(int tile_width, int tile_height, int columns, int rows);
|
||||
|
||||
int tile_width() const;
|
||||
int tile_height() const;
|
||||
int columns() const;
|
||||
int rows() const;
|
||||
|
||||
void set_cell(int x, int y, const Subtexture* tex);
|
||||
void set_cells(int x, int y, int w, int h, const Subtexture* tex);
|
||||
void render(Batch& batch) override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<Subtexture[]> m_grid;
|
||||
int m_tile_width = 0;
|
||||
int m_tile_height = 0;
|
||||
int m_columns = 0;
|
||||
int m_rows = 0;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user