Skip to content

Commit

Permalink
Step one refactoring : Created Actor and renderer.
Browse files Browse the repository at this point in the history
  • Loading branch information
nbrignol committed Apr 9, 2014
1 parent e166dc2 commit 35573e9
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 89 deletions.
2 changes: 1 addition & 1 deletion 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
9 changes: 9 additions & 0 deletions 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";
}
16 changes: 16 additions & 0 deletions src/Actor.h
@@ -0,0 +1,16 @@
#ifndef ACTORH
#define ACTORH

#include <string>

class Actor {
public:
Actor();
int x;
int y;
int width;
int height;

std::string name;
};
#endif
87 changes: 87 additions & 0 deletions 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;
}

}
}
27 changes: 27 additions & 0 deletions src/Sdl2Renderer.h
@@ -0,0 +1,27 @@
#ifndef SDL2RENDERERH
#define SDL2RENDERERH

#include <SDL2/SDL.h>
#include <SDL2_Image/SDL_image.h>
#include <iostream>
#include <string>

#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
35 changes: 35 additions & 0 deletions src/main.cpp
@@ -0,0 +1,35 @@
#include <SDL2/SDL.h>
#include <SDL2_Image/SDL_image.h>
#include <iostream>

#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;
}
88 changes: 0 additions & 88 deletions src/test.cpp

This file was deleted.

0 comments on commit 35573e9

Please sign in to comment.