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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
#include "sdl.h"
#include "sdl_image.h"
#include <string>
// The surfaces
SDL_Surface *dots = NULL;
SDL_Surface *screen = NULL;
// The window attributs
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
// the event structur
SDL_Event event;
// The portions of the Sprite map to be blitted
SDL_Rect clip [4];
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect *clip);
SDL_Surface *load_image( std::string filename );
bool init ();
bool load_files ();
void clean_up ();
int main (int argc, char *args[])
{
clip[0].x = 0;
clip[0].y = 0;
clip[0].w = 100;
clip[0].h = 100;
clip[1].x = 100;
clip[1].y = 0;
clip[1].w = 100;
clip[1].h = 100;
clip[2].x = 0;
clip[2].y = 100;
clip[2].w = 100;
clip[2].h = 100;
clip[3].x = 100;
clip[3].y = 100;
clip[3].w = 100;
clip[3].h = 100;
// Initialize
if (init () == false)
{
return 1;
};
// Load files
if (load_files == false)
{
return 1;
};
// Fill the screen white
SDL_FillRect (screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF));
// Apply the images to the screen
apply_surface(0, 0, dots, screen, &clip[0]);
apply_surface(540, 0, dots, screen, &clip[1]);
apply_surface(0, 380, dots, screen, &clip[2]);
apply_surface(540, 380, dots, screen, &clip[3]);
// Update SDL
if (SDL_Flip (screen) == -1)
{
return 1;
};
SDL_Delay (5000);
clean_up ();
return 0;
};
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect *clip)
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface( source, clip, destination, &offset );
};
SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image using SDL_image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old image
SDL_FreeSurface( loadedImage );
}
//Return the optimized image
return optimizedImage;
};
bool init ()
{
// Initialize all SDL subsystems
if (SDL_Init (SDL_INIT_EVERYTHING) == -1)
{
return false;
};
// Set up Screen
screen = SDL_SetVideoMode (SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE | SDL_DOUBLEBUF);
// If there was an error in setting up the screen
if (screen == NULL)
{
return false;
};
// Set the windows caption
SDL_WM_SetCaption("Sprite_Sheets test", NULL);
// If everything fine
return true;
};
bool load_files ()
{
// Load the image
dots = load_image("Data/a.bmp");
// If there was an error on loading the image
if (dots == NULL)
{
return 1;
};
// If everything loaded fine
return true;
};
void clean_up ()
{
// Free the image
SDL_FreeSurface (dots);
// SDL Quit
SDL_Quit ();
};
|