DEV-C++ ege.h库入门

C++的ege.h库是一个简单的图形库引擎,其下载地址:

https://xege.org/download/ege20.08_all.7z

下载后,把相对应的 libgraphics.a 和 libgraphics64.a 放到 C++ 的文件夹里。

 把这三个文件

 放入 里。

打开DEV-C++,点开

 在下面输入 -lgraphics -lgdiplus -luuid -lmsimg32 -lgdi32 -limm32 -lole32 -loleaut32 -lwinmm -mwindows

其中 -mwindows 可加可不加,加了运行时会隐藏cmd窗口。

配置完成后,便可以用ege.h库写代码了。

#include<ege.h>
using namespace ege;
int main(){
    initgraph(640,480);
    getch();
    closegraph();
    return 0;
}

 用ege库时记得不要同时#include<conio.h>,会报错。

接下来让我们看一看这些函数声明。

inline void EGEAPI initgraph(int Width, int Height);                // 初始化画布
void EGEAPI closegraph();                                           // 关闭图形环境

initgraph中,Width代表窗口宽度,Height则表示窗口高度。

closegraph则用来关闭画布。

getch函数的意思差不多跟stdlib.h里的system("pause")一样,为了防止画布在一瞬间内关闭。

运行结果如下:

 按下任意一个键,窗口就关闭了。

接下来我们就能开始画图了。

#include<ege.h>
using namespace ege;
int main(){
	initgraph(640,480);
	circle(320,240,100);
	setcolor(RED);
	rectangle(220,140,420,340);
	setfillcolor(WHITE);
	bar(0,0,100,100);
	getch();
	closegraph();
	return 0;
}

这里又多了很多新的函数。

void EGEAPI circle(int x, int y, int radius, PIMAGE pimg = NULL);                                          // 画圆
void EGEAPI rectangle(int left, int top, int right, int bottom, PIMAGE pimg = NULL);   // 画矩形
void EGEAPI bar(int left, int top, int right, int bottom, PIMAGE pimg = NULL);                             // 画无边框填充矩形
void EGEAPI setcolor(color_t color, PIMAGE pimg = NULL);        // 设置当前绘图前景色
void EGEAPI setfillcolor(color_t color, PIMAGE pimg = NULL);    // 设置当前绘图填充色

而RED和WHITE是枚举体里的变量。

// 颜色
enum COLORS {
	BLACK           = EGERGB(0, 0, 0),
	BLUE            = EGERGB(0, 0, 0xA8),
	GREEN           = EGERGB(0, 0xA8, 0),
	CYAN            = EGERGB(0, 0xA8, 0xA8),
	RED             = EGERGB(0xA8, 0, 0),
	MAGENTA         = EGERGB(0xA8, 0, 0xA8),
	BROWN           = EGERGB(0xA8, 0xA8, 0),
	LIGHTGRAY       = EGERGB(0xA8, 0xA8, 0xA8),
	DARKGRAY        = EGERGB(0x54, 0x54, 0x54),
	LIGHTBLUE       = EGERGB(0x54, 0x54, 0xFC),
	LIGHTGREEN      = EGERGB(0x54, 0xFC, 0x54),
	LIGHTCYAN       = EGERGB(0x54, 0xFC, 0xFC),
	LIGHTRED        = EGERGB(0xFC, 0x54, 0x54),
	LIGHTMAGENTA    = EGERGB(0xFC, 0x54, 0xFC),
	YELLOW          = EGERGB(0xFC, 0xFC, 0x54),
	WHITE           = EGERGB(0xFC, 0xFC, 0xFC),
};

我们同样也可以用EGERGB来表示颜色。

setfillcolor(EGERGB(255,255,0));    //黄色

setfillcolor是设置填充颜色的,和setcolor还是有不一样的。

运行效果如下:

在看下面的代码:

#include<ege.h>
using namespace ege;
int main(){
	initgraph(640,480);
	circle(320,240,100);
	setcolor(RED);
	xyprintf(0,0,"I'm using the ege library!!!");
	getch();
	closegraph();
	return 0;
}

 xyprintf其实相当于ege的printf,x和y为坐标。

void EGEAPI xyprintf(int x, int y, LPCSTR  fmt, ...); // 在指定位置输出格式化字符串,指定绘图目标调用settarget
void EGEAPI xyprintf(int x, int y, LPCWSTR fmt, ...); // 在指定位置输出格式化字符串,指定绘图目标调用settarget

 setfont函数可以设置字体及其大小。

