SDL Error: Invalid Renderer on SDL_CreateTextureFromSurface - c++

Alrighty, from what I have researched, it appears that the Invalid Renderer error applies to a variety of cases and I'm lost onto why my code is creating it.
I have narrowed it down to a specific area of code
//If existing texture is there, free's and sets to NULL. Along with iWidth & iHeight = 0;
freetexture();
//final image texture
SDL_Texture* niTexture = NULL;
//Loads image at specified path
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
{
printf("SpriteSheet :: Loaded\n");
Init mkey;
//Color key DOUBLE CHECK IF ERROR CHANGE TO ORIGINAL 0, 0xFF, 0xFF
SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 50, 96, 166));
//create texture from surface pixels
niTexture = SDL_CreateTextureFromSurface(mkey.Renderer, loadedSurface);
if (niTexture == NULL)
{
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
Specifically at the line,
niTexture = SDL_CreateTextureFromSurface(mkey.Renderer, loadedSurface);
is causing SDL to return an Invalid renderer error. In my init class, the renderer initializes perfectly, only when I attempt to use it to load an image am I getting the Invalid Renderer error. Any help on how to fix this error is appreciated.
Edit::
Here's some code from the Init Class relating to the renderer,
printf("Linear Texture Filtering :: Enabled\n");
//Create Window
Window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, sw, sh, SDL_WINDOW_SHOWN);
if (Window == NULL)
{
printf("Window failed to be created\n");
SDLSuccess = false;
}
else
{
printf("Window :: Created\n");
//Create VYNC'd renderer for the window
Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (Renderer == NULL)
{
printf("Renderer failed to be created\n");
SDLSuccess = false;
}
Hope this helps with finding the issue.

It looks like your Renderer isn't initialized, unless the code you posted is in the constructor of your Init class.
Do you already have an instance of Init somewhere in your code that you mean to be referencing in your texture method? Check the value of your Renderer before you try to use it, something like:
if (mkey.Renderer) {
niTexture = SDL_CreateTextureFromSurface(mkey.Renderer, loadedSurface);
if (niTexture == NULL)
{
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
} else {
printf("Renderer is not initialized");
}

Related

SDL2 opens no window without giving any errors

I'm trying to test a simple SDL2 application. The app runs without any errors. However, no window shows up.
I'm using Ubuntu18.04 on a VMWare machine. I also tried to compile SDL from the source but got the same result. I am unsure if the cause of the problem is related to my OS or because of executing from a virtual machine.
Here is a simple application from docs (I just added SDL_GetNumDisplayModes to make sure the display mode is OK- returns 1)
// Using SDL and standard IO
#include <SDL2/SDL.h>
#include <stdio.h>
// Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main(int argc, char *args[])
{
// The window we'll be rendering to
SDL_Window *window = NULL;
// The surface contained by the window
SDL_Surface *screenSurface = NULL;
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
}
else
{
int res = SDL_GetNumDisplayModes(0);
printf("SDL_GetNumDisplayModes: %d\r\n", res);
// Create window
window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL)
{
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
}
else
{
// Get window surface
screenSurface = SDL_GetWindowSurface(window);
// Fill the surface white
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF));
// Update the surface
SDL_UpdateWindowSurface(window);
// Hack to get window to stay up
SDL_Event e;
bool quit = false;
while (quit == false)
{
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
{
quit = true;
}
}
}
}
}
// Destroy window
SDL_DestroyWindow(window);
// Quit SDL subsystems
SDL_Quit();
return 0;
}

Calling SDL "IMG_Load" a second time results in a "EXC_BAD_ACCESS (code=EXC_I386_GPFLT)"

