gpt4 book ai didi

c++ - SDL2 错误我在做什么 错了

转载 作者:行者123 更新时间:2023-11-28 02:26:39 24 4
gpt4 key购买 nike

在我尝试加载我的图像之前它正在工作。

这是我得到的错误:

Error 1 error LNK2005: "struct SDL_Window * m_pWindow"(?m_pWindow@@3PAUSDL_Window@@A) already defined inGame.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj

Error 2 error LNK2005: "struct SDL_Renderer * m_pRenderer"(?m_pRenderer@@3PAUSDL_Renderer@@A) already defined inGame.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj

Error 3 error LNK2005: "struct SDL_Texture * m_pTexture"(?m_pTexture@@3PAUSDL_Texture@@A) already defined inGame.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj

Error 4 error LNK2005: "struct SDL_Rect m_sourceWREK"
(?m_sourceWREK@@3USDL_Rect@@A) already defined inGame.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj

Error 5 error LNK2005: "struct SDL_Rect m_destWREK"
(?m_destWREK@@3USDL_Rect@@A) already defined inGame.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj

Error 7 error LNK1169: one or more multiply defined symbols found
C:\Users\Joseph\Desktop\DuckGotti\Debug\DuckGotti.exe 1

这是我的代码(main.cpp):

#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include "Game.h"






/**my first game obj**/
Game* g_game = 0;


int main(int argc, char* argv[])
{
g_game = new Game();
g_game->init("Duck Gotti", 100, 100, 640, 480, SDL_WINDOW_FULLSCREEN );

while (g_game->running())
{
g_game->handelEvents();
g_game->update();
g_game->render();
}
g_game->clean();
return 0;
}

游戏.h

#ifndef __Game__
#define __Game__
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
#include <iostream>

SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;

SDL_Texture* m_pTexture;
SDL_Rect m_sourceWREK;
SDL_Rect m_destWREK;

class Game
{
public:
Game(){}
~Game(){}

/**setting the run var to true**/
bool init(const char* title, int xpos, int ypos, int width, int height
,bool fullscreen);

void render();
void update();
void handelEvents();
void clean();
/** a func to access the privet var **/
bool running() { return m_bRunning; }

private:
SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;
bool m_bRunning;

};

#endif /**defined (__Game__) **/

游戏.cpp:

#include "game.h"


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 initalize SDL**/
if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
std::cout << "SDL init sucess/n";
/**init the winn**/
m_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if (m_pWindow !=0)
{
std::cout << "window creation sucess/n";
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);

if (m_pRenderer != 0)
{
std::cout << "renderer created/n";
SDL_SetRenderDrawColor(m_pRenderer, 255, 255, 255, 255);
}
else
{
std::cout << "renderer broked/n";
return false;
}
}
else
{
std::cout << "winn init nope/n";
return false;
}
}
else
{
std::cout << "SDL init broked/n";
return false;
}
std::cout << "init sucess/n";
m_bRunning = true;
return true;

SDL_Surface* pTempSurf = SDL_LoadBMP("img3.bmp");
m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer, pTempSurf);
SDL_FreeSurface(pTempSurf);
SDL_QueryTexture(m_pTexture, NULL, NULL, &m_sourceWREK.w, &m_destWREK.h);

m_destWREK.x = m_sourceWREK.x = 0;
m_destWREK.y = m_sourceWREK.y = 0;
m_destWREK.w = m_sourceWREK.w;
m_destWREK.h = m_sourceWREK.h;


}
void Game::render()
{
SDL_RenderClear(m_pRenderer);
SDL_RenderCopy(m_pRenderer, m_pTexture, &m_sourceWREK, &m_destWREK);
SDL_RenderPresent(m_pRenderer);

}
void Game::update()
{


}
void Game::handelEvents()
{
SDL_Event evil;
if (SDL_PollEvent(&evil))
{
switch (evil.type)
{
case SDL_QUIT:
m_bRunning = false;
break;
default:
break;
}
}
}
void Game::clean()
{
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();

}

最佳答案

我相信你正在读这本书SDL Game Development .请注意,这本书有很多错误。

删除这些行

SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;

SDL_Texture* m_pTexture;
SDL_Rect m_sourceWREK;
SDL_Rect m_destWREK;

它们已经在游戏类中定义。

我正在读这本书,幸运的是我有条理并且保留了文件。看来你在第一章

main.cpp

#include "game.h"

Game* g_game = 0;


int main(int argc, char* args[])
{
g_game = new Game();
g_game->init("Chapter 1: Setting up SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);


while( g_game->running() ){

g_game->handleEvents();
g_game->update();
g_game->render();
SDL_Delay(10);
}

g_game->clean();

return 0;
}

game.h

#ifndef _GAME_H
#define _GAME_H

#include "SDL.h"

class Game
{
public:
Game() {}
~Game() {}
bool init(const char* title, int xpos, int ypos, int height, int width, int flags);
void render();
void update();
void handleEvents();
void clean();

bool running() { return m_bRunning; }

private:
SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;

bool m_bRunning;

};



#endif /* defined(_GAME_H) */

游戏.cpp

#include "game.h"
#include <iostream>


bool Game::init(const char* title, int xpos, int ypos, int height, int width, int flags)
{
if ( SDL_Init(SDL_INIT_EVERYTHING) == 0 ){
std::cout << "SDL init success\n";
m_pWindow = SDL_CreateWindow(title, xpos, ypos, height, width, flags);

if ( m_pWindow != 0 ){
std::cout << "window creation success \n";
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);

if ( m_pRenderer != 0 ){
std::cout << "renderer creation success\n";
SDL_SetRenderDrawColor(m_pRenderer, 0, 0, 0, 255);
}else{
std::cout << "renderer init fail\n";
return false;
}
}else{
std::cout << "window init fail\n";
return false;
}
}else{
std::cout << "SDL init fail\n";
return false;
}

std::cout << "init success\n";
m_bRunning = true;
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@



return true;
}

void Game::render()
{
SDL_SetRenderDrawColor(m_pRenderer, 0, 0, 0, 255);
SDL_RenderClear(m_pRenderer);
SDL_RenderPresent(m_pRenderer);
}

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


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()
{

}

这些是我写的一些笔记

enter image description here

enter image description here

enter image description here

在第 2 章之后,您需要组织您的类,因为继承很容易让您迷失方向。例如下图,你会在第 3 章看到类的连接方式

enter image description here

对于第 4 章,

enter image description here

希望这对您有所帮助。

关于c++ - SDL2 错误我在做什么 错了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30418702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com