1 /* File: main-tnb.c */
2 
3 /* Purpose: program entry point */
4 
5 /*
6  * Copyright (c) 1997-2001 Tim Baker
7  *
8  * This software may be copied and distributed for educational, research, and
9  * not for profit purposes provided that this copyright and statement are
10  * included in all such copies.
11  */
12 
13 #include "angband.h"
14 #ifdef USE_TNB
15 
16 #include "tk/tnb.h"
17 #include "maid-grf.h"
18 
19 cptr help_tnb[] =
20 {
21 	"To use tk/tcl port",
22 	NULL
23 };
24 
25 
26 static term data;
27 bool game_in_progress = FALSE;
28 char ANGBAND_DIR_TK[1024];
29 Tcl_Interp *g_interp;
30 
31 /* Graphics information */
32 int tnb_tile_x;
33 int tnb_tile_y;
34 
35 char tnb_tile_file[1024];
36 
37 /* Font information */
38 char tnb_font_file[1024];
39 int tnb_font_size;
40 
tnb_get_term(int x,int y,byte * a,char * c,byte * ta,char * tc)41 void tnb_get_term(int x, int y, byte *a, char *c, byte *ta, char *tc)
42 {
43 	/* Get the term information */
44 	*a = data.scr->a[y][x];
45 	*c = data.scr->c[y][x];
46 	*ta = data.scr->ta[y][x];
47 	*tc = data.scr->tc[y][x];
48 }
49 
Term_user_tnb(int n)50 static errr Term_user_tnb(int n)
51 {
52 	/* Hack - ignore parameters for now */
53 	(void) n;
54 
55 	return (0);
56 }
57 
58 /*
59  * Process at least one event
60  */
Term_xtra_tnb_event(int v)61 static errr Term_xtra_tnb_event(int v)
62 {
63 	int flags;
64 
65 	/* Wait for an event */
66 	if (v) {
67 
68 		/* Block */
69 		flags = TCL_ALL_EVENTS;
70 
71 	/* Check for an event */
72 	} else {
73 
74 		/* Check */
75 		flags = TCL_ALL_EVENTS | TCL_DONT_WAIT;
76 	}
77 
78 	(void) Tcl_DoOneEvent(flags);
79 
80 	/* Success */
81 	return 0;
82 }
83 
84 
85 /*
86  * Process all pending events
87  */
Term_xtra_tnb_flush(void)88 static errr Term_xtra_tnb_flush(void)
89 {
90 	int flags = TCL_ALL_EVENTS | TCL_DONT_WAIT;
91 
92 	while (Tcl_DoOneEvent(flags)) ;
93 
94 	/* Success */
95 	return (0);
96 }
97 
98 /*
99  * Hack -- make a noise
100  */
Term_xtra_tnb_noise(void)101 static errr Term_xtra_tnb_noise(void)
102 {
103 #ifdef PLATFORM_WIN
104 	MessageBeep(MB_ICONASTERISK);
105 #endif /* PLATFORM_WIN */
106 
107 	return (0);
108 }
109 
110 /*
111  * Hack -- make a sound
112  */
Term_xtra_tnb_sound(int v)113 static errr Term_xtra_tnb_sound(int v)
114 {
115 	/* Hack - ignore parameters for now */
116 	(void) v;
117 
118 	return (0);
119 }
120 
121 /*
122  * Delay for "x" milliseconds
123  */
Term_xtra_tnb_delay(int v)124 static int Term_xtra_tnb_delay(int v)
125 {
126 	if (v <= 0)
127 		return (0);
128 
129 #ifdef PLATFORM_WIN
130 
131 	/* Sleep */
132 	Sleep(v);
133 
134 #endif /* PLATFORM_WIN */
135 
136 #ifdef PLATFORM_X11
137 
138 	usleep(1000 * v);
139 
140 #endif /* PLATFORM_X11 */
141 
142 	/* Success */
143 	return (0);
144 }
145 
146 /*
147  * Do a "special thing"
148  */
Term_xtra_tnb(int n,int v)149 static errr Term_xtra_tnb(int n, int v)
150 {
151 	/* Handle a subset of the legal requests */
152 	switch (n)
153 	{
154 		/* Make a bell sound */
155 		case TERM_XTRA_NOISE:
156 		{
157 			return (Term_xtra_tnb_noise());
158 		}
159 
160 		/* Make a special sound */
161 		case TERM_XTRA_SOUND:
162 		{
163 			return (Term_xtra_tnb_sound(v));
164 		}
165 
166 		/* Process random events */
167 		case TERM_XTRA_BORED:
168 		{
169 			return (Term_xtra_tnb_event(0));
170 		}
171 
172 		/* Process an event */
173 		case TERM_XTRA_EVENT:
174 		{
175 			return (Term_xtra_tnb_event(v));
176 		}
177 
178 		/* Flush all events */
179 		case TERM_XTRA_FLUSH:
180 		{
181 			return (Term_xtra_tnb_flush());
182 		}
183 
184 		/* React to global changes */
185 		case TERM_XTRA_REACT:
186 		{
187 			return (Term_xtra_tnb_react());
188 		}
189 
190 		/* Delay for some milliseconds */
191 		case TERM_XTRA_DELAY:
192 		{
193 			return (Term_xtra_tnb_delay(v));
194 		}
195 
196 		/* Flush the output XXX XXX */
197 		case TERM_XTRA_FRESH:
198 		{
199 			int flags = TCL_WINDOW_EVENTS | TCL_IDLE_EVENTS | TCL_DONT_WAIT;
200 
201 			while (Tcl_DoOneEvent(flags) != 0)
202 				;
203 
204 			return (0);
205 		}
206 	}
207 
208 	/* Oops */
209 	return 1;
210 }
211 
212 
term_data_link(term * t)213 static void term_data_link(term *t)
214 {
215 	/* Initialize the term */
216 	term_init(t, 80, 24, 1024);
217 
218 	/* Use a "software" cursor */
219 	t->soft_cursor = TRUE;
220 
221 	/* Use "Term_pict" for "graphic" data */
222 	t->higher_pict = TRUE;
223 
224 	/* Erase with "black space" */
225 	t->attr_blank = TERM_DARK;
226 	t->char_blank = ' ';
227 
228 	/* Prepare the template hooks */
229 	t->user_hook = Term_user_tnb;
230 	t->xtra_hook = Term_xtra_tnb;
231 	t->curs_hook = Term_curs_tnb;
232 	t->wipe_hook = Term_wipe_tnb;
233 	t->text_hook = Term_text_tnb;
234 	t->pict_hook = Term_pict_tnb;
235 
236 	/* Remember where we came from */
237 	t->data = NULL;
238 }
239 
init_windows(void)240 static void init_windows(void)
241 {
242 	int i;
243 
244 	term *t = &data;
245 
246 	/* Main window */
247 	term_data_link(t);
248 	angband_term[0] = t;
249 
250 	/* No extra Term's required */
251 	for (i = 1; i < 8; i++)
252 	{
253 		angband_term[i] = NULL;
254 	}
255 
256 	Term_activate(t);
257 }
258 
259 
260 /*
261  * Display error message and quit (see "z-util.c")
262  */
hook_quit(cptr str)263 static void hook_quit(cptr str)
264 {
265 	(void) str;
266 
267 	Icon_Exit();
268 
269 	/* cleanup_angband(); */
270 
271 	/* Cleanup Tcl and Tk */
272 	Tcl_DeleteInterp(g_interp);
273 
274 	/* Hack - no longer hook tcl memory routines */
275 	rnfree_aux = NULL;
276 }
277 
278 /*
279  * Memory allocation wrappers
280  */
my_tcl_free(vptr data)281 static vptr my_tcl_free(vptr data)
282 {
283 	Tcl_Free((char *) data);
284 
285 	return (NULL);
286 }
287 
my_tcl_alloc(huge size)288 static vptr my_tcl_alloc(huge size)
289 {
290 	return (Tcl_Alloc(size));
291 }
292 
293 
294 #ifdef PLATFORM_X11
295 
296 /*
297  * Init the tk port
298  */
init_tnb(int argc,cptr * argv)299 int init_tnb(int argc, cptr *argv)
300 {
301 	/* Hack -ignore parameter */
302 	(void) argc;
303 
304 	/* Save the "tk" directory */
305 	path_make(ANGBAND_DIR_TK, ANGBAND_DIR_SCRIPT, "tk");
306 
307 	/* Use graphics */
308 	pick_graphics(GRAPHICS_ADAM_BOLT, &tnb_tile_x, &tnb_tile_y, tnb_tile_file);
309 
310 	/* Try the "16x16.bmp" file */
311 	path_make(tnb_font_file, ANGBAND_DIR_XTRA, "font/16x16.txt");
312 
313 	/* Use the "16x16.bmp" file if it exists */
314 	if (fd_close(fd_open(tnb_font_file, O_RDONLY)))
315 	{
316 		quit("Could not initialise font metrics!");
317 	}
318 
319 	/* Always 16x16 for now */
320 	tnb_font_size = 16;
321 
322 	/* Prepare the windows */
323 	init_windows();
324 
325 	/* Activate hooks */
326 	quit_aux = hook_quit;
327 	core_aux = hook_quit;
328 
329 	/* Hack - TclTk_Init doesn't fail gracefully, so check manually for X11 */
330 	if (!XOpenDisplay("")) return (1);
331 
332 	/* Initialize Tcl and Tk. */
333 	g_interp = TclTk_Init(argv);
334 
335 	/* Paranoia */
336 	if (!g_interp) return(1);
337 
338 	/* Set the memory-allocation hooks */
339 	rnfree_aux = my_tcl_free;
340 	ralloc_aux = my_tcl_alloc;
341 
342 	/* Initialize */
343 	angtk_init();
344 
345 	/* Catch nasty signals */
346 	signals_init();
347 
348 	/* Initialize */
349 	init_angband();
350 
351 	/* Program is intialized */
352 	angtk_angband_initialized();
353 
354 	/* Init colours */
355 	Term_xtra_tnb_react();
356 
357 #if 0
358 	while (TRUE)
359 	{
360 	while (Tcl_DoOneEvent(TRUE) != 0)
361 				;
362 	}
363 #endif /* 0 */
364 
365 	/* Press a key for the player */
366 	Term_keypress(' ');
367 
368 	/* Paranoia */
369 	return (0);
370 }
371 
372 #endif /* PLATFORM_X11 */
373 
374 #endif /* USE_TNB */
375