Skip to content

Commit 279b9c3

Browse files
committed
Added XWindows version, fixed a bug in UNIX version.
1 parent eade680 commit 279b9c3

File tree

3 files changed

+234
-6
lines changed

3 files changed

+234
-6
lines changed

unix/Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
CC=cc
2-
CCOPTS=
2+
CCOPTS=-Wall -Wpedantic
33

4-
all: mandelbrot
4+
all: mandelbrot xmandelbrot
55

66
mandelbrot: mandelbrot.c
77
$(CC) $(CCOPTS) -o mandelbrot mandelbrot.c -lcurses
8+
9+
xmandelbrot: xmandelbrot.c
10+
$(CC) $(CCOPTS) -o xmandelbrot xmandelbrot.c -lX11
11+
812
clean:
913
rm -f *.o
1014
rm -f mandelbrot
15+
rm -f xmandelbrot

unix/mandelbrot.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void cleanup()
8080
void calculate()
8181
{
8282
int i, col, row, iteration;
83-
double width, height, x0, y0, x, y, x_scale, y_scale, xtemp;
83+
double width, height, x0, y0, x, y, xtemp;
8484

8585
width = (double)cols;
8686
height = (double)lines;
@@ -115,7 +115,6 @@ void display()
115115
{
116116
int i, col, row;
117117
int value;
118-
int color;
119118
char symbol;
120119

121120
/* Clear the screen */
@@ -155,7 +154,7 @@ double time_in_seconds()
155154
return t / 60.0;
156155
}
157156

158-
void main()
157+
int main()
159158
{
160159
double start, after_calc, after_display, calc_time, display_time;
161160

@@ -173,5 +172,5 @@ void main()
173172

174173
printf("\nCalculation Time: %0.3f secs, Display Time: %0.3f secs, Total Time: %0.3f secs\n", calc_time, display_time, calc_time + display_time);
175174

176-
exit(0);
175+
return 0;
177176
}

unix/xmandelbrot.c

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
/*
2+
3+
hellox -- Hello world with Xlib.
4+
5+
$(CC) -o hellox hellox.c -lX11 -L/usr/X11/lib
6+
7+
*/
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
#include <string.h>
11+
#include <X11/Xlib.h>
12+
#include <X11/Xutil.h>
13+
#include <sys/types.h>
14+
#include <sys/times.h>
15+
16+
#define MAX_ITERATIONS 16
17+
18+
const char *title = "xmandelbrot";
19+
20+
void get_window_size(display, window, height, width)
21+
Display *display;
22+
Window window;
23+
unsigned int *height;
24+
unsigned int *width;
25+
{
26+
Window root;
27+
int x, y;
28+
unsigned int border, depth;
29+
XGetGeometry(display, window, &root, &x, &y, width, height, &border, &depth);
30+
}
31+
32+
unsigned long lookup_color(display, colormap, color_name, color)
33+
Display *display;
34+
Colormap colormap;
35+
char *color_name;
36+
XColor *color;
37+
{
38+
XColor exact, screen;
39+
if (!XAllocNamedColor(display, colormap, color_name, &exact, &screen))
40+
{
41+
printf("Color Not Found: %s\n", color_name);
42+
return 0;
43+
}
44+
return screen.pixel;
45+
}
46+
47+
double time_in_seconds()
48+
{
49+
struct tms buffer;
50+
clock_t t = times(&buffer);
51+
return t / 60.0;
52+
}
53+
54+
void calculate(display, window, gc, palette, palette_size)
55+
Display *display;
56+
Window window;
57+
GC gc;
58+
unsigned long int *palette;
59+
size_t palette_size;
60+
{
61+
unsigned int i, col, row, iteration;
62+
double width, height, x0, y0, x, y, xtemp;
63+
unsigned int lines, cols;
64+
unsigned long color;
65+
double start, finish;
66+
67+
start = time_in_seconds();
68+
69+
get_window_size(display, window, &lines, &cols);
70+
height = (double)lines;
71+
width = (double)cols;
72+
73+
XClearWindow(display, window);
74+
75+
for (row = 0; row < lines; row++)
76+
{
77+
// printf("Row: %u\n", row);
78+
y0 = (row * 2.1 / height) - 1;
79+
for (col = 0; col < cols; col++)
80+
{
81+
x0 = (col * 3.5 / width) - 2.5;
82+
x = 0.0;
83+
y = 0.0;
84+
iteration = 0;
85+
while ( ((x*x) + (y*y) <= 4) && (iteration < MAX_ITERATIONS))
86+
{
87+
xtemp = (x*x) - (y*y) + x0;
88+
y = (2*x*y) + y0;
89+
x = xtemp;
90+
iteration++;
91+
}
92+
93+
i = (iteration % palette_size);
94+
color = palette[i];
95+
96+
XSetForeground(display, gc, color);
97+
XDrawPoint(display, window, gc, col, row);
98+
}
99+
}
100+
101+
finish = time_in_seconds();
102+
printf("Height: %u, Width: %u, Time: %0.3f Seconds\n", lines, cols, (finish-start));
103+
104+
}
105+
106+
int main(argc,argv)
107+
int argc;
108+
char **argv;
109+
{
110+
Display *display;
111+
Window window;
112+
113+
GC gc;
114+
115+
XEvent event;
116+
KeySym key;
117+
118+
XSizeHints hint;
119+
120+
int screen;
121+
unsigned long fg, bg;
122+
int i;
123+
char text[10];
124+
int done;
125+
126+
Colormap colormap;
127+
128+
unsigned long palette[16];
129+
char *color_names[16];
130+
color_names[0] = "Black";
131+
color_names[1] = "DarkBlue";
132+
color_names[2] = "DarkGreen";
133+
color_names[3] = "DarkCyan";
134+
color_names[4] = "DarkRed";
135+
color_names[5] = "DarkMagenta";
136+
color_names[6] = "Brown";
137+
color_names[7] = "LightGray";
138+
color_names[8] = "Gray";
139+
color_names[9] = "Blue";
140+
color_names[10] = "Green";
141+
color_names[11] = "Cyan";
142+
color_names[12] = "Red";
143+
color_names[13] = "Magenta";
144+
color_names[14] = "Yellow";
145+
color_names[15] = "White";
146+
147+
/* setup display/screen */
148+
display = XOpenDisplay("");
149+
150+
screen = DefaultScreen(display);
151+
152+
colormap = DefaultColormap(display, screen);
153+
154+
for (i = 0; i < 16; i++)
155+
{
156+
palette[i] = lookup_color(display, colormap, color_names[i]);
157+
}
158+
159+
/* drawing contexts for an window */
160+
bg = BlackPixel(display, screen);
161+
fg = WhitePixel(display, screen);
162+
hint.x = 100;
163+
hint.y = 100;
164+
hint.width = 500;
165+
hint.height = 300;
166+
hint.flags = PPosition|PSize;
167+
168+
/* create window */
169+
window = XCreateSimpleWindow(display, DefaultRootWindow(display),
170+
hint.x, hint.y,
171+
hint.width, hint.height,
172+
5, fg, bg);
173+
174+
/* window manager properties (yes, use of StdProp is obsolete) */
175+
XSetStandardProperties(display, window, title, title,
176+
None, argv, argc, &hint);
177+
178+
/* graphics context */
179+
gc = XCreateGC(display, window, 0, 0);
180+
XSetBackground(display, gc, bg);
181+
XSetForeground(display, gc, fg);
182+
183+
/* allow receiving mouse events */
184+
XSelectInput(display,window,
185+
ButtonPressMask|KeyPressMask|ExposureMask);
186+
187+
/* show up window */
188+
XMapRaised(display, window);
189+
190+
/* event loop */
191+
done = 0;
192+
while(done==0){
193+
194+
/* fetch event */
195+
XNextEvent(display, &event);
196+
197+
switch(event.type){
198+
case Expose:
199+
/* Window was showed. */
200+
if(event.xexpose.count==0)
201+
calculate(display, window, gc, palette, 16);
202+
break;
203+
case MappingNotify:
204+
/* Modifier key was up/down. */
205+
XRefreshKeyboardMapping(&event.xmapping);
206+
break;
207+
case ButtonPress:
208+
/* Mouse button was pressed. */
209+
break;
210+
case KeyPress:
211+
/* Key input. */
212+
i = XLookupString(&event.xkey, text, 10, &key, 0);
213+
if(i==1 && text[0]=='q') done = 1;
214+
break;
215+
}
216+
}
217+
218+
/* finalization */
219+
XFreeGC(display,gc);
220+
XDestroyWindow(display, window);
221+
XCloseDisplay(display);
222+
223+
exit(0);
224+
}

0 commit comments

Comments
 (0)