C++/Linux - Drawing to a Window -
i"m taking object oriented programming class, , did project had implement conway's game of life. project specification output lines of text terminal, shows evolution of cells, it's not pretty. thought fun modify program so, instead of sending lines of text terminal, drawing window updated current state of our cells. i'm not looking delve graphics...i'm fine using textual representation of cells original project specified. title of question implies, program written in c++ , made work on linux boxes. what's easiest way me make happen.
edit: okay, think i'm pretty close. problem line breaks aren't appearing. in life "tostring" operator i've tried endl
, \n
, neither seems work. here code...
#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 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); }