Several Questions
Posted: Mon Jun 14, 2010 12:44 pm
I would love to hear your opinions on how you handle some stuff that I'm currently having problems with.
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:
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
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 ?
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
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;
}
}