Im new on SDL and trying code fail


#1

Hi first sorry for my english im try to speak much better and im new on world of programming. If any person suggest me how to learn spaces with code i would apreciate. Now the problem im learning how to make games with SDL from an tutorial but i have a problem. I try to fail the code with a sdl or window or surface but can not print in screen the errors. instead of failing I can not see the error code with SDL_GetError()
and the window does not close.
Im using code::blocks and learning from here Tutorial Lazyfoo
Here the code can’t upload:

#include <SDL2/SDL.h>
#include <iostream>
using namespace std;
const int width=640,height=480;
int main(){
SDL_Window* window = NULL; 
SDL_Surface* surface = NULL;  

if(SDL_Init(SDL_INIT_VIDEO)<0){
cout<<"Can't start SDL .\nError: "<<SDL_GetError()<<endl;
}else{
    window = SDL_CreateWindow("Game Window",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,height,width,SDL_WINDOW_SHOWN);
    if(window == NULL){
    cout<<"Can't start Window.\nError: "<<SDL_GetError()<<endl;
    }else{
        surface = SDL_GetWindowSurface(surface);
        SDL_FillRect(surface,NULL,SDL_MapRGB( surface->format,0xFF,0xFF,0xFF) );
        SDL_UpdateWindowSurface(window);
        SDL_Delay(2000);
        }
    SDL_DestroyWindow(window);
    SDL_Quit();
}

return 0;
}

#2

The reason why you don’t see any error messages, and no window shutdown occur, is because there’s no error in your code and in the creation of the window and surface. :slight_smile:

If you do really want to see the error messaging occur, you can always comment out your window = SDL_CreateWindow(); code and the error message will be printed to the console.
You’ll probably not have the time to see the error message though, since the program will shutdown right after the message has been printed to the console.
If you want, you can add a std::cin.get(); call right after the cout call, which will wait for an input from the user before the program is shutdown. This means that the program won’t shutdown until you press a key on your keyboard.

Code example:

if(window == NULL)
{
	std::cout << "Window creation failed. Error: " << SDL_GetError() << endl;

	std::cin.get();
}