除了使用终端时,可执行文件不在Windows中运行

时间:2015-04-23 11:00:44

标签: windows mingw sdl

我正在使用SDL_TFF库并进行编译,但可执行文件不会直接运行,除非我从终端运行它。如果我注释掉SDL_TFF代码,它会正常运行。

有谁知道可能导致此问题的原因?

编辑:

#include <stdio.h>
#include <SDL.h>
#include <SDL_ttf.h> //version 2.0.12

TTF_Font*           Font;
SDL_DisplayMode     Desktop_Display_Mode;
SDL_Window*         Window;
SDL_Surface*        Window_surface;
SDL_Renderer*       Renderer;

bool load_font()
{
    //Font = TTF_OpenFont("fonts\\NotoSans-Bold.ttf", 16);
    Font = TTF_OpenFont("fonts\\NotoSansCJKjp-Bold.otf", 18);
    if (Font == NULL)
    {
        SDL_LogCritical(
            SDL_LOG_CATEGORY_APPLICATION,
            "Count not load font! TTF_Error: %s\n", TTF_GetError()
        );
        return false;
    }
    return true;
}

bool initialize()
{

    // Initialize SDL Library
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        SDL_LogCritical(
            SDL_LOG_CATEGORY_APPLICATION,
            "SDL could not initialize! SDL_Error: %s\n", SDL_GetError()
        );
        return false;
    }

    // Initialize SDL_ttf library
    if (TTF_Init() != 0)
    {   
        SDL_LogCritical(
            SDL_LOG_CATEGORY_APPLICATION,
            "SDL_ttf could not initialize! TTF_Error: %s\n", TTF_GetError()
        );
        return false;
    }

    // Load Font ---------------------------------------------------------------
    if ( !load_font() )
    {
        return false;
    }

    // Get Desktop Display Mode
    if (SDL_GetDesktopDisplayMode(0, &Desktop_Display_Mode) != 0)
    {
        SDL_LogCritical(
            SDL_LOG_CATEGORY_APPLICATION,
            "SDL could not get Desktop Display Mode! SDL_Error: %s\n",
            SDL_GetError()
        );
        return false;
    }

    // Create Window
    Window = SDL_CreateWindow( 
        "SDL Test",
        SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
        Desktop_Display_Mode.w, Desktop_Display_Mode.h,
        SDL_WINDOW_BORDERLESS //Flags
        );

    if( Window == NULL )
    {
        SDL_LogCritical(
            SDL_LOG_CATEGORY_APPLICATION,
            "Window could not be created! SDL_Error: %s\n",
            SDL_GetError()
        );
        return false;
    }

    Renderer = SDL_CreateRenderer( Window, -1,
        SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
    );

    if ( Renderer == NULL )
    {
        SDL_LogCritical(
            SDL_LOG_CATEGORY_APPLICATION,
            "Renderer could not be created! SDL_Error: %s\n",
            SDL_GetError()
        );
        return false;
    }

    return true;

}

bool clear_screen()
{
    if ( SDL_SetRenderDrawColor( Renderer, 0x00, 0x00, 0x00, 0xFF ) != 0 )
    {
        SDL_LogCritical(
            SDL_LOG_CATEGORY_APPLICATION,
            "Could not set render draw color! SDL_Error: %s\n",
            SDL_GetError()
        );
        return false;
    }
    if ( SDL_RenderClear( Renderer ) != 0 )
    {
        SDL_LogCritical(
            SDL_LOG_CATEGORY_APPLICATION,
            "Could not clear the renderer! SDL_Error: %s\n",
            SDL_GetError()
        );
        return false;
    }
    return true;
}

int main( int argc, char* args[] )
{
    SDL_Log("Started.");

    bool running = initialize();


    SDL_Color text_color = {255, 255, 255};

    const char* text_string;
    text_string = "A journey of a thousand miles begins with a single step.\n こんにちは";

    SDL_Rect text_dest;
    text_dest.x = 100;
    text_dest.y = 100;
    text_dest.w = 0;
    text_dest.h = 0;

    if ( TTF_SizeUTF8(Font, text_string, &text_dest.w, &text_dest.h) != 0)
    {
        SDL_LogCritical(
            SDL_LOG_CATEGORY_APPLICATION,
            "Unable to get text size! TTF_Error: %s\n", TTF_GetError()
        );
        running = false;
    }

    SDL_Surface* text_surface = NULL;
    text_surface = TTF_RenderUTF8_Solid(
        Font,
        text_string,
        text_color
    );

    if ( text_surface == NULL )
    {
        SDL_LogCritical(
            SDL_LOG_CATEGORY_APPLICATION,
            "Unable to render text! SDL_Error: %s\n",
            TTF_GetError()
        );
        running = false;
    }

    SDL_Texture* text_texture = NULL;
    text_texture = SDL_CreateTextureFromSurface( Renderer, text_surface );
    if ( text_texture == NULL )
    {
        SDL_LogCritical(
            SDL_LOG_CATEGORY_APPLICATION,
            "Unable to render text! SDL_Error: %s\n",
            TTF_GetError()
        );
        running = false;
    }

    // MAIN LOOP ===============================================================
    SDL_Event event;
    while (running)
    {

        // Clear the screen
        if ( !clear_screen() )
        {
            break;
        }

        // Check for events
        if (SDL_PollEvent( &event))
        {
            // Check for the quit event
            if (event.type == SDL_QUIT)
            {
                SDL_Log("Quit.");
                break;
            }
        }

        // Apply the text
        if ( SDL_RenderCopy( Renderer, text_texture, NULL, &text_dest ) != 0 )
        {
            SDL_LogCritical(
                SDL_LOG_CATEGORY_APPLICATION,
                "Unable to draw text! SDL_Error: %s\n",
                SDL_GetError()
            );
            break;
        }
        //Update Window
        SDL_RenderPresent( Renderer );

    }

    //Destroy Window
    SDL_DestroyWindow( Window );
    SDL_DestroyRenderer( Renderer);

    //Quit SDL subsystems
    TTF_Quit();
    SDL_Quit();

    SDL_Log("Ended.");

    return 0;
}

Edit2:我测试的更加彻底,看起来函数TTF_OpenFont()导致问题。只要我不调用该函数,exe就会正常运行。

1 个答案:

答案 0 :(得分:0)

解决!

这是我从命令行执行程序的方式:“\ bin \ myprog.exe”

因此相对文件路径不正确,因为“\ bin \ fonts ...”不存在。由于我没有实现SDL_LogSetOutputFunction来将日志写入文件并且std out显然被抑制,我看不出我必须收到错误消息,表明文件没有被加载。