Draw tiles.

This commit is contained in:
Michele Santullo 2015-05-22 14:50:10 +02:00
parent a6eadb9c30
commit a9630341c7

View file

@ -40,6 +40,7 @@ namespace {
void addLayer ( dk::Tyler<2>& parTiler, LayerWithData<dkh::AsciiMapSource, int>& parLayerInfo, const char* parPath );
void printViewport ( const dk::Viewport<2>& parView, const dk::Layer<int, 2>& parLayer );
std::pair<int, std::string> GetRenderingDriver ( void );
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 );
} //unnamed namespace
int main() {
@ -54,7 +55,7 @@ int main() {
printWelcome();
SDLWindowUPtr sdl_window(
SDL_CreateWindow("DoorKeeper test", 100, 100, 320, 240, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
SDL_CreateWindow("DoorKeeper test", 100, 100, 128 * 6, 128 * 4, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
&SDL_DestroyWindow
);
const auto rendererDriver = GetRenderingDriver();
@ -88,8 +89,18 @@ int main() {
//Main loop
bool running = true;
int y = 0;
dk::Viewport<2> viewport(tiler, coords2(6, 4), coords2(0));
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)
y = 0;
SDL_RenderPresent(sdl_renderer.get());
SDL_Delay(1000 / 60);
SDL_Event eve;
while (SDL_PollEvent(&eve)) {
switch (eve.type) {
@ -198,4 +209,28 @@ namespace {
return RetPairType(-1, "default");
}
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) {
SDL_Rect rect_src;
rect_src.x = rect_src.y = 0;
SDL_QueryTexture(parTile0, nullptr, nullptr, &rect_src.w, &rect_src.h);
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;
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