All pastes #281342 Raw Edit

iamwill

public text v1 · immutable
#281342 ·published 2006-12-16 18:39 UTC
rendered paste body

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
#include <SDL_mouse.h>


SDL_Surface *screen = NULL;
SDL_Surface *image = NULL;
SDL_Surface *background = NULL;
SDL_Rect rect;
int x, y;


bool init()
{
  if (SDL_Init (SDL_INIT_VIDEO) < 0)
    atexit (SDL_Quit);
    screen = SDL_SetVideoMode (720, 480, 16, SDL_SWSURFACE | SDL_DOUBLEBUF); 
    SDL_WM_SetCaption ("EVM-3015", NULL); 
    image = SDL_LoadBMP("images/ship.bmp"); 
      rect.x = 0; 
      rect.y = 0; 
    SDL_EnableKeyRepeat(5, 4);
	background = SDL_LoadBMP("images/grid.bmp");
		x = 0;
		y = 0;
  return true; 
}


void draw () 
{ 
    Uint32 color; 
    color = SDL_MapRGB (screen->format, 0, 0, 0); 
    SDL_FillRect (screen, NULL, color); 
    SDL_BlitSurface(background, NULL, screen, NULL);
    SDL_BlitSurface(image, NULL, screen, &rect); 
    SDL_Flip (screen); 
    SDL_Delay (1); 
}


int main (int argc, char *argv[]) 
{ 
    int done = 0; 
    Uint8 *keys; 
    init(); 
    while (!done) 
    { 
      SDL_Event event; 
      while (SDL_PollEvent (&event)) 
      { 
        switch (event.type) 
        { 
		case SDL_MOUSEMOTION:
          	SDL_ShowCursor(0);
		SDL_GetMouseState(&x, &y);
            		rect.x = x - 32;
			rect.y = y - 32;
		break;

          case SDL_KEYDOWN:
          keys = SDL_GetKeyState(NULL);
            if ( keys[SDLK_ESCAPE] ) { done = 1;}
	    if ( keys[SDLK_LEFT] ) {rect.x--;}
	    if ( keys[SDLK_RIGHT] ) {rect.x++;}
	    if ( keys[SDLK_UP] ) {rect.y--;}
	    if ( keys[SDLK_DOWN] ) {rect.y++;}
          break;
 
 
 
           case SDL_QUIT: 
            done = 1; 
            break; 
          default: 
            break; 
        }
       }
      draw ();
    }

    return 0;
}