1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
/* Different areas of the entities.png image contain different things.
* For example the first row are player pictures whilst the second are
* of echidnas.
*
* 'SDL_Rect's are simple structures containing the location and size
* of these individual pictures inside of the larger 'entities.png'
*
* The same thing is done for the different level graphics
*/
// Sets the SDL_Rect * crop to the location of specified echinida
// ( there are four echidna pictures, so which_echidna can be 0-3)
void get_pixmap_echidna(int which_echidna, SDL_Rect * crop )
{
crop->x = which_echidna * TILE_SIZE;
crop->y = 1 * TILE_SIZE; // all echidnas are in the second row
}
// Same as above, but for the player (lemon)
void get_pixmap_lemon(int which_lemon, SDL_Rect * crop )
{
crop->x = which_lemon * TILE_SIZE;
crop->y = 0; // all lemons are in the first row
}
// Getting the rectangle for level tiles is a little harder, as they are spread
// across multiple rows. Otherwise this does similarly to above modules
void get_pixmap_level( char number, SDL_Rect * crop )
{
int row, column;
row = 0;
column = (int)number;
// Compensate for the fact these pictures span multiple rows
while ( column >= 4 )
{
column = column - 4;
row++; // add one to row's value
}
crop->x = column * TILE_SIZE;
crop->y = row * TILE_SIZE;
}
// render() paints everything onto the screen when called
void render(SDL_Renderer* renderer, struct level_struct *currentlevel)
{
/* The picture on-screen is made in three steps:
* 1) Draw the level onto the screen surface
* 2) Draw the player and echidnas ontop of this
* 3) 'flip' the screen, so the changes can be seen
*/
SDL_Rect destination; // where the image is going to be painted to on-screen
SDL_Rect pixmap_crop; // where the tile image is on the source image
destination.w = TILE_SIZE;
destination.h = TILE_SIZE;
pixmap_crop.w = TILE_SIZE;
pixmap_crop.h = TILE_SIZE;
/// Level drawing
// ieterate through each square of the level, drawing the appropriate
// pictures for each onto the screen
int tilex, tiley; // Where we are up to
for ( tiley=0; tiley < SCREEN_WIDTH_SQUARES; tiley++ )
{
for ( tilex=0; tilex < SCREEN_WIDTH_SQUARES; tilex++ )
{
/// Determine which section of the entities image we are going to use
get_pixmap_level( currentlevel->squares[tilex][tiley] , &pixmap_crop );
// remember that the currentlevel structure contains the array of
// level squares/tiles
/// Determine where we are going to draw this image on-screen
destination.x = tilex * TILE_SIZE;
destination.y = tiley * TILE_SIZE;
/// Render this tile onto the screen
int ret = SDL_RenderCopy(renderer, currentlevel->tiles,
&pixmap_crop,
&destination);
if (ret < 0 ) {
printf("Error (SDL): %s: %s\n", "could not render copy tiles", SDL_GetError());
}
}
}
/// Entity drawing
// First of all the character
get_pixmap_lemon( currentlevel->player_face, &pixmap_crop );
destination.x = currentlevel->px * TILE_SIZE;
destination.y = currentlevel->py * TILE_SIZE;
// Render onto screen
SDL_RenderCopy(renderer, currentlevel->entities,
&pixmap_crop,
&destination);
// Now draw all of the echidnas
int ech_no; // Echidna number
for ( ech_no = 0; ech_no < 10; ech_no++ )
{
struct echidna current_echidna = currentlevel->echidnas[ech_no];
if ( current_echidna.this_echidna_exists == 1 )
{
// This echidnas exists and is alive
// Get its location and face
get_pixmap_echidna( current_echidna.face, &pixmap_crop );
destination.x = current_echidna.xpos * TILE_SIZE;
destination.y = current_echidna.ypos * TILE_SIZE;
// Render it onscreen
SDL_RenderCopy(renderer, currentlevel->entities,
&pixmap_crop,
&destination);
}
}
// Flip screen, so everything we have 'blitted' can now be seen
SDL_RenderPresent(renderer);
}
void render_a_losingscreen(SDL_Renderer* renderer, struct level_struct *currentlevel)
{
// Render a losing screen
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, currentlevel->winning_screen, NULL, NULL);
SDL_RenderPresent(renderer);
// Wait a second so the user can see the losing screen
SDL_Delay(2000);
}
|