Oto cały kod źródłowy wraz z logiem błędów:
main.cpp:
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>
#include <SFML\System.hpp>
#include <SFML\Audio.hpp>
#include "Engine.hpp"
#include "Data.hpp"
#include<iostream>
int main() {
//Create Window
W.create(sf::VideoMode(1080, 720, 32), "War in space");
W.setActive(true);
W.setKeyRepeatEnabled(true);
W.setFramerateLimit(30);
W.setPosition(sf::Vector2i(580, 50));
//Initialize textures and sprites
GlobalFont.loadFromFile("vollkorn-4-105/TTF/Vollkorn-bold.ttf");
DataInitialization();
//Initialize Objects
SpaceShip PlayerShip = SpaceShip(A1);
//Create Views
sf::View MainMenu;
MainMenu.setCenter(0, 0);
MainMenu.setSize( 1080 , 720);
W.setView(MainMenu);
PlayerShip.setPosition(sf::Vector2f(100,100));
sf::Event e;
while (W.isOpen()) {
const sf::Vector2i mousePos = sf::Mouse::getPosition(W);
const sf::Vector2f mouseCoord = W.mapPixelToCoords(mousePos);
bool PlayerUp = false;
bool PlayerDown = false;
bool PlayerRight = false;
bool PlayerLeft = false;
//Event system
while (W.pollEvent(e)) {
if (e.type == sf::Event::Closed || (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
W.close();
bool isLeftMousePressed;
}
//Move player
if (true) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) == true)
PlayerUp = true;
else
PlayerUp = false;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) == true)
PlayerDown = true;
else
PlayerDown = false;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) == true)
PlayerRight = true;
else
PlayerRight = false;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) == true)
PlayerLeft = true;
else
PlayerLeft = false;
PlayerShip.Move(PlayerUp, PlayerDown, PlayerRight, PlayerLeft);
}
//Render
W.clear();
//Main UI
W.draw(MainMenuBackground);
if (UIStatus == Standard) {
W.draw(NewGameButton);
W.draw(LoadButton);
W.draw(OptionsButton);
W.draw(CreatorsButton);
W.draw(ExitButton);
W.draw(MainMenuText1);
W.draw(MainMenuText2);
}
if (UIStatus != Standard) {
W.draw(MenuXButton);
}
if (UIStatus == Options) {
W.draw(LanguageBackground);
W.draw(LanguageLeftButton);
W.draw(LanguageRightButton);
}
//W.draw(PlayerShip.sprite);
if (LevelStarted) {
W.draw(PlayerShip);
}
W.display();
}
return 0;
}
Data.hpp:
#pragma once
#ifndef DATA_HPP_
#define DATA_HPP_
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>
#include <SFML\System.hpp>
#include <SFML\Audio.hpp>
#include <string>
#include<cmath>
#include "Engine.hpp"
extern sf::RenderWindow W;
extern sf::Texture MainMenuBackgroundTex;
extern sf::Texture ButtonTex;
extern sf::Texture ButtonMouseOnTex;
extern sf::Texture SmallButtonTex;
extern sf::Texture SmallButtonMouseOnTex;
extern ShipTex A1tex;
extern ShipTex A2tex;
extern ShipTex C1tex;
extern ShipTex C2tex;
extern ShipTex B9tex;
extern Button NewGameButton;
extern Button LoadButton;
extern Button CreatorsButton;
extern Button ExitButton;
extern Button OptionsButton;
extern Button MenuXButton;
extern Button LanguageLeftButton;
extern Button LanguageRightButton;
extern sf::RectangleShape LanguageBackground;
extern sf::Sprite MainMenuBackground;
extern SpaceShip A1;
extern SpaceShip A2;
extern SpaceShip C1;
extern SpaceShip C2;
extern SpaceShip B9;
extern sf::Font GlobalFont;
extern sf::Text MainMenuText1;
extern sf::Text MainMenuText2;
extern sf::Text OptionsLanguageText;
extern sf::Text ButtonText;
extern SpaceShip Enemies[100];
extern Language TextLanguage;
extern UI UIStatus;
extern bool LevelStarted;
extern int playerCoins;
void inline DataInitialization();
void inline ConfigurationInitialization();
void inline TextureInitilization();
void inline GUIInitialization();
#endif
Data.cpp:
#include "Data.hpp"
#include "func.h"
#include <fstream>
void inline DataInitialization() {
ConfigurationInitialization();
TextureInitilization();
GUIInitialization();
}
void inline ConfigurationInitialization() {
UIStatus = Standard;
LevelStarted = false;
TextLanguage = EN;
}
void inline TextureInitilization() {
ButtonTex.loadFromFile("textures/GUI/Button.png");
ButtonMouseOnTex.loadFromFile("textures/GUI/ButtonMouseOn.png");
SmallButtonTex.loadFromFile("textures/GUI/SmallButton.png");
SmallButtonMouseOnTex.loadFromFile("textures/GUI/SmallButtonMouseOn.png");
MainMenuBackgroundTex.loadFromFile("textures/GUI/MainMenuBackground.png");
A1tex.NoThurst.loadFromFile("textures/ships/A-1 nothurst.png");
}
void inline GUIInitialization() {
MainMenuBackground.setTexture(MainMenuBackgroundTex);
MainMenuBackground.setPosition(sf::Vector2f(0, 0));
MainMenuBackground.setScale(3, 3);
MainMenuBackground.setOrigin(180, 120);
NewGameButton = Button(ButtonTex, ButtonMouseOnTex, L"New game", sf::Vector2f(-450, -250), GlobalFont, 50, sf::Color(0, 0, 0), sf::Color(255, 255, 255), NewGameButtonFunc);
LoadButton = Button(ButtonTex, ButtonMouseOnTex, L"Load", sf::Vector2f(-450, -125), GlobalFont, 50, sf::Color(0, 0, 0), sf::Color(255, 255, 255), LoadButtonFunc);
OptionsButton = Button(ButtonTex, ButtonMouseOnTex, L"Options", sf::Vector2f(-450, 0), GlobalFont, 50, sf::Color(0, 0, 0), sf::Color(255, 255, 255), OptionsButtonFunc);
CreatorsButton = Button(ButtonTex, ButtonMouseOnTex, L"Creators", sf::Vector2f(-450, 125), GlobalFont, 50, sf::Color(0, 0, 0), sf::Color(255, 255, 255), CreatorsButtonFunc);
ExitButton = Button(ButtonTex, ButtonMouseOnTex, L"Exit", sf::Vector2f(-450, 250), GlobalFont, 50, sf::Color(0, 0, 0), sf::Color(255, 255, 255), ExitButtonFunc);
MenuXButton = Button(SmallButtonTex, SmallButtonMouseOnTex, L"×", sf::Vector2f(450, -300), GlobalFont, 100, sf::Color(0, 0, 0), sf::Color(255, 255, 255), MenuXButtonFunc);
LanguageLeftButton = Button(SmallButtonTex, SmallButtonMouseOnTex, L"<", sf::Vector2f(-300, -250), GlobalFont, 70, sf::Color(0, 0, 0), sf::Color(255, 255, 255), LanguageLeftButtonFunc);
LanguageRightButton = Button(SmallButtonTex, SmallButtonMouseOnTex, L">", sf::Vector2f(0, -250), GlobalFont, 70, sf::Color(0, 0, 0), sf::Color(255, 255, 255), LanguageRightButtonFunc);
LanguageBackground.setFillColor(sf::Color(10, 10, 10));
LanguageBackground.setPosition(-204, -250);
LanguageBackground.setSize(sf::Vector2f(204, 96));
MainMenuText1.setFont(GlobalFont);
MainMenuText1.setPosition(sf::Vector2f(0, -250));
MainMenuText1.setScale(2, 2);
MainMenuText1.setFillColor(sf::Color(0, 128, 255));
MainMenuText2.setFont(GlobalFont);
MainMenuText2.setPosition(sf::Vector2f(200, -150));
MainMenuText2.setFillColor(sf::Color(0, 191, 230));
MainMenuText1.setString("War in space");
MainMenuText2.setString("First attack");
}
Engine.hpp:
#pragma once
#ifndef ENGINE_HPP_
#define ENGINE_HPP_
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>
#include <SFML\System.hpp>
#include <SFML\Audio.hpp>
#include <cmath>
enum Behavior {Wave, Kamikaze, Null, Slow, Random};
enum Language
{
PL, EN
};
enum UI {
Standard, Options, Creators
};
struct ShipTex
{
sf::Texture NoThurst;
sf::Texture Up;
sf::Texture Down;
sf::Texture Left;
sf::Texture Right;
};
sf::Vector2f centerText(const sf::Text& Text, const sf::Sprite& button);
double Angle_Between_Points(sf::Vector2f a, sf::Vector2f b);
template <typename T>
class GameObject : public sf::Drawable {
protected:
int HP;
T Texture;
sf::Sprite Sprite;
GameObject() { HP = 1;}
~GameObject() { delete this; }
GameObject(int hp, T textures) {
HP = hp;
Texture = textures;
}
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override {
target.draw(Sprite, states);
}
void collision(GameObject& target) {
HP -= target.getHP();
}
public:
int getHP() { return HP; }
};
class SpaceShip : public GameObject<ShipTex> {
private:
int Ammo;
int GunDamage;
int Rockets;
int RocketDamage;
int Speed;
int Fuel;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override {
target.draw(Sprite, states);
}
public:
SpaceShip(ShipTex textures, int hp, int ammo, int gunDamage, int rockets, int rocketDamage, int speed, int fuel) {
Texture = textures;
HP = hp;
Ammo = ammo;
GunDamage = gunDamage;
Rockets = rockets;
RocketDamage = rocketDamage;
Speed = speed;
Fuel = fuel;
}
SpaceShip(const SpaceShip& ship) {
Texture = ship.Texture;
HP = ship.HP;
Ammo = ship.Ammo;
GunDamage = ship.GunDamage;
Rockets = ship.Rockets;
RocketDamage = ship.RocketDamage;
Speed = ship.Speed;
Fuel = ship.Fuel;
}
SpaceShip() : Ammo(0), GunDamage(0), Rockets(0), RocketDamage(0), Speed(0), Fuel(0) {}
void setPosition(sf::Vector2f pos) {
Sprite.setPosition(pos);
}
void Move(bool Up, bool Down, bool Right, bool Left) {
int x, y;
x = Sprite.getPosition().x;
y = Sprite.getPosition().y;
if (Up)
y -= Speed;
if (Down)
y += Speed;
if (Right)
x += Speed;
if (Left)
x -= Speed;
Sprite.setPosition(x, y);
}
};
/*
class Enemy : public SpaceShip {
public:
Behavior behavior;
};
*/
class Button : public sf::Drawable {
private:
typedef void(*buttonFunc) ();
sf::Sprite ButtonBackground;
sf::Text Text;
sf::Texture StandardTexture;
sf::Texture MouseOnTexture;
sf::Color StandardTextColor;
sf::Color MouseOnTextColor;
//sf::Music mouseOnMusic;
//sf::Music clickMusic;
buttonFunc ButtonFunc;
sf::Vector2f centerText(const sf::Text& Text, const sf::Sprite& button) {
sf::FloatRect textRect = Text.getLocalBounds();
return sf::Vector2f(button.getPosition().x + (button.getGlobalBounds().width - textRect.width) / 2.0f - textRect.left,
button.getPosition().y + (button.getGlobalBounds().height - textRect.height) / 2.0f - textRect.top);
}
void updateTextPosition() {
Text.setPosition(centerText(Text, ButtonBackground));
}
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override {
target.draw(ButtonBackground, states);
target.draw(Text, states);
}
public:
//Constuctors
Button() {}
~Button() {};
template<typename F>
Button(sf::Texture& texture, sf::Texture & mouseOnTexture, std::wstring string, sf::Vector2f position, sf::Font& font, int characterSize,
sf::Color textColor, sf::Color mouseOnTextColor, F func) {
StandardTexture = texture;
MouseOnTexture = mouseOnTexture;
StandardTextColor = textColor;
MouseOnTextColor = mouseOnTextColor;
ButtonBackground.setTexture(StandardTexture);
ButtonBackground.setPosition(position);
ButtonBackground.setScale(3, 3);
ButtonBackground.setOrigin(0, 0);
Text.setString(string);
Text.setFont(font);
Text.setCharacterSize(characterSize);
Text.setFillColor(StandardTextColor);
updateTextPosition();
ButtonFunc = func;
}
//Set
void setButtonStandardTexture(sf::Texture& texture) {
StandardTexture = texture;
}
void setButtonMouseOnTexture(sf::Texture& texture) {
MouseOnTexture = texture;
}
void setTextures(sf::Texture& standardTexture, sf::Texture& mouseOnTexture) {
StandardTexture = standardTexture;
MouseOnTexture = mouseOnTexture;
}
void setPosition(sf::Vector2f position) {
ButtonBackground.setPosition(position);
updateTextPosition();
}
void setFont(sf::Font& font) {
Text.setFont(font);
updateTextPosition();
}
void setCharacterSize(int size) {
Text.setCharacterSize(size);
updateTextPosition();
}
void setString(std::wstring string) {
Text.setString(string);
updateTextPosition();
}
void setTextStandardColor(sf::Color color) {
StandardTextColor = color;
}
void setTextMouseOnColor(sf::Color color) {
MouseOnTextColor = color;
}
void setTextColors(sf::Color standardColor, sf::Color mouseOnColor) {
StandardTextColor = standardColor;
MouseOnTextColor = mouseOnColor;
}
//Get
sf::Vector2f getPosition() const {
return ButtonBackground.getPosition();
}
int getCharacterSize() const {
return Text.getCharacterSize();
}
std::wstring getString() const {
Text.getString();
}
//Other
bool isMouseOn(sf::Vector2f mousePos) {
if (mousePos.x > ButtonBackground.getPosition().x &&
mousePos.x < ButtonBackground.getPosition().x + ButtonBackground.getGlobalBounds().width &&
mousePos.y > ButtonBackground.getPosition().y &&
mousePos.y < ButtonBackground.getPosition().y + ButtonBackground.getGlobalBounds().height) {
ButtonBackground.setTexture(MouseOnTexture);
Text.setFillColor(MouseOnTextColor);
return true;
}
else {
ButtonBackground.setTexture(StandardTexture);
Text.setFillColor(StandardTextColor);
return false;
}
}
};
#endif
Engine.cpp:
#include"Engine.hpp"
sf::Vector2f centerText(const sf::Text& Text, const sf::Sprite& button) {
sf::FloatRect textRect = Text.getLocalBounds();
return sf::Vector2f(button.getPosition().x + (button.getGlobalBounds().width - textRect.width) / 2.0f - textRect.left,
button.getPosition().y + (button.getGlobalBounds().height - textRect.height) / 2.0f - textRect.top);
}
double Angle_Between_Points(sf::Vector2f a, sf::Vector2f b) {
double A, B, C;
A = a.x - b.x;
B = a.y - b.y;
C = std::pow(A * A + B * B, 0.5);
return std::asin(A / C) * 180 / 3.14159265358979323846;
}
func.h:
#pragma once
#ifndef FUNC_H_
#define FUNC_H_
void inline NewGameButtonFunc();
void inline LoadButtonFunc();
void inline OptionsButtonFunc();
void inline CreatorsButtonFunc();
void inline ExitButtonFunc();
void inline MenuXButtonFunc();
void inline LanguageLeftButtonFunc();
void inline LanguageRightButtonFunc();
#endif
func.cpp:
#include "func.h"
#include "Data.hpp"
void inline NewGameButtonFunc() {
playerCoins = 200;
}
void inline LoadButtonFunc() {
}
void inline OptionsButtonFunc() {
UIStatus = Options;
}
void inline CreatorsButtonFunc() {
UIStatus = Creators;
}
void inline ExitButtonFunc() {
W.close();
}
void inline MenuXButtonFunc() {
UIStatus = Standard;
}
void inline LanguageLeftButtonFunc() {
}
Błędy:
1>main.obj : error LNK2019: nierozpoznany symbol zewnętrzny "void __cdecl DataInitialization(void)" (?DataInitialization@@YAXXZ) przywołany w funkcji main
1>main.obj : error LNK2001: nierozpoznany symbol zewnętrzny "class sf::RenderWindow W" (?W@@3VRenderWindow@sf@@A)
1>main.obj : error LNK2001: nierozpoznany symbol zewnętrzny "class Button NewGameButton" (?NewGameButton@@3VButton@@A)
1>main.obj : error LNK2001: nierozpoznany symbol zewnętrzny "class Button LoadButton" (?LoadButton@@3VButton@@A)
1>main.obj : error LNK2001: nierozpoznany symbol zewnętrzny "class Button CreatorsButton" (?CreatorsButton@@3VButton@@A)
1>main.obj : error LNK2001: nierozpoznany symbol zewnętrzny "class Button ExitButton" (?ExitButton@@3VButton@@A)
1>main.obj : error LNK2001: nierozpoznany symbol zewnętrzny "class Button OptionsButton" (?OptionsButton@@3VButton@@A)
1>main.obj : error LNK2001: nierozpoznany symbol zewnętrzny "class Button MenuXButton" (?MenuXButton@@3VButton@@A)
1>main.obj : error LNK2001: nierozpoznany symbol zewnętrzny "class Button LanguageLeftButton" (?LanguageLeftButton@@3VButton@@A)
1>main.obj : error LNK2001: nierozpoznany symbol zewnętrzny "class Button LanguageRightButton" (?LanguageRightButton@@3VButton@@A)
1>main.obj : error LNK2001: nierozpoznany symbol zewnętrzny "class sf::RectangleShape LanguageBackground" (?LanguageBackground@@3VRectangleShape@sf@@A)
1>main.obj : error LNK2001: nierozpoznany symbol zewnętrzny "class sf::Sprite MainMenuBackground" (?MainMenuBackground@@3VSprite@sf@@A)
1>main.obj : error LNK2001: nierozpoznany symbol zewnętrzny "class SpaceShip A1" (?A1@@3VSpaceShip@@A)
1>main.obj : error LNK2001: nierozpoznany symbol zewnętrzny "class sf::Font GlobalFont" (?GlobalFont@@3VFont@sf@@A)
1>main.obj : error LNK2001: nierozpoznany symbol zewnętrzny "class sf::Text MainMenuText1" (?MainMenuText1@@3VText@sf@@A)
1>main.obj : error LNK2001: nierozpoznany symbol zewnętrzny "class sf::Text MainMenuText2" (?MainMenuText2@@3VText@sf@@A)
1>main.obj : error LNK2001: nierozpoznany symbol zewnętrzny "enum UI UIStatus" (?UIStatus@@3W4UI@@A)
1>main.obj : error LNK2001: nierozpoznany symbol zewnętrzny "bool LevelStarted" (?LevelStarted@@3_NA)
Projekt jest w trakcie przebudowy, więc mogły pozostać w nim resztki starych, tymczasowych rozwiązań.