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

SDL学习

Watrt1年前 (2024-08-14)LINUX11220

今天简单学习一了一下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

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

相关文章

基于 debootstrap 和 busybox 构建 mini ubuntu

基于 debootstrap 和 busybox 构建 mini ubuntu

最近的工作涉及到服务器自动安装和网络部署操作系统,然后使用 ansible 和 saltsatck 进行配置并安装 openstack 。难点在于服务器的自动安装,由于不单只是通过 PXE 安装服务器,还需要能够安装时进行分区、配置网卡等工作,因此需要在开始安装前,必须先收集服务器的硬件信息。调研了一下目前的开源项目中,提供此类功能的有 tinycorelinux 、 puppet razor-el-mk 可做类似的工作。tinycorelinux 是个很好的工具,整个系统在 PXE 之后在内存...

制作荔枝派Zero开发板(全志V3s) TF/SD卡启动盘

制作荔枝派Zero开发板(全志V3s) TF/SD卡启动盘

0. 前言近几天买了一块荔枝派0开发板,以及官方配的480×272的屏幕。让我记录一下入坑与采坑过程。1. u-boot的编译git clone https://github.com/Lichee-Pi/u-boot -b v3s-current cd u-boot make ARCH=arm LicheePi_Zero_480x272LCD_defconfig make ARCH=arm CROSS...

LINUX 使用sendmail邮件备份

LINUX 使用sendmail邮件备份

首先安装:sudo apt-get install sendmail然后在终端可以使用mail来发邮件echo "ESP32固件"|mail -s "esp32" -A "./fw.bin" -r "bak@xxx.com" xb100@qq.com注意很有可能收到的邮件在垃圾箱里面。把发件地址加到白 名单中...

荔枝派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...

Linux没有最小只有更小----迷你Linux版本大集合

Linux没有最小只有更小----迷你Linux版本大集合

    自从去年到现在已经收集了上百种版本的Linux和Unix,至于Unix就不想说了,没有Linux的功底是很难驾驭Unix的,我在这里只把小于360M的Linux以及一些非Linux但是很像Linux的版本也发布一下,我本人喜欢安静,如果你想要这些迷你版本的Linux光盘的话,建议去官网下载就OK了。英语不行的话随时带个字典。有些没有桌面,想知道哪些没有桌面的话请自己网上查资料!下面就是绝大部分小于361M的Linux及其非Linux名单,参考时间为...

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

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

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

debootstrap报"Release signed by unknown key"错误的解决方法

debootstrap报"Release signed by unknown key"错误的解决方法

1、下载最新的ASC文件:wget https://ftp-master.debian.org/keys/release-11.asc -qO- | gpg --import --no-default-keyring --keyring ./debian-release-11.gpg注意这里的11对应debian的版本号。我这里是debian11 (bullseye)2、debootstrap指定asc文件sudo&...

老是提示:newer kernel available 解决方法

老是提示:newer kernel available 解决方法

这个是由于没有正确的安装内核造成的。可以直接关掉提示 sudo apt purge needrestart...

发表评论

访客

看不清,换一张

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