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 ...
随机推荐
- Find them, Catch them
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36488 Accepted: 111 ...
- InheritableThreadLocal原理
转载:https://github.com/pzxwhc/MineKnowContainer/issues/20 介绍 InheritableThreadLocal 之前,假设对 ThreadLoca ...
- WebForm在JS中从Dropdownlist添加数据,在C#段读取
(1)页面设置和JS代码 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Def ...
- centOS6.5安装SUN-jdk7
一.下载SUN-JDK1.7.tar.gz文件他 地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1 ...
- react学习
http://www.cnblogs.com/yunfeifei/ http://wiki.jikexueyuan.com/project/react-native/native-ui-compone ...
- c++学习总结--复合类型,const。
复习c++,有必要对一些东西做一下笔记,方便以后学习,如有问题,欢迎提出. 首先,最常用两种复合类型应该是引用和指针. 所谓复合类型,是在基本类型基础(如:int)上定义的类型. 引用是c++的一个复 ...
- C语言第2天基本运算
getchar 一.首先给出<The_C_Programming_Language>这本书中的例子: #include <stdio.h> int main( ) { ...
- Android开发开始--环境搭建
一.搭建Android开发环境 1.JDK (Java Development Kit) 2.Eclipse 3.Android SDK (Software Development Kit) 4.AD ...
- 【转】Ajax中send方法参数的使用(get/post)
Ajax中send方法参数的使用 一般情况下,使用Ajax提交的参数多是些简单的字符串,可以直接使用GET方法将要提交的参数写到open方法的url参数中,此时send方法的参数为null. 例如 : ...
- GZFramwork快速开发框架演练之会员系统(一)框架源码下载
GZFramwork框架开发环境为.NET 4.0 (必须) VS2013+SQL2005+DevExpress v13.2.8+FastReport (推荐) 数据库建模工具PowerDesi ...