[已解决:纹理创建返回NULL

问题描述 投票:-1回答:1

当尝试将Surface转换为Texture时,SDL让我出错。 SDL_Surface是正确的,因为它不会像SDL_Texture一样返回NULL。有什么想法吗?我已经把m_pRenderer作为Game类的私有变量,也作为全局变量,只是为了对其进行测试,两种方法均无效。

程序的想法是创建一个显示Background.png的界面环境,并添加单击按钮,但是在此早期阶段,我只需要显示图像。

游戏头包含class Game {},而Game.cpp包含函数。

Game.cpp:

#include"Game.h"


//init,render,update,handleEvents,clean


bool Game::init(const char* title,int xpos,int ypos,int width,int height,int flags){

    //attempt to initialize SDL
    if(SDL_Init(SDL_INIT_VIDEO)==0){
    std::cout<<"SDL init success\n";
    //init window
    m_pWindow = SDL_CreateWindow(title,xpos,ypos,width,height,flags);

    if(m_pWindow!=0) { //window init success
        std::cout<<"Window creation success\n";

        int flags=IMG_INIT_JPG|IMG_INIT_PNG;
        int initted=IMG_Init(flags);

        if((initted&flags) != flags){

            printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );

            return false;
            //renderer init fail
        }

        else{
            //IMG_INIT was a success
            SDL_Surface *m_pSurface;
            m_pSurface = IMG_Load("background.png");

            if(m_pSurface == NULL){
                printf("Error while trying creating surface! SDL_image Error: %s\n",IMG_GetError());
            }

            m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer,m_pSurface);

            if(m_pTexture == NULL){
                printf("Error while trying creating texture! SDL_image Error: %s\n",IMG_GetError());
            }


            else{
                //success creating the texture
                SDL_FreeSurface(m_pSurface);
                SDL_QueryTexture(m_pTexture, NULL, NULL,&m_sourceRectangle.w, &m_sourceRectangle.h);

                m_destinationRectangle.x=m_sourceRectangle.x = 0;
                m_destinationRectangle.y=m_sourceRectangle.y = 0;
                m_destinationRectangle.w=m_sourceRectangle.w;
                m_destinationRectangle.h=m_sourceRectangle.h;

                SDL_RenderCopy(m_pRenderer, m_pTexture, &m_sourceRectangle,&m_destinationRectangle);;
                SDL_RenderPresent(m_pRenderer);

            }
        }



    }
    else{
        std::cout<<"Window creation failed\n";
        return false;
         //window init fail
    }

    }

    else{
    std::cout<<"SDL init fail\n";
    return false;
     //SDL init fail
    }

std::cout<<"Initialization was a success\n";
return true; //everything initialized successfully, start the main loop

}



void Game::render(){

SDL_RenderClear(m_pRenderer); //clear the renderer to draw color
SDL_RenderPresent(m_pRenderer); //draw to the screen

}




void Game::handleEvents(){

SDL_Event event;

    if(SDL_PollEvent(&event)){
        switch(event.type){
            case SDL_QUIT: m_bRunning =false; break;
            default: break;
        }
    }
}



void Game::clean(){

std::cout<<"Cleaning game\n";
SDL_DestroyTexture(m_pTexture);
IMG_Quit();
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}

Game.h:

#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

#include<SDL2/SDL.h>
#include<SDL2/SDL_image.h>
#include<iostream>


class Game{
public:
Game(){};
~Game(){};

//simply set the boolean value to true
bool init(const char*,int,int,int,int,int);

void render();
void update(){};
void handleEvents();
void clean();

bool running(){return m_bRunning;}

private:

SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;
SDL_Texture* m_pTexture;
SDL_Rect m_sourceRectangle,m_destinationRectangle;

bool m_bRunning=true;
};

#endif // GAME_H_INCLUDED

main.cpp:

#include"Game.h"

//our Game object
Game* g_game=0;

int main(int argc, char* argv[]){
g_game = new Game();
g_game->init("Hello SDL",100,100,800,600,SDL_WINDOW_SHOWN);

    while(g_game->running()){
    g_game->handleEvents();
    g_game->update();
    g_game->render();
    }

g_game->clean();

return 0;

}
c++ rendering sdl-2
1个回答
0
投票

从未创建过m_pRenderer。

将其添加到Game.cpp文件的第16行之后

m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, SDL_RENDERER_ACCELERATED);

        if(m_pRenderer == nullptr){
            printf( "SDL could not create Renderer! SDL_image Error: %s\n", SDL_GetError() );

            return false;
        }

固定的Game.cpp文件

#include "Game.h"


//init,render,update,handleEvents,clean


bool Game::init(const char* title,int xpos,int ypos,int width,int height,int flags){

    //attempt to initialize SDL
    if(SDL_Init(SDL_INIT_VIDEO)==0){
    std::cout<<"SDL init success\n";
    //init window
    m_pWindow = SDL_CreateWindow(title,xpos,ypos,width,height,flags);

    if(m_pWindow!=0) { //window init success
        std::cout<<"Window creation success\n";

        m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, SDL_RENDERER_ACCELERATED);

        if(m_pRenderer == nullptr){
            printf( "SDL could not create Renderer! SDL_image Error: %s\n", SDL_GetError() );

            return false;
        }

        int flags=IMG_INIT_JPG|IMG_INIT_PNG;
        int initted=IMG_Init(flags);

        if((initted&flags) != flags){

            printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );

            return false;
            //renderer init fail
        }

        else{
            //IMG_INIT was a success
            SDL_Surface *m_pSurface;
            m_pSurface = IMG_Load("background.png");

            if(m_pSurface == NULL){
                printf("Error while trying creating surface! SDL_image Error: %s\n",IMG_GetError());
            }

            m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer,m_pSurface);

            if(m_pTexture == NULL){
                printf("Error while trying creating texture! SDL_image Error: %s\n",IMG_GetError());
            }


            else{
                //success creating the texture
                SDL_FreeSurface(m_pSurface);
                SDL_QueryTexture(m_pTexture, NULL, NULL,&m_sourceRectangle.w, &m_sourceRectangle.h);

                m_destinationRectangle.x=m_sourceRectangle.x = 0;
                m_destinationRectangle.y=m_sourceRectangle.y = 0;
                m_destinationRectangle.w=m_sourceRectangle.w;
                m_destinationRectangle.h=m_sourceRectangle.h;

                SDL_RenderCopy(m_pRenderer, m_pTexture, &m_sourceRectangle,&m_destinationRectangle);;
                SDL_RenderPresent(m_pRenderer);

            }
        }



    }
    else{
        std::cout<<"Window creation failed\n";
        return false;
         //window init fail
    }

    }

    else{
    std::cout<<"SDL init fail\n";
    return false;
     //SDL init fail
    }

std::cout<<"Initialization was a success\n";
return true; //everything initialized successfully, start the main loop

}



void Game::render(){

SDL_RenderClear(m_pRenderer); //clear the renderer to draw color
SDL_RenderPresent(m_pRenderer); //draw to the screen

}




void Game::handleEvents(){

SDL_Event event;

    if(SDL_PollEvent(&event)){
        switch(event.type){
            case SDL_QUIT: m_bRunning =false; break;
            default: break;
        }
    }
}



void Game::clean(){

std::cout<<"Cleaning game\n";
SDL_DestroyTexture(m_pTexture);
IMG_Quit();
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}

经过测试:操作系统:Linux库:SDL2,SDL2_imageIDE:Netbeans

© www.soinside.com 2019 - 2024. All rights reserved.