Skip to content

Instantly share code, notes, and snippets.

@hobnob
Last active December 10, 2015 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hobnob/4505662 to your computer and use it in GitHub Desktop.
Save hobnob/4505662 to your computer and use it in GitHub Desktop.
SDL 1.2 Proof of Concept code
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
namespace core
{
class Game
{
private:
SDL_Surface* Surf_Display;
SDL_Surface* Surf_Image;
int x;
bool Running;
public:
void init()
{
Running = true;
x = 0;
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
//return false;
}
if((Surf_Display = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL) {
//return false;
}
SDL_Surface* Surf_Temp = NULL;
if((Surf_Temp = IMG_Load("loading-text.png")) == NULL) {
Running = false;
}
else
{
Surf_Image = SDL_DisplayFormatAlpha(Surf_Temp);
SDL_FreeSurface(Surf_Temp);
}
//return true;
}
void handleInput(SDL_Event* event){
switch(event->type) {
case SDL_QUIT:
Running = false;
break;
case SDL_KEYDOWN:
if ( SDLK_RIGHT == event->key.keysym.sym )
{
x+=10;
}
else if ( SDLK_LEFT == event->key.keysym.sym )
{
x-=10;
}
break;
}
}
void update(){}
void draw()
{
if ( Surf_Image )
{
SDL_Rect DestR;
DestR.x = x;
DestR.y = 0;
//Clear the screen
SDL_Rect r={0,0,640,480};
SDL_FillRect( Surf_Display, &r, -1 );
//Paint the screen
SDL_BlitSurface(Surf_Image, NULL, Surf_Display, &DestR);
//Bring the backbuffer forward
SDL_Flip(Surf_Display);
}
}
void playSounds(){}
void clean(){
SDL_FreeSurface(Surf_Display);
SDL_Quit();
}
bool shouldExit(){ return !Running; }
};
}
#include <SDL/SDL.h>
#include "./lib/core/Game.cpp"
core::Game game;
SDL_Event event;
void iterate()
{
while(SDL_PollEvent(&event))
{
game.handleInput(&event);
}
game.update();
game.draw();
game.playSounds();
}
int main()
{
game.init();
while( !game.shouldExit() )
{
iterate();
SDL_Delay(20);
}
return 0;
}
all:
#Compile on the native platform
g++ main.cpp -o main -lSDL -lSDL_image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment