Posted By

ozkriff on 05/02/09


Tagged


Versions (?)

io.c


 / Published in: C
 

WIP

  1. #if 0
  2. #include "SDL/include/SDL.h"
  3. #include "SDL/SDL_GfxPrimitives/SDL_gfxPrimitives.h"
  4.  
  5. #pragma comment (lib, "SDL/lib/SDL.lib")
  6. #pragma comment (lib, "SDL/SDL_GfxPrimitives/SDL_GfxPrimitives_Static.lib")
  7. #endif
  8.  
  9. enum color {BLACK, RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW, WHITE, COLOR_MAX};
  10.  
  11.  
  12. static SDL_Surface* Screen;
  13. static Uint32 Colors [COLOR_MAX] = {0x000000ff,
  14. 0xff0000ff, 0x00ff00ff, 0x0000ffff,
  15. 0x00ffffff, 0xff00ffff, 0xffff00ff,
  16. 0xffffffff};
  17.  
  18. void clear_screen();
  19. void draw_rectangle( int X1, int Y1, int X2, int Y2, enum color C );
  20. int get_screen_height();
  21. void update_screen();
  22. int poll_key();
  23. int get_key();
  24. int is_key_down( int key );
  25. int init_graph();
  26.  
  27.  
  28. /* ---------------------------- CLEAR SCREEN ---------------------------- */
  29.  
  30. void clear_screen()
  31. {
  32. boxColor (Screen, 0, 0, Screen->w - 1, Screen->h - 1, Colors[BLACK]);
  33. }
  34.  
  35.  
  36. /* ---------------------------- DRAW RECTANGLE --------------------------- */
  37.  
  38. void draw_rectangle (
  39. int X1, int Y1, /* Upper left corner of the rectangle. */
  40. int X2, int Y2, /* Lower right corner of the rectangle. */
  41. enum color C ) /* Rectangle color. */
  42. {
  43. boxColor ( Screen, X1, Y1, X2, Y2-1, Colors[C] );
  44. }
  45.  
  46.  
  47. /* ---------------------------- GET_SCREEN_HEIGHT ---------------------- */
  48.  
  49. int get_screen_height()
  50. {
  51. return Screen->h;
  52. }
  53.  
  54.  
  55. /* ---------------------------- UPDATE_SCREEN ---------------------------- */
  56.  
  57. void update_screen()
  58. {
  59. SDL_Flip(Screen);
  60. }
  61.  
  62.  
  63. /* ---------------------------- POLL_KEY-------------------------------- */
  64. /* Keyboard input. */
  65.  
  66. int poll_key()
  67. {
  68. SDL_Event event;
  69.  
  70. while ( SDL_PollEvent(&event) ) {
  71. switch (event.type) {
  72. case SDL_KEYDOWN:
  73. return event.key.keysym.sym;
  74. case SDL_QUIT:
  75. exit(3);
  76. }
  77. }
  78. return -1;
  79. }
  80.  
  81.  
  82. /* ---------------------------- GET_KEY --------------------------------- */
  83. /* Keyboard input. */
  84.  
  85. int get_key()
  86. {
  87. SDL_Event event;
  88.  
  89. while (1) {
  90. SDL_WaitEvent(&event);
  91. if (event.type == SDL_KEYDOWN)
  92. break;
  93. if (event.type == SDL_QUIT)
  94. exit(3);
  95. };
  96. return event.key.keysym.sym;
  97. }
  98.  
  99.  
  100. /* ---------------------------- IS_KEY_DOWN ------------------------------ */
  101. /* Keyboard input. */
  102.  
  103. int is_key_down (int key)
  104. {
  105. Uint8* Keytable;
  106. int Numkeys;
  107. SDL_PumpEvents();
  108. Keytable = SDL_GetKeyState(&Numkeys);
  109. return Keytable[Key];
  110. }
  111.  
  112.  
  113. /* ---------------------------- GRAPHICAL INITIALIZATION ----------------- */
  114.  
  115. int init_graph()
  116. {
  117. const SDL_VideoInfo *info;
  118. Uint8 video_bpp;
  119. Uint32 videoflags;
  120.  
  121. /* Initialize SDL. */
  122. if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
  123. fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
  124. return 1;
  125. }
  126. atexit(SDL_Quit);
  127.  
  128. /* Alpha blending doesn't work well at 8-bit color. */
  129. info = SDL_GetVideoInfo();
  130. if ( info->vfmt->BitsPerPixel > 8 )
  131. video_bpp = info->vfmt->BitsPerPixel;
  132. else
  133. video_bpp = 16;
  134. videoflags = SDL_SWSURFACE | SDL_DOUBLEBUF;
  135.  
  136. /* Set 640x480 video mode. */
  137. if ( (Screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
  138. fprintf( stderr, "Couldn't set %ix%i video mode: %s\n",
  139. 640, 480, SDL_GetError() );
  140. return 2;
  141. }
  142.  
  143. return 0;
  144. }

Report this snippet  

You need to login to post a comment.