From 0c6275f41c4df2cce197a9361cc0bd959f029801 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 12 Mar 2014 11:04:22 +0100 Subject: [PATCH] Draw in abstract units and scale to pixels at the end. --- CMakeLists.txt | 1 + src/character.cpp | 7 +++--- src/character.hpp | 4 ++-- src/drawable.cpp | 43 ++++++++++++++++++++++++++++++++++++ src/drawable.hpp | 14 +++++++----- src/gameplaysceneclassic.cpp | 2 +- src/texture.cpp | 13 ++++++----- src/texture.hpp | 6 ++--- src/tiledwallpaper.cpp | 5 +++-- 9 files changed, 73 insertions(+), 22 deletions(-) create mode 100644 src/drawable.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 16970ba..210dfb0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,7 @@ add_executable(${PROJECT_NAME} src/inputbag.cpp src/moverleftright.cpp src/tiledwallpaper.cpp + src/drawable.cpp ) target_link_libraries(${PROJECT_NAME} diff --git a/src/character.cpp b/src/character.cpp index 310b80e..e507f6c 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -24,7 +24,7 @@ namespace cloonel { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- - Character::Character (const std::string& parPath, SDLMain* parMain, ushort2 parSize) : + Character::Character (const std::string& parPath, SDLMain* parMain, float2 parSize) : Placeable(float2(0.0f)), Drawable(parSize), m_texture(new Texture(parPath, parMain, false)) @@ -33,7 +33,7 @@ namespace cloonel { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- - Character::Character (const std::string&& parPath, SDLMain* parMain, ushort2 parSize) : + Character::Character (const std::string&& parPath, SDLMain* parMain, float2 parSize) : Placeable(float2(0.0f)), Drawable(parSize), m_texture(new Texture(parPath, parMain, false)) @@ -63,7 +63,6 @@ namespace cloonel { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- void Character::Draw() const { - const int2 pos(GetPos() + 0.5f); - m_texture->Render(pos, m_wh); + m_texture->Render(GetPos(), WidthHeight()); } } //namespace cloonel diff --git a/src/character.hpp b/src/character.hpp index e0c3898..3ca8d29 100644 --- a/src/character.hpp +++ b/src/character.hpp @@ -32,8 +32,8 @@ namespace cloonel { class Character : public Placeable, public Drawable { public: - Character ( const std::string& parPath, SDLMain* parMain, ushort2 parSize ); - Character ( const std::string&& parPath, SDLMain* parMai, ushort2 parSize ); + Character ( const std::string& parPath, SDLMain* parMain, float2 parSize ); + Character ( const std::string&& parPath, SDLMain* parMai, float2 parSize ); virtual ~Character ( void ) noexcept; void Prepare ( void ); diff --git a/src/drawable.cpp b/src/drawable.cpp new file mode 100644 index 0000000..edd3656 --- /dev/null +++ b/src/drawable.cpp @@ -0,0 +1,43 @@ +/* + Copyright 2014 Michele "King_DuckZ" Santullo + + This file is part of CloonelJump. + + CloonelJump 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. + + CloonelJump 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 CloonelJump. If not, see . +*/ + +#include "drawable.hpp" +#include "sdlmain.hpp" +#include + +namespace cloonel { + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + Drawable::Drawable (float parWidth, float parHeight) : + m_wh(parWidth, parHeight) + { + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + Drawable::Drawable (float2 parWH) : + m_wh(parWH) + { + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + Drawable::~Drawable() noexcept { + } +} //namespace cloonel diff --git a/src/drawable.hpp b/src/drawable.hpp index 7cd3419..12b1bda 100644 --- a/src/drawable.hpp +++ b/src/drawable.hpp @@ -21,20 +21,24 @@ #define idC5A880D06A03407DB4E9FC21593A47FB #include "vector.hpp" -#include namespace cloonel { + class SDLMain; + class Drawable { public: Drawable ( void ) = default; - Drawable ( uint16_t parWidth, uint16_t parHeight ) : m_wh(parWidth, parHeight) {} - explicit Drawable ( ushort2 parWH ) : m_wh(parWH) {} - virtual ~Drawable ( void ) noexcept = default; + Drawable ( float parWidth, float parHeight ); + explicit Drawable ( float2 parWH ); + virtual ~Drawable ( void ) noexcept; virtual void Draw ( void ) const = 0; protected: - ushort2 m_wh; + const float2& WidthHeight ( void ) const { return m_wh; } + + private: + float2 m_wh; }; } //namespace cloonel diff --git a/src/gameplaysceneclassic.cpp b/src/gameplaysceneclassic.cpp index d26447e..dabe11c 100644 --- a/src/gameplaysceneclassic.cpp +++ b/src/gameplaysceneclassic.cpp @@ -58,7 +58,7 @@ namespace cloonel { ///-------------------------------------------------------------------------- void GameplaySceneClassic::Prepare() { std::unique_ptr moverSine(new MoverSine()); - std::unique_ptr player(new Character("resources/graphics/player.png", SDLObject(), ushort2(80, 120))); + 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())); diff --git a/src/texture.cpp b/src/texture.cpp index 007836a..eb9c4ff 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -261,18 +261,21 @@ namespace cloonel { m_texture = SDL_CreateTextureFromSurface(m_sdlmain->GetRenderer(), surf); SDL_FreeSurface(surf); if (m_texture) { - int width, height; - SDL_QueryTexture(m_texture, nullptr, nullptr, &width, &height); - m_size = static_cast(int2(width, height)); + int2 wh; + SDL_QueryTexture(m_texture, nullptr, nullptr, &wh.x(), &wh.y()); + m_size = static_cast(wh); } } ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- - void Texture::Render (int2 parPos, ushort2 parSize) const { + void Texture::Render (const float2& parPos, const float2& parSize) const { assert(IsLoaded()); + const float2 scaling(m_sdlmain->WHScaling()); + const ushort2 pos(static_cast(parPos * scaling + 0.5f)); + const ushort2 siz(static_cast(parSize * scaling + 0.5f)); const int screenHeight = m_sdlmain->WidthHeight().y(); - const SDL_Rect dest = { parPos.x(), screenHeight - parPos.y() - parSize.y(), parSize.x(), parSize.y() }; + const SDL_Rect dest = { pos.x(), screenHeight - pos.y() - siz.y(), siz.x(), siz.y() }; SDL_RenderCopy(m_sdlmain->GetRenderer(), m_texture, nullptr, &dest); } } //namespace cloonel diff --git a/src/texture.hpp b/src/texture.hpp index 08944f9..d4ca914 100644 --- a/src/texture.hpp +++ b/src/texture.hpp @@ -36,15 +36,15 @@ namespace cloonel { void Reload ( void ); void Destroy ( void ) noexcept; bool IsLoaded ( void ) const { return nullptr != m_texture; } - void Render ( int2 parPos ) const { Render(parPos, m_size); } - void Render ( int2 parPos, ushort2 parSize ) const; + void Render ( const float2& parPos ) const { Render(parPos, m_size); } + void Render ( const float2& parPos, const float2& parSize ) const; const SDLMain* SDLObject ( void ) const { return m_sdlmain; } private: const std::string m_path; + float2 m_size; SDL_Texture* m_texture; SDLMain* const m_sdlmain; - ushort2 m_size; }; } //namespace cloonel diff --git a/src/tiledwallpaper.cpp b/src/tiledwallpaper.cpp index 0e49d2d..d002e4b 100644 --- a/src/tiledwallpaper.cpp +++ b/src/tiledwallpaper.cpp @@ -19,12 +19,13 @@ #include "tiledwallpaper.hpp" #include "texture.hpp" +#include "sdlmain.hpp" namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- TiledWallpaper::TiledWallpaper (const std::string&& parPath, SDLMain* parMain) : - Drawable(0, 0), + Drawable(128.0f, 128.0f), m_tile(new Texture(parPath, parMain, false)) { } @@ -49,6 +50,6 @@ namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- void TiledWallpaper::Draw() const { - m_tile->Render(int2(0)); + m_tile->Render(float2(0.0f)); } } //namespace cloonel