diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c6b63a..21c4018 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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_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) 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) PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2) +PKG_SEARCH_MODULE(SDL2IMAGE REQUIRED SDL2_image>=2.0.0) find_package(PNG REQUIRED) find_package(Boost 1.55.0 REQUIRED) @@ -59,10 +62,12 @@ add_executable(${PROJECT_NAME} src/worldviewport.cpp src/inputbag.cpp src/tileiterator.cpp + src/texture.cpp ) target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE ${SDL2_INCLUDE_DIR} + PRIVATE ${SDL2IMAGE_INCLUDE_DIRS} PRIVATE ${PNG_INCLUDE_DIRS} PRIVATE ${Boost_INCLUDE_DIRS} PRIVATE lib/tree-2.81/src @@ -71,10 +76,12 @@ target_include_directories(${PROJECT_NAME} PRIVATE src PRIVATE lib/vectorwrapper/include PRIVATE lib/DeathHandler + PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ) target_link_libraries(${PROJECT_NAME} PRIVATE ${SDL2_LIBRARIES} + PRIVATE ${SDL2IMAGE_LIBRARIES} PRIVATE ${PNG_LIBRARIES} ) @@ -101,3 +108,4 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE ${PNG_DEFINITIONS} ) +configure_file(src/${PROJECT_NAME}Config.h.in ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.h) diff --git a/src/ingamescene.cpp b/src/ingamescene.cpp index 5cb57b6..b3350d1 100644 --- a/src/ingamescene.cpp +++ b/src/ingamescene.cpp @@ -1,31 +1,38 @@ #include "ingamescene.hpp" #include "sdlmain.hpp" -#include +#include "mycurryConfig.h" +#include namespace curry { IngameScene::IngameScene (cloonel::SDLMain* parSDLMain) : GameSceneBase(parSDLMain), - m_world(vec2us(64), vec2us(100)), + m_world(vec2us(96), vec2us(100)), m_viewport(&m_world, vec2i(parSDLMain->WidthHeight())) { - m_viewport.set_position(vec2i(38)); + //m_viewport.set_position(vec2i(38)); } IngameScene::~IngameScene() noexcept = default; void IngameScene::on_prepare() { - std::cout << "game prepare\n"; - - for (auto& tile : m_viewport) { - } + m_worldtiles.load(RESOURCES_PATH "textures.png", *this->sdl_main()); } void IngameScene::on_update() { - set_wants_to_quit(); - std::cout << "game exec\n"; + const auto tilesize(m_world.tile_size()); + 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 { - std::cout << "game destroy\n"; + m_worldtiles.unload(); } } //namespace curry diff --git a/src/ingamescene.hpp b/src/ingamescene.hpp index 89629df..1244409 100644 --- a/src/ingamescene.hpp +++ b/src/ingamescene.hpp @@ -3,6 +3,7 @@ #include "gamescenebase.hpp" #include "worldgrid.hpp" #include "worldviewport.hpp" +#include "texture.hpp" namespace cloonel { class SDLMain; @@ -22,5 +23,6 @@ namespace curry { private: WorldGrid m_world; WorldViewport m_viewport; + Texture m_worldtiles; }; } //namespace curry diff --git a/src/mycurryConfig.h.in b/src/mycurryConfig.h.in new file mode 100644 index 0000000..48e9e0f --- /dev/null +++ b/src/mycurryConfig.h.in @@ -0,0 +1,3 @@ +#pragma once + +#define RESOURCES_PATH "@MYCURRY_RESOURCES_PATH@/" diff --git a/src/texture.cpp b/src/texture.cpp new file mode 100644 index 0000000..b510e17 --- /dev/null +++ b/src/texture.cpp @@ -0,0 +1,33 @@ +#include "texture.hpp" +#include "sdlmain.hpp" +#include + +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; + + 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 diff --git a/src/texture.hpp b/src/texture.hpp new file mode 100644 index 0000000..6b13f4c --- /dev/null +++ b/src/texture.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include + +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 m_texture; + }; +} //namespace curry diff --git a/src/tileiterator.cpp b/src/tileiterator.cpp index aaeea84..1c61f8a 100644 --- a/src/tileiterator.cpp +++ b/src/tileiterator.cpp @@ -10,6 +10,7 @@ namespace curry { m_index(parEnd ? -1 : 0) { assert(m_viewport); + m_pixel_pos = m_viewport->position(); } void TileIterator::increment() { @@ -29,13 +30,17 @@ namespace curry { else { m_index = vec2i(-1); } - std::cout << "got index " << m_index << " pixel " << - m_index * tile_size - m_viewport->position() << '\n'; + //std::cout << "got index " << m_index << " pixel " << + //m_index * tile_size - m_viewport->position() << '\n'; + m_pixel_pos = tile_size * m_index - m_viewport->position(); } - Tile& TileIterator::dereference() const { - static int mic = 10; - return mic; + ScreenTile TileIterator::dereference() const { + auto index = static_cast(m_index); + return ScreenTile { + m_viewport->world()->tile(index), + m_pixel_pos + }; } bool TileIterator::equal (const TileIterator& parOther) const { diff --git a/src/tileiterator.hpp b/src/tileiterator.hpp index e2e1e80..3e4148e 100644 --- a/src/tileiterator.hpp +++ b/src/tileiterator.hpp @@ -8,18 +8,24 @@ namespace curry { typedef int Tile; class WorldViewport; - class TileIterator : public boost::iterator_facade { - typedef boost::iterator_facade base_class; + struct ScreenTile { + Tile index; + vec2us pixel_pos; + }; + + class TileIterator : public boost::iterator_facade { + typedef boost::iterator_facade base_class; friend class boost::iterator_core_access; public: TileIterator (WorldViewport* parViewport, bool parEnd); private: - Tile& dereference() const; + ScreenTile dereference() const; void increment(); bool equal (const TileIterator& parOther) const; WorldViewport* m_viewport; vec2i m_index; + vec2i m_pixel_pos; }; } //namespace curry diff --git a/src/worldgrid.cpp b/src/worldgrid.cpp index 0e1971b..fcaa546 100644 --- a/src/worldgrid.cpp +++ b/src/worldgrid.cpp @@ -10,4 +10,8 @@ namespace curry { const vec2us& WorldGrid::tile_size() const { return m_tile_size; } + + WorldGrid::TileIndex WorldGrid::tile (const vec2us& parIndex) const { + return 0; + } } //namespace curry diff --git a/src/worldgrid.hpp b/src/worldgrid.hpp index 8c379f6..889a67b 100644 --- a/src/worldgrid.hpp +++ b/src/worldgrid.hpp @@ -5,9 +5,12 @@ namespace curry { class WorldGrid { public: + typedef int TileIndex; + WorldGrid (vec2us parTileSize, vec2us parWorldSize); const vec2us& tile_size() const; + TileIndex tile (const vec2us& parIndex) const; private: vec2us m_tile_size; diff --git a/textures.png b/textures.png new file mode 100644 index 0000000..f936b93 Binary files /dev/null and b/textures.png differ diff --git a/textures.xcf b/textures.xcf new file mode 100644 index 0000000..1fa1754 Binary files /dev/null and b/textures.xcf differ