我正在尝试初始化我的游戏窗口,但无法解决此错误。我试过了:
请参阅以下代码供您参考:
标题文件:
#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&gt; SDL2main.lib(SDL_windows_main.obj):错误LNK2019:函数main_utf8中引用的未解析的外部符号SDL_main
1&gt; C:\ Dev \ 2D_GameEngine \ x64 \ Debug \ 2D_GameEngine.exe:致命错误LNK1120:1个未解析的外部
非常感谢你的帮助!
答案 0 :(得分:1)
SDL(不幸的是,IMO)做了一件奇怪的事情,他们重新定义main
,以便抽象出应用程序中入口点的平台特定细节。要不使用此功能,请执行以下操作。在加入SDL.h
之前,请先定义SDL_MAIN_HANDLED
。
#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>
这抑制了main
的重新定义。然后,在main
中,在使用SDL执行任何其他操作之前,请调用此函数
SDL_SetMainReady();
这将执行SDL所需的任何初始化(除了将名为SDL_MainIsReady
的全局变量设置为true之外,目前只执行任何操作)。