Show diamond-shaped tiles on a larger map.

This commit is contained in:
King_DuckZ 2015-05-24 17:19:10 +02:00
parent edbb3fe1d8
commit 1c5cb6299c
8 changed files with 36 additions and 25 deletions

View File

@ -8,6 +8,7 @@
#include <vector>
#include <type_traits>
#include <cstdint>
#include <cassert>
namespace dk {
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 ( void ) = default;
const coords& position ( void ) const { return m_pos; }
const coords& abs_position ( void ) const { return m_pos; }
private:
void increment ( void );

View File

@ -52,5 +52,8 @@ namespace dk {
void Layer<T, D>::onPreload (const coords& parFrom, const coords& parTo) {
m_tiles.clear();
m_tilemap.fetch(m_tiles, parFrom, parTo);
#if !defined(NDEBUG)
std::cout << "Preloading layer from " << parFrom << " to " << parTo << '\n';
#endif
}
}

View File

@ -1,6 +1,8 @@
1101111011
1100011000
1111011110
1111000110
0000010111
1111110111
1101111001
1101111001
1110111100
1110111000
1111011011
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

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

BIN
test/graphics/tile_1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

View File

@ -3,6 +3,7 @@
#include "platform.h"
#include "platformstrings.h"
#include "doorkeeper/helpers/asciimapsource.hpp"
#include "doorkeeper/helpers/tilecoordinates.hpp"
#include "doorkeeper/mapreaders/mapstreamraw.hpp"
#include <iostream>
#include <fstream>
@ -64,7 +65,7 @@ int main() {
&SDL_DestroyRenderer
);
dk::Tyler<2> tiler(coords2(10, 6), coords2(64));
dk::Tyler<2> tiler(coords2(10, 8), coords2(64));
LayerWithData<AsciiMapSource, int> bottomLayer;
addLayer(tiler, bottomLayer, DATA_PATH"/test.map");
@ -90,13 +91,15 @@ int main() {
//Main loop
bool running = true;
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 {
SDL_RenderClear(sdl_renderer.get());
viewport.setFrom(coords2(0, y / 128));
draw_tiles(sdl_renderer.get(), tile_0.get(), tile_1.get(), viewport, *bottomLayer.layer, -(y % 128));
++y;
if (128 * bottomLayer.layer->mapSize().y() == y)
viewport.setFrom(coords2(0, y / tile_size.y()));
draw_tiles(sdl_renderer.get(), tile_0.get(), tile_1.get(), viewport, *bottomLayer.layer, -(y % tile_size.y()));
//++y;
if (tile_size.y() * bottomLayer.layer->mapSize().y() == y)
y = 0;
SDL_RenderPresent(sdl_renderer.get());
@ -145,7 +148,7 @@ namespace {
void addLayer (dk::Tyler<2>& parTiler, LayerWithData<dkh::AsciiMapSource, int>& parLayerInfo, const char* 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));
parLayerInfo.tilemap = std::unique_ptr<dk::TileMapData<int, 2>>(new dk::TileMapData<int, 2>(std::move(stream)));
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) {
typedef dk::Tyler<2>::coords coords2;
SDL_Rect rect_src;
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) {
if (col == tilecount.x()) {
col = 0;
++row;
}
SDL_Rect rect_dst;
rect_dst.x = col * rect_src.w;
rect_dst.y = row * rect_src.h + parYOffs;
const auto pixel_pos = dkh::get_diamond_coordinates(itTile, tilesize);
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.h = rect_src.h;
SDL_Texture* const curr_texture = (1 == *itTile ? parTile1 : parTile0);
SDL_RenderCopy(parRenderer, curr_texture, &rect_src, &rect_dst);
++col;
}
}
} //unnamed namespace