Make and SDL2 window and try to load some textures, untested

This commit is contained in:
King_DuckZ 2015-05-21 09:54:59 +02:00
parent 2d61a84c13
commit bb7de4f4dd
2 changed files with 123 additions and 0 deletions

View file

@ -1,6 +1,9 @@
cmake_minimum_required(VERSION 2.8 FATAL_ERROR) cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(doorkeepertest CXX) project(doorkeepertest CXX)
include(FindPkgConfig)
pkg_search_module(SDL2 REQUIRED sdl2 image)
pkg_search_module(SDL2IMAGE REQUIRED SDL2_image>=2.0.0)
configure_file( configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.h.in" "${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.h" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.h"
@ -9,6 +12,7 @@ configure_file(
include_directories( include_directories(
. .
"${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_BINARY_DIR}"
${SDL2_INCLUDE_DIR}
) )
add_executable(${PROJECT_NAME} add_executable(${PROJECT_NAME}
@ -18,4 +22,5 @@ add_executable(${PROJECT_NAME}
target_link_libraries(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME}
doorkeeper doorkeeper
${SDL2_LIBRARIES}
) )

View file

@ -8,6 +8,12 @@
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <memory> #include <memory>
#include <ciso646>
#include <utility>
#include <cstring>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
template <typename Device, typename Tile> template <typename Device, typename Tile>
struct LayerWithData { struct LayerWithData {
@ -18,11 +24,23 @@ struct LayerWithData {
}; };
namespace { namespace {
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_Surface, void(*)(SDL_Surface*)> SDLSurfaceUPtr;
typedef std::unique_ptr<SDL_Texture, void(*)(SDL_Texture*)> SDLTextureUPtr;
struct SDLSimple {
SDLSimple ( void );
~SDLSimple ( void ) noexcept;
bool initialized;
};
template <typename Device, typename Tile> template <typename Device, typename Tile>
void createLayer ( dk::Tyler<2>& parTiler, LayerWithData<Device, Tile>& parOut ); void createLayer ( dk::Tyler<2>& parTiler, LayerWithData<Device, Tile>& parOut );
void printWelcome ( void ); void printWelcome ( void );
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 );
void printViewport ( const dk::Viewport<2>& parView, const dk::Layer<int, 2>& parLayer ); void printViewport ( const dk::Viewport<2>& parView, const dk::Layer<int, 2>& parLayer );
std::pair<int, std::string> GetRenderingDriver ( void );
} //unnamed namespace } //unnamed namespace
int main() { int main() {
@ -30,8 +48,22 @@ int main() {
using dk::TileMapData; using dk::TileMapData;
using dkh::AsciiMapSource; using dkh::AsciiMapSource;
SDLSimple sdl_init;
if (not sdl_init.initialized)
return 1;
printWelcome(); printWelcome();
SDLWindowUPtr sdl_window(
SDL_CreateWindow("DoorKeeper test", 100, 100, 320, 240, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
&SDL_DestroyWindow
);
const auto rendererDriver = GetRenderingDriver();
SDLRendererUPtr sdl_renderer(
SDL_CreateRenderer(sdl_window.get(), rendererDriver.first, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
&SDL_DestroyRenderer
);
dk::Tyler<2> tiler(coords2(10, 6), coords2(64)); dk::Tyler<2> tiler(coords2(10, 6), coords2(64));
LayerWithData<AsciiMapSource, int> bottomLayer; LayerWithData<AsciiMapSource, int> bottomLayer;
@ -47,6 +79,46 @@ int main() {
std::cout << "Map size: " << tiler.map_size() << '\n'; std::cout << "Map size: " << tiler.map_size() << '\n';
#endif #endif
std::cout << "Total tiles: " << tiler.tiles_count() << '\n'; std::cout << "Total tiles: " << tiler.tiles_count() << '\n';
//Load resources
SDLTextureUPtr tile_0, tile_1;
{
SDLSurfaceUPtr surf_0(IMG_Load("tile_0.png"), &SDL_FreeSurface);
SDLSurfaceUPtr surf_1(IMG_Load("tile_1.png"), &SDL_FreeSurface);
tile_0 = SDLTextureUPtr(SDL_CreateTextureFromSurface(sdl_renderer.get(), surf_0.get()), &SDL_DestroyTexture);
tile_1 = SDLTextureUPtr(SDL_CreateTextureFromSurface(sdl_renderer.get(), surf_1.get()), &SDL_DestroyTexture);
}
//Main loop
bool running = true;
do {
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);
return 0; return 0;
} }
@ -83,4 +155,50 @@ namespace {
} }
} }
} }
SDLSimple::SDLSimple() {
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
std::cerr << "Error initializing SDL" << std::endl;
initialized = false;
}
else {
initialized = true;
}
}
SDLSimple::~SDLSimple() {
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");
}
} //unnamed namespace } //unnamed namespace