Jump to content

Help with C++ error regarding SDL.

Failbound

Alright, I'm rather new in the whole topic of using SDL to create images and take inputs in.

I've been coding a simple program to just display a animated sprite loop, but I've come to an error "SDL Error: Invalid Renderer"

I've narrowed down the specific area of code that is causing the error, as shown

//If existing texture is there, free's and sets to NULL. Along with iWidth & iHeight = 0;freetexture();//final image textureSDL_Texture* niTexture = NULL;//Loads image at specified pathSDL_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, 0xFFSDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 50, 96, 166));//create texture from surface pixelsniTexture = 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 the line

niTexture = SDL_CreateTextureFromSurface(mkey.Renderer, loadedSurface);

is causing the Invalid Renderer error.

Here is the renderer function

//Create WindowWindow = 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 windowRenderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);if (Renderer == NULL){printf("Renderer failed to be created\n");SDLSuccess = false;}

Any suggestion on how to proceed and fix this problem?

Link to comment
Share on other sites

Link to post
Share on other sites

Sorry I'm an SFML guy, I don't think there is anyone here who uses SDL you are probably better off asking on stackoverflow or the sdl website.

Link to comment
Share on other sites

Link to post
Share on other sites

I have made a game in SDL but I used OpenGL for graphics, I assume Renderer is SDL's 2d graphics lib. I remember it was so slow it cannot handle screen full of squares, I was doing some fiddling with Box2D lib back there. My guess is you should look really carefully at how you initializing things, and how it should be done, and scope of the variable.

 

To debug this, I would check what Renderer variable contains right after you initialize it first, and what it looks like when you try use it in function.

 

Edit: I just made a working example using Dev that loads png texture and draws it on window, so if you would show your "Init" class it would help to find the problem.

 

I can give you my working example with all libraries (full Dev's project)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×