SDL_ConvertSurface() causes break - c++

I have the following piece of code where among with lot of other stuff (which i didn't include in this topic), i'm trying to start up sdl, create a render and load some sprites.
Everything compiles just fine but when i run my application a break is caused saying: Unhandled exception at 0x681252D5 (SDL.dll) in Carribean World SDL.exe: 0xC0000005: Access violation reading location 0x16161804
The break occurs and the point where i use the SDL_ConvertSurface() function
Can anyone help me out, i can't see what's wrong
Declerations:
SDL_Texture* background = NULL;
SDL_Surface* tmp = NULL;
SDL_Surface* surface = NULL;
SDL_Window *window = SDL_CreateWindow("Carribean World",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
1360, 768,
SDL_WINDOW_RESIZABLE);
SDL_Surface* screen = SDL_GetWindowSurface(window);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
SDL_PixelFormat* fmt = screen->format;
IN MAIN:
Initialize all SDL subsystems
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return 0;
}
Load images to surfaces
if ((tmp = IMG_Load("images/water.jpg")) == NULL)
{
cout << "SDL_SetVideoMode() Failed: " << SDL_GetError() << endl;
return 0;
}
Right here a break is caused
if ((surface = SDL_ConvertSurface(tmp, fmt, 0)) == NULL)
{
cout << "SDL_ConvertSurface() Failed: " << SDL_GetError() << endl;
}
background = SDL_CreateTextureFromSurface(renderer, tmp);

You haven't checked return value of SDL_GetWindowSurface. But anyway, SDL documentation for this function says 'You may not combine this with 3D or the rendering API on this window.'. So either you exclusively use SDL_Renderer API, or using SDL_BlitSurface and alike and after that calling SDL_UpdateWindowSurface, but you can't use both.

Related

Can't open a window with SDL/C++

i stuck in a big bug with my code which i can't open the first window of my game after running it! it just open blank for a few secondes and then it closes automatically. I doon't know what wrong with my code beacause i'm following series of tutorials and my code seems like the code in the tutorials. here's my code:
#include "stdafx.h"
#include "FirstClass.h"
FirstClass::FirstClass(void){
bool quit = false;
window = NULL;
window = SDL_CreateWindow("Snaykie v1.0",350, 150, 800, 500, SDL_WINDOW_SHOWN);
if (window == NULL){
std::cout << "Can not open the game!" << std::endl;
}
renderer = NULL;
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
evt = new SDL_Event();
//background image
txt = NULL;
txt = IMG_LoadTexture(renderer, "mainview.bmp");
rct.x = 0 ;
rct.y = 0;
rct.h = 500;
rct.w = 800;
//button start
sbutt = NULL;
sbutt = IMG_LoadTexture(renderer, "startbutton.bmp");
startrct.x = 0 ;
startrct.y = 0;
startrct.h = 100;
startrct.w = 100;
}
FirstClass::~FirstClass(void)
{
SDL_DestroyTexture(txt);
SDL_DestroyTexture(sbutt);
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
delete evt;
}
//the code below is the loops that let the window stucks forever to keep opened
void FirstClass :: GameStart(void){
while (!quit && evt->type != SDL_QUIT){
SDL_PollEvent(evt);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, txt, NULL, &rct);
SDL_RenderCopy(renderer, sbutt, NULL, &startrct);
SDL_RenderPresent(renderer);
}
}
Please help me and thanks i'm really in a big problem.
You are handling the evt variable in a bad way. It is allocated but not initialized, and you compare the uninitialized value to SDL_QUIT. You should reorder the code inside FirstClass::GameStart and preferably use a local variable (since there is no actual need to use a dynamically allocated one) and remove the allocation and deletion of the existing evt from constructor and destructor. Here is a better version of FirstClass::GameStart:
void FirstClass::GameStart(void){
SDL_Event evt;
while (SDL_PollEvent(&evt)){
if (evt.type == SDL_QUIT) {
// a user requested termination has been received, exit the loop
break;
}
else {
// filter the event?
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, txt, NULL, &rct);
SDL_RenderCopy(renderer, sbutt, NULL, &startrct);
SDL_RenderPresent(renderer);
}
}
}
Also ensure that you are actually invoking the FirstClass::GameStart funcion from main, maybe you are simply not running the event loop at all.

SDL Error: Invalid Renderer on SDL_CreateTextureFromSurface

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");
}

'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?

SDL2: Slow window clean and update

I found out that my program - which only draw two pictures on the screen - runs at 200FPS. 200FPS is really low number at this point of development! It does not matter if I remove all textures and other data.
The slow part of code are SDL_RenderPresent and SDL_RenderClear. Without them, the program runs at about 300K FPS. Could you tell me where could be the problem?
Window and Renderer
_window = SDL_CreateWindow( "Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, 0 );
if ( _window == NULL ) {
std::cout << "CreateWindow problem: " << SDL_GetError() << std::endl;
}
_renderer = SDL_CreateRenderer( _window, -1, SDL_RENDERER_ACCELERATED );
if ( _renderer == NULL ) {
std::cout << "CreateRenderer problem: " << SDL_GetError() << std::endl;
}
Acording to SDL_RendererInfo only two flags are used - SDL_RENDERER_ACCELERATED and SDL_RENDERER_TARGETTEXTURE.

Should I make sure to destroy SDL 2.0 objects (renderer, window, textures, etc.) before exiting the program?

This tutorial on SDL 2.0 uses code that returns from main without first destroying any of the resource pointers:
int main(int argc, char** argv){
if (SDL_Init(SDL_INIT_EVERYTHING) == -1){
std::cout << SDL_GetError() << std::endl;
return 1;
}
window = SDL_CreateWindow("Lesson 2", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == nullptr){
std::cout << SDL_GetError() << std::endl;
return 2; //this
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED
| SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr){
std::cout << SDL_GetError() << std::endl;
return 3; //and this too
}
Should I tell my terminate function to DestroyRenderer, DestroyWindow, DestroyTexture, etc. before exiting?
Same question as 'should i free memory that i've allocated before quitting a program'. Yes, if there is no bugs in SDL/X11/GL/etc. finalize code - all will be freed anyway. But i see no reasons why you shouldn't want to do that yourself.
Of course if you crash rather then exit - there is a good chance some things wouldn't be done and e.g. you wouldn't return display to native desktop resolution.
Ive personally had problems with SDL_TEXTURE that caused a memory leak while the program was running and the display of pictures just stopped after the program leaked about 2gb of ram when normally it uses 37 mb of ram.
SDL_DestroyTexture(texture);
Just called this afer every time i used to display a different picture with renderer and the memory leak was gone