From fc50c6af55ceb179d302ed4130a07915b4186830 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 28 Mar 2014 10:52:59 +0100 Subject: [PATCH] Generate one platform and draw it. WiP. --- src/gameplaysceneclassic.cpp | 8 +++++++ src/gameplaysceneclassic.hpp | 3 +++ src/platform.hpp | 2 ++ src/platformsystem.cpp | 43 +++++++++++++++++++++++++++++------- src/platformsystem.hpp | 20 +++++++++++++---- 5 files changed, 64 insertions(+), 12 deletions(-) diff --git a/src/gameplaysceneclassic.cpp b/src/gameplaysceneclassic.cpp index dabe11c..ae77fbb 100644 --- a/src/gameplaysceneclassic.cpp +++ b/src/gameplaysceneclassic.cpp @@ -25,6 +25,8 @@ #include "key.hpp" #include "moverleftright.hpp" #include "tiledwallpaper.hpp" +#include "texture.hpp" +#include "platformsystem.hpp" #include #include #include @@ -61,16 +63,19 @@ namespace cloonel { std::unique_ptr player(new Character("resources/graphics/player.png", SDLObject(), float2(80.0f, 120.0f))); std::unique_ptr moverLeftRight(new MoverLeftRight(1.5f, 5.0f, 40.0f)); std::unique_ptr wallpaper(new TiledWallpaper("resources/graphics/background_tile.png", SDLObject())); + std::unique_ptr platforms(new PlatformSystem("resources/graphics/platform.png", SDLObject(), this)); player->Prepare(); moverSine->RegisterPlaceable(player.get()); moverLeftRight->RegisterPlaceable(player.get()); wallpaper->Reload(); + platforms->Prepare(); std::swap(moverSine, m_moverSine); std::swap(player, m_player); std::swap(moverLeftRight, m_moverLeftRight); std::swap(wallpaper, m_wallpaper); + std::swap(platforms, m_platforms); AddMover(m_moverSine.get()); AddMover(m_moverLeftRight.get()); @@ -87,6 +92,7 @@ namespace cloonel { m_player = std::move(std::unique_ptr(nullptr)); m_moverLeftRight = std::move(std::unique_ptr(nullptr)); m_wallpaper = std::move(std::unique_ptr(nullptr)); + m_platforms = std::move(std::unique_ptr(nullptr)); } ///-------------------------------------------------------------------------- @@ -102,5 +108,7 @@ namespace cloonel { else { m_moverLeftRight->SetMovement(MoverLeftRight::MovementDirection_Still); } + + m_platforms->SpawnPlatforms(); } } //namespace cloonel diff --git a/src/gameplaysceneclassic.hpp b/src/gameplaysceneclassic.hpp index d50199c..6e2d5d0 100644 --- a/src/gameplaysceneclassic.hpp +++ b/src/gameplaysceneclassic.hpp @@ -29,6 +29,8 @@ namespace cloonel { class MoverSine; class MoverLeftRight; class TiledWallpaper; + class Texture; + class PlatformSystem; class GameplaySceneClassic : public GameplayScene { public: @@ -45,6 +47,7 @@ namespace cloonel { std::unique_ptr m_moverSine; std::unique_ptr m_moverLeftRight; std::unique_ptr m_wallpaper; + std::unique_ptr m_platforms; }; } //namespace cloonel diff --git a/src/platform.hpp b/src/platform.hpp index c099299..ed8a2bf 100644 --- a/src/platform.hpp +++ b/src/platform.hpp @@ -36,6 +36,8 @@ namespace cloonel { Platform& operator= ( Platform&& parOther ) = delete; virtual void Draw ( void ) const; + const float2& TopLeft ( void ) const { return m_position; } + float2 BottomRight ( void ) const { return m_position + m_size; } private: SizeNotifiable m_screenRatio; diff --git a/src/platformsystem.cpp b/src/platformsystem.cpp index 5a29693..178170d 100644 --- a/src/platformsystem.cpp +++ b/src/platformsystem.cpp @@ -19,19 +19,24 @@ #include "platformsystem.hpp" #include "platform.hpp" +#include "CloonelJumpConfig.h" +#include "texture.hpp" +#include "gameplayscene.hpp" +#include +#include +#include namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- - PlatformSystem::PlatformSystem() : - m_platforms() + PlatformSystem::PlatformSystem (const char* parTexturePath, SDLMain* parSDLMain, GameplayScene* parScene) : + m_sizeRatio(parSDLMain), + m_platforms(), + m_texture(new Texture(parTexturePath, parSDLMain, false)), + m_sdlmain(parSDLMain), + m_scene(parScene) { - } - - ///-------------------------------------------------------------------------- - ///-------------------------------------------------------------------------- - PlatformSystem::PlatformSystem (PlatformSystem&& parOther) { - m_platforms.swap(parOther.m_platforms); + assert(m_sdlmain); } ///-------------------------------------------------------------------------- @@ -42,10 +47,32 @@ namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- void PlatformSystem::SpawnPlatforms() { + const int2 refWH(REFERENCE_WIDTH, REFERENCE_HEIGHT); + const int2 platfWH(110, 30); + if (m_platforms.empty()) { + const float2 newPos(static_cast(std::rand() % (refWH.x() - platfWH.x())), static_cast(std::rand() % (refWH.y() - platfWH.y()))); + m_platforms.push(Platform(m_sdlmain, newPos, m_texture.get(), static_cast(platfWH))); + m_scene->AddDrawable(&m_platforms.back()); + } } ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- void PlatformSystem::GarbageCollect() { + while (not m_platforms.empty() and m_platforms.front().TopLeft().y() <= 0.0f) { + m_platforms.pop(); + } + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + void PlatformSystem::Prepare() { + m_texture->Reload(); + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + void PlatformSystem::Destroy() noexcept { + m_texture->Destroy(); } } //namespace cloonel diff --git a/src/platformsystem.hpp b/src/platformsystem.hpp index 8b5df3d..b4be75c 100644 --- a/src/platformsystem.hpp +++ b/src/platformsystem.hpp @@ -19,24 +19,36 @@ #ifndef id17908979556C47F8A978688BBE4A9D22 -#include +#include "sizenotifiable.hpp" +#include +#include namespace cloonel { class Platform; + class SDLMain; + class Texture; + class GameplayScene; class PlatformSystem { public: - PlatformSystem ( void ); + PlatformSystem ( void ) = delete; + PlatformSystem ( const char* parTexturePath, SDLMain* parSDLMain, GameplayScene* parScene ); PlatformSystem ( const PlatformSystem& ) = delete; - PlatformSystem ( PlatformSystem&& parOther ); + PlatformSystem ( PlatformSystem&& parOther ) = delete; ~PlatformSystem ( void ) noexcept; PlatformSystem& operator= ( const PlatformSystem& ) = delete; + void Prepare ( void ); + void Destroy ( void ) noexcept; void SpawnPlatforms ( void ); void GarbageCollect ( void ); private: - std::vector m_platforms; + SizeNotifiable m_sizeRatio; + std::queue m_platforms; + const std::unique_ptr m_texture; + SDLMain* const m_sdlmain; + GameplayScene* const m_scene; }; } //namespace cloonel