clooneljump/src/main.cpp

64 lines
1.8 KiB
C++
Raw Normal View History

2014-01-09 23:04:39 +00:00
#include <SDL2/SDL.h>
#include <iostream>
#include "CloonelJumpConfig.h"
#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, 640, 480, 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;
try {
InitSDL(sdlstuff);
}
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;
}