From: Gabriel C. <gab...@da...> - 2001-04-03 04:36:16
|
On Sun, 1 Apr 2001, Wuzzeb wrote: > Anyone completed the assignment? It was moving a > picture around the screen. If no one posts a > solution, I will post one in a couple of days. > > Wuzzeb Yeah, I have, here goes: ( This expects a file named sdl.bmp to live in the directory you run it from. ) ------ cut here ------ #include <stdio.h> #include <stdlib.h> #include "SDL.h" #define MSTEP 4 #define FILETOOPEN "sdl.bmp" struct Ipos_t { SDL_Rect pos; SDL_Surface *bitmap; }; int main( void ) { SDL_Surface *window = NULL; int win_w, win_h; struct Ipos_t image; Uint32 cBlack; bool done = false; int updown = 0; SDL_Surface *buffer; SDL_Event event; if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError() ); exit(1); } window = SDL_SetVideoMode( 640, 480, 16, SDL_SWSURFACE ); if( !window ) { fprintf(stderr,"Unable to open a surface. Bye.\n"); exit(1); } buffer = SDL_CreateRGBSurface( SDL_SWSURFACE, 640, 480, 16, 0, 0, 0, 0); if( !buffer ) { fprintf( stderr, "Unable to create double buffer. Bye.\n"); exit(1); } image.bitmap = SDL_LoadBMP( FILETOOPEN ); if( ! image.bitmap ) { fprintf(stderr, "Unable to load sdl.bmp. Bye bye.\n"); exit(1); } image.pos.w = image.bitmap->w; image.pos.h = image.bitmap->h; image.pos.x = image.pos.y = 0; win_w = window->w; win_h = window->h; cBlack = SDL_MapRGB( window->format, 0, 0, 0 ); fprintf(stderr, "Controls: Left arrow moves it left, " "moves it right. Up = up, down = down. Q = quit.\n"); while( !done ) { // do Event Polling while( SDL_PollEvent( &event )) { switch( event.type ) { case SDL_KEYDOWN: updown=1; case SDL_KEYUP: { switch( event.key.keysym.sym ) { case SDLK_q: done = true; break; case SDLK_UP: if( updown ) { if( image.pos.y > 0+MSTEP ) image.pos.y -= MSTEP; } break; case SDLK_DOWN: if( updown ) { if( image.pos.y < (win_h - image.pos.h -MSTEP)) image.pos.y += MSTEP; } break; case SDLK_LEFT: if( updown ) { if( image.pos.x > 0+MSTEP ) image.pos.x -=MSTEP; } break; case SDLK_RIGHT: if( updown ) { if( image.pos.x < (win_w - image.pos.w -MSTEP)) image.pos.x += MSTEP; } break; default: // printf("Key was %d\n", // event.key.keysym.sym ); break; } } updown = 0; break; case SDL_QUIT: done = true; break; } } SDL_FillRect( buffer, NULL, cBlack ); SDL_BlitSurface( image.bitmap, 0, buffer, &image.pos ); SDL_UpdateRect( buffer, 0, 0, 0, 0 ); SDL_BlitSurface( buffer, 0, window, 0 ); SDL_UpdateRect( window, 0, 0, 0, 0 ); } SDL_FreeSurface( image.bitmap ); return 0; } _______________________________________________ cpptraining mailing list cpp...@ma... https://mail.worldforge.org/lists/listinfo/cpptraining |