2015-08-19 19:06:58 +00:00
|
|
|
/* Copyright 2015, Michele Santullo
|
|
|
|
* This file is part of DoorKeeper.
|
|
|
|
*
|
|
|
|
* DoorKeeper is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* DoorKeeper is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with DoorKeeper. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-01-06 15:38:52 +00:00
|
|
|
#include "doorkeeper/doorkeeper.hpp"
|
2014-12-29 15:17:59 +00:00
|
|
|
#include "doorkeepertestConfig.h"
|
2014-12-15 22:24:00 +00:00
|
|
|
#include "platform.h"
|
|
|
|
#include "platformstrings.h"
|
2015-01-06 15:38:52 +00:00
|
|
|
#include "doorkeeper/helpers/asciimapsource.hpp"
|
2015-08-17 13:39:28 +00:00
|
|
|
#include "doorkeeper/components/pixelconv.hpp"
|
2015-06-06 12:14:00 +00:00
|
|
|
#include "doorkeeper/helpers/maploader.hpp"
|
2014-08-27 14:03:56 +00:00
|
|
|
#include <iostream>
|
2014-12-09 22:53:09 +00:00
|
|
|
#include <fstream>
|
2014-12-29 15:17:59 +00:00
|
|
|
#include <string>
|
2014-12-09 22:53:09 +00:00
|
|
|
#include <memory>
|
2015-05-21 07:54:59 +00:00
|
|
|
#include <ciso646>
|
|
|
|
#include <utility>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
#include <SDL2/SDL_image.h>
|
2014-08-27 14:03:56 +00:00
|
|
|
|
2015-08-20 17:24:02 +00:00
|
|
|
namespace {
|
|
|
|
template <typename T, typename T1>
|
|
|
|
inline
|
|
|
|
dk::Vector<2> get_diamond_coordinates (const dk::TileIterator<T, 2, T1>& parIterator, const dk::Vector<2>& parSize) {
|
|
|
|
dk::PixelConvDiamond pconv(parSize, true, false);
|
|
|
|
return pconv.to_pixel(parIterator->block_position());
|
|
|
|
}
|
|
|
|
} //unnamed namespace
|
|
|
|
|
2014-12-29 15:17:59 +00:00
|
|
|
template <typename Device, typename Tile>
|
|
|
|
struct LayerWithData {
|
2015-05-24 23:37:20 +00:00
|
|
|
std::unique_ptr<dkh::AsciiMapSource> device;
|
2014-12-29 15:17:59 +00:00
|
|
|
dk::Layer<Tile, 2>* layer;
|
|
|
|
std::string path;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
2015-05-21 07:54:59 +00:00
|
|
|
typedef std::unique_ptr<SDL_Renderer, void(*)(SDL_Renderer*)> SDLRendererUPtr;
|
|
|
|
typedef std::unique_ptr<SDL_Window, void(*)(SDL_Window*)> SDLWindowUPtr;
|
|
|
|
typedef std::unique_ptr<SDL_Texture, void(*)(SDL_Texture*)> SDLTextureUPtr;
|
|
|
|
|
|
|
|
struct SDLSimple {
|
|
|
|
SDLSimple ( void );
|
|
|
|
~SDLSimple ( void ) noexcept;
|
|
|
|
bool initialized;
|
|
|
|
};
|
|
|
|
|
2014-12-29 15:17:59 +00:00
|
|
|
void printWelcome ( void );
|
2015-01-05 17:01:28 +00:00
|
|
|
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 );
|
2015-05-21 07:54:59 +00:00
|
|
|
std::pair<int, std::string> GetRenderingDriver ( void );
|
2015-05-22 12:50:10 +00:00
|
|
|
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 );
|
2014-12-29 15:17:59 +00:00
|
|
|
} //unnamed namespace
|
|
|
|
|
2014-08-27 14:03:56 +00:00
|
|
|
int main() {
|
2014-12-29 15:17:59 +00:00
|
|
|
typedef dk::Tyler<2>::coords coords2;
|
|
|
|
using dkh::AsciiMapSource;
|
2014-12-09 21:30:06 +00:00
|
|
|
|
2015-05-21 07:54:59 +00:00
|
|
|
SDLSimple sdl_init;
|
|
|
|
if (not sdl_init.initialized)
|
|
|
|
return 1;
|
|
|
|
|
2014-12-29 15:17:59 +00:00
|
|
|
printWelcome();
|
2014-12-15 22:24:00 +00:00
|
|
|
|
2015-05-21 07:54:59 +00:00
|
|
|
SDLWindowUPtr sdl_window(
|
2015-05-22 12:50:10 +00:00
|
|
|
SDL_CreateWindow("DoorKeeper test", 100, 100, 128 * 6, 128 * 4, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
|
2015-05-21 07:54:59 +00:00
|
|
|
&SDL_DestroyWindow
|
|
|
|
);
|
|
|
|
const auto rendererDriver = GetRenderingDriver();
|
|
|
|
SDLRendererUPtr sdl_renderer(
|
|
|
|
SDL_CreateRenderer(sdl_window.get(), rendererDriver.first, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
|
|
|
|
&SDL_DestroyRenderer
|
|
|
|
);
|
|
|
|
|
2015-05-24 23:37:20 +00:00
|
|
|
dk::Tyler<2> tiler(coords2(64));
|
2014-12-12 19:04:48 +00:00
|
|
|
|
2014-12-29 15:17:59 +00:00
|
|
|
LayerWithData<AsciiMapSource, int> bottomLayer;
|
2015-01-05 17:01:28 +00:00
|
|
|
addLayer(tiler, bottomLayer, DATA_PATH"/test.map");
|
2014-12-13 03:50:23 +00:00
|
|
|
|
2014-12-29 15:17:59 +00:00
|
|
|
LayerWithData<AsciiMapSource, int> topLayer;
|
2015-01-05 17:01:28 +00:00
|
|
|
addLayer(tiler, topLayer, DATA_PATH"/test_2.map");
|
2014-12-09 22:53:09 +00:00
|
|
|
|
2015-01-05 17:01:28 +00:00
|
|
|
printViewport(dk::Viewport<2>(tiler, coords2(10, 6), coords2(0)), *bottomLayer.layer);
|
|
|
|
printViewport(dk::Viewport<2>(tiler, coords2(4, 4), coords2(0)), *bottomLayer.layer);
|
2014-12-09 22:53:09 +00:00
|
|
|
|
|
|
|
#if !defined(NDEBUG)
|
|
|
|
std::cout << "Map size: " << tiler.map_size() << '\n';
|
|
|
|
#endif
|
|
|
|
std::cout << "Total tiles: " << tiler.tiles_count() << '\n';
|
2015-05-21 07:54:59 +00:00
|
|
|
|
|
|
|
//Load resources
|
2015-05-21 16:14:07 +00:00
|
|
|
SDLTextureUPtr tile_0(nullptr, &SDL_DestroyTexture), tile_1(nullptr, &SDL_DestroyTexture);
|
2015-05-21 07:54:59 +00:00
|
|
|
{
|
2015-05-21 16:14:07 +00:00
|
|
|
tile_0 = SDLTextureUPtr(IMG_LoadTexture(sdl_renderer.get(), "tile_0.png"), &SDL_DestroyTexture);
|
|
|
|
tile_1 = SDLTextureUPtr(IMG_LoadTexture(sdl_renderer.get(), "tile_1.png"), &SDL_DestroyTexture);
|
2015-05-21 07:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Main loop
|
|
|
|
bool running = true;
|
2015-05-22 12:50:10 +00:00
|
|
|
int y = 0;
|
2015-05-24 15:19:10 +00:00
|
|
|
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());
|
2015-05-21 07:54:59 +00:00
|
|
|
do {
|
2015-05-22 12:50:10 +00:00
|
|
|
SDL_RenderClear(sdl_renderer.get());
|
2015-05-24 15:19:10 +00:00
|
|
|
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)
|
2015-05-22 12:50:10 +00:00
|
|
|
y = 0;
|
|
|
|
SDL_RenderPresent(sdl_renderer.get());
|
|
|
|
|
|
|
|
SDL_Delay(1000 / 60);
|
2015-05-21 07:54:59 +00:00
|
|
|
SDL_Event eve;
|
|
|
|
while (SDL_PollEvent(&eve)) {
|
|
|
|
switch (eve.type) {
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
////eve.key.keysym.sym
|
|
|
|
//parInput.NotifyKeyAction(InputDevice_Keyboard, eve.key.keysym.scancode, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_KEYUP:
|
|
|
|
//parInput.NotifyKeyAction(InputDevice_Keyboard, eve.key.keysym.scancode, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_QUIT:
|
|
|
|
running = false;
|
|
|
|
|
|
|
|
case SDL_WINDOWEVENT:
|
|
|
|
if (SDL_WINDOWEVENT_RESIZED == eve.window.event) {
|
|
|
|
//parSdlMain->SetResolution(ushort2(static_cast<uint16_t>(eve.window.data1), static_cast<uint16_t>(eve.window.data2)));
|
|
|
|
}
|
|
|
|
else if (SDL_WINDOWEVENT_CLOSE == eve.window.event) {
|
|
|
|
running = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while(running);
|
2014-08-27 14:03:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-12-29 15:17:59 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
void printWelcome() {
|
|
|
|
std::cout << "Welcome to " << APP_NAME << ' ' << DK_DEVICE_STRING << " version for " << DK_OS_STRING << ' ' << DK_ARCH_STRING << ' ' << DK_BIT_STRING;
|
|
|
|
#if defined(DK_ARM)
|
|
|
|
std::cout << ' ' << DK_ARM_FAMILY_STRING << ' ' << DK_ARM_ARCH_STRING;
|
|
|
|
#endif
|
|
|
|
std::cout << '\n';
|
|
|
|
}
|
2015-01-05 17:01:28 +00:00
|
|
|
|
|
|
|
void addLayer (dk::Tyler<2>& parTiler, LayerWithData<dkh::AsciiMapSource, int>& parLayerInfo, const char* parPath) {
|
2015-05-27 22:20:40 +00:00
|
|
|
typedef dkh::AsciiMapSource::coords coords;
|
|
|
|
|
2015-01-05 17:01:28 +00:00
|
|
|
parLayerInfo.path = parPath;
|
2015-05-27 22:20:40 +00:00
|
|
|
parLayerInfo.device.reset(new dkh::AsciiMapSource(parLayerInfo.path, coords(10, 8), dk::MapType_IsometricStaggered, coords(64, 64)));
|
2015-06-07 01:57:18 +00:00
|
|
|
parLayerInfo.layer = &parTiler.push_layer<int>(parLayerInfo.device.get(), 0);
|
2015-01-05 17:01:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void printViewport (const dk::Viewport<2>& parView, const dk::Layer<int, 2>& parLayer) {
|
|
|
|
int col = 0;
|
|
|
|
const auto tilecount = parView.count();
|
|
|
|
for (auto itTile = parView.begin(parLayer), itTileEND = parView.end(parLayer); itTile != itTileEND; ++itTile) {
|
2015-08-18 22:15:31 +00:00
|
|
|
std::cout << itTile->data();
|
2015-01-05 17:01:28 +00:00
|
|
|
++col;
|
|
|
|
if (col == tilecount.x()) {
|
|
|
|
col = 0;
|
|
|
|
std::cout << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-21 07:54:59 +00:00
|
|
|
|
|
|
|
SDLSimple::SDLSimple() {
|
|
|
|
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
|
|
|
|
std::cerr << "Error initializing SDL" << std::endl;
|
|
|
|
initialized = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 22:25:06 +00:00
|
|
|
SDLSimple::~SDLSimple() noexcept {
|
2015-05-21 07:54:59 +00:00
|
|
|
if (initialized) {
|
|
|
|
initialized = false;
|
|
|
|
SDL_Quit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<int, std::string> GetRenderingDriver() {
|
|
|
|
typedef std::pair<int, std::string> RetPairType;
|
|
|
|
|
|
|
|
const int count = SDL_GetNumRenderDrivers();
|
|
|
|
int opengles = -1;
|
|
|
|
int opengles2 = -1;
|
|
|
|
int opengl = -1;
|
|
|
|
SDL_RendererInfo info;
|
|
|
|
for (int z = 0; z < count; ++z) {
|
|
|
|
const int ret = SDL_GetRenderDriverInfo(z, &info);
|
|
|
|
if (0 == ret) {
|
|
|
|
if (std::strcmp("opengles", info.name) == 0)
|
|
|
|
opengles = z;
|
|
|
|
else if (std::strcmp("opengles2", info.name) == 0)
|
|
|
|
opengles2 = z;
|
|
|
|
else if (std::strcmp("opengl", info.name) == 0)
|
|
|
|
opengl = z;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (opengl > -1)
|
|
|
|
return RetPairType(opengl, "opengl");
|
|
|
|
if (opengles2 > -1)
|
|
|
|
return RetPairType(opengles2, "opengles2");
|
|
|
|
if (opengles > -1)
|
|
|
|
return RetPairType(opengles, "opengles");
|
|
|
|
|
|
|
|
return RetPairType(-1, "default");
|
|
|
|
}
|
2015-05-22 12:50:10 +00:00
|
|
|
|
|
|
|
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) {
|
2015-05-24 15:19:10 +00:00
|
|
|
typedef dk::Tyler<2>::coords coords2;
|
2015-05-22 12:50:10 +00:00
|
|
|
SDL_Rect rect_src;
|
|
|
|
rect_src.x = rect_src.y = 0;
|
2015-05-24 15:19:10 +00:00
|
|
|
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};
|
2015-05-22 12:50:10 +00:00
|
|
|
|
|
|
|
for (auto itTile = parView.begin(parLayer), itTileEND = parView.end(parLayer); itTile != itTileEND; ++itTile) {
|
|
|
|
SDL_Rect rect_dst;
|
2015-08-20 17:24:02 +00:00
|
|
|
const auto pixel_pos = get_diamond_coordinates(itTile, tilesize);
|
2015-08-18 22:15:31 +00:00
|
|
|
rect_src.y = offsets[itTile->data()];
|
|
|
|
rect_src.h = original_size.y() - offsets[itTile->data()];
|
2015-05-24 15:19:10 +00:00
|
|
|
|
|
|
|
rect_dst.x = pixel_pos.x();
|
|
|
|
rect_dst.y = pixel_pos.y() + parYOffs;
|
2015-05-22 12:50:10 +00:00
|
|
|
rect_dst.w = rect_src.w;
|
|
|
|
rect_dst.h = rect_src.h;
|
2015-08-18 22:15:31 +00:00
|
|
|
SDL_Texture* const curr_texture = (1 == itTile->data() ? parTile1 : parTile0);
|
2015-05-22 12:50:10 +00:00
|
|
|
SDL_RenderCopy(parRenderer, curr_texture, &rect_src, &rect_dst);
|
|
|
|
}
|
|
|
|
}
|
2014-12-29 15:17:59 +00:00
|
|
|
} //unnamed namespace
|