Could someone help me with this code? When I compile & run my project, it says "Battlefront.exe has stopped working. Windows is looking for a solution to this problem"
This is my code:
//-----------------------------------------------
/// Battlefront.cpp Source Code File
/// Part of the Battlefront.cbp Code::Blocks Project
//-----------------------------------------------
/// This file's purpose is to provide general functions and the project's entry point
//-----------------------------------------------
#include "Battlefront.h"
#include "PlayerClass.h"
sf::RenderWindow MainWindow(sf::VideoMode(800, 600, 32), "Battlefront INDEV");
GameSection Mode;
PlayerClass PlayerA(UK);
int main()
{
if(!GameSetUp())
return EXIT_FAILURE;
GameLoop();
}
void Draw()
{
if(Mode == GAME)
{
MainWindow.Draw(PlayerA.Arms);
}
}
//-----------------------------------------------
/// GameEngine.cpp Source Code File
/// Part of the Battlefront.cbp Code::Blocks Project
//-----------------------------------------------
/// This file's purpose is to provide generic support for actions such as testing for events
//-----------------------------------------------
#include "Battlefront.h"
#include "PlayerClass.h"
bool GameSetUp()
{
// Declare the global variables
Mode = GAME;
if(!MatchSprites())
return false;
else
return true;
}
void GameLoop()
{
while(MainWindow.IsOpened())
{
sf::Event Event;
while(MainWindow.GetEvent(Event))
{
// General "anytime" events
if(Event.Type == sf::Event::Closed)
MainWindow.Close();
// Gamemode specific events
if(Mode == GAME)
{
if(Event.Type == sf::Event::KeyPressed)
HandleKeys(Event.Key.Code, true);
if(Event.Type == sf::Event::KeyReleased)
HandleKeys(Event.Key.Code, false);
}
else if(Mode == MENU || Mode == OPTIONS || Mode == LOBBY)
{
if(Event.Type == sf::Event::MouseButtonPressed)
HandleMouseClick(Event.MouseButton.Button, true);
if(Event.Type == sf::Event::MouseButtonReleased)
HandleMouseClick(Event.MouseButton.Button, false);
if(Event.Type == sf::Event::MouseMoved)
HandleMouseMove(Event.MouseMove.X, Event.MouseMove.Y);
}
}
PostEvent();
}
}
void PostEvent()
{
MainWindow.Clear();
Draw();
MainWindow.Display();
}
void CheckForCollisions()
{
}
bool TestCollision(sf::Sprite Hitter, sf::Sprite Hittee)
{
return false;
}
//-----------------------------------------------
/// VariableSetUp.cpp Source Code File
/// Part of the Battlefront.cbp Code::Blocks Project
//-----------------------------------------------
/// This file's purpose is to define all variables with "extern" in Battlefront.h
//-----------------------------------------------
#include "Battlefront.h"
#include "PlayerClass.h"
// Images
sf::Image iUK_Body;
sf::Image iUK_Head;
sf::Image iUK_Arms;
sf::Image iNZ_Body;
sf::Image iNZ_Head;
sf::Image iNZ_Arms;
sf::Image iFeet;
// Others
bool MatchSprites()
{
if(!iUK_Body.LoadFromFile("Res\\UK_Body.png"))
return false;
if(!iUK_Arms.LoadFromFile("Res\\UK_Arms.png"))
return false;
if(!iUK_Head.LoadFromFile("Res\\UK_Head.png"))
return false;
if(!iNZ_Body.LoadFromFile("Res\\NZ_Body.png"))
return false;
if(!iNZ_Arms.LoadFromFile("Res\\NZ_Arms.png"))
return false;
if(!iNZ_Head.LoadFromFile("Res\\NZ_Head.png"))
return false;
if(!iFeet.LoadFromFile("Res\\Feet.png"))
return false;
return true;
}
//-----------------------------------------------
/// HandleInput.cpp Source Code File
/// Part of the Battlefront.cbp Code::Blocks Project
//-----------------------------------------------
/// This file's purpose is to provide functions to handle input from events in GameEngine.cpp
//-----------------------------------------------
#include "Battlefront.h"
#include "PlayerClass.h"
void HandleKeys(sf::Key::Code Key, bool IsPressed)
{
}
void HandleMouseClick(sf::Mouse::Button Button, bool IsClicked)
{
if(Button == sf::Mouse::Left && IsClicked)
Mode = GAME;
}
void HandleMouseMove(int x, int y)
{
}
//-----------------------------------------------
/// PlayerClass.cpp Source Code File
/// Part of the Battlefront.cbp Code::Blocks Project
//-----------------------------------------------
/// The purpose of this file is to define the class "PlayerClass"
//-----------------------------------------------
#include "Battlefront.h"
#include "PlayerClass.h"
PlayerClass::PlayerClass(Nationality natl)
{
PlayerFeet.SetImage(iFeet);
HitPoints = 100;
CurrentClass = SCOUT_SNIPER;
if(natl == UK)
{
Arms.SetImage(iUK_Arms);
Body.SetImage(iUK_Body);
Head.SetImage(iUK_Head);
}
else
{
Arms.SetImage(iNZ_Arms);
Body.SetImage(iNZ_Body);
Head.SetImage(iNZ_Head);
}
}
PlayerClass::~PlayerClass()
{
}
//-----------------------------------------------
/// Battlefront.h Header Code File
/// Part of the Battlefront.cbp Code::Blocks Project
//-----------------------------------------------
#ifndef BATTLEFRONT_H
#define BATTLEFRONT_H
//----------------------
/// Includes
//----------------------
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
//----------------------
/// Enums
//----------------------
enum GameSection { MENU, OPTIONS, LOBBY, GAME };
enum Nationality { US, UK, RU, NZ, JP };
enum GameClass { SUPPORT, MEDIC, ENGINEER, MARKSMAN, OFFICER, SCOUT_SNIPER };
//----------------------
/// Global Variables
//----------------------
extern sf::RenderWindow MainWindow;
extern GameSection Mode;
// Images
extern sf::Image iUK_Body;
extern sf::Image iUK_Head;
extern sf::Image iUK_Arms;
extern sf::Image iNZ_Body;
extern sf::Image iNZ_Head;
extern sf::Image iNZ_Arms;
extern sf::Image iFeet;
// Sprites
//----------------------
/// Function Prototypes
//----------------------
bool GameSetUp();
void GameLoop();
void PostEvent();
bool TestCollision(sf::Sprite, sf::Sprite);
void CheckForCollisions();
void Draw();
bool MatchSprites();
void HandleKeys(sf::Key::Code, bool IsPressed);
void HandleMouseClick(sf::Mouse::Button, bool IsClicked);
void HandleMouseMove(int x, int y);
#endif
//-----------------------------------------------
/// PlayerClass.h Header Code File
/// Part of the Battlefront.cbp Code::Blocks Project
//-----------------------------------------------
/// This is a class header file for the class "PlayerClass"
//-----------------------------------------------
#ifndef PLAYERCLASS_H
#define PLAYERCLASS_H
#include "Battlefront.h"
class PlayerClass
{
public:
PlayerClass(Nationality); // Constructor
~PlayerClass(); // Destructor
sf::Sprite PlayerFeet;
sf::Sprite Arms;
sf::Sprite Body;
sf::Sprite Head;
int HitPoints;
GameClass CurrentClass;
};
#endif
The only idea I could come up with as to why the .exe file stops working is that maybe I'm using MainWindow incorrectly?
By the way, if you couldn't tell, I'm using SFML, hence the sf namespace.