If you don't initialize paused it's undefined whether it will be trur or false.
Also if you have a global window, you have a bad design and possibility of certain things going wrong. I advise you to not use global variables at all.
Other than that, the code is incomplete and we can't tell you much more. Provide a minimal and compilable example instead.
sorry, i think id better post the rest of the code. Dont worry everything isnt global, its the name of the class those are in
(i know my code design isnt perfect, but thats more of why im building this, to learn
)
heres globals.h
//Evan
#ifndef GLOBALS_H
#define GLOBALS_H
#include <vector>
#include <string>
#include <stdexcept>
#include <iostream>
#include <memory>
#include "Map.h"
#include "Button.h"
#include "PlayerCharacter.h"
#include "UIPlayerMenu.h"
#include "MagicWrench.h"
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
struct TextureArray
{
std::string fileName;
sf::Texture texture;
};
class Globals
{
private:
bool paused;
static sf::RenderWindow window;
static sf::Event event;
static sf::View view;
static sf::View userInterface;
static sf::Font gameFont;
static sf::Clock gameClock;
static sf::Time gameTime;
static sf::Time accumulator;
static UIPlayerMenu itemMenu;
TextureArray textures[5];
Map gameMap;
//temp fix - 1 is for game 2 is for mapMaker
int gameMode;
PlayerCharacter player;
//ai for enemies
//assume this is a survival game,
//if it is then ai's arent particularly targeting you over another ai
//lets assume they have multiple methods of killing an enemy, for example
//fist, foot, or staff
//if ai gets kill with staff then that skill levels up and that specific ai becomes better,
//this state is also possibly saved if the player is killed
//what this would cause is a more dynamic world that seemed more alive
//this could also initialize the neural network programming technique i want to implement
public:
Globals();
Globals(int mapx, int mapy, int cameraX, int cameraY);
void mainMenu();
void initializeGame();
void runGame();
void handleEvents();
void update();
void runMapMaker();
sf::Texture getTexture(int textureNumb);
};
#endif
and heres Globals.cpp
//Evan
#include "globals.h"
Globals::Globals()
{
Globals::textures[0].fileName = "overWorld.png";
Globals::textures[1].fileName = "menuButton.png";
Globals::textures[2].fileName = "menuButtonPressed.png";
Globals::textures[3].fileName = "king.png";
Globals::textures[4].fileName = "titleScreen.png";
gameMode = 0;
accumulator = sf::Time::Zero;
paused = false;
}
Globals::Globals(int mapx,int mapy, int cameraX, int cameraY)
{
Globals::textures[0].fileName = "overWorld.png";
Globals::textures[1].fileName = "menuButton.png";
Globals::textures[2].fileName = "menuButtonPressed.png";
Globals::textures[3].fileName = "king.png";
Globals::textures[4].fileName = "titleScreen.png";
gameMode = 0;
accumulator = sf::Time::Zero;
paused = false;
}
void Globals::initializeGame()
{
//window and view setup
Globals::window.create(sf::VideoMode(1280, 640), "Kill It With Fire!");
Globals::window.setKeyRepeatEnabled(false);
Globals::window.setFramerateLimit(60);
Globals::view = sf::View(sf::FloatRect(0, 0, 1280, 640));
Globals::userInterface = sf::View(sf::FloatRect(0, 0, 1280, 640));
Globals::window.setView(view);
Globals::view.setCenter(640, 320);
//font setup
if(!gameFont.loadFromFile("kongtext.ttf"))
{
throw(42);
}
//tiles setup
for(int i = 0; i < 5; i++)
{
if(!Globals::textures[i].texture.loadFromFile(Globals::textures[i].fileName))
{
throw(42);
}
}
//insert main menu screen here
mainMenu();
//create character
player = PlayerCharacter(textures[3].texture);
player.setSpritePostion(sf::Vector2f(640, 320));
if(Globals::window.isOpen())
{
//map loading
gameMap = Map(100, 100);
gameMap.setDefaultTiles(textures[0].texture);
for(int i = 0; i < 40; i++)
{
for(int j = 0; j < 20; j++)
{
if(gameMap.validTilePosition(i, j))
{
gameMap.setTileDefaultPosition(i, j, i*32, j*32);
}
}
}
Globals::window.setMouseCursorVisible(false);
if(gameMode == 1)
{
runGame();
}
else
{
runMapMaker();
}
}
}
void Globals::runMapMaker()
{
sf::Texture menuTexture;
if(!menuTexture.loadFromFile("mapMakerMenu.png"))
{
throw(42);
}
itemMenu = UIPlayerMenu(true, menuTexture, textures[0].texture);
itemMenu.setPosition(1132.0, 0.0);
itemMenu.toggle();//why?
std::shared_ptr<Weapon> newWeapon(new MagicWrench(textures[0].texture));
player.setWeapon(newWeapon); //put derived class magicWrench into char inventory
//game loop
while (Globals::window.isOpen())
{
gameTime = gameClock.restart();
handleEvents();
if(!paused)
{
update();
}
Globals::window.clear();
Globals::window.setView(Globals::view);
Globals::gameMap.draw(window);
Globals::window.setView(Globals::userInterface);
Globals::player.draw(window);
if(itemMenu.getCanDraw())
{
itemMenu.draw(window); //change item menu to a uiplayermenu when its finished
}
Globals::window.display();
}
}
void Globals::runGame()
{
//game loop
while (Globals::window.isOpen())
{
while (Globals::window.pollEvent(Globals::event))
{
switch(Globals::event.type)
{
case sf::Event::Closed:
Globals::window.close();
break;
default:
break;
}
}
//check for movement keys pressed
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
if(!(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::S)))
{
//move up
Globals::view.move(0.0, -5.0);
//change to player.move
}
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
//move down
Globals::view.move(0.0, 5.0);
//change to player.move
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
if(!(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::D)))
{
//move left
Globals::view.move(-5.0, 0.0);
//change to player.move
}
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
//right
Globals::view.move(5.0, 0.0);
//change to player.move
}
Globals::window.clear();
Globals::window.setView(Globals::view);
Globals::gameMap.draw(window);
Globals::window.display();
Globals::gameClock.restart();
}
}
void Globals::mainMenu()
{
Button mapMaker(&textures[1].texture, &textures[2].texture, std::string("Map Maker"), sf::Vector2f(100.0f, 580.0f), &gameFont);
Button playGame(&textures[1].texture, &textures[2].texture, std::string("Play!"), sf::Vector2f(350.0f, 580.0f), &gameFont);
sf::Sprite mainMenuImage(textures[4].texture);
sf::Vector2i localPosition;
bool clicked = false;
bool countDown = false;
sf::Time timer = sf::Time::Zero;
while(timer <= sf::seconds(0.5))
{
if(countDown)
{
timer += sf::seconds(1.0/60.0);
}
while (Globals::window.pollEvent(Globals::event))
{
if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && countDown == 0)
{
// left mouse button is pressed check if button is clicked
localPosition = sf::Mouse::getPosition(Globals::window);
clicked = mapMaker.checkIfClick(&sf::Vector2f(localPosition.x, localPosition.y));
if(clicked)
{
gameMode = 2;
countDown = true;
}
else
{
clicked = playGame.checkIfClick(&sf::Vector2f(localPosition.x, localPosition.y));
if(clicked)
{
gameMode = 1;
countDown = true;
}
}
}
}
Globals::window.clear();
Globals::window.draw(mainMenuImage);
Globals::window.draw(*mapMaker.getSprite());
Globals::window.draw(*mapMaker.getText());
Globals::window.draw(*playGame.getSprite());
Globals::window.draw(*playGame.getText());
Globals::window.display();
}
}
void Globals::handleEvents()
{
while (Globals::window.pollEvent(Globals::event))
{
switch(event.type)
{
case sf::Event::Closed:
Globals::window.close();
break;
case sf::Event::KeyPressed:
if(Globals::event.key.code == sf::Keyboard::Tab)
{
paused = itemMenu.toggle();
}
else if(Globals::event.key.code == sf::Keyboard::W)
{
if(paused)
{
//move up in menu
itemMenu.moveActiveItem(0, -1);
}
}
else if(Globals::event.key.code == sf::Keyboard::S)
{
if(paused)
{
//move down in menu
itemMenu.moveActiveItem(0, 1);
}
}
else if(Globals::event.key.code == sf::Keyboard::A)
{
if(paused)
{
//move left menu
itemMenu.moveActiveItem(-1, 0);
}
}
else if(Globals::event.key.code == sf::Keyboard::D)
{
if(paused)
{
//move right menu
itemMenu.moveActiveItem(1, 0);
}
}
else if(Globals::event.key.code == sf::Keyboard::E)
{
if(!paused)
{
//this is gonna need some work. currently does nothing
player.attack();
sf::Vector2i playerPosition = player.getLocation();
gameMap.setTile(playerPosition.x / 32, playerPosition.y / 32, player.summon()); // divided by length of a tile
}
else
{
//select item inside the ui object?
//this will allow you to set the current type of tile to be placed as well as its properties (colidable, animated, etc)
}
}
break;
default:
break;
}
}
if(!paused) //check for movement keys pressed
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
if(!(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::S)))
{
//move up
player.moveEvent(0, 1, Globals::window.getPosition());
}
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
//move down
player.moveEvent(0, -1, Globals::window.getPosition());
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
if(!(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::D)))
{
//move left
player.moveEvent(-1, 0, Globals::window.getPosition());
}
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
//move right
player.moveEvent(1, 0, Globals::window.getPosition());
}
}
}
void Globals::update()
{
//all of the code for everything that needs to be upadted in the game is to be put here
//update player information
player.update(view, gameClock.getElapsedTime() + fps);
}
sf::Texture Globals::getTexture(int textureNumb)
{
if(textureNumb < sizeof(textures))
{
return textures[textureNumb].texture;
}
else
{
std::cout<<"error getting texture number: "<< textureNumb << " using texture 1 instead.";
return textures[0].texture;
}
}
//static globals variables
sf::RenderWindow Globals::window;
sf::Event Globals::event;
sf::View Globals::view;
sf::View Globals::userInterface;
sf::Font Globals::gameFont;
sf::Clock Globals::gameClock;
sf::Time Globals::gameTime;
sf::Time Globals::accumulator;
UIPlayerMenu Globals::itemMenu;