问题描述
我正在尝试初始化我的游戏窗口,无法克服此错误.我已经尝试过:
I am trying to initialize my game window and can't get past this error. I have tried:
- 一遍又一遍地检查我所有的代码是否存在语法错误
- 检查SDL2库是否正确包含在我的项目中
请参阅以下代码供您参考:
Please see below code for your reference:
头文件:
#ifndef Game_hpp
#define Game_hpp
#include "SDL.h"
#include <iostream>
class Game
{
public:
Game();
~Game();
void init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
void handleEvents();
void update();
void render();
void clean();
bool running()
{
return isRunning;
}
private:
bool isRunning;
SDL_Window *window;
SDL_Renderer *renderer;
};
#endif /* Game_hpp */
Game.cpp-定义函数的地方
#include "Game.h"
Game::Game()
{}
Game::~Game()
{}
void Game::init(const char *title, int xpos, int ypos, int width, int height, bool fullscreen)
{
int flags = 0;
if (fullscreen)
{
flags = SDL_WINDOW_FULLSCREEN;
}
if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
std::cout << "Subsystems Initialised..." << std::endl;
window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if (window)
{
std::cout << "Window Created" << std::endl;
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer)
{
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
std::cout << "Renderer Created" << std::endl;
}
isRunning = true;
}
else
{
isRunning = false;
}
}
void Game::handleEvents()
{
SDL_Event event;
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_QUIT:
isRunning = false;
break;
default:
break;
}
}
void Game::update()
{}
void Game::render()
{
SDL_RenderClear(renderer);
//this is where we add stuff to render
SDL_RenderPresent(renderer);
}
void Game::clean()
{
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
std::cout << "Game Cleaned." << std::endl;
}
主要功能:
#include "Game.h"
Game *game = nullptr;
int main(int argc, const char * argv[])
{
game = new Game();
game->init("BirchEngine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false);
while (game->running())
{
game->handleEvents();
game->update();
game->render();
}
game->clean();
return 0;
}
我得到的错误:
1> SDL2main.lib(SDL_windows_main.obj):错误LNK2019:函数main_utf8中引用了未解析的外部符号SDL_main
1>SDL2main.lib(SDL_windows_main.obj) : error LNK2019: unresolved external symbol SDL_main referenced in function main_utf8
1> C:\ Dev \ 2D_GameEngine \ x64 \ Debug \ 2D_GameEngine.exe:致命错误LNK1120:1个未解决的外部组件
1>C:\Dev\2D_GameEngine\x64\Debug\2D_GameEngine.exe : fatal error LNK1120: 1 unresolved externals
非常感谢您的帮助!
推荐答案
SDL(不幸的是,IMO)在重新定义 main
的地方做了这个奇怪的事情,以便抽象出特定于平台的条目细节指向应用程序.为了不利用此功能,请执行以下操作.在包含 SDL.h
之前,首先定义 SDL_MAIN_HANDLED
.
SDL (unfortunately, IMO) does this strange thing where they redefine main
, in order to abstract away platform specific details of the entry point in applications. In order to not utilize this feature, perform the following. Before including SDL.h
, first define SDL_MAIN_HANDLED
.
#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>
禁止重新定义 main
.然后,在 main
中,在使用SDL进行任何其他操作之前,请调用此函数
That inhibits the redefinition of main
. Then, in main
, before doing anything else with SDL, call this function
SDL_SetMainReady();
这将执行SDL所需的任何初始化(当前除了将名为 SDL_MainIsReady
的全局变量设置为true以外,什么都不做).
That will do any initialization that is needed by SDL (currently does nothing other than setting a global variable named SDL_MainIsReady
to true).
这篇关于LNK2019-无法解析的外部符号-C ++-SDL2库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!