1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
--- xworld.c.orig 1999-07-28 15:39:11 UTC
+++ xworld.c
@@ -63,6 +63,8 @@
#define BORDER 5
#define SLEEP 120
+#define MIN(x,y) (((x)<(y))?(x):(y))
+
char *MainTitle[] = {"Hello, world"};
char *IconTitle[] = {"xworld"};
@@ -92,6 +94,26 @@ usage()
exit(1);
}
+
+static char *
+alloc_image(XImage *image)
+{
+ char *Map;
+ int ix, iy, j;
+ Map = (char *)malloc(image->height * image->bytes_per_line);
+ if (Map == NULL) {
+ fprintf(stderr, "xworld: not enough memory\n");
+ exit(1);
+ }
+ image->data = Map;
+
+ for(j = 0, ix = 0; ix < image->width; ix++)
+ for (iy = 0; iy < image->height; iy++)
+ XPutPixel(image, ix, iy, j++ % NCOLORS);
+
+ return Map;
+}
+
int
main(int argc, char **argv)
{
@@ -305,29 +327,16 @@ main(int argc, char **argv)
xcolor[i].blue = color[i].blue;
if (XAllocColor(display, cmap, &xcolor[i]) == 0) {
fprintf(stderr, "xworld: can't allocate colors\n");
+#if 0
exit(-1);
+#endif
}
}
/*
* generate startup image
*/
- Map = (char *)malloc(size*size*sizeof(char));
- if (Map == NULL) {
- fprintf(stderr, "xworld: not enough memory\n");
- exit(1);
- }
-
- j = 0;
- for (iy = 0; iy < size; iy++) {
- i = iy % (NCOLORS + 1);
- for (ix = 0; ix < size; ix++) {
- *(Map + j++) = xcolor[i].pixel;
- if (i++ >= NCOLORS) i = 0;
- }
- }
-
- image->data = Map;
+ Map = alloc_image(image);
gc = XCreateGC(display, win, 0, 0);
@@ -414,21 +423,21 @@ main(int argc, char **argv)
* Map elevations to colors
*/
if (i != SOK) {
- *(Map + j) = xcolor[BLACK].pixel;
+ XPutPixel(image, ix, iy, xcolor[BLACK].pixel);
continue;
}
if (Value > level[NLEVELS - 1]) {
- *(Map + j) = xcolor[HIGH].pixel;
+ XPutPixel(image, ix, iy, xcolor[HIGH].pixel);
continue;
}
for (i = 0; i <= NLEVELS - 1; i++) {
if (Value <= level[i]) {
- *(Map + j) = xcolor[i].pixel;
+ XPutPixel(image, ix, iy, xcolor[i].pixel);
break;
}
}
} else
- *(Map + j) = xcolor[BLACK].pixel;
+ XPutPixel(image, ix, iy, xcolor[BLACK].pixel);
}
}
XPutImage(display, win, gc, image, 0, 0, 0, 0, size, size);
@@ -444,8 +453,12 @@ main(int argc, char **argv)
ConfigureEvent = (XConfigureEvent *)&event;
size = ConfigureEvent->width;
if (ConfigureEvent->height != size) {
+#if 0
fprintf(stderr, "xworld: error: width != height\n");
exit(1);
+#else
+ size = MIN(size, ConfigureEvent->height);
+#endif
}
if (size != old_size) {
old_size = size;
@@ -453,28 +466,14 @@ main(int argc, char **argv)
r1 = n/2;
r2 = size/2;
XDestroyImage(image);
- free(Map);
image = XCreateImage(display, visual, depth, format,
0, 0, size, size, bitmap_pad, 0);
if (image == NULL) {
fprintf(stderr, "xworld: can't create XImage\n");
exit(1);
}
- Map = (char *)malloc(size*size*sizeof(char));
- if (Map == NULL) {
- fprintf(stderr, "xworld: not enough memory\n");
- exit(1);
- }
- image->data = Map;
+ Map = alloc_image(image);
- j = 0;
- for (iy = 0; iy < size; iy++) {
- i = iy % (NCOLORS + 1);
- for (ix = 0; ix < size; ix++) {
- *(Map + j++) = xcolor[i].pixel;
- if (i++ >= NCOLORS) i = 0;
- }
- }
XPutImage(display, win, gc, image, 0, 0, 0, 0,
size, size);
break;
|