No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 

229 líneas
4.6 KiB

  1. /*
  2. * ttyvaders Textmode shoot'em up
  3. * Copyright (c) 2002 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include "config.h"
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <sys/time.h>
  26. #include <time.h>
  27. #include "common.h"
  28. int init_graphics( void )
  29. {
  30. #ifdef USE_SLANG
  31. /* Initialize slang library */
  32. SLsig_block_signals();
  33. SLtt_get_terminfo();
  34. if( SLkp_init() == -1 )
  35. {
  36. SLsig_unblock_signals();
  37. return 1;
  38. }
  39. SLang_init_tty (-1, 0, 1);
  40. if( SLsmg_init_smg() == -1 )
  41. {
  42. SLsig_unblock_signals();
  43. return 1;
  44. }
  45. SLsig_unblock_signals();
  46. SLsmg_cls();
  47. SLtt_set_cursor_visibility( 0 );
  48. SLsmg_refresh();
  49. #elif USE_NCURSES
  50. /* Initialize ncurses library */
  51. initscr();
  52. keypad(stdscr, TRUE);
  53. nonl();
  54. cbreak();
  55. noecho();
  56. nodelay(stdscr, TRUE);
  57. curs_set( 0 );
  58. #else
  59. /* Dummy driver */
  60. #endif
  61. return 0;
  62. }
  63. void init_game( game *g )
  64. {
  65. #ifdef USE_SLANG
  66. static char * const colors[] =
  67. {
  68. "black", "green", "yellow", "white",
  69. "red", "gray", "lightgray", "blue", "cyan", "magenta", NULL
  70. };
  71. int i;
  72. for( i = 0; colors[i] ; i++ )
  73. {
  74. SLtt_set_color( i+1, NULL, colors[i], "black" );
  75. }
  76. g->w = SLtt_Screen_Cols;
  77. g->h = SLtt_Screen_Rows;
  78. #elif USE_NCURSES
  79. start_color();
  80. init_pair( BLACK, COLOR_BLACK, COLOR_BLACK );
  81. init_pair( GREEN, COLOR_GREEN, COLOR_BLACK );
  82. init_pair( YELLOW, COLOR_YELLOW, COLOR_BLACK );
  83. init_pair( WHITE, COLOR_WHITE, COLOR_BLACK );
  84. init_pair( RED, COLOR_RED, COLOR_BLACK );
  85. init_pair( GRAY, COLOR_WHITE, COLOR_BLACK ); // XXX
  86. init_pair( LIGHTGRAY, COLOR_WHITE, COLOR_BLACK ); // XXX
  87. init_pair( BLUE, COLOR_BLUE, COLOR_BLACK );
  88. init_pair( CYAN, COLOR_CYAN, COLOR_BLACK );
  89. init_pair( MAGENTA, COLOR_MAGENTA, COLOR_BLACK );
  90. g->w = COLS;
  91. g->h = LINES;
  92. #else
  93. /* Use dummy driver */
  94. g->w = 80;
  95. g->h = 25;
  96. #endif
  97. }
  98. char get_key( void )
  99. {
  100. #ifdef USE_SLANG
  101. if( SLang_input_pending (0) )
  102. {
  103. return SLang_getkey();
  104. }
  105. #elif USE_NCURSES
  106. char key;
  107. if( ( key = getch() ) != ERR )
  108. {
  109. return key;
  110. }
  111. #else
  112. /* Use dummy driver */
  113. char key = GET_RAND(0,256);
  114. if( key != 'q' && key != 'p' && key != '\t' )
  115. {
  116. return key;
  117. }
  118. #endif
  119. return 0;
  120. }
  121. void clear_graphics( game *g )
  122. {
  123. #ifdef USE_SLANG
  124. //SLsmg_cls();
  125. int y;
  126. for( y = 0; y < g->h; y++ )
  127. {
  128. gfx_goto( 0, y );
  129. gfx_putstr( " " );
  130. }
  131. #elif USE_NCURSES
  132. //clear();
  133. int y;
  134. for( y = 0; y < g->h; y++ )
  135. {
  136. gfx_goto( 0, y );
  137. gfx_putstr( " " );
  138. }
  139. #else
  140. /* Use dummy driver */
  141. #endif
  142. }
  143. static int64_t local_time(void)
  144. {
  145. struct timeval tv;
  146. int64_t now;
  147. gettimeofday(&tv, NULL);
  148. now = tv.tv_sec;
  149. now *= 1000000;
  150. now += tv.tv_usec;
  151. return now;
  152. }
  153. #define DELAY 40000
  154. void refresh_graphics( void )
  155. {
  156. static int64_t local_clock = 0;
  157. int64_t now;
  158. gfx_goto( 0, 0 );
  159. if( !local_clock )
  160. {
  161. /* Initialize local_clock */
  162. local_clock = local_time();
  163. }
  164. if( local_time() > local_clock + 10000 )
  165. {
  166. /* If we are late, we shouldn't display anything */
  167. }
  168. #ifdef USE_SLANG
  169. SLsmg_refresh();
  170. #elif USE_NCURSES
  171. refresh();
  172. #else
  173. /* Use dummy driver */
  174. #endif
  175. now = local_time();
  176. if( now < local_clock + DELAY - 10000 )
  177. {
  178. usleep( local_clock + DELAY - 10000 - now );
  179. }
  180. local_clock += DELAY;
  181. }
  182. void end_graphics( void )
  183. {
  184. #ifdef USE_SLANG
  185. SLtt_set_cursor_visibility( 1 );
  186. SLang_reset_tty();
  187. SLsmg_reset_smg();
  188. #elif USE_NCURSES
  189. curs_set( 1 );
  190. endwin();
  191. #else
  192. /* Use dummy driver */
  193. #endif
  194. }