在上文 SDL 开发实战(二):SDL 2.0 核心 API 解析 我们讲解了SDL最核心的API,并结合Hello World代码了解了SDL渲染画面的基本原理。

本文我们来讲一下,如何使用SDL的API绘制基本的图形。

SDL中绘制基本图形的 API并不多,主要是 点、线、矩形、填充矩形。其它图形都可以通过 点、线、矩形组合出来。

0、设置画笔的颜色

设置画笔颜色是绘制图形的首要步骤,函数方法为SDL_SetRenderDrawColor,下面是函数的原型:

int SDL_SetRenderDrawColor(SDL_Renderer* renderer,   // 渲染器
Uint8 r, // 红
Uint8 g, // 绿
Uint8 b, // 蓝
Uint8 a) // 透明值

设置好画笔的颜色后,我们就可以开始绘制了我们想要的图形了,如果需要修改绘制颜色的色值,可以再次调用此函数即可。

1. 绘制一个点

我们可以使用SDL_RenderDrawPoint函数可以绘制一个点,下面是函数的原型:

int SDL_RenderDrawPoint(SDL_Renderer* renderer, int x, int y)

渲染器为参数renderer,绘制的坐标为x,y。

2. 绘制多个点

我们可以使用SDL_RenderDrawPoints函数绘制多个点,下面是函数的原型:

int SDL_RenderDrawPoints(SDL_Renderer* renderer, const SDL_Point* points, int count)

其中points为绘制的点的数组,count为要绘制的点的个数。

3. 绘制直线

我们可以使用SDL_RenderDrawLine函数绘制一条之间,下面是函数的原型:

int SDL_RenderDrawLine(SDL_Renderer* renderer,  // 渲染器
int x1, // 端点1的x坐标
int y1, // 端点1的y坐标
int x2, // 段点2的x坐标
int y2) // 端点2的y坐标

渲染器为参数renderer,两个端点的坐标为(x1,y1)和(x2,y2)。

4. 绘制多条线

我们可以使用SDL_RenderDrawLines函数绘制多条直线,下面是函数的原型:

int SDL_RenderDrawLines(SDL_Renderer* renderer, const SDL_Point* points, int count)

该函数会将使用两个相邻的点之间进行连线。最终画出你想画的图形。如画三角形,多边形或圆形。

5. 绘制矩形

我们可以使用SDL_RenderDrawRect函数绘制矩形,下面是函数的原型:

int SDL_RenderDrawRect(SDL_Renderer* renderer, const SDL_Rect* rect)

rect: 是要绘制的一块区域。它包括x,y,w,h这些元素。

6. 填充矩形

我们可以使用SDL_RenderFillRect函数绘制矩形,下面是函数的原型:

int SDL_RenderFillRect(SDL_Renderer* renderer, const SDL_Rect* rect)

使用指定的色彩填充一块矩形。rect: 是要绘制的一块区域,包括x,y,w,h这些元素。

7. 填充多块矩形

我们可以使用SDL_RenderFillRect函数绘制矩形,下面是函数的原型:

int SDL_RenderDrawRects(SDL_Renderer* renderer, const SDL_Rect* rects, int count)

其中 rects 为矩形数组,count为矩形个数。

实战

代码:

// SDL.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// #include "pch.h"
#include <iostream> extern "C" {
#include "SDL.h"
} #define POINTS_COUNT 4 static SDL_Point points[POINTS_COUNT] = {
{, },
{, },
{, },
{, }
}; static SDL_Rect bigrect = { ,,, }; int main(int argc, char* argv[])
{
int flag = ; SDL_Window *window; // Declare a pointer
SDL_Renderer *renderer; SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2 // Create an application window with the following settings:
window = SDL_CreateWindow(
"SDL2 Draw Window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
, // width, in pixels
, // height, in pixels
SDL_WINDOW_SHOWN // flags - see below
); // Check that the window was successfully created
if (window == NULL) {
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return ;
} /* We must call SDL_CreateRenderer in order for draw calls to affect this window. */
renderer = SDL_CreateRenderer(window, -, ); /* Select the color for drawing. It is set to red here. */
SDL_SetRenderDrawColor(renderer, , , , ); /* Clear the entire screen to our selected color. */
SDL_RenderClear(renderer); SDL_SetRenderDrawColor(renderer, , , , SDL_ALPHA_OPAQUE); SDL_RenderDrawLines(renderer, points, POINTS_COUNT); SDL_Rect rect = { , , , };
SDL_RenderDrawRect(renderer, &rect); SDL_SetRenderDrawColor(renderer, , , , );
SDL_RenderFillRect(renderer, &rect); SDL_SetRenderDrawColor(renderer, , , , );
SDL_RenderFillRect(renderer, &bigrect); /* Up until now everything was drawn behind the scenes.
This will show the new, red contents of the window. */
SDL_RenderPresent(renderer); // The window is open: could enter program loop here (see SDL_PollEvent())
SDL_Delay(); // Pause execution for 5000 milliseconds, for example //destory renderer
if (renderer) {
SDL_DestroyRenderer(renderer);
} // Close and destroy the window
SDL_DestroyWindow(window); // Clean up
SDL_Quit();
return ; }

