You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

247 lines
4.6 KiB

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