SDL入门操练_sdl_setrendertarget-程序员宅基地

技术标签: 音视频  SDL 入门  

概述

本文使用SDL作为图形库,实现了在一个界面上, 隔一会就出来一个方块,算是学习SDL图形库的入门, 为了方便, 使用了Qt IDE, 而不是在Linux上构建。对于新手来说,对各个函数的名称, 参数, 作用, 返回值都不熟悉, 借用IDE能显著降低入门难度, 就不要逞能直接在vim里写项目了。

项目实现

逻辑流程

在这里插入图片描述

重要控制原语

PS:鉴于其他的函数看名字就知道是啥玩意, 在这里就不赘述,只把不太明显的讲解一下, 大家自己手动敲一遍, 就什么都明白了
SDL_SetRenderTarget把一个纹理设置为渲染目标。接下来的所有对render的操作其实都是操作了纹理。

/**
 * \brief Set a texture as the current rendering target.
 *
 * \param renderer The renderer.
 * \param texture The targeted texture, which must be created with the SDL_TEXTUREACCESS_TARGET flag, or NULL for the default render target
 *
 * \return 0 on success, or -1 on error
 *
 *  \sa SDL_GetRenderTarget()
 */
extern DECLSPEC int SDLCALL SDL_SetRenderTarget(SDL_Renderer *renderer,
                                                SDL_Texture *texture);

SDL_SetRenderDrawColor这里是以RGB的形式, 定义了一种颜色, 此颜色将被用来在render上画各种图形

/**
 *  \brief Set the color used for drawing operations (Rect, Line and Clear).
 *
 *  \param renderer The renderer for which drawing color should be set.
 *  \param r The red value used to draw on the rendering target.
 *  \param g The green value used to draw on the rendering target.
 *  \param b The blue value used to draw on the rendering target.
 *  \param a The alpha value used to draw on the rendering target, usually
 *           ::SDL_ALPHA_OPAQUE (255).
 *
 *  \return 0 on success, or -1 on error
 */
extern DECLSPEC int SDLCALL SDL_SetRenderDrawColor(SDL_Renderer * renderer,
                                           Uint8 r, Uint8 g, Uint8 b,
                                           Uint8 a);

SDL_RenderClear用预设的颜色, 清空render: 说是清空, 其实就是用上一步预设的颜色把整个render都糊上。 这里的clear有很强的迷惑性, 从源码看, 就是把renderer结构体里的r、g、b、a参数, 赋值到了一个SDL_RenderCommand 中相应的变量中,然后就是一系列的回调函数了, 再次要感叹一下SDL的团队了, 把C语言函数指针用的炉火纯青。

/**
 *  \brief Clear the current rendering target with the drawing color
 *
 *  This function clears the entire rendering target, ignoring the viewport and
 *  the clip rectangle.
 *
 *  \return 0 on success, or -1 on error
 */
extern DECLSPEC int SDLCALL SDL_RenderClear(SDL_Renderer * renderer);

SDL_RenderDrawRect在render上画一个矩形, 大家有心里有数, 这里的render 究竟是真的render还是texture冒充的

/**
 *  \brief Draw a rectangle on the current rendering target.
 *
 *  \param renderer The renderer which should draw a rectangle.
 *  \param rect A pointer to the destination rectangle, or NULL to outline the entire rendering target.
 *
 *  \return 0 on success, or -1 on error
 */
extern DECLSPEC int SDLCALL SDL_RenderDrawRect(SDL_Renderer * renderer,
                                               const SDL_Rect * rect);

SDL_RenderFillRect使用预设的颜色, 填充当前render上的一个矩形

/**
 *  \brief Fill a rectangle on the current rendering target with the drawing color.
 *
 *  \param renderer The renderer which should fill a rectangle.
 *  \param rect A pointer to the destination rectangle, or NULL for the entire
 *              rendering target.
 *
 *  \return 0 on success, or -1 on error
 */
extern DECLSPEC int SDLCALL SDL_RenderFillRect(SDL_Renderer * renderer,
                                               const SDL_Rect * rect);

SDL_RenderCopy把texture上srcrect 的内容 拷贝到 renderer上的dstrect上, 如果srcrect/dstrect为null, 则代表整个。

