Learn More

Wednesday, August 17, 2011

Basic Using Keyboard To Control The Event

game result
Hi everybody, we meet again in the topic about how to make a game with c++. Now, let’s continue our discussion. But first, if you don’t understand about c++ very well, then please follow the tutorial of c++ from us. We discuss it step by step. We all know that every files of the game always contains so many images. But in the previous post, we just discuss it only for one file of image. Not only that, all game must be able to controled by using keyboard, mouse, joystick or else. For that purpose, we want to continue our discussion about basic load many files and basic control for game.

Select libraries

Yes, we must select libraries for our program. Of course, we need SDL.h and SDL_image.h as external libraries. Also we use the string as additional library, because we want to make a class for loading many files. With this way, we will be able to control algorithm of our program. In here, we use namespace just to make sure

Global variables

All global variables needed by a program very depend on the creator. We using wg, wb, and bg as global variables not only for size of game windows application but also for several condition. And that condition you will know after this.

Generate the surface and event

SDL_Surface's represent areas of "graphical" memory, memory that can be drawn to. And in here we using pointer because we want to get the actual data of graphics.  We give a free store (signed by NULL) for SDL_surface because every pointer that not pointing to anything is a good step if we give NULL first. On the previous post, SDL_Event located inside of “int main”. But, for now and next if you still do that your programs will not work. So that’s way we put SDL_Event outside of “int main”.

Function for loading images

On our explanation above we must be able to loading many files by using one command. And for that purpose we must make a function for loading images. Remember, we will try to loading image with the non bmp extensions. So using IMG_Load from SDL_image is a good idea. But first please give NULL for SDL_Surface for images. Images need a free place before come to your game. SDL_DisplayFormat is using to takes a surface and copies it to a new surface of the pixel format and colors of the video framebuffer, suitable for fast blitzing onto the display surface. Without this, you will not be able to show the picture of game. Because in here, SDL_Surface is only takes a place for your images in computer memory. Keep in your main, after using SDL_Surface follow it by SDL_Display Format. SDL_FreeSurface is aimed to free the resources used by a previously created SDL_Surface. Basically, animation in a game is draw image several times. If you don’t clear previous image then old image will still on windows application during the games.

Function for draw image

Imagine if you have so many files to draw and then you must write it one by one. We don’t want to do that. And we need to make a new function for draw image once. See support files from us for this tutorial. If you don’t have it please go to Loading Image From Another Extention or download support files here. In sprites.png you will see so many photos. Every photo locates on the different pixels. Let say, you want to choose a tank on the upper left corner with the size width = 35 px and height = 25 px, you can use Paint application in operating windows. Then it photos define by SDL_Rect as a source.  After you choose that tank, you place on you windows everywhere. Of course depend on you. Let say, we define before that our game has width = 320 px and height = 240 px. And you want to place the source in coordinate x = 100 px and y = 100 px. This source on game windows application named as destination by SDL_Rect. The next step is blit it as fast as possible by using SDL_BlitSurface. Remember try to always follow SDL_Rect with SDL_BlitSurface.

sdl_rect process

Function for initial of SDL and function for check files

Basically, for initial is same with previous, the different is we using a Boolean. Also it happens for check files. If you are not giving specified address for your images. So you must, put the image inside of game project folder in your computer. If you don’t do it that your computer will give information that your program is not true.

Function for clean up your memory.

This also same with the previous post. The different here is we using SDL_Quit, and not using atexit. Because atexit is not always success for advance programs.

Inside “int main”

The different between this script than before on our post is we are using classes.


SDL_Event has two uses:
Reading events
Placing events
There are 14 event structure for SDL_Event: type, active, key, motion, button, jaxis, jball, jhat, jbutton, resize, expose, quit, user, and syswm. Reading events from the event queue is done with either SDL_PollEvent or SDL_PeepEvents. But it is still different, SDL_PollEvent aimed for poll pending events. But SDL_PeepEvents is for checks the event queue for messages and optionally returns them. So please select what you need. Because SDL_Event contains 14 structure data then when we want to handle those, we must using logic process. If there are no events then SDL_PollEvents will return 0. Type structure data is define as the type of event. There are 13 type of event: SDL_ACTIVEEVENT, SDL_KEYUP/DOWN, SDL_MOUSEMOTION, SDL_MOUSEBUTTONDOWN/UP, SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYBUTTONDOWN/UP, SDL_QUIT, SDL_SYSWMEVENT, SDL_VIDEORESIZE, SDL_VIDEOEXPOSE, SDL_USEREVENT. In this tutorial, we using SDL_QUIT (for quit a game) and SDL_KEYDOWN (for detect key button which pressed).

Next step

Basically, the next step is same with previous post. Please run and save your project. If you success run your project then the image will same with the first image of this posting. We hope you understand. Thank you. Please download the source code for this tutorial.

Post a Comment