2013-04-25 305 views
1

我正在做一個面向對象的編程類,我們只是做了一個項目,我們不得不實現Conway's Game of Life。項目規範只是輸出文本行到終端,顯示雖然它不是很漂亮,但我認爲修改程序會很有趣,所以不是給終端發送文本行,而是使用當前單元格的狀態更新繪圖窗口。不想深入研究圖形......我很好地使用原始項目指定的單元格的文本表示形式。正如這個問題的標題所暗示的,程序是用C++編寫的,並且可以在Linux機器上工作。有什麼最簡單的方法讓我做到這一點。C++/Linux - 繪製到一個窗口

編輯:好的,所以我瘦k我非常接近。問題是沒有出現換行符。在我的生活「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); 
} 
+0

我發現[本教程](http://www.dreamincode.net/forums/topic/166837-linux-writing-our-first-x-windows-application/),它已經讓我離開了地面。 – 2013-04-25 12:50:51

回答

3

我會給SFML一槍。如果不符合您的需求,請參閱this link獲取更多建議。

+0

對於SFML +1。它看起來好像已經寫了很多代碼,但也許考慮將來的圖形項目使用SFML。 – Chaosed0 2013-04-25 15:12:35

+0

或者也許Qt與[graphics view](http://qt-project.org/doc/qt-4.8/graphicsview.html) – 2013-04-25 18:50:42

相關問題