From 35573e9f287ee4e6e7b668ae72534ee81f2897ed Mon Sep 17 00:00:00 2001 From: Nicolas Brignol Date: Wed, 9 Apr 2014 15:17:40 +0200 Subject: [PATCH] Step one refactoring : Created Actor and renderer. --- buildAndRunOsx.sh | 2 +- src/Actor.cpp | 9 +++++ src/Actor.h | 16 ++++++++ src/Sdl2Renderer.cpp | 87 +++++++++++++++++++++++++++++++++++++++++++ src/Sdl2Renderer.h | 27 ++++++++++++++ src/main.cpp | 35 ++++++++++++++++++ src/test.cpp | 88 -------------------------------------------- 7 files changed, 175 insertions(+), 89 deletions(-) create mode 100644 src/Actor.cpp create mode 100644 src/Actor.h create mode 100644 src/Sdl2Renderer.cpp create mode 100644 src/Sdl2Renderer.h create mode 100644 src/main.cpp delete mode 100644 src/test.cpp diff --git a/buildAndRunOsx.sh b/buildAndRunOsx.sh index c1f13dd..06ca77e 100644 --- a/buildAndRunOsx.sh +++ b/buildAndRunOsx.sh @@ -1 +1 @@ -g++ -framework SDL2 -framework SDL2_Image src/test.cpp && ./a.out +g++ -framework SDL2 -framework SDL2_Image src/*.cpp && ./a.out diff --git a/src/Actor.cpp b/src/Actor.cpp new file mode 100644 index 0000000..fff8624 --- /dev/null +++ b/src/Actor.cpp @@ -0,0 +1,9 @@ +#include "Actor.h" + +Actor::Actor(){ + this->x = 0; + this->y = 0; + this->width = 0; + this->height = 0; + this->name = "unknown"; +} diff --git a/src/Actor.h b/src/Actor.h new file mode 100644 index 0000000..902ace9 --- /dev/null +++ b/src/Actor.h @@ -0,0 +1,16 @@ +#ifndef ACTORH +#define ACTORH + +#include + +class Actor { + public: + Actor(); + int x; + int y; + int width; + int height; + + std::string name; +}; +#endif \ No newline at end of file diff --git a/src/Sdl2Renderer.cpp b/src/Sdl2Renderer.cpp new file mode 100644 index 0000000..e74df7b --- /dev/null +++ b/src/Sdl2Renderer.cpp @@ -0,0 +1,87 @@ + +#include "Sdl2Renderer.h" + +Sdl2Renderer::Sdl2Renderer(std::string winTitle, int winWidth, int winHeight){ + this->windowTitle = winTitle; + this->windowHeight = winHeight; + this->windowWidth = winWidth; + this->window = NULL; + this->screen = NULL; +} + +void Sdl2Renderer::start(){ + std::cout << "Starting SDL2 Renderer..." << std::endl; + + + if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { + std::cerr << "SDL init fail. SDL_Error : " << SDL_GetError() << std::endl; + return this->stop(); + } + + if (IMG_Init( IMG_INIT_PNG ) < 0) { // IMG_INIT_JPG | IMG_INIT_PNG + std::cerr << "SDL init IMG fail. SDL_Error : " << IMG_GetError() << std::endl; + return this->stop(); + } + + this->window = SDL_CreateWindow( this->windowTitle.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, this->windowWidth, this->windowHeight, SDL_WINDOW_SHOWN ); + + if( window == NULL ) { + std::cerr << "SDL CreateWindow fail. SDL_Error : " << SDL_GetError() << std::endl; + return this->stop(); + } + + this->screen = SDL_GetWindowSurface( window ); +} + + +void Sdl2Renderer::stop(){ + std::cout << "Stopping SDL2 Renderer." << std::endl; + + SDL_FreeSurface( this->screen ); + SDL_DestroyWindow( this->window ); + + IMG_Quit(); + SDL_Quit(); +} + + +void Sdl2Renderer::showActor(Actor * actor) { + SDL_Surface * heroImage = IMG_Load( actor->name.c_str() ); + + if( heroImage == NULL ) { + std::cerr << "SDL LoadBMP fail. SDL_Error : " << SDL_GetError() << std::endl; + return this->stop(); + } + + SDL_Rect destRect; + destRect.x = actor->x; + destRect.y = actor->y; + destRect.w = actor->width; + destRect.h = actor->height; + + SDL_FillRect( this->screen, NULL, SDL_MapRGB( this->screen->format, 0xAA, 0xAA, 0xAA ) ); + SDL_BlitSurface( heroImage, NULL, this->screen, &destRect ); + SDL_UpdateWindowSurface( this->window ); + + SDL_FreeSurface( heroImage ); +} + +void Sdl2Renderer::waitForExit(){ + SDL_Event e; + bool running = true; + + while(running) { + + if (SDL_PollEvent( &e ) == 0) { + continue; + } + + //std::cout << "Event fired : " << e.type << std::endl; + + if( e.type == SDL_QUIT ) { + running = false; + std::cout << "Event was QUIT : will quit." << std::endl; + } + + } +} diff --git a/src/Sdl2Renderer.h b/src/Sdl2Renderer.h new file mode 100644 index 0000000..c58476e --- /dev/null +++ b/src/Sdl2Renderer.h @@ -0,0 +1,27 @@ +#ifndef SDL2RENDERERH +#define SDL2RENDERERH + +#include +#include +#include +#include + +#include "Actor.h" + +class Sdl2Renderer { + protected: + int windowHeight; + int windowWidth; + std::string windowTitle; + + SDL_Window * window; + SDL_Surface * screen; + + public: + Sdl2Renderer(std::string winTitle, int winWidth, int winHeight); + void start(); + void stop(); + void showActor(Actor * actor); + void waitForExit(); +}; +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..69e8c12 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,35 @@ +#include +#include +#include + +#include "Actor.h" +#include "Sdl2Renderer.h" + +const int WINDOW_WIDTH = 640; +const int WINDOW_HEIGHT = 480; + +int main( int argc, char* args[] ) +{ + + std::cout << "Testing SDL2..." << std::endl; + + Actor * hero = new Actor(); + hero->name = "hero.png"; + hero->width = 100; + hero->height = 158; + hero->x = (WINDOW_WIDTH/2) - hero->width/2; + hero->y = (WINDOW_HEIGHT/2) - hero->height/2; + + Sdl2Renderer * renderer = new Sdl2Renderer("Incrementalist step 1", WINDOW_WIDTH, WINDOW_HEIGHT); + renderer->start(); + renderer->showActor(hero); + renderer->waitForExit(); + renderer->stop(); + + delete hero; + delete renderer; + + std::cout << "...Done testing SDL2." << std::endl; + + return 0; +} \ No newline at end of file diff --git a/src/test.cpp b/src/test.cpp deleted file mode 100644 index 07982ed..0000000 --- a/src/test.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include -#include -#include - -const int WINDOW_WIDTH = 640; -const int WINDOW_HEIGHT = 480; - -int main( int argc, char* args[] ) -{ - - std::cout << "Testing SDL2..." << std::endl; - - - SDL_Window * window = NULL; - SDL_Surface * screenSurface = NULL; - SDL_Surface * heroImage = NULL; - - if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { - std::cerr << "SDL init fail. SDL_Error : " << SDL_GetError() << std::endl; - SDL_Quit(); - return 1; - } - - if (IMG_Init( IMG_INIT_PNG ) < 0) { // IMG_INIT_JPG | IMG_INIT_PNG - std::cerr << "SDL init IMG fail. SDL_Error : " << IMG_GetError() << std::endl; - SDL_Quit(); - return 1; - } - - window = SDL_CreateWindow( "INCREMENTALIST step 1", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN ); - - if( window == NULL ) { - std::cerr << "SDL CreateWindow fail. SDL_Error : " << SDL_GetError() << std::endl; - SDL_Quit(); - return 1; - } - - screenSurface = SDL_GetWindowSurface( window ); - - //heroImage = SDL_LoadBMP( "hero.bmp" ); - heroImage = IMG_Load("hero.png"); - - - if( heroImage == NULL ) { - std::cerr << "SDL LoadBMP fail. SDL_Error : " << SDL_GetError() << std::endl; - SDL_DestroyWindow( window ); - SDL_Quit(); - return 1; - } - - SDL_Rect destRect; - destRect.x = (WINDOW_WIDTH/2) - 100/2; - destRect.y = (WINDOW_HEIGHT/2) - 158/2; - destRect.w = 100; - destRect.h = 158; - - SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xAA, 0xAA, 0xAA ) ); - SDL_BlitSurface( heroImage, NULL, screenSurface, &destRect ); - SDL_UpdateWindowSurface( window ); - - //event loop - SDL_Event e; - bool running = true; - - while(running) { - - if (SDL_PollEvent( &e ) == 0) { - continue; - } - - std::cout << "Event fired : " << e.type << std::endl; - - if( e.type == SDL_QUIT ) { - running = false; - std::cout << "Event was QUIT : will quit." << std::endl; - } - - } - - SDL_FreeSurface( heroImage ); - SDL_DestroyWindow( window ); - IMG_Quit(); - SDL_Quit(); - - std::cout << "...Done testing SDL2." << std::endl; - - return 0; -} \ No newline at end of file