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

SDL学习

Watrt2年前 (2024-08-14)LINUX11570

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

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

相关文章

[转]Linux内核配置和编译过程详解

[转]Linux内核配置和编译过程详解

一、引言: 本文档的内容大部份内容都是从网上收集而来,然后配合一些新的截 图(内核版本:V2.4.19)。在每一配置项后会有一个选择指南的部份,用来指导大家怎么样 根据自己的情况来做相应的选择;还有在每一个大项和文档的最后会有一个经验谈,它是一些高手们在应对问题和处理特有硬件时的一些经验(这个还得靠各位)。 文档最后会发到网上,到时会根据网友们的回复随时进行更新。 我们的目的是让我们有一个全面的、简单明了内核编译帮手。#make mrproper -----删除不必要的文件和...

基于 debootstrap 和 busybox 构建 mini ubuntu

基于 debootstrap 和 busybox 构建 mini ubuntu

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

使用buildroot构建根文件系统

使用buildroot构建根文件系统

使用buildroot构建根文件系统buildroot可用于构建小型的linux根文件系统。大小最小可低至2M,与内核一起可以放入最小8M的spi flash中。buildroot中可以方便地加入第三方软件包(其实已经内置了很多),省去了手工交叉编译的烦恼。下载安装首先安装一些依赖,比如linux头文件:apt-get install linux-headers-$(uname -r)然后下载安装:wget https://buildroot.org/do...

关于CentOS 下 httpd 修改默认的目录后 403 错误的 解决

关于CentOS 下 httpd 修改默认的目录后 403 错误的 解决

step 1 : 首先改你要设置的目录权限       chmod -R 755 /home/html          -------------->>  /home/html   是你存放你的页面的目录   这里我顺便记录下关于selinux 的命令吧    getenforce=>查看 selinux的状态&nbs...

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

制作荔枝派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系统信息查看命令大全

Linux系统信息查看命令大全

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

荔枝派nano(f1c100s)的SPI-Flash系统编译创建全过程

荔枝派nano(f1c100s)的SPI-Flash系统编译创建全过程

前言本文的目标是创建一个运行在SPI-Flash上的精简系统,附带填一些前人没有提及的坑。在开始之前,请先通读官方教程的即食部分(U-Boot)、Linux编译和SPI-Flash系统的创建部分的教程,并搭建好编译工具链。以下我假设你已经按照上面的教程下载好了U-Boot和Linux内核,并且到Buildroot的官网下载好了Buildroot(但没按教程创建config文件)。SPI-Flash的分区结构以下是我这里的分区结构。你可以自由的分配后面两个分区的大小。ID  S...

发表评论

访客

看不清,换一张

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