Help with Dynamicly Allocating A 2D SDL_Rect Array

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 its functions:

class SpriteSheet  {
        SDL_Surface *surface;

        int number_rows;
        int number_cols;

        SDL_Rect *bounds_array;
        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 doesnt 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…

well if you want to make the rows and columns const within a class, the way to do that is:

static const int rows = suchandsuch;
static const int columns = suchandsuch;

that’s all that works, just const won’t work for defining it within a class.

I personally don’t know much about the tile concept because I don’t do tile-based mmorpgs or anything…but if all you need is a screen composed of these tiles, one other way to do it is 1) create your tiles in MS-Paint, GIMP or whatever, and 2)go into MS-Paint and just start doing Edit-> Paste From your tiles’ filenames and just line them up and paste them all over the place. Blow the screen up as big as you need to (up to 8x) and you won’t even need a really good eye or steady hand. That’s what I would do; Idk how big these screens are, but hopefully not too huge. Then just load the whole screen into memory as a regular SDL_Surface.

(Edit 5-10-12: removed an inaccurate claim regarding arrays of sdl_rects.)

I have no idea what you were doing there with that pointer, but what do I know, I don’t know how to do ‘new’ pointers yet.

Tim Jones’s is good (c++), and this guy below (c-style c++) is decent too


Thanks for your help!

Mmmm… That`s one feature I miss from C# now, Dynamic 2D array allocation.

Ill just use a single dimensional array in its stead thanks!


Oh, you can do dynamically allocated multidimensional arrays is C++, I just googled it.

I was just concerned with the line

bounds_array = new SDL_Rect[cols][rows];

because it reminded me of my own attempt to create an array of sdl_rects in the past; what worked was just creating an array of Xs and one of Ys and then using one rect in a loop to draw 500 4x4 rectangles to the screen for one of my explosions. The rectangles go off in 360 directions and with 8 speeds. The point I’m making is I’m ignorant of the subject of dynamic memory allocation and “new” pointers but I was giving you a heads up about attempting an array of sdl_rects. Maybe that line is fine; I couldn’t say for sure.

Edit 5-10-12: The above problem refers to my attempt to pass arrays of sdl_rects by reference, and compliers don’t like it when you try to pass an array of references. Recently I got around a similar problem by sticking an array into a struct and passing that, though what you’re really supposed to do is use a regular stack pointer, in this case an SDL_Rect*. Someday…