Debian Codegraph @

docs/toolkit.html

raw symbols SCIP
1 <title>The Ace of Penguins Toolkit</title>
2 <body bgcolor="#e0e0e0" background="penguin.gif" text="#000000">
3
4 <h1 align=center>
5 <img src="title.gif" width=416 height=55 alt="The Ace of Penguins"><br>
6 Toolkit</h1>
7
8 <img src="as.gif" width=93 height=117 alt="" align=right>
9
10 <p>The Ace of Penguins Toolkit allows you to create your own solitaire
11 games pretty easily. For example, I wrote the first version of
12 <tt>golf</tt> in about ten minutes (the toolkit was already done at
13 that point). My wife watched, and played it when I was done. I later
14 added more bells and whistles, but the basic game only took ten
15 minutes. </p>
16
17 <p>There are some utilities that come with the toolkit that you'll
18 need to be familiar with in order to build your game. These tools
19 allow you to integrate pictures and online help into your program,
20 much like the Windows(tm) resource compiler. Graphics are created in
21 the standard GIF format and integrated into your program with the
22 <tt>gif2pack</tt> program. Help is written as a limited HTML file and
23 integrated with <tt>text2c</tt>. The toolkit has functions that
24 access these resources when the program needs them.</p>
25
26 <p>All Ace programs use <a href="http://www.libpng.org/pub/png/">the
27 PNG library</a> and <a
28 href="http://www.info-zip.org/pub/infozip/zlib/">ZLib</a>. Your
29 system may have them installed, otherwise you'll need to download and
30 install them.</p>
31
32 <p>The library consists of three main parts: A collection of standard
33 images (including, of course, the deck of cards), functionality to
34 maintain and manipulate the "table" and interface with X, and a set of
35 functions to create and manipulate "stacks" of cards.</p>
36
37 <h2>Getting Started</h2>
38
39 <p>Your source file must inlude cards.h from wherever you installed
40 it. In my case, I use <tt>#include "cards.h"</tt> and let the
41 <tt>Makefile</tt> worry about how to tell <tt>gcc</tt> where to
42 include it from.
43
44 <p>Your program should, in <tt>main()</tt>, call three functions:
45
46 <pre>
47 init_ace(argc, argv);
48 init_table(width, height);
49 table_loop();
50 </pre>
51
52 Here, <tt>argc</tt> and <tt>argv</tt> are the parameters passed to
53 <tt>main()</tt> and <tt>width</tt> and <tt>height</tt> are whatever
54 width and height your window ("table") should be. You'll probably
55 want to calculate the initial size of the table based on the sizes of
56 the cards and margins (they're defined in <tt>cards.h</tt>). The
57 <tt>init_*()</tt> calls do everything needed to get the X interface
58 running. The <tt>table_loop()</tt> call passes control over to the
59 Ace library, and does not return. Instead, the Ace library manages
60 the X interface, and calls functions you define when user interaction
61 occurs (see the events section below).
62
63 <h2>Makefiles and Linking</h2>
64
65 You'll need to add<tt> -lpng -lz -lX11 </tt>to the end of your link line
66 in your <tt>Makefile</tt>. You'll also need to link the Ace library,
67 which for me means adding <tt>../lib/cardlib.a</tt> but you'll need to
68 do whatever is appropriate for where you installed the library. For
69 example, if you copied <tt>cardlib.a</tt> to
70 <tt>/usr/lib/libcards.a</tt> then you'd add <tt>-lcards</tt> instead.
71 Note that the card library gets linked before the X libraries.
72 Example:
73
74 <pre>
75 sol : sol.o sol-help.o sol-img.o
76 $(CC) $(CFLAGS) -o $@ $^ -lcards -lpng -lz -lX11
77 </pre>
78
79 Note in the example above that your game will probably consist of at
80 least three files. First is the game itself (<tt>sol.o</tt> in this
81 example). Next is the help file (<tt>sol-help.o</tt>), which started
82 as HTML and was converted to C by <tt>text2c</tt>. Last is the image
83 library (<tt>sol-img.o</tt>) which is generated from your pngs by
84 <tt>make-imglib</tt>. To build these, you'll need rules like this:
85
86 <pre>
87 %-help.c : %.html ../lib/text2c
88 ../lib/text2c $*_help < $< > $@
89
90 %-img.c : %.png
91 ../lib/make-imglib -i $(srcdir) -n appimglib - $@ $<
92 </pre>
93
94 Of course, you'll need to adjust these for your situation.
95
96
97 <h2>Events</h2>
98
99 There are a number of functions that you may define in your program to
100 add functionality. Each of these is called when some event happens,
101 such as a mouse click or exposure. Here are the events you may define,
102 with their prototypes:
103
104 <dl>
105
106 <dt> <tt>void <b>init</b>();</tt>
107
108 <dd> This function is called once just after your window appears on
109 the screen. You should use this function to set up the pitures,
110 stacks, and other data you need to initialize once per program run.
111 What I do is define a second function called start_over() that resets
112 the game itself, and have init call start_over as the last thing it
113 does. That way, I can call start_over from other places also.</p>
114
115 <dt> <tt>void <b>redraw</b>()</tt>
116
117 <dd> This function is called whenever part of the X window is
118 "exposed" and needs to be redrawn. The computer doesn't remember what
119 was there, it needs to draw it from scratch. Note that the
120 put_picture functions are designed to work with this event, so that
121 they will automatically optimize themselves. An X clipping region is
122 also set up for you. The best way to deal with redraw events is to
123 redraw the entire window and let the Ace and X libraries worry about
124 limiting you to the parts that were actually exposed.</p>
125
126 <dt> <tt>void <b>resize</b>(int width, int height)</tt>
127
128 <dd> If you provide this function, your game becomes resizable, and
129 you will be responsible for repositioning all the images and stacks
130 according to the new size. You will probably need to call
131 <tt>stack_set_card_size</tt> (and read back the actual sizes with
132 <tt>stack_get_card_size</tt> and <tt>stack_get_fans</tt>).</p>
133
134 <dt> <tt>void <b>key</b>(int k, int x, int y)</tt>
135
136 <dd> This function is called each time the user presses a key. If the
137 key they pressed is a regular key with an ASCII equivalent (like `h'),
138 the ASCII code is passed as <tt>k</tt>, otherwise the keyc ode (like
139 KEY_F(1) or KEY_UP) is passed. The location of the pointer is passed
140 as x and y. If you do not define this function, a default is provided
141 which exits when the user presses ESC or Ctrl-C. Your routine should
142 do a similar check to remain consistent with other Ace programs.</p>
143
144 <dt> <tt>void <b>click</b>(int x, int y, int b)</tt>
145
146 <dd> This function is called whenever the user presses a mouse button.
147 the primary button is number 1. For maximum compatibility, you should
148 only check to see if the button is 1 or greater than 1, since the
149 numbering of buttons isn't guaranteed to be standard.</p>
150
151 <dt> <tt>void <b>double_click</b>(int x, int y, int b)</tt>
152
153 <dd> This function is called when the user double-clicks the mouse.
154 Note that there will be a click() call for the first click but not for
155 the second. If you do not define this function, click() is called for
156 every click.</p>
157
158 <dt> <tt>void <b>drag</b>(int x, int y, int b)</tt>
159
160 <dd> This function is called when the user moves the pointer while
161 holding the button down.</p>
162
163 <dt> <tt>void <b>drop</b>(int x, int y, int b)</tt>
164
165 <dd> This function is called when the user releases the button.</p>
166
167 </dl>
168
169
170
171 <h2>Table Functions</h2>
172
173 <dl>
174
175 <dt> <tt><b>CARD_WIDTH</b>, <b>CARD_HEIGHT</b>, <b>CARD_MARGIN</b></tt>
176
177 <dd> These macros define the default size of the cards, and the amount
178 of space you should allow for between cards and around the edge of the
179 window. By using these macros, your program will have the same style
180 as other programs written with this library.</p>
181
182 <dt> <tt><b>CARD_FAN_DOWN</b>, <b>CARD_FAN_RIGHT</b>,
183 <b>CARD_FAN_TBDOWN</b>, <b>CARD_FAN_TBRIGHT</b></tt>
184
185 <dd> These macros define the default offsets to be used when a stack
186 of cards is "fanned" in a given direction (only down and right are
187 supported). The <tt>TB</tt> versions are for a "tiny bit" of fanning;
188 just enough to hint at the value of the card, whereas the others
189 offset them enough to clearly see the value of the card.</p>
190
191 <dt> <tt>void <b>init_ace</b>(int argc, char **argv)</tt>
192
193 <dd> This function initlizes the Ace library. It processes all
194 command line options and connects to the display. After calling this,
195 the global variables <tt>display_width</tt> and
196 <tt>display_height</tt> are valid, and <tt>table_width</tt> and
197 <tt>table_height</tt> are non-zero if the user requested a specific
198 size on the command line. If you need to see a command line option,
199 create an OptionDesc list for your game (see cards.h for details).</p>
200
201 <dt> <tt>void <b>init_table</b>(int argc, char **argv, int
202 table_width, int table_height)</tt>
203
204 <dd> This function sets up the interface to X and creates the initial
205 window. This function must be called exactly once from
206 <tt>main()</tt>.</p>
207
208 <dt> void <b>table_loop</b>()
209
210 <dd> <tt>main()</tt> must call this function as the last thing it
211 does. This function will never return. Once called, the library
212 starts managing the interaction between the user (via X) and the
213 program (via events).</p>
214
215 <dt> <tt>Picture *<b>get_picture</b>(char *name)</tt>
216
217 <dd> This function loads one of the images that you added to your
218 program with the <tt>make-imglib</tt> program (either the
219 application-specific ones, or the standard ones). This function
220 searches one table prefixed by <tt>imglib</tt> (the standard set) and
221 one prefixed by <tt>appimglib</tt> (the app-specific set). Note that
222 you may call this function at any time, even before
223 <tt>init_table()</tt>, since the actual conversion of the image to
224 native X format is deferred until it is needed. The format of the
225 <tt>Picture</tt> type is as follows:
226
227 <ul>
228 <li><tt>int w, h;</tt> - the size of the image
229 </ul>
230
231 Note that images may have a non-rectangular shape. This is
232 automatically handled by the library.</p>
233
234 <dt><tt>void <b>put_picture</b>(Picture *picture, int dx, int dy,
235 int x, int y, int w, int h)</tt>
236
237 <dd> This puts a picture onto the screen. This should only be called
238 by an event function, since it (like most of these types of functions)
239 call X routines. The <tt>picture</tt> is one as returned by
240 <tt>get_picture</tt>. The <tt>dx,dy</tt> tells where the upper left
241 pixel of the picture should be placed on the window (regardless of
242 what sub-part of the picture you are putting). The <tt>x,y</tt> and
243 <tt>w,h</tt> indicate a sub-part of the picture to be shown. In most
244 cases, <tt>x,y</tt> will be zero and <tt>w,h</tt> will be the actual
245 size of the picture. Note that, during a <tt>redraw</tt> event, the
246 library knows what part of the window actually needs to be redrawn,
247 and may not draw your picture (or only draw part of it) if it's not
248 needed to redraw that part of the picture.</p>
249
250 <dt><tt>void <b>put_picture_inverted</b>(Picture *picture, int dx, int dy,
251 int x, int y, int w, int h)</tt>
252
253 <dd> Same as <tt>put_picture</tt>, except that the picture is inverted
254 before putting it. Some things to note: On 24-bit displays, each RGB
255 component is separately inverted, but on other displays "inverting"
256 means only swapping black and white pixels. Plan your pictures
257 accordingly. Also, there is only one inversion cache, so if you have
258 multiple inverted images, performance will be degraded.</p>
259
260 <dt><tt>void <b>set_centered_pic</b>(Picture *picture)</tt>
261
262 <dd> This allows the program to tell the library to maintain a single
263 picture that is displayed centered on the window. This is used by the
264 splash, win, and lose pictures. Pass a <tt>NULL</tt> pointer to
265 remove the picture.</p>
266
267 <dt><tt>Picture *<b>get_centered_pic</b>()</tt>
268
269 <dd> This returns the current centered picture, or <tt>NULL</tt> if
270 none. My programs use this in the <tt>click</tt> and <tt>key</tt>
271 events to automatically remove the splash picture on the first event,
272 and to detect when the user clicks on a win or lose picture.</p>
273
274 <dt><tt>void <b>clip</b>(int x, int y, int w, int h)</tt>
275
276 <dd> This changes the clipping rectangle. Most programs won't need
277 this, since it's set appropriately by the library during exposures.</p>
278
279 <dt><tt>void <b>clear</b>(int x, int y, int w, int h)</tt>
280
281 <dd> This clears (fills with the table color) a given section of the
282 window.</p>
283
284 <dt><tt>void <b>invalidate</b>(int x, int y, int w, int h)</tt>
285
286 <dd>Forces the library to redraw part of the table. This is different
287 than just calling <tt>redraw()</tt> because it sets the clipping info,
288 clears the area, calls redraw, and handles the centered picture. This
289 is the preferred way of causing part of the screen to be redrawn.
290 Your program should alter the information about the layout, then
291 invalidate the area and let <tt>redraw()</tt> update the screen.</p>
292
293 <dt><tt>void <b>invalidate_nc</b>(int x, int y, int w, int h)</tt>
294
295 <dd> Same, except that the area is not cleared first. This is an
296 optimization for the case where you're adding an image, and you don't
297 want the screen to flash the background color.</p>
298
299 <dt><tt>void <b>invalidate_exposure</b>(int ox, int oy, int ow, int oh,
300 int nx, int ny, int nw, int nh)</tt>
301
302 <dd>This is a specialty function for the case where you're moving
303 something from one point to another. Pass the old and new regions,
304 and it will invalidate the portion of the old region that isn't
305 covered by the new region. You'd then use <tt>invalidate</tt> to
306 update the new region (or just <tt>put_picture</tt>). By doing it
307 this way, you can avoid flickering.</p>
308
309 <dt><tt>void <b>flush</b>()</tt>
310
311 <dd> This forces all drawing operations to be pushed to the server
312 immediately, rather than cached. You should use this between
313 iterations of an animation to ensure that the server is updating the
314 screen at a steady pace.</p>
315
316 <dt><tt>void <b>beep</b>()</tt>
317
318 <dd>It makes a beep. Go figure.</p>
319
320 <dt><tt>void <b>text</b>(char *s, int x, int y)</tt>
321
322 <dd> This draws the given string at the given coordinates on the
323 window. Note that the coordinates are the lower left corner of the
324 region the text occupies, not the starting baseline point. The font
325 can be assumed to be a monospaced font, with each character occupying
326 a rectangle of width <tt>font_width</tt> and height
327 <tt>font_height</tt> (defined in <tt>cards.h</tt>). This avoids the
328 messy calculations involving descenders and bearings.</p>
329
330 <dt><tt>void <b>help</b>(char *filename, char *text)</tt>
331
332 <dd> This invokes the help system. Pass a file name and a pointer to
333 the file's data as prepared by <tt>text2c</tt>. If the file exists,
334 it is used, else the data is used. All the events are redirected to
335 the help system until it decides to give control back.</p>
336
337 <dt><tt>void <b>snap_to_grid</b>(int *x, int *y,
338 int step_x, int step_y,
339 int origin_x, int origin_y,
340 int max_distance)</tt>
341
342 <dd>This function is a general-purpose grid alignment function. Pass
343 the coordinates you want to align as <tt>x</tt> and <tt>y</tt>. The
344 grid is determined by the <tt>step</tt> and <tt>origin</tt>
345 parameters, with the <tt>origin</tt> indicating one of the points to
346 snap to. If the given point is more than <tt>max_distance</tt> from
347 one of the grid points, <tt>x</tt> and <tt>y</tt> are unchanged, else
348 <tt>x</tt> and <tt>y</tt> are replaced with the closest grid point.
349 Note that <tt>max_distance</tt> is not a pythagorian distance, it's an
350 absolute value of the distance along each axis independently (i.e. it
351 defines a square of side 2*<tt>max_distance</tt> centered on the grid
352 point, not a circle of radius <tt>max_distance</tt>).</p>
353
354 </dl>
355
356 <h2>Stack Functions</h2>
357
358 <p>The stack functions all deal with stacks of cards. Each card is
359 described by a single number that encodes its value, suit, and whether
360 the card is face up or face down. <tt>cards.h</tt> provides a number
361 of macros for dealing with these numbers. Also, the standard ace
362 library provides all the images matched up with these numbers, so you
363 do not need to worry about them. Each stack is positioned on the
364 table, and may be neatly piled up or fanned to the right or down.</p>
365
366 <p>Note that the type <tt>Stack</tt> is an opaque pointer. All stacks
367 are of type <tt>Stack *</tt> but there are no fields you may access
368 directly.</p>
369
370 <dl>
371
372 <dt><tt><b>MAKE_CARD</b>(s, v, f)</tt>
373
374 <dd> This macro combines the suit (<tt>s</tt>), value (<tt>v</tt>),
375 and face-upness (<tt>f</tt>) into a single card number. The suit is
376 one of <tt>SUIT_HEARTS</tt>, <tt>SUIT_DIAMONDS</tt>,
377 <tt>SUIT_CLUBS</tt>, or <tt>SUIT_SPADES</tt>. Note that these are
378 <tt>0..3</tt>, but don't rely on that. The value is <tt>1..13</tt>,
379 but you may use <tt>ACE</tt>, <tt>2..10</tt>, <tt>JACK</tt>,
380 <tt>QUEEN</tt>, and <tt>KING</tt> as well. Zero is reserved for "no
381 card", so a zero value is not used to refer to a card. The
382 face-upness is either <tt>FACEUP</tt> or <tt>FACEDOWN</tt>. The macro
383 returns the card number.</p>
384
385 <dt><tt><b>SUIT</b>(c)</tt>
386
387 <dd> Takes a card number and returns its suit (the <tt>SUIT_</tt>
388 macros above).</p>
389
390 <dt><tt><b>COLOR</b>(c)</tt>
391
392 <dd>Returns the "color" of a card (red or black). Note that no
393 specific color values are defined; the only thing you can do with the
394 returned value of this macro is compare it to other values returned by
395 this macro. In other words, you can tell if two cards are the same or
396 different colors, but nothing else.</p>
397
398 <dt><tt><b>VALUE</b>(c)</tt>
399
400 <dd>Returns the value of a card (<tt>ACE</tt>, <tt>2..10</tt>,
401 <tt>JACK</tt>, <tt>QUEEN</tt>, <tt>KING</tt>).</p>
402
403 <dt><tt><b>FACEDOWNP</b>(c)</tt>
404
405 <dd> The face-down predicate returns the face-downness of a card. It
406 returns either <tt>FACEUP</tt> or <tt>FACEDOWN</tt>.</p>
407
408 <dt><tt>Stack * <b>stack_create</b>(int x, int y)</tt>
409
410 <dd> This function creates a stack and positions it on the table. The
411 stack is initially empty and is not fanned in any direction (i.e. the
412 cards are neatly piled upon each other).</p>
413
414 <dt><tt>void <b>stack_destroy</b>(Stack *s)</tt>
415
416 <dd>This function destroys a stack and frees up the resources it was
417 using. Most solitaire-type games won't ever destroy stacks, they'll
418 just reshuffle the cards among them.</p>
419
420 <dt><tt>void <b>stack_set_pictures</b>(Picture **fronts, Picture *back)</tt>
421
422 <dd>This function allows the application to specify the pictures to be
423 used to display the cards. The <tt>fronts</tt> array is indexed by
424 the card number (i.e. <tt>MAKE_CARD</tt> with FACEUP); you should
425 allow for 64 slots in your array. The <tt>back</tt> picture is for
426 all face-down cards. All pictures should be <tt>CARD_WIDTH</tt> by
427 <tt>CARD_HEIGHT</tt> pixels.</p>
428
429 <dt><tt>void <b>stack_load_standard_deck</b>()</tt>
430
431 <dd> This function loads all the standard card pictures from the
432 built-in library. Most card games should call this once in
433 <tt>init()</tt>.</p>
434
435 <dt><tt>void <b>stack_move</b>(Stack *s, int x, int y)</tt>
436
437 <dd> This function moves a stack to a new location on the screen.
438 While not generally useful to common solitaire games, it is used
439 internally to drag and drop cards between stacks.</p>
440
441 <dt><tt>void <b>stack_set_offset</b>(Stack *s, int which_offset)</tt>
442
443 <dd>This function changes the offset for each card in a stack. By
444 offsetting cards, the player can view parts of obscured cards, thus
445 being able to see the value of the card. Valid values for
446 <tt>which_offset</tt> are <tt>STACK_OFFSET_NONE</tt>,
447 <tt>STACK_OFFSET_RIGHT</tt>, <tt>STACK_OFFSET_DOWN</tt>,
448 <tt>STACK_OFFSET_TBRIGHT</tt>, and <tt>STACK_OFFSET_TBDOWN</tt>.
449 The <tt>TB</tt> versions are for a "tiny bit" of offset.</p>
450
451 <dt><tt>void <b>stack_set_card_size</b>(int width, int height)</tt>
452
453 <dd> Request that the card size be changed, such as after a resize.
454 Note that the values you give are requests - the library may need to
455 use a different size. Use <tt>stack_get_card_size</tt> to find out
456 what was really used.</p>
457
458 <dt><tt>void <b>stack_get_card_size</b>(int *width, int *height)</tt>
459
460 <dd> Find out how big the current cards are.</p>
461
462 <dt><tt>void <b>stack_get_fans</b>(int *down, int *right, int *tbdown,
463 int *tbright)</tt>
464
465 <dd> Get the offsets for fan_down, etc.</p>
466
467 <dt><tt>void <b>stack_set_empty_picture</b>(Stack *s, Picture *p)</tt>
468
469 <dd> This lets you specify a picture that is drawn when the stack is
470 empty. For example, some games want it to look like you're stacking
471 the cards on top of something. This is where you specify the
472 something. There is a standard picture called <tt>empty.gif</tt> that
473 simulates a small platform for the cards.</p>
474
475 <dt><tt>void <b>stack_redraw</b>()</tt>
476
477 <dd> This function should be called from your <tt>redraw()</tt>
478 function (the default <tt>redraw</tt> does this for you if you don't
479 specify one yourself). This causes all stacks to redraw their
480 cars.</p>
481
482 <dt><tt>void <b>stack_peek_card</b>(Stack *s, int n, int show)</tt>
483
484 <dd>This function lets the player "peek" at an otherwise obscured
485 card. What it does is force that card to be drawn on top of all the
486 other cards in the stack, regardless of where it is in the stack.
487 This is used to support the right-mouse button in solitaire, which
488 lets you see the suit of obscured cards. Note that this function will
489 not show you the face of a face-down card. Note that when <tt>n</tt>
490 (the index of the card in the stack) is zero, it means the bottom-most
491 card on the deck (the one closest to the table, not the one closest to
492 the bottom of your monitor). The top-most card (the one the player
493 sees the most of) has the highest number, one less than
494 <tt>stack_count_cards()</tt> returns. Pass a non-zero value for
495 <tt>show</tt> to show the card, or pass zero to put the card back and
496 show the stack normally. </p>
497
498 <dt><tt>int <b>stack_count_cards</b>(Stack *s)</tt>
499
500 <dd> Returns the number of cards in the stack. Zero means empty.</p>
501
502 <dt><tt>int <b>stack_get_card</b>(Stack *s, int n)</tt>
503
504 <dd>Returns the number of the given card on the stack (suitable for
505 the <tt>SUIT</tt>, <tt>COLOR</tt>, and <tt>VALUE</tt> macros, etc).</p>
506
507 <dt><tt>void <b>stack_add_card</b>(Stack *s, int c)</tt>
508
509 <dd>Adds one card to the top of the given stack.</p>
510
511 <dt><tt>int <b>stack_take_card</b>(Stack *s)</tt>
512
513 <dd>Removes one card from the top of the stack and returns it.</p>
514
515 <dt><tt>void <b>stack_change_card</b>(Stack *s, int n, int c)</tt>
516
517 <dd>Changes the given card to the given number. Note that whan
518 <tt>n</tt> is zero, that indicates the bottom-most card. This
519 function is most often used to change a card from <tt>FACEDOWN</tt> to
520 <tt>FACEUP</tt>. </p>
521
522 <dt><tt>void <b>stack_shuffle</b>(Stack *s)</tt>
523
524 <dd> This function randomizes the sequence of the cards stored in the
525 stack. To simulate a deck of cards, you'd create one stack and fill
526 it with all 52 combinations (or more, depending on the game) and
527 shuffle it. Then, you'd use one of the other functions (like
528 <tt>stack_flip_card</tt>) to move cards from this "deck" to the other
529 stacks. To restart the game, use <tt>stack_flip_stack</tt> to move
530 them all back to the deck, shuffle again, and start over.</p>
531
532 <dt><tt>int <b>stack_find</b>(int x, int y, Stack **stack_ret, int *n_ret)</tt>
533
534 <dd>This function is used by the <tt>click</tt> type events to
535 identify which card the player selected. You pass it the <tt>x,y</tt>
536 of the event, and a pointer to a stack pointer and integer. If the
537 player selected a card, the <tt>stack_ret</tt> and <tt>n_ret</tt>
538 variables are set to the stack and index into the stack they selected
539 (when <tt>n_ret</tt> is zero, they selected the bottom-most card) and
540 a non-zero value is returned. If the player did not select a card,
541 <tt>stack_ret</tt> and <tt>n_ret</tt> are unchanged and a zero value
542 is returned.</p>
543
544 <dt><tt>int <b>stack_drag_find</b>(int x, int y, Stack **stack_ret)</tt>
545
546 <dd>When dragging a stack, this function tells you which stack you
547 have dragged it over. Similar to <tt>stack_find</tt>. Note that the
548 library remembers what you're dragging from <tt>stack_begin_drag</tt>
549 and will do the Right Thing.</p>
550
551 <dt><tt>int <b>stack_card_posn</b>(Stack *s, int n, int *x, int *y)</tt>
552
553 <dd>Given a card in a stack (when <tt>n</tt> is zero, it refers to the
554 bottom-most card), find its position on the table. This takes into
555 account the offset the stack might have. The size of the card is
556 <tt>CARD_WIDTH</tt> by <tt>CARD_HEIGHT</tt>. If the card specified
557 exists, <tt>x</tt> and <tt>y</tt> are set to the coordinates of the
558 upper left pixel and a non-zero value is returned, else zero is
559 returned and <tt>x</tt> and <tt>y</tt> are unchanged.</p>
560
561 <dt><tt>void <b>stack_move_cards</b>(Stack *src, Stack *dest, int num, int flag)</tt>
562
563 <dd>Move <tt>num</tt> cards from the top of stack <tt>src</tt> to the top of
564 stack <tt>dest</tt>. If <tt>flag</tt> is nonzero then <tt>stack_undo</tt>
565 considers this an automatic move.</p>
566
567 <dt><tt>void <b>stack_move_card</b>(Stack *src, Stack *dest, int flag)</tt>
568
569 <dd>Like <tt>stack_move_cards</tt> but moves just one card.</p>
570
571 <dt><tt>void <b>stack_flip_cards</b>(Stack *src, Stack *dest, int num, int flag)</tt>
572
573 <dd>Take <tt>num</tt> cards from <tt>src</tt>, flip them over, and put
574 them on top of <tt>dest</tt>. If <tt>flag</tt> is nonzero then
575 <tt>stack_undo</tt> considers this an automatic move.</p>
576
577 <dt><tt>void <b>stack_flip_card</b>(Stack *src, Stack *dest, int flag)</tt>
578
579 <dd>Like <tt>stack_flip_cards</tt> but flips just one card.</p>
580
581 <dt><tt>void <b>stack_flip_stack</b>(Stack *src, Stack *dest, int flag)</tt>
582
583 <dd>Like <tt>stack_flip_cards</tt> but flips the entire stack.</p>
584
585 <dt><tt>void <b>stack_flip_move_stack</b>(Stack *src, Stack *dest, int flag)</tt>
586
587 <dd>Combination of <tt>stack_flip_cards</tt> and <tt>stack_move_cards</tt>;
588 assuming that the cards at the top of <tt>src</tt> are face up and the rest are
589 face down, first flip those cards that are face up, then move the rest.</p>
590
591 <dt><tt>void <b>stack_animate</b>(Stack *src, Stack *dest, int flag)</tt>
592
593 <dd>Moves one card from <tt>src</tt> to <tt>dest</tt>, but does it in
594 such a way that the player can <em>see</em> it moving from one to the
595 other. If <tt>flag</tt> is nonzero then <tt>stack_undo</tt> considers
596 this an automatic move. You should use this for auto-play so that the
597 player can see what's happening.</p>
598
599 <dt><tt>void <b>stack_undo</b>()</tt>
600
601 <dd>All operations on stacks are recorded. Calling this function
602 reverses those operations. You may continue calling this function,
603 and it will continue reversing the operations, until no more exist.
604 Calling it when no further undo is available causes no actions.</p>
605
606 <dt><tt>void <b>stack_undo_reset</b>()</tt>
607
608 <dd>This function erases all undo information. You should call this
609 function after dealing the cards each time you start (or restart) a
610 game, so that the player cannot undo the deal itself.</p>
611
612 <dt><tt>void <b>stack_begin_drag</b>(Stack *s, int n, int x, int y)</tt>
613
614 <dd> This function is one of three that coordinate drag-and-drop
615 between stacks. With this function, you identify what you are picking
616 up - the stack, the index of the card within the stack (when
617 <tt>n</tt> is zero, the whole stack is picked up), and the coordinates
618 of the pointer at the time you picked it up. If you call this
619 function while a drag is in progress, the previous drag is
620 cancelled.</p>
621
622 <dt><tt>void <b>stack_continue_drag</b>(int n, int x, int y)</tt>
623
624 <dd>Called in your <tt>drag()</tt> function, this tells the library to
625 move the cards you've picked up to a new location to follow the
626 pointer. The library maintains a constant offset relative to the
627 pointer to give it a "natural" feel (the card don't jump into your
628 hand, for example). You also must pass the index of the card that is
629 allowed to be dropped at that time (use <tt>stack_drag_find</tt>). As
630 you pass different values of <tt>n</tt> to this function, the window
631 will change to reflect carrying the right number of cards. If you
632 pass <tt>n</tt> greater than the largest card index (see
633 <tt>stack_count_cards</tt>), a special graphic that indicates "drop
634 not allowed here" is used.</p>
635
636 <dt><tt>void <b>stack_drop</b>(Stack *onto, int n, int flag)</tt>
637
638 <dd>This function ends a drag-and-drop operation by specifying which
639 cards from the original stack are to be dropped onto <tt>onto</tt>.
640 If <tt>flag</tt> is nonzero then <tt>stack_undo</tt> considers this
641 an automatic move.
642 You may pass the original stack as <tt>onto</tt> or pass <tt>n</tt>
643 greater than the largest card index (see <tt>stack_count_cards</tt>)
644 to cancel a drag operation. </p>
645
646 </dl>
647
648 <h2>Adding Pictures</h2>
649
650 <p>It is to be expected that most applications will have some pictures
651 that are specific to that application. For example, the splash screen
652 ("Welcome to my game") usually has the name of the application, so it
653 will be specific. Each application is allowed to create one
654 additional library of pictures that can be linked into your program;
655 the library has the other one. To add images, paint your images in
656 standard PNG format (no animations, transparency is supported). Then,
657 call <tt>make-imglib</tt> like this:
658
659 <pre>
660 gif2pack -i $(srcdir) -n appimglib - mypictures.c *.png
661 </pre>
662
663 Of course, replace <tt>mypictures.c</tt> and <tt>*.png</tt> with
664 appropriate stuff for your application. This will create a C source
665 file that contains your image data. Then, compile and link that
666 source with your other sources. By calling your picture library
667 <tt>appimglib</tt>, the ace library functions will automatically use
668 it. When you call <tt>get_picture</tt>, pass the name of the original
669 file (<tt>*.png</tt>) but without path info or ".png". Run
670 "make-imglib -h" for a detailed list of options.</p>
671
672 </ul>
673
674 <h2>Calling X Directly</h2>
675
676 If you must call X directly (to draw lines or whatnot), look in xwin.c
677 for a list of non-static variables you can use <em>after</em> your
678 <tt>init()</tt> function is called. These are:
679
680 <pre>
681 extern Display *display;
682 extern int screen;
683 extern Visual *visual;
684 extern Colormap cmap;
685 extern Window window;
686 extern Window rootwin;
687 extern GC gc;
688 extern XFontStruct *font;
689 </pre>
690
691 You'll also need to include whatever X headers you need. Note that
692 during exposures, the clip mask in the gc is set as needed. If you
693 must mess with the clip mask, be careful to restore it if you can, or
694 create your own GC to draw with.
695
696 <p>If you do this, your program will not be portable to other
697 windowing systems if/when that ever happens.
698
699 <h2>Online Help</h2>
700
701 To add online help to your application, you need to write the help
702 file in plain HTML. There are only a few HTML constructs that are
703 recognized, and you <strong>must</strong> write valid HTML. Note that
704 closing tags are <em>not</em> optional if you want it to work right.
705 Here are the supported tags. You must use lower case, and when the
706 tag allows options (like <tt>img</tt>) they must be provided in the
707 order given with exactly the syntax shown. The HTML parser will not
708 accept just any HTML file. When a closing tag is shown, it is
709 mandatory. Tags may be nested, but do not need to be properly nested
710 (you <em>should</em> properly nest them, but the program won't blow up
711 if you don't).
712
713 <dl>
714
715 <dt><tt>&lt;b&gt; . . . &lt;/b&gt;</tt>
716
717 <dd> Bold
718
719 <dt><tt>&lt;i&gt; . . . &lt;/i&gt;</tt>
720
721 <dd> Italics
722
723 <dt><tt>&lt;tt&gt; . . . &lt;/tt&gt;</tt>
724
725 <dd> Teletype (monospaced) font
726
727 <dt><tt>&lt;big&gt; . . . &lt;/big&gt;</tt>
728
729 <dd> Big font
730
731 <dt><tt>&lt;h1&gt; . . . &lt;/h1&gt;</tt>
732
733 <dd> Primary headers. These are used to create the tabs that separate
734 pages, so don't use any other HTML inside an H1, avoid newlines, and
735 keep it short.
736
737 <dt><tt>&lt;h2&gt; . . . &lt;/h2&gt;</tt>
738
739 <dd> Secondary headers. Same as &lt;p&gt;&lt;big&gt;&lt;b&gt;. You
740 should put one of these (with more elaborate text) after each H1 so
741 that each page has a title banner.
742
743 <dt><tt>&lt;p&gt; . . . &lt;/p&gt;</tt>
744
745 <dd> Paragraph. Note that both &lt;p&gt; and &lt;/p&gt; do exactly
746 the same thing - they cause the next "thing" (word or picture) to
747 start a new paragraph - but you should use them properly anyway.
748
749 <dt><tt>&lt;br&gt;</tt>
750
751 <dd>Line break. The next "thing" will be at the left margin of the
752 next line. Multiple BR act like one BR.
753
754 <dt><tt>&lt;img src=file&gt;</tt>
755 <dt><tt>&lt;img align=center src=file&gt;</tt>
756 <dt><tt>&lt;img align=left src=file&gt;</tt>
757 <dt><tt>&lt;img align=right src=file&gt;</tt>
758
759 <dd>An image. No quotes around the file name, which is as you'd pass
760 to <tt>get_picture</tt> (since that's what it does). The
761 <tt>align=center</tt> variant aligns the picture vertically centered
762 on the line. If text is included in the line also, the text
763 centerline (not the baseline, but half the height of an "m") aligns
764 with the image centerline. Left and right alignment cause the image
765 to float to the margins, with text wrapping around them.
766
767 </dl>
768
769 <p>While developing the HTML help, run your application such that the
770 filename passed to <tt>help()</tt> refers to your HTML file. Then,
771 press <tt>F1</tt> to invoke the online help of your application, and
772 it will read the file instead of using the built-in text. Each time
773 you exit help (<tt>F1</tt> again, or <tt>Esc</tt>) and re-invoke it,
774 it re-reads the help file (you don't need to exit your
775 application).</p>
776
777 When you're done, use <tt>text2c</tt> to convert the HTML file to a C
778 source file, which you can compile and link in to your application:
779
780 <pre>
781 text2c my_help < mygame-help.html > mygame-help.c
782 </pre>
783
784 That first argument (<tt>my_help</tt> in this example) defines the
785 name of a symbol which you then refer to in your program:
786
787 <pre>
788 extern char my_help[];
789 ...
790 help("mygame-help.html", my_help);
791 </pre>
792
793 <hr noshade size=1> <i>Copyright &copy; 2001 <a
794 href="http://www.delorie.com/users/dj/">DJ Delorie</a></i>