Show diamond-shaped tiles on a larger map.
This commit is contained in:
parent
edbb3fe1d8
commit
1c5cb6299c
8 changed files with 36 additions and 25 deletions
|
@ -8,6 +8,7 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
namespace dk {
|
namespace dk {
|
||||||
template <typename T, uint32_t D, typename T1>
|
template <typename T, uint32_t D, typename T1>
|
||||||
|
@ -60,7 +61,7 @@ namespace dk {
|
||||||
TileIterator ( qualif_vector_type* parData, const coords& parFrom, const coords& parTo, const coords& parAreaFrom, const coords& parAreaTo, const coords& parSubdiv );
|
TileIterator ( qualif_vector_type* parData, const coords& parFrom, const coords& parTo, const coords& parAreaFrom, const coords& parAreaTo, const coords& parSubdiv );
|
||||||
~TileIterator ( void ) = default;
|
~TileIterator ( void ) = default;
|
||||||
|
|
||||||
const coords& position ( void ) const { return m_pos; }
|
const coords& abs_position ( void ) const { return m_pos; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void increment ( void );
|
void increment ( void );
|
||||||
|
|
|
@ -52,5 +52,8 @@ namespace dk {
|
||||||
void Layer<T, D>::onPreload (const coords& parFrom, const coords& parTo) {
|
void Layer<T, D>::onPreload (const coords& parFrom, const coords& parTo) {
|
||||||
m_tiles.clear();
|
m_tiles.clear();
|
||||||
m_tilemap.fetch(m_tiles, parFrom, parTo);
|
m_tilemap.fetch(m_tiles, parFrom, parTo);
|
||||||
|
#if !defined(NDEBUG)
|
||||||
|
std::cout << "Preloading layer from " << parFrom << " to " << parTo << '\n';
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
14
test.map
14
test.map
|
@ -1,6 +1,8 @@
|
||||||
1101111011
|
1101111001
|
||||||
1100011000
|
1101111001
|
||||||
1111011110
|
1110111100
|
||||||
1111000110
|
1110111000
|
||||||
0000010111
|
1111011011
|
||||||
1111110111
|
1111010111
|
||||||
|
1111101111
|
||||||
|
1111111111
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 26 KiB |
Binary file not shown.
Before Width: | Height: | Size: 38 KiB |
BIN
test/graphics/tile_0.png
Normal file
BIN
test/graphics/tile_0.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.4 KiB |
BIN
test/graphics/tile_1.png
Normal file
BIN
test/graphics/tile_1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.5 KiB |
|
@ -3,6 +3,7 @@
|
||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
#include "platformstrings.h"
|
#include "platformstrings.h"
|
||||||
#include "doorkeeper/helpers/asciimapsource.hpp"
|
#include "doorkeeper/helpers/asciimapsource.hpp"
|
||||||
|
#include "doorkeeper/helpers/tilecoordinates.hpp"
|
||||||
#include "doorkeeper/mapreaders/mapstreamraw.hpp"
|
#include "doorkeeper/mapreaders/mapstreamraw.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
@ -64,7 +65,7 @@ int main() {
|
||||||
&SDL_DestroyRenderer
|
&SDL_DestroyRenderer
|
||||||
);
|
);
|
||||||
|
|
||||||
dk::Tyler<2> tiler(coords2(10, 6), coords2(64));
|
dk::Tyler<2> tiler(coords2(10, 8), coords2(64));
|
||||||
|
|
||||||
LayerWithData<AsciiMapSource, int> bottomLayer;
|
LayerWithData<AsciiMapSource, int> bottomLayer;
|
||||||
addLayer(tiler, bottomLayer, DATA_PATH"/test.map");
|
addLayer(tiler, bottomLayer, DATA_PATH"/test.map");
|
||||||
|
@ -90,13 +91,15 @@ int main() {
|
||||||
//Main loop
|
//Main loop
|
||||||
bool running = true;
|
bool running = true;
|
||||||
int y = 0;
|
int y = 0;
|
||||||
dk::Viewport<2> viewport(tiler, coords2(6, 4), coords2(0));
|
dk::Viewport<2> viewport(tiler, coords2(10, 8), coords2(0));
|
||||||
|
coords2 tile_size;
|
||||||
|
SDL_QueryTexture(tile_0.get(), nullptr, nullptr, &tile_size.x(), &tile_size.y());
|
||||||
do {
|
do {
|
||||||
SDL_RenderClear(sdl_renderer.get());
|
SDL_RenderClear(sdl_renderer.get());
|
||||||
viewport.setFrom(coords2(0, y / 128));
|
viewport.setFrom(coords2(0, y / tile_size.y()));
|
||||||
draw_tiles(sdl_renderer.get(), tile_0.get(), tile_1.get(), viewport, *bottomLayer.layer, -(y % 128));
|
draw_tiles(sdl_renderer.get(), tile_0.get(), tile_1.get(), viewport, *bottomLayer.layer, -(y % tile_size.y()));
|
||||||
++y;
|
//++y;
|
||||||
if (128 * bottomLayer.layer->mapSize().y() == y)
|
if (tile_size.y() * bottomLayer.layer->mapSize().y() == y)
|
||||||
y = 0;
|
y = 0;
|
||||||
SDL_RenderPresent(sdl_renderer.get());
|
SDL_RenderPresent(sdl_renderer.get());
|
||||||
|
|
||||||
|
@ -145,7 +148,7 @@ namespace {
|
||||||
|
|
||||||
void addLayer (dk::Tyler<2>& parTiler, LayerWithData<dkh::AsciiMapSource, int>& parLayerInfo, const char* parPath) {
|
void addLayer (dk::Tyler<2>& parTiler, LayerWithData<dkh::AsciiMapSource, int>& parLayerInfo, const char* parPath) {
|
||||||
parLayerInfo.path = parPath;
|
parLayerInfo.path = parPath;
|
||||||
parLayerInfo.device = std::unique_ptr<dkh::AsciiMapSource>(new dkh::AsciiMapSource(parLayerInfo.path, dkh::AsciiMapSource::coords(10, 6)));
|
parLayerInfo.device = std::unique_ptr<dkh::AsciiMapSource>(new dkh::AsciiMapSource(parLayerInfo.path, dkh::AsciiMapSource::coords(10, 8)));
|
||||||
std::unique_ptr<dk::MapStreamRaw<int, 2>> stream(new dk::MapStreamRaw<int, 2>(*parLayerInfo.device));
|
std::unique_ptr<dk::MapStreamRaw<int, 2>> stream(new dk::MapStreamRaw<int, 2>(*parLayerInfo.device));
|
||||||
parLayerInfo.tilemap = std::unique_ptr<dk::TileMapData<int, 2>>(new dk::TileMapData<int, 2>(std::move(stream)));
|
parLayerInfo.tilemap = std::unique_ptr<dk::TileMapData<int, 2>>(new dk::TileMapData<int, 2>(std::move(stream)));
|
||||||
parLayerInfo.layer = &parTiler.push_layer(*parLayerInfo.tilemap);
|
parLayerInfo.layer = &parTiler.push_layer(*parLayerInfo.tilemap);
|
||||||
|
@ -211,26 +214,28 @@ namespace {
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_tiles (SDL_Renderer* parRenderer, SDL_Texture* parTile0, SDL_Texture* parTile1, const dk::Viewport<2>& parView, const dk::Layer<int, 2>& parLayer, int parYOffs) {
|
void draw_tiles (SDL_Renderer* parRenderer, SDL_Texture* parTile0, SDL_Texture* parTile1, const dk::Viewport<2>& parView, const dk::Layer<int, 2>& parLayer, int parYOffs) {
|
||||||
|
typedef dk::Tyler<2>::coords coords2;
|
||||||
SDL_Rect rect_src;
|
SDL_Rect rect_src;
|
||||||
rect_src.x = rect_src.y = 0;
|
rect_src.x = rect_src.y = 0;
|
||||||
SDL_QueryTexture(parTile0, nullptr, nullptr, &rect_src.w, &rect_src.h);
|
coords2 original_size;
|
||||||
|
SDL_QueryTexture(parTile0, nullptr, nullptr, &original_size.x(), &original_size.y());
|
||||||
|
rect_src.w = original_size.x();
|
||||||
|
rect_src.h = original_size.y();
|
||||||
|
const coords2 tilesize(original_size.x(), original_size.y() - 47);
|
||||||
|
const int offsets[2] = {5, 0};
|
||||||
|
|
||||||
int col = 0;
|
|
||||||
int row = 0;
|
|
||||||
const auto tilecount = parView.count();
|
|
||||||
for (auto itTile = parView.begin(parLayer), itTileEND = parView.end(parLayer); itTile != itTileEND; ++itTile) {
|
for (auto itTile = parView.begin(parLayer), itTileEND = parView.end(parLayer); itTile != itTileEND; ++itTile) {
|
||||||
if (col == tilecount.x()) {
|
|
||||||
col = 0;
|
|
||||||
++row;
|
|
||||||
}
|
|
||||||
SDL_Rect rect_dst;
|
SDL_Rect rect_dst;
|
||||||
rect_dst.x = col * rect_src.w;
|
const auto pixel_pos = dkh::get_diamond_coordinates(itTile, tilesize);
|
||||||
rect_dst.y = row * rect_src.h + parYOffs;
|
rect_src.y = offsets[*itTile];
|
||||||
|
rect_src.h = original_size.y() - offsets[*itTile];
|
||||||
|
|
||||||
|
rect_dst.x = pixel_pos.x();
|
||||||
|
rect_dst.y = pixel_pos.y() + parYOffs;
|
||||||
rect_dst.w = rect_src.w;
|
rect_dst.w = rect_src.w;
|
||||||
rect_dst.h = rect_src.h;
|
rect_dst.h = rect_src.h;
|
||||||
SDL_Texture* const curr_texture = (1 == *itTile ? parTile1 : parTile0);
|
SDL_Texture* const curr_texture = (1 == *itTile ? parTile1 : parTile0);
|
||||||
SDL_RenderCopy(parRenderer, curr_texture, &rect_src, &rect_dst);
|
SDL_RenderCopy(parRenderer, curr_texture, &rect_src, &rect_dst);
|
||||||
++col;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} //unnamed namespace
|
} //unnamed namespace
|
||||||
|
|
Loading…
Reference in a new issue