使用Vs Code调试C SDL2游戏,如何在运行时添加断点?

发布于 2025-01-18 11:39:38 字数 4602 浏览 1 评论 0原文

我正在使用VS代码作为我的IDE。

我已经弄清楚了如何添加断点并让调试器击中它们,但前提是在编译之前添加它们。在其他游戏开发环境中,每当我想停止游戏并查看内容时,我都可以添加断点。

我的游戏运行时有什么方法可以添加断点吗?


这是我正在运行的命令来构建我的游戏。

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

这是我从互联网上复制的小“游戏”,以防有助于提供上下文。这是一个红色的广场,可以移动并跳跃。

#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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心如狂蝶 2025-01-25 11:39:38

添加检查点后,您需要通过在其终端中按 ctrl ctrl ctrl

这将暂停程序。使用调试器恢复后,任何先前设置的检查点都将起作用。

我没有股票C ++扩展的经验,但是至少在 /a>扩展程序您需要添加“ Windows”:{“终端”:“”}, abought.json.json,以获取一个专用终端您的应用程序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文