From 2429229a950273d60cb1bf5b46b038f3e45cf328 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sat, 8 Feb 2014 23:11:26 +0100 Subject: [PATCH] Code refactored into different files. --- CMakeLists.txt | 3 ++ src/main.cpp | 50 +++----------------------------- src/sdlerror.cpp | 32 +++++++++++++++++++++ src/sdlerror.hpp | 11 +++++++ src/sdlmain.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ src/sdlmain.hpp | 30 ++++++++++++++++++++ src/texture.cpp | 36 +++++++++++++++++++++++ src/texture.hpp | 27 ++++++++++++++++++ 8 files changed, 217 insertions(+), 46 deletions(-) create mode 100644 src/sdlerror.cpp create mode 100644 src/sdlerror.hpp create mode 100644 src/sdlmain.cpp create mode 100644 src/sdlmain.hpp create mode 100644 src/texture.cpp create mode 100644 src/texture.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c7f043..8da09bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,9 @@ configure_file( add_executable(${PROJECT_NAME} src/main.cpp + src/texture.cpp + src/sdlerror.cpp + src/sdlmain.cpp ) target_link_libraries(${PROJECT_NAME} diff --git a/src/main.cpp b/src/main.cpp index dbd869b..81eadf5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,65 +1,23 @@ -#include -#include #include "CloonelJumpConfig.h" +#include "sdlmain.hpp" +#include #include -namespace { - struct InitSDLStuff { - SDL_Window* window; - SDL_Renderer* renderer; - bool initialized; - }; - - ///------------------------------------------------------------------------ - ///------------------------------------------------------------------------ - void InitSDL (InitSDLStuff& parInitSDL) { - parInitSDL.window = nullptr; - parInitSDL.renderer = nullptr; - parInitSDL.initialized = false; - - if (SDL_Init(SDL_INIT_EVERYTHING) == -1) - throw std::runtime_error(SDL_GetError()); - parInitSDL.initialized = true; - - SDL_Window* const win = SDL_CreateWindow(GameName, 100, 100, DEF_WIN_WIDTH, DEF_WIN_HEIGHT, SDL_WINDOW_SHOWN); - if (!win) - throw std::runtime_error(SDL_GetError()); - parInitSDL.window = win; - - SDL_Renderer* const renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); - if (!renderer) - throw std::runtime_error(SDL_GetError()); - parInitSDL.renderer = renderer; - } - - ///------------------------------------------------------------------------ - ///------------------------------------------------------------------------ - void ClearIFN (InitSDLStuff& parInitSDL) { - if (parInitSDL.renderer) - SDL_DestroyRenderer(parInitSDL.renderer); - if (parInitSDL.window) - SDL_DestroyWindow(parInitSDL.window); - if (parInitSDL.initialized) - SDL_Quit(); - } -} //unnamed namespace - ///---------------------------------------------------------------------------- ///following http://twinklebeardev.blogspot.co.uk/2012/07/lesson-1-hello-world.html ///---------------------------------------------------------------------------- int main() { std::cout << GameName << " v" << GameVersionMajor << "." << GameVersionMinor << std::endl; - InitSDLStuff sdlstuff; + cloonel::SDLMain sdlmain(GameName, DEF_WIN_WIDTH, DEF_WIN_HEIGHT); try { - InitSDL(sdlstuff); + sdlmain.Init(); } catch (const std::runtime_error& e) { std::cerr << "Error during SDL2 initialization:\n"; std::cerr << e.what() << std::endl; } - ClearIFN(sdlstuff); std::cout << "Quitting now" << std::endl; return 0; } diff --git a/src/sdlerror.cpp b/src/sdlerror.cpp new file mode 100644 index 0000000..6425e27 --- /dev/null +++ b/src/sdlerror.cpp @@ -0,0 +1,32 @@ +#include "sdlerror.hpp" +#include +#include +#include +#include + +namespace cloonel { + namespace { + } //unnamed namespace + + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + std::string GetFullErrorMessage (const char* parFunction, const std::string& parMessage) { + std::ostringstream oss; + if (parFunction) + oss << "Error in " << parFunction << ": "; + else + oss << "Error: "; + + if (not parMessage.empty()) + oss << parMessage << " - "; + + oss << SDL_GetError(); + return oss.str(); + } + + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + void ShowError (const char* parFunction, const std::string& parMessage) { + std::cerr << GetFullErrorMessage(parFunction, parMessage) << std::endl; + } +} //namespace cloonel diff --git a/src/sdlerror.hpp b/src/sdlerror.hpp new file mode 100644 index 0000000..3917962 --- /dev/null +++ b/src/sdlerror.hpp @@ -0,0 +1,11 @@ +#ifndef idE2DD0E628C364995868580C60903B0BF +#define idE2DD0E628C364995868580C60903B0BF + +#include + +namespace cloonel { + std::string GetFullErrorMessage ( const char* parFunction, const std::string& parMessage ); + void ShowError ( const char* parFunction, const std::string& parMessage ); +} //namespace cloonel + +#endif diff --git a/src/sdlmain.cpp b/src/sdlmain.cpp new file mode 100644 index 0000000..cb67f59 --- /dev/null +++ b/src/sdlmain.cpp @@ -0,0 +1,74 @@ +#include "sdlmain.hpp" +#include + +namespace cloonel { + struct SDLMain::LocalData { + SDL_Window* window; + SDL_Renderer* renderer; + bool initialized; + }; + + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + SDLMain::SDLMain (const char* parGameName, int parWidth, int parHeight) : + m_gameName(parGameName), + m_defWidth(parWidth), + m_defHeight(parHeight) + { + } + + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + SDLMain::~SDLMain() noexcept { + ClearIFN(*m_localData); + } + + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + void SDLMain::Init() { + if (not m_localData->initialized) + InitSDL(*m_localData); + } + + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + void SDLMain::InitSDL (LocalData& parInitSDL) { + parInitSDL.window = nullptr; + parInitSDL.renderer = nullptr; + parInitSDL.initialized = false; + + if (SDL_Init(SDL_INIT_EVERYTHING) == -1) + throw std::runtime_error(SDL_GetError()); + parInitSDL.initialized = true; + + SDL_Window* const win = SDL_CreateWindow(m_gameName.c_str(), 100, 100, m_defWidth, m_defHeight, SDL_WINDOW_SHOWN); + if (!win) + throw std::runtime_error(SDL_GetError()); + parInitSDL.window = win; + + SDL_Renderer* const renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + if (!renderer) + throw std::runtime_error(SDL_GetError()); + parInitSDL.renderer = renderer; + } + + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + void SDLMain::ClearIFN (LocalData& parInitSDL) noexcept { + if (parInitSDL.renderer) + SDL_DestroyRenderer(parInitSDL.renderer); + if (parInitSDL.window) + SDL_DestroyWindow(parInitSDL.window); + if (parInitSDL.initialized) + SDL_Quit(); + } + + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + SDL_Renderer* SDLMain::GetRenderer() { + if (m_localData->initialized) + return m_localData->renderer; + else + return nullptr; + } +} //namespace cloonel diff --git a/src/sdlmain.hpp b/src/sdlmain.hpp new file mode 100644 index 0000000..43760d9 --- /dev/null +++ b/src/sdlmain.hpp @@ -0,0 +1,30 @@ +#ifndef id8E7A054DAC9040B887F2620EFD229EE8 +#define id8E7A054DAC9040B887F2620EFD229EE8 + +#include + +struct SDL_Renderer; + +namespace cloonel { + class SDLMain { + public: + SDLMain ( const char* parGameName, int parWidth, int parHeight ); + ~SDLMain ( void ) noexcept; + + void Init ( void ); + SDL_Renderer* GetRenderer ( void ); + + private: + struct LocalData; + + void InitSDL ( LocalData& parData ); + void ClearIFN ( LocalData& parData ) noexcept; + + const std::string m_gameName; + std::unique_ptr m_localData; + const int m_defWidth; + const int m_defHeight; + }; +} //namespace cloonel + +#endif diff --git a/src/texture.cpp b/src/texture.cpp new file mode 100644 index 0000000..11ff4b5 --- /dev/null +++ b/src/texture.cpp @@ -0,0 +1,36 @@ +#include "texture.hpp" +#include "sdlerror.hpp" +#include "sdlmain.hpp" +#include +#include + +namespace cloonel { + Texture::Texture (const std::string& parPath, SDLMain* parMain, bool parLoadNow) : + m_path(parPath), + m_texture(nullptr), + m_sdlmain(parMain) + { + if (parLoadNow) + Reload(); + } + + Texture::~Texture() noexcept { + Destroy(); + } + + void Texture::Destroy() noexcept { + if (m_texture) { + SDL_DestroyTexture(m_texture); + m_texture = nullptr; + } + } + + void Texture::Reload() { + Destroy(); + SDL_Surface* surf = SDL_LoadBMP(m_path.c_str()); + if (nullptr == surf) + throw std::runtime_error(GetFullErrorMessage(__PRETTY_FUNCTION__, m_path)); + + m_texture = SDL_CreateTextureFromSurface(m_sdlmain->GetRenderer(), surf); + } +} //namespace cloonel diff --git a/src/texture.hpp b/src/texture.hpp new file mode 100644 index 0000000..ac76c27 --- /dev/null +++ b/src/texture.hpp @@ -0,0 +1,27 @@ +#ifndef id0F37904CB7274575B7E9419E615DA250 +#define id0F37904CB7274575B7E9419E615DA250 + +#include + +struct SDL_Texture; + +namespace cloonel { + class SDLMain; + + class Texture { + public: + Texture ( const std::string& parPath, SDLMain* parMain, bool parLoadNow ); + ~Texture ( void ) noexcept; + + void Reload ( void ); + void Destroy ( void ) noexcept; + bool IsLoaded ( void ) const { return nullptr != m_texture; } + + private: + const std::string m_path; + SDL_Texture* m_texture; + SDLMain* const m_sdlmain; + }; +} //namespace cloonel + +#endif