Hi Guys!
This is my first post in this forum (and my first SDL game) so
please bare with my noobish question.
Ok, that aside, lets get to the meat:
I am creating a Level Editor for a game I am plaining to make; it`s based with the concept of tiles, and I created a class that is meant to hold and represent a SpriteSheet, it contains a surface and has SDL_Rects represeting each unqiue sprite.
Heres the code for the class and it
s functions:
class SpriteSheet {
private:
SDL_Surface *surface;
int number_rows;
int number_cols;
SDL_Rect *bounds_array;
public:
SpriteSheet(string, int, int);
SDL_Surface get_spritesheet();
SDL_Surface get_bounds(int, int)
};
SpriteSheet::SpriteSheet(string filename, int rows, int cols) {
surface = load_image(filename.c_str());
// Do i have to dynamiclly allocate it? Am I initalizing it correctly?
// Is this the way to implyment it? Declare a pointer an do it like this?
bounds_array = new SDL_Rect[cols][rows];
int width_of_single_frame = surface->w / cols;
int height_of_single_frame = surface->h / rows;
// For loop implymented propley?
for (int x = 0; x < number_cols; x++) {
for (int y = 0; y < number_rows; y++) {
SDL_Rect element_bounds;
// these are the x and y coords on the sprite sheet...
element_bounds.x = x;
element_bounds.y = y;
element_bounds.w = width_of_single_frame;
element_bounds.h = height_of_single_frame;
bounds_array[x][y] = element_bounds;
}
}
}
SDL_Surface* SpriteSheet:get_spriteheet() {
return surface;
}
// Get the bounds of the specified element, y is initialized to 0 as sometimes we just
// want the array to act linear and beable to accsess it with a const var that number
// respresents the tile eg: SDL_Rect bob = tilesSheet.get_bounds(TILE_GRASS);
SDL_Rect SpriteSheet::get_bounds(int x, int y = 0) {
return bounds_array[x][y];
}
What am trying to do here is create a in memory representation of a SpriteSheet, each unique tile (including it`s width, height and x and y position respectively on the sheet.
The compiler complains about the rows and col varibles in the constructor of the class, not being a const varible - im sure there are may other errors in that function but the compiler doesn
t mention them…
Thanks for the help guys! I am really not used to manual dynamic allocation as before C++ I used C# and XNA for game developement…