Draw in abstract units and scale to pixels at the end.

This commit is contained in:
King_DuckZ 2014-03-12 11:04:22 +01:00
parent a561242395
commit 0c6275f41c
9 changed files with 73 additions and 22 deletions

View file

@ -48,6 +48,7 @@ add_executable(${PROJECT_NAME}
src/inputbag.cpp src/inputbag.cpp
src/moverleftright.cpp src/moverleftright.cpp
src/tiledwallpaper.cpp src/tiledwallpaper.cpp
src/drawable.cpp
) )
target_link_libraries(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME}

View file

@ -24,7 +24,7 @@
namespace cloonel { 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)), Placeable(float2(0.0f)),
Drawable(parSize), Drawable(parSize),
m_texture(new Texture(parPath, parMain, false)) 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)), Placeable(float2(0.0f)),
Drawable(parSize), Drawable(parSize),
m_texture(new Texture(parPath, parMain, false)) m_texture(new Texture(parPath, parMain, false))
@ -63,7 +63,6 @@ namespace cloonel {
///------------------------------------------------------------------------- ///-------------------------------------------------------------------------
///------------------------------------------------------------------------- ///-------------------------------------------------------------------------
void Character::Draw() const { void Character::Draw() const {
const int2 pos(GetPos() + 0.5f); m_texture->Render(GetPos(), WidthHeight());
m_texture->Render(pos, m_wh);
} }
} //namespace cloonel } //namespace cloonel

View file

