2014-10-27 92 views
0

我一直在这些论坛中潜伏,当时我需要整个大学编程课程的帮助,但最近我遇到了麻烦,无法在任何地方找到答案。SFML - LNK2001游戏从零开始

我开始学习,为矿井通过一个更大的项目编写游戏程序: http://www.gamefromscratch.com/page/Game-From-Scratch-CPP-Edition-Part-3.aspx

当我编译我的代码,我得到

1>------ Build started: Project: PANG, Configuration: Debug Win32 ------ 
1> Game.cpp 
1>MainMenu.obj : error LNK2001: unresolved external symbol "public: static class  sf::RenderStates const sf::RenderStates::Default" ([email protected]@[email protected]@[email protected]) 
1>SplashScreen.obj : error LNK2001: unresolved external symbol "public: static class sf::RenderStates const sf::RenderStates::Default" ([email protected]@[email protected]@[email protected]) 
1>G:\My Documents\Visual Studio 2012\Projects\PANG\Debug\PANG.exe : fatal error LNK1120: 1 unresolved externals 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

我不明白的事情是,我做的定义闪屏和MainMenu的在我的Game.cpp ...

Game.h

#pragma once 
#include <SFML/Window.hpp> 
#include <SFML/Graphics.hpp> 

class Game 
{ 
public: 
    static void Start(); 
    static sf::RenderWindow& GetWindow(); 

private: 
    static bool isExiting(); 
    static void GameLoop(); 

    static void ShowSplashScreen(); 
    static void ShowMenu(); 

    enum GameState { Uninitialized, ShowingSplash, Paused, ShowingMenu, Playing, Exiting }; 

    static GameState _gameState; 
    static sf::RenderWindow _mainWindow; 

}; 

Game.cpp

#include "stdafx.h" 
#include "Game.h" 
#include "MainMenu.h" 
#include "SplashScreen.h" 

void Game::Start(void) 
{ 
    if(_gameState != Uninitialized) 
     return; 

    _mainWindow.create(sf::VideoMode(1024,768,32),"Pang!"); 

    _gameState= Game::ShowingSplash; 

    while(!isExiting()) 
    { 
     GameLoop(); 
    } 

    _mainWindow.close(); 
} 

bool Game::isExiting() 
{ 
    if(_gameState == Game::Exiting) 
     return true; 
    else 
     return false; 
} 
sf::RenderWindow& Game::GetWindow() 
{ 
    return _mainWindow; 
} 

void Game::GameLoop() 
{ 
    switch(_gameState) 
    { 
    case Game::ShowingMenu: 
     { 
      ShowMenu(); 
      break; 
     } 
    case Game::ShowingSplash: 
     { 
      ShowSplashScreen(); 
      break; 
     } 
    case Game::Playing: 
     { 
      sf::Event currentEvent; 
      while(_mainWindow.pollEvent(currentEvent)) 
       { 
       _mainWindow.clear(sf::Color(0,0,0)); 
       _mainWindow.display(); 

       if(currentEvent.type == sf::Event::Closed) 
        { 
         _gameState = Game::Exiting; 
        } 

       if(currentEvent.type == sf::Event::KeyPressed) 
        { 
//       if(currentEvent.key.code == sf::Key::Escape) ShowMenu(); 
        } 
       } 
      break; 
     } 
    } 
} 

void Game::ShowSplashScreen() 
{ 
    SplashScreen splashScreen; 
    splashScreen.Show(_mainWindow); 
    _gameState = Game::ShowingMenu; 
} 

void Game::ShowMenu() 
{ 
    MainMenu mainMenu; 
    MainMenu::MenuResult result = mainMenu.Show(_mainWindow); 
    switch(result) 
    { 
    case MainMenu::Exit: 
     _gameState = Game::Exiting; 
     break; 
    case MainMenu::Play: 
     _gameState = Game::Playing; 
     break; 
    } 
} 

Game::GameState Game::_gameState = Uninitialized; 
sf::RenderWindow Game::_mainWindow; 

SplashScreen.cpp

#include "StdAfx.h" 
#include "SplashScreen.h" 