Im running this on MacOS 10.12 using Xcode 8.3.3 with SDL2 installed via Homebrew as Dylibs.
Below is some slightly modified sample code from lazy foo.
I just added a second texture gTexture2 and the function loadMedia2 to be able to reproduce the issue. The second time IMG_Load is executed it crashes with the following message:
EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
Searching on how to solve a "General Protection Fault" problem did also not get me further, the crash seems to happen inside SDL. I probably really misunderstand here something that leads to this issue and would really welcome any help.
The really confusing thing is, it does not crash always, only about 2 of 3 times.
The crash seem to happen inside SDL_AllocFormat_REAL ():
Here is the code sample.
/*This source code copyrighted by Lazy Foo' Productions (2004-2015)
and may not be redistributed without written permission.*/
//Using SDL, SDL_image, standard IO, and strings
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
//Loads individual image as texture
SDL_Texture* loadTexture( std::string path );
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The window renderer
SDL_Renderer* gRenderer = NULL;
//Current displayed texture
SDL_Texture* gTexture = NULL;
SDL_Texture* gTexture2 = NULL;
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
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
{
//Create renderer for window
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
{
//Initialize renderer color
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
//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;
}
bool loadMedia()
{
//Loading success flag
bool success = true;
//Load PNG texture
gTexture = loadTexture( "../assets/player.png" );
if( gTexture == NULL )
{
printf( "Failed to load texture image!\n" );
success = false;
}
return success;
}
bool loadMedia2()
{
//Loading success flag
bool success = true;
//Load PNG texture
gTexture2 = loadTexture( "../assets/scene_main/background.png" );
if( gTexture == NULL )
{
printf( "Failed to load texture image!\n" );
success = false;
}
return success;
}
void close()
{
//Free loaded image
SDL_DestroyTexture( gTexture );
SDL_DestroyTexture( gTexture2 );
gTexture = NULL;
gTexture2 = NULL;
//Destroy window
SDL_DestroyRenderer( gRenderer );
SDL_DestroyWindow( gWindow );
gWindow = NULL;
gRenderer = NULL;
//Quit SDL subsystems
IMG_Quit();
SDL_Quit();
}
SDL_Texture* loadTexture( std::string path )
{
//The final texture
SDL_Texture* newTexture = NULL;
//Load image at specified path
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
{
//Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface );
if( newTexture == NULL )
{
printf( "Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
}
//Get rid of old loaded surface
SDL_FreeSurface( loadedSurface );
}
return newTexture;
}
int main( int argc, char* args[] )
{
//Start up SDL and create window
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
//Load media
if( !loadMedia() || !loadMedia2() )
{
printf( "Failed to load media!\n" );
}
else
{
//Main loop flag
bool quit = false;
//Event handler
SDL_Event e;
//While application is running
while( !quit )
{
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
}
//Clear screen
SDL_RenderClear( gRenderer );
//Render texture to screen
SDL_RenderCopy( gRenderer, gTexture, NULL, NULL );
//Update screen
SDL_RenderPresent( gRenderer );
}
}
}
//Free resources and close SDL
close();
return 0;
}
Little Update:
I've tried it on windows, there it runs completely fine. So I guess the issue is related to MacOs.
I already tried to reinstall all libraries.
I'm using C++14.
The solution
Well its just half of a solution its more a workaround.
Thanks to #Sahib Yar he pointed out to try to put the images in the same directory. Which resolves the issue.
But I think this is really weird, you should be able to load resources from different directories or at least subdirectory.
The final question
Now I would really love an explanation why we can't load images from multiple directories using SDL on MacOS. Is that just a bug, known thing or did I make a big mistake?
It seems that you are not destroying texture2 that is not needed.
SDL_DestroyTexture( gTexture );
SDL_DestroyTexture( gTexture2 );
gTexture = NULL;
gTexture2 = NULL;
In this lazyfoo tutorial, it is mentioned that
In our clean up function, we have to remember to deallocate our
textures using SDL_DestroyTexture.
Edit 1:
Try to put all your images in the same directory.
Edit 2:
It is not related to directory in MacOS From this tutorial, it seems like compiler is doing some optimization with std::string path as the std::string is mutable.
Try to clear the std::string path object at end of function to clear up all the memory reserved by its objects.
add this line.
std::string().swap(path);
Your issue is a dangling pointer. EXC_BAD_ACCESS is the CPU moaning that you are addressing non-existent memory or memory which is outside of your access rights area. The cause is a lack of reatainment of an object which causes early deallocation and then is overwritten. At which time (which may be delayed), the pointer will point to garbage whose dereference causes an EXC_BAD_ACCESS to be thrown.
Edit 3:
It is not something related to SDL2. After Googling, I have found that in Xcode, everything is eventually packed into 1 single directory. I have found multiple questions regarding this. It may be something related to folder reference and groups. To my guess it could be something related to blue folders. If this is the case you can consult this answer and use accordingly for SDL.

'SDL_SetVideoMode': identifier not found

