Layers are now named, so code can request a layer by name.
This commit is contained in:
parent
7a391c5743
commit
9428e63fc2
10 changed files with 78 additions and 40 deletions
3
.gitmodules
vendored
3
.gitmodules
vendored
|
@ -10,3 +10,6 @@
|
||||||
[submodule "lib/clooneljump"]
|
[submodule "lib/clooneljump"]
|
||||||
path = lib/clooneljump
|
path = lib/clooneljump
|
||||||
url = https://bitbucket.org/King_DuckZ/clooneljump.git
|
url = https://bitbucket.org/King_DuckZ/clooneljump.git
|
||||||
|
[submodule "lib/better-enums"]
|
||||||
|
path = lib/better-enums
|
||||||
|
url = https://github.com/aantron/better-enums
|
||||||
|
|
|
@ -60,6 +60,7 @@ target_compile_definitions(${PROJECT_NAME}
|
||||||
)
|
)
|
||||||
target_include_directories(${PROJECT_NAME}
|
target_include_directories(${PROJECT_NAME}
|
||||||
INTERFACE lib/kakoune
|
INTERFACE lib/kakoune
|
||||||
|
INTERFACE lib/better-enums
|
||||||
)
|
)
|
||||||
|
|
||||||
add_subdirectory(lib/clooneljump/src/cloonelgraphics)
|
add_subdirectory(lib/clooneljump/src/cloonelgraphics)
|
||||||
|
|
1
lib/better-enums
Submodule
1
lib/better-enums
Submodule
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 37d8f987ca939af846a2d29a8f8198f9bf3ac4b7
|
|
@ -25,6 +25,7 @@
|
||||||
#include "drawing_queue.hpp"
|
#include "drawing_queue.hpp"
|
||||||
#include "worldviewport.hpp"
|
#include "worldviewport.hpp"
|
||||||
#include "texture.hpp"
|
#include "texture.hpp"
|
||||||
|
#include "draw_layer_names.hpp"
|
||||||
#include <ciso646>
|
#include <ciso646>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
@ -118,7 +119,7 @@ namespace curry {
|
||||||
item.source = source_rect();
|
item.source = source_rect();
|
||||||
item.destination = destination_rect(parViewport.position());
|
item.destination = destination_rect(parViewport.position());
|
||||||
item.texture = texture();
|
item.texture = texture();
|
||||||
parDQ.add_for_rendering(0 , std::move(item));
|
parDQ.add_for_rendering(DrawaLayerNames::Background, std::move(item));
|
||||||
assert(not item.texture);
|
assert(not item.texture);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
30
src/gamelib/draw_layer_names.hpp
Normal file
30
src/gamelib/draw_layer_names.hpp
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
/*
|
||||||
|
Copyright 2016, 2017 Michele "King_DuckZ" Santullo
|
||||||
|
|
||||||
|
This file is part of MyCurry.
|
||||||
|
|
||||||
|
MyCurry 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.
|
||||||
|
|
||||||
|
MyCurry 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 MyCurry. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "enum.h"
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace curry {
|
||||||
|
BETTER_ENUM(DrawaLayerNames, uint16_t,
|
||||||
|
Background,
|
||||||
|
Debug
|
||||||
|
);
|
||||||
|
} //namespace curry
|
|
@ -26,9 +26,15 @@
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <ciso646>
|
#include <ciso646>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
#include <boost/container/flat_map.hpp>
|
||||||
|
|
||||||
namespace curry {
|
namespace curry {
|
||||||
namespace {
|
namespace {
|
||||||
|
typedef std::vector<DrawingQueue::ItemInfo> TextureList;
|
||||||
|
typedef std::vector<TextureList> LayerList;
|
||||||
|
typedef boost::container::flat_map<uint16_t, std::size_t> LayerIdxMap;
|
||||||
|
|
||||||
#if !defined(NDEBUG)
|
#if !defined(NDEBUG)
|
||||||
bool are_equal_rel (float parA, float parB, float parEpsilon) a_pure;
|
bool are_equal_rel (float parA, float parB, float parEpsilon) a_pure;
|
||||||
|
|
||||||
|
@ -93,9 +99,6 @@ namespace curry {
|
||||||
DrawingQueue::ItemInfo& DrawingQueue::ItemInfo::operator=(ItemInfo&&) = default;
|
DrawingQueue::ItemInfo& DrawingQueue::ItemInfo::operator=(ItemInfo&&) = default;
|
||||||
|
|
||||||
struct DrawingQueue::LocalData {
|
struct DrawingQueue::LocalData {
|
||||||
typedef std::vector<ItemInfo> TextureList;
|
|
||||||
typedef std::vector<TextureList> LayerList;
|
|
||||||
|
|
||||||
explicit LocalData (cloonel::SDLMain* parSDLMain) :
|
explicit LocalData (cloonel::SDLMain* parSDLMain) :
|
||||||
sdlmain(parSDLMain),
|
sdlmain(parSDLMain),
|
||||||
screen_res(0.0f),
|
screen_res(0.0f),
|
||||||
|
@ -104,6 +107,7 @@ namespace curry {
|
||||||
}
|
}
|
||||||
|
|
||||||
LayerList layers;
|
LayerList layers;
|
||||||
|
LayerIdxMap layer_idx_map;
|
||||||
cloonel::SDLMain* sdlmain;
|
cloonel::SDLMain* sdlmain;
|
||||||
vec2f screen_res;
|
vec2f screen_res;
|
||||||
std::size_t def_layer_id;
|
std::size_t def_layer_id;
|
||||||
|
@ -118,13 +122,19 @@ namespace curry {
|
||||||
|
|
||||||
DrawingQueue::~DrawingQueue() noexcept = default;
|
DrawingQueue::~DrawingQueue() noexcept = default;
|
||||||
|
|
||||||
std::size_t DrawingQueue::add_layer() {
|
bool DrawingQueue::add_layer (uint16_t parName) {
|
||||||
m_local_data->layers.push_back(LocalData::TextureList());
|
auto& idx_map = m_local_data->layer_idx_map;
|
||||||
return m_local_data->layers.size() - 1;
|
auto it_found = idx_map.lower_bound(parName);
|
||||||
|
assert(&*it_found != nullptr or idx_map.end() == it_found);
|
||||||
|
if (it_found != idx_map.end() and it_found->first == parName) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
m_local_data->layers.push_back(TextureList());
|
||||||
|
const auto new_layer_index = m_local_data->layers.size() - 1;
|
||||||
|
idx_map.insert(it_found, std::make_pair(parName, new_layer_index));
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t DrawingQueue::default_layer_id() const {
|
|
||||||
return m_local_data->def_layer_id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DrawingQueue::flush_to_renderer() {
|
void DrawingQueue::flush_to_renderer() {
|
||||||
|
@ -137,16 +147,17 @@ namespace curry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DrawingQueue::add_for_rendering (std::size_t parLayer, ItemInfo&& parItem) {
|
void DrawingQueue::add_for_rendering (uint16_t parName, ItemInfo&& parItem) {
|
||||||
|
const auto idx = m_local_data->layer_idx_map.at(parName);
|
||||||
auto& layers = m_local_data->layers;
|
auto& layers = m_local_data->layers;
|
||||||
assert(parLayer < layers.size());
|
assert(idx < layers.size());
|
||||||
assert(parItem.source.is_valid());
|
assert(parItem.source.is_valid());
|
||||||
assert(parItem.destination.is_valid());
|
assert(parItem.destination.is_valid());
|
||||||
assert(parItem.texture);
|
assert(parItem.texture);
|
||||||
layers[parLayer].push_back(std::move(parItem));
|
layers[idx].push_back(std::move(parItem));
|
||||||
assert(not layers[parLayer].empty());
|
assert(not layers[idx].empty());
|
||||||
assert(layers[parLayer].back().source.is_valid());
|
assert(layers[idx].back().source.is_valid());
|
||||||
assert(layers[parLayer].back().destination.is_valid());
|
assert(layers[idx].back().destination.is_valid());
|
||||||
}
|
}
|
||||||
|
|
||||||
void DrawingQueue::draw_clipped (Texture& parTexture, Rect<float> parSrc, Rect<float> parDest) {
|
void DrawingQueue::draw_clipped (Texture& parTexture, Rect<float> parSrc, Rect<float> parDest) {
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
#include "rect.hpp"
|
#include "rect.hpp"
|
||||||
#include "sizenotifiable.hpp"
|
#include "sizenotifiable.hpp"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
namespace cloonel {
|
namespace cloonel {
|
||||||
class SDLMain;
|
class SDLMain;
|
||||||
|
@ -39,10 +40,10 @@ namespace curry {
|
||||||
DrawingQueue (cloonel::SDLMain* parSDLMain, const cloonel::DeferredRegister& parDeferredRegister);
|
DrawingQueue (cloonel::SDLMain* parSDLMain, const cloonel::DeferredRegister& parDeferredRegister);
|
||||||
virtual ~DrawingQueue() noexcept;
|
virtual ~DrawingQueue() noexcept;
|
||||||
|
|
||||||
std::size_t add_layer();
|
bool add_layer (uint16_t parName);
|
||||||
std::size_t default_layer_id() const;
|
std::size_t default_layer_id() const;
|
||||||
void flush_to_renderer();
|
void flush_to_renderer();
|
||||||
void add_for_rendering (std::size_t parLayer, ItemInfo&& parItem);
|
void add_for_rendering (uint16_t parName, ItemInfo&& parItem);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void draw_clipped (Texture& parTexture, Rect<float> parSrc, Rect<float> parDest);
|
void draw_clipped (Texture& parTexture, Rect<float> parSrc, Rect<float> parDest);
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
#include "sdlmain.hpp"
|
#include "sdlmain.hpp"
|
||||||
#include "rect.hpp"
|
#include "rect.hpp"
|
||||||
#include "drawing_queue.hpp"
|
#include "drawing_queue.hpp"
|
||||||
|
#include "draw_layer_names.hpp"
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
@ -78,6 +79,9 @@ namespace curry {
|
||||||
m_wants_to_quit(false)
|
m_wants_to_quit(false)
|
||||||
{
|
{
|
||||||
assert(m_sdlmain);
|
assert(m_sdlmain);
|
||||||
|
const bool layer_added = m_drawing_queue->add_layer(DrawaLayerNames::Background);
|
||||||
|
assert(layer_added);
|
||||||
|
static_cast<void>(layer_added);
|
||||||
}
|
}
|
||||||
|
|
||||||
GameSceneBase::~GameSceneBase() noexcept = default;
|
GameSceneBase::~GameSceneBase() noexcept = default;
|
||||||
|
|
|
@ -80,10 +80,9 @@ namespace curry {
|
||||||
inp.AddAction(ActionRight, Key(InputDevice_Keyboard, SDL_SCANCODE_RIGHT), "Move right");
|
inp.AddAction(ActionRight, Key(InputDevice_Keyboard, SDL_SCANCODE_RIGHT), "Move right");
|
||||||
inp.AddAction(ActionDown, Key(InputDevice_Keyboard, SDL_SCANCODE_DOWN), "Move down");
|
inp.AddAction(ActionDown, Key(InputDevice_Keyboard, SDL_SCANCODE_DOWN), "Move down");
|
||||||
#if !defined(NDEBUG)
|
#if !defined(NDEBUG)
|
||||||
//TODO: client code should be able to register named layers so other
|
const bool layer_added = drawing_queue().add_layer(DrawaLayerNames::Debug);
|
||||||
//parts of the code can try and request a layer without the need to pass
|
assert(layer_added);
|
||||||
//dynamic ids around
|
static_cast<void>(layer_added);
|
||||||
m_local_data->debug_layer_id = drawing_queue().add_layer();
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,7 +134,7 @@ namespace curry {
|
||||||
|
|
||||||
Rect<float> src_rect(src_rect_xy, src_rect_xy + tilesize);
|
Rect<float> src_rect(src_rect_xy, src_rect_xy + tilesize);
|
||||||
Rect<float> dst_rect(pixel_pos, pixel_pos + tilesize);
|
Rect<float> dst_rect(pixel_pos, pixel_pos + tilesize);
|
||||||
this->draw(layer_id_def(), m_local_data->worldtiles, src_rect, dst_rect);
|
this->draw(DrawaLayerNames::Background, m_local_data->worldtiles, src_rect, dst_rect);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_local_data->character.draw(drawing_queue(), viewport);
|
m_local_data->character.draw(drawing_queue(), viewport);
|
||||||
|
@ -147,7 +146,7 @@ namespace curry {
|
||||||
m_local_data->character.unload_textures();
|
m_local_data->character.unload_textures();
|
||||||
}
|
}
|
||||||
|
|
||||||
void IngameScene::draw (std::size_t parLayer, Kakoune::SafePtr<Texture>& parTexture, Rect<float> parSrc, Rect<float> parDest) {
|
void IngameScene::draw (DrawaLayerNames parLayer, Kakoune::SafePtr<Texture>& parTexture, Rect<float> parSrc, Rect<float> parDest) {
|
||||||
DrawingQueue::ItemInfo item{};
|
DrawingQueue::ItemInfo item{};
|
||||||
item.source = parSrc;
|
item.source = parSrc;
|
||||||
item.destination = parDest;
|
item.destination = parDest;
|
||||||
|
@ -155,14 +154,4 @@ namespace curry {
|
||||||
drawing_queue().add_for_rendering(parLayer, std::move(item));
|
drawing_queue().add_for_rendering(parLayer, std::move(item));
|
||||||
assert(not item.texture);
|
assert(not item.texture);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t IngameScene::layer_id_def() const {
|
|
||||||
return drawing_queue().default_layer_id();
|
|
||||||
}
|
|
||||||
|
|
||||||
#if !defined(NDEBUG)
|
|
||||||
std::size_t IngameScene::layer_id_debug() const {
|
|
||||||
return m_local_data->debug_layer_id;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
} //namespace curry
|
} //namespace curry
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
|
|
||||||
#include "gamescenebase.hpp"
|
#include "gamescenebase.hpp"
|
||||||
#include "safe_ptr.hh"
|
#include "safe_ptr.hh"
|
||||||
|
#include "draw_layer_names.hpp"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
|
@ -40,11 +41,7 @@ namespace curry {
|
||||||
virtual void on_prepare() override;
|
virtual void on_prepare() override;
|
||||||
virtual void on_destroy() noexcept override;
|
virtual void on_destroy() noexcept override;
|
||||||
virtual void on_update (float parDeltaT) override;
|
virtual void on_update (float parDeltaT) override;
|
||||||
void draw (std::size_t parLayer, Kakoune::SafePtr<Texture>& parTexture, Rect<float> parSrc, Rect<float> parDest);
|
void draw (DrawaLayerNames parLayer, Kakoune::SafePtr<Texture>& parTexture, Rect<float> parSrc, Rect<float> parDest);
|
||||||
std::size_t layer_id_def() const;
|
|
||||||
#if !defined(NDEBUG)
|
|
||||||
std::size_t layer_id_debug() const;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct LocalData;
|
struct LocalData;
|
||||||
|
|
Loading…
Add table
Reference in a new issue