void SplashScreen::Show(sf::RenderWindow & renderWindow) 
{ 
    sf::Texture texture; 
    if(texture.loadFromFile("images/SplashScreen.png") != true) 
    { 
    return; 
    } 

sf::Sprite sprite(texture); 

renderWindow.draw(sprite); 
renderWindow.display(); 

sf::Event event; 
while(true) 
{ 
    while(renderWindow.pollEvent(event)) 
    { 
     if(event.type == sf::Event::EventType::KeyPressed 
      || event.type == sf::Event::EventType::MouseButtonPressed 
      || event.type == sf::Event::EventType::Closed) 
     { 
      return; 
     } 
    } 
} 


} 

SplashScreen.h

#pragma once 
#include <SFML/Window.hpp> 
#include <SFML/Graphics.hpp> 

class SplashScreen 
{ 
    public: 
    void Show(sf::RenderWindow& window); 
}; 

MainMenu.cpp

#include "stdafx.h" 
#include "MainMenu.h" 

MainMenu::MenuResult MainMenu::Show(sf::RenderWindow& window) 
{ 
    sf::Texture texture; 
    texture.loadFromFile("images/MainMenu.png"); 
    sf::Sprite sprite(texture); 

    MenuItem playButton; 
    playButton.rect.top = 145; 
    playButton.rect.height = 380; 
    playButton.rect.left = 0; 
    playButton.rect.width = 1023; 
    playButton.action = Play; 

    MenuItem exitButton; 
    exitButton.rect.top = 383; 
    exitButton.rect.left = 0; 
    exitButton.rect.width = 1023; 
    exitButton.rect.height = 560; 
    exitButton.action = Exit; 

    _menuItems.push_back(playButton); 
    _menuItems.push_back(exitButton); 

    window.draw(sprite); 
    window.display(); 

    return GetMenuResponse(window); 
} 

MainMenu::MenuResult MainMenu::HandleClick(int x,int y) 
{ 
    std::list<MenuItem>::iterator it; 

    for (it = _menuItems.begin(); it != _menuItems.end(); it++) 
    { 
    sf::Rect<int>menuItemRect = (*it).rect; 
    if(x > menuItemRect.left 
     && x < menuItemRect.left + menuItemRect.width 
     && y > menuItemRect.top 
     && y < menuItemRect.height + menuItemRect.top) 
     { 
      return (*it).action; 
     } 
    } 

    return Nothing; 
} 

MainMenu::MenuResult MainMenu::GetMenuResponse(sf::RenderWindow& window) 
{ 
    sf::Event menuEvent; 

    while(true) 
    { 
    while(window.pollEvent(menuEvent)) 
    { 
     if(menuEvent.type == sf::Event::MouseButtonPressed) 
     { 
      return HandleClick(menuEvent.mouseButton.x,menuEvent.mouseButton.y); 
     } 
     if (menuEvent.type == sf::Event:: Closed) 
     { 
      return Exit; 
     } 
    } 
    } 
} 

MainMenu.h

#pragma once 
#include "SFML/Window.hpp" 
#include "SFML/Graphics.hpp" 
#include <list> 

class MainMenu 
{ 
    public: 
    enum MenuResult {Nothing, Exit, Play}; 

    struct MenuItem 
    { 
    public: 
     sf::Rect<int> rect; 
     MenuResult action; 
    }; 

    MenuResult Show(sf::RenderWindow & window); 

    private: 
    MenuResult GetMenuResponse(sf::RenderWindow& window); 
    MenuResult HandleClick(int x, int y); 
    std::list<MenuItem> _menuItems; 
}; 

任何帮助将不胜感激,谢谢!

+0

您是否包含所有相关的.lib文件? – 2014-10-27 18:48:40

+0

以及我已添加的链接器的输入部分: sfml-system-d.lib; sfml-main-d.lib; sfml-graphics-d.lib; sfml-audio-d.lib; sfml-network- d.lib; SFML窗口-d.lib; 和链接器一般我已经包括图书馆 – 2014-10-27 18:55:10

回答

0

我找到了解决方案。我从属性中的预处理器中删除了SFML_STATIC并且它工作正常!感谢阿德里亚诺的帮助!