1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 
5 #include "strings.h"
6 #include "types.h"
7 #include "util.h"
8 
9 #include "config.h"
10 
11 #include "Game.h"
12 #include "UI.h"
13 
14 #if USE_ATHENA
15 #include "x11-athena.h"
16 #endif
17 
18 #if USE_MOTIF
19 #include "x11-motif.h"
20 #endif
21 
22 #if USE_GTK
23 #include "gtk.h"
24 #endif
25 
26 static int playing;
27 static UI_methods *methods;
28 static const char *dialog_strings[DIALOG_MAX + 1];
29 static const char *menu_strings[DIALOG_MAX + 1];
30 
31 /*
32  * Timer control routines
33  */
34 
35 void
UI_restart_timer()36 UI_restart_timer() {
37 	methods->start_timer(200);
38 }
39 
40 void
UI_kill_timer()41 UI_kill_timer() {
42 	methods->stop_timer();
43 }
44 
45 void
UI_pause_game()46 UI_pause_game() {
47 	if (methods->timer_active())
48 		playing = 1;
49 	UI_kill_timer();
50 }
51 
52 void
UI_resume_game()53 UI_resume_game() {
54 	if (playing && !methods->timer_active())
55 		UI_restart_timer();
56 	playing = 0;
57 }
58 
59 /*
60  * Window routines
61  */
62 
63 typedef struct guimap {
64 	const char *name;
65 	void (*setmethods)(UI_methods **methodsp);
66 } guimap;
67 
68 static guimap guis[] = {
69 #ifdef USE_GTK
70 	{"gtk", gtk_ui_setmethods},
71 #endif
72 #ifdef USE_MOTIF
73 	{"motif", x11_motif_setmethods},
74 #endif
75 #ifdef USE_ATHENA
76 	{"athena", x11_athena_setmethods},
77 #endif
78 	{NULL, NULL},
79 };
80 
81 void
UI_initialize(const char * gui,int * argc,char ** argv)82 UI_initialize(const char *gui, int *argc, char **argv) {
83 	guimap *map;
84 	if (gui == NULL) {
85 		map = guis;
86 		if (map->name == NULL)
87 			fatal("no configured GUIs");
88 		map->setmethods(&methods);
89 	} else {
90 		for (map = guis; map->name != NULL; map++)
91 			if (strcasecmp(gui, map->name) == 0)
92 				break;
93 		if (map->name == NULL)
94 			fatal("GUI '%s' not found", gui);
95 		map->setmethods(&methods);
96 	}
97 	methods->initialize(argc, argv);
98 }
99 
100 void
UI_make_main_window(int size)101 UI_make_main_window(int size) {
102 	menu_strings[DIALOG_NEWGAME] = newgame_menu_str;
103 	menu_strings[DIALOG_PAUSEGAME] = pause_menu_str;
104 	menu_strings[DIALOG_WARPLEVEL] = warp_menu_str;
105 	menu_strings[DIALOG_HIGHSCORE] = highscore_menu_str;
106 	menu_strings[DIALOG_QUITGAME] = quit_menu_str;
107 	menu_strings[DIALOG_STORY] = story_menu_str;
108 	menu_strings[DIALOG_RULES] = rules_menu_str;
109 	menu_strings[DIALOG_ABOUT] = about_menu_str;
110 	menu_strings[DIALOG_SCORE] = score_menu_str;
111 	menu_strings[DIALOG_ENDGAME] = endgame_menu_str;
112 	menu_strings[DIALOG_ENTERNAME] = entername_menu_str;
113 	methods->make_main_window(size);
114 }
115 
116 void
UI_graphics_init()117 UI_graphics_init() {
118 	methods->graphics_init();
119 }
120 
121 void
UI_make_dialogs(Picture * logo,Picture * icon,Picture * about)122 UI_make_dialogs(Picture *logo, Picture *icon, Picture *about) {
123 	dialog_strings[DIALOG_NEWGAME] = newgame_dialog_str;
124 	dialog_strings[DIALOG_PAUSEGAME] = pause_dialog_str;
125 	dialog_strings[DIALOG_WARPLEVEL] = warp_dialog_str;
126 	dialog_strings[DIALOG_HIGHSCORE] = NULL;
127 	dialog_strings[DIALOG_QUITGAME] = quit_dialog_str;
128 	dialog_strings[DIALOG_STORY] = story_dialog_str;
129 	dialog_strings[DIALOG_RULES] = rules_dialog_str;
130 	dialog_strings[DIALOG_ABOUT] = NULL;
131 	dialog_strings[DIALOG_SCORE] = NULL;
132 	dialog_strings[DIALOG_ENDGAME] = endgame_dialog_str;
133 	dialog_strings[DIALOG_ENTERNAME] = entername_dialog_str;
134 	methods->create_dialogs(logo, icon, about);
135 }
136 
137 void
UI_popup_dialog(int dialog)138 UI_popup_dialog(int dialog) {
139 	methods->popup_dialog(dialog);
140 }
141 
142 /*
143  * Graphics routines
144  */
145 
146 void
UI_set_cursor(MCursor * cursor)147 UI_set_cursor(MCursor *cursor) {
148 	methods->set_cursor(cursor);
149 }
150 
151 void
UI_set_icon(Picture * icon)152 UI_set_icon(Picture *icon) {
153 	methods->set_icon(icon);
154 }
155 
156 void
UI_clear()157 UI_clear() {
158 	methods->clear_window();
159 }
160 
161 void
UI_refresh()162 UI_refresh() {
163 	methods->refresh_window();
164 }
165 
166 void
UI_draw(Picture * pict,int x,int y)167 UI_draw(Picture *pict, int x, int y) {
168 	methods->draw_image(pict, x, y);
169 }
170 
171 void
UI_draw_line(int x1,int y1,int x2,int y2)172 UI_draw_line(int x1, int y1, int x2, int y2) {
173 	methods->draw_line(x1, y1, x2, y2);
174 }
175 
176 void
UI_draw_str(const char * str,int x,int y)177 UI_draw_str(const char *str, int x, int y) {
178 	methods->draw_string(str, x, y);
179 }
180 
181 /*
182  * Other routines
183  */
184 
185 void
UI_set_pausebutton(int action)186 UI_set_pausebutton (int action) {
187 	methods->set_pausebutton(action);
188 }
189 
190 void
UI_main_loop()191 UI_main_loop() {
192 	methods->main_loop();
193 }
194 
195 void
UI_load_picture_indexed(const char * name,int index,int trans,Picture ** pictp)196 UI_load_picture_indexed(const char *name, int index, int trans, Picture **pictp)
197 {
198 	char *newname;
199 	if (index > 99)
200 		fatal("image index too large");
201 	newname = xalloc(strlen(name) + 4);
202 	sprintf(newname, "%s_%d", name, index);
203 	UI_load_picture(newname, trans, pictp);
204 	free(newname);
205 }
206 
207 void
UI_load_picture(const char * name,int trans,Picture ** pictp)208 UI_load_picture(const char *name, int trans, Picture **pictp) {
209 	methods->load_picture(name, trans, pictp);
210 }
211 
212 int
UI_picture_width(Picture * pict)213 UI_picture_width(Picture *pict) {
214 	return methods->picture_width(pict);
215 }
216 
217 int
UI_picture_height(Picture * pict)218 UI_picture_height(Picture *pict) {
219 	return methods->picture_height(pict);
220 }
221 
222 void
UI_load_cursor(const char * name,int masked,MCursor ** cursorp)223 UI_load_cursor(const char *name, int masked, MCursor **cursorp) {
224 	methods->load_cursor(name, masked, cursorp);
225 }
226 
227 int
UI_intersect(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2)228 UI_intersect(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) {
229 	return ((abs(x2 - x1 + (w2 - w1) / 2) < (w1 + w2) / 2) &&
230 		(abs(y2 - y1 + (h2 - h1) / 2) < (h1 + h2) / 2));
231 }
232 
233 void
UI_update_dialog(int index,const char * str)234 UI_update_dialog(int index, const char *str) {
235 	methods->update_dialog(index, str);
236 }
237 
238 const char *
UI_dialog_string(int index)239 UI_dialog_string(int index) {
240 	return dialog_strings[index];
241 }
242 
243 const char *
UI_menu_string(int index)244 UI_menu_string(int index) {
245 	return menu_strings[index];
246 }
247