简介

这个头文件是用来在C++控制台绘图的,借鉴了 pygame 的一些思想,提供了一些函数用来绘图,可以有效的避免多次绘制重复图像时屏闪等问题。

函数介绍

1、init(int x, int fz)

初始化屏幕, 将屏幕大小设定为 x , 字体大小为 fz 。

2、fill(concol color)

用来将整个屏幕填充为一定的颜色。

3、update()

注意!绘画完后的图像并不会立刻出现在控制台上,使用这个函数来更新。

4、getmousepos(pos & p)

获取鼠标坐标并存到 p 中

5、gt(int x, int y)

将控制台光标移到(x,y)的位置,x,y 从1开始

6、HideCursor()

隐藏控制台光标

7、settextcolor(concol color)

设置文字颜色

8、setbackcolor(concol color)

设置背景颜色

9、rect(int sx, int sy, int ex, int ey, concol color)

以(sx, sy)为左上端点、(ex, ey)为右下端点,绘制颜色为color的矩形。

10、line(int sx, int sy, int ex, int ey, concol color)

绘制一条从(sx, sy)到(ex, ey)的线。

11、dot(int x, int y, concol color)

在(x,y)的位置画一个点

先介绍到这里

里面更多的功能、函数会在以后的文章后详细解释。

示例代码

我这里将头文件命名为 Drawer.h ,如果用了别的名字保存头文件,这里也要改过来。

功能是从(1,1)到鼠标的位置画线。

