C481 B581 Computer Graphics
Dana Vrajitoru
Games Programming
Arcade Games
- Usually happen in a maze (2D matrix).
- Contain one or more characters, usually one of which is
user-controlled.
- Relatively simple graphics.
- Using a limited number of basic images, easily identifiable with the
elements of the game.
- Many of them use discrete player movement (from one cell to the next).
Implementation Ideas
- Separate the game functionality from the graphics.
- The game operates on some data structures non-related to the graphics.
- The graphics only display something that reflects the state of the
data structures in the game.
- The game is defined by a set of rules and by possible user actions.
- Usually the main functionality of the game is in a function of type
Next_frame which can be associated with a timeout event or triggered by
user's actions (events).
Basic Program Structure
-
Basic data structure: a 2D array (matrix, table) of an enumerated type.
-
enum Cell_type { brick, box, player, hostile};
-
Cell_type table[t_width][t_height];
-
For each type of cell we have a particular pixmap, each of them of dimensions
c_width, c_height.
-
The drawing area for the game is of size c_width*t_width, c_height*t_height.
Character in the Game
-
Type (struct) or class Player_type.
-
Must contain information about:
-
the current position (x, y) of the player in the table (if you allow continuous
movement, these could be floats, like 2.5 would be between cells 2 and
3)
-
the type of player (enumerated type, != Cell_type)
-
functions to determine its behavior (move, eat)
-
speed, power, nr. of lives, etc.
Drawing the Table
A pixmap for each type of cell.
for (x=0, x<t_width, x++)
for (y=0; y<t_height; y++) {
pixmap = select_pixmap(table[x][y]);
draw_pixmap(area, pixmap, 0, 0,
//src
c_width*x, c_height*y,//dest
c_width, c_height); //dim
}
Accelerate the Display
-
If the number of players is a lot smaller than the number of cells, for
every movement of the player from an initial position to a new position,
do the following:
-
Draw_cell(initial_position, space_pixmap);
-
Draw_cell(new_position, player_pixmap);
-
In this case, we may draw the cells to a big pixmap to be displayed on
the drawing area.
Origin and Orientation
-
The origin of a drawing area on the screen is in the top-left corner of
the window.
-
We display the cell table[0][0] starting from the origin of the drawing
area and of the pixmap.
-
Moving the table starting from the position (x, y) means:
-
up (x, y-1)
-
down (x, y+1)
-
left (x-1, y)
-
right (x+1, y)
Multiple Players at Various Speed
class Player { ... int speed ... };
Player players[nr_players];
void next_frame() {
for (int i=0; i<nr_players; i++) {
pl = players[i];
if (pl.counter == 0)
pl.Move();
pl.counter = (pl.counter+1) % pl.speed;
}
}
Animated Players
class Player {
... int frame=0, nr_frames;
GdkPixmap pixmaps[nr_frames]...
};
next_frame() {
for (int i=0; i<nr_players; i++) {
pl = players[i];
pixmap = pl.pixmaps[pl.frame];
Draw(pixmap, pl.position);
pl.frame = (pl.frame +1)%pl.nr_frames;
}
}
Game Interaction
- Link some events to actions in the game.
- Keyboard and mouse events
- Timeout event linked to a call for a function next_frame().
- Connecting any signal to a function that determines the type of event
and what to be done with it.
Connecting the signals
stateStruct state;
gtk_signal_connect(object, "event", GTK_SIGNAL_FUNC(Event_mouse), &state);
gtk_signal_connect(GTK_OBJECT(app), "key_press_event",
GTK_SIGNAL_FUNC(Event_press_key), (gpointer)area);