Code refactored into different files.
This commit is contained in:
parent
26c50fe1d0
commit
2429229a95
8 changed files with 217 additions and 46 deletions
|
@ -24,6 +24,9 @@ configure_file(
|
||||||
|
|
||||||
add_executable(${PROJECT_NAME}
|
add_executable(${PROJECT_NAME}
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
|
src/texture.cpp
|
||||||
|
src/sdlerror.cpp
|
||||||
|
src/sdlmain.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(${PROJECT_NAME}
|
target_link_libraries(${PROJECT_NAME}
|
||||||
|
|
50
src/main.cpp
50
src/main.cpp
|
@ -1,65 +1,23 @@
|
||||||
#include <SDL2/SDL.h>
|
|
||||||
#include <iostream>
|
|
||||||
#include "CloonelJumpConfig.h"
|
#include "CloonelJumpConfig.h"
|
||||||
|
#include "sdlmain.hpp"
|
||||||
|
#include <iostream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
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
|
///following http://twinklebeardev.blogspot.co.uk/2012/07/lesson-1-hello-world.html
|
||||||
///----------------------------------------------------------------------------
|
///----------------------------------------------------------------------------
|
||||||
int main() {
|
int main() {
|
||||||
std::cout << GameName << " v" << GameVersionMajor << "." << GameVersionMinor << std::endl;
|
std::cout << GameName << " v" << GameVersionMajor << "." << GameVersionMinor << std::endl;
|
||||||
|
|
||||||
InitSDLStuff sdlstuff;
|
cloonel::SDLMain sdlmain(GameName, DEF_WIN_WIDTH, DEF_WIN_HEIGHT);
|
||||||
try {
|
try {
|
||||||
InitSDL(sdlstuff);
|
sdlmain.Init();
|
||||||
}
|
}
|
||||||
catch (const std::runtime_error& e) {
|
catch (const std::runtime_error& e) {
|
||||||
std::cerr << "Error during SDL2 initialization:\n";
|
std::cerr << "Error during SDL2 initialization:\n";
|
||||||
std::cerr << e.what() << std::endl;
|
std::cerr << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
ClearIFN(sdlstuff);
|
|
||||||
std::cout << "Quitting now" << std::endl;
|
std::cout << "Quitting now" << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
32
src/sdlerror.cpp
Normal file
32
src/sdlerror.cpp
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
#include "sdlerror.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <ciso646>
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
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
|
11
src/sdlerror.hpp
Normal file
11
src/sdlerror.hpp
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#ifndef idE2DD0E628C364995868580C60903B0BF
|
||||||
|
#define idE2DD0E628C364995868580C60903B0BF
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace cloonel {
|
||||||
|
std::string GetFullErrorMessage ( const char* parFunction, const std::string& parMessage );
|
||||||
|
void ShowError ( const char* parFunction, const std::string& parMessage );
|
||||||
|
} //namespace cloonel
|
||||||
|
|
||||||
|
#endif
|
74
src/sdlmain.cpp
Normal file
74
src/sdlmain.cpp
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
#include "sdlmain.hpp"
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
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
|
30
src/sdlmain.hpp
Normal file
30
src/sdlmain.hpp
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
#ifndef id8E7A054DAC9040B887F2620EFD229EE8
|
||||||
|
#define id8E7A054DAC9040B887F2620EFD229EE8
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
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<LocalData> m_localData;
|
||||||
|
const int m_defWidth;
|
||||||
|
const int m_defHeight;
|
||||||
|
};
|
||||||
|
} //namespace cloonel
|
||||||
|
|
||||||
|
#endif
|
36
src/texture.cpp
Normal file
36
src/texture.cpp
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
#include "texture.hpp"
|
||||||
|
#include "sdlerror.hpp"
|
||||||
|
#include "sdlmain.hpp"
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
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
|
27
src/texture.hpp
Normal file
27
src/texture.hpp
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
#ifndef id0F37904CB7274575B7E9419E615DA250
|
||||||
|
#define id0F37904CB7274575B7E9419E615DA250
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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
|
Loading…
Reference in a new issue