c - 为什么SDL2在最后一个printf之前显示对话框消息?很有意思

标签 c graphics sdl-2

我不明白为什么对话框消息显示在“printf”语句之前。

如果我向“printf”添加“\n”,它的行为就像正常情况一样,但如果没有它,对话框最终会在“printf”之前显示。

这是测试程序:

/* Build: gcc -o test `sdl2-config --libs --cflags` test.c */

#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>

void MessageBox(void);
void clean(void);

int main(int argc, char* argv[]) {
    SDL_Init(SDL_INIT_EVERYTHING);
    atexit(clean);
    SDL_DisplayMode mode;
    int id = 0;
    SDL_GetCurrentDisplayMode(id, &mode);
    char smode[20];
    sprintf(smode, "%ix%i@%iHz", mode.h, mode.w, mode.refresh_rate);
    printf("%s", smode);

    MessageBox();

    SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
            "Missing file",
            "File is missing. Please reinstall the program.",
            NULL
            );
    return(0);
}

void MessageBox() {
    const SDL_MessageBoxButtonData buttons[] ={ /* .flags, .buttonid, .text */
        {0, 0, "no"},
        {SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "yes" },
        { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 2, "cancel" },
        };

    const SDL_MessageBoxColorScheme colorScheme = { /* .colors (.r, .g, .b) */
        {   /* [SDL_MESSAGEBOX_COLOR_BACKGROUND] */
            { 0, 0, 0 },
                /* [SDL_MESSAGEBOX_COLOR_TEXT] */
            { 0, 255, 0 },
                /* [SDL_MESSAGEBOX_COLOR_BUTTON_BORDER] */
            { 255, 255, 255 },
                /* [SDL_MESSAGEBOX_COLOR_BUTTON_BACKGROUND] */
            { 0, 0, 0 },
                /* [SDL_MESSAGEBOX_COLOR_BUTTON_SELECTED] */
            { 255, 0, 255 }
        }
    };
    const SDL_MessageBoxData messageboxdata = {
        SDL_MESSAGEBOX_INFORMATION, /* .flags */
        NULL,                       /* .window */
        "example message box",      /* .title */
        "select a button",          /* .message */
        SDL_arraysize(buttons),     /* .numbuttons */
        buttons,                    /* .buttons */
        &colorScheme                /* .colorScheme */
    };
    int buttonid;
    if(SDL_ShowMessageBox(&messageboxdata, &buttonid) < 0) {
        SDL_Log("error displaying message box");
    }
    if(buttonid == -1) {
        SDL_Log("no selection");
    } else {
        SDL_Log("selection was %s", buttons[buttonid].text);
    }
}

void clean() {
    SDL_Quit();
}

最佳答案

看似混合输出顺序的原因是因为在 C 中,stdout 是内部缓冲的。

只有几种方法可以将标准输出缓冲区实际刷新到终端。

  1. a fprintf( stdout,... 的格式字符串以 '\n' 结尾
  2. printf() 的类似格式字符串注意事项
  3. 通过 %s 输出的字符串项包含 \n
  4. 函数 fflush( stdout ) 被执行
  5. 程序尝试从 stdin 输入任何内容
  6. 程序退出
  7. 缓冲区已完全填满
  8. 在程序开始时调用 setbuf() 函数,并将 stdout 缓冲区设置为长度 0
  9. 调用 puts() 函数。
  10. 使用 char 参数“\n”调用 putc() 函数

关于c - 为什么SDL2在最后一个printf之前显示对话框消息?很有意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35182818/

相关文章:

创建没有警告的位图

c - 伽罗华域算法从C到Matlab

c++ - 是否在特定实现上定义了两个数组之间的指针差异?

reflection - 在面向服务的架构中绘制消息流图(使用 NServiceBus)

java - 范围内最大/最小值的数据结构

更改 Pipewire/Pulseaudio 中应用程序的名称

c++ - 在 C++/SDL2 中显示 .bmp 图像

c++ - SDL2 根本没有收到任何事件

c - 什么是高效稳定的外部排序算法实现(用c编写)?

java - 将 Gamma 校正应用于压缩整数像素