Hello everyone, i have a newbie question.
I am doing basic animation as seen in any SDL tutorial over there, however there is something that isn’t working:
when reading game resources, i load the background resource and the scenario map, and then, still in the init() phase, i try to fill a SDL_surface with the resulting background, so i avoid the big loop on draw.
the problem is i don’t have it working properly.
for instance, if i move the very same loop from init phase to draw phase, it works perfectly, but in this way the draw becomes slower.
is there possible to render entire background before animation starts so i avoid to recreate it again and again?
thanks in advance.
btw, here goes the actual relevant code:
Code:
/*
- Scenario.cc*
- Created on: 09/03/2012
-
Author: sombriks
*/
#include “Scenario.hh”
Scenario::Scenario(json_object json_scenario) {
json_object json = json_object_object_get(json_scenario, “scenario_name”);
name = (char) json_object_get_string(json);
json = json_object_object_get(json_scenario, “resource”);
char res = (char*) json_object_get_string(json);
json = json_object_object_get(json_scenario, “tile_width”);
tileWidth = json_object_get_int(json);
json = json_object_object_get(json_scenario, “tile_height”);
tileHeight = json_object_get_int(json);
json = json_object_object_get(json_scenario, “rows”);
rows = json_object_get_int(json);
json = json_object_object_get(json_scenario, “cols”);
cols = json_object_get_int(json);
resource = IMG_Load(res);
buffer = SDL_CreateRGBSurface(SDL_HWSURFACE, //
rows * tileWidth, rows * tileHeight, 32, 0, 0, 0, 0);
SDL_Rect a;
SDL_Rect b;
a.x = b.x = 0;
a.y = b.y = 0;
a.w = b.w = tileWidth;
a.h = b.h = tileHeight;
int i = -1;
while (++i < rows) {
int j = -1;
while (++j < cols) {
b.x = tileWidth * i;
b.y = tileHeight * j;
SDL_BlitSurface(resource, &a, buffer, &b);
}
}
pos.x = des.x = 0;
pos.y = des.y = 0;
pos.w = des.w = tileWidth;
pos.h = des.h = tileHeight;
}
void Scenario::step(int delta, SDL_Event event) {
}
void Scenario::draw(SDL_Surface* screen) {
// SDL_Rect a;
// SDL_Rect b;
// a.x = b.x = 0;
// a.y = b.y = 0;
// a.w = b.w = tileWidth;
// a.h = b.h = tileHeight;
// int i = -1;
// while (++i < 100) {
// int j = -1;
// while (++j < 100) {
// b.x = tileWidth * i;
// b.y = tileHeight * j;
// SDL_BlitSurface(resource, &a, screen, &b);
// }
// }
SDL_BlitSurface(buffer, &des, screen, &pos);
}
for context the entire code can be seen there:
http://code.google.com/p/sombriks-hq/source/browse/#svn%2Ftrunk%2Fsdl-sample