I have a problem with SDL lib. I'm using VS2012 Ultimate and i was actually using this tutorial: http://lazyfoo.net/tutorials/SDL/01_hello_SDL/index2.php to set everything and i did it step by step few times, but I still have problems this is my code, very simple:
#include <iostream>
#include <SDL.h>
SDL_Surface * ekran = NULL;
int main (int argc, char *args [] )
{
SDL_Init( SDL_INIT_EVERYTHING );
ekran = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
SDL_Flip( ekran );
SDL_Delay( 2000 );
SDL_Quit();
return 0;
}
and im having this errors:
error C3861: 'SDL_SetVideoMode': identifier not found
error C3861: 'SDL_Flip': identifier not found
Here below is an example how to replace SDL_SetVideoMode() in SDL2. The old way to init SDL is commented and left along with the new way for comparison purposes. Basically, SDL2 creates a window with a title, then a surface attached to it, while SDL1 creates a surface alone and then calls the window manager to give it a name.
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "SDL video init failed: %s\n", SDL_GetError());
return 1;
}
// SDL_Surface *screenSurface = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_SWSURFACE);
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
window = SDL_CreateWindow("Sphere Rendering",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL) {
fprintf(stderr, "Window could not be created: %s\n", SDL_GetError());
return 1;
}
screenSurface = SDL_GetWindowSurface(window);
if (!screenSurface) {
fprintf(stderr, "Screen surface could not be created: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
// SDL_WM_SetCaption("Sphere Rendering", NULL);
Take a look at that tutorial page again. Your code does not match it (e.g. SDL_SetVideoMode() no longer exists). Your code uses SDL 1.2 and the (updated) tutorial uses SDL 2.0. Are you using an old cached version of that page?

SDL load image from file

I'm trying to learn SDL 2.0 and I've been following lazyfoo tutorials. The problem is that those tutorials keeps everything in one file. So I tried to split some things up when starting a new game-project. My problem is that when I seperated my texture class from the rest it sometimes crashes the program, not always but pretty often.
I have a global window and a global renderer which I'm using in my class. I get segfault at difference places in the program but always in one of the following functions.
bool myTexture::loadFromFile(string path) {
printf("In loadFromFile\n");
//Free preexisting texture
free();
//The final texture
SDL_Texture *l_newTexture = NULL;
//Load image at specified path
SDL_Surface *l_loadedSurface = IMG_Load(path.c_str());
if(l_loadedSurface == NULL) {
printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
} else {
//Color key image for transparancy
//SDL_SetColorKey(l_loadedSurface, SDL_TRUE, SDL_MapRGB(l_loadedSurface->format, 0, 0xFF, 0xFF));
//Create texture from surface pixels
l_newTexture = SDL_CreateTextureFromSurface(g_renderer, l_loadedSurface);
if(l_newTexture == NULL) {
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
} else {
//Get image dimensions
m_width = l_loadedSurface->w;
m_height = l_loadedSurface->h;
}
//Get rid of old loaded surface
SDL_FreeSurface(l_loadedSurface);
}
m_texture = l_newTexture;
//return success
printf("end from file \n");
return m_texture != NULL;
}
void myTexture::free() {
printf("In myTexture::free\n");
//Free texture if it exist
if(m_texture != NULL) {
cout << (m_texture != NULL) << endl;
SDL_DestroyTexture(m_texture);
printf("Destroyed m_texture\n");
m_texture = NULL;
m_width = 0;
m_height = 0;
}
printf("end free\n");
}
After reading on SDL and other stuffs I understood that there might be some thread trying to deallocate something which it isn't allowed to deallocate. However I haven't threaded anything yet.
I managed to solve this by my own. It turned out that I never created a new myTexture object which was really idiotic. But I still don't understand how it managed to render it sometimes... for me it don't make any sense at all. I never created it but I could still call its render function sometimes...

Why won't my image render?

So I'm learning C++ with SDL (on Visual Studio 2013). I'm following Lazy Foo's tutorials (specifically: http://lazyfoo.net/tutorials/SDL/02_getting_an_image_on_the_screen/index.php).
My image won't render. In contrast to the tutorial (which works) I do not have global surface variables. Instead I declared them in the main and passed the pointers around.
Code:
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
const int SCREEN_WIDTH = 600;
const int SCREEN_HEIGHT = 480;
//SDL init.
bool initialiseSDL(SDL_Window* gWindow, SDL_Surface* gWindowSurface)
{
bool success = true;
/*SDL_Init() initisalises SDL library. Returning 0 or less signifies an error.
SDL_INIT_VIDEO flag refers to graphics sub system.*/
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialise. SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Create the window
gWindow = SDL_CreateWindow("Asteroids", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (gWindow == NULL)
{
printf("Could not create window. SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Get the window surface.
gWindowSurface = SDL_GetWindowSurface(gWindow);
}
}
return success;
}
bool loadMedia(SDL_Surface* surface, std::string path)
{
//Success flag
bool success = true;
surface = SDL_LoadBMP(path.c_str());
if (surface == NULL)
{
printf("SDL surface failed to load. SDL Error: %s\n", SDL_GetError());
success = false;
}
return success;
}
void close()
{
}
int main(int argc, char* argv[])
{
SDL_Window* gWindow = NULL;
SDL_Surface* gWindowSurface = NULL;
SDL_Surface* gImageSurface = NULL;
if (!initialiseSDL(gWindow, gWindowSurface))
{
printf("Failed to initialise.\n");
}
else
{
if (!loadMedia(gImageSurface, "hw.bmp"))
{
printf("Failed to load inital media.");
}
else
{
//Apply the image
SDL_BlitSurface(gImageSurface, NULL, gWindowSurface, NULL);
//Update the surface
SDL_UpdateWindowSurface(gWindow);
//Wait two seconds
SDL_Delay(2000);
}
}
return 0;
}
Your program exhibits undefined behavior, because the pointers inside the main function are not initialized.
When you pass them to the initialiseSDL function you pass them by value, meaning they are copied and the function only operates on the copies and not the original pointers, which will still be uninitialized after the call.
What you need to do is to pass the pointers by reference instead:
bool initialiseSDL(SDL_Window*& gWindow, SDL_Surface*& gWindowSurface)
You of course need to do the same with the loadMedia function.