// 设置当前字体样式(详见帮助)
//      nHeight: 字符的平均高度;
//      nWidth: 字符的平均宽度(0 表示自适应);
//      lpszFace: 字体名称;
//      nEscapement: 字符串的书写角度(单位 0.1 度);
//      nOrientation: 每个字符的书写角度(单位 0.1 度);
//      nWeight: 字符的笔画粗细(0 表示默认粗细);
//      bItalic: 是否斜体;
//      bUnderline: 是否下划线;
//      bStrikeOut: 是否删除线;
//      fbCharSet: 指定字符集;
//      fbOutPrecision: 指定文字的输出精度;
//      fbClipPrecision: 指定文字的剪辑精度;
//      fbQuality: 指定文字的输出质量;
//      fbPitchAndFamily: 指定以常规方式描述字体的字体系列。
void EGEAPI setfont(int nHeight, int nWidth, LPCSTR lpszFace,  PIMAGE pimg = NULL);
void EGEAPI setfont(int nHeight, int nWidth, LPCWSTR lpszFace, PIMAGE pimg = NULL);
void EGEAPI setfont(int nHeight, int nWidth, LPCSTR lpszFace,  int nEscapement, int nOrientation,
					int nWeight, int bItalic, int bUnderline, int bStrikeOut, PIMAGE pimg = NULL);
void EGEAPI setfont(int nHeight, int nWidth, LPCWSTR lpszFace, int nEscapement, int nOrientation,
					int nWeight, int bItalic, int bUnderline, int bStrikeOut, PIMAGE pimg = NULL);
void EGEAPI setfont(int nHeight, int nWidth, LPCSTR lpszFace,  int nEscapement, int nOrientation,
					int nWeight, int bItalic, int bUnderline, int bStrikeOut, BYTE fbCharSet,
					BYTE fbOutPrecision, BYTE fbClipPrecision, BYTE fbQuality, BYTE fbPitchAndFamily, PIMAGE pimg = NULL);
void EGEAPI setfont(int nHeight, int nWidth, LPCWSTR lpszFace, int nEscapement, int nOrientation,
					int nWeight, int bItalic, int bUnderline, int bStrikeOut, BYTE fbCharSet,
					BYTE fbOutPrecision, BYTE fbClipPrecision, BYTE fbQuality, BYTE fbPitchAndFamily, PIMAGE pimg = NULL);
void EGEAPI setfont(const LOGFONTA *font, PIMAGE pimg = NULL); // 设置当前字体样式
void EGEAPI setfont(const LOGFONTW *font, PIMAGE pimg = NULL); // 设置当前字体样式
#include<ege.h>
using namespace ege;
int main(){
	initgraph(640,480);
	circle(320,240,100);
	setcolor(RED);
	setfont(10,5,"Fixedsys");
	xyprintf(0,0,"I'm using the ege library!!!");
	getch();
	closegraph();
	return 0;
}

以上就是ege库的基本用法,还有很多拓展函数没说到,下次再说啦。 

#include "graphics.h"
#include "ege/fps.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int width = 640, height = 480;

struct point //定义点,包含坐标,速度
{
    double x;
    double y;
    double dx;
    double dy;
    color_t color;                  //颜色
    color_t nextcolor, prevcolor;   //上一次的颜色,目标颜色
    int chtime, nowtime;            //过渡变化时间,当前时间
    int nextcolortime;              //距离一下次改变颜色的时间
};

struct poly //定义多边形,包含点的个数,和点数组
{
    int n_point;
    point p[20];
};

struct polys //定义多边形队列组
{
    int n_poly;                 //多边形队列长度
    poly p[100];                //多边形数组
};

double rand_float(double dv, double db) //返回一个db 到 db+dv之间的随机浮点数
{
    return randomf()*dv + db;
}

color_t getcolor(color_t prevcolor, color_t nextcolor, double t)
{
    if (t <= 0) return prevcolor;
    if (t >= 1) return nextcolor;
    color_t r, g, b;
    r = (color_t)(EGEGET_R(prevcolor) * (1 - t) + EGEGET_R(nextcolor) * t);
    g = (color_t)(EGEGET_G(prevcolor) * (1 - t) + EGEGET_G(nextcolor) * t);
    b = (color_t)(EGEGET_B(prevcolor) * (1 - t) + EGEGET_B(nextcolor) * t);
    if (r > 255) r = 255;
    if (g > 255) g = 255;
    if (b > 255) b = 255;
    return EGERGB(r, g, b);
}

void movepoint(struct point* b) //根据点的速度属性移动这个点,如果移出屏幕则进行反弹计算
{
    double dv = 1.0, db = 0.5;
    double tw = width / 640.0, th = height / 480.0;
    if (b->x <0) b->dx = rand_float(dv, db) * tw;
    if (b->y <0) b->dy = rand_float(dv, db) * th;
    if (b->x >width) b->dx = -rand_float(dv, db) * tw;
    if (b->y >height) b->dy = -rand_float(dv, db) * th;
    b->x += b->dx;
    b->y += b->dy;

    b->nowtime += 1;
    if (b->nowtime > b->chtime + b->nextcolortime)
    {
        b->nowtime = 0;
        b->prevcolor = b->nextcolor;
        b->nextcolor = hsv2rgb((float)random(360), 1.0f, 1.0f);
        b->chtime = random(1024) + 512;
        b->nextcolortime = random(1024) + 512;
    }
    b->color = getcolor(b->prevcolor, b->nextcolor, (double)b->nowtime / b->chtime);
}