@ -32,8 +32,8 @@ namespace cloonel {
class Character : public Placeable, public Drawable { class Character : public Placeable, public Drawable {
public: public:
Character ( const std::string& parPath, SDLMain* parMain, ushort2 parSize ); Character ( const std::string& parPath, SDLMain* parMain, float2 parSize );
Character ( const std::string&& parPath, SDLMain* parMai, ushort2 parSize ); Character ( const std::string&& parPath, SDLMain* parMai, float2 parSize );
virtual ~Character ( void ) noexcept; virtual ~Character ( void ) noexcept;
void Prepare ( void ); void Prepare ( void );

43
src/drawable.cpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "drawable.hpp"
#include "sdlmain.hpp"
#include <cassert>
namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
Drawable::Drawable (float parWidth, float parHeight) :
m_wh(parWidth, parHeight)
{
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
Drawable::Drawable (float2 parWH) :
m_wh(parWH)
{
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
Drawable::~Drawable() noexcept {
}
} //namespace cloonel

View file

@ -21,20 +21,24 @@
#define idC5A880D06A03407DB4E9FC21593A47FB #define idC5A880D06A03407DB4E9FC21593A47FB
#include "vector.hpp" #include "vector.hpp"
#include <cstdint>
namespace cloonel { namespace cloonel {
class SDLMain;
class Drawable { class Drawable {
public: public:
Drawable ( void ) = default; Drawable ( void ) = default;
Drawable ( uint16_t parWidth, uint16_t parHeight ) : m_wh(parWidth, parHeight) {} Drawable ( float parWidth, float parHeight );
explicit Drawable ( ushort2 parWH ) : m_wh(parWH) {} explicit Drawable ( float2 parWH );
virtual ~Drawable ( void ) noexcept = default; virtual ~Drawable ( void ) noexcept;
virtual void Draw ( void ) const = 0; virtual void Draw ( void ) const = 0;
protected: protected:
ushort2 m_wh; const float2& WidthHeight ( void ) const { return m_wh; }
private:
float2 m_wh;
}; };
} //namespace cloonel } //namespace cloonel

View file

@ -58,7 +58,7 @@ namespace cloonel {
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
void GameplaySceneClassic::Prepare() { void GameplaySceneClassic::Prepare() {
std::unique_ptr<MoverSine> moverSine(new MoverSine()); std::unique_ptr<MoverSine> moverSine(new MoverSine());
std::unique_ptr<Character> player(new Character("resources/graphics/player.png", SDLObject(), ushort2(80, 120))); std::unique_ptr<Character> player(new Character("resources/graphics/player.png", SDLObject(), float2(80.0f, 120.0f)));
std::unique_ptr<MoverLeftRight> moverLeftRight(new MoverLeftRight(1.5f, 5.0f, 40.0f)); std::unique_ptr<MoverLeftRight> moverLeftRight(new MoverLeftRight(1.5f, 5.0f, 40.0f));
std::unique_ptr<TiledWallpaper> wallpaper(new TiledWallpaper("resources/graphics/background_tile.png", SDLObject())); std::unique_ptr<TiledWallpaper> wallpaper(new TiledWallpaper("resources/graphics/background_tile.png", SDLObject()));

View file

@ -261,18 +261,21 @@ namespace cloonel {
m_texture = SDL_CreateTextureFromSurface(m_sdlmain->GetRenderer(), surf); m_texture = SDL_CreateTextureFromSurface(m_sdlmain->GetRenderer(), surf);
SDL_FreeSurface(surf); SDL_FreeSurface(surf);
if (m_texture) { if (m_texture) {
int width, height; int2 wh;
SDL_QueryTexture(m_texture, nullptr, nullptr, &width, &height); SDL_QueryTexture(m_texture, nullptr, nullptr, &wh.x(), &wh.y());
m_size = static_cast<ushort2>(int2(width, height)); m_size = static_cast<float2>(wh);
} }
} }
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
void Texture::Render (int2 parPos, ushort2 parSize) const { void Texture::Render (const float2& parPos, const float2& parSize) const {
assert(IsLoaded()); assert(IsLoaded());
const float2 scaling(m_sdlmain->WHScaling());
const ushort2 pos(static_cast<ushort2>(parPos * scaling + 0.5f));
const ushort2 siz(static_cast<ushort2>(parSize * scaling + 0.5f));
const int screenHeight = m_sdlmain->WidthHeight().y(); 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); SDL_RenderCopy(m_sdlmain->GetRenderer(), m_texture, nullptr, &dest);
} }
} //namespace cloonel } //namespace cloonel

View file

@ -36,15 +36,15 @@ namespace cloonel {
void Reload ( void ); void Reload ( void );
void Destroy ( void ) noexcept; void Destroy ( void ) noexcept;
bool IsLoaded ( void ) const { return nullptr != m_texture; } bool IsLoaded ( void ) const { return nullptr != m_texture; }
void Render ( int2 parPos ) const { Render(parPos, m_size); } void Render ( const float2& parPos ) const { Render(parPos, m_size); }
void Render ( int2 parPos, ushort2 parSize ) const; void Render ( const float2& parPos, const float2& parSize ) const;
const SDLMain* SDLObject ( void ) const { return m_sdlmain; } const SDLMain* SDLObject ( void ) const { return m_sdlmain; }
private: private:
const std::string m_path; const std::string m_path;
float2 m_size;
SDL_Texture* m_texture; SDL_Texture* m_texture;
SDLMain* const m_sdlmain; SDLMain* const m_sdlmain;
ushort2 m_size;
}; };
} //namespace cloonel } //namespace cloonel

View file

@ -19,12 +19,13 @@
#include "tiledwallpaper.hpp" #include "tiledwallpaper.hpp"
#include "texture.hpp" #include "texture.hpp"
#include "sdlmain.hpp"
namespace cloonel { namespace cloonel {
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
TiledWallpaper::TiledWallpaper (const std::string&& parPath, SDLMain* parMain) : TiledWallpaper::TiledWallpaper (const std::string&& parPath, SDLMain* parMain) :
Drawable(0, 0), Drawable(128.0f, 128.0f),
m_tile(new Texture(parPath, parMain, false)) m_tile(new Texture(parPath, parMain, false))
{ {
} }
@ -49,6 +50,6 @@ namespace cloonel {
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
void TiledWallpaper::Draw() const { void TiledWallpaper::Draw() const {
m_tile->Render(int2(0)); m_tile->Render(float2(0.0f));
} }
} //namespace cloonel } //namespace cloonel