翁恺老师课程上的一个教学用的图形库。源码地址:https://github.com/wengkai/ACLLib。
开始
一个基本的框架
#include "acllib.h"
int Setup()
{
initWindow("小游戏", 100, 100, 400, 400);//初始化窗口
initConsole();//开启终端
beginPaint();//绘图开始,绘图必须这样调用
line(20, 20, 100, 100);//各种绘图
endPaint();//绘图结束
return 0;
}
监控鼠标键盘事件相关函数
概述
//定义回调函数的类型
typedef void(*KeyboardEventCallback) (int key, int event);//键盘事件
typedef void(*CharEventCallback) (char c);//输入字符事件
typedef void(*MouseEventCallback) (int x, int y, int button, int event);//鼠标事件,包括移动,左单双击,右单双击,滚轮,滚轮单击等
typedef void(*TimerEventCallback) (int timerID);//定时器
//注册函数
void registerKeyboardEvent(KeyboardEventCallback callback);
void registerCharEvent(CharEventCallback callback);
void registerMouseEvent(MouseEventCallback callback);
//定时器相关
void registerTimerEvent(TimerEventCallback callback);
void startTimer(int timerID, int timeinterval);
void cancelTimer(int timerID);
示例-可以示例观察
#include "acllib.h"
#include <stdio.h>
void cha(char c)
{
printf("%c\n", c);
}
void key(int key, int event)
{
printf("key = %d, event = %d\n", key, event);
}
void mouse(int x, int y, int button, int event)
{
printf("x = %d, y = %d, button = %d, event = %d \n", x, y, button, event);
}
void timer(int timeID)
{
printf("timeID = %d\n", timeID);
}
int Setup()
{
initWindow("小游戏", 100, 100, 400, 400);
initConsole();
registerCharEvent(cha);//注册输入字符监听函数
registerKeyboardEvent(key);//注册键盘监听函数
registerMouseEvent(mouse);//注册鼠标监听函数
registerTimerEvent(timer);//注册定时器的监听函数
//开始两个定时器
startTimer(0, 1000);
startTimer(1, 2000);
beginPaint();
line(20, 20, 100, 100);
endPaint();
return 0;
}
窗口相关
initWindow()-初始化窗口
原型
void initWindow(const char title[], int left, int top, int width, int height);
//title 窗口左上角的标题
//left横坐标,距离左边的距离
//top 纵坐标,距离上边的距离
//width 窗口的宽度
//height 窗口的高度
窗口的坐标示例
msgBox()-消息弹窗
原型
void msgBox(const char title[], const char text[], int flag);
//tilte 标题
//text 内容
//flag 弹窗的按钮的样式,比如可能是一个确认,可能是确认和取消。从0-n可以挨着试试,这个函数是对win32API中MessageBox()的封装
clearDevice()-清除所有的图形
原型
void clearDevice(void);
该函数也是必须在beginPaint()和endPaint()函数之间
getWidth()和getHeight()-获取窗口的大小
原型
int getWidth();
int getHeight();
媒体相关
音频相关
原型
void loadSound(const char *fileName, ACL_Sound *pSound);
//加载音频
//filename 路径字符串,可以是相对路径,从项目根目录开始
//pSound 音频编号,用来区别不同的音频,后面的函数都是使用这个编号来操作
void playSound(ACL_Sound soundID, int repeat);
//repeat 重复次数,0就是不重复,仅播放一次
void stopSound(ACL_Sound soundID);
//停止播放
测试的时候遇到下面一个大坑
错误信息:libpng warning: iCCP: cHRM chunk does not match sRGB。原因是QQ输入法,切换输入法即可避免这个错误。
图片相关
原型
void loadImage(const char *pImageFileName, ACL_Image *pImage);
//加载图片资源,测试的遇到一个大坑,图片的格式是有要求的,找了半天。教学用的框架,并没有做的很完善。
//The stream must be in BMP (bitmap), WMF (metafile), or ICO (icon) format.
void freeImage(ACL_Image *pImage);
//释放资源
//下面这三个参数就得在begionPaint()和endPaint()之间使用了,目的是在窗口上显示图片
void putImage(ACL_Image *pImage, int x, int y);
//将图片按照原尺寸放到窗口内
void putImageScale(ACL_Image *pImage, int x, int y, int width, int height);
//将图片按照指定的大小放到窗口内
void putImageTransparent(ACL_Image *pImage, int x, int y, int width, int height, ACL_Color bkColor);
//可以设置图片的透明度,不过这里的透明度设置有点复杂,ACL_Color是RGB。简单的说,bkColor指定的是不进行渲染的颜色。
绘制图形
绘制图形相关的函数必须在,beginPaint()和endPaint()之间
beginPaint()和endPaint()
原型
void beginPaint();
void endPaint();
绘制图形相关的函数必须在,beginPaint()和endPaint()之间
beginPaint();
line(20, 20, 100, 100);
endPaint();
文字相关
void setTextColor(ACL_Color color);
//设置问文字颜色
void setTextBkColor(ACL_Color color);
//设置文字背景色
void setTextSize(int size);
//设置文字大小
void setTextFont(const char *pFontName);
//设置文字字体,pFontName指的是字体的名字,不是字体的路径,有些字体是不能用的
void paintText(int x, int y, const char *pStr);
//渲染
光标相关
设置闪烁的光标的位置
void setCaretSize(int w, int h);
//光标的长宽
void setCaretPos(int x, int y);
//光标的位置
void showCaret();
//显示光标
void hideCaret();
//隐藏光标
像素点相关
void putPixel(int x, int y, ACL_Color color);
//设置一个带颜色的像素点
//ACL_Color 在我当前环境 就是一个long。另外还有RGB这个宏可以用
ACL_Color getPixel(int x, int y);
//返回一个颜色值,比如rgb(1,2,3)返回197121,等于0X30201
线相关
圆弧
void arc(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \
int nXStartArc, int nYStartArc, int nXEndArc, int nYEndArc);
//这个函数真的很无语,没点几何能力直接看不明白,简单说明一下
前四个坐标,两对(x,y),都是从原点出发,分别代表一个矩形的左上点和右下点。然后然后在这个矩形中,画一个内切的圆,有可能是椭圆也有可能是圆形。
示例:
arc(10,10,200,100, 0,0,0,0);
//这样会得到一个椭圆
后四个坐标,两对(x,y),从上边得到的圆的圆心出发,分别连接这两对坐标,得到两条直线,逆时针取上述椭圆的一部分。
arc(10,10,200,100, 10,10,10,100);
//圆的一部分
arc(10,10,200,100, 10,100,10,10);
//调换一下坐标,得到另外一部分
直线
一个起点,一个终点,得到一条直线。这里的起点和终点均能单独设置。
设置起点
int getX(void);//获取x坐标
int getY(void);//获取x坐标
void moveTo(int x, int y);//将起点移动到(x,y),绝对坐标
void moveRel(int dx, int dy);//将起点从当前位置移动(x,y),相对坐标,相对于当前位置
设置终点
void lineTo(int nXEnd, int nYEnd);//终点绝对坐标,同时将起点设置为该坐标
void lineRel(int dx, int dy);//相对于起点
直接画一条直线
void line(int x0, int y0, int x1, int y1);
//四个坐标,两对(x,y),分别是直线的起点和终点
示例
int Setup()
{
int i = 0;
initConsole();
int x, y;
initWindow("小游戏", 100, 100, 400, 400);
beginPaint();
for(i=10; i<100; i+=20) {
lineTo(i,10);
//重置起点
moveTo(0,0);
printf("x = %d, y = %d\n", getX(), getY());
}
endPaint();
return 0;
}
曲(折)线
void polyBezier(const POINT *lppt, int cPoints);
//贝塞尔曲线,比较复杂,没弄懂
//lppt POINT的数组
//cPoints lppt的个数
void polyLine(const POINT *lppt, int cPoints);
//折线,将lppt中的点连起来
//lppt POINT的数组
//cPoints lppt的个数
如何设置线的样式
线是用钢笔画的,所以是pen
void setPenColor(ACL_Color color);//设置颜色
void setPenWidth(int width);//设置宽度
void setPenStyle(ACL_Pen_Style style);//设置样式
typedef enum
{
PEN_STYLE_SOLID,
PEN_STYLE_DASH, /* ------- */
PEN_STYLE_DOT, /* ....... */
PEN_STYLE_DASHDOT, /* _._._._ */
PEN_STYLE_DASHDOTDOT, /* _.._.._ */
PEN_STYLE_NULL
} ACL_Pen_Style; //支持的样式
图形相关
各种形状
void chrod(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \
int nXRadial1, int nYRadial1, int nXRadial2, int nYRadial2);
//类似于线中arc()
void ellipse(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
//椭圆
void pie(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \
int nXRadial1, int nYRadial1, int nXRadial2, int nYRadial2);
//饼图
void polygon(const POINT *lpPoints, int nCount);
//多边形
void rectangle(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
//矩形
void roundrect(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \
int nWidth, int nHeight);
//带倒角的矩形
如何改变填充的样式
void setBrushColor(ACL_Color color);
void setBrushStyle(ACL_Brush_Style style);
typedef enum
{
BRUSH_STYLE_SOLID = -1,
BRUSH_STYLE_HORIZONTAL, /* ----- */
BRUSH_STYLE_VERTICAL, /* ||||| */
BRUSH_STYLE_FDIAGONAL, /* \\\\\ */
BRUSH_STYLE_BDIAGONAL, /* ///// */
BRUSH_STYLE_CROSS, /* +++++ */
BRUSH_STYLE_DIAGCROSS, /* xxxxx */
BRUSH_STYLE_NULL
} ACL_Brush_Style;