Help with game engine design

Started by
12 comments, last by FritoBandito 13 years, 7 months ago
Hi,

I'm making a game engine in SDL and need some help with the design.

I wanna make an Input interface class which all games should inherit to handle events.
Quote:
class MyGameInputHandler: public Input{
void HandleEvent(Event event){
if(event.get() == QUIT)
//Quit...
...
}
};

This should be used for mouse movement, keyboard input and such. Is this a good idea?

My real question is: If I have a few sprites and such, how can 'MyGameInputHandler' access them to change their movement direction etc?


Thanks.
Advertisement
The event or input class should handle input.. not any other logic. so it should not access the other classes to change their movement or anything...

one way to do what you want is to define an interface say:
class eventhandler{   public:    virtual void handlemouseclick(args...) = 0;    virtual void handleanothermouseevent(args...) = 0;}


and in your input class keep a list of these handlers and call them for each event.
Quote:Original post by karpatzio
The event or input class should handle input.. not any other logic. so it should not access the other classes to change their movement or anything...

one way to do what you want is to define an interface say:
class eventhandler{   public:    virtual void handlemouseclick(args...) = 0;    virtual void handleanothermouseevent(args...) = 0;}


and in your input class keep a list of these handlers and call them for each event.

Do you mean that every sprite/graphic should implement these methods? Please elaborate.
Quote:Original post by shuwo
I wanna make an Input interface class which all games should inherit to handle events.
Good luck. This interface would be bloated to support all the possible events (btw, are we talking about input only? game-logic events?).
I think your first step is to think in terms of a set of interfaces. Multiple inheritance of pure interface classes is good.

Previously "Krohm"

Quote:Original post by shuwo
My real question is: If I have a few sprites and such, how can 'MyGameInputHandler' access them to change their movement direction etc?

Well, you typically wouldn't change the movement of sprites, but rather you'd give your game objects specific instructions. The code that's responsible for visualizing them should take care of updating sprite positions (as a reaction to the game object having changed it's position). Keep in mind that game objects are not sprites, they're just visualized with sprites (or particle effects, or 3D models, or whatever you want).

So, when the user presses the up key, you'd call the jump() function on your player object, rather than setting the velocity of a player sprite. You'd then have some code that checks the players state and that draws a sprite (or sprites) accordingly.

Quote:Do you mean that every sprite/graphic should implement these methods? Please elaborate.

Well, no. Only things that need to react to user input need to implement this interface, as only they need to be registered with the input code to listen for input events.

Well, it's possible to have to game react to input and, depending on the game state, perform certain actions, but it's also possible to let individual objects listen for input. I think the latter is a bit too low-level, though. For example, if the player object itself was listening for input directly, it would explicitly need to be paused or disabled when pausing the game.

So I tend to prefer a 'screen-based' approach, e.g. only menus and game-screens handle input directly, and only one screen can be active (visible, or on top of another screen) at the same time. Pausing the game would simply push a pause menu on top of the game-screen, preventing the game-screen from reacting to input.
Create-ivity - a game development blog Mouseover for more information.
Thanks for the answers.

I'm kinda lost however.. Let's ignore all my ideas and make a fresh question.
My design looks like this
Quote:
class Enemy : public Sprite{
private:
int str;
int life;

.......
}


Sprite contains the SDL_Surface and such things.

My game engine main loop will look like this,
1. Gather input from user
2. Perform logic and such
3. Draw to screen.


What would be the best way to gather input? And how do I access my game objects (e.g Enemy) to make it respond to user input and update it's life from the logic class/method?
I don't want to have a lot of global objects..

Please tell me and make suggestions if my view on this is completely bananas.

Thanks!
Who is responsible for creating your enemies?

You don't have to store them as global objects, but I would say any time you create something that could potentially receive input, you need to register this object with the input controller. Essentially somewhere you will have to create something like this:

InputHandler inputHandler;....Enemy* enemy = new Enemy()if(enemy.NeedsInput())   inputHandler.RegisterEnemy(enemy) 


Then whenever you have your Input handler receive an event, it can loop over all the objects it knows about and send them a notification "hey I got this input, it may be relevant to you". You can filter this however you like or let units decide how they want to respond to a particular event.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Hi again.

Thanks a lot for your answers! I've build a system to register objects in the input class now.

Now I've got a new problem. Where do I put all my objects? For example, if I have a lot of game objects (sprites) like Enemy, Player, Fish, etc, where do I "create" them?

Do I have to create them in the same file/class that draws them to the screen? Do I have to create them in the global scope somehow? So many question marks for me here.

Please help me clarify this! Thanks :)
Quote:Original post by shuwo
Now I've got a new problem. Where do I put all my objects? For example, if I have a lot of game objects (sprites) like Enemy, Player, Fish, etc, where do I "create" them?

I usually make a Game class of some sort and put them there. It may be useful for certain game objects to have a reference back to the Game object - this would allow your characters to create Bullet objects and add them to the Game, or to ask the Game which other characters are nearby.

No need to use globals here, though. If some code needs to know about the Game object, just pass it a reference or pointer to the Game object.
Create-ivity - a game development blog Mouseover for more information.
Quote:Original post by Captain P
Quote:Original post by shuwo
Now I've got a new problem. Where do I put all my objects? For example, if I have a lot of game objects (sprites) like Enemy, Player, Fish, etc, where do I "create" them?

I usually make a Game class of some sort and put them there. It may be useful for certain game objects to have a reference back to the Game object - this would allow your characters to create Bullet objects and add them to the Game, or to ask the Game which other characters are nearby.

No need to use globals here, though. If some code needs to know about the Game object, just pass it a reference or pointer to the Game object.


This would create unnecessary dependencies, game logic should be handled by other classes. For example create a sprite collection class that would handle collisions, movement and creation/destruction instead!

This topic is closed to new replies.

Advertisement