Created
January 10, 2013 21:43
-
-
Save hobnob/4506055 to your computer and use it in GitHub Desktop.
SDL 2.0 Proof of Concept code
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
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_image.h> | |
namespace core | |
{ | |
class Game | |
{ | |
private: | |
SDL_Window* window; // Declare a pointer to an SDL_Window | |
SDL_Texture* Surf_Image; | |
SDL_Renderer* renderer; | |
int x; | |
bool Running; | |
public: | |
void init() | |
{ | |
Running = true; | |
x = 0; | |
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) { | |
//return false; | |
} | |
// Create an application window with the following settings: | |
window = SDL_CreateWindow( | |
"An SDL2 window", // window title | |
SDL_WINDOWPOS_UNDEFINED, // initial x position | |
SDL_WINDOWPOS_UNDEFINED, // initial y position | |
640, // width, in pixels | |
480, // height, in pixels | |
SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL // flags - see below | |
); | |
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); | |
//SDL_Surface* Surf_Temp = NULL; | |
if((Surf_Image = IMG_LoadTexture(renderer, "loading-text.png")) == NULL) { | |
Running = false; | |
} | |
//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; | |
SDL_Rect SrcR; | |
SrcR.x = 0; | |
SrcR.y = 0; | |
SrcR.w = DestR.w = 210; | |
SrcR.h = DestR.h = 79; | |
SDL_RenderClear(renderer); | |
SDL_RenderCopy (renderer,Surf_Image,&SrcR,&DestR); | |
SDL_RenderPresent(renderer); | |
} | |
} | |
void playSounds(){} | |
void clean(){ | |
SDL_DestroyTexture(Surf_Image); | |
SDL_DestroyRenderer(renderer); | |
// Close and destroy the window | |
SDL_DestroyWindow(window); | |
SDL_Quit(); | |
} | |
bool shouldExit(){ return !Running; } | |
}; | |
} |
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
#include <SDL2/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(60); | |
} | |
return 0; | |
} |
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
all: | |
#Compile on the native platform | |
g++ main.cpp -o main -lSDL2 -lSDL2_image -Wl,-rpath=/usr/local/lib |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment