c++ /Linux -绘制到窗口
C++/Linux - Drawing to a Window
我正在上一门面向对象的编程课,我们刚刚完成了一个项目,我们必须实现Conway's Game of Life。项目规范只是将文本行输出到终端,这显示了细胞的演变,但它不是很漂亮。我认为这将是有趣的修改程序,所以,而不是发送文本行到终端,绘制窗口更新与我们的细胞的当前状态。我并不打算在图像方面钻研得太深……我可以使用原始项目指定的单元格的文本表示形式。正如这个问题的标题所暗示的那样,该程序是用c++编写的,并且可以在Linux机器上运行。对我来说,实现这个目标最简单的方法是什么?
编辑:好吧,我想我已经很接近了。问题是没有出现换行符。在我的生活中"toString"操作符我尝试过endl
和n
,但似乎都不起作用。下面是代码…
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <sstream>
#include "Cell.h"
#include "Life.h"
int main (int argc, char *argv[])
{
Display *display;
Visual *visual;
int depth;
int text_x;
int text_y;
XSetWindowAttributes frame_attributes;
Window frame_window;
XFontStruct *fontinfo;
XGCValues gr_values;
GC graphical_context;
XEvent event;
char hello_string[] = "Hello World";
int hello_string_length = strlen(hello_string);
display = XOpenDisplay(NULL);
visual = DefaultVisual(display, 0);
depth = DefaultDepth(display, 0);
frame_attributes.background_pixel = XWhitePixel(display, 0);
/* create the application window */
frame_window = XCreateWindow(display, XRootWindow(display, 0),
0, 0, 400, 400, 5, depth,
InputOutput, visual, CWBackPixel,
&frame_attributes);
XStoreName(display, frame_window, "The Game of Life");
XSelectInput(display, frame_window, ExposureMask | StructureNotifyMask);
fontinfo = XLoadQueryFont(display, "10x20");
gr_values.font = fontinfo->fid;
gr_values.foreground = XBlackPixel(display, 0);
graphical_context = XCreateGC(display, frame_window,
GCFont+GCForeground, &gr_values);
XMapWindow(display, frame_window);
Life <ConwayCell> aLife (21, 21);
aLife.animate (10, 5, '*');
aLife.animate (10, 6, '*');
aLife.animate (10, 7, '*');
aLife.animate (10, 8, '*');
aLife.animate (10, 9, '*');
aLife.animate (10, 10, '*');
aLife.animate (10, 11, '*');
aLife.animate (10, 12, '*');
aLife.animate (10, 13, '*');
aLife.animate (10, 14, '*');
std::ostringstream outStream;
outStream << aLife;
string aString = outStream.str ();
const char* aChar = aString.c_str ();
int len = outStream.str ().size ();
while ( 1 ) {
XNextEvent(display, (XEvent *)&event);
switch ( event.type ) {
case Expose:
{
XWindowAttributes window_attributes;
int font_direction, font_ascent, font_descent;
XCharStruct text_structure;
XTextExtents(fontinfo, aChar, len,
&font_direction, &font_ascent, &font_descent,
&text_structure);
XGetWindowAttributes(display, frame_window, &window_attributes);
text_x = (window_attributes.width - text_structure.width)/2;
text_y = (window_attributes.height -
(text_structure.ascent+text_structure.descent))/2;
outStream << aLife;
XDrawString(display, frame_window, graphical_context,
text_x, text_y, aChar, len);
break;
}
default:
break;
}
}
return(0);
}
我会给SFML一个机会。如果它不适合您的需求,请参阅此链接以获取更多建议。
相关文章:
- QuadTree只在窗口的右上角绘制
- SFML 文本未绘制在窗口上
- GDI 绘制到外部窗口 (C++)
- 使用标准窗口与wxWidgets一起绘制?
- 如何在SDL2窗口上使用Cairo绘制一条白色直线
- 为什么在GLFW窗口中没有用这个代码在我的屏幕上绘制立方体
- 如何在 direct2d 中使用随其窗口重新缩放的效果绘制图像
- 如何使用WinAPI每帧只重新绘制一次子窗口
- 使用 Win32 将 GDI 绘制大小缩放为窗口大小
- 如何在窗口外绘制图形(OpenGL GLFW)
- SFML参考窗口在绝对位置绘制
- 在窗口大小上绘制的OpenGL消失
- 在 c++ 生成器中检测绘制线的窗口坐标的最佳方法
- 如何在调用函数时将对象绘制到 sfml 窗口上
- 如何将成员从类绘制到窗口 c++ 上
- 如何在子小部件而不是父窗口小部件上绘制一条线
- OpenGL-如何绘制完整的窗口矩形
- 如何调用OnEraseBkgnd()在另一个函数中重新绘制窗口?(MFC)
- WTL 如何在同一窗口中使用 2 个自定义绘制的 CListViewCtr
- 如何在win32中在不覆盖标题栏的情况下围绕窗口绘制矩形