我使用SDL函数没有定义SDL_main。是好吗? [英] I'm using the SDL functions without the SDL_main be defined. Is that fine?

查看:1306
本文介绍了我使用SDL函数没有定义SDL_main。是好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

Lib.h

#ifdef ExportLib
    #define Lib __declspec(dllexport)
#else
    #define Lib __declspec(dllimport)
#endif
extern void Lib Launch();

Lib.cpp

#include <SDL/SDL.h>
#include "Lib.h"
void Launch() {
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Window* win = SDL_CreateWindow("Untitle", 100, 100, 400, 400, 0);
    SDL_DestroyWindow(win);
    SDL_Quit();
}

我将此代码构建到静态库。然后我创建了一个新的源文件并使用这个库。

I build this code to a static library. Then I created a new source file and used this library.

main.cpp

#include "Lib.h"

int main() {
    Launch();
    return 0;
}

最后,我使用静态库编译main.cpp而不定义SDL_main和SDL的依赖。工作正常,窗口出现。

Finally, I compile main.cpp using my static library without the SDL_main be defined and the SDL's dependecies. That works fine, the window appears.

但是真的很好吗?我失去了什么功能?

But is really fine do it? What functionalities I lost doing it?

推荐答案

SDL_main 是SDL的自动初始化和清理。大多数情况下你不需要手动做,它也会经过正确设置一个窗口化应用程序的平台上的编译工作的努力,但它是罚< a>到 #include 之前的 SDL_MAIN_HANDLED SDL.h,这将防止SDL将 main 转换为 SDL_main的宏只需确保初始化并退出SDL在你自己的代码里面。

SDL_main is for SDL's automatic initialization and cleanup. It's mostly so you don't need to do it manually, though it also goes through the effort of properly setting everything up for a windowed application on the platform where it's compiled, but it's fine to #define the macro SDL_MAIN_HANDLED before #includeing SDL.h, which will prevent SDL from turning main into a macro for SDL_main Simply make sure to initialize and quit SDL properly inside your own code.

如果您想确定自己正在进行必要的初始化,可以只需检查源代码并模拟其中的内容。

If you want to be sure you're doing the necessary initialization right, you can just check the source code and emulate what's there.

编辑:

在某些平台上, SDL_Init将失败,如果您不使用 SDL_main 。您可以通过在 SDL_Init 之前调用 SDL_SetMainReady 来禁用此失败,但是请注意,这将禁用SDL的错误处理,在调用 SDL_SetMainReady 后不正确地初始化SDL,您将无法获得最清晰的错误消息。

On some platforms, SDL_Init will fail if you don't use SDL_main. You can disable this failure by calling SDL_SetMainReady before SDL_Init, but be aware this will disable SDL's error handling, and if you improperly initialize SDL after calling SDL_SetMainReady you won't get the clearest of error messages.

退出SDL是更简单的(以及如果你不使用SDL_main也需要做):

Quitting SDL is much more straightforward (and also needs to be done if you're not using SDL_main):

只需调用 SDL_Quit 。这将正确关闭当前活动的任何SDL子系统。

Just call SDL_Quit when you're done with SDL. This will properly close any SDL subsystems presently active.

这篇关于我使用SDL函数没有定义SDL_main。是好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