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 ...
随机推荐
- Python基本概念及零碎知识点
1.python面向对象 类和对象是面向对象编程的两个主要方面.类创建一个新类型,而对象这个类的实例:这类似于你有一个int类型的变量,这存储整数的变量是int类的实例(对象)把握一点:在python ...
- U3D刚体测试3(constraints)
程序这边的接口: mRigidbody.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.Freeze ...
- Uva 11584,划分成回文串
题目链接:https://uva.onlinejudge.org/external/115/11584.pdf 题意: 一个字符串,将它划分一下,使得每个串都是回文串,求最少的回文串个数. 分析: d ...
- git: fatal unable to auto-detect email address
参考:http://stackoverflow.com/questions/25671785/git-fatal-unable-to-auto-detect-email-address 正确使用命令: ...
- jquery简单插件到复杂插件(1)--tabs
写在前面,到了新公司开始转做前段,之前一直写php,一共写了半年,转过来,jq都用不好,但是还是得不断的学习,谁没菜过.从最简单的开始写,最近也在学习些html5的小游戏,加油吧.js原生写的可以说惨 ...
- JavaScript 中的Object的使用详解笔记(一)
昨天的学习笔记,今天更新上.第三遍看oop,还是理解的比较到位了. 1.JavaScript的组成:ECMAScript(最新6) + DOM + BOM 2.数组与对象的应用: object的基 ...
- FlashFXP命令行
flashfxp.exe -upload ftp://user:pass@ip:port -localpath="本地路径" -remotepath="远程FTP上的路 ...
- mvcAPI (入门 3)(源码)
mvcAPI (入门 3)(源码) http://download.csdn.net/detail/chunfang740/9078579
- C语言第4天循环,流程控制。
C语言第四天 :first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: 0; } img { borde ...
- Python if条件语句
1. if 条件语句 if 条件: 内容1 内容2 else: 内容3 内容4 # 一个等号是赋值(=) # 两个等号是比较(==) #不等号(!=) #if 条件1 and 条件2(并且) #if ...