Executable created by CodeBlocks opens and closes alone

0

I have a problem with my program in CodeBlocks. When I compile and run the program, CodeBlocks creates an executable in the Debug folder that is inside the bin folder, located in the same folder where the project is. When trying to open the .exe of this Debug folder, it simply opens and closes, in a fraction of a second, however, compiling and running in CodeBlocks, the program works normally, waiting for the user to press the X of the window or press ESC on the keyboard. I made the program using C ++ and SDL 2.0, below the code:

#include <SDL.h>
#include <SDL_image.h>
#define SCREEN_W 800
#define SCREEN_H 600

int main(int argc, char** argv)
{
    bool quit = false;
    SDL_Window* window = NULL;
    SDL_Surface* windowSurface = NULL;
    SDL_Surface* imagem = NULL;
    SDL_Renderer* renderer = NULL;
    SDL_Texture* texture = NULL;
    SDL_Event event;

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        SDL_Log("%s\n",SDL_GetError());
        return 1;
    }

    int imgFlags = IMG_INIT_PNG;
    if (!IMG_Init(imgFlags) & imgFlags) {
        SDL_Log("%s\n",IMG_GetError());
        return 1;
    }

    imagem = IMG_Load("camaro.png");
    if (imagem == NULL) {
        SDL_Log("%s\n",IMG_GetError());
        return 1;
    }

    window = SDL_CreateWindow("Nome", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_W, SCREEN_H, SDL_WINDOW_SHOWN);
    if (window == NULL) {
        SDL_Log("%s\n",SDL_GetError());
        return 1;
    }

    windowSurface = SDL_GetWindowSurface(window);
    if (windowSurface == NULL) {
        SDL_Log("%s\n",SDL_GetError());
        return 1;
    }

    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    if (renderer == NULL) {
        SDL_Log("%s\n",SDL_GetError());
        return 1;
    }

    texture = SDL_CreateTextureFromSurface(renderer, imagem);
    if (texture == NULL) {
        SDL_Log("%s\n",SDL_GetError());
        SDL_FreeSurface(imagem);
        return 1;
    }
    SDL_FreeSurface(imagem);

    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);

    while(!quit) {
        while(SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                quit = true;
            }
            if (event.type == SDL_KEYDOWN) {
                switch (event.key.keysym.sym) {
                    case SDLK_ESCAPE: quit = true; break;
                    default: break;
                }
            }
        }
        SDL_RenderClear(renderer);
        SDL_RenderCopy(renderer, texture, NULL, NULL);
        SDL_RenderPresent(renderer);
    }

    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

Who knows how to solve, thank you.

    
asked by anonymous 28.07.2015 / 06:37

1 answer

1

When a program runs without using the terminal to the false sense that it opens and closes. This is because when running with two clicks it opens, runs and as it came to the end it closes the window ... To see the console / window make the program wait for something like for example the user press enter

int main()
{
    /*
     * Codigo do programa
    */

    cin.get(); // Ira fazer o programa aguardar um enter para ir pro final
    return 0;
}

If you search you will find some codes where in the end it contains SDL_Delay (x)

int main()
{
    /*
     * Codigo do programa
    */

    SDL_Delay( 2000 ); // Para a execução do programa por 2 segundos
    SDL_Quit();
    return 0;
}
    
28.07.2015 / 15:25