Use pimpl to hide properties used by the gameplay scene.
This commit is contained in:
parent
33c18680d6
commit
f1467d7d99
2 changed files with 32 additions and 19 deletions
|
@ -3,6 +3,9 @@
|
|||
#include "mycurryConfig.h"
|
||||
#include "key.hpp"
|
||||
#include "inputbag.hpp"
|
||||
#include "worldgrid.hpp"
|
||||
#include "worldviewport.hpp"
|
||||
#include "texture.hpp"
|
||||
#include <SDL2/SDL.h>
|
||||
#include <algorithm>
|
||||
|
||||
|
@ -26,15 +29,25 @@ namespace curry {
|
|||
}
|
||||
} //unnamed namespace
|
||||
|
||||
struct IngameScene::LocalData {
|
||||
LocalData (const vec2us& parTileSize, const vec2us& parTileCount, cloonel::SDLMain* parSDLMain) :
|
||||
world(parTileSize, parTileCount),
|
||||
viewport(&world, vec2i(parSDLMain->WidthHeight()))
|
||||
{
|
||||
}
|
||||
|
||||
WorldGrid world;
|
||||
WorldViewport viewport;
|
||||
Texture worldtiles;
|
||||
};
|
||||
|
||||
IngameScene::IngameScene (cloonel::SDLMain* parSDLMain) :
|
||||
GameSceneBase(parSDLMain),
|
||||
m_world(vec2us(96), vec2us(100)),
|
||||
m_viewport(&m_world, vec2i(parSDLMain->WidthHeight()))
|
||||
m_local_data(std::make_unique<LocalData>(vec2us(96), vec2us(100), parSDLMain))
|
||||
{
|
||||
using cloonel::Key;
|
||||
using cloonel::InputDevice_Keyboard;
|
||||
|
||||
//m_viewport.set_position(vec2i(38));
|
||||
auto& inp = this->input_bag();
|
||||
inp.AddAction(ActionLeft, Key(InputDevice_Keyboard, SDL_SCANCODE_LEFT), "Move left");
|
||||
inp.AddAction(ActionUp, Key(InputDevice_Keyboard, SDL_SCANCODE_UP), "Move up");
|
||||
|
@ -45,27 +58,29 @@ namespace curry {
|
|||
IngameScene::~IngameScene() noexcept = default;
|
||||
|
||||
void IngameScene::on_prepare() {
|
||||
m_worldtiles.load(RESOURCES_PATH "textures.png", *this->sdl_main());
|
||||
m_local_data->worldtiles.load(RESOURCES_PATH "textures.png", *this->sdl_main());
|
||||
}
|
||||
|
||||
void IngameScene::on_update (float parDeltaT) {
|
||||
const auto& world = m_local_data->world;
|
||||
const float speed_per_sec =
|
||||
static_cast<float>(
|
||||
std::max(m_world.tile_size().x(), m_world.tile_size().y())
|
||||
std::max(world.tile_size().x(), world.tile_size().y())
|
||||
) * 1.5f;
|
||||
|
||||
auto& viewport = m_local_data->viewport;
|
||||
const float speed = parDeltaT * speed_per_sec;
|
||||
if (cloonel::IsPressed(input_bag(), ActionRight))
|
||||
m_viewport.set_position(m_viewport.position() + vec2f(speed, 0));
|
||||
viewport.set_position(viewport.position() + vec2f(speed, 0));
|
||||
if (cloonel::IsPressed(input_bag(), ActionLeft))
|
||||
m_viewport.set_position(m_viewport.position() - vec2f(speed, 0));
|
||||
viewport.set_position(viewport.position() - vec2f(speed, 0));
|
||||
if (cloonel::IsPressed(input_bag(), ActionUp))
|
||||
m_viewport.set_position(m_viewport.position() - vec2f(0, speed));
|
||||
viewport.set_position(viewport.position() - vec2f(0, speed));
|
||||
if (cloonel::IsPressed(input_bag(), ActionDown))
|
||||
m_viewport.set_position(m_viewport.position() + vec2f(0, speed));
|
||||
viewport.set_position(viewport.position() + vec2f(0, speed));
|
||||
|
||||
const auto tilesize(m_world.tile_size());
|
||||
for (auto tile : m_viewport) {
|
||||
const auto tilesize(world.tile_size());
|
||||
for (auto tile : viewport) {
|
||||
vec2us idx(tile.index & 1, (tile.index >> 1) & 1);
|
||||
vec2us src_rect_xy = idx * tilesize;
|
||||
|
||||
|
@ -73,12 +88,12 @@ namespace curry {
|
|||
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()};
|
||||
clip(src, dst, vec2i(0), sdl_main()->WidthHeight());
|
||||
SDL_RenderCopy(this->sdl_main()->GetRenderer(), m_worldtiles.texture(), &src, &dst);
|
||||
SDL_RenderCopy(this->sdl_main()->GetRenderer(), m_local_data->worldtiles.texture(), &src, &dst);
|
||||
}
|
||||
SDL_RenderPresent(this->sdl_main()->GetRenderer());
|
||||
}
|
||||
|
||||
void IngameScene::on_destroy() noexcept {
|
||||
m_worldtiles.unload();
|
||||
m_local_data->worldtiles.unload();
|
||||
}
|
||||
} //namespace curry
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "gamescenebase.hpp"
|
||||
#include "worldgrid.hpp"
|
||||
#include "worldviewport.hpp"
|
||||
#include "texture.hpp"
|
||||
#include <memory>
|
||||
|
||||
namespace cloonel {
|
||||
class SDLMain;
|
||||
|
@ -21,8 +19,8 @@ namespace curry {
|
|||
virtual void on_update (float parDeltaT) override;
|
||||
|
||||
private:
|
||||
WorldGrid m_world;
|
||||
WorldViewport m_viewport;
|
||||
Texture m_worldtiles;
|
||||
struct LocalData;
|
||||
|
||||
std::unique_ptr<LocalData> m_local_data;
|
||||
};
|
||||
} //namespace curry
|
||||
|
|
Loading…
Add table
Reference in a new issue