Page 1 of 1

Several Questions

Posted: Mon Jun 14, 2010 12:44 pm
by adikid89
I would love to hear your opinions on how you handle some stuff that I'm currently having problems with. :bow:

1) How do you handle entities(or what you call them)
Here is how I currently handle them but it seems retarded... An entity(in my engine) is everything you can see on the screen. Every entity has a sprite, pos, an id etc(collision box... yeah... that was dumb.. but I'll soon get it out of there, just need to figure out some stuff before) it looks something like this:

Code: Select all

class Entity {
	friend class EntityManager;
public:
	Entity(): x(0), y(0), id(0), alive(true), interactible(true), loaded(false) {}
	virtual ~Entity();
	virtual void update(int diff);							/* Main update function handles everything */
	virtual void load(const string& filename);				/* Loads the spritesheet for the Entity */
	virtual void draw();									/* Renders the Entity on the screen */
	virtual void remove();									/* Removes the Entity and frees the memory */
// ... some more functions down
The EntityManager has a map<int, Entity> of entities. Before I had a UnitManager, and everything on the screen was a unit.. which was extremely dumb, but everything had a unique id, through which I could have access to the units. But then when I had the level being loaded from a file, I sort of lost the ability to reference a specific unit. I need to specify that I needed the reference of the unit to handle the game logic... which was sort of like this:
In the main loop, based on the game state, it handled that state, if it was play state, it handled the play state, where i needed the reference to the player, and to all the enemies, to check whether somebody won(if player.dead or enemies.size() < 1 someone won... ). I realized it's a pretty dumb idea... so I wanted to ask where(and/or how) do you handle the game logic and how do you have access(if you do) to any unit(or entity).

2) How do you handle text output to the screen? Basically now I use a font file, and the sdl TTF_Render function to render text on the screen, I just find that this method is a huge pain in the ass... and I often have amazing bugs with it. So I figured that I'm definitely doing it wrong!
Here is a look to my font rendering process

Code: Select all

void Font::draw(const string& text, int x, int y, int alpha)
{
	_text = TTF_RenderText_Solid(font,text.c_str(), textColor);
	
	if(_text == NULL) {
		Singleton<Log>::getInstance()->print("Could not draw text! %s", TTF_GetError());
	} else {
		SDL_SetAlpha(_text,SDL_SRCALPHA,alpha);                       
		Singleton<Graphics>::getInstance()->Draw(this->_text,x,y);
		SDL_FreeSurface(_text);
		_text = NULL;
	}
}
Basically every time you draw() text on the screen, I call TTF_RenderText_Solid( ), and every time you change the size or the color of the font it reloads the font used, which seems pretty retarded. I also wrote a Text class that uses a font. You can either load a font, or use an existing one, with which I had loads of problems, but kinda works now. So how do you handle fonts ?

Re: Several Questions

Posted: Mon Jun 14, 2010 1:12 pm
by XianForce
Well in response to your first question, I personally use a list. I've found that with an ID system, the IDs usually become useless (for me), because I'll be iterating through, and not looping via ID. But a map works fine, in a book I've been reading: Programming Game AI by Example, they use maps to store their entities.

And why, may I ask, was calling them Units 'dumb'? Seems like the system would just be the same, except the Entity Class was instead a Unit Class.

And why is having a collision box dumb? I mean... Your going to need to detect collision in some way, right? So you should remove either the position, or the collision box and add dimensions (which is equivalent to a rect anyways...)

As for your second question...

It would seem as though your using the screen for debugging? If so, just route it to a file, or to the console...

Re: Several Questions

Posted: Mon Jun 14, 2010 1:32 pm
by adikid89
XianForce wrote:Well in response to your first question, I personally use a list. I've found that with an ID system, the IDs usually become useless (for me), because I'll be iterating through, and not looping via ID. But a map works fine, in a book I've been reading: Programming Game AI by Example, they use maps to store their entities.
Yeah, the ids do become useless, but in my defense, the first time I made the engine units were hard-coded, so I knew and needed all the ids, so the id system wasn't useless, and I made a map to have O(log(n)) find time. Still not I good idea I know...
XianForce wrote:And why, may I ask, was calling them Units 'dumb'? Seems like the system would just be the same, except the Entity Class was instead a Unit Class.
I didn't say that calling them units was dumb... I said it was dumb because everything on the screen was a unit... For example I had some 32x32 title, which were units. If that isn't the definition of dumb I don't know what is..
XianForce wrote:And why is having a collision box dumb? I mean... Your going to need to detect collision in some way, right? So you should remove either the position, or the collision box and add dimensions (which is equivalent to a rect anyways...)
It's dumb because I defined Entity as being basically anything that you can see on the screen. Not everything on the screen can collide so that a dumb waste of memory right there. Examples: background, console window, etc.
Edit: Right.. I just read the entire passage.. yeah.. it would be pretty much the same thing, it'll just look a little messy maybe.
Also this idea probably wasn't very good.. because I'm not sure how to handle them.. I'll probably make the EntityManager iterate through all the entities and update() them but in what order ? That will force me to load the background first and etc...
XianForce wrote:It would seem as though your using the screen for debugging? If so, just route it to a file, or to the console...
Yeah, I use the screen a little but for debugging, but not too much, I also have a logging system that saves stuff the a .txt file, and I don't know how to make the console visible. But still I use the font to draw for example the menu text, the console texts etc.

Re: Several Questions

Posted: Mon Jun 14, 2010 2:46 pm
by dandymcgee
adikid89 wrote:... and I don't know how to make the console visible.

Code: Select all

freopen( "CON", "wt", stdout );
freopen( "CON", "wt", stderr );
  
In case you ever want console output without re-compiling SDL.