0

I'm trying to make some game engine (I know, should do an actual game instead). Sadly have defeat at project structure. it looks like this (footage from my fifth attempt):

#include <iostream>
#include <vector>
#include <windows.h> 

using namespace std;

class Player;
class Engine {
private:
    CHAR_INFO *chiBuffer;
    int width = 0;
    int height = 0;

    vector <Player> players;

public:
    Engine(int x = 120, int y = 30) {
        chiBuffer = new CHAR_INFO[x*y];
    }

    void addPlayer(Player player) {
        players.push_back(player);
    }

    void render() {
        for (int i = 0; i < players.size(); i++) {
            Player *player = &players[i];
            player->draw();
        }
    }

protected:
    inline CHAR_INFO& getPixel(int x, int y) { return chiBuffer[x + width * y]; }
};

class Drawable : Engine {
protected:
    inline CHAR_INFO& getPixel(int x, int y) { return Engine::getPixel(x, y); }
    virtual void draw() = 0;
};

class Player : Drawable {
protected:
    int x = 0;
    int y = 0;
public:
    void draw() {
        getPixel(x, y).Char.UnicodeChar = L'*';
    }
};

int main() {

    Engine *eng = new Engine(120, 30);
    Player p;
    eng->addPlayer(p);

    return 0;
}

I would like to make it easily extensible, so in my mind had been born an idea of creating master class (Engine) sub class Drawable which will have draw() method and later Tickable which would have onTick() etc... But I'm pretty sure I'm doing it so wrong. Could you tell me better idea of doing this? Or make this just working because this gives me too many errors to write it right here (VS 2017)

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Mateusz Budzisz
  • 598
  • 7
  • 15

1 Answers1

1

First, why use this ?

for (int i = 0; i < players.size(); i++) {
        Player *player = &players[i];
        player->draw();
 }

It's the same as: for(...) { players[i].draw() ;}.
Don't use variables and pointers where you don't need to (like in your main, don't use the new keyword: it's pointless. Engine e = Engine(120,30) is ok.).

The for loop on a vector should use Iterators :

for (auto it = begin (vector); it != end (vector); ++it) {
    it->draw;
}

In manners of good practice, you should write your code in different files.
Engine.h for Engine declaration, Engine.cpp for implementation.
Same for Player.

Stefano
  • 103
  • 5
  • Why? It's just draft. But thanks anyway. Could you explain why should I use Iterators? This for loop have seen from another stack overflow [question](https://gamedev.stackexchange.com/questions/46584/how-to-remove-an-object-from-a-stdvector). Yes, I would separate It but only if it start working. For simplify it's one file project. In merit um, do you know anything that can solve my problem? – Mateusz Budzisz Mar 15 '19 at 22:36
  • You have not problem, you juste want an opinion: "Could You tell me better idea of doing this?". And don't look at stackoverflow question, look at stackoverflow [answers](https://stackoverflow.com/a/12702744/11210641) – Stefano Mar 15 '19 at 22:55