Executable created by Codeblocks opens and closes alone

Asked

Viewed 1,507 times

0

I have a problem with my Codeblocks program. When I compile and run the program, Codeblocks creates a executable in the Debug folder that is inside the bin folder, located in the same folder where the project is located. By trying to open the . exe from that Debug folder, it simply opens and closes, in a fraction of a second, however, by compiling and running in Codeblocks, the program works normally, waiting for the user to press the X of the window or press ESC from the keyboard. I made the program using C++ and SDL 2.0, follows 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;
}

Whoever knows how to fix it, thank you.

1 answer

1

When a program is run without using the terminal to the false sense that it opens and closes. This is because when running with two clicks it opens, executes and how it got to the end it closes the window... To see the console/window make the program wait for something like for the user to 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 at the end 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.