Jump to content
  • Advertisement
Sign in to follow this  
thedevil

problem taking input from the keyboard in SDL

This topic is 5200 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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); }

Share this post


Link to post
Share on other sites
Advertisement
Try replacing


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 = yes

while(!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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!