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

SDL学习

Watrt2年前 (2024-08-14)LINUX12320

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

打赏 支付宝打赏 微信打赏

相关文章

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...

Linux系统信息查看命令大全

Linux系统信息查看命令大全

最近看了一些Linux命令行的文章,在系统信息查看方面学到不少命令。想起以前写过的一篇其实Linux这样用更简单,发现这些系统信息查看命令也可以总结出一篇小小的东西来了。另外这里还有非常多的命令,可以作为参考。系统# uname -a               # 查看内核/操作系统/CPU信息# head -n 1 /etc/issue   # 查看操作系统版本# cat /proc/cpuinfo  ...

Deepin Linux修复grub引导

Deepin Linux修复grub引导

环境说明:一直使用的是Win7+Deepin 15.5。后来全新安装了Win 10,需要修复grub第一步:在Windows操作系统下使用深度官方的U盘启动制作器 制作U盘第二步:开机U盘启动进入Deepin linux安装界面,待进入到安装界面选择语言时,按住Crtl+Alt+F2/F1进入Linux tty终端。并执行以下命令完成修复sudo fdisk -l/*根据查询结果确定deepin 的/目录和/boot目录所在的分区编号*/sudo mount&nbs...

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...

利用BusyBox ~私人定制 My LINUX~

利用BusyBox ~私人定制 My LINUX~

前言  我在今天在这里跟大家详细地探讨一下Linux系统的定制过程和实现例如、用户能够远程登录;和Nginx能够稳定地运行在我们私人定制的LINUX系统上、一步一步从头开始定制属于我们自己的系统。正文   首先我们先来简单的介绍一下我们这里定制属于自己的Linux系统的基本元素.   一个定制的linux内核+一个定制的busybox就可以定制一个小型的Linux操作系统了,安装Dropbear和Nginx,Linux的组成 部分包括内核空间和用户空间、而...

 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...

发表评论

访客

看不清,换一张

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