1 // Copyright 2008 Brian Caine
3 // This file is part of Potpourri.
5 // Potpourri is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Potpourri is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTIBILITY of FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Potpourri. If not, see <http://www.gnu.org/licenses/>.
21 // This is supposed to be a demo of the Sprite class. To make sure it's
28 #include <SDL/SDL_image.h>
30 #include "../Sprite.h"
31 #include "../XMLParser.h"
33 #include "ImageLoader.h"
35 const std::string SCREEN_TITLE
= "Sprite class demo";
37 const int SCREEN_HEIGHT
= 480;
38 const int SCREEN_WIDTH
= 600;
40 const std::string FRAME_ONE
= "frame1.png";
41 const std::string FRAME_TWO
= "frame2.png";
42 const std::string FRAME_THREE
= "frame3.png";
44 const int SPRITE_POS
[] = {100, 100};
46 const std::string DATA
= "<?xml version=\"1.0\" encoding=\"UTF-8\"?><sprites> <source> <filename>battle_knucklessheet_stuff.png</filename> <name>knuckles_sheet</name> </source> <section> <x>10</x> <y>150</y> <width>40</width> <height>40</height> <source>knuckles_sheet</source> <name>punch1</name> </section> <section> <x>50</x> <y>150</y> <width>40</width> <height>40</height> <source>knuckles_sheet</source> <name>punch2</name> </section> <section> <x>90</x> <y>150</y> <width>40</width> <height>40</height> <source>knuckles_sheet</source> <name>punch3</name> </section> <section> <x>140</x> <y>150</y> <width>40</width> <height>40</height> <source>knuckles_sheet</source> <name>punch3</name> </section> <section> <x>180</x> <y>150</y> <width>40</width> <height>40</height> <source>knuckles_sheet</source> <name>punch4</name> </section> <section> <x>220</x> <y>150</y> <width>40</width> <height>40</height> <source>knuckles_sheet</source> <name>punch5</name> </section> <sprite> <name>punch</name> <items> <item>punch1</item> <item>punch2</item> <item>punch3</item> <item>punch4</item> <item>punch5</item> </items> <sequence> <frame> <item>0</item> <duration>100</duration> </frame> <frame> <item>1</item> <duration>100</duration> </frame> <frame> <item>2</item> <duration>100</duration> </frame> <frame> <item>3</item> <duration>100</duration> </frame> <frame> <item>4</item> <duration>100</duration> </frame> </sequence> </sprite></sprites>";
47 const std::string ROOT
= "../../relaxng/";
49 const int SPRITE_ANGLE
= 0;
51 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
52 const int rmask
= 0xff000000;
53 const int gmask
= 0x00ff0000;
54 const int bmask
= 0x0000ff00;
55 const int amask
= 0x000000ff;
57 const int rmask
= 0x000000ff;
58 const int gmask
= 0x0000ff00;
59 const int bmask
= 0x00ff0000;
60 const int amask
= 0xff000000;
63 SDL_Surface
* dscreen
= 0;
66 int run(SDL_Surface
* screen
);
67 void processEvents(bool* running
);
68 std::vector
<fragrant::Sprite
*> createSprites();
69 fragrant::Sprite
* makeSprite(std::string sprite_name
);
70 void cleanUp(std::vector
<fragrant::Sprite
*> sprites
);
72 int main(int argc
, char* argv
[])
74 SDL_Surface
* screen
= init();
85 if (SDL_Init(SDL_INIT_EVERYTHING
) == -1)
87 std::cerr
<< "init(): Error initializing SDL." << std::endl
;
91 SDL_Surface
* screen
= SDL_SetVideoMode(SCREEN_WIDTH
,
92 SCREEN_HEIGHT
, 0, SDL_SWSURFACE
);
97 SDL_WM_SetCaption(SCREEN_TITLE
.c_str(), 0);
102 int run(SDL_Surface
* screen
)
106 std::vector
<fragrant::Sprite
*> sprites
= createSprites();
107 std::vector
<fragrant::Sprite
*>::iterator iter
;
109 int last_ticks
= SDL_GetTicks();
110 int cur_ticks
= SDL_GetTicks();
115 processEvents(&running
);
117 last_ticks
= cur_ticks
;
118 cur_ticks
= SDL_GetTicks();
120 angle
+= (static_cast<float>(cur_ticks
- last_ticks
) / 1000.0f
) * 10;
121 sprites
.at(0)->setAngle(static_cast<int>(angle
));
124 angle
= static_cast<int>(angle
) % 360;
126 std::cout
<< "angle is " << angle
<< std::endl
;
128 for (iter
= sprites
.begin(); iter
!= sprites
.end(); iter
++)
129 (*iter
)->draw(screen
);
131 SDL_UpdateRect(screen
, 0, 0, 0, 0);
139 void processEvents(bool* running
)
143 while (SDL_PollEvent(&event
))
145 if (event
.type
== SDL_QUIT
)
152 std::vector
<fragrant::Sprite
*> createSprites()
154 // we create a few sprites to test various features
156 std::vector
<fragrant::Sprite
*> sprites
;
159 std::vector
<SDL_Surface
*> surfaces
;
161 temp
= fragrant::ImageLoader::getLoader()->getImage(FRAME_ONE
);
162 surfaces
.push_back(temp
);
164 temp
= fragrant::ImageLoader::getLoader()->getImage(FRAME_TWO
);
165 surfaces
.push_back(temp
);
167 temp
= fragrant::ImageLoader::getLoader()->getImage(FRAME_THREE
);
168 surfaces
.push_back(temp
);
170 fragrant::SpriteParams parameters
;
172 parameters
.position
.push_back(SPRITE_POS
[0]);
173 parameters
.position
.push_back(SPRITE_POS
[1]);
175 parameters
.focal_point
.push_back(64);
176 parameters
.focal_point
.push_back(64);
178 parameters
.repeat
= true;
179 parameters
.angle
= SPRITE_ANGLE
;
181 fragrant::Frame frame
;
184 frame
.duration
= 1000; // milliseconds, i think
185 parameters
.frames
.push_back(frame
);
187 frame
= fragrant::Frame();
189 frame
.duration
= 2000;
190 parameters
.frames
.push_back(frame
);
192 frame
= fragrant::Frame();
194 frame
.duration
= 500;
195 parameters
.frames
.push_back(frame
);
197 fragrant::Sprite
* sprite
= fragrant::Sprite::makeSprite(surfaces
,
201 sprites
.push_back(sprite
);
203 std::cerr
<< "Error making sprite" << std::endl
;
205 fragrant::Sprite
* tempd
= makeSprite(std::string("punch"));
207 sprites
.push_back(tempd
);
212 fragrant::Sprite
* makeSprite(std::string the_sprite
)
214 fragrant::XMLParser
* parser
= fragrant::XMLParser::getXMLParser();
215 fragrant::ImageLoader
* loader
= fragrant::ImageLoader::getLoader();
217 parser
->setRelaxNGRoot(ROOT
);
218 fragrant::XMLS_Results parsed_data
= parser
->parseSpriteData(DATA
);
220 if (parsed_data
.error
)
223 std::map
<std::string
, SDL_Surface
*> sources
;
224 std::map
<std::string
, SDL_Surface
*> sections
;
226 std::vector
<fragrant::XMLS_Source
>::iterator src_iter
;
227 for (src_iter
= parsed_data
.sources
.begin();
228 src_iter
!= parsed_data
.sources
.end();
230 sources
[src_iter
->name
] = loader
->getImage(src_iter
->filename
);
232 std::vector
<fragrant::XMLS_Section
>::iterator sec_iter
;
233 for (sec_iter
= parsed_data
.sections
.begin();
234 sec_iter
!= parsed_data
.sections
.end();
237 SDL_Surface
* temp
= SDL_CreateRGBSurface(SDL_SWSURFACE
|SDL_SRCALPHA
,
238 sec_iter
->width
, sec_iter
->height
, 32, rmask
, gmask
, bmask
, amask
);
239 SDL_SetAlpha(temp
, SDL_SRCALPHA
, 0);
240 loader
->addSurface(temp
);
242 if ((sources
.find(sec_iter
->source
) != sources
.end()) && !sec_iter
->error
)
248 src
.w
= sec_iter
->width
;
249 src
.h
= sec_iter
->height
;
251 SDL_BlitSurface(sources
[sec_iter
->source
], &src
, temp
, 0);
252 sections
[sec_iter
->name
] = temp
;
256 sections
[sec_iter
->name
] = 0;
259 std::map
<std::string
, fragrant::Sprite
*> created_sprites
;
260 std::vector
<fragrant::XMLS_Sprite
>::iterator spr_iter
;
261 for (spr_iter
= parsed_data
.sprites
.begin();
262 spr_iter
!= parsed_data
.sprites
.end();
265 std::vector
<SDL_Surface
*> sprite_surfaces
;
267 std::vector
<std::string
>::iterator str_iter
;
268 for (str_iter
= spr_iter
->items
.begin();
269 str_iter
!= spr_iter
->items
.end();
272 if (sections
.find(*str_iter
) != sections
.end())
273 sprite_surfaces
.push_back(sections
.at(*str_iter
));
276 sprite_surfaces
.push_back(0);
279 fragrant::SpriteParams the_params
;
280 the_params
.frames
= spr_iter
->frames
;
281 the_params
.angle
= 30;
283 std::vector
<int> focal_point
;
284 focal_point
.push_back(0);
285 focal_point
.push_back(0);
287 the_params
.focal_point
= focal_point
;
289 std::vector
<int> position
;
290 position
.push_back(150);
291 position
.push_back(150);
292 the_params
.position
= position
;
293 the_params
.repeat
= true;
295 fragrant::Sprite
* temp
= fragrant::Sprite::makeSprite(sprite_surfaces
,
297 created_sprites
[spr_iter
->name
] = temp
;
302 fragrant::Sprite
* result
= 0;
304 if (created_sprites
.find(the_sprite
) != created_sprites
.end())
305 result
= created_sprites
[the_sprite
];
307 std::map
<std::string
, fragrant::Sprite
*>::iterator iter
;
308 for (iter
= created_sprites
.begin(); iter
!= created_sprites
.end(); iter
++)
309 if (iter
->first
!= the_sprite
)
315 void cleanUp(std::vector
<fragrant::Sprite
*> sprites
)
319 std::vector
<fragrant::Sprite
*>::iterator iter
;
320 for (iter
= sprites
.begin(); iter
!= sprites
.end(); iter
++)
323 if (fragrant::ImageLoader::getLoader())
324 delete fragrant::ImageLoader::getLoader();