C++/Linux - 绘制到窗口

标签 c++ linux drawing conways-game-of-life

我正在上一门面向对象的编程课,我们刚刚做了一个项目,我们必须实现 Conway's Game of Life 。项目规范只是将文本行输出到终端,显示单元格的演变,但它不是很漂亮。我认为修改程序会很有趣,而不是将文本行发送到终端,绘图窗口会根据我们单元格的当前状态进行更新。我不想深入研究进入图形......我很好用原始项目指定的单元格的文本表示。正如这个问题的标题所暗示的,该程序是用 C++ 编写的,并且可以在 Linux 机器上运行。最简单的方法是什么我来实现这一目标。

编辑:好的,所以我认为我已经很接近了。问题是没有出现换行符。在我的 Life“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一个镜头。如果它不适合您的需要,请参阅 this link获取更多建议。

关于C++/Linux - 绘制到窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16214987/

相关文章:

c++ - 是否可以仅使用循环在形状内绘制形状?如果是这样,请指出正确的方向

c++ - 为什么允许拥有一个自身具有不完整类型静态成员的类?

c++ - 删除带有堆栈的二叉树

linux - 用于Linux的自定义登录程序

c - 是否可以通过编程方式设置当前的 Linux 运行级别?

iPhone SDK - 如何将一个 UIImage 绘制到另一个 UIImage 上?

c++ - 如何在 Linux 上以编程方式 (C/C++) 获取国家/地区代码?

c++ - 如何将 Switch Qml 添加到 qt widgets?

linux - Linux下如何杀死一个进程及其所有子进程?

drawing - javascript库透视扭曲绘图