Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1427528
  • 博文数量: 430
  • 博客积分: 9995
  • 博客等级: 中将
  • 技术积分: 4388
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-24 18:04
文章存档

2013年(1)

2008年(2)

2007年(14)

2006年(413)

分类: LINUX

2006-05-26 11:33:03

Last Updated 2/21/06
You've already done some simple event handling with SQL_QUIT, now this tutorial will teach you to detect when a key is pressed, and how to check which key it was.
//Generate the message surfaces up = TTF_RenderText_Solid( font, "Up was pressed.", textColor ); down = TTF_RenderText_Solid( font, "Down was pressed.", textColor ); left = TTF_RenderText_Solid( font, "Left was pressed", textColor ); right = TTF_RenderText_Solid( font, "Right wa pressed", textColor );
After everything is initialized and loaded, we generate all 4 message surfaces before hand.

I probably should have checked for errors when rendering the text but ....eh, less typing for me.
//If there's an event to handle if( SDL_PollEvent( &event ) ) { //If a key was pressed if( event.type == SDL_KEYDOWN ) {
Now when we want to check when a key is pressed, we check for event type SDL_KEYDOWN.
//Set the proper text switch( event.key.keysym.sym ) { case SDLK_UP: message = up; break; case SDLK_DOWN: message = down; break; case SDLK_LEFT: message = left; break; case SDLK_RIGHT: message = right; break; } } //If the user has Xed out the window else if( event.type == SDL_QUIT ) { //Quit the program quit = true; } }
Now if a key was pressed, we need to check which key it was.

SDL_PollEvent() put the SDL_KEYDOWN data in the event structure as a SDL_KeyboardEvent named "key":

and inside of "key" is the is a keysym structure:

and inside of the keysym is the SDL_Key named "sym", which is which key was pressed.

If the up arrow was pressed, the sym will be SDLK_UP and we'll set the message to be up, if the down arrow was pressed, the sym will be SDLK_DOWN and we'll set the message to be down, etc, etc.

We also check if the user wants to X out the window, and handle it accordingly.
//If a message needs to be displayed if( message != NULL ) { //Apply the images to the screen apply_surface( 0, 0, background, screen ); apply_surface( ( SCREEN_WIDTH - message->w ) / 2, ( SCREEN_HEIGHT - message->h ) / 2, message, screen ); //Null the surface pointer message = NULL; } //Update the screen if( SDL_Flip( screen ) == -1 ) { return 1; }
When the message surface isn't pointing to anything it'll be NULL and nothing will be blitted. When the message surface is pointing to something we'll apply the background, then apply the message surface centered on the screen.

The way you center a surface is to first subtract the width/height from the width/height of the surface you're blitting to. Since when surface is centered the padding on both sides is equal, you divide the remaining distance into two equal halves.

After that we reset the message to NULL, then we update the screen.
Download the media and source code for this tutorial .
阅读(548) | 评论(0) | 转发(0) |
0

上一篇:True Type Fonts

下一篇:Timers

给主人留下些什么吧!~~