SFML - 从头开始​​的LNK2001游戏

时间:2014-10-27 18:39:58

标签: c++ visual-c++ 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" (?Default@RenderStates@sf@@2V12@B)
1>SplashScreen.obj : error LNK2001: unresolved external symbol "public: static class sf::RenderStates const sf::RenderStates::Default" (?Default@RenderStates@sf@@2V12@B)
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 ==========

我没有得到的是我在Game.cpp中定义SplashScreen和MainMenu ......

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;
};

非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

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