SDL_Rect / Dynamic allocation question[solved]

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
GameDevver4Evr
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Mon Mar 08, 2010 9:34 pm

SDL_Rect / Dynamic allocation question[solved]

Post 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:
Last edited by GameDevver4Evr on Thu Jun 03, 2010 12:14 pm, edited 1 time in total.
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: SDL_Rect / Dynamic allocation question

Post 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. :)
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: SDL_Rect / Dynamic allocation question

Post by Falco Girgis »

Yeah, I think there is a definite concept misunderstanding. Dynamic memory allocation has nothing to do with being in a class...
GameDevver4Evr
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Mon Mar 08, 2010 9:34 pm

Re: SDL_Rect / Dynamic allocation question

Post 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 :) .
Post Reply