运行效果:

SDL 开发实战(三):使用 SDL 绘制基本图形的更多相关文章

  1. 【C语言探索之旅】 第三部分第一课:SDL开发游戏之安装SDL

    内容简介 1.课程大纲 2.第三部分第一课: SDL开发游戏之安装SDL 3.第三部分第二课预告: SDL开发游戏之创建窗口和画布 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会 ...

  2. SDL 开发实战(二):SDL 2.0 核心 API 解析

    在上一篇文章 SDL 开发实战(一):SDL介绍及开发环境配置 中,我们配置好了SDL的开发环境,并成功运行了SDL的Hello World 代码.但是可能大部分人还是读不太明白具体Hello Wol ...

  3. SDL 开发实战(五): SDL 纹理渲染

    本文我们讲一下如何使用SDL_Texture将视频纹理渲染出来. 1. SDL 视频渲染相关对象 SDL 视频渲染主要涉及到四个对象:SDL_Window.SDL_Render.SDL_Texture ...

  4. SDL 开发实战(七): SDL 多线程与锁机制

    为什么要用多线程?在音视频领域主要是实现音视频同步.实现了音视频同步,我们的播放器就基本上合格了. 这里我们将讲解一下SDL的多线程与锁机制. 多线程的好处主要是能使程序更加充分利用硬件(主要是CPU ...

  5. SDL 开发实战(一):SDL介绍及开发环境配置

    一.什么是SDL? SDL是 “Simple DirectMedia Layer”的缩写,SDL是一个开源的跨平台的多媒体库,封装了复杂的音视频底层操作,简化了音视频处理的难度. SDL使用C语言写成 ...

  6. SDL 开发实战(七): 使用 SDL 实现 PCM播放器

    在上文,我们做了YUV播放器,这样我们就入门了SDL播放视频.下面我们来做一个PCM播放,即使用SDL播放PCM数据. 下面说明一下使用SDL播放PCM音频的基本流程,主要分为两大部分:初始化SDL. ...

  7. SDL 开发实战(六): 使用 SDL 实现 YUV 播放器

    前面铺垫了这么多,现在终于进入核心的主题了,那就是使用SDL播放视频,本节我们将使用SDL播放YUV视频,也就是做一个YUV播放器. 下面说明一下使用SDL播放YUV视频的基本流程,主要分为两大部分: ...

  8. SDL 开发实战(四): SDL 事件处理

    在前面学习SDL的例子运行时,我们发现我们的窗口只停留了几秒,但是如果设置更长时间显然也有其他的弊端. 那么有没有一种好的办法可以解决这个问题呢?例如:能不能让窗口一直显示,直到检测到用户用鼠标点击关 ...

  9. spring-cloud-square开发实战(三种类型全覆盖)

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 前文<五分钟搞懂spring-clou ...

随机推荐

  1. 记录一次go性能调试的过程

    安装 go, git, go-torch 备注, go1.11后, 集成了 go-torch

  2. 用 pdf.js兼容部分安卓显示PDF在线预览 时,a标签直接链接参数文件不能含中文的解决办法

    例子: 项目部署在 Tomcat 上的: <a href="../generic/web/viewer.html?file=doc/register/要显示的文件.pdf" ...

  3. react安装 项目构建

    1.nodejs安装 下载安装包,解压.如果是已编译文件,在/etc/profile中设置PATH(/etc/profile文件中的变量设置,所有用户可用,但需求重启服务器),并source /etc ...

  4. Spark SQL官网阅读笔记

    Spark SQL是Spark中用于结构化数据处理的组件. Spark SQL可以从Hive中读取数据. 执行结果是Dataset/DataFrame. DataFrame是一个分布式数据容器.然而D ...

  5. RxJS操作符(三)

    一.过滤类操作符:debounce, debounceTime 跟时间相关的过滤 debounceTime自动完成:性能,避免每次请求都往出发 ); debounce中间传入Observable co ...

  6. CentOS 7安装OpenCV 3.3.1

    1.CentOS具体版本: 2.安装步骤: 1).安装依赖: sudo yum groupinstall "Development Tools" -y sudo yum insta ...

  7. youtube去广告

    https://www.digitbin.com/youtube-ads-block/ 1. OGYouTube | Mod AdBlocker YouTube OGYouTube App is a ...

  8. Final Cut Pro X for Mac(FCPX专业视频剪辑工具)实用技巧篇!

    Final Cut Pro X for Mac是一款非常强大的专业视频剪辑工具,全球很多人都在用!现在小编给大家带来一些关于Final Cut Pro X for Mac的使用技巧,希望对大家以后的应 ...

  9. UOJ#346. 【清华集训2017】某位歌姬的故事 动态规划

    原文链接www.cnblogs.com/zhouzhendong/p/UOJ346.html 题解 首先按照 $m_i$ 的大小排个序. 如果某一个区间和一个 m 值比他小的区间有交,那么显然可以将这 ...

  10. afx.h(78): fatal error C1083: 无法打开包括文件: “new.h”: No such file or directory

    vs2015新建mfc工程,编译错误: D:\program files (x86)\microsoft visual studio 14.0\vc\atlmfc\include\afx.h(78): ...