SDL2.0的几何图行绘画
SDL2.0的几何图形绘画
通过SDL_Window、SDL_Renderer、SDL_Texture三者实现了简单的几何图形绘画。
包括了SDL_RenderDrawPoint、SDL_RenderFillRect、SDL_RenderDrawLine、SDL_SetRenderDrawColor等。
具体看代码吧(VS2012运行):
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <SDL2\SDL.h>
#include <SDL2\SDL_image.h>
#include <SDL2\ex\SDLex.h> SDL_Window *sdlWindow = NULL;
SDL_Renderer *sdlRender = NULL;
SDL_Texture *sdlTexture = NULL;
SDL srcRect;
int w = ;
int h = ; void DrawCircle(SDL_Renderer *ren,int radius){
int st=clock(),tx=,ty=radius,d=-(radius<<),x=radius,y=radius;
while(tx<ty){
for (int i=x-ty;i<=x+ty;++i){
SDL_RenderDrawPoint(ren,i,y-tx);
if (tx)
SDL_RenderDrawPoint(ren,i,y+tx);
}
if (d<)
d+=(tx<<)+;
else{
for (int i=x-tx;i<=x+tx;++i){
SDL_RenderDrawPoint(ren,i,y-ty);
SDL_RenderDrawPoint(ren,i,y+ty);
}
d+=((tx - ty)<<)+,ty--;
}
tx++;
}
if (tx==ty)
for (int i=x-ty;i<=x+ty;++i){
SDL_RenderDrawPoint(ren,i,y-tx);
SDL_RenderDrawPoint(ren,i,y+tx);
}
int en=clock();
} bool InitView(int width, int height, const char *iconName)
{
//初始化窗体
SDL_Init(SDL_INIT_VIDEO); sdlWindow = SDL_CreateWindow(
"The First SDL Program",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height,
SDL_WINDOW_RESIZABLE);
if (sdlWindow == NULL) return false; //加载窗体图标
SDL_Surface *iconSurface = IMG_Load(iconName);
if (iconSurface == NULL) return false; SDL_SetWindowIcon(sdlWindow, iconSurface); return true;
} bool InitDraw()
{
//加载渲染器
sdlRender = SDL_CreateRenderer(sdlWindow, -, );
if (sdlRender == NULL) return false; sdlTexture = SDL_CreateTexture(sdlRender, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
if (sdlTexture == NULL) return false;
//SDL_SetTextureBlendMode(sdlTexture, SDL_BLENDMODE_BLEND);
//SDL_SetRenderTarget(sdlRender, sdlTexture); return true;
} void UpdateDraw()
{
//设置背景颜色
SDL_SetRenderDrawColor(sdlRender, , , , 0xFF);
SDL_RenderClear(sdlRender); //左眼
SDL_SetRenderDrawColor(sdlRender, 0x00, 0xFF, 0xFF, 0xFF);
srcRect = SDLMake(w/*, h/, w/, h/);
SDL_RenderFillRect(sdlRender, &srcRect); //左眉毛
SDL_SetRenderDrawColor(sdlRender, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderDrawLine(sdlRender, srcRect.x, srcRect.y - , SDLMaxX(srcRect), srcRect.y - ); //右眼
SDL_SetRenderDrawColor(sdlRender, 0xFF, 0x00, 0xFF, 0xFF);
srcRect = SDLMake(w/*, srcRect.y, srcRect.w, srcRect.h);
SDL_RenderDrawRect(sdlRender, &srcRect); //右眉毛
SDL_SetRenderDrawColor(sdlRender, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderDrawLine(sdlRender, srcRect.x, srcRect.y - , SDLMaxX(srcRect), srcRect.y - ); //嘴巴
SDL_SetRenderDrawColor(sdlRender, 0xFF, 0xFF, 0x00, 0xFF);
srcRect = SDLMake(w/*, h/*, w/*, h/);
SDL_RenderFillRect(sdlRender, &srcRect); SDL_SetRenderDrawColor(sdlRender, 0xFF, 0x00, 0x00, 0xFF);
DrawCircle(sdlRender, ); SDL_RenderPresent(sdlRender);
} void Quit(int code)
{
const char *errMsg = SDL_GetError();
if (errMsg && strlen(errMsg)) {
SDL_Log("Error : %s\n", errMsg);
} //销毁窗口、渲染器、纹理
if (sdlWindow) SDL_DestroyWindow(sdlWindow);
if (sdlRender) SDL_DestroyRenderer(sdlRender);
if (sdlTexture) SDL_DestroyTexture(sdlTexture);
SDL_Quit();
exit(code);
} void HandleKeyEvent(const SDL_Keysym* keysym)
{
int key = keysym->sym;
switch(key)
{
case SDLK_ESCAPE:
Quit();
break;
case SDLK_SPACE:
break;
case SDLK_UP:
case SDLK_DOWN:
case SDLK_LEFT:
case SDLK_RIGHT:
int x, y;
SDL_GetWindowPosition(sdlWindow, &x, &y);
x = (key == SDLK_LEFT ? x- : (key == SDLK_RIGHT ? x+ : x));
y = (key == SDLK_UP ? y- : (key == SDLK_DOWN ? y+ : y));
SDL_SetWindowPosition(sdlWindow, x, y);
SDL_Log("x=%d, y=%d\n", x, y);
break;
case SDLK_KP_PLUS:
case SDLK_KP_MINUS:
w = (key == SDLK_KP_PLUS ? w+ : w-);
h = (key == SDLK_KP_PLUS ? h+ : h-);
SDL_SetWindowSize(sdlWindow, w, h);
SDL_Log("w=%d, h=%d\n", w, h);
break;
default:
break;
}
} void HandleEvents()
{
//Our SDL event placeholder.
SDL_Event event;
//Grab all the events off the queue.
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_KEYDOWN:
//Handle key Event
HandleKeyEvent(&event.key.keysym);
break;
case SDL_QUIT:
//Handle quit requests (like Ctrl-c).
Quit();
break;
}
}
} int main(int argc, char* argv[])
{
printf("可以通过↑↓←→+ -按键控制移动和大小\n");
if (InitView(w, h, "yp.ico") == false) {
SDL_Log("sdlWindow is null @_@\n");
Quit();
return -;
} if (InitDraw() == false) {
SDL_Log("Init Fail @_@\n");
Quit();
return -;
} //配置客户区大小
SDL_QueryTexture(sdlTexture,NULL, NULL, &w, &h);
SDL_SetWindowSize(sdlWindow, w + , h);
SDL_Log("w=%d, h=%d\n", w, h); while () {
HandleEvents();
UpdateDraw();
} SDL_DestroyWindow(sdlWindow);
SDL_Quit();
return ;
}
效果图:

SDL2.0的几何图行绘画的更多相关文章
- [原]零基础学习SDL开发之在Android使用SDL2.0显示BMP图
关于如何移植SDL2.0到安卓上面来参考我的上一篇文章:[原]零基础学习SDL开发之移植SDL2.0到Android 在一篇文章我们主要使用SDL2.0来加载一张BMP图来渲染显示. 博主的开发环境: ...
- SDL2.0的SDL_Event事件处理
SDL_Event事件集合 SDL_AudioDeviceEvent SDL_ControllerAxisEvent SDL_ControllerButtonEvent SDL_ControllerD ...
- [原]零基础学习SDL开发之在Android使用SDL2.0显示BMP叠加图
关于如何移植在android上使用SDL,可以参考[原]零基础学习SDL开发之移植SDL2.0到Android 和 [原]零基础学习SDL开发之在Android使用SDL2.0显示BMP图 . 在一篇 ...
- [原]如何在Android用FFmpeg+SDL2.0解码显示图像
如何在Android上使用FFmpeg解码图像参考文章[原]如何在Android用FFmpeg解码图像 ,如何在Android上使用SDL2.0来显示图像参考[原]零基础学习SDL开发之在Androi ...
- 最简单的基于FFMPEG+SDL的视频播放器 ver2 (採用SDL2.0)
===================================================== 最简单的基于FFmpeg的视频播放器系列文章列表: 100行代码实现最简单的基于FFMPEG ...
- 最简单的基于FFMPEG+SDL的音频播放器 ver2 (采用SDL2.0)
===================================================== 最简单的基于FFmpeg的音频播放器系列文章列表: <最简单的基于FFMPEG+SDL ...
- 最简单的基于FFMPEG+SDL的视频播放器 ver2 (采用SDL2.0)
===================================================== 最简单的基于FFmpeg的视频播放器系列文章列表: 100行代码实现最简单的基于FFMPEG ...
- 基于<最简单的基于FFMPEG+SDL的视频播放器 ver2 (采用SDL2.0)>的一些个人总结
最近因为项目接近收尾阶段,所以变的没有之前那么忙了,所以最近重新拿起了之前的一些FFMPEG和SDL的相关流媒体播放器的例子在看. 同时自己也用FFMPEG2.01,SDL2.01结合MFC以及网上罗 ...
- (转)SDL 1.2 to 2.0 Migration Guide--SDL1.2更新到SDL2.0指南
SDL 1.2 to 2.0 Migration Guide 目录 SDL 1.2 to 2.0 Migration Guide Translations Introduction Overview ...
随机推荐
- onActivityResult传值的使用
参考地址: http://wang-peng1.iteye.com/blog/632833 http://www.cnblogs.com/linjiqin/archive/2011/06/03/207 ...
- hiho(1081),SPFA最短路,(非主流写法)
题目链接:http://hihocoder.com/problemset/problem/1081 SPFA求最短路,是不应-羁绊大神教我的,附上头像. 我第一次写SPFA,我用的vector存邻接表 ...
- Python网络爬虫Scrapy框架研究 以及 代理设置
地址:https://github.com/yidao620c/core-scrapy 例子:https://github.com/geekan/scrapy-examples 中文翻译文档: htt ...
- P2679 子串
http://www.luogu.org/problem/show?pid=2679 题目描述 有两个仅包含小写英文字母的字符串 A 和 B.现在要从字符串 A 中取出 k 个互不重叠的非空子串,然后 ...
- Http 请求
public static string HttpGet(string url) { HttpWebRequest request = (HttpWebRequest)WebRequest.Creat ...
- CentOS系统识别NTFS分区的移动硬盘
第一步:下载rpmforge,下载对应的版本,就是对应CentOS版本,还有32位与64位也要对应上.rpmforge拥有4000多种CentOS的软件包,被CentOS社区认为是最安全也是最稳定的一 ...
- mysql 修改数据库的列
alter table tableName oldcolumn newcolumn datatype;
- 修改数据库表的schema,(表的[dbo.]前缀)
数据库使用过程中遇到这种问题,请看下图
- Topic Model
Topic Model 标签(空格分隔): 机器学习 \(\Gamma\)函数 \(\Gamma\)函数可以看做是阶乘在实数域上的推广,即: \(\Gamma(x) = \int_{0}^{+\inf ...
- [51NOD1230]幸运数(数位DP)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1230 dp(l,s,ss)表示长度为l的数各位和为s,各位平方 ...