Page 1 of 1

SDL_Rect / Dynamic allocation question[solved]

Posted: Thu Jun 03, 2010 3:50 am
by GameDevver4Evr
Not sure how to do this properly, or if it can be done, but I've seen it done a similar way in allegro, can tiles be dynamically created in this way using SDL_Rect? ( forgive my code ).

Code: Select all

int amount;

SDL_Rect tile;

tile = new Tile[ amount ];
Or does this need to be done a completely different way?, such as this. ( it's a bit pseudo ).

Code: Select all


// HEADER
//make a global pointer to the tile
extern class Tile *p_tile;

class Tile{

public:
     Tile( int width, int height, int type, int layer )
   ~Tile()

private:
   
    int layer;
    int height;
    int width;
    int type;
    SDL_Rect tile;
};

//CPP FILE

//init global pointer to NULL
Tile *p_tile = 0;

Tile::Tile( int width, int height, int type, int layer ){

p_tile = this;

}

int main(){

int x, y, z, a;

Tile *p_tile = new Tile( x, y, z, a );

}

//and delete of course

Not so sure if it has to be done through a class, or if it can by dynamically allocated rects within the class, I'm actually just trying to make a tile grid that you can define at runtime ( enter in your variables, then it sets it up for tiles width/height/ and iterates through placing them ).
well, if you guys can shed any light thanks, in the meantime I'll keep working at it :mrgreen:

Re: SDL_Rect / Dynamic allocation question

Posted: Thu Jun 03, 2010 5:55 am
by RyanPridgeon
The second way would be better... but it looks like you're misunderstanding some concepts. For example, to do new Something[5], you need to be assigning it to a Something*.

Also, I don't understand the purpose of the global pointer in your second method. But yes, often you would have some sort of class to store a tile, and some kind of list or array of all the tiles in a map, etc. :)

Re: SDL_Rect / Dynamic allocation question

Posted: Thu Jun 03, 2010 8:20 am
by Falco Girgis
Yeah, I think there is a definite concept misunderstanding. Dynamic memory allocation has nothing to do with being in a class...

Re: SDL_Rect / Dynamic allocation question

Posted: Thu Jun 03, 2010 11:11 am
by GameDevver4Evr
I think I see where I went wrong now, I was misreading some code that actually was pointing to a class / more like the second way I had it setup, except instead of global they just had the pointer within the class public, my mistake, thanks guys :) .