didn't work, but I think i'm going to change gameState to a string var, see if that works
Error is driving me nuts
ok, i've tried changing it to a string variable and then i get this error:
>game.obj : error LNK2001: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > Game::gameState" (?gameState@Game@@2V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A)
and i don't really understand it
here are the code
class Game{
public:
static void Start();
static std::string gameState;
Game();
~Game();
private:
static bool IsExiting();
static void GameLoop();
};
#include "basic.h"
#include "game.h"
#include <string>
Game::Game(){
gameState = "Uninitialized";
};
Game::~Game(){
};
void Game::Start(){
if(gameState != "Uninitialized"){
return;
};
gameState= "Playing";
};
bool Game::IsExiting(){
if(gameState == "Exiting"){
return true;
}
else{
return false;
};
}
void Game::GameLoop(){
sf::Event Event;
sf::RenderWindow mainWindow(sf::VideoMode(1024,768,32),"Pong");
while(mainWindow.pollEvent(Event)){
if(gameState == "Playing"){
mainWindow.clear(sf::Color::Black);
mainWindow.display();
if((Event.type == sf::Event::Closed)||(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))){
gameState = "Exiting";
};
}
};
};
As for your error, that means the linker cannot find a definition of "gameState".
When you have a static variable declaration in a class, you must also provide a definition in exactly one source file:
std::string Game::gameState = "Uninitialized";
Also note that using a constructor and destructor here might not do what you want as up until now you've not been using any Game class instances, so the constructor may not run.
What i meant was that the same thing happend as before, when the console screen appeared, an error message appeared saying '"Unhandled exception at 0x77cad062 in Pong.exe: 0xC0000005: Access violation writing location 0x00000004"
think I was wrong and that the problem lays with sf::RenderWindow Game::MainWindow;
because i've changed the code and it worked(kind of, not like I wanted) but when i put this line (sf::RenderWindow Game::MainWindow;) it showed the error.
Edit:
I think i've found it, ive found this link http://stackoverflow.com/questions/24318663/displaying-window-with-sfml and when i removed the static, the error vanished,
IT'S ALIVE!!!!
I finally completed step 2 from http://www.gamefromscratch.com/page/Game-from-Scratch-CPP-Edition-Part-2.aspx after two days of searching
here is the now correct code for people who experienced similar problems:
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>
#include <SFML\Audio.hpp>
#include <SFML\System.hpp>
#include <iostream>
#include <ctime>
#include <stdio.h>
#include <map>
#include <cassert>
#include <string>
game.h:
#include "basic.h"
class Game{
public:
void Start();
private:
void GameLoop();
bool IsExiting();
enum GameState { Uninitialized, ShowingSplash, Paused,
ShowingMenu, Playing, Exiting };
GameState gameState;
sf::RenderWindow mainWindow;
};
Game.cpp:
#include "basic.h"
#include "game.h"
#include <string>
void Game::Start(){
gameState= Game::Playing;
mainWindow.create(sf::VideoMode(1027,768,32),"Pong");
while(!IsExiting()){
GameLoop();
}
mainWindow.close();
};
bool Game::IsExiting(){
if(gameState == Game::Exiting){
return true;}
else{ return false;};
}
void Game::GameLoop(){
sf::Event Event;
while(mainWindow.pollEvent(Event)){
switch(gameState){
case Game::Playing:
mainWindow.clear(sf::Color::Black);
mainWindow.display();
if((Event.type == sf::Event::Closed)||(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))){
gameState = Game::Exiting;
};
break;
};
};
};
and main.cpp
#include "basic.h"
#include "game.h"
int main(){
Game Pong;
Pong.Start();
return 0;
};
God i'm glad that it works
Great.
Many would argue that this is a superior design to using static variables anyway. Global variables like that make it much easier to write code that is hard to understand and change.
And i'm stuck again :(
got up to part 5 pretty easy but know i've got an error and i don't know why:
PlayerPaddle *player1 = new PlayerPaddle();
player1->Load("images/paddle.png");
player1->SetPosition((1024/2)-45,700);
gameObjectManager.Add("Paddle1",player1);
In the last line, it says that the type of Playerpaddle* is incompatible with parameter of type VisibleGameObject* but Playerpaddle is a class derived from VisibleGameObject so why doesn't he accept it?
posting the output of the debugger in case it would help