Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Step one refactoring : Created Actor and renderer.
- Loading branch information
Showing
7 changed files
with
175 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
g++ -framework SDL2 -framework SDL2_Image src/test.cpp && ./a.out | ||
g++ -framework SDL2 -framework SDL2_Image src/*.cpp && ./a.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include "Actor.h" | ||
|
||
Actor::Actor(){ | ||
this->x = 0; | ||
this->y = 0; | ||
this->width = 0; | ||
this->height = 0; | ||
this->name = "unknown"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file was deleted.
Oops, something went wrong.