Skip to content

Instantly share code, notes, and snippets.

@aginor
Last active August 29, 2015 14:27
Show Gist options
  • Save aginor/9cd26ee154600bd4dad9 to your computer and use it in GitHub Desktop.
Save aginor/9cd26ee154600bd4dad9 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#define WIDTH 800
#define HEIGHT 600
#define IMG_PATH "blinky.png"
int main (int argc, char *argv[]) {
// variable declarations
SDL_Window *win = NULL;
SDL_Renderer *renderer = NULL;
SDL_Surface *img = NULL;
SDL_Rect src;
SDL_Rect dst;
// Initialize SDL.
if (SDL_Init(SDL_INIT_VIDEO) < 0)
return 1;
int imgFlags = IMG_INIT_PNG;
if( !( IMG_Init( imgFlags ) & imgFlags ) ) {
printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
exit(1);
}
// create the window
win = SDL_CreateWindow("Image Loading", 0, 0, WIDTH, HEIGHT, 0);
// load our image
img = IMG_Load(IMG_PATH);
if (!img) {
printf("failed to load image\n");
exit(1);
}
/* set up the basic pattern */
for (int i = 0; i < 4; i++) {
for (int j = 0; i < 4; i++) {
dst.x = 172*i;
dst.y = 176*j;
dst.w = 172;
dst.h = 176;
SDL_BlitSurface(img, NULL, SDL_GetWindowSurface(win), &dst);
}
}
/* this is where things fail */
src.x = 0;
src.y = 50;
src.w = WIDTH;
src.h = HEIGHT;
dst = src;
dst.y = 100;
int r = SDL_BlitSurface(SDL_GetWindowSurface(win), &src, SDL_GetWindowSurface(win), &dst);
if (r) {
printf("Blitting failed!\n");
}
/* verify that dst has been altered as expected */
printf("x,y,w,h: %d,%d,%d,%d\n",dst.x, dst.y, dst.w, dst.h);
// main loop
while (1) {
// event handling
SDL_Event e;
if ( SDL_PollEvent(&e) ) {
if (e.type == SDL_QUIT)
break;
else if (e.type == SDL_KEYUP && e.key.keysym.sym == SDLK_ESCAPE)
break;
}
// SDL_BlitSurface(img, NULL, SDL_GetWindowSurface(win), NULL);
SDL_UpdateWindowSurface(win);
SDL_Delay(10);
}
SDL_FreeSurface(img);
SDL_DestroyWindow(win);
return 0;
}
@aginor
Copy link
Author

aginor commented Aug 15, 2015

blinky

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment