]> git.zerfleddert.de Git - micropolis/blob - src/sim/w_x.c
3d8dd481979966f6514b3f0414345ecdec9a0af3
[micropolis] / src / sim / w_x.c
1 /* w_x.c: X Window System support
2 *
3 * Micropolis, Unix Version. This game was released for the Unix platform
4 * in or about 1990 and has been modified for inclusion in the One Laptop
5 * Per Child program. Copyright (C) 1989 - 2007 Electronic Arts Inc. If
6 * you need assistance with this program, you may contact:
7 * http://wiki.laptop.org/go/Micropolis or email micropolis@laptop.org.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details. You should have received a
18 * copy of the GNU General Public License along with this program. If
19 * not, see <http://www.gnu.org/licenses/>.
20 *
21 * ADDITIONAL TERMS per GNU GPL Section 7
22 *
23 * No trademark or publicity rights are granted. This license does NOT
24 * give you any right, title or interest in the trademark SimCity or any
25 * other Electronic Arts trademark. You may not distribute any
26 * modification of this program using the trademark SimCity or claim any
27 * affliation or association with Electronic Arts Inc. or its employees.
28 *
29 * Any propagation or conveyance of this program must include this
30 * copyright notice and these terms.
31 *
32 * If you convey this program (or any modifications of it) and assume
33 * contractual liability for the program to recipients of it, you agree
34 * to indemnify Electronic Arts for any liability that those contractual
35 * assumptions impose on Electronic Arts.
36 *
37 * You may not misrepresent the origins of this program; modified
38 * versions of the program must be marked as such and not identified as
39 * the original program.
40 *
41 * This disclaimer supplements the one included in the General Public
42 * License. TO THE FULLEST EXTENT PERMISSIBLE UNDER APPLICABLE LAW, THIS
43 * PROGRAM IS PROVIDED TO YOU "AS IS," WITH ALL FAULTS, WITHOUT WARRANTY
44 * OF ANY KIND, AND YOUR USE IS AT YOUR SOLE RISK. THE ENTIRE RISK OF
45 * SATISFACTORY QUALITY AND PERFORMANCE RESIDES WITH YOU. ELECTRONIC ARTS
46 * DISCLAIMS ANY AND ALL EXPRESS, IMPLIED OR STATUTORY WARRANTIES,
47 * INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY, SATISFACTORY QUALITY,
48 * FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT OF THIRD PARTY
49 * RIGHTS, AND WARRANTIES (IF ANY) ARISING FROM A COURSE OF DEALING,
50 * USAGE, OR TRADE PRACTICE. ELECTRONIC ARTS DOES NOT WARRANT AGAINST
51 * INTERFERENCE WITH YOUR ENJOYMENT OF THE PROGRAM; THAT THE PROGRAM WILL
52 * MEET YOUR REQUIREMENTS; THAT OPERATION OF THE PROGRAM WILL BE
53 * UNINTERRUPTED OR ERROR-FREE, OR THAT THE PROGRAM WILL BE COMPATIBLE
54 * WITH THIRD PARTY SOFTWARE OR THAT ANY ERRORS IN THE PROGRAM WILL BE
55 * CORRECTED. NO ORAL OR WRITTEN ADVICE PROVIDED BY ELECTRONIC ARTS OR
56 * ANY AUTHORIZED REPRESENTATIVE SHALL CREATE A WARRANTY. SOME
57 * JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF OR LIMITATIONS ON IMPLIED
58 * WARRANTIES OR THE LIMITATIONS ON THE APPLICABLE STATUTORY RIGHTS OF A
59 * CONSUMER, SO SOME OR ALL OF THE ABOVE EXCLUSIONS AND LIMITATIONS MAY
60 * NOT APPLY TO YOU.
61 */
62 #include "sim.h"
63
64
65 struct XDisplay *XDisplays = NULL;
66 int DisplayCount = 0;
67 #ifdef IS_LINUX
68 int FlushStyle = 3;
69 #else
70 int FlushStyle = 4;
71 #endif
72 int GotXError;
73
74
75 unsigned char ColorIntensities[] = {
76 /* COLOR_WHITE */ 255,
77 /* COLOR_YELLOW */ 170,
78 /* COLOR_ORANGE */ 127,
79 /* COLOR_RED */ 85,
80 /* COLOR_DARKRED */ 63,
81 /* COLOR_DARKBLUE */ 76,
82 /* COLOR_LIGHTBLUE */ 144,
83 /* COLOR_BROWN */ 118,
84 /* COLOR_LIGHTGREEN */ 76,
85 /* COLOR_DARKGREEN */ 42,
86 /* COLOR_OLIVE */ 118,
87 /* COLOR_LIGHTBROWN */ 144,
88 /* COLOR_LIGHTGRAY */ 191,
89 /* COLOR_MEDIUMGRAY */ 127,
90 /* COLOR_DARKGRAY */ 63,
91 /* COLOR_BLACK */ 0,
92 };
93
94
95 ViewToTileCoords(SimView *view, int x, int y, int *outx, int *outy)
96 {
97 x = (view->pan_x - ((view->w_width >>1) - x)) >>4;
98 y = (view->pan_y - ((view->w_height >>1) - y)) >>4;
99
100 if (x < 0) x = 0;
101 if (x >= WORLD_X) x = WORLD_X - 1;
102 if (y < 0) y = 0;
103 if (y >= WORLD_Y) y = WORLD_Y - 1;
104
105 if (x < view->tile_x)
106 x = view->tile_x;
107 if (x >= view->tile_x + view->tile_width)
108 x = view->tile_x + view->tile_width - 1;
109 if (y < view->tile_y)
110 y = view->tile_y;
111 if (y >= view->tile_y + view->tile_height)
112 y = view->tile_y + view->tile_height - 1;
113
114 if (view->tool_x_const != -1)
115 x = view->tool_x_const;
116 if (view->tool_y_const != -1)
117 y = view->tool_y_const;
118
119 *outx = x; *outy = y;
120 }
121
122
123 ViewToPixelCoords(SimView *view, int x, int y, int *outx, int *outy)
124 {
125 x = view->pan_x - ((view->w_width >>1) - x);
126 y = view->pan_y - ((view->w_height >>1) - y);
127
128 if (x < 0) x = 0;
129 if (x >= (WORLD_X <<4)) x = (WORLD_X <<4) - 1;
130 if (y < 0) y = 0;
131 if (y >= (WORLD_Y <<4)) y = (WORLD_Y <<4) - 1;
132
133 if (x < (view->tile_x <<4))
134 x = (view->tile_x <<4);
135 if (x >= ((view->tile_x + view->tile_width) <<4))
136 x = ((view->tile_x + view->tile_width) <<4) - 1;
137 if (y < (view->tile_y <<4))
138 y = (view->tile_y <<4);
139 if (y >= ((view->tile_y + view->tile_height) <<4))
140 y = ((view->tile_y + view->tile_height) <<4) - 1;
141
142 if (view->tool_x_const != -1)
143 x = (view->tool_x_const <<4) + 8;
144 if (view->tool_y_const != -1)
145 y = (view->tool_y_const <<4) + 8;
146
147 *outx = x; *outy = y;
148 }
149
150
151 UpdateFlush()
152 {
153 struct XDisplay *xd;
154
155 if (sim_skips > 0) {
156 if (sim_skip > 0) {
157 sim_skip--;
158 return;
159 }
160 sim_skip = sim_skips;
161 }
162
163 switch (FlushStyle) {
164
165 case 0:
166 break;
167
168 case 1:
169 for (xd = XDisplays; xd != NULL; xd = xd->next)
170 XFlush(xd->dpy);
171 break;
172
173 case 2:
174 for (xd = XDisplays; xd != NULL; xd = xd->next)
175 XSync(xd->dpy, False);
176 break;
177
178 case 3:
179 if (XDisplays && XDisplays->next) {
180 for (xd = XDisplays; xd != NULL; xd = xd->next) {
181 XFlush(xd->dpy);
182 }
183 }
184 for (xd = XDisplays; xd != NULL; xd = xd->next) {
185 XSync(xd->dpy, False);
186 }
187 break;
188
189 case 4:
190 for (xd = XDisplays; xd != NULL; xd = xd->next) {
191 #ifndef IS_LINUX
192 /* XXX TODO: figure this out for linux and new x libs */
193 if ((xd->request != xd->dpy->request) ||
194 (xd->last_request_read != xd->dpy->last_request_read)) {
195 XSync(xd->dpy, False);
196 xd->request = xd->dpy->request;
197 xd->last_request_read = xd->dpy->last_request_read;
198 }
199 #endif
200 }
201 break;
202
203 }
204 }
205
206
207 int
208 CatchXError(Display *dpy, XErrorEvent *err)
209 {
210 GotXError = 1;
211 #if 0
212 printf("GOT X ERROR code %d request code %d %d\n",
213 err->error_code, err->request_code, err->minor_code);
214 #endif
215 return (0);
216 }
217
218
219 DoStopMicropolis()
220 {
221 (void)XSetErrorHandler(CatchXError);
222
223 StopToolkit();
224
225 if (sim) {
226 while (sim->editor != NULL) {
227 DestroyView(sim->editor);
228 }
229
230 while (sim->map != NULL) {
231 DestroyView(sim->map);
232 }
233
234 while (sim->graph != NULL) {
235 DestroyGraph(sim->graph);
236 }
237
238 #ifdef CAM
239 while (sim->scam != NULL) {
240 DestroyCam(sim->scam);
241 }
242 #endif
243 }
244 }
245
246
247 DoTimeoutListen()
248 {
249 while (Tk_DoOneEvent(TK_DONT_WAIT)) ;
250 }
251
252
253 Sim *
254 MakeNewSim()
255 {
256 Sim *sim;
257
258 sim = (Sim *)ckalloc(sizeof(Sim));
259 sim->editors = 0; sim->editor = NULL;
260 sim->maps = 0; sim->map = NULL;
261 sim->graphs = 0; sim->graph = NULL;
262 sim->sprites = 0; sim->sprite = NULL;
263 #ifdef CAM
264 sim->scams = 0; sim->scam = NULL;
265 #endif
266 sim->overlay = NULL;
267
268 return (sim);
269 }
270
271
272 XDisplay *
273 FindXDisplay(Tk_Window tkwin)
274 {
275 XDisplay *xd;
276 int d = 8;
277 unsigned long valuemask = 0;
278 XGCValues values;
279 XColor rgb, *color;
280 Display *dpy = Tk_Display(tkwin);
281 Screen *screen = Tk_Screen(tkwin);
282 #ifdef IS_LINUX
283 char *display = ":0"; /* XXX TODO: fix this for new x libs */
284 #else
285 char *display = dpy->display_name;
286 #endif
287
288 for (xd = XDisplays;
289 xd && (xd->screen != screen);
290 xd = xd->next) ;
291
292 if (xd != NULL) {
293 return (xd);
294 } else {
295 xd = (struct XDisplay *)ckalloc(sizeof (struct XDisplay));
296
297 xd->references = 0;
298 xd->dpy = dpy;
299 xd->display = (char *)ckalloc(strlen(display) + 1);
300 xd->tkDisplay = ((TkWindow *)tkwin)->dispPtr;
301 strcpy(xd->display, display);
302 xd->screen = screen;
303 xd->root = RootWindowOfScreen(xd->screen);
304
305 xd->visual = Tk_DefaultVisual(xd->screen);
306 xd->depth = Tk_DefaultDepth(xd->screen);
307 xd->colormap = Tk_DefaultColormap(xd->screen);
308
309 xd->color = (xd->depth != 1);
310
311 xd->pixels = (int *)ckalloc(16 * sizeof(int));
312 if (xd->color) { /* Color screen */
313 int GotColor = 1;
314
315 #define GETCOLOR(i, name) \
316 if (!GotColor) { \
317 xd->pixels[i] = Rand16() & 255; \
318 } else { \
319 if ((color = Tk_GetColor(tk_mainInterp, tkwin, \
320 None, name)) == NULL) { \
321 xd->pixels[i] = Rand16() & 255; \
322 GotColor = 0; \
323 } else { \
324 switch (xd->depth) { \
325 case 8: \
326 xd->pixels[i] = \
327 color->pixel; \
328 break; \
329 case 15: \
330 if (xd->visual->red_mask == 0x7c00) { \
331 xd->pixels[i] = \
332 (((color->red >> (8 + 3)) & 0x1f) << (5 + 5)) | \
333 (((color->green >> (8 + 2)) & 0x1f) << (5)) | \
334 (((color->blue >> (8 + 3)) & 0x1f) << (0)); \
335 } else { \
336 (((color->blue >> (8 + 3)) & 0x1f) << (5 + 5)) | \
337 (((color->green >> (8 + 2)) & 0x1f) << (5)) | \
338 (((color->red >> (8 + 3)) & 0x1f) << (0)); \
339 } \
340 break; \
341 case 16: \
342 if (xd->visual->red_mask == 0xf800) { \
343 xd->pixels[i] = \
344 (((color->red >> (8 + 3)) & 0x1f) << (6 + 5)) | \
345 (((color->green >> (8 + 2)) & 0x3f) << (5)) | \
346 (((color->blue >> (8 + 3)) & 0x1f) << (0)); \
347 } else { \
348 xd->pixels[i] = \
349 (((color->blue >> (8 + 3)) & 0x1f) << (6 + 5)) | \
350 (((color->green >> (8 + 2)) & 0x3f) << (5)) | \
351 (((color->red >> (8 + 3)) & 0x1f) << (0)); \
352 } \
353 break; \
354 case 24: \
355 case 32: \
356 if (xd->visual->red_mask == 0xff0000) { \
357 xd->pixels[i] = \
358 ((color->red & 0xff) << 16) | \
359 ((color->green & 0xff) << 8) | \
360 ((color->blue & 0xff) << 0); \
361 } else { \
362 xd->pixels[i] = \
363 ((color->blue & 0xff) << 16) | \
364 ((color->green & 0xff) << 8) | \
365 ((color->red & 0xff) << 0); \
366 } \
367 break; \
368 } \
369 } \
370 }
371
372 if ((xd->depth == 8) &&
373 (Tk_DefaultColormap(xd->screen) ==
374 DefaultColormapOfScreen(xd->screen))) {
375 xd->pixels[COLOR_WHITE] = WhitePixelOfScreen(xd->screen);
376 xd->pixels[COLOR_BLACK] = BlackPixelOfScreen(xd->screen);
377 } else {
378 GETCOLOR(COLOR_WHITE, "#ffffff");
379 GETCOLOR(COLOR_BLACK, "#000000");
380 }
381
382 GETCOLOR(COLOR_YELLOW, "#ffff00");
383 GETCOLOR(COLOR_ORANGE, "#ff7f00");
384 GETCOLOR(COLOR_RED, "#ff0000");
385 GETCOLOR(COLOR_DARKRED, "#bf0000");
386 GETCOLOR(COLOR_DARKBLUE, "#0000e6");
387 GETCOLOR(COLOR_LIGHTBLUE, "#6666e6");
388 GETCOLOR(COLOR_BROWN, "#cc4c4c");
389 GETCOLOR(COLOR_LIGHTGREEN, "#00e600");
390 GETCOLOR(COLOR_DARKGREEN, "#007f00");
391 GETCOLOR(COLOR_OLIVE, "#997f4c");
392 GETCOLOR(COLOR_LIGHTBROWN, "#cc7f66");
393 GETCOLOR(COLOR_LIGHTGRAY, "#bfbfbf");
394 GETCOLOR(COLOR_MEDIUMGRAY, "#7f7f7f");
395 GETCOLOR(COLOR_DARKGRAY, "#3f3f3f");
396
397 if (!GotColor) {
398 fprintf(stderr,
399 "Oh, dear. There don't seem to be enough free colors on X display \"%s\".\n",
400 xd->display);
401 fprintf(stderr,
402 "Micropolis will try to run anyway, but might look pretty weird!\n");
403 }
404 } else { /* Black and white screen */
405 int white = WhitePixelOfScreen(xd->screen);
406 int black = BlackPixelOfScreen(xd->screen);
407
408 xd->pixels[COLOR_WHITE] = white;
409 xd->pixels[COLOR_BLACK] = black;
410
411 xd->pixels[COLOR_YELLOW] = white;
412 xd->pixels[COLOR_ORANGE] = white;
413 xd->pixels[COLOR_RED] = white;
414 xd->pixels[COLOR_DARKRED] = black;
415 xd->pixels[COLOR_DARKBLUE] = black;
416 xd->pixels[COLOR_LIGHTBLUE] = white;
417 xd->pixels[COLOR_BROWN] = black;
418 xd->pixels[COLOR_LIGHTGREEN] = white;
419 xd->pixels[COLOR_DARKGREEN] = black;
420 xd->pixels[COLOR_OLIVE] = black;
421 xd->pixels[COLOR_LIGHTBROWN] = white;
422 xd->pixels[COLOR_LIGHTGRAY] = white;
423 xd->pixels[COLOR_MEDIUMGRAY] = white;
424 xd->pixels[COLOR_DARKGRAY] = black;
425 }
426
427 xd->gc = Tk_DefaultGC(xd->screen);
428 XSetForeground(xd->dpy, xd->gc, xd->pixels[COLOR_BLACK]);
429 XSetBackground(xd->dpy, xd->gc, xd->pixels[COLOR_WHITE]);
430 XSetLineAttributes(xd->dpy, xd->gc,
431 1, LineSolid, CapButt, JoinMiter);
432 XSetGraphicsExposures(xd->dpy, xd->gc, False);
433
434 #ifndef MSDOS
435 { int major, minor, event, error, pixmaps;
436 if (WireMode ||
437 (XQueryExtension(xd->dpy, "MIT-SHM", /* Jeez! */
438 &major, &event, &error) != True) ||
439 (XShmQueryVersion(xd->dpy,
440 &major, &minor, &pixmaps) != True)) {
441 fprintf(stderr,
442 "Darn, X display \"%s\" doesn't support the shared memory extension.\n",
443 xd->display);
444 xd->shared = 0;
445 } else {
446 if (!pixmaps) {
447 fprintf(stderr,
448 "Darn, X display \"%s\" claims to support the shared memory extension,\n",
449 xd->display);
450 fprintf(stderr,
451 "but is too lame to support shared memory pixmaps, so Micropolis will run slower.\n");
452 fprintf(stderr,
453 "Please complain to your X server vendor, %s\n",
454 XServerVendor(xd->dpy));
455 xd->shared = -1;
456 } else {
457 fprintf(stderr,
458 "Cool, I found the shared memory extension!\n");
459 fprintf(stderr,
460 "Disabled SHM, because it is currently broken!\n");
461 xd->shared = 0;
462 }
463 }
464 }
465 #else
466 xd->shared = 0;
467 #endif
468
469 xd->request = -1;
470 xd->last_request_read = -1;
471 xd->big_tile_pixmap = None;
472 xd->objects = NULL;
473 xd->overlay_gc = NULL;
474 xd->gray25_stipple = None;
475 xd->gray50_stipple = None;
476 xd->gray75_stipple = None;
477 xd->vert_stipple = None;
478 xd->horiz_stipple = None;
479 xd->diag_stipple = None;
480
481 xd->big_tile_image = xd->small_tile_image = NULL;
482
483 xd->next = XDisplays; XDisplays = xd;
484 }
485
486 return (xd);
487 }
488
489
490 IncRefDisplay(XDisplay *xd)
491 {
492 xd->references++;
493 }
494
495
496 DecRefDisplay(XDisplay *xd)
497 {
498 if ((--xd->references) == 0) {
499 /* I'd blow it away, but tk may still be using the display */
500 }
501 }
502
503
504 SimView *
505 InitNewView(SimView *view, char *title, int class, int w, int h)
506 {
507 int type, i;
508 int test = 1;
509 int d = 8;
510 unsigned long valuemask = 0;
511 char *t;
512 struct XDisplay *xd;
513 XGCValues values;
514 XColor rgb, *color;
515
516 t = (char *)ckalloc(strlen(title) + 1);
517 strcpy(t, title);
518
519 view->next = NULL;
520 view->title = t;
521 view->type = -1;
522 view->class = class;
523 view->bigtiles = view->smalltiles = NULL;
524 view->pixels = NULL;
525 view->line_bytes = 0;
526 view->line_bytes8 = 0;
527 view->pixel_bytes = 0;
528 view->depth = 0;
529 view->data = NULL;
530 view->data8 = NULL;
531 view->visible = 0;
532 view->invalid = 0;
533 view->skips = view->skip = 0;
534 view->update = 0;
535 view->map_state = ALMAP;
536 view->show_editors = 1;
537 view->tool_showing = 0;
538 view->tool_mode = 0;
539 view->tool_x = view->tool_y = 0;
540 view->tool_x_const = view->tool_y_const = -1;
541 view->tool_state = dozeState;
542 view->tool_state_save = -1;
543 view->super_user = 0;
544 view->show_me = 1;
545 view->dynamic_filter = 0;
546 view->auto_scroll_token = 0;
547 view->tool_event_time = 0;
548 view->tool_last_event_time = 0;
549 view->w_x = view->w_y = 0;
550 view->w_width = view->w_height = 16;
551 view->m_width = view->m_height = 0;
552 view->i_width = w; view->i_height = h;
553 view->pan_x = view->pan_y = 0;
554 view->tile_x = view->tile_y = 0;
555 view->tile_width = view->tile_height = 0;
556 view->screen_x = view->screen_y = 0;
557 view->screen_width = view->screen_height = 0;
558 view->last_x = view->last_y = view->last_button = 0;
559 view->track_info = NULL;
560 view->message_var = NULL;
561
562 /* This stuff was initialized in our caller (SimViewCmd) */
563 /* view->tkwin = NULL; */
564 /* view->interp = NULL; */
565 /* view->flags = 0; */
566
567 view->x = NULL;
568 view->shminfo = NULL;
569 view->tiles = NULL;
570 view->other_tiles = NULL;
571 view->image = NULL;
572 view->other_image = NULL;
573 view->other_data = NULL;
574 view->pixmap = None;
575 view->pixmap2 = None;
576 view->overlay_pixmap = None;
577 view->overlay_valid = 0;
578 view->fontPtr = NULL;
579 view->updates = 0;
580 view->update_real = view->update_user = view->update_system = 0.0;
581 view->update_context = 0;
582 view->auto_goto = 0;
583 view->auto_going = 0;
584 view->auto_x_goal = view->auto_x_goal = 0;
585 view->auto_speed = 75;
586 view->follow = NULL;
587 view->sound = 1;
588 view->width = 0; view->height = 0;
589 view->show_overlay = 1;
590 view->overlay_mode = 0;
591
592 view->x = FindXDisplay(view->tkwin);
593 IncRefDisplay(view->x);
594
595 /* view->x->shared is 1 if the shared memory extension is present and
596 supports shared memory pixmaps, and -1 if it is present but doesn't. */
597 if (view->x->shared != 1) {
598 view->type = X_Wire_View;
599 } else {
600 view->type = X_Mem_View;
601 }
602
603 view->x->needs_swap = !(*(unsigned char*) (&test));
604
605
606 GetPixmaps(view->x);
607 view->pixels = view->x->pixels;
608
609 if (w == EDITOR_W) w = 256; /* XXX */
610 if (h == EDITOR_H) h = 256; /* XXX */
611
612 view->pan_x = w / 2; view->pan_y = h / 2;
613 DoResizeView(view, w, h);
614
615 GetViewTiles(view);
616
617 return (view);
618 }
619
620
621 DestroyView(SimView *view)
622 {
623 SimView **vp;
624
625 CancelRedrawView(view);
626
627 for (vp = ((view->class == Editor_Class) ?
628 (&sim->editor) : (&sim->map));
629 (*vp) != NULL;
630 vp = &((*vp)->next)) {
631 if ((*vp) == view) {
632 (*vp) = view->next;
633 if (view->class == Editor_Class)
634 sim->editors--;
635 else
636 sim->maps--;
637
638 break;
639 }
640 }
641
642 if (view->title != NULL) {
643 ckfree (view->title);
644 view->title = NULL;
645 }
646
647 if (view->pixmap != None) {
648 XFreePixmap(view->x->dpy, view->pixmap);
649 view->pixmap = None;
650 }
651
652 if (view->pixmap2 != None) {
653 XFreePixmap(view->x->dpy, view->pixmap2);
654 view->pixmap2 = None;
655 }
656
657 if (view->overlay_pixmap != None) {
658 XFreePixmap(view->x->dpy, view->overlay_pixmap);
659 view->overlay_pixmap = None;
660 }
661
662 if (view->auto_scroll_token) {
663 Tk_DeleteTimerHandler(view->auto_scroll_token);
664 view->auto_scroll_token = 0;
665 }
666
667 #ifndef MSDOS
668 if (view->shminfo) {
669 XShmDetach(view->x->dpy, view->shminfo);
670 shmdt(view->shminfo->shmaddr);
671 shmctl(view->shminfo->shmid, IPC_RMID, 0);
672 ckfree(view->shminfo);
673 view->shminfo = NULL;
674 if (view->image) {
675 view->image->data = NULL;
676 view->data = NULL;
677 XDestroyImage(view->image);
678 view->image = NULL;
679 }
680 } else {
681 #endif
682 if (view->image) {
683 if (view->image->data) {
684 ckfree(view->image->data);
685 view->image->data = NULL;
686 }
687 view->data = NULL;
688 XDestroyImage(view->image);
689 view->image = NULL;
690 }
691 #ifndef MSDOS
692 }
693 #endif
694
695 if (view->other_image) {
696 if (view->other_image->data) {
697 ckfree(view->other_image->data);
698 view->other_image->data = NULL;
699 }
700 view->other_data = NULL;
701 XDestroyImage(view->other_image);
702 view->other_image = NULL;
703 }
704
705 if (view->tiles)
706 FreeTiles(view);
707
708 DecRefDisplay(view->x);
709
710 ckfree((char *) view);
711 }
712
713
714 unsigned char *
715 AllocPixels(int len, unsigned char pixel)
716 {
717 int i;
718 unsigned char *data, *cp;
719
720 cp = data = (unsigned char *)ckalloc(len);
721 for (i = len; i > 0; i--) {
722 *(cp++) = pixel;
723 }
724
725 return (data);
726 }
727
728
729 DoResizeView(SimView *view, int w, int h)
730 {
731 int resize = 0;
732
733 view->w_width = w;
734 view->w_height = h;
735
736 if (view->class == Map_Class) { /* Map_Class */
737 view->m_width = w;
738 view->m_height = h;
739
740 if (view->pixmap2 == None) {
741
742 view->pixmap2 = XCreatePixmap(view->x->dpy, view->x->root,
743 w, h, view->x->depth);
744 if (view->pixmap2 == None) {
745 fprintf(stderr,
746 "Sorry, Micropolis can't create a pixmap on X display \"%s\"!\n",
747 view->x->display);
748 sim_exit(1); // Just sets tkMustExit and ExitReturn
749 return;
750 }
751 }
752
753 } else { /* Editor_Class */
754
755 if ((w = (w + 31) & (~15)) > view->m_width)
756 view->m_width = w, resize++;
757 if ((h = (h + 31) & (~15)) > view->m_height)
758 view->m_height = h, resize++;
759
760 if (resize || (view->pixmap2 == None)) {
761 if (view->pixmap2 != None) {
762 XFreePixmap(view->x->dpy, view->pixmap2);
763 view->pixmap2 = None;
764 }
765 view->pixmap2 = XCreatePixmap(view->x->dpy, view->x->root,
766 view->m_width, view->m_height,
767 view->x->depth);
768 if (view->pixmap2 == None) {
769 fprintf(stderr,
770 "Sorry, Micropolis couldn't create a pixmap on X display \"%s\"!\n",
771 view->x->display);
772 sim_exit(1); // Just sets tkMustExit and ExitReturn
773 return;
774 }
775 }
776
777 if (resize || (view->overlay_pixmap == None)) {
778 view->overlay_mode = 0;
779 if (view->overlay_pixmap != None) {
780 XFreePixmap(view->x->dpy, view->overlay_pixmap);
781 view->overlay_pixmap = None;
782 }
783 view->overlay_pixmap = XCreatePixmap(view->x->dpy, view->x->root,
784 view->m_width, view->m_height,
785 1);
786 if (view->overlay_pixmap == None) {
787 fprintf(stderr,
788 "Sorry, Micropolis couldn't create another pixmap on X display \"%s\".\n",
789 view->x->display);
790 sim_exit(1); // Just sets tkMustExit and ExitReturn
791 return;
792 }
793 if (view->x->overlay_gc == NULL) {
794 unsigned long valuemask = 0;
795 XGCValues values;
796
797 view->x->overlay_gc =
798 XCreateGC(view->x->dpy, view->overlay_pixmap, valuemask, &values);
799 XSetForeground(view->x->dpy, view->x->overlay_gc, 0);
800 XSetBackground(view->x->dpy, view->x->overlay_gc, 1);
801 XSetLineAttributes(view->x->dpy, view->x->overlay_gc,
802 1, LineSolid, CapButt, JoinMiter);
803 XSetGraphicsExposures(view->x->dpy, view->x->overlay_gc, False);
804 }
805 }
806
807 }
808
809 #ifndef MSDOS
810 if (view->type != X_Mem_View) {
811 goto SPRING_FORWARD;
812 }
813
814 if (resize || (view->image == NULL)) {
815 if (view->shminfo && view->image) {
816 if (view->pixmap != None) {
817 XFreePixmap(view->x->dpy, view->pixmap);
818 view->pixmap = None;
819 }
820 XShmDetach(view->x->dpy, view->shminfo);
821 shmdt(view->shminfo->shmaddr);
822 shmctl(view->shminfo->shmid, IPC_RMID, 0);
823 view->image->data = NULL;
824 if (view->data == view->data8)
825 view->data8 = NULL;
826 view->data = NULL;
827 XDestroyImage(view->image);
828 view->image = NULL;
829 }
830
831 #if 0
832 /* XShmPixmapFormat is documented but does not exist !!! */
833 if (XShmPixmapFormat(view->x->dpy) != ZPixmap) {
834 fprintf(stderr,
835 "Darn, display \"%s\" has the wrong shared memory format.\n",
836 view->x->display);
837 goto FALL_BACK;
838 }
839 #endif
840
841 if (!view->shminfo) {
842 view->shminfo = (XShmSegmentInfo *)ckalloc(sizeof (XShmSegmentInfo));
843 }
844
845 view->image =
846 XShmCreateImage(view->x->dpy, view->x->visual, view->x->depth,
847 view->x->color ? ZPixmap : XYBitmap,
848 NULL, view->shminfo,
849 view->m_width, view->m_height);
850
851 view->line_bytes = view->image->bytes_per_line;
852
853 switch (view->x->depth) {
854
855 case 1:
856 view->pixel_bytes = 0;
857 view->depth = 1;
858 break;
859
860 case 8:
861 view->pixel_bytes = 1;
862 view->depth = 8;
863 break;
864
865 case 15:
866 view->pixel_bytes = 2;
867 view->depth = 15;
868 break;
869
870 case 16:
871 view->pixel_bytes = 2;
872 view->depth = 16;
873 break;
874
875 case 24:
876 /* XXX: TODO: 24 and 32 bit support */
877 view->pixel_bytes = 4;
878 //view->pixel_bytes = 3;
879 view->depth = 24;
880 break;
881
882 case 32:
883 /* XXX: TODO: 24 and 32 bit support */
884 view->pixel_bytes = 4;
885 view->depth = 32;
886 break;
887
888 default:
889 view->pixel_bytes = 0;
890 view->depth = 0;
891 break;
892
893 } // switch
894
895 view->shminfo->shmid = shmget(IPC_PRIVATE,
896 (view->line_bytes *
897 view->m_height),
898 (IPC_CREAT | 0777));
899 if (view->shminfo->shmid < 0) {
900 perror("shmget");
901 fprintf(stderr,
902 "Darn, Micropolis can't share memory with X display \"%s\".\n",
903 view->x->display);
904 goto FALL_BACK;
905 }
906
907 view->data = (unsigned char *)shmat(view->shminfo->shmid, 0, 0);
908 if ((int)view->data == -1) {
909 perror("shmat");
910 fprintf(stderr,
911 "Darn, Micropolis can't find any memory to share with display \"%s\".\n",
912 view->x->display);
913 goto FALL_BACK;
914 }
915
916 view->image->data = (char *)view->data;
917 view->shminfo->shmaddr = (char *)view->data;
918 view->shminfo->readOnly = False;
919
920 { int (*old)();
921 int result;
922 int attached = 0;
923 GotXError = 0;
924 old = XSetErrorHandler(CatchXError);
925
926 result =
927 XShmAttach(view->x->dpy, view->shminfo);
928 if (result == 0) {
929 fprintf(stderr,
930 "Darn, the X display \"%s\" can't access Micropolis's shared memory.\n",
931 view->x->display);
932 GotXError = 1;
933 }
934
935 XSync(view->x->dpy, False);
936
937 if (!GotXError) {
938 attached = 1;
939 view->pixmap = XShmCreatePixmap(view->x->dpy, view->x->root,
940 view->data, view->shminfo,
941 view->m_width, view->m_height,
942 view->x->depth);
943 XSync(view->x->dpy, False);
944
945 if (GotXError ||
946 (view->pixmap == None)) {
947 fprintf(stderr,
948 "Darn, Micropolis couldn't get a shared memory pixmap on X display \"%s\".\n",
949 view->x->display);
950 GotXError = 1;
951 }
952 }
953
954 XSetErrorHandler(old);
955
956 if (GotXError) {
957 view->pixmap = None;
958 if (attached) {
959 XShmDetach(view->x->dpy, view->shminfo);
960 } // if
961 result = shmdt(view->shminfo->shmaddr);
962 result = shmctl(view->shminfo->shmid, IPC_RMID, 0);
963 ckfree(view->shminfo);
964 view->shminfo = NULL;
965 if (view->image) {
966 view->image->data = NULL;
967 view->data = NULL;
968 XDestroyImage(view->image);
969 view->image = NULL;
970 }
971 goto FALL_BACK;
972 }
973
974 if (view->x->color) {
975 XSetForeground(view->x->dpy, view->x->gc,
976 view->pixels[COLOR_LIGHTBROWN]);
977 } else {
978 XSetForeground(view->x->dpy, view->x->gc,
979 view->pixels[COLOR_WHITE]);
980 }
981
982 XFillRectangle(view->x->dpy, view->pixmap, view->x->gc,
983 0, 0, view->m_width, view->m_height);
984 }
985 }
986
987 goto FINISH;
988
989 FALL_BACK:
990
991 fprintf(stderr,
992 "Falling back to the X network protocol on display \"%s\"...\n",
993 view->x->display);
994 #endif
995
996 view->x->shared = 0;
997 view->type = X_Wire_View;
998 if (view->pixmap != None) {
999 XFreePixmap(view->x->dpy, view->pixmap);
1000 view->pixmap = None;
1001 }
1002 #ifndef MSDOS
1003 if (view->shminfo) {
1004 if (view->shminfo->shmid >= 0) {
1005 if (view->shminfo->shmaddr) {
1006 shmdt(view->shminfo->shmaddr);
1007 }
1008 shmctl(view->shminfo->shmid, IPC_RMID, 0);
1009 }
1010 ckfree((char *)view->shminfo);
1011 view->shminfo = NULL;
1012 }
1013 #endif
1014 if (view->image) {
1015 view->image->data = NULL;
1016 XDestroyImage(view->image);
1017 view->image = NULL;
1018 }
1019 view->data = NULL;
1020 view->line_bytes = 0;
1021 view->pixel_bytes = 0;
1022 view->depth = 0;
1023
1024 SPRING_FORWARD:
1025
1026 if (resize || (view->pixmap == None)) {
1027 if (view->pixmap != None) {
1028 XFreePixmap(view->x->dpy, view->pixmap);
1029 view->pixmap = None;
1030 }
1031 view->pixmap = XCreatePixmap(view->x->dpy, view->x->root,
1032 view->m_width, view->m_height,
1033 view->x->depth);
1034 if (view->pixmap == None) {
1035 fprintf(stderr,
1036 "Sorry, Micropolis can't create pixmap on X display \"%s\".\n",
1037 view->x->display);
1038 sim_exit(1); // Just sets tkMustExit and ExitReturn
1039 return;
1040 }
1041 if (view->x->color) {
1042 XSetForeground(view->x->dpy, view->x->gc,
1043 view->pixels[COLOR_LIGHTBROWN]);
1044 } else {
1045 XSetForeground(view->x->dpy, view->x->gc,
1046 view->pixels[COLOR_WHITE]);
1047 }
1048 XFillRectangle(view->x->dpy, view->pixmap, view->x->gc,
1049 0, 0, view->m_width, view->m_height);
1050 }
1051
1052 FINISH:
1053
1054 if (view->class == Editor_Class) {
1055
1056 AllocTiles(view);
1057 DoAdjustPan(view);
1058
1059 } else if (view->class == Map_Class) {
1060
1061 if (view->type == X_Mem_View) { /* Memory Map */
1062
1063 if (view->x->color) {
1064
1065 /* Color, Shared Memory */
1066
1067 view->data8 = view->data;
1068 view->line_bytes8 = view->line_bytes; /* XXX: ??? */
1069
1070 switch (view->x->depth) {
1071
1072 case 8:
1073 view->pixel_bytes = 1;
1074 view->depth = 8;
1075 break;
1076
1077 case 15:
1078 view->pixel_bytes = 2;
1079 view->depth = 15;
1080 break;
1081
1082 case 16:
1083 view->pixel_bytes = 2;
1084 view->depth = 16;
1085 break;
1086
1087 case 24:
1088 /* XXX: TODO: 24 and 32 bit support */
1089 view->pixel_bytes = 4;
1090 //view->pixel_bytes = 3;
1091 view->depth = 24;
1092 break;
1093
1094 case 32:
1095 /* XXX: TODO: 24 and 32 bit support */
1096 view->pixel_bytes = 4;
1097 view->depth = 32;
1098 break;
1099
1100 default:
1101 view->pixel_bytes = 0;
1102 view->depth = 0;
1103 break;
1104
1105 } // switch
1106
1107 } else {
1108
1109 /* Black and White, Shared Memory */
1110
1111 if (view->other_image != NULL) {
1112 XDestroyImage(view->other_image);
1113 }
1114
1115 view->line_bytes8 = view->m_width; /* XXX: fix depth */
1116 view->pixel_bytes = 0;
1117 view->depth = 1;
1118
1119 view->other_data = view->data8 =
1120 AllocPixels(view->m_height * view->line_bytes8, /* XXX: fix depth */
1121 view->pixels[COLOR_WHITE]);
1122 view->other_image =
1123 XCreateImage(view->x->dpy, view->x->visual, 8, /* XXX: fix depth */
1124 ZPixmap, 0, (char *)view->other_data,
1125 view->m_width, view->m_height,
1126 8, view->line_bytes8); /* XXX: fix depth */
1127 }
1128
1129 } else { /* Wire Map */
1130 int bitmap_pad;
1131 int bitmap_depth;
1132
1133 if (view->image != NULL) {
1134 XDestroyImage(view->image);
1135 view->image = NULL;
1136 }
1137
1138 if (view->other_image != NULL) {
1139 XDestroyImage(view->other_image);
1140 view->other_image = NULL;
1141 }
1142
1143 if (view->x->color) {
1144
1145 /* Color, Wire */
1146
1147 switch (view->x->depth) {
1148
1149 case 8:
1150 view->pixel_bytes = 1;
1151 view->depth = 8;
1152 bitmap_pad = 8;
1153 bitmap_depth = 8;
1154 view->line_bytes8 =
1155 ((view->m_width * view->pixel_bytes) + 3) & (~3);
1156 break;
1157
1158 case 15:
1159 view->pixel_bytes = 2;
1160 view->depth = 15;
1161 bitmap_pad = 16;
1162 bitmap_depth = 15;
1163 view->line_bytes8 =
1164 ((view->m_width * view->pixel_bytes) + 3) & (~3);
1165 break;
1166
1167 case 16:
1168 view->pixel_bytes = 2;
1169 view->depth = 16;
1170 bitmap_pad = 16;
1171 bitmap_depth = 16;
1172 view->line_bytes8 =
1173 ((view->m_width * view->pixel_bytes) + 3) & (~3);
1174 break;
1175
1176 case 24:
1177 view->pixel_bytes = 4;
1178 //view->pixel_bytes = 3;
1179 view->depth = 24;
1180 bitmap_depth = 24;
1181 bitmap_pad = 32;
1182 view->line_bytes8 =
1183 ((view->m_width * 4) + 3) & (~3);
1184 break;
1185
1186 case 32:
1187 view->pixel_bytes = 4;
1188 view->depth = 32;
1189 bitmap_pad = 32;
1190 bitmap_depth = 32;
1191 view->line_bytes8 =
1192 ((view->m_width * 4) + 3) & (~3);
1193 break;
1194
1195 default:
1196 assert(0); /* Unknown depth */
1197 break;
1198
1199 } // switch
1200
1201 view->line_bytes =
1202 view->line_bytes8;
1203
1204 } else {
1205
1206 /* Black and White, Wire */
1207
1208 view->pixel_bytes = 0;
1209 view->depth = 1;
1210 view->line_bytes8 =
1211 (view->m_width + 3) & (~3); /* XXX: handle depth */
1212 view->line_bytes =
1213 (view->m_width + 7) >>3;
1214 bitmap_pad = 8;
1215 bitmap_depth = 8;
1216
1217 }
1218
1219 view->data =
1220 AllocPixels(view->m_height * view->line_bytes, 0);
1221 view->image =
1222 XCreateImage(view->x->dpy, view->x->visual,
1223 bitmap_depth,
1224 view->x->color ? ZPixmap : XYBitmap,
1225 0, (char *)view->data,
1226 view->m_width, view->m_height,
1227 bitmap_pad,
1228 view->line_bytes);
1229
1230 view->other_data =
1231 AllocPixels(view->m_height * view->line_bytes8, 0);
1232 view->other_image =
1233 XCreateImage(view->x->dpy, view->x->visual,
1234 bitmap_depth,
1235 ZPixmap,
1236 0, (char *)view->other_data,
1237 view->m_width, view->m_height,
1238 bitmap_pad,
1239 view->line_bytes);
1240
1241 if (view->x->color) {
1242 view->data8 = view->data;
1243 } else {
1244 view->data8 = view->other_data;
1245 }
1246 }
1247 }
1248 }
1249
1250
1251 DoPanBy(struct SimView *view, int dx, int dy)
1252 {
1253 DoPanTo(view, view->pan_x + dx, view->pan_y + dy);
1254 }
1255
1256
1257 DoPanTo(struct SimView *view, int x, int y)
1258 {
1259 if (view->class != Editor_Class) {
1260 return;
1261 }
1262
1263 if (x < 0) x = 0;
1264 if (y < 0) y = 0;
1265 if (x > view->i_width) x = view->i_width - 1;
1266 if (y > view->i_height) y = view->i_height - 1;
1267 if ((view->pan_x != x) ||
1268 (view->pan_y != y)) {
1269 view->pan_x = x;
1270 view->pan_y = y;
1271 DoAdjustPan(view);
1272 }
1273 }
1274
1275 /* #define DEBUG_PAN */
1276
1277 DoAdjustPan(struct SimView *view)
1278 {
1279 int ww2 = view->w_width >>1, wh2 = view->w_height >>1;
1280 int px = view->pan_x, py = view->pan_y;
1281 int last_tile_x = view->tile_x, last_tile_y = view->tile_y;
1282 int last_tile_width = view->tile_width, last_tile_height = view->tile_height;
1283 int total_width = view->m_width >>4, total_height = view->m_height >>4;
1284 //fprintf(stderr, "DoAdjustPan\n");
1285
1286 #ifdef DEBUG_PAN
1287 printf("AdjustPan window %d %d ww2 %d wh2 %d pan %d %d\n",
1288 view->w_width, view->w_height, ww2, wh2, px, py);
1289 printf(" last tile %d %d %d %d\n",
1290 last_tile_x, last_tile_y, last_tile_width, last_tile_height);
1291 #endif
1292
1293 if ((view->tile_x = ((px - ww2) >>4)) < 0)
1294 view->tile_x = 0;
1295 if ((view->tile_y = ((py - wh2) >>4)) < 0)
1296 view->tile_y = 0;
1297
1298 #ifdef DEBUG_PAN
1299 printf(" now tile %d %d\n", view->tile_x, view->tile_y);
1300 #endif
1301
1302 view->tile_width = ((15 + px + ww2) >>4);
1303 view->tile_height = ((15 + py + wh2) >>4);
1304
1305 #ifdef DEBUG_PAN
1306 printf(" outer tile %d %d\n", view->tile_width, view->tile_height);
1307 #endif
1308
1309 if (view->tile_width > (view->i_width >>4))
1310 view->tile_width = (view->i_width >>4);
1311 view->tile_width -= view->tile_x;
1312 if (view->tile_height > (view->i_height >>4))
1313 view->tile_height = (view->i_height >>4);
1314 view->tile_height -= view->tile_y;
1315
1316 #ifdef DEBUG_PAN
1317 printf(" tile size %d %d\n", view->tile_width, view->tile_height);
1318 #endif
1319
1320 if (view->tile_width > (view->m_width >>4))
1321 view->tile_width = (view->m_width >>4);
1322 if (view->tile_height > (view->m_height >>4))
1323 view->tile_height = (view->m_height >>4);
1324
1325 #ifdef DEBUG_PAN
1326 printf(" clipped size %d %d\n", view->tile_width, view->tile_height);
1327 printf(" maximum size %d %d\n", view->m_width >>4, view->m_height >>4);
1328 #endif
1329
1330 view->screen_x = (ww2 - px) + (view->tile_x <<4);
1331 view->screen_y = (wh2 - py) + (view->tile_y <<4);
1332 view->screen_width = (view->tile_width <<4);
1333 view->screen_height = (view->tile_height <<4);
1334
1335 #ifdef DEBUG_PAN
1336 printf(" screen %d %d %d %d\n",
1337 view->screen_x, view->screen_y,
1338 view->screen_width, view->screen_height);
1339 #endif
1340
1341 view->overlay_mode = 0;
1342 // view->skip = 0;
1343 view->invalid = 1;
1344 if (SimSpeed == 0) {
1345 EventuallyRedrawView(view);
1346 }
1347 /* InvalidateEditors(); */
1348 if (view->show_me) {
1349 RedrawMaps();
1350 }
1351 /* FixMicropolisTimer(); */
1352
1353 { int dx = last_tile_x - view->tile_x,
1354 dy = last_tile_y - view->tile_y;
1355 short **want = view->other_tiles,
1356 **have = view->tiles;
1357
1358 #ifdef DEBUG_PAN
1359 printf("scrolling %d %d\n", dx, dy);
1360 #endif
1361
1362 if ((dx != 0) || (dy != 0)) {
1363 int row, col, x, y,
1364 width = view->tile_width,
1365 height = view->tile_height;
1366
1367 for (col = 0; col < width; col++)
1368 memcpy(want[col], have[col], (height * sizeof(short)));
1369
1370 for (col = 0; col < total_width; col++) {
1371 x = col - dx;
1372 for (row = 0; row < total_height; row++) {
1373 y = row - dy;
1374 if ((x >= 0) && (x < width) &&
1375 (y >= 0) && (y < height)) {
1376 have[col][row] = want[x][y];
1377 } else {
1378 have[col][row] = -1;
1379 }
1380 }
1381 }
1382
1383 XCopyArea(view->x->dpy, view->pixmap, view->pixmap, view->x->gc,
1384 0, 0, view->tile_width <<4, view->tile_height <<4,
1385 dx <<4, dy <<4);
1386
1387 if (view->type == X_Mem_View) {
1388 XSync(view->x->dpy, False);
1389 }
1390 }
1391 }
1392 }
1393
1394
1395 AllocTiles(SimView *view)
1396 {
1397 int row, col;
1398 short **have, **want;
1399 int w = view->m_width / 16, h = view->m_height / 16;
1400 int n = (w + 1) * sizeof (short *);
1401
1402 if (view->tiles)
1403 FreeTiles(view);
1404
1405 have = view->tiles =
1406 (short **)ckalloc(n);
1407
1408 want = view->other_tiles =
1409 (short **)ckalloc(n);
1410
1411 have[w] = want[w] = NULL;
1412
1413 n = h * sizeof(short);
1414 for (col = 0; col < w; col++) {
1415
1416 have[col] = (short *)ckalloc(n);
1417 want[col] = (short *)ckalloc(n);
1418 for (row = 0; row < h; row++) {
1419 have[col][row] = -1;
1420 want[col][row] = -1;
1421 }
1422 }
1423 }
1424
1425
1426 FreeTiles(SimView *view)
1427 {
1428 int col;
1429
1430 for (col = 0; view->tiles[col] != NULL; col++) {
1431 ckfree ((char *)view->tiles[col]);
1432 ckfree ((char *)view->other_tiles[col]);
1433 }
1434 ckfree ((char *)view->tiles);
1435 view->tiles = NULL;
1436 ckfree ((char *)view->other_tiles);
1437 view->other_tiles = NULL;
1438 }
1439
1440
1441 #define POINT_BATCH 32
1442
1443 Ink *OldInk = NULL;
1444
1445
1446 /* XXX: todo: ink locking so someone doesn't erase ink that's being drawn */
1447
1448 Ink *
1449 NewInk()
1450 {
1451 Ink *ink;
1452
1453 if (OldInk) {
1454 ink = OldInk;
1455 OldInk = OldInk->next;
1456 } else {
1457 ink = (Ink *)ckalloc(sizeof(Ink));
1458 ink->maxlength = POINT_BATCH;
1459 ink->points = (XPoint *)ckalloc(POINT_BATCH * sizeof(XPoint));
1460 }
1461 ink->length = 0;
1462 ink->color = COLOR_WHITE;
1463 ink->next = NULL;
1464 ink->left = ink->right = ink->top = ink->bottom =
1465 ink->last_x = ink->last_y = -1;
1466 return (ink);
1467 }
1468
1469
1470 FreeInk(Ink *ink)
1471 {
1472 ink->next = OldInk;
1473 OldInk = ink;
1474 }
1475
1476
1477 StartInk(Ink *ink, int x, int y)
1478 {
1479 ink->length = 1;
1480 ink->left = ink->right = ink->last_x = ink->points[0].x = x;
1481 ink->top = ink->bottom = ink->last_y = ink->points[0].y = y;
1482 }
1483
1484
1485 AddInk(Ink *ink, int x, int y)
1486 {
1487 int dx = x - ink->last_x;
1488 int dy = y - ink->last_y;
1489
1490 if ((dx != 0) || (dy != 0)) {
1491 /*
1492 if (ink->length > 1) {
1493 if ((dx == 0) &&
1494 (ink->points[ink->length - 1].x == 0) &&
1495 ((ink->points[ink->length - 1].y < 0) ?
1496 (dy < 0) : (dy > 0))) {
1497 ink->points[ink->length - 1].y += dy;
1498 goto ADJUST;
1499 } else if ((dy == 0) &&
1500 (ink->points[ink->length - 1].y == 0) &&
1501 ((ink->points[ink->length - 1].x < 0) ?
1502 (dx < 0) : (dx > 0))) {
1503 ink->points[ink->length - 1].x += dx;
1504 goto ADJUST;
1505 }
1506 }
1507 */
1508
1509 if (ink->length >= ink->maxlength) {
1510 ink->maxlength += POINT_BATCH;
1511 ink->points = (XPoint *)realloc((void *)ink->points,
1512 ink->maxlength * sizeof(XPoint));
1513 }
1514 ink->points[ink->length].x = dx;
1515 ink->points[ink->length].y = dy;
1516 ink->length++;
1517
1518 ADJUST:
1519 if (x < ink->left)
1520 ink->left = x;
1521 if (x > ink->right)
1522 ink->right = x;
1523 if (y < ink->top)
1524 ink->top = y;
1525 if (y > ink->bottom)
1526 ink->bottom = y;
1527
1528 { int left, right, top, bottom;
1529 SimView *view;
1530
1531 if (ink->last_x < x) { left = ink->last_x; right = x; }
1532 else { left = x; right = ink->last_x; }
1533 if (ink->last_y < y) { top = ink->last_y; bottom = y; }
1534 else { top = y; bottom = ink->last_y; }
1535
1536 left -= 5; right += 5; top -= 5; bottom += 5;
1537
1538 for (view = sim->editor; view != NULL; view = view->next) {
1539 int vleft, vtop;
1540
1541 if ((right >= (vleft = view->pan_x - (view->w_width / 2))) &&
1542 (left <= vleft + view->w_width) &&
1543 (bottom >= (vtop = view->pan_y - (view->w_height / 2))) &&
1544 (top <= vtop + view->w_height)) {
1545 /* XXX: do studly incremental update instead */
1546 view->overlay_mode = 0;
1547 EventuallyRedrawView(view);
1548 }
1549 }
1550 }
1551 ink->last_x = x; ink->last_y = y;
1552 }
1553 }
1554
1555
1556 EraseOverlay()
1557 {
1558 Ink *ink;
1559
1560 while (sim->overlay) {
1561 ink = sim->overlay;
1562 sim->overlay = ink->next;
1563 FreeInk(ink);
1564 }
1565 }
Impressum, Datenschutz