/**
 *  \brief Copy a portion of the texture to the current rendering target.
 *
 *  \param renderer The renderer which should copy parts of a texture.
 *  \param texture The source texture.
 *  \param srcrect   A pointer to the source rectangle, or NULL for the entire
 *                   texture.
 *  \param dstrect   A pointer to the destination rectangle, or NULL for the
 *                   entire rendering target.
 *
 *  \return 0 on success, or -1 on error
 */
extern DECLSPEC int SDLCALL SDL_RenderCopy(SDL_Renderer * renderer,
                                           SDL_Texture * texture,
                                           const SDL_Rect * srcrect,
                                           const SDL_Rect * dstrect);

代码实现

#include <iostream>
#include <SDL2/SDL.h>

#undef main
using namespace std;
int main()
{
    
    SDL_Window      *window = nullptr;
    SDL_Renderer    *render = nullptr;
    SDL_Texture     *texture = nullptr;
    SDL_Rect        rect;
    SDL_Event       event;

    SDL_Init(SDL_INIT_VIDEO);
    window = SDL_CreateWindow("sdl window", 30, 30, 700, 400, SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
    if (window == nullptr) {
    
        SDL_Log("create window failed\n");
        goto _EXIT;
    }
    render = SDL_CreateRenderer(window, -1, 0);
    if (render == nullptr) {
    
        SDL_Log("create render failed\n");
        goto _FREE_WINDOW;
    }

    texture =  SDL_CreateTexture(render, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_TARGET, 700, 400);
    if (!texture) {
    
        SDL_Log("create Texture failed\n");
        goto _FREE_RENDER;
    }

    rect.w = 10;
    rect.h = 10;
    while (1) {
    
        SDL_WaitEventTimeout(&event, 100);
        SDL_Log("event type = %d", event.type);
        if (event.type == SDL_QUIT) {
    
            SDL_Log("it will quit\n");
            break;
        }

        rect.x = rand() % 700;
        rect.y = rand() % 400;

        //Set a texture as the current rendering target.
        SDL_SetRenderTarget(render, texture);
        //Set the color used for drawing operations (Rect, Line and Clear)
        SDL_SetRenderDrawColor(render, 255, 155, 55, 255);
        //Clear the current rendering target with the drawing color
        SDL_RenderClear(render);

        //Draw a rectangle on the current rendering target
        SDL_RenderDrawRect(render, &rect);
        SDL_SetRenderDrawColor(render, 200, 50, 200, 150);
        //Fill a rectangle on the current rendering target with the drawing color
        SDL_RenderFillRect(render, &rect);

        SDL_SetRenderTarget(render, nullptr);
        //Copy a portion of the texture to the current rendering target.
        SDL_RenderCopy(render, texture, nullptr, nullptr);

        SDL_RenderPresent(render);
    }

    SDL_DestroyTexture(texture);
_FREE_RENDER:
    SDL_DestroyRenderer(render);
_FREE_WINDOW:
    SDL_DestroyWindow(window);
_EXIT:
    SDL_Quit();
    return 0;
}

因为结果是一个动态的, 本人也比较懒惰, 请大家自己运行, 看一下效果!

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/longjiang321/article/details/103438443

智能推荐

Python使用模块Pyserial模块报-程序员宅基地

文章浏览阅读440次。用pip安装pyserial后:sudo -H pip install pyserial,运行新建的程序,名称为serial.py,程序中用到:import serial.toos.list_ports,但总是提示ImportError:No module named tools.list_ports,在度娘一顿乱搜后,在stack overflow<http://stack..._"file \"c:\\projects\\salla-com\\com.py\", line 18, in from protocols.udp"

视觉与学习青年学者研讨会VALSE 2020线上大会简明日程发布_valse 官网-程序员宅基地

文章浏览阅读1.2k次。2020/07/27官网:http://valser.org/2020直播:https://live.bilibili.com/22300737VALSE 2020在线大会将于2020年7月31日-8月5日在线举办。大会共包含1个学生论坛,9个专题Workshop和11个重要主题年度进展回顾(APR),整整60位计算机视觉、模式识别与机器学习领域的优秀青年学者将依次登台,为大家呈上一场满汉全席式的饕餮盛宴。特别值得一提的是,VALSE隆重推出首次Student Webinar(学生论坛._valse 官网

解决 Springboot Unable to build Hibernate SessionFactory @Column命名不起作用_spring jpa 整合hibnate 报[persistenceunit: default] u-程序员宅基地

文章浏览阅读834次。jpa: hibernate: ddl-auto: createddl-auto:create----每次运行该程序,没有表格会新建表格,表内有数据会清空ddl-auto:create-drop----每次程序结束的时候会清空表ddl-auto:update----每次运行程序,没有表格会新建表格,表内有数据不会清空,只会更新ddl-auto:validate----运行程序会校验数据..._spring jpa 整合hibnate 报[persistenceunit: default] unable to build hibernat

2023秋招--游卡--游戏客户端--一面面经_游卡面试题-程序员宅基地

文章浏览阅读1.3k次。2023秋招--游卡--游戏客户端--一面面经_游卡面试题

spawn cmd ENOENT错误处理方法-程序员宅基地

文章浏览阅读4.5k次,点赞6次,收藏5次。点击此电脑–>高级系统设置–>环境变量–>用户变量–>点击Path -->点击新建–>添(C:\WINDOWS\system32)–>重启电脑即可_spawn cmd enoent

全国省市区SQL_全国sql-程序员宅基地

文章浏览阅读173次。数据来源:http://www.mca.gov.cn/article/sj/xzqh/2019/http://xzqh.mca.gov.cn/map该SQL取自2019年8月的版本(不包含港澳台的下级)截图如下:_全国sql

随便推点

Android之collection(集合)_android collections-程序员宅基地

文章浏览阅读3.9k次。Java集合框架的基本接口/类层次结构:[I]:接口 [C]:类 java.util.Collection [I]+--java.util.List [I] +--java.util.ArrayList [C] +--java.util.LinkedList [C] +--java.util.Vector [C] +--java.util.Stack ..._android collections

eslint安装配置-自动格式化_yoyo930021.vuter-程序员宅基地

文章浏览阅读461次。eslint安装配置-自动格式化1、推荐编辑器vscode,文件-首选项-设置-打开设置(json),添加代码{ "eslint.options": { "extensions": [ ".js", ".vue" ] }, "eslint.validate": [ "javascript", { "language": "vue", "autoFix": true }, "html", "_yoyo930021.vuter

回流与重绘_css font-size改变会导致重绘码-程序员宅基地

文章浏览阅读505次。高性能WEB开发-页面呈现、重绘、回流-页面呈现流程:(DOMSource->DOMTree)+(CSSSource->样式结构体)==>renderTree(呈现树)==>绘制页面_css font-size改变会导致重绘码

智能车仿真 —— 室外光电创意组(racecar车模仿真,阿克曼车辆模型)_仿真racecar尺寸-程序员宅基地

文章浏览阅读2.9k次,点赞5次,收藏15次。前言之前出了自己搭建的摄像头车模,平衡车车模,室外光电车模等,用的差速驱动多少有点瑕疵,这一次直接上阿克曼模型,完美还原赛车车模,速度上也有了提升,会重新搭建智能车系列教程,先上个视屏预热下。 智能车室外光电创意组仿真 这一周会提供教程链接,努力的码ing… …..._仿真racecar尺寸

#1 爬虫:豆瓣图书TOP250 「requests、BeautifulSoup」-程序员宅基地

文章浏览阅读565次。一、项目背景随着时代的发展,国人对于阅读的需求也是日益增长,既然要阅读,就要读好书,什么是好书呢?本项目选择以豆瓣图书网站为对象,统计其排行榜的前250本书籍。二、项目介绍本项目使用Python爬虫技术统计豆瓣图书网站上排名前250的书籍信息,包括书名、作者、出版社、出版日期、价格、评星、简述信息将获取到的信息存储在Mysql数据库中三、项目流程3.1 分析第一页...

package.json和package-lock.json的坑(区别)_vue项目 使用淘宝镜像 package-lock.json的版本与package.json的版本不-程序员宅基地

文章浏览阅读1.6k次。场景描述VUE项目里用到了前端iview插件,在package.json中添加依赖"dependencies": { "view-design": "^4.1.1",}SVN 项目拉取的package.json,版本是一致的,但是前台部分组件样式不一致。解析说明package.json用于指定依赖版本号。版本号说明:语义版本号分为 X.Y.Z 三位,分别代表主版本号、次版本号和补丁版本号。当代码变更时,版本号按以下原则更新 如果只是修复bug,需要更新Z位。 _vue项目 使用淘宝镜像 package-lock.json的版本与package.json的版本不一样

推荐文章

热门文章

相关标签