1

I was making a game in C++ , SFML. so I made the Game Engine but then I decided to make the Player and PlayerController classes. The game with the Player class works perfectly but when I include the PlayerController class which listens to keys pressed and moves player, it says this error: syntax error: identifier. main.cpp:

#include "GameEngine.h"
int main() {
    GameEngine game;
    game.run();
    return 0;
}

Game Engine h file:

#ifndef GAMEENGINE_H
#define GAMEENGINE_H

#include "Player.h"
#include "PlayerController.h"
#include <iostream>
#include <SFML/Graphics.hpp>
#include <cstdlib>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>

class GameEngine
{

public:

    GameEngine(); // non functional
    virtual ~GameEngine(); // not yet
    void loop();
    void run();
    void constructWindow();
    void render();
    void handleSFMLEvent();
    float getDeltaTime();
    void update(); // later

    sf::RenderWindow* win; // creates the window
    
    // Variables
    
    const std::string game_path = "Game/Data";

    

    
protected:
    float deltaTime; // delta time number
    sf::Clock deltaTimeClock; // delta time calculator
    sf::Event sfmlEvent; // SFML event
    
};

#endif

Game Engine CPP file:

#include "GameEngine.h"

GameEngine::GameEngine()
{
}

GameEngine::~GameEngine()
{
    delete this->win;

}

void GameEngine::run()
{
    constructWindow();
    loop();
}

void GameEngine::constructWindow()
{
    
    this->win = new sf::RenderWindow(sf::VideoMode(500,500),"Title",sf::Style::Close);
}

void GameEngine::render()
{
    win->clear();

    //Render stuff in between

    Player mainPlayer;
    mainPlayer.constructPlayer();
    mainPlayer.drawPlayer(win);
    PlayerController cont;
    mainPlayer.updatePlayer(cont);



    win->display();
}

void GameEngine::handleSFMLEvent()
{
    while (win->pollEvent(this->sfmlEvent)) {

        if (sfmlEvent.type == sfmlEvent.Closed)
            win->close();

    }

}

float GameEngine::getDeltaTime()
{
    deltaTime = deltaTimeClock.restart().asSeconds();

    system("cls");
    std::cout << "Deltat time is: " << deltaTime << std::endl;
    return deltaTime;

}

void GameEngine::update() {

}

void GameEngine::loop()
{
    
    
    while (this->win->isOpen()) {

        getDeltaTime();
        render();
        update();
        handleSFMLEvent();

    }
}

Player h file:

#pragma once
#include "SFML/Graphics.hpp"
#include "GameEngine.h"
#include "PlayerController.h"


class Player
{
public:


    void constructPlayer();
    void drawPlayer(sf::RenderWindow *rw);
    void movePlayer(float x, float y);
    void setPosition(float x, float y);

    void updatePlayer(PlayerController cont);


    
    
    sf::Sprite playerSprite;
    sf::Texture playerTexture;
    
    
    float playerSpeed = 5.0f;
private:

};

Player CPP file:

#include "Player.h"




void Player::constructPlayer()
{
    GameEngine eng;
    if (!playerTexture.loadFromFile(eng.game_path + "/Player/Texture.png")) {
        std::cout << "GAME FAILED. (texture wasn't found)\n";
    }
    
    playerSprite.setScale({ 5,5 });
    playerSprite.setTexture(playerTexture);
    playerSprite.setPosition({ 200,200 });
}

void Player::drawPlayer(sf::RenderWindow *rw)
{
    rw->draw(playerSprite);

}

void Player::movePlayer(float x, float y)
{
    playerSprite.move({ x,y });
}

void Player::setPosition(float x, float y)
{
    playerSprite.setPosition({ x,y });
}

void Player::updatePlayer(PlayerController cont) {
    while (true) {
        cont.control(*this);
    }
}

PlayerController h file:

#pragma once
#include "SFML/Graphics.hpp"
#include "Player.h"
#include "GameEngine.h"

class PlayerController
{
public:
    
    void control(Player &controllingPlayer);
    
    
};

PlayerController CPP file:

#include "PlayerController.h"





void PlayerController::control(Player &controllingPlayer)
{
    GameEngine eng;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
        // Left
        controllingPlayer.movePlayer(controllingPlayer.playerSpeed * eng.getDeltaTime(),0);

    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
        // Right
        controllingPlayer.movePlayer(-controllingPlayer.playerSpeed * eng.getDeltaTime(), 0);
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
        // Up
        controllingPlayer.movePlayer( 0 , -controllingPlayer.playerSpeed * eng.getDeltaTime());
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
        // Down
        controllingPlayer.movePlayer(0, controllingPlayer.playerSpeed * eng.getDeltaTime());
    }


}

Finally the error:

The Error

Thank you :3 Sorry if there had been already a similar question. I am new to here. I am selflearning and have no clue of the tutorials on Youtube or other platforms.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Spixa
  • 25
  • 6
  • 3
    Please post the compiler error in textual form here instead of posting a link to an image. – R Sahu Jun 24 '20 at 18:37
  • 4
    Does this answer your question? [Resolve build errors due to circular dependency amongst classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – manveti Jun 24 '20 at 18:47
  • yes it does, thank you – Spixa Jun 24 '20 at 18:49

0 Answers0