C++ 纹理创建正在返回NULL
SDL在尝试将曲面转换为纹理时出错。SDL_曲面是正确的,因为它不会像SDL_纹理那样返回NULL。有什么想法吗?我已经把m_pRenderer作为游戏类的私有变量和全局变量,只是为了测试它,两种方法都不起作用 该程序的想法是创建一个带有Background.png显示和添加点击按钮的界面环境,但在早期阶段,我只需要显示图像 Game header包含类Game{}和Game.cpp包含函数 Game.cpp:C++ 纹理创建正在返回NULL,c++,rendering,sdl-2,C++,Rendering,Sdl 2,SDL在尝试将曲面转换为纹理时出错。SDL_曲面是正确的,因为它不会像SDL_纹理那样返回NULL。有什么想法吗?我已经把m_pRenderer作为游戏类的私有变量和全局变量,只是为了测试它,两种方法都不起作用 该程序的想法是创建一个带有Background.png显示和添加点击按钮的界面环境,但在早期阶段,我只需要显示图像 Game header包含类Game{}和Game.cpp包含函数 Game.cpp: m_pRenderer从未被创造过 将此添加到文件Game.cpp的第16行之后 m
m_pRenderer从未被创造过 将此添加到文件Game.cpp的第16行之后
m_pRenderer=SDL_CreateRenderer(m_pWindow,-1,SDL_RENDERER_加速);
如果(m_pRenderer==nullptr){
printf(“SDL无法创建渲染器!SDL_图像错误:%s\n”,SDL_GetError());
返回false;
}
或
修正了Game.cpp文件
#包括“Game.h”
//初始化、渲染、更新、handleEvents、清理
bool Game::init(const char*title、int xpos、int ypos、int width、int height、int flags){
//尝试初始化SDL
if(SDL_Init(SDL_Init_VIDEO)==0){
std::在SDL_CreateTextureFromSurface失败后,SDL_GetError会返回什么?它解决了SDL_GetError()问题,但即使将NULL作为SDL_RenderCopy()的标志传递,它也不会显示图像[即SDL_RenderCopy(m_pRenderer,m_pTexture,NULL,NULL)]将SDL_RenderCopy放在render函数中,在RenderClear之后和RenderPresent之前。谢谢,我已经弄清楚了,甚至没有想到它可能是render()函数。
#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();
}
#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
#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;
}