void movepoly(struct poly* p) //移动单个多边形,内部调用点的移动
{
    int i;
    for (i=0; i<p->n_point; ++i)
    {
        movepoint(&(p->p[i]));
    }
}

void initpolys(struct polys* p, int npoly, int npoint) //初始化多边形队列组
{
    int i,j;
    p->n_poly = npoly;
    j = 0;
    p->p[j].n_point = npoint;
    for (i=0; i<npoint; ++i)
    {
        p->p[j].p[i].x = random(width);
        p->p[j].p[i].y = random(height);
        p->p[j].p[i].dx = (randomf() * 2 + 1);
        p->p[j].p[i].dy = (randomf() * 2 + 1);
        p->p[j].p[i].color = 0;
        p->p[j].p[i].prevcolor = 0;
        p->p[j].p[i].nextcolor = hsv2rgb((float)random(360), 1.0f, 0.5f);
        p->p[j].p[i].chtime = 1000;
        p->p[j].p[i].nowtime = 0;
        p->p[j].p[i].nextcolortime = 1000;
    }
    for (j=1; j<npoly; ++j)
    {
        p->p[j] = p->p[j-1];
    }
}

void draw_poly(struct poly* p) //绘制一个多边形
{
    ege_colpoint points[100];
    int i;
    for (i=0; i<p->n_point; ++i)
    {
        points[i].x = p->p[i].x;
        points[i].y = p->p[i].y;
        points[i].color = p->p[i].color;
    }
        points[i].x = p->p[0].x;
        points[i].y = p->p[0].y;
        points[i].color = p->p[0].color;
    //setcolor(color);
    fillpoly_gradient(p->n_point, points);
}

int main()
{
    static struct polys p[10] = {{0}};
    int n_points[10] = {3,3,3,6,7};
    int n_poly[10] = {1,1,1,1,1};
    int n_polys = 3, i;
    randomize();
    //图形初始化
    {
        setinitmode(1, 0, 0);
        initgraph(-1, -1);
        width  = getmaxx();
        height = getmaxy();
        setrendermode(RENDER_MANUAL);
    }
    //多边形对象初始化
    for (i=0; i< n_polys; ++i)
    {
        initpolys(&p[i], n_poly[i], n_points[i]);
    }
    fps ui_fps;
    //主循环
    for ( ; is_run(); delay_fps(60))
    {
        if (kbhit() > 0) //有按键按下就退出
        {
            break;
        }
        for (i=0; i< n_polys; ++i)
        {
            movepoly(p[i].p);
        }
        cleardevice();
        for (i=0; i< n_polys; ++i)
        {
            draw_poly(p[i].p);
        }
        //imagefilter_blurring(NULL, 0xff, 0x100);
    }
    closegraph();
    return 0;
}

下一期:DEV-C++ ege.h库进阶

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
以下是一个使用EGE绘图Dev-C++中编写的烟花程序的代码: ```c #include <graphics.h> #include <time.h> #include <stdlib.h> int main() { initgraph(640, 480); // 初始化绘图窗口 srand(time(NULL)); // 设置随机数种子 while (1) // 循环绘制烟花 { int x = rand() % 640; // 随机生成烟花的x坐标 int y = rand() % 480; // 随机生成烟花的y坐标 int r = rand() % 255; // 随机生成烟花的颜色 int g = rand() % 255; int b = rand() % 255; setlinecolor(RGB(r, g, b)); // 设置烟花的颜色 for (int i = 0; i < 100; i++) // 循环绘制烟花的尾巴 { int dx = rand() % 21 - 10; // 随机生成尾巴的x方向偏移量 int dy = rand() % 21 - 10; // 随机生成尾巴的y方向偏移量 line(x, y, x + dx, y + dy); // 绘制尾巴 delay(10); // 延迟一段时间,形成动画效果 } setfillcolor(RGB(r, g, b)); // 设置烟花的颜色 fillcircle(x, y, 5); // 绘制烟花的中心点 for (int i = 0; i < 50; i++) // 循环绘制烟花的爆炸效果 { int dx = rand() % 101 - 50; // 随机生成爆炸效果的x方向偏移量 int dy = rand() % 101 - 50; // 随机生成爆炸效果的y方向偏移量 setfillcolor(RGB(r, g, b)); // 设置爆炸效果的颜色 solidcircle(x + dx, y + dy, 2); // 绘制爆炸效果 delay(10); // 延迟一段时间,形成动画效果 } } closegraph(); // 关闭绘图窗口 return 0; } ``` 请注意,此代码需要在Dev-C++配置EGE绘图才能正常运行。具体配置方法可以参考引用中提供的链接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值