I'm a pretty experienced programmer, but I'm still relatively new to the architecture and design of OOP in C ++. Most of my experience is with C # and Java. I recently tried to code a simple game engine in C ++. I used SDL for graphics. In this article, I would like to discuss my architecture and get some feedback on the topic. In particular, I ran into a design problem that I would like to help with. So here goes:
- In my main function, I initialize all SDL materials for drawing on the screen, etc.
- Then I create an instance of all the objects that I intend to use: floor, wall, player, etc.
- Then I start the main loop. This cycle performs each movement of the object, the functions of collision detection and conflict processing and redraws them.
- The main loop starts until the application completes, showing one frame at each iteration.
My problem is this: I tried to make some kind of interface-style design. It includes a number of abstract base classes that allow each object to implement behavior. For example, if I want the object to be movable, it would have to inherit from the movable base class, which contains a virtual function called move() and some coordinate position. If I wanted it to be unstable, the object would inherit from a resolvable abstract class that contains the virtual functions checkCollision() and handleCollision() , as well as the hitbox member variable. An object as a player inherits both of these base classes, as well as several others.
This works well enough while I do everything manually in the main loop. I can just say:
player.move(); player.checkCollision(); player.handleCollision(); player.draw().
and this is normal. But I would like to have a vector or an array of shared objects in the main loop and do something like this:
for each object in vector if object is of type movable object.move(); if object is of type collidable object.checkCollision();
I thought I could accomplish this with dynamic casting, but I really couldn't think of anything. I tried to store them as void pointers, but this does not work the way I want. I read about this Gameobject component architecture for video games that I could try, but I really would like to save what I already have written. I think this is a good learning opportunity. If anyone has any ideas, I would really appreciate it. How does my architecture compare to other simple game engine designs? Does my interface architecture really make sense or is it completely elusive?
zallik
source share