Skip to content

Instantly share code, notes, and snippets.

@nilsso
Created October 2, 2014 18:04
Show Gist options
  • Select an option

  • Save nilsso/c76a3c9037337e30c891 to your computer and use it in GitHub Desktop.

Select an option

Save nilsso/c76a3c9037337e30c891 to your computer and use it in GitHub Desktop.
SDL example
/* Example of initializing SDL */
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_keyboard.h>
#include <cstdlib>
#include <string>
using namespace std;
const int WINDOW_W = 512, WINDOW_H = 512;
// Get window size
//SDL_Rect wRect;
//SDL_GetWindowSize(w, &wRect.x, &wRect.y);
const int THROTTLE = 50;
Uint32 t_cur, t_last = 0;
bool done = false;
const int renderer_flag = SDL_RENDERER_ACCELERATED;
//const int renderer_flag = 0;
SDL_Window *w;
SDL_Renderer *r;
SDL_Event event;
SDL_Surface *load_surf;
SDL_Texture *bgd, *img;
int bgd_w, bgd_h, img_w, img_h;
SDL_Rect src, dest;
string bgd_path = "resources/doom_tex.png";
string img_path = "resources/doomguy.png";
void draw();
void printKeyInfo(SDL_KeyboardEvent *key);
enum KEY { LEFT, RIGHT, UP, DOWN };
bool isKeyDown[4];
int main()
{
dest.x = 0;
dest.y = 0;
// Initialize SDL's video system and check for errors.
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("Unabled to initialize SDL: %s\n", SDL_GetError());
return EXIT_FAILURE;
}
// Make sure SDL_Quit gets called when the program exits.
atexit(SDL_Quit);
// Attempt to create a 256x256 window.
w = SDL_CreateWindow("SDL Test Window",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
WINDOW_W, WINDOW_H, 0);
if (w == NULL) {
printf("Unable to create window: %s\n", SDL_GetError());
return EXIT_FAILURE;
}
// Attempt to create rendering context.
r = SDL_CreateRenderer(w, -1, renderer_flag);
if (r == NULL) {
printf("Unable to create rendering context: %s\n", SDL_GetError());
return EXIT_FAILURE;
}
// Delay (give rendering context some time before creating textures).
SDL_Delay(100);
// Draw black to renderer.
SDL_SetRenderDrawColor(r, 0, 0, 0, 255);
SDL_RenderClear(r);
// Load the background.
load_surf = IMG_Load(bgd_path.c_str());
if (load_surf == NULL) {
printf("Unable to load background image to surface.\n");
return EXIT_FAILURE;
}
bgd = SDL_CreateTextureFromSurface(r, load_surf);
if (bgd == NULL) {
printf("Unable to convert background surface to texture.\n");
return EXIT_FAILURE;
}
bgd_w = load_surf->w;
bgd_h = load_surf->h;
SDL_FreeSurface(load_surf);
// Load the image.
load_surf = IMG_Load(img_path.c_str());
if (load_surf == NULL) {
printf("Unabled to load image to surface.\n");
return EXIT_FAILURE;
}
img = SDL_CreateTextureFromSurface(r, load_surf);
if (img == NULL) {
printf("Unabled to convert image surface to texture.\n");
return EXIT_FAILURE;
}
img_w = load_surf->w;
img_h = load_surf->h;
SDL_FreeSurface(load_surf);
while (!done) {
t_cur = SDL_GetTicks();
if (t_cur > t_last + THROTTLE) {
t_last = t_cur;
//printf("[%.2f]\n", (float)t_cur/1000);
// INPUT
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
done = true;
break;
case SDL_KEYDOWN:
printKeyInfo(&event.key);
switch (event.key.keysym.scancode) {
case SDL_SCANCODE_LEFT:
isKeyDown[LEFT] = true;
break;
case SDL_SCANCODE_RIGHT:
isKeyDown[RIGHT] = true;
break;
case SDL_SCANCODE_UP:
isKeyDown[UP] = true;
break;
case SDL_SCANCODE_DOWN:
isKeyDown[DOWN] = true;
break;
}
break;
case SDL_KEYUP:
switch (event.key.keysym.scancode) {
case SDL_SCANCODE_LEFT:
isKeyDown[LEFT] = false;
break;
case SDL_SCANCODE_RIGHT:
isKeyDown[RIGHT] = false;
break;
case SDL_SCANCODE_UP:
isKeyDown[UP] = false;
break;
case SDL_SCANCODE_DOWN:
isKeyDown[DOWN] = false;
break;
}
break;
default:
break;
}
}
if (isKeyDown[LEFT] xor isKeyDown[RIGHT]) {
if (isKeyDown[LEFT]) {
dest.x--;
} else {
dest.x++;
}
}
if (isKeyDown[UP] xor isKeyDown[DOWN]) {
if (isKeyDown[UP]) {
dest.y--;
} else {
dest.y++;
}
}
// OUTPUT (Render)
draw();
}
}
// Exit
return EXIT_SUCCESS;
}
void draw()
{
// Draw the background.
SDL_RenderCopy(r, bgd, NULL, NULL);
// Draw the image.
//printf("%.2f\n", double (img_w));
src.x = 0;
src.y = 0;
src.w = img_w;
src.h = img_h;
if (src.w > src.h) {
dest.w = 64;
dest.h = (64.0 / src.w) * src.h;
} else {
dest.h = 64;
dest.w = (64.0 / src.h) * src.w;
}
SDL_RenderCopy(r, img, &src, &dest);
SDL_RenderPresent(r);
}
void printKeyInfo(SDL_KeyboardEvent *key)
{
printf("Scancode: 0x%02X", key->keysym.scancode);
printf(", Name: %s\n", SDL_GetKeyName(key->keysym.sym));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment