SDL事件处理以及线程使用(2)

简介: SDL库中事件处理和多线程编程的基本概念和示例代码,包括如何使用SDL事件循环来处理键盘和鼠标事件,以及如何创建和管理线程、互斥锁和条件变量。

SDL事件处理以及线程使用

事件使用

#include <stdio.h>
#include <SDL.h>

#define FF_QUIT_EVENT (SDL_USEREVENT + 1)       // 定义自定义事件

#undef main
int main()
{
    SDL_Window* pWindow = NULL;

    SDL_Init(SDL_INIT_VIDEO);

    // 创建窗口
    pWindow = SDL_CreateWindow("Event Test Title",
                               SDL_WINDOWPOS_UNDEFINED,
                               SDL_WINDOWPOS_UNDEFINED,
                               640,
                               480,
                               SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
    if(NULL == pWindow)
    {
        printf("Create window error: %s\n", SDL_GetError());
        return -1;
    }

    SDL_Event event;
    int is_exit = 1;
    while(is_exit)
    {
        SDL_WaitEvent(&event);
        switch (event.type)             // 区分鼠标事件、键盘事件
        {
        case SDL_KEYDOWN:               // 键盘按下事件
            switch (event.key.keysym.sym)
            {
                case SDLK_a:
                    break;
                case SDLK_s:
                    break;
                case SDLK_d:
                    break;
                case SDLK_w:
                    break;
                case SDLK_q:{
                    SDL_Event myEvent;              // 自定义事件并发送
                    myEvent.type = FF_QUIT_EVENT;
                    SDL_PushEvent(&myEvent);
                    break;
                }
            }
            break;
        case SDL_MOUSEBUTTONDOWN:           // 鼠标按键按下事件
            switch(event.button.button)
            {
            case SDL_BUTTON_LEFT:
                printf("mouse button left down\n");
                break;
            case SDL_BUTTON_RIGHT:
                printf("mouse button right down\n");
                break;
            }
            break;
        case SDL_MOUSEMOTION:           // 鼠标移动事件
            printf("mouse move {%d, %d}\n", event.button.x, event.button.y);
            break;
        case FF_QUIT_EVENT:             // 自定义事件
            printf("myself event is triggered\n");
            is_exit = 0;
            break;
        }
    }

    // 销毁窗口,释放资源
    if(pWindow)
        SDL_DestroyWindow(pWindow);
    SDL_Quit();

    return 0;
}

线程使用

#include <SDL.h>
#include <stdio.h>

int n = 0;
// 线程处理函数
int thread_work(void* arg)
{
    printf("enter thread arg: %d\n", *((int*)arg));
    for(int i = 0; i < 500000; i++)
    {
        n++;
    }
    return 0;
}

#undef main
int main()
{
    int thread_num = 520;
    // 创建线程
    SDL_Thread* thread1 = SDL_CreateThread(thread_work, "thread1Name", &thread_num);
    if(NULL == thread1)
    {
        printf("thread1 create error %s\n", SDL_GetError());
        return -1;
    }
    thread_num++;
    SDL_Thread* thread2 = SDL_CreateThread(thread_work, "thread2Name", &thread_num);
    if(NULL == thread2)
    {
        printf("thread2 create error: %s\n", SDL_GetError());
        return -1;
    }

    // 等待线程汇入主线程
    SDL_WaitThread(thread1, NULL);
    SDL_WaitThread(thread2, NULL);
    printf("n is value: %d\n", n);

    return 0;
}

互斥锁使用

#include <SDL.h>
#include <stdio.h>

/// 互斥锁 ///
SDL_mutex* mutex = NULL;
/

int n = 0;
// 线程处理函数
int thread_work(void* arg)
{
    printf("enter thread arg: %d\n", *((int*)arg));
    /// 上锁 ///
    SDL_LockMutex(mutex);
    /
    for(int i = 0; i < 500000; i++)
    {
        n++;
    }/// 解锁 ///
    SDL_UnlockMutex(mutex);
    /
    return 0;
}

#undef main
int main()
{
    /// 创建互斥锁 ///
    mutex = SDL_CreateMutex();
    if(NULL == mutex)
    {
        printf("Create mutex error: %s\n", SDL_GetError());
        return -1;
    }
    /

    int thread_num = 520;
    // 创建线程
    SDL_Thread* thread1 = SDL_CreateThread(thread_work, "thread1Name", &thread_num);
    if(NULL == thread1)
    {
        printf("thread1 create error %s\n", SDL_GetError());
        return -1;
    }
    thread_num++;
    SDL_Thread* thread2 = SDL_CreateThread(thread_work, "thread2Name", &thread_num);
    if(NULL == thread2)
    {
        printf("thread2 create error: %s\n", SDL_GetError());
        return -1;
    }

    // 等待线程汇入主线程
    SDL_WaitThread(thread1, NULL);
    SDL_WaitThread(thread2, NULL);
    printf("n is value: %d\n", n);

    /// 释放锁资源 ///
    SDL_DestroyMutex(mutex);
    /

    return 0;
}

条件变量使用

#include <SDL.h>
#include <stdio.h>

SDL_mutex* g_mutex = NULL;  // 互斥锁
SDL_cond*  g_cond = NULL;   // 条件变量

// 线程等待函数
int thread_waitFunc(void* arg)
{
    printf("thread %d wait......\n", *(int*)arg);
    SDL_LockMutex(g_mutex);
    SDL_CondWait(g_cond, g_mutex);
    SDL_UnlockMutex(g_mutex);
    printf("thread wait end, start work......\n");

    return 1;
}

// 线程发送信号函数
int thread_signFunc(void* arg)
{
    printf("thread %d sleep 5s\n", *(int*)arg);
    sleep(5);                   // 5秒后唤醒等待的线程
    SDL_CondSignal(g_cond);     // 唤醒等待的线程
    printf("thread %d send signal success\n", *(int*)arg);
    return 666;
}

#undef main
int main()
{
    // 创建互斥锁和条件变量
    g_mutex = SDL_CreateMutex();
    g_cond  = SDL_CreateCond();
    if(g_mutex == NULL || g_cond == NULL)
    {
        printf("Create mutex or cond failed: %s\n", SDL_GetError());
        return -1;
    }
    // 创建线程
    int wait_num = 1001;
    SDL_Thread* thread_wait = SDL_CreateThread(thread_waitFunc, "threadWait", &wait_num);
    if(NULL == thread_wait)
    {
        printf("create threadWait error: %s\n", SDL_GetError());
        return -1;
    }
    wait_num++;
    SDL_Thread* thread_sign = SDL_CreateThread(thread_signFunc, "threadSignal", &wait_num);
    if(NULL == thread_sign)
    {
        printf("create threadSign error: %s\n", SDL_GetError());
        SDL_CondSignal(g_cond);
        SDL_WaitThread(thread_wait, NULL);
        return -1;
    }

    // 将两个线程汇入主线程
    SDL_WaitThread(thread_wait, NULL);
    SDL_WaitThread(thread_sign, NULL);

    // 释放锁跟条件变量的资源
    SDL_DestroyMutex(g_mutex);
    SDL_DestroyCond(g_cond);

    return 0;
}

相关文章
|
27天前
|
弹性计算 人工智能 架构师
阿里云携手Altair共拓云上工业仿真新机遇
2024年9月12日,「2024 Altair 技术大会杭州站」成功召开,阿里云弹性计算产品运营与生态负责人何川,与Altair中国技术总监赵阳在会上联合发布了最新的“云上CAE一体机”。
阿里云携手Altair共拓云上工业仿真新机遇
|
4天前
|
人工智能 Rust Java
10月更文挑战赛火热启动,坚持热爱坚持创作!
开发者社区10月更文挑战,寻找热爱技术内容创作的你,欢迎来创作!
438 17
|
7天前
|
JSON 自然语言处理 数据管理
阿里云百炼产品月刊【2024年9月】
阿里云百炼产品月刊【2024年9月】,涵盖本月产品和功能发布、活动,应用实践等内容,帮助您快速了解阿里云百炼产品的最新动态。
阿里云百炼产品月刊【2024年9月】
|
20天前
|
存储 关系型数据库 分布式数据库
GraphRAG:基于PolarDB+通义千问+LangChain的知识图谱+大模型最佳实践
本文介绍了如何使用PolarDB、通义千问和LangChain搭建GraphRAG系统,结合知识图谱和向量检索提升问答质量。通过实例展示了单独使用向量检索和图检索的局限性,并通过图+向量联合搜索增强了问答准确性。PolarDB支持AGE图引擎和pgvector插件,实现图数据和向量数据的统一存储与检索,提升了RAG系统的性能和效果。
|
7天前
|
Linux 虚拟化 开发者
一键将CentOs的yum源更换为国内阿里yum源
一键将CentOs的yum源更换为国内阿里yum源
379 2
|
22天前
|
人工智能 IDE 程序员
期盼已久!通义灵码 AI 程序员开启邀测,全流程开发仅用几分钟
在云栖大会上,阿里云云原生应用平台负责人丁宇宣布,「通义灵码」完成全面升级,并正式发布 AI 程序员。
|
24天前
|
机器学习/深度学习 算法 大数据
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
2024“华为杯”数学建模竞赛,对ABCDEF每个题进行详细的分析,涵盖风电场功率优化、WLAN网络吞吐量、磁性元件损耗建模、地理环境问题、高速公路应急车道启用和X射线脉冲星建模等多领域问题,解析了问题类型、专业和技能的需要。
2600 22
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
|
6天前
|
存储 人工智能 搜索推荐
数据治理,是时候打破刻板印象了
瓴羊智能数据建设与治理产品Datapin全面升级,可演进扩展的数据架构体系为企业数据治理预留发展空间,推出敏捷版用以解决企业数据量不大但需构建数据的场景问题,基于大模型打造的DataAgent更是为企业用好数据资产提供了便利。
286 2
|
4天前
|
编译器 C#
C#多态概述:通过继承实现的不同对象调用相同的方法,表现出不同的行为
C#多态概述:通过继承实现的不同对象调用相同的方法,表现出不同的行为
106 65
|
24天前
|
机器学习/深度学习 算法 数据可视化
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码
2024年中国研究生数学建模竞赛C题聚焦磁性元件磁芯损耗建模。题目背景介绍了电能变换技术的发展与应用,强调磁性元件在功率变换器中的重要性。磁芯损耗受多种因素影响,现有模型难以精确预测。题目要求通过数据分析建立高精度磁芯损耗模型。具体任务包括励磁波形分类、修正斯坦麦茨方程、分析影响因素、构建预测模型及优化设计条件。涉及数据预处理、特征提取、机器学习及优化算法等技术。适合电气、材料、计算机等多个专业学生参与。
1582 17
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码