🎉 Celebrating 25 Years of GameDev.net! 🎉
Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!
problem taking input from the keyboard in SDL
im trying to create a progrm which takes a bmp image as a parameter (as an exercise), i have succeded in displaying the image but i would like the image to be diplayed until a key has been pressed, and i haven't had much luck.
here is the code i work in visual studio not by choice:
/* -- Include the precompiled libraries -- */
#ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif
# include "SDL.h"
# include <stdio.h>
# include <stdlib.h>
void ShowBMP(char *file, SDL_Surface *screen, int x, int y);
int x = 0,y = 0,i = 0;
int main(int argc, char **argv)
{
/* start SDL */
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
fprintf( stderr, "Video initialization failed: %s\n",SDL_GetError( ) );
SDL_Quit( );
}
/* set screen var */
SDL_Surface *screen;
screen = SDL_SetVideoMode(1024, 768, 32, SDL_SWSURFACE|SDL_ANYFORMAT);
if ( screen == NULL )
{
fprintf(stderr, "Couldn't set 1024x768x32 video mode: %s\n",SDL_GetError());
SDL_Quit();
}
/* set event var's */
SDL_Event event;
SDL_WaitEvent(&event);
if(event.type != SDL_KEYDOWN)
ShowBMP(argv[1] , screen, x, y);
/* quit SDL */
SDL_Quit( );
return 0;
}
void ShowBMP(char *file, SDL_Surface *screen, int x, int y)
{
SDL_Surface *image;
SDL_Rect dest;
/* Load the BMP file into a surface */
image = SDL_LoadBMP(file);
if ( image == NULL ) {
fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
return;
}
/* Blit onto the screen surface.
The surfaces should not be locked at this point.
*/
dest.x = x;
dest.y = y;
dest.w = image->w;
dest.h = image->h;
SDL_BlitSurface(image, NULL, screen, &dest);
/* Update the changed portion of the screen */
SDL_UpdateRects(screen, 1, &dest);
/* Clear the image surface */
SDL_FreeSurface(image);
}
Try replacing
with:
Remember to put SDL_QUIT in the switch to take care of when you hit the close button.
SDL_Event event;SDL_WaitEvent(&event);if(event.type != SDL_KEYDOWN) ShowBMP(argv[1] , screen, x, y);
with:
SDL_Event event;int iDone = 0; // variable used to get out of the loop // think, Am I done, 0 = no, 1 = yeswhile(!iDone){ while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_KEYUP: // A key has been released iDone = 1; // get out of the loop break; default: break; } } ShowBMP(argv[1], screen, x, y);}
Remember to put SDL_QUIT in the switch to take care of when you hit the close button.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement