SDL is getting me a error when try to convert Surface to Texture. SDL_Surface is correct because it wont return NULL as like the SDL_Texture. Any ideas of what it might be? I already put m_pRenderer as private variable from Game class and also as global variable ,just to test it, both ways didnt work.
The idea of the program is to create a interface environment with Background.png showing off and add clicking buttons , but in this early stages I only need to show the image.
Game header contains class Game{} and the Game.cpp contains the functions.
#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;
}
m_pRenderer was never created.
Add this after line 16 in file Game.cpp
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;
}
or
Fixed Game.cpp file
#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();
}
Tested With:
OS: Linux
Libs: SDL2, SDL2_image
IDE: Netbeans