-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.c
575 lines (446 loc) · 14.8 KB
/
room.c
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/**********************************************************************
* room.c - Room object definition
*
* Copyright 1993, David Nedde
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for any purpose and without fee is granted
* provided that the above copyright notice appears in all copies.
* It is provided "as is" without express or implied warranty.
**********************************************************************/
/* System Headers */
#include <stdio.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/cursorfont.h>
#ifdef MOTIF
#include <Xm/DrawingA.h>
#endif
#ifdef ATHENA
#include <X11/Xaw_d/DrawingA.h>
#endif
/* Local headers */
#include "room.h"
#include "misc.h"
#include "miscx.h"
#include "scrollbar.h"
/* Macros */
/* This is the spacing between the balls representing pixels on the bitmap */
#define SF 1.5 /* FIX - make a resource */
/* Functions */
static void redisplay_cb(/*w, room, call_data*/);
static void resize_cb(/*w, datap_ptr, call_data*/);
static void process_input_cb(/*w, room, call_data*/);
static void wait_for_resize(/* room*/);
static Widget create_canvas(/* parent, name*/);
/* Object public methods */
/* Create the room object */
room_type room__create( form_w, items, file_sel)
Widget form_w;
items_type items;
file_sel_type file_sel;
{
Dimension right_wall;
Dimension floor;
Widget canvas;
room_type room = XtNew( room_struct_type);
room->item = (item_type)0;
room->items = items;
room->file_sel = file_sel;
room->left_wall = int2intf(0);
room->ceiling = int2intf(0);
/* room->gravity is set in xball_sys via scrollbar callback */
room->button1 = False;
room->button2 = False;
room->button3 = False;
room->canvas = canvas = create_canvas( room, form_w, "canvas");
XtVaGetValues(canvas,
XtNbackground, &room->background,
XtNwidth, &right_wall,
XtNheight, &floor,
NULL);
room->right_wall = int2intf( right_wall);
room->floor = int2intf( floor);
return room;
}
/* Destroy the room object */
void room__destroy( room)
room_type room;
{
XtDestroyWidget( room->canvas);
XtFree( (char *)room);
}
/* Called when the scrollbar changes. Sets the gravity value */
void room__set_gravity_cb(w, room, call_data)
Widget w;
room_type room;
scrollbarCallbackStruct * call_data;
{
room->gravity = call_data->value;
}
/* A menu callback that clears the room of all balls */
void room__clear_mcb(w, room, call_data, extra)
Widget w;
room_type room;
caddr_t call_data;
char * extra;
{
items__destroy_all( room->items);
}
/* A menu callback that randomizes all balls in the room */
void room__randomize_mcb(w, room, call_data, extra)
Widget w;
room_type room;
caddr_t call_data;
char * extra;
{
item_type item;
for (item = items__get_first( room->items);
item != (item_type)0;
item = items__get_next( room->items))
{
item__randomize( item, -16, 16, -16, 16);
}
}
/* A menu callback that queries the user for a bitmap and then loads it */
void room__load_bitmap_mcb(w, room, call_data, extra)
Widget w;
room_type room;
caddr_t call_data;
char * extra;
{
char *file_name = file_sel__display( room->file_sel, "Load Bitmap");
if (file_name != (char *)0)
room__load_bitmap( room, file_name, 10, 10);
}
/* Loads the specified bitmap file at the specified position in the room */
void room__load_bitmap(room, filename, pos_x, pos_y)
room_type room;
char * filename;
int pos_x;
int pos_y;
{
unsigned width, height; /* Bitmap dimensions */
int x_hot, y_hot;
Pixmap bitmap; /* The bitmap as a pixmap */
XImage * ximage; /* The bitmap as an image */
int x, y;
int status;
Dimension new_width, new_height;
status = XReadBitmapFile(XtDisplay(room->canvas), XtWindow(room->canvas),
filename, &width, &height, &bitmap,
&x_hot, &y_hot);
switch (status)
{
case BitmapOpenFailed:
fprintf(stderr,
"room__load_bitmap: Could not open bitmap file %s\n",filename);
return;
break;
case BitmapFileInvalid:
fprintf(stderr,
"room__load_bitmap: Bitmap file %s does not contain valid data\n",
filename);
return;
break;
case BitmapNoMemory:
fprintf(stderr,
"room__load_bitmap: Not enough memory to open bitmap file %s\n",
filename);
return;
break;
}
/* Get a local copy of the pixmap so we can query the pixel values */
ximage = XGetImage(XtDisplay(room->canvas),
bitmap,0,0,width,height,AllPlanes,XYPixmap);
if (ximage == NULL)
{
fprintf(stderr,"room__load_bitmap: Problem getting bitmap image\n");
return;
}
new_width = MAX( intf2int(room->right_wall),
pos_x + width * item__get_width() * SF+10);
new_height = MAX( intf2int(room->floor),
pos_y + height * item__get_height() * SF+10);
if (new_width != intf2int(room->right_wall) ||
new_height != intf2int(room->floor))
{
room->resized = False;
/* Set the window width and height so the balls fit on it */
XtVaSetValues(room->canvas,
XtNwidth, new_width,
XtNheight, new_height,
NULL);
wait_for_resize( room);
}
/* Create a ball for each pixel set in the bitmap */
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
{
if (XGetPixel(ximage, x, y))
items__new_item(room->items,
(int)(x * item__get_width() * SF + pos_x),
(int)(y * item__get_height() * SF + pos_y),
/*x_vel*/0, /*y_vel*/0);
}
}
}
/* Set the room's width and wait for the window to resize before returning */
void room__set_width(room, width)
room_type room;
int width;
{
Dimension d_width = width;
if (intf2int(room->right_wall) == width)
return;
room->resized = False;
XtVaSetValues(room->canvas,
XtNwidth, d_width,
NULL);
/* room->right_wall = int2intf(width); Set in window resize callback */
wait_for_resize( room);
}
/* Set the height of the room, resize the actual room and wait for it
to resize before returning. */
void room__set_height(room, height)
room_type room;
Dimension height;
{
Dimension d_height = height;
if (intf2int(room->floor) == height)
return;
room->resized = False;
XtVaSetValues(room->canvas,
XtNheight, d_height,
NULL);
/* room->floor = int2intf(height); set in the window resize callback */
wait_for_resize( room);
}
/* Private object methods */
/**********************************************************************
* redisplay - Called when DrawingArea canvas gets an expose event
* Redraws all balls in room
**********************************************************************/
static void redisplay(room, event)
room_type room;
XEvent * event;
{
Display * display = XtDisplay( room->canvas);
Window window = XtWindow ( room->canvas);
if (!item__initted())
{
Cursor cursor = XCreateFontCursor( display, XC_watch );
XDefineCursor( display, window, cursor);
item__init( get_toplevel( room->canvas), display, window,
room->background);
XFreeCursor( display, cursor);
XUndefineCursor( display, window);
}
if (event->xexpose.count == 0) /* Wait until last expose event*/
{
item_type item;
XClearWindow( display, window);
for (item = items__get_first( room->items);
item != (item_type)0;
item = items__get_next( room->items))
{
item__redraw( item);
}
}
}
/**********************************************************************
* resize_cb - Called when DrawingArea canvas is resized
* Adjusts the right wall and floor
**********************************************************************/
static void resize_cb(w, room, call_data)
Widget w;
room_type room;
caddr_t call_data;
{
Dimension width,height;
XtVaGetValues(w,
XtNwidth, &width,
XtNheight, &height,
NULL);
room->right_wall = int2intf(width);
room->floor = int2intf(height);
/* Set flag in case someone is waiting for the resize */
room->resized = True;
}
/* Waits for the window to actually resize. This depends on the
resize callback code setting the room->resized flag to True. Callers
should set the room->resized flag to False before calling. */
static void wait_for_resize( room)
room_type room;
{
XEvent event;
XtAppContext app_context;
app_context = XtWidgetToApplicationContext( room->canvas);
/* Wait for the window resize to occur */
while (!room->resized)
{
XtAppNextEvent(app_context, &event);
XtDispatchEvent(&event);
}
}
/**********************************************************************
* process_input - Called when DrawingArea canvas has input
* Handles mouse button events
**********************************************************************/
static void process_input( room, event)
room_type room;
XEvent * event;
{
switch (event->type)
{
/* Process mouse button events */
case ButtonPress:
switch (event->xbutton.button)
{
case 1:
/* Button 1 - Create 1 item under the pointer */
room->oldx = room->newx = event->xbutton.x;
room->oldy = room->newy = event->xbutton.y;
/* Create an item under the pointer */
room->item = item__create(event->xbutton.x,
event->xbutton.y,
/*x,y velocity*/ 0, 0);
room->button1 = True;
break;
/* Remember the button we have pressed (used in mouse motion) */
case 2: room->button2 = True; break;
case 3: room->button3 = True; break;
}
break;
case ButtonRelease:
switch (event->xbutton.button)
{
/* button 1 release - move item to items list */
case 1:
item__set_x_vel( room->item, room->newx - room->oldx);
item__set_y_vel( room->item, room->newy - room->oldy);
items__add( room->items, room->item);
room->item = (item_type)0;
room->button1 = False;
break;
/* Remember the button we have unpressed (used in mouse motion) */
case 2: room->button2 = False; break;
case 3: room->button3 = False; break;
}
}
}
/**********************************************************************
* process_motion - Called when DrawingArea canvas have pointer motion events
**********************************************************************/
static void process_motion( room, event)
room_type room;
XEvent * event;
{
if (room->button1)
{
/* When button 1 pressed during movement */
/* Save 'speed' of pointer for when the button is released */
room->oldx = room->newx;
room->oldy = room->newy;
room->newx = event->xmotion.x;
room->newy = event->xmotion.y;
item__set_x_vel( room->item, (room->newx - room->oldx));
item__set_y_vel( room->item, (room->newy - room->oldy));
item__move_pos( room->item,
event->xmotion.x, event->xmotion.y);
}
else
if (room->button2)
{
/* Create a random velocity ball */
items__new_item(room->items, event->xmotion.x, event->xmotion.y,
rand_range( -8, 8),
rand_range( -8, 0));
}
else
if (room->button3)
{
items__new_item(room->items, event->xmotion.x, event->xmotion.y,
0, 0);
}
}
#ifdef MOTIF
static void process_motion_act(/*w, datap_ptr, event*/);
/* Create motif canvas as a DrawingArea Widget */
static Widget create_canvas( room, parent, name)
room_type room;
Widget parent;
char * name;
{
Widget canvas =
XtCreateManagedWidget("canvas", xmDrawingAreaWidgetClass, parent,
NULL, 0);
XtAddCallback(canvas, XmNexposeCallback, redisplay_cb, (XtPointer)room);
XtAddCallback(canvas, XmNinputCallback, process_input_cb,(XtPointer)room);
XtAddCallback(canvas, XmNresizeCallback, resize_cb, (XtPointer)room);
XtAddEventHandler(canvas, ButtonMotionMask, FALSE,
process_motion_act, (XtPointer)room);
return canvas;
}
static void redisplay_cb(w, room, call_data)
Widget w;
room_type room;
XmDrawingAreaCallbackStruct * call_data;
{
redisplay( room, call_data->event);
}
static void process_input_cb(w, room, call_data)
Widget w;
room_type room;
XmDrawingAreaCallbackStruct * call_data;
{
process_input( room, call_data->event);
}
static void process_motion_act(w, room, event)
Widget w;
room_type room;
XEvent * event;
{
process_motion( room, event);
}
#endif
#ifdef ATHENA
static void process_motion_cb(/*w, room, call_data*/);
/* Create canvas as a DrawingArea widget */
static Widget create_canvas( room, parent, name)
room_type room;
Widget parent;
char * name;
{
Widget canvas =
XtCreateManagedWidget("canvas", drawingAreaWidgetClass, parent,
NULL, 0);
XtAddCallback(canvas,XtNexposeCallback,redisplay_cb, (XtPointer)room);
XtAddCallback(canvas,XtNinputCallback, process_input_cb, (XtPointer)room);
XtAddCallback(canvas,XtNresizeCallback,resize_cb, (XtPointer)room);
XtAddCallback(canvas,XtNmotionCallback,process_motion_cb, (XtPointer)room);
return canvas;
}
static void redisplay_cb(w, room, call_data)
Widget w;
room_type room;
XawDrawingAreaCallbackStruct * call_data;
{
redisplay( room, call_data->event);
}
static void process_input_cb(w, room, call_data)
Widget w;
room_type room;
XawDrawingAreaCallbackStruct * call_data;
{
process_input( room, call_data->event);
}
static void process_motion_cb(w, room, call_data)
Widget w;
room_type room;
XawDrawingAreaCallbackStruct * call_data;
{
process_motion( room, call_data->event);
}
#endif