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.