Several Questions

Anything related in any way to game development as a whole is welcome here. Tell us about your game, grace us with your project, show us your new YouTube video, etc.

Moderator: PC Supremacists

Post Reply
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Several Questions

Post 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 ?
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Several Questions

Post 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...
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Re: Several Questions

Post 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.
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Several Questions

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply