当前位置:首页 > 技术 > LINUX > 正文内容

SDL学习

Watrt1年前 (2024-08-14)LINUX9250

今天简单学习一了一下SDL库的使用代码如下:

#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
SDL_Window *window;
SDL_Renderer *renderer;
TTF_Font *font;
TTF_Font *font1;
TTF_Font *font2;
int init(void)
{
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)
    {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
        return -1;
    }
    window = SDL_CreateWindow("实时时钟", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
    if (!window)
    {
        printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
        return -1;
    }
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    if (!renderer)
    {
        printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError());
        return -1;
    }
    if (TTF_Init() == -1)
    {
        printf("TTF could not initialize! SDL_ttf Error: %s\n", TTF_GetError());
        return -1;
    }
    font = TTF_OpenFont("/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc", 36);
    font1 = TTF_OpenFont("/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc", 96);
    font2 = TTF_OpenFont("/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc", 48);
    TTF_SetFontHinting(font, TTF_HINTING_LIGHT);
    TTF_SetFontHinting(font1, TTF_HINTING_LIGHT);
    TTF_SetFontHinting(font2, TTF_HINTING_LIGHT);
    if (!font)
    {
        printf("Failed to load font! SDL_ttf Error: %s\n", TTF_GetError());
        return -1;
    }
    return 0;
}
char *getFormattedDateTime(void)
{
    time_t rawtime;
    struct tm *timeinfo;
    static char buffer[80];
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    strftime(buffer, sizeof(buffer), "%H:%M:%S", timeinfo);
    return buffer;
}
char *getFormattedDate(void)
{
    time_t rawtime;
    struct tm *timeinfo;
    static char buffer[80];
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    strftime(buffer, sizeof(buffer), "%Y年%m月%d日", timeinfo);
    return buffer;
}
void renderText(const char *text, int x, int y, TTF_Font *f, SDL_Color textColor)
{
    SDL_Surface *textSurface = TTF_RenderUTF8_Solid(f, text, textColor);
    SDL_Texture *textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
    SDL_Rect textRect = {x, y, textSurface->w, textSurface->h};
    SDL_RenderCopy(renderer, textTexture, NULL, &textRect);
    SDL_FreeSurface(textSurface);
    SDL_DestroyTexture(textTexture);
}
int main(int argc, char *args[])
{
    if (init() != 0)
    {
        return -1;
    }
    int quit = 0;
    SDL_Event e;
    int windowWidth, windowHeight;
    SDL_Surface *backgroundSurface = NULL;
    SDL_Texture *backgroundTexture = NULL;
    SDL_GetWindowSize(window, &windowWidth, &windowHeight);
    if ((backgroundSurface = IMG_Load("./x.jpg")) == NULL)
    { // 请替换为你的图片路径
        printf("无法加载背景图片: %s\n", IMG_GetError());
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    }
    else
    {
        backgroundTexture = SDL_CreateTextureFromSurface(renderer, backgroundSurface);
        SDL_FreeSurface(backgroundSurface);
    }
    while (!quit)
    {
        while (SDL_PollEvent(&e) != 0)
        {
            if (e.type == SDL_QUIT)
            {
                quit = 1;
            }
        }
        SDL_RenderClear(renderer);
        if (backgroundTexture)
        {
            SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL);
        }
        char *dateTimeStr = getFormattedDateTime();
        SDL_Color dateTimeColor = {255, 255, 255, 255}; // 白色
        renderText(dateTimeStr, 100, 160, font1, dateTimeColor);                                      // 假设在左上角显示
        dateTimeStr = getFormattedDate();
        SDL_Color dateColor = {0, 0, 255, 255}; // 蓝色
        renderText(dateTimeStr, 30, 10, font, dateColor);
        SDL_Color greetingColor = {255, 0, 0, 255}; // 红色
        renderText((char *)"大家好", windowWidth - 200, windowHeight - 100, font2, greetingColor); // 在右下角显示问候语
        // 注意:阴历显示需额外实现,这里省略
        SDL_RenderPresent(renderer);
        SDL_Delay(1000); // 每秒更新一次
    }
    TTF_CloseFont(font);
    TTF_Quit();
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

编译代码:

gcc -I/usr/include/x86_64-linux-gnu/ -o clock  clock.c -lSDL2 -lSDL2_ttf -lSDL2_image  &&./clock

VirtualBox_xubuntu_14_08_2024_02_10_13.png

打赏 支付宝打赏 微信打赏
分享给朋友:

相关文章

mkfs 参数

mkfs 参数

#mkfs.jffs2 -r rootfs -o fs.jffs2 -e 0x20000 --pad=0x500000 -s 0x800 –n -l即可生成 rootfs.jffs2Mkfs.jffs2各参数的意义如下:-r:指定要做成image的目录名。-o:指定输出image的文件名。-e:每一块要擦除的block size,默认是64KB.要注意,不同的flash, 其block size会不一样,三星的K9F2G08U0A的block size为0x20000(从其datasheet里可...

licheepi_zero开发板 buildroot配置 一键编译

licheepi_zero开发板 buildroot配置 一键编译

开贴记录配置最新buildroot-2018.08.使用buildroot自带工具链一键编译生成uboot,kernel,dtb和rootfs.最新稳定版buildroot下载:buildroot-2018.08.02稳定版我配置好的config文件:配置好的.config本人的开发环境是vmware workstation 15 pro+ubuntu 16.04_x64参照沉鱼的帖子荔枝派Zero V3s开发板入坑记录 (TF/SD卡启动)(主线Linux,主线u-boot)做如下配置:1.t...

buildroot编译中的问题

buildroot编译中的问题

在编译中遇到flex 报错。一直过不了。后来发现安装flex bison两个包后解决sudo apt-get install flex bison...

荔枝派Nano 全流程指南

荔枝派Nano 全流程指南

u-boot 初体验安装交叉编译链首先需要安装交叉编译链:# 此处为获取7.2.1版本,您可获取其他版本或者通过链接直接下载 wget http://releases.linaro.org/components/toolchain/binaries/7.2-2017.11/arm-linux-gnueabi/gcc-linaro-7.2.1-2017.11-x86_64_arm-linux-gnueabi.tar.xz tar -vxJf gcc-li...

 Debian 9.9 (stretch) 文件系统制作

Debian 9.9 (stretch) 文件系统制作

0. 准备工作:sudo apt install qemu-user-static -y sudo apt install debootstrap -y mkdir rootfs1. debootstrapdebootstrap --foreign --verbose --arch=armhf  stretch rootfs http...

[转]《保姆级教程》全志F1C100S/F1C200S spi-flash 启动全流程适配烧录及踩坑指南

[转]《保姆级教程》全志F1C100S/F1C200S spi-flash 启动全流程适配烧录及踩坑指南

转自哇酷网=丨晋丨通过参考荔枝派nano官方和论坛大佬的帖子,总结了烧录 spi-flash 启动的方法。通过搜寻资料,把其中有错误或者做了多余的操作的步骤做了修正,以免大家再次踩坑,耗费青春。以下包括 uboot、kernel、buildroot 和 烧录的详细步骤和需要注意的问题,尽量精简方法,以期容易上手和理解。各种配置项也做了详细注释,要知其然,也知其所以然。最理想的状态应该是是:有的坑,踩的人多了,也便没有了坑。论坛不太好排版,有需要的也可以去我的博客看:P:全志F1C100S/F1C...

Configure & Build

Configure & Build

Configure and build Linux kernel for Surface RTEditing kernel build configurationFirst run the command make ARCH=arm tegra_defconfigThis will create a standard kernel config for tegra SoC's.Open the file .config in the kernel source directory wit...

f1c100s编译启动所需的uboot,kernel,rootfs

f1c100s编译启动所需的uboot,kernel,rootfs

 https://github.com/Icenowy/linux.git 有f1c100s-480272lcd-test和f1c100s分支, 然后自己手动修复一个 USB 问题,驱动就比较全了https://github.com/Lichee-Pi/linux.git 有nano-4.14-exp和nano-5.2-flash分支,用哪个合适个人画了块没有连接任何其他模块的F1C100S开发板,没有链接任何外设,本文是编译所有启动所需的三大件。(只要编...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。