Simple question
Moderator: Coders of Rage
- Zer0XoL
- ES Beta Backer
- Posts: 54
- Joined: Fri Apr 24, 2009 1:18 pm
- Current Project: Zelda untitled multiplayer game
- Favorite Gaming Platforms: PC, GBA, N64
- Programming Language of Choice: C++, Lua
- Location: Sweden
- Contact:
Simple question
is it good if i make every object and things inherit from the class entity( wich i made)
and then make a vector of entity pointers and have all my game objects in that vector, so it would be easier to check collision and things?
what is good and what i bad about this and can it be done in a better way? :P
thx
and then make a vector of entity pointers and have all my game objects in that vector, so it would be easier to check collision and things?
what is good and what i bad about this and can it be done in a better way? :P
thx
- MrDeathNote
- ES Beta Backer
- Posts: 594
- Joined: Sun Oct 11, 2009 9:57 am
- Current Project: cocos2d-x project
- Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
- Programming Language of Choice: C/++
- Location: Belfast, Ireland
- Contact:
Re: Simple question
I'm not really sure thats the best way to do it. A lot of ppl make a generic sprite class and then make every object that will appear on screen inherit from it but i don't think it's a good idea to make every object inherit from it. For example there are alot of things that may be implemented behind the scenes and that will never appear on screen so it wouldn't be necessary to have such direct access to them(for collision detection etc.), too much access can be a bad thing, that's why we create private members/functions. Saying that i could be completely wrong this is just an opinion .Zer0XoL wrote:is it good if i make every object and things inherit from the class entity( wich i made)
and then make a vector of entity pointers and have all my game objects in that vector, so it would be easier to check collision and things?
what is good and what i bad about this and can it be done in a better way? :P
thx
http://www.youtube.com/user/MrDeathNote1988
"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
Re: Simple question
Well, you probably don't want EVERYTHING to inherit from an Entity class.MrDeathNote wrote:I'm not really sure thats the best way to do it. A lot of ppl make a generic sprite class and then make every object that will appear on screen inherit from it but i don't think it's a good idea to make every object inherit from it. For example there are alot of things that may be implemented behind the scenes and that will never appear on screen so it wouldn't be necessary to have such direct access to them(for collision detection etc.), too much access can be a bad thing, that's why we create private members/functions. Saying that i could be completely wrong this is just an opinion .Zer0XoL wrote:is it good if i make every object and things inherit from the class entity( wich i made)
and then make a vector of entity pointers and have all my game objects in that vector, so it would be easier to check collision and things?
what is good and what i bad about this and can it be done in a better way? :P
thx
For me, I have an Entity class, and an Entity is anything that has the ability to move (aka has a velocity vector).
Then I create an Entity Manager class which holds usually a map of integers and entity pointers. The integer is the ID number of the entity. Each entity has a unique ID, so they can be referenced by that.
Re: Simple question
EDIT: crap, double post >.>
Last edited by XianForce on Sat Mar 27, 2010 8:26 pm, edited 1 time in total.
- xiphirx
- Chaos Rift Junior
- Posts: 324
- Joined: Mon Mar 22, 2010 3:15 pm
- Current Project: ******** (Unkown for the time being)
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Contact:
Re: Simple question
Did you get this idea from quake?
Like others have said, I would only use it for stuff that is active (moving etc).
a sample class would be
Like others have said, I would only use it for stuff that is active (moving etc).
a sample class would be
Code: Select all
/*
UNTESTED CODE, PROBABLY REALLY CRAPPY STUFF D:
*/
class Entity
{
public:
Entity(SDL_Surface* image);
Entity();
~Entity();
void move(float nvx, float nvy);
private:
int x, y;
float vx, vy;
// Assuming you use SDL
SDL_Surface* sprite;
}
Entity(SDL_Surface* image) // load with sprite
{
x = y = 0;
vx = vy = 0.0;
sprite = image;
}
Entity() // Blank sprite
{
x = y = 0;
vx = vy = 0.0;
sprite = NULL;
}
~Entity()
{
SDL_FreeSurface(sprite);
}
void Entity::move(float nvx, float nvy)
{
vx = nvx;
vy = nvy;
x += int(vx);
y += int(vy);
vx = vy = 0.0;
}
StarCraft II Zerg Strategy, open to all levels of players!
Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of
Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of
- Zer0XoL
- ES Beta Backer
- Posts: 54
- Joined: Fri Apr 24, 2009 1:18 pm
- Current Project: Zelda untitled multiplayer game
- Favorite Gaming Platforms: PC, GBA, N64
- Programming Language of Choice: C++, Lua
- Location: Sweden
- Contact:
Re: Simple question
I did not get the idea from quake, but that class you made is almost the same as the one im using :Dxiphirx wrote:Did you get this idea from quake?
Like others have said, I would only use it for stuff that is active (moving etc).
a sample class would beCode: Select all
/* UNTESTED CODE, PROBABLY REALLY CRAPPY STUFF D: */ class Entity { public: Entity(SDL_Surface* image); Entity(); ~Entity(); void move(float nvx, float nvy); private: int x, y; float vx, vy; // Assuming you use SDL SDL_Surface* sprite; } Entity(SDL_Surface* image) // load with sprite { x = y = 0; vx = vy = 0.0; sprite = image; } Entity() // Blank sprite { x = y = 0; vx = vy = 0.0; sprite = NULL; } ~Entity() { SDL_FreeSurface(sprite); } void Entity::move(float nvx, float nvy) { vx = nvx; vy = nvy; x += int(vx); y += int(vy); vx = vy = 0.0; }
I thought that entities would have physics and abillities like chests containg items and suff :P and tiles being solid or non solid, does anyone know a good way of making some tiles animated?
- xiphirx
- Chaos Rift Junior
- Posts: 324
- Joined: Mon Mar 22, 2010 3:15 pm
- Current Project: ******** (Unkown for the time being)
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Contact:
Re: Simple question
Yes, you might want to look into spritesheets and SDL_Rect s.
StarCraft II Zerg Strategy, open to all levels of players!
Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of
Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of
- Zer0XoL
- ES Beta Backer
- Posts: 54
- Joined: Fri Apr 24, 2009 1:18 pm
- Current Project: Zelda untitled multiplayer game
- Favorite Gaming Platforms: PC, GBA, N64
- Programming Language of Choice: C++, Lua
- Location: Sweden
- Contact:
Re: Simple question
Yes, ofc i know what that is and how to use it, its just i ask how to keep it somewhat structured as tiles and still have it animated.. hmmxiphirx wrote:Yes, you might want to look into spritesheets and SDL_Rect s.
- xiphirx
- Chaos Rift Junior
- Posts: 324
- Joined: Mon Mar 22, 2010 3:15 pm
- Current Project: ******** (Unkown for the time being)
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Contact:
Re: Simple question
Well, if your tile is a class that inherits properties from entity, then you can add a SDL_Rect to hold clips if the tile's image is wider than a normal tile.Zer0XoL wrote:Yes, ofc i know what that is and how to use it, its just i ask how to keep it somewhat structured as tiles and still have it animated.. hmmxiphirx wrote:Yes, you might want to look into spritesheets and SDL_Rect s.
So, in your constructor, you can have something like
Code: Select all
//clips is a SDL_Rect array declared in the tile class assuming it inherits things from entity
if (sprite->w > YOUR_NORMAL_TILE_DIMENSION)
{
animated = true; //bool that can tell your rendering process to render the animation
for (int i = 0; i < sprite->w/2; i++)
{
clips[i].x = i*YOUR_NORMAL_TILE_DIMENSION;
clips[i].y = 0;
clips[i].w = clips[i].h = YOUR_NORMAL_TILE_DIMENSION;
}
}
Including that in your tile constructor should give you a tile, with clips of the animation if there is one.
StarCraft II Zerg Strategy, open to all levels of players!
Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of
Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of
- MrDeathNote
- ES Beta Backer
- Posts: 594
- Joined: Sun Oct 11, 2009 9:57 am
- Current Project: cocos2d-x project
- Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
- Programming Language of Choice: C/++
- Location: Belfast, Ireland
- Contact:
Re: Simple question
You would just create another class eg. animatedTile and have code very similar to your sprite animation. The tile itself would need to be a spritesheet so it mite actually be better to create that tile as an animated sprite. But really it's up to you there are numerous ways to deal with this, i'd prob create a new class for animated tiles but thats just my opinion.Zer0XoL wrote:Yes, ofc i know what that is and how to use it, its just i ask how to keep it somewhat structured as tiles and still have it animated.. hmmxiphirx wrote:Yes, you might want to look into spritesheets and SDL_Rect s.
http://www.youtube.com/user/MrDeathNote1988
"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
- Zer0XoL
- ES Beta Backer
- Posts: 54
- Joined: Fri Apr 24, 2009 1:18 pm
- Current Project: Zelda untitled multiplayer game
- Favorite Gaming Platforms: PC, GBA, N64
- Programming Language of Choice: C++, Lua
- Location: Sweden
- Contact:
Re: Simple question
Thanks for the idea :DMrDeathNote wrote:You would just create another class eg. animatedTile and have code very similar to your sprite animation. The tile itself would need to be a spritesheet so it mite actually be better to create that tile as an animated sprite. But really it's up to you there are numerous ways to deal with this, i'd prob create a new class for animated tiles but thats just my opinion.Zer0XoL wrote:Yes, ofc i know what that is and how to use it, its just i ask how to keep it somewhat structured as tiles and still have it animated.. hmmxiphirx wrote:Yes, you might want to look into spritesheets and SDL_Rect s.
I think im going to inherit the original tile class and have it contain a value of how many tiles the animation is and a value for the speed of animating it :D
- MrDeathNote
- ES Beta Backer
- Posts: 594
- Joined: Sun Oct 11, 2009 9:57 am
- Current Project: cocos2d-x project
- Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
- Programming Language of Choice: C/++
- Location: Belfast, Ireland
- Contact:
Re: Simple question
Souunds like a good planZer0XoL wrote:Thanks for the idea :DMrDeathNote wrote:You would just create another class eg. animatedTile and have code very similar to your sprite animation. The tile itself would need to be a spritesheet so it mite actually be better to create that tile as an animated sprite. But really it's up to you there are numerous ways to deal with this, i'd prob create a new class for animated tiles but thats just my opinion.Zer0XoL wrote:Yes, ofc i know what that is and how to use it, its just i ask how to keep it somewhat structured as tiles and still have it animated.. hmmxiphirx wrote:Yes, you might want to look into spritesheets and SDL_Rect s.
I think im going to inherit the original tile class and have it contain a value of how many tiles the animation is and a value for the speed of animating it :D
http://www.youtube.com/user/MrDeathNote1988
"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup