1 /*
2  *                               Alizarin Tetris
3  * The main header file. Data structures, definitions and global variables
4  * needed by all.
5  *
6  * Copyright 2000, Westley Weimer & Kiri Wagstaff
7  */
8 
9 #ifndef __ATRIS_H
10 #define __ATRIS_H
11 
12 #ifndef TRUE
13 #define TRUE 1
14 #endif
15 #ifndef FALSE
16 #define FALSE 0
17 #endif
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <SDL.h>
23 
24 /* configure magic for string.h */
25 #if STDC_HEADERS
26 # include <string.h>
27 #else
28 # ifndef HAVE_STRCHR
29 #  define strchr index
30 #  define strrchr rindex
31 # endif
32 char *strchr (), *strrchr ();
33 # ifndef HAVE_MEMCPY
34 #  define memcpy(d, s, n) bcopy ((s), (d), (n))
35 #  define memmove(d, s, n) bcopy ((s), (d), (n))
36 # endif
37 # ifndef HAVE_STRERROR
38 #  define strerror(x) "unknown error message (no strerror())"
39 # endif
40 # ifndef HAVE_STRDUP
41 #  error "We need strdup()!"
42 # endif
43 # ifndef HAVE_STRSTR
44 #  error "We need strstr()!"
45 # endif
46 #endif
47 
48 /* configure magic for time.h */
49 #if TIME_WITH_SYS_TIME
50 # include <sys/time.h>
51 # include <time.h>
52 #else
53 # if HAVE_SYS_TIME_H
54 #  include <sys/time.h>
55 # else
56 #  include <time.h>
57 # endif
58 #endif
59 
60 typedef enum {
61     SINGLE		=0,
62     MARATHON		=1,
63     SINGLE_VS_AI	=2,
64     TWO_PLAYERS		=3,
65     NETWORK		=4,
66     AI_VS_AI		=5,
67     QUIT		=6,
68     DEMO		=7
69 } GT;
70 GT gametype;
71 
72 #ifndef min
73 #define min(a,b)	((a)<(b)?(a):(b))
74 #endif
75 #ifndef max
76 #define max(a,b)	((a)>(b)?(a):(b))
77 #endif
78 
79 /* Debugging Output */
80 #define Debug(fmt, args...) printf("%-14.14s| ",__FUNCTION__), printf(fmt, ## args), fflush(stdout)
81 
82 /* Panic! Exit gracelessly. */
83 void Panic(const char *func, const char *file, char *fmt, ...) __attribute__ ((noreturn));
84 #define PANIC(fmt, args...) Panic(__FUNCTION__,__FILE__,fmt, ##args)
85 
86 #define Malloc(ptr,cast,size) {if(!(ptr=(cast)malloc(size)))PANIC("Out of Memory:\n\tcannot allocate %d bytes for "#ptr,size);}
87 #define Calloc(ptr,cast,size) {if(!(ptr=(cast)calloc(size,1)))PANIC("Out of Memory:\n\tcannot callocate %d bytes for "#ptr,size);}
88 #define Realloc(ptr,cast,size) {if(!(ptr=(cast)realloc(ptr,size)))PANIC("Out of Memory:\n\tcannot reallocate %d bytes for "#ptr,size);}
89 #define Free(ptr)       {free(ptr);ptr=NULL;}
90 #ifdef __STRING
91 #define Assert(cond)    {if(!(cond))PANIC("Failed assertion \"%s\" on line %d",__STRING(cond),__LINE__);}
92 #else
93 #define Assert(cond)    {if(!(cond))PANIC("Failed assertion \"%s\" on line %d",#cond,__LINE__);}
94 #endif
95 
96 extern void SeedRandom(Uint32 Seed);
97 extern Uint16 FastRandom(Uint16 range);
98 
99 
100 /*
101  * Quick safety routines.
102  */
103 #define SPRINTF(buf, fmt, args...) snprintf(buf, sizeof(buf), fmt, ## args)
104 
105 #define SDL_BlitSafe(a,b,c,d) { SDL_Rect *my_b = b, *my_d = d; \
106     if (my_b) { \
107 	if (my_b->x < 0) my_b->x = 0; \
108 	if (my_b->y < 0) my_b->y = 0; \
109 	if (my_b->x + my_b->w > 640) my_b->w = 640-my_b->x; \
110 	if (my_b->y + my_b->h > 480) my_b->h = 480-my_b->y; \
111     } if (my_d) { \
112 	if (my_d->x < 0) my_d->x = 0; \
113 	if (my_d->y < 0) my_d->y = 0; \
114 	if (my_d->x + my_d->w > 640) my_d->w = 640-my_d->x; \
115 	if (my_d->y + my_d->h > 480) my_d->h = 480-my_d->y; \
116     } \
117     Assert(SDL_BlitSurface(a,my_b,c,my_d) == 0); }
118 #define SDL_UpdateSafe(a,b,c) { SDL_Rect *my_c = c; \
119     Assert(my_c); \
120     if (my_c) { \
121 	if (my_c->x < 0) my_c->x = 0; \
122 	if (my_c->y < 0) my_c->y = 0; \
123 	if (my_c->x + my_c->w > 640) my_c->w = 640-my_c->x; \
124 	if (my_c->y + my_c->h > 480) my_c->h = 480-my_c->y; \
125     } \
126     SDL_UpdateRects(a,b,c); }
127 
128 #define ADJUST_UP	0
129 #define ADJUST_SAME	1
130 #define ADJUST_DOWN	2
131 
132 #include "atris.pro"
133 
134 #endif /* __ATRIS_H */
135 
136 /*
137  * $Log: atris.h,v $
138  * Revision 1.19  2000/11/06 01:22:40  wkiri
139  * Updated menu system.
140  *
141  * Revision 1.18  2000/10/29 21:23:28  weimer
142  * One last round of header-file changes to reflect my newest and greatest
143  * knowledge of autoconf/automake. Now if you fail to have some bizarro
144  * function, we try to go on anyway unless it is vastly needed.
145  *
146  * Revision 1.17  2000/10/29 03:16:59  weimer
147  * fflush on "Debug()"-style output
148  *
149  * Revision 1.16  2000/10/29 00:17:39  weimer
150  * added support for a system independent random number generator
151  *
152  * Revision 1.15  2000/10/21 01:14:42  weimer
153  * massic autoconf/automake restructure ...
154  *
155  * Revision 1.14  2000/10/18 23:57:49  weimer
156  * general fixup, color changes, display changes.
157  * Notable: "Safe" Blits and Updates now perform "clipping". No more X errors,
158  * we hope!
159  *
160  * Revision 1.13  2000/10/13 15:41:53  weimer
161  * revamped AI support, now you can pick your AI and have AI duels (such fun!)
162  * the mighty Aliz AI still crashes a bit, though ... :-)
163  *
164  * Revision 1.12  2000/10/12 22:21:25  weimer
165  * display changes, more multi-local-play threading (e.g., myScore ->
166  * Score[0]), that sort of thing ...
167  *
168  * Revision 1.11  2000/10/12 19:17:08  weimer
169  * Further support for AI players and multiple game types.
170  *
171  * Revision 1.10  2000/10/12 00:49:08  weimer
172  * added "AI" support and walking radio menus for initial game configuration
173  *
174  * Revision 1.9  2000/09/09 17:05:35  wkiri
175  * Hideous log changes (Wes: how dare you include a comment character!)
176  *
177  * Revision 1.8  2000/09/09 16:58:27  weimer
178  * Sweeping Change of Ultimate Mastery. Main loop restructuring to clean up
179  * main(), isolate the behavior of the three game types. Move graphic files
180  * into graphics/-, style files into styles/-, remove some unused files,
181  * add game flow support (breaks between games, remembering your level within
182  * this game), multiplayer support for the event loop, some global variable
183  * cleanup. All that and a bag of chips!
184  *
185  * Revision 1.7  2000/09/03 21:06:31  wkiri
186  * Now handles three different game types (and does different things).
187  * Major changes in display.c to handle this.
188  *
189  * Revision 1.6  2000/09/03 19:41:30  wkiri
190  * Now allows you to choose the game type (though it doesn't do anything yet).
191  *
192  * Revision 1.5  2000/09/03 18:26:10  weimer
193  * major header file and automatic prototype generation changes, restructuring
194  *
195  * Revision 1.4  2000/09/03 17:56:36  wkiri
196  * Reorganization of files (display.[ch], event.c are new).
197  *
198  * Revision 1.3  2000/08/14 01:06:45  wkiri
199  * Keeps track of high scores and adds to the list when appropriate.
200  * The high scores are kept in highscores.txt.
201  *
202  * Revision 1.2  2000/08/13 21:24:03  weimer
203  * preliminary sound
204  *
205  * Revision 1.1  2000/08/13 19:08:45  weimer
206  * Initial CVS creation for Atris.
207  *
208  * Revision 1.4  2000/08/05 00:50:53  weimer
209  * Weimer Changes
210  *
211  * Revision 1.3  2000/07/23 16:41:50  weimer
212  * yada
213  *
214  * Revision 1.2  2000/07/23 13:48:55  weimer
215  * added link to piece.h
216  *
217  * Revision 1.1  2000/07/23 13:29:30  wkiri
218  * Initial revision
219  *
220  */
221