I'm using Vs Code as my IDE.
I've figured out how to add breakpoints and have the debugger hit them, but only if they're added before compiling. In other game development environments I'm used to being able to add a breakpoint whenever I want to halt the game and check things out.
Is there any way to add breakpoints while my game is running?
This is the command I'm running to build my game.
gcc -std=c17 main.c -g -I "C:\Program Files\clib\SDL2\include" -L "C:\Program Files\clib\SDL2\lib" -Wall -lmingw32 -lSDL2main -lSDL2 -o game
This is my little "game" that I copy-pasted from the internet, in case that helps provide context. It's a little red square that can move and jump around.
#include <stdio.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#define WIDTH 640
#define HEIGHT 480
#define SIZE 200
#define SPEED 600
#define GRAVITY 60
#define FPS 60
#define JUMP -1200
int main(int argc, char *argv[])
{
/* Initializes the timer, audio, video, joystick,
haptic, gamecontroller and events subsystems */
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
printf("Error initializing SDL: %s\n", SDL_GetError());
return 0;
}
/* Create a window */
SDL_Window *wind = SDL_CreateWindow(
"Hello Platformer!",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
WIDTH,
HEIGHT,
0
);
if (!wind)
{
printf("Error creating window: %s\n", SDL_GetError());
SDL_Quit();
return 0;
}
/* Create a renderer */
Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
SDL_Renderer *rend = SDL_CreateRenderer(wind, -1, render_flags);
if (!rend)
{
printf("Error creating renderer: %s\n", SDL_GetError());
SDL_DestroyWindow(wind);
SDL_Quit();
return 0;
}
/* Main loop */
bool running = true,
jump_pressed = false,
can_jump = true,
left_pressed = false,
right_pressed = false;
float x_pos = (WIDTH - SIZE) / 2, y_pos = (HEIGHT - SIZE) / 2, x_vel = 0, y_vel = 0;
SDL_Rect rect = {(int)x_pos, (int)y_pos, SIZE, SIZE};
SDL_Event event;
while (running)
{
/* Process events */
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.scancode)
{
case SDL_SCANCODE_SPACE:
jump_pressed = true;
break;
case SDL_SCANCODE_A:
case SDL_SCANCODE_LEFT:
left_pressed = true;
break;
case SDL_SCANCODE_D:
case SDL_SCANCODE_RIGHT:
right_pressed = true;
break;
default:
break;
}
break;
case SDL_KEYUP:
switch (event.key.keysym.scancode)
{
case SDL_SCANCODE_SPACE:
jump_pressed = false;
break;
case SDL_SCANCODE_A:
case SDL_SCANCODE_LEFT:
left_pressed = false;
break;
case SDL_SCANCODE_D:
case SDL_SCANCODE_RIGHT:
right_pressed = false;
break;
default:
break;
}
break;
default:
break;
}
}
/* Clear screen */
SDL_SetRenderDrawColor(rend, 0, 0, 0, 255);
SDL_RenderClear(rend);
/* Move the rectangle */
x_vel = (right_pressed - left_pressed) * SPEED;
y_vel = GRAVITY;
if (jump_pressed && can_jump)
{
can_jump = false;
y_vel = JUMP;
}
x_pos = x_vel / 60;
y_pos = y_vel / 60;
if (x_pos <= 0) x_pos = 0;
if (x_pos >= WIDTH - rect.w) x_pos = WIDTH - rect.w;
if (y_pos <= 0) y_pos = 0;
if (y_pos >= HEIGHT - rect.h)
{
y_vel = 0;
y_pos = HEIGHT - rect.h;
if (!jump_pressed)
can_jump = true;
}
rect.x = (int)x_pos;
rect.y = (int)y_pos;
/* Draw the rectangle */
SDL_SetRenderDrawColor(rend, 255, 0, 0, 255);
SDL_RenderFillRect(rend, &rect);
/* Draw to window and loop */
SDL_RenderPresent(rend);
SDL_Delay(1000 / FPS);
}
/* Release resources */
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(wind);
SDL_Quit();
return 0;
}
CodePudding user response:
After adding a checkpoint, you need to send SIGINT to your program by pressing CtrlC in its terminal.
This will pause the program. After you resume it with the debugger, any previously set checkpoints will work.
I don't have experience with the stock C extension, but at least in the Native Debug extension you need to add "windows": {"terminal": ""},
to the current configuration in launch.json
, to get a dedicated terminal for your app.