#include "Drawer.h"
using namespace std;Pos mp; // Mouse_Posvoid game()
{while(1){getmousepos(mp); // Get Mouse Posfill(black); // Let Screen be blackline(1, 1, mp.x, mp.y, green); // from (1, 1) to (mp.x, mp.y) draw Line//                        ^//                        |// You can change this to red, blue, white or moreupdate(); // Update Screen}
}int main(){system("mode con cols=102 lines=52");init(50, 16);// 50x50 Screen and font size is 16game();
}

示例图片

截屏截不到鼠标。。。

头文件代码

#include <windows.h>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#ifndef KEY_DOWN
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
#endif
#define clean 0x2
#define box 0x4
#define full 0x1
using namespace std;enum concol {black = 0,dark_blue = 1,dark_green = 2,dark_aqua = 3, dark_cyan = 3,dark_red = 4,dark_purple = 5, dark_pink = 5, dark_magenta = 5,dark_yellow = 6,dark_white = 7,gray = 8,blue = 9,green = 10,cyan = 11,red = 12,purple = 13, pink = 13,yellow = 14,white = 15
};struct pixel {concol color;
};struct Pos {int x, y;
};struct Pos3d
{int x, y, z ;
};CONSOLE_CURSOR_INFO CursorInfo;
COORD _GoToPos;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
HWND hwnd = GetForegroundWindow();
int backcol, textcol;
pixel _LastScreen[1000][1000];
pixel _NewScreen[1000][1000];
int _ScreenSideLength;
int Py3dx = 0, Py3dy = 0 ;
int xzlen = 2, yzlen = 1 ;
int fontsize = 16;inline void getmousepos(Pos &p) {POINT pt;GetCursorPos(&pt);ScreenToClient(hwnd, &pt);p.y = (pt.y / fontsize + 0.5);p.x = (pt.x / fontsize + 0.5);
}inline void gt(short x, short y) {--x;--y;_GoToPos = {x * xzlen, y * yzlen};SetConsoleCursorPosition(hOut, _GoToPos);
}inline void HideCursor()
{GetConsoleCursorInfo(hOut, &CursorInfo);CursorInfo.bVisible = false;SetConsoleCursorInfo(hOut, &CursorInfo);
}inline void settextcolor(concol textcolor) {textcol = textcolor;unsigned short wAttributes = ((unsigned int)backcol << 4) | (unsigned int)textcol;SetConsoleTextAttribute(hOut, wAttributes);
}inline void setbackcolor(concol backcolor) {hOut = GetStdHandle(STD_OUTPUT_HANDLE);backcol = backcolor;unsigned short wAttributes = ((unsigned int)backcol << 4) | (unsigned int)textcol;SetConsoleTextAttribute(hOut, wAttributes);
}struct Button {Pos mp;const char* name;int len, qx, px, py;void rename(const char* ne) {name = ne;len = strlen(ne);}void setpos(int x, int y) {py = y;qx = (x - len / 2);px = (x + len / 2);gt(qx, y);printf(name);}bool check() {getmousepos(mp);if (mp.x <= px and mp.x >= qx and mp.y <= py and mp.y >= py - 2) {gt(qx - 2, py);printf(">>");gt(px, py);printf("<<");if (KEY_DOWN(MOUSE_MOVED)) return true;} else {gt(qx - 2, py);printf("  ");gt(px, py);printf("  ");}return false;}
};inline void init(int x, int fz) {fontsize = fz;GetConsoleCursorInfo(hOut, &CursorInfo);CursorInfo.bVisible = false;SetConsoleCursorInfo(hOut, &CursorInfo);_ScreenSideLength = x;
}inline void update() {for (int i = 1; i <= _ScreenSideLength ; ++i) {for (int j = 1; j <= _ScreenSideLength ; ++j) {if (_LastScreen[j][i].color != _NewScreen[j][i].color) {gt(j, i);setbackcolor(_NewScreen[j][i].color);for(int i=1;i<=yzlen;++i){for(int j=1;j<=xzlen;++j)putchar(' ');puts("\n");}        _LastScreen[j][i] = _NewScreen[j][i];}}}
}void fill(concol color) {for (int i = 1; i <= _ScreenSideLength; ++i) {for (int j = 1; j <= _ScreenSideLength; ++j) {if (_NewScreen[j][i].color != color) {_NewScreen[j][i].color = color;}}}
}void rect(int sx, int sy, int ex, int ey, concol color) {if (ey < sy) swap(ey, sy);if (ex < sx) swap(ex, sx);for (int i = sy; i <= ey; ++i) {for (int j = sx; j <= ex; ++j) {_NewScreen[j][i].color = color;}}
}void ChangePy(int x, int y)
{Py3dx = x;Py3dy = y;
}inline void dot(int x, int y, concol color) {_NewScreen[x][y].color = color;
}inline void line(int sx, int sy, int ex, int ey, concol color)
{int xlen = ex - sx ;int ylen = ey - sy ;int len = sqrt(pow(xlen, 2) + pow(ylen, 2)) ;for(double i=0; i<=len; i += 1){int x = xlen * i / len ;int y = ylen * i / len ;_NewScreen[x + sx][y + sy].color = color ;}
}inline Pos pos3t2(Pos3d pos)
{if(pos.z == 0){return Pos {pos.x, pos.y} ;}return Pos{pos.x / (log10(pos.z)) + Py3dx, pos.y / (log10(pos.z)) + Py3dy} ;
}inline void line3d(Pos3d a, Pos3d b, concol color)
{Pos a2 = pos3t2(a);Pos b2 = pos3t2(b);
//  line(a2.x, b2.y, a2.y, b2.x, color);line(a2.x, a2.y, b2.x, b2.y, color);
}inline int dis3d(Pos3d a, Pos3d b)
{int x = abs(a.x - b.x);int y = abs(a.y - b.y);int z = abs(a.z - b.z);return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2)) ;
}/*
Q------W\    /A--S
*/
inline void rect3d(Pos3d q, Pos3d w, Pos3d a, Pos3d s, concol color)
{int qlen = dis3d(q, a);int plen = dis3d(w, s);int len = max(qlen, plen);for(double i=1;i<=len; i+=0.1){Pos3d qpos = Pos3d {abs(q.x-a.x)*i/len + q.x, abs(q.y-a.y)*i/len + q.y, abs(q.z-a.z)*i/len + q.z} ;Pos3d ppos = Pos3d {abs(w.x-s.x)*i/len + w.x, abs(w.y-s.y)*i/len + w.y, abs(w.z-s.z)*i/len + w.z} ;line3d(qpos, ppos, color);}
}struct cube
{Pos3d pos ;int xlen, ylen, zlen ;void init(int xp, int yp, int zp, int xl, int yl, int zl){pos = Pos3d {xp - Py3dx, yp - Py3dy, zp};xlen = xl;ylen = yl;zlen = zl;}inline Pos3d getdotpos(int doti){switch(doti){case 8: return Pos3d {pos.x + xlen, pos.y + ylen, pos.z + zlen} ;case 7: return Pos3d {pos.x, pos.y + ylen, pos.z + zlen} ;case 6: return Pos3d {pos.x + xlen, pos.y, pos.z + zlen} ;case 4: return Pos3d {pos.x + xlen, pos.y + ylen, pos.z} ;case 5: return Pos3d {pos.x, pos.y, pos.z + zlen} ;case 3: return Pos3d {pos.x, pos.y + ylen, pos.z} ;case 2: return Pos3d {pos.x + xlen, pos.y, pos.z} ;case 1: return Pos3d {pos.x, pos.y, pos.z} ;}return {0, 0, 0}; //Else}/*5----------6/|         /|/ |        / |/  |       /  |1----------2   ||   7------|---8
ylen|  /       |  /| /        | /zlen|/  xlen   |/3----------4^|O*/ inline void draw(concol color, int mode){if(mode & full){rect3d(getdotpos(5), getdotpos(7), getdotpos(6), getdotpos(8), green);rect3d(getdotpos(1), getdotpos(3), getdotpos(5), getdotpos(7), red);rect3d(getdotpos(5), getdotpos(1), getdotpos(6), getdotpos(2), blue);rect3d(getdotpos(7), getdotpos(3), getdotpos(8), getdotpos(4), pink);rect3d(getdotpos(2), getdotpos(4), getdotpos(6), getdotpos(8), yellow);rect3d(getdotpos(1), getdotpos(3), getdotpos(2), getdotpos(4), white);          }if(mode & clean){line3d(getdotpos(1), getdotpos(2), color) ;line3d(getdotpos(1), getdotpos(3), color) ;line3d(getdotpos(1), getdotpos(5), color) ;line3d(getdotpos(2), getdotpos(6), color) ;line3d(getdotpos(2), getdotpos(4), color) ;line3d(getdotpos(3), getdotpos(4), color) ;line3d(getdotpos(3), getdotpos(7), color) ;line3d(getdotpos(4), getdotpos(8), color) ;line3d(getdotpos(5), getdotpos(6), color) ;line3d(getdotpos(5), getdotpos(7), color) ;line3d(getdotpos(6), getdotpos(8), color) ;line3d(getdotpos(7), getdotpos(8), color) ;}if(mode & box){line3d(getdotpos(1), getdotpos(4), color) ;line3d(getdotpos(1), getdotpos(7), color) ;line3d(getdotpos(2), getdotpos(3), color) ;line3d(getdotpos(2), getdotpos(8), color) ;line3d(getdotpos(3), getdotpos(5), color) ;line3d(getdotpos(3), getdotpos(8), color) ;line3d(getdotpos(4), getdotpos(7), color) ;line3d(getdotpos(4), getdotpos(6), color) ;line3d(getdotpos(5), getdotpos(2), color) ;line3d(getdotpos(5), getdotpos(8), color) ;line3d(getdotpos(6), getdotpos(1), color) ;line3d(getdotpos(6), getdotpos(7), color) ;              }}
};

C++控制台绘图头文件相关推荐

  1. C++std命名空间和头文件详解

    一个中大型软件往往由多名程序员共同开发,会使用大量的变量和函数,不可避免地会出现变量或函数的命名冲突.当所有人的代码都测试通过,没有问题时,将它们结合到一起就有可能会出现命名冲突. 例如小李和小韩都参 ...

  2. C++头文件和std命名空间(精辟)

    C++是在C语言的基础上开发的,早期的 C++ 还不完善,不支持命名空间,没有自己的编译器,而是将 C++ 代码翻译成C代码,再通过C编译器完成编译.这个时候的 C++ 仍然在使用C语言的库,stdi ...

  3. 关于<graphics.h>的头文件

    关于<graphics.h>的头文件如下 /******************************************************* EasyX Library fo ...

  4. 【C语言】标准库(头文件、静态库、动态库),windows与Linux平台下的常用C语言标准库

    一.Introduction 1.1 C语言标准库 1.2 历代C语言标准 1.3 主流C语言编译器 二.C语言标准库 2.1 常用标准头文件 2.2 常用标准静态库 三.windows平台 四.Li ...

  5. C++ 笔记(02)— 程序结构(头文件说明、命名空间、函数返回值、函数参数、注释、语句结束符、cin/cout)

    先看以下代码 #include <iostream> #include <string> using namespace std;int main() // main() 是程 ...

  6. gsoap 学习 1-由wsdl文件生成h头文件

    开始前先看一下用户向导吧 http://www.cs.fsu.edu/~engelen/soap.html 中左侧点击Documentation 英语水平确实有限,有些内容可能说的不准确,敬请参考向导 ...

  7. Windows Pe 第三章 PE头文件(下)

    3.5  数据结构字段详解 3.5.1  PE头IMAGE_NT_HEADER的字段 1.IMAGE_NT_HEADER.Signature +0000h,双字.PE文件标识,被定义为00004550 ...

  8. 【C 语言】动态库封装与设计 ( 动态库调用环境搭建 | 创建应用 | 拷贝动态库相关文件到源码路径 | 导入头文件 | 配置动态库引用 | 调用动态库中的函数 )

    文章目录 一.在 Visual Studio 2019 中创建 " 控制台应用 " 程序 二.拷贝 xxx.lib.xxx.dll.xxx.h 到源码路径 三.导入 xxx.h 头 ...

  9. 【OpenGL】七、桌面窗口搭建 ( 导入头文件 | 桌面程序入口函数 | 注册窗口 | 创建窗口 | 显示窗口 )

    文章目录 一.导入头文件 二.桌面程序入口函数 三.注册窗口 四.创建窗口 五.显示窗口 六.完整代码示例 七.相关资源 基于 [OpenGL]一.Visual Studio 2019 创建 Wind ...

最新文章

  1. Luogu P1087 FBI树
  2. html页面加文字横向滚动,js实现文字横向滚动
  3. 【ZZ】Python的主(main)函数问题
  4. MySQL 主从同步故障处理-小记
  5. linux debian 8.3 发布时间,Robolinux 8.3 发布下载,基于 Debian 的 Linux 发行
  6. OOAD实践之路——真实案例解析OO理论与实践(五、需求分析之前的故事)
  7. beeline-导出csv
  8. [翻译]架构师应该知道的97件事_03关键问题可能不是出在技术上
  9. 证件照尺寸修改、图片背景换色、照片大小压缩…几个在线图片编辑、处理网站推荐
  10. kali下经典的ddos攻击软件_kali DOS/DDOS攻击(局域网内)
  11. Java贪吃蛇(附完整代码下载链接)-跟随狂神一天完成
  12. NodeJS 微信公共号开发 - 响应微信发送的Token验证(山东数漫江湖)
  13. Android打包动态配置签名
  14. 火柴棍游戏c语言,C语言题目
  15. Eclips配置模板消息
  16. 别太在意人走茶凉 物是人非
  17. Head First Design Patterns(深入浅出设计模式)-目录
  18. Retrofit(一)
  19. 武汉大学计算机软件与理论博士,武汉大学博士学术屌爆,人大的不服不行
  20. 变频器必须设置的5大参数

热门文章

  1. CSS超链接样式,去除下划线等
  2. 硕士生论文存在的问题
  3. [转]计算机视觉入门
  4. 1024 科学计数法(C语言)
  5. idc机房数据中心租赁机柜的优势
  6. 操作系统课程设计-进程管理
  7. Impala服务:unable to find SASL plugin: PLAIN
  8. dd指令打包iso文件 linux_Linux_如何在Linux操作系统下创建ISO镜像文件,1、用dd命令#dd if=/dev/cdrom - phpStudy...
  9. Hibernate的下载和安装
  10. java中什么叫引用