YYDD125 2024-05-25 21:24 采纳率: 11.1%
浏览 25

如何使用c语言和sdl2创建按钮

如何实现创建30个按钮在屏幕上,窗口大小为屏幕大小,按钮上显示文字,按下分别执行一个函数

  • 写回答

5条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-05-25 21:24
    关注

    引自免费微信小程序:皆我百晓生

    为了在C语言中使用SDL2库创建30个按钮,你需要完成以下几个步骤:

    1. 安装SDL2:首先确保你已经安装了SDL2库。你可以从SDL2官网下载并按照指示进行安装。

    2. 创建项目:设置一个新的C语言项目,并将SDL2库包含到你的项目中。

    3. 编写头文件:创建一个名为button.h的头文件,定义按钮的相关结构体和函数声明。

    // button.h
    #ifndef BUTTON_H
    #define BUTTON_H
    
    #include "SDL2/SDL.h"
    
    typedef struct {
        SDL_Rect rect; // 按钮的位置和大小
        char text[50]; // 显示的文字
        int (*callback)(void); // 当按钮被点击时调用的回调函数
    } Button;
    
    Button create_button(int x, int y, int width, int height, const char *text, int (*func)(void));
    
    int button_clicked(SDL_Event *event, Button *buttons, int num_buttons);
    
    #endif
    
    1. 实现功能:编写一个名为button.c的源文件,实现按钮的创建、绘制以及检测点击的功能。
    // button.c
    #include "button.h"
    #include "SDL2/SDL.h"
    
    Button create_button(int x, int y, int width, int height, const char *text, int (*func)(void)) {
        Button button = { .rect = { .x = x, .y = y, .w = width, .h = height },
                          .text = text,
                          .callback = func };
        return button;
    }
    
    int button_clicked(SDL_Event *event, Button *buttons, int num_buttons) {
        for (int i = 0; i < num_buttons; ++i) {
            if (event->type == SDL_MOUSEBUTTONDOWN && event->button.button == SDL_BUTTON_LEFT &&
                SDL_PointInRect(&event->motion.position, &buttons[i].rect)) {
                return buttons[i].callback();
            }
        }
        return 0;
    }
    
    1. 主程序:在主程序(例如main.c)中初始化SDL、创建窗口、加载资源、创建按钮数组、事件循环等。
    // main.c
    #include <stdio.h>
    #include "SDL2/SDL.h"
    #include "button.h"
    
    // 假设你有30个不同的函数,每个函数都有一个唯一的ID
    int func_1(void) { printf("Function 1\n"); return 1; }
    int func_2(void) { printf("Function 2\n"); return 2; }
    // ... 直到 func_30
    
    int main(int argc, char **argv) {
        SDL_Init(SDL_INIT_VIDEO);
        
        // 获取屏幕尺寸
        SDL_Window *window = SDL_CreateWindow(
            "Buttons",
            SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
            SDL_GetVideoSurface()->w, SDL_GetVideoSurface()->h,
            SDL_WINDOW_SHOWN
        );
        SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
    
        // 创建30个按钮
        Button buttons[30];
        for (int i = 0; i < 30; ++i) {
            buttons[i] = create_button(i * 60, 60, 50, 50, "Func", func_i);
        }
    
        while (true) {
            SDL_Event event;
            while (SDL_PollEvent(&event)) {
                if (event.type == SDL_QUIT) break;
                
                if (button_clicked(&event, buttons, 30)) {
                    break; // 可选:退出循环,当某个按钮被点击后停止运行
                }
            }
    
            // 清除屏幕
            SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
            SDL_RenderClear(renderer);
    
            // 绘制按钮
            for (int i = 0; i < 30; ++i) {
                SDL_Color color = { 0, 0, 0, 255 };
                if (event.type == SDL_MOUSEMOTION && SDL_PointInRect(&event.motion.position, &buttons[i].rect)) {
                    color.r = 255;
                    color.g = 0;
                    color.b = 0;
                }
                SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 255);
                SDL_RenderFillRect(renderer, &buttons[i].rect);
    
                // 绘制文本
                TTF_Font *font = TTF_OpenFont("arial.ttf", 18);
                SDL_Color textColor = { 255, 255, 255, 255 };
                SDL_Surface *textSurface = TTF_RenderText_Solid(font, buttons[i].text, textColor);
                SDL_Texture *textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
                SDL_FreeSurface(textSurface);
                SDL_Rect textRect = { buttons[i].rect.x + 5, buttons[i].rect.y + 5, 0, 0 };
                SDL_QueryTexture(textTexture, NULL, NULL, &textRect.w, &textRect.h);
                SDL_RenderCopy(renderer, textTexture, NULL, &textRect);
                SDL_DestroyTexture(textTexture);
                TTF_CloseFont(font);
            }
    
            SDL_RenderPresent(renderer);
        }
    
        // 清理资源
        for (int i = 0; i < 30; ++i) {
            free(buttons[i].text);
        }
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        SDL_Quit();
    
        return 0;
    }
    

    注意:

    1. 这里使用了SDL_TTF库来渲染文本,你需要将其也添加到项目中。你可以从SDL_TTF官网下载。
    2. 请确保你有一个名为"arial.ttf"的字体文件在你的项目目录中,如果没有,替换为你项目中存在的字体文件。
    3. 代码中的func_i应该替换成实际的函数名(例如func_1),并在创建按钮时传递相应的回调函数指针。

    这个示例创建了一个窗口,其中包含30个按钮,每个按钮显示其对应的函数名称。当鼠标左键单击按钮时,会调用该按钮关联的函数。要终止程序,只需关闭窗口或按下Esc键。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 5月25日

悬赏问题

  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见
  • ¥15 一共有五道问题关于整数幂的运算还有房间号码 还有网络密码的解答?(语言-python)
  • ¥20 sentry如何捕获上传Android ndk 崩溃
  • ¥15 在做logistic回归模型限制性立方条图时候,不能出完整图的困难
  • ¥15 G0系列单片机HAL库中景园gc9307液晶驱动芯片无法使用硬件SPI+DMA驱动,如何解决?