Load and display actual tiles

This commit is contained in:
King_DuckZ 2016-10-27 00:06:59 +02:00
parent b5a6b9f9bc
commit b8840045a5
12 changed files with 114 additions and 18 deletions

View file

@ -15,6 +15,8 @@ set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${common_gcc_flags}")
option(CURRY_FORCE_OPENGLES "Try to chose the openGL ES renderer if available. Enable this on Raspberry Pi" OFF) option(CURRY_FORCE_OPENGLES "Try to chose the openGL ES renderer if available. Enable this on Raspberry Pi" OFF)
option(CURRY_RASPBERRY_PI "Compile for Raspberry Pi" OFF) option(CURRY_RASPBERRY_PI "Compile for Raspberry Pi" OFF)
set(MYCURRY_RESOURCES_PATH "${CMAKE_CURRENT_SOURCE_DIR}" CACHE STRING "Path to the program's resources")
target_architecture(TARGET_ARCH) target_architecture(TARGET_ARCH)
message (STATUS "Target architecture: ${TARGET_ARCH}") message (STATUS "Target architecture: ${TARGET_ARCH}")
@ -33,6 +35,7 @@ if (CURRY_FORCE_OPENGLES OR CURRY_RASPBERRY_PI)
endif (CURRY_FORCE_OPENGLES OR CURRY_RASPBERRY_PI) endif (CURRY_FORCE_OPENGLES OR CURRY_RASPBERRY_PI)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2) PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2IMAGE REQUIRED SDL2_image>=2.0.0)
find_package(PNG REQUIRED) find_package(PNG REQUIRED)
find_package(Boost 1.55.0 REQUIRED) find_package(Boost 1.55.0 REQUIRED)
@ -59,10 +62,12 @@ add_executable(${PROJECT_NAME}
src/worldviewport.cpp src/worldviewport.cpp
src/inputbag.cpp src/inputbag.cpp
src/tileiterator.cpp src/tileiterator.cpp
src/texture.cpp
) )
target_include_directories(${PROJECT_NAME} SYSTEM target_include_directories(${PROJECT_NAME} SYSTEM
PRIVATE ${SDL2_INCLUDE_DIR} PRIVATE ${SDL2_INCLUDE_DIR}
PRIVATE ${SDL2IMAGE_INCLUDE_DIRS}
PRIVATE ${PNG_INCLUDE_DIRS} PRIVATE ${PNG_INCLUDE_DIRS}
PRIVATE ${Boost_INCLUDE_DIRS} PRIVATE ${Boost_INCLUDE_DIRS}
PRIVATE lib/tree-2.81/src PRIVATE lib/tree-2.81/src
@ -71,10 +76,12 @@ target_include_directories(${PROJECT_NAME}
PRIVATE src PRIVATE src
PRIVATE lib/vectorwrapper/include PRIVATE lib/vectorwrapper/include
PRIVATE lib/DeathHandler PRIVATE lib/DeathHandler
PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
) )
target_link_libraries(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME}
PRIVATE ${SDL2_LIBRARIES} PRIVATE ${SDL2_LIBRARIES}
PRIVATE ${SDL2IMAGE_LIBRARIES}
PRIVATE ${PNG_LIBRARIES} PRIVATE ${PNG_LIBRARIES}
) )
@ -101,3 +108,4 @@ target_compile_definitions(${PROJECT_NAME}
PRIVATE ${PNG_DEFINITIONS} PRIVATE ${PNG_DEFINITIONS}
) )
configure_file(src/${PROJECT_NAME}Config.h.in ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.h)

View file

@ -1,31 +1,38 @@
#include "ingamescene.hpp" #include "ingamescene.hpp"
#include "sdlmain.hpp" #include "sdlmain.hpp"
#include <iostream> #include "mycurryConfig.h"
#include <SDL2/SDL.h>
namespace curry { namespace curry {
IngameScene::IngameScene (cloonel::SDLMain* parSDLMain) : IngameScene::IngameScene (cloonel::SDLMain* parSDLMain) :
GameSceneBase(parSDLMain), GameSceneBase(parSDLMain),
m_world(vec2us(64), vec2us(100)), m_world(vec2us(96), vec2us(100)),
m_viewport(&m_world, vec2i(parSDLMain->WidthHeight())) m_viewport(&m_world, vec2i(parSDLMain->WidthHeight()))
{ {
m_viewport.set_position(vec2i(38)); //m_viewport.set_position(vec2i(38));
} }
IngameScene::~IngameScene() noexcept = default; IngameScene::~IngameScene() noexcept = default;
void IngameScene::on_prepare() { void IngameScene::on_prepare() {
std::cout << "game prepare\n"; m_worldtiles.load(RESOURCES_PATH "textures.png", *this->sdl_main());
for (auto& tile : m_viewport) {
}
} }
void IngameScene::on_update() { void IngameScene::on_update() {
set_wants_to_quit(); const auto tilesize(m_world.tile_size());
std::cout << "game exec\n"; for (auto tile : m_viewport) {
vec2us idx(tile.index & 1, (tile.index >> 1) & 1);
vec2us src_rect_xy = idx * tilesize;
//std::cout << "Drawing src " << src_rect_xy << " dst " << tile_it.coordinates() << '\n';
SDL_Rect src{src_rect_xy.x(), src_rect_xy.y(), tilesize.x(), tilesize.y()};
SDL_Rect dst{tile.pixel_pos.x(), tile.pixel_pos.y(), tilesize.x(), tilesize.y()};
SDL_RenderCopy(this->sdl_main()->GetRenderer(), m_worldtiles.texture(), &src, &dst);
}
SDL_RenderPresent(this->sdl_main()->GetRenderer());
} }
void IngameScene::on_destroy() noexcept { void IngameScene::on_destroy() noexcept {
std::cout << "game destroy\n"; m_worldtiles.unload();
} }
} //namespace curry } //namespace curry

View file

@ -3,6 +3,7 @@
#include "gamescenebase.hpp" #include "gamescenebase.hpp"
#include "worldgrid.hpp" #include "worldgrid.hpp"
#include "worldviewport.hpp" #include "worldviewport.hpp"
#include "texture.hpp"
namespace cloonel { namespace cloonel {
class SDLMain; class SDLMain;
@ -22,5 +23,6 @@ namespace curry {
private: private:
WorldGrid m_world; WorldGrid m_world;
WorldViewport m_viewport; WorldViewport m_viewport;
Texture m_worldtiles;
}; };
} //namespace curry } //namespace curry

3
src/mycurryConfig.h.in Normal file
View file

@ -0,0 +1,3 @@
#pragma once
#define RESOURCES_PATH "@MYCURRY_RESOURCES_PATH@/"

33
src/texture.cpp Normal file
View file

@ -0,0 +1,33 @@
#include "texture.hpp"
#include "sdlmain.hpp"
#include <SDL_image.h>
namespace curry {
Texture::Texture() :
m_texture(nullptr, &SDL_DestroyTexture)
{
}
Texture::Texture (const char* parPath, cloonel::SDLMain& parSDLMain) :
Texture()
{
this->load(parPath, parSDLMain);
}
Texture::~Texture() noexcept = default;
SDL_Texture* Texture::texture() {
return m_texture.get();
}
void Texture::load (const char* parPath, cloonel::SDLMain& parSDLMain) {
using SurfaceType = std::unique_ptr<SDL_Surface, void(*)(SDL_Surface*)>;
SurfaceType surface(IMG_Load(parPath), &SDL_FreeSurface);
m_texture.reset(SDL_CreateTextureFromSurface(parSDLMain.GetRenderer(), surface.get()));
}
void Texture::unload() noexcept {
m_texture.reset(nullptr);
}
} //namespace curry

25
src/texture.hpp Normal file
View file

@ -0,0 +1,25 @@
#pragma once
#include <memory>
struct SDL_Texture;
namespace cloonel {
class SDLMain;
} //namespace cloonel
namespace curry {
class Texture {
public:
Texture();
Texture (const char* parPath, cloonel::SDLMain& parSDLMain);
~Texture() noexcept;
void load (const char* parPath, cloonel::SDLMain& parSDLMain);
void unload() noexcept;
SDL_Texture* texture();
private:
std::unique_ptr<SDL_Texture, void(*)(SDL_Texture*)> m_texture;
};
} //namespace curry

View file

@ -10,6 +10,7 @@ namespace curry {
m_index(parEnd ? -1 : 0) m_index(parEnd ? -1 : 0)
{ {
assert(m_viewport); assert(m_viewport);
m_pixel_pos = m_viewport->position();
} }
void TileIterator::increment() { void TileIterator::increment() {
@ -29,13 +30,17 @@ namespace curry {
else { else {
m_index = vec2i(-1); m_index = vec2i(-1);
} }
std::cout << "got index " << m_index << " pixel " << //std::cout << "got index " << m_index << " pixel " <<
m_index * tile_size - m_viewport->position() << '\n'; //m_index * tile_size - m_viewport->position() << '\n';
m_pixel_pos = tile_size * m_index - m_viewport->position();
} }
Tile& TileIterator::dereference() const { ScreenTile TileIterator::dereference() const {
static int mic = 10; auto index = static_cast<vec2us>(m_index);
return mic; return ScreenTile {
m_viewport->world()->tile(index),
m_pixel_pos
};
} }
bool TileIterator::equal (const TileIterator& parOther) const { bool TileIterator::equal (const TileIterator& parOther) const {

View file

@ -8,18 +8,24 @@ namespace curry {
typedef int Tile; typedef int Tile;
class WorldViewport; class WorldViewport;
class TileIterator : public boost::iterator_facade<TileIterator, Tile, boost::bidirectional_traversal_tag> { struct ScreenTile {
typedef boost::iterator_facade<TileIterator, Tile, boost::bidirectional_traversal_tag> base_class; Tile index;
vec2us pixel_pos;
};
class TileIterator : public boost::iterator_facade<TileIterator, Tile, boost::bidirectional_traversal_tag, ScreenTile> {
typedef boost::iterator_facade<TileIterator, Tile, boost::bidirectional_traversal_tag, ScreenTile> base_class;
friend class boost::iterator_core_access; friend class boost::iterator_core_access;
public: public:
TileIterator (WorldViewport* parViewport, bool parEnd); TileIterator (WorldViewport* parViewport, bool parEnd);
private: private:
Tile& dereference() const; ScreenTile dereference() const;
void increment(); void increment();
bool equal (const TileIterator& parOther) const; bool equal (const TileIterator& parOther) const;
WorldViewport* m_viewport; WorldViewport* m_viewport;
vec2i m_index; vec2i m_index;
vec2i m_pixel_pos;
}; };
} //namespace curry } //namespace curry

View file

@ -10,4 +10,8 @@ namespace curry {
const vec2us& WorldGrid::tile_size() const { const vec2us& WorldGrid::tile_size() const {
return m_tile_size; return m_tile_size;
} }
WorldGrid::TileIndex WorldGrid::tile (const vec2us& parIndex) const {
return 0;
}
} //namespace curry } //namespace curry

View file

@ -5,9 +5,12 @@
namespace curry { namespace curry {
class WorldGrid { class WorldGrid {
public: public:
typedef int TileIndex;
WorldGrid (vec2us parTileSize, vec2us parWorldSize); WorldGrid (vec2us parTileSize, vec2us parWorldSize);
const vec2us& tile_size() const; const vec2us& tile_size() const;
TileIndex tile (const vec2us& parIndex) const;
private: private:
vec2us m_tile_size; vec2us m_tile_size;

BIN
textures.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

BIN
textures.xcf Normal file

Binary file not shown.