C++/Linux - 在窗口中绘制图形

3
我正在学习面向对象编程课程,我们刚刚完成了一个项目,需要实现康威生命游戏。项目的规格说明只是将文本行输出到终端,显示细胞的演化情况,但不太美观。我认为修改程序很有趣,不是将文本行发送到终端,而是更新绘图窗口以呈现当前单元格状态。我不打算深入研究图形...我可以使用原始项目指定的单元格文本表示。正如这个问题的标题所暗示的那样,程序是用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);
}

我找到了这个教程,它已经让我入门了。 - Dan Forbes
1个回答

3

我建议你试试SFML。如果它不能满足你的需求,可以看看这个链接获取更多建议。


+1 for SFML。看起来你已经写了很多代码,但也许可以考虑在未来的图形项目中使用SFML。 - Chaosed0
或许可以使用Qt及其图形视图 - Basile Starynkevitch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接