I am confused here... I have 3 classes in place, (code below) - A GameMode, a GameState(base), a MenuState(derived)
Now, the line causing problem is in GameMode.cpp constructor at the line:
GameStates{
{ EGameState::MAIN_MENU, std::make_unique<MenuState>(this, windowInfo)}
}
and the error mentioning "deleted function" clearly has to do with some of my = delete for copy and move constructors, but I fail to see the connection.
For what I can tell, I am calling the initializer list constructor of a map<>, and the make_unique should be returning an rvalue which should be moving the unique_ptr into the initializer list, so I just can't see how my deleted function are involved in this.
But since the error is there, something else is going on, and if is not my deleted function, then it is something with the unique_ptr. Can you see what it is?
GameMode.h:
#pragma once
#include <map>
#include <memory>
class GameState;
class GameMode
{
enum class EGameState{MAIN_MENU,PLAY};
private://variables
Util::WindowInfo WindowInfo;
GameState* CurrState;
std::map<EGameState, std::unique_ptr<GameState>> GameStates;
public://methods
GameMode(Util::WindowInfo windowInfo);
~GameMode();
GameMode(const GameMode&) = delete;
GameMode& operator=(const GameMode&) = delete;
GameMode(GameMode&&) = delete;
GameMode& operator=(GameMode&&) = delete;
GameState* CurrentState()const { return CurrState; }
void SetCurrentState(EGameState newState);
};
GameMode.cpp
#include "GameState.h"
#include "MenuState.h"
#include "PlayState.h"
#include "Utility.h"
#include "GameMode.h"
GameMode::GameMode(Util::WindowInfo windowInfo) :
CurrState{ nullptr },
GameStates{
{ EGameState::MAIN_MENU, std::make_unique<MenuState>(this, windowInfo)}
}
{
SetCurrentState(EGameState::MAIN_MENU);
}
GameMode::~GameMode()
{
}
void GameMode::SetCurrentState(EGameState newState)
{
if (GameStates.find(newState) != GameStates.end()) {
CurrState = GameStates[newState].get();
}
}
GameState.h (base class for states):
#pragma once
#include "Utility.h"
class GameMode;
class GameState
{
protected:
Util::WindowInfo WindowInfo;
GameMode* Game;
public:
GameState(GameMode* game,Util::WindowInfo windowInfo);
virtual ~GameState();
virtual void Update(float deltaTime) = 0;
virtual void Draw(float interpolation) = 0;
};
GameState.cpp (base class for states):
#include "GameState.h"
GameState::GameState(GameMode* game, Util::WindowInfo windowInfo):
Game{game}, WindowInfo { windowInfo }
{
}
GameState::~GameState()
{
}
MenuState (derived from GameState):
#pragma once
#include "GameState.h"
class MenuState : public GameState
{
public:
MenuState(GameMode* game, Util::WindowInfo windowInfo);
~MenuState();
MenuState(const MenuState&) = delete;
MenuState& operator=(const MenuState&) = delete;
MenuState(MenuState&&) = delete;
MenuState& operator=(MenuState&&) = delete;
void Update(float deltaTime) override;
void Draw(float interpolation) override;
};
MenuState.cpp (derived from GameState)
#include "MenuState.h"
#include "SDL2\SDL.h"
MenuState::MenuState(GameMode* game, Util::WindowInfo windowInfo):
GameState(game, windowInfo)
{
}
MenuState::~MenuState()
{
}
void MenuState::Update(float deltaTime)
{
}
void MenuState::Draw(float interpolation)
{
SDL_SetRenderDrawColor(WindowInfo.Renderer, 30, 30, 50, 255);
SDL_RenderClear(WindowInfo.Renderer);
SDL_RenderPresent(WindowInfo.Renderer);
}