last article ( time to play) I introduced the concept of timing in games, in this article I will introduce the basic concepts of design and movement of objects on the screen using the SDL libraries .
start by defining some constants, the width and height of one of our hypothetical character on the screen:
# define PLAYER_W 10 / / # define Width
PLAYER_H 10 / / Height
In this case, your character will have 'a form strictly square, we can now turn to the definition of a structure designed to contain other useful information.
Create the data type " Player" for example, as follows:
struct _Player
{int
x, y; / / Coordinates of the player
Player };
Two simple integer variables to manage separately the coordinates of our players in the game window, these coordinates are incremented and decremented by pressing keys on the keyboard.
on the coordinates of your character will be 'designed his image, to comfort me' I've enclosed everything in a function:
void drawRect (int x, int y, int width, int height, int color)
rect.x = {x, y =
rect.y;
rect.w = width, height =
rect.h;
SDL_FillRect (screen, & rect, color);
}
This feature takes advantage of structures already 'declared inside the SDL library, including SDL_Surface and SDL_Rect .
We declare as global variables of this type on top of our source:
SDL_Surface * screen; / / The entire playing area
SDL_Rect rect; / / An area for design
Within the surface " screen" will be settled as in a collage of different images represented "rect ," all is done by the function " SDL_FillRect () .
Now we just 'call the drawRect function in a similar way to draw our character on the screen:
drawRect (Player.x, Player.y, PLAYER_W, PLAYER_H, 0xffffff);
about colors and 'a simple hexadecimal value as those used in the HTML page for color, in this case, your character will be' white. This function will be 'called in the main game loop for each cycle by drawing a colored rectangle to the coordinates of the specified size "current" player.
To increase or decrease the coordinates of the player nostor step by step and then write our function getInput () 'that will' also used later in the main game loop:
getInput int () {
while (SDL_PollEvent (& event)) {
if (event.type == SDL_QUIT executes it and takes it away from the queue.
In this case, check to see if you and 'the will' to exit the program by checking whether pressing the ESC key, in case of positivity 'assign a variable to the value of
quit
1, this variable will meet more' forward during the spring.
/ / Check the status of the keyboard KeyState SDL_GetKeyState = (NULL); / * Handles the key press * / if
(KeyState [SDLK_LEFT]) = -1 Player.x ;
if
(KeyState [SDLK_RIGHT])
Player.x = 1;
if (KeyState [SDLK_UP]) Player.y = -1;
if (KeyState [SDLK_DOWN]) Player.y = 1;}
Here we find the variable "KeyState" associated with the function
SDL_GetKeyState () , This function returns us the status of a given key as an argument passed to it. values \u200b\u200b"
SDLK_
" are numerical constants defined in the SDL library refer to the ASCII values \u200b\u200bof the keys. In this case it checks if a certain button and 'down, if so is increased by a coordinate of the player. If the input listed below were handled in the polling cycle as the ESC key, you should continually re-press button to increase or decrease the coordinates of the player.
This method allows us to manage the input gain control "continuous" pressure of any key at every cycle. Take note that the variables KeyState, quit and event are not defined in the function, will be defined in a comprehensive way as follows: SDL_Event event;
/ / Structure for events
Uint8 *
KeyState ;
/ / keyboard state
int quit = 0;
Now that we have the function of the drawing for the management of the movements of our players, we can assemble the , insert our management function of timing as in the previous article :
fps_sync int () { SDL_GetTicks t = ();
if (t - tl> = frequency)
{temp = (t - tl) / frequency;
tl + = temp * frequency;
return temp;}
else {
SDL_Delay (frequency - (t - tl )); tl + = frequency;
return 1;}
}
Now we implement our main game loop in a specific function, then called him back within a loop.
int MainLoop () {
/ / main game loop
while (! Quit) {
fps_sync repeat = ();
for (i = 0; i {
getInput ();}
As already 'seen in the article "Time to play , limit the number of game actions executed per second. We also note the presence of a < style="font-weight: bold;"> !
quit as an argument in
while, until 'will quit' equal to 0, the cycle will continue '.
/ / Clear the entire game screen SDL_FillRect (screen, NULL, SDL_MapRGB (screen-> format, 0, 0, 0)); / / Draw the character drawRect (Player.x , Player.y, PLAYER_W, PLAYER_H, 0xffffff);
/ / Update the game screen
SDL_Flip (screen);}
}
SDL_FillRect Here I used the function () to fill black screen, if you did what 'will continue to see the drawings of the players in various positions in the past. Let's take a shot with rubber and then redraw the function
drawRect ()
our character. With SDL_Flip ()
fully update the screen and make it ready for display on a surface is called alternatively SDL_UpdateRect software (). Now we can do is populate our main function and test our product.
int main () {
/ / Initialize the SDL library
SDL_SetVideoMode screen = (SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_SWSURFACE)
if (screen == NULL
) {
fprintf (stderr, "Can not initialize SDL:% s \\ n"
, SDL_GetError ());
exit (-1);}
SDL_WM_SetCaption ( "Example" ,
"Example" )
/ / Starting position of our player Player.x = 64;
Player.y = 64;
/ / main game loop MainLoop ();
SDL_Quit ();
/ / finished using the library properly
return 0;}
Our basic game and 'ready, we do not do much to move a part white square on the screen but as a basis for understanding the concepts of a video game is more 'than good. Save all as
game.c and fill your game with gcc-o game game.c-LSdL
or set your favorite IDE and have fun!
0 comments:
Post a Comment