Simple question

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
User avatar
Zer0XoL
ES Beta Backer
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

Post by Zer0XoL »

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 :)
Image
Im Blue
User avatar
MrDeathNote
ES Beta Backer
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

Post by MrDeathNote »

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 :)
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 :) .
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"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
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Simple question

Post by XianForce »

MrDeathNote wrote:
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 :)
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 :) .
Well, you probably don't want EVERYTHING to inherit from an Entity class.

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.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Simple question

Post by XianForce »

EDIT: crap, double post >.>
Last edited by XianForce on Sat Mar 27, 2010 8:26 pm, edited 1 time in total.
User avatar
xiphirx
Chaos Rift Junior
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

Post by xiphirx »

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

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 :)
User avatar
Zer0XoL
ES Beta Backer
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

Post by Zer0XoL »

xiphirx 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 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;
}
I did not get the idea from quake, but that class you made is almost the same as the one im using :D
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?
Image
Im Blue
User avatar
xiphirx
Chaos Rift Junior
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

Post by xiphirx »

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 :)
User avatar
Zer0XoL
ES Beta Backer
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

Post by Zer0XoL »

xiphirx wrote:Yes, you might want to look into spritesheets and SDL_Rect s.
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.. hmm
Image
Im Blue
User avatar
xiphirx
Chaos Rift Junior
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

Post by xiphirx »

Zer0XoL wrote:
xiphirx wrote:Yes, you might want to look into spritesheets and SDL_Rect s.
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.. hmm
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.

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;
}
}
Note: that code is not tested, so do not go and copypasta it.

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 :)
User avatar
MrDeathNote
ES Beta Backer
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

Post by MrDeathNote »

Zer0XoL wrote:
xiphirx wrote:Yes, you might want to look into spritesheets and SDL_Rect s.
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.. hmm
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.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"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
User avatar
Zer0XoL
ES Beta Backer
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

Post by Zer0XoL »

MrDeathNote wrote:
Zer0XoL wrote:
xiphirx wrote:Yes, you might want to look into spritesheets and SDL_Rect s.
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.. hmm
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.
Thanks for the idea :D
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
Image
Im Blue
User avatar
MrDeathNote
ES Beta Backer
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

Post by MrDeathNote »

Zer0XoL wrote:
MrDeathNote wrote:
Zer0XoL wrote:
xiphirx wrote:Yes, you might want to look into spritesheets and SDL_Rect s.
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.. hmm
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.
Thanks for the idea :D
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
Souunds like a good plan :)
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"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
Post Reply