SDL错误:使用SDL_CreateTextureFromSurface时无效的渲染器

时间:2016-06-03 20:52:17

标签: c++ sdl-2

当我运行程序时,我会在控制台上写出来:

Unable to create texture from Textures/background.png! SDL Error: Invalid renderer

Failed to load background texture image!

Failed to load media!

所以这是我的完整程序,所有类都在不同的文件中:

//main.cpp
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>

class LTexture;
#include "LTexture.h"
class Engine;
#include "Engine.h"

int main( int argc, char* args[] )
{
    Engine start;
    return 0;
}

Engine.h文件:

#ifndef ENGINE_H
#define ENGINE_H

#include "LTexture.h"
#include "v.h"

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

class Engine: public v
{
    public:
        Engine();
        ~Engine();

        bool init();
        bool loadMedia();
        void close();

        //LTexture gFooTexture;
        LTexture gBackgroundTexture;
};

#endif // ENGINE_H

Engine.cpp文件:

#include "Engine.h"

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

Engine::Engine()
{
    if( !init() )
    {
        printf( "Failed to initialize!\n" );
    }
    else
    {
        if( !loadMedia() )
        {
            printf( "Failed to load media!\n" );
        }
        else
        {
            bool quit = false;

            SDL_Event e;

            while( !quit )
            {
                while( SDL_PollEvent( &e ) != 0 )
                {
                    if( e.type == SDL_QUIT )
                    {
                        quit = true;
                    }
                }

                SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
                SDL_RenderClear( gRenderer );

                gBackgroundTexture.render( 0, 0 );

                gFooTexture.render( 240, 190 );

                SDL_RenderPresent( gRenderer );
            }
        }
    }
}

Engine::~Engine()
{
    close();
}

bool Engine::init()
{
    bool success = true;
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
        success = false;
    }
    else
    {
        if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
        {
            printf( "Warning: Linear texture filtering not enabled!" );
        }
        gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
        if( gWindow == NULL )
        {
            printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
            success = false;
        }
        else
        {
            gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );
            if( gRenderer == NULL )
            {
                printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
                success = false;
            }
            else
            {
                SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
                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;
}

bool Engine::loadMedia()
{
    bool success = true;

    if( !gFooTexture.loadFromFile( "Textures/foo.png" ) )
    {
        printf( "Failed to load Foo' texture image!\n" );
        success = false;
    }

    if( !gBackgroundTexture.loadFromFile( "Textures/background.png" ) )
    {
        printf( "Failed to load background texture image!\n" );
        success = false;
    }

    return success;
}

void Engine::close()
{

    gFooTexture.freeup();
    gBackgroundTexture.freeup();

    SDL_DestroyRenderer( gRenderer );
    SDL_DestroyWindow( gWindow );
    gWindow = NULL;
    gRenderer = NULL;

    IMG_Quit();
    SDL_Quit();
}

LTexture.h文件:

#ifndef LTEXTURE_H
#define LTEXTURE_H

#include "v.h"

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

class LTexture: public v
{
public:
    LTexture();
    ~LTexture();

    bool loadFromFile( std::string path );
    void freeup();
    void render( int x, int y );

    int getWidth();
    int getHeight();

private:
    SDL_Texture* mTexture;

    int mWidth;
    int mHeight;
};

#endif // LTEXTURE_H

LTexture.cpp文件:

#include "LTexture.h"

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

LTexture::LTexture()
{
    mTexture = NULL;
    mWidth = 0;
    mHeight = 0;
}

LTexture::~LTexture()
{
    freeup();
}

bool LTexture::loadFromFile( std::string path )
{
    freeup();

    SDL_Texture* newTexture = NULL;

    SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
    if( loadedSurface == NULL )
    {
        printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() );
    }
    else
    {
        SDL_SetColorKey( loadedSurface, SDL_TRUE, SDL_MapRGB( loadedSurface->format, 0, 0xFF, 0xFF ) );

        newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface );
        if( newTexture == NULL )
        {
            printf( "Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
        }
        else
        {
            mWidth = loadedSurface->w;
            mHeight = loadedSurface->h;
        }
        SDL_FreeSurface( loadedSurface );
    }
    mTexture = newTexture;
    return mTexture != NULL;
}

void LTexture::freeup()
{
    if( mTexture != NULL )
    {
        SDL_DestroyTexture( mTexture );
        mTexture = NULL;
        mWidth = 0;
        mHeight = 0;
    }
}

void LTexture::render( int x, int y )
{
    SDL_Rect renderQuad = { x, y, mWidth, mHeight };
    SDL_RenderCopy( gRenderer, mTexture, NULL, &renderQuad );
}

int LTexture::getWidth()
{
    return mWidth;
}

int LTexture::getHeight()
{
    return mHeight;
}

v.h文件:

#ifndef V_H
#define V_H

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

class v
{
    public:
        v();
        ~v();
        const int SCREEN_WIDTH = 640;
        const int SCREEN_HEIGHT = 480;

        SDL_Window* gWindow = NULL;
        SDL_Renderer* gRenderer = NULL;
};

#endif // V_H

v.cpp文件:

#include "v.h"

v::v()
{
    //ctor
}

v::~v()
{
    //dtor
}

1 个答案:

答案 0 :(得分:0)

你的问题实际上只有一个单词 - “类”。您将渲染器存储在基类中,但从不初始化LTexture的{​​{1}}部分,因此纹理vgRenderer,这实际上是无效的渲染器。