2015-01-04 25 views
0

我正在關注「Shaun Mitchell開發的SDL遊戲開發」一書,以瞭解SDL2,並遇到了我無法解決的編譯器錯誤。在嘗試構建項目時,我收到了「錯誤C2011:'TextureManager':'class'type redefinition」。我正在使用Visual Studio 2008.我已經添加了預編譯命令,正如通常針對此錯誤所建議的那樣。我已經清理了該項目,然後構建了它。每次嘗試構建項目時都會出現錯誤。收到「錯誤C2011:'TextureManager':'class'type redefinition」錯誤。 #ifndef #define文件中的#endif

TextureManager.h

#include "SDL.h" 
#include <iostream> 
#include <map> 
#include <string> 

#ifndef __TextureManger__ 
#define __TextureManager__ 


class TextureManager 
{ 
    public: 
     bool load(std::string fileName,std::string id,SDL_Renderer* pRenderer); 
    // draw 
    void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 

    // drawframe 
    void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 

    std::map<std::string, SDL_Texture*> m_textureMap; 
}; 


#endif /* defined(__TextureManager__) */ 

TextureManager.cpp

#include "TextureManager.h" 
#include "SDL_image.h" 
#include "SDL.H" 

bool TextureManager::load(std::string fileName, std::string id, SDL_Renderer* pRenderer) 
{ 
SDL_Surface* pTempSurface = IMG_Load(fileName.c_str()); 
if(pTempSurface == 0) 
{ 
    return false; 
} 
SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface); 
SDL_FreeSurface(pTempSurface); 
// everything went ok, add the texture to our list 
if(pTexture != 0) 
{ 
    m_textureMap[id] = pTexture; 
    return true; 
} 
// reaching here means something went wrong 
return false; 
} 

void TextureManager::draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip) 
{ 
SDL_Rect srcRect; 
SDL_Rect destRect; 
srcRect.x = 0; 
srcRect.y = 0; 
srcRect.w = destRect.w = width; 
srcRect.h = destRect.h = height; 
destRect.x = x; 
destRect.y = y; 
SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 
} 

void TextureManager::drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer *pRenderer, SDL_RendererFlip flip) 
{ 
SDL_Rect srcRect; 
SDL_Rect destRect; 
srcRect.x = width * currentFrame; 
srcRect.y = height * (currentRow - 1); 
srcRect.w = destRect.w = width; 
srcRect.h = destRect.h = height; 
destRect.x = x; 
destRect.y = y; 
SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 
} 

Game.h

#include "SDL.h" 
#include "TextureManager.h" 
#include <iostream> 

#ifndef __Game__ 
#define __Game__ 

class Game 
{ 
public: 
    Game() {} 
    ~Game() {} 
    // simply set the running variable to true 
    bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); 
    void render(); 
    void update(); 
    void handleEvents(); 
    void clean(); 

    // a function to access the private running variable 
    bool running() 
    { 
      return m_bRunning; 
    } 
private: 
    SDL_Window* m_pWindow; 
    SDL_Renderer* m_pRenderer; 
    int m_currentFrame; 
    TextureManager m_textureManager; 
    bool m_bRunning; 
}; 
#endif /* defined(__Game__) */ 

Game.cpp

#include "Game.h" 
#include "SDL_image.h" 
#include "TextureManager.h" 

//defined init function 
bool Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen) 
{ 
int flags = 0; 
if(fullscreen) 
{ 
    flags = SDL_WINDOW_FULLSCREEN; 
} 
//attempt to initialize SDL 
if(SDL_Init(SDL_INIT_EVERYTHING) == 0) 
{ 
    std::cout << "SDL init success\n"; 
    // init the window 
    m_pWindow = SDL_CreateWindow(title, xpos, ypos, 
    width, height, flags); 
    if(m_pWindow != 0) // window init success 
    { 
     std::cout << "window creation success\n"; 
     m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0); 
     if(m_pRenderer != 0) // renderer init success 
     { 
      std::cout << "renderer creation success\n"; 
      SDL_SetRenderDrawColor(m_pRenderer,255,0,0,255); 
     } 
     else 
     { 
      std::cout << "renderer init fail\n"; 
      return false; // renderer init fail 
     } 
    } 
    else 
    { 
     std::cout << "window init fail\n"; 
     return false; // window init fail 
    } 
} 
else 
{ 
    std::cout << "SDL init fail\n"; 
    return false; // SDL init fail 
} 
std::cout << "init success\n"; 
//Load the image 
m_textureManager.load("assets/animate-alpha.png", "animate", m_pRenderer); 
m_bRunning = true; // everything inited successfully, start the main loop 
return true; 
} 

//define render function 
void Game::render() 
{ 
SDL_RenderClear(m_pRenderer); //clear 
m_textureManager.draw("animate", 0,0, 128, 82, m_pRenderer); 
m_textureManager.drawFrame("animate", 100,100, 128, 82, 1, m_currentFrame, m_pRenderer); 
SDL_RenderPresent(m_pRenderer); //draw 
} 


//defining clean function 
void Game::clean() 
{ 
std::cout << "cleaning game\n"; 
SDL_DestroyWindow(m_pWindow); 
SDL_DestroyRenderer(m_pRenderer); 
SDL_Quit(); 
} 

//handle event function 
void Game::handleEvents() 
{ 
SDL_Event event; 
if(SDL_PollEvent(&event)) 
{ 
    switch (event.type) 
    { 
     case SDL_QUIT: 
      m_bRunning = false; 
      break; 
     default: 
      break; 
    } 
} 
} 

void Game::update() 
{ 
m_currentFrame = 128 * int(((SDL_GetTicks()/100) % 6)); 
} 

Main.cpp的

#include<SDL.h> 
#include <iostream> 
#include "Game.h" 
// our Game object 

Game* g_game = 0; 
int main(int argc, char* argv[]) 
{ 
    g_game = new Game(); 
    g_game->init("Chapter 1", 100, 100, 640, 480, 0); 
    while(g_game->running()) 
{ 
    g_game->handleEvents(); 
    g_game->update(); 
    g_game->render(); 
} 

g_game->clean(); 
return 0; 
} 

回答

0

它看起來像包括TextureManager後衛是不正確的(在#ifndef檢查拼寫不正確。

您有:

#ifndef __TextureManger__ 
#define __TextureManager__ 
. 
. 
. 
#endif 

它應該是:

#ifndef __TextureManager__ 
#define __TextureManager__ 
. 
. 
. 
#endif 
+0

謝謝,我想在代碼找了幾個小時後,我去了一個小十字架眼睛。謝謝。 – ironforge