C ++ / SDL2:无法加载图像

时间:2018-04-11 13:50:08

标签: c++ sdl-2

我开始关注Foo's SDL tutorial,Lazy提供的代码在我的机器上工作正常(代码块17.2,sdl2)。但我厌倦了将示例函数移动到一个单独的文件中:(我还在文件头中声明了静态变量。

结果:

主要代码:

#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include "commonFunc.h"

int main(int argc, char* args[])
{
    if(!Init())
    {
        printf("can't initialize \n");

    }
    else
    {

        if( !loadMedia() )
        {
            printf("can't load backgroud");
        }
        else
        {

            SDL_RenderClear( g_renderer );


            SDL_RenderCopy( g_renderer, g_texture, NULL, NULL );


            SDL_RenderPresent( g_renderer );

            SDL_Delay(5000);
        }
    }
    Close();
    return 0;
}

commonFunc.h:

#ifndef COMMONFUNC_H_
#define COMMONFUNC_H_

#include <string>
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>

const int SCREEN_WIDTH = 1000;
const int SCREEN_HEIGHT = 399;


static SDL_Window *g_window = NULL;
static SDL_Renderer *g_renderer = NULL;
static SDL_Texture *g_texture = NULL;

bool Init();
SDL_Texture* loadTexture(std::string file_path);
bool loadMedia();
void Close();


#endif // COMMONFUNC_H_

这里是commonFunc.cpp:

#include "commonFunc.h"
#include <cstddef>
#include <string>
#include <SDL.h>
#include <SDL_image.h>


bool Init()
{
    //Initialization flag
    bool success = true;

    //Initialize SDL
    if( SDL_Init( SDL_INIT_VIDEO) < 0 )
    {
        printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
        success = false;
    }
    else
    {
        //Set texture filtering to linear
        if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
        {
            printf( "Warning: Linear texture filtering not enabled!" );
        }

        //Create window
        g_window = SDL_CreateWindow( "Tictactoe", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
        if( g_window == NULL )
        {
            printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
            success = false;
        }
        else
        {
            //Create renderer for window
            g_renderer = SDL_CreateRenderer( g_window, -1, SDL_RENDERER_ACCELERATED );
            if( g_renderer == NULL )
            {
                printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
                success = false;
            }
            else
            {
                //Initialize renderer color
                SDL_SetRenderDrawColor( g_renderer, 0xFF, 0xFF, 0xFF, 0xFF );
                printf("set render color \n");
                //Initialize PNG loading
                int imgFlags = IMG_INIT_PNG;
                if( !( IMG_Init( imgFlags ) & imgFlags ) )
                {
                    printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
                    success = false;
                }
            }
        }
    }
    return success;
}
SDL_Texture* loadTexture(std::string file_path)
{
    //The final texture
    SDL_Texture* newTexture = NULL;
    printf("crater newtexture \n");
    //Load image at specified path
    SDL_Surface* loadedSurface = IMG_Load( file_path.c_str() );
    printf("load img \n");
    if( loadedSurface == NULL )
    {
        printf( "Unable to load image %s! SDL_image Error: %s\n", file_path.c_str(), IMG_GetError() );
    }
    else
    {
        //Create texture from surface pixels
        newTexture = SDL_CreateTextureFromSurface( g_renderer, loadedSurface );
        printf("create from surface \n");
        if( newTexture == NULL )
        {
            printf( "Unable to create texture from %s! SDL Error: %s\n", file_path.c_str(), SDL_GetError() );
        }
        SDL_FreeSurface( loadedSurface );
    }

    return newTexture;
}

bool loadMedia()
{
    //Loading success flag
    bool success = true;

    //Load PNG texture
    g_texture = loadTexture("main_bg.png");
    if( g_texture == NULL )
    {
        printf( "Failed to load texture image!\n" );
        success = false;
    }
    printf("load img xong! \n");
    return success;
}

void Close()
{
    //Free loaded image
    SDL_DestroyTexture( g_texture );
    g_texture = NULL;

    //Destroy window
    SDL_DestroyRenderer( g_renderer );
    SDL_DestroyWindow( g_window );
    g_window = NULL;
    g_renderer = NULL;

    //Quit SDL subsystems
    IMG_Quit();
    SDL_Quit();
    printf("close anything ...\n");
}

我的程序由3个文件组成。这里发生了什么? https://i.stack.imgur.com/u92vq.png

0 个答案:

没有答案