Created
July 19, 2012 07:09
-
-
Save tasdomas/3141257 to your computer and use it in GitHub Desktop.
simple SDL example
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
package gc | |
import ( | |
"launchpad.net/gocheck" | |
) | |
func Failure(c *gocheck.C) func() { | |
return func() { | |
// fails immediately | |
c.Fail() | |
} | |
} |
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 "SDL/SDL.h" | |
#include <assert.h> | |
#include <math.h> | |
/* This macro simplifies accessing a given pixel component on a surface. */ | |
#define pel(surf, x, y, rgb) ((unsigned char *)(surf->pixels))[y*(surf->pitch)+x*3+rgb] | |
int main(int argc, char *argv[]) | |
{ | |
int x, y, t; | |
/* Event information is placed in here */ | |
SDL_Event event; | |
/* This will be used as our "handle" to the screen surface */ | |
SDL_Surface *scr; | |
SDL_Init(SDL_INIT_VIDEO); | |
/* Get a 640x480, 24-bit software screen surface */ | |
scr = SDL_SetVideoMode(640, 480, 24, SDL_SWSURFACE); | |
assert(scr); | |
/* Ensures we have exclusive access to the pixels */ | |
SDL_LockSurface(scr); | |
for(y = 0; y < scr->h; y++) | |
for(x = 0; x < scr->w; x++) | |
{ | |
/* This is what generates the pattern based on the xy co-ord */ | |
t = ((x*x + y*y) & 511) - 256; | |
if (t < 0) | |
t = -(t + 1); | |
/* Now we write to the surface */ | |
pel(scr, x, y, 0) = 255 - t; //red | |
pel(scr, x, y, 1) = t; //green | |
pel(scr, x, y, 2) = t; //blue | |
} | |
SDL_UnlockSurface(scr); | |
/* Copies the `scr' surface to the _actual_ screen */ | |
SDL_UpdateRect(scr, 0, 0, 0, 0); | |
/* Now we wait for an event to arrive */ | |
while(SDL_WaitEvent(&event)) | |
{ | |
/* Any of these event types will end the program */ | |
if (event.type == SDL_QUIT | |
|| event.type == SDL_KEYDOWN | |
|| event.type == SDL_KEYUP) | |
break; | |
} | |
SDL_Quit(); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment