home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / goattracker_2.73.zip / src / bme / bme_win.c < prev    next >
Encoding:
C/C++ Source or Header  |  2014-07-23  |  6.2 KB  |  254 lines

  1. //
  2. // BME (Blasphemous Multimedia Engine) windows & timing module
  3. //
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. #include <SDL/SDL.h>
  10. #include "bme_main.h"
  11. #include "bme_gfx.h"
  12. #include "bme_mou.h"
  13. #include "bme_io.h"
  14. #include "bme_err.h"
  15. #include "bme_cfg.h"
  16.  
  17. SDL_Joystick *joy[MAX_JOYSTICKS] = {NULL};
  18. Sint16 joyx[MAX_JOYSTICKS];
  19. Sint16 joyy[MAX_JOYSTICKS];
  20. Uint32 joybuttons[MAX_JOYSTICKS];
  21.  
  22. // Prototypes
  23.  
  24. int win_openwindow(char *appname, char *icon);
  25. void win_closewindow(void);
  26. void win_messagebox(char *string);
  27. void win_checkmessages(void);
  28. int win_getspeed(int framerate);
  29. void win_setmousemode(int mode);
  30.  
  31. // Global variables
  32.  
  33. int win_fullscreen = 0; // By default windowed
  34. int win_windowinitted = 0;
  35. int win_quitted = 0;
  36. unsigned char win_keytable[MAX_KEYS] = {0};
  37. unsigned char win_asciikey = 0;
  38. unsigned win_virtualkey = 0;
  39. unsigned win_mousexpos = 0;
  40. unsigned win_mouseypos = 0;
  41. unsigned win_mousexrel = 0;
  42. unsigned win_mouseyrel = 0;
  43. unsigned win_mousebuttons = 0;
  44. int win_mousemode = MOUSE_FULLSCREEN_HIDDEN;
  45. unsigned char win_keystate[MAX_KEYS] = {0};
  46.  
  47. // Static variables
  48.  
  49. static int win_lasttime = 0;
  50. static int win_currenttime = 0;
  51. static int win_framecounter = 0;
  52. static int win_activateclick = 0;
  53.  
  54. int win_openwindow(char *appname, char *icon)
  55. {
  56.     if (!win_windowinitted)
  57.     {
  58.         if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0)
  59.         {
  60.             return BME_ERROR;
  61.         }
  62.         atexit(SDL_Quit);
  63.         win_windowinitted = 1;
  64.     }
  65.  
  66.     SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
  67.     SDL_EnableUNICODE(1);
  68.     SDL_WM_SetCaption(appname, icon);
  69.     return BME_OK;
  70. }
  71.  
  72. void win_closewindow(void)
  73. {
  74. }
  75.  
  76. void win_messagebox(char *string)
  77. {
  78.     return;
  79. }
  80.  
  81. int win_getspeed(int framerate)
  82. {
  83.     // Note: here 1/10000th of a second accuracy is used (although
  84.     // timer resolution is only millisecond) for minimizing
  85.     // inaccuracy in frame duration calculation -> smoother screen
  86.     // update
  87.  
  88.     int frametime = 10000 / framerate;
  89.     int frames = 0;
  90.  
  91.     while (!frames)
  92.     {
  93.         win_checkmessages();
  94.  
  95.         win_lasttime = win_currenttime;
  96.         win_currenttime = SDL_GetTicks();
  97.  
  98.         win_framecounter += (win_currenttime - win_lasttime)*10;
  99.         frames = win_framecounter / frametime;
  100.         win_framecounter -= frames * frametime;
  101.  
  102.         if (!frames) SDL_Delay((frametime - win_framecounter)/10);
  103.     }
  104.  
  105.     return frames;
  106. }
  107.  
  108. // This is the "message pump". Called by following functions:
  109. // win_getspeed();
  110. // kbd_waitkey();
  111. //
  112. // It is recommended to be called in any long loop where those two functions
  113. // are not called.
  114.  
  115. void win_checkmessages(void)
  116. {
  117.     SDL_Event event;
  118.     unsigned keynum;
  119.  
  120.     win_activateclick = 0;
  121.  
  122.     SDL_PumpEvents();
  123.  
  124.     while (SDL_PollEvent(&event))
  125.     {
  126.         switch (event.type)
  127.         {
  128.             case SDL_JOYBUTTONDOWN:
  129.             joybuttons[event.jbutton.which] |= 1 << event.jbutton.button;
  130.             break;
  131.  
  132.             case SDL_JOYBUTTONUP:
  133.             joybuttons[event.jbutton.which] &= ~(1 << event.jbutton.button);
  134.             break;
  135.  
  136.             case SDL_JOYAXISMOTION:
  137.             switch (event.jaxis.axis)
  138.             {
  139.                 case 0:
  140.                 joyx[event.jaxis.which] = event.jaxis.value;
  141.                 break;
  142.  
  143.                 case 1:
  144.                 joyy[event.jaxis.which] = event.jaxis.value;
  145.                 break;
  146.             }
  147.             break;
  148.  
  149.             case SDL_MOUSEMOTION:
  150.             win_mousexpos = event.motion.x;
  151.             win_mouseypos = event.motion.y;
  152.             win_mousexrel += event.motion.xrel;
  153.             win_mouseyrel += event.motion.yrel;
  154.             break;
  155.  
  156.             case SDL_MOUSEBUTTONDOWN:
  157.             switch(event.button.button)
  158.             {
  159.                 case SDL_BUTTON_LEFT:
  160.                 win_mousebuttons |= MOUSEB_LEFT;
  161.                 break;
  162.  
  163.                 case SDL_BUTTON_MIDDLE:
  164.                 win_mousebuttons |= MOUSEB_MIDDLE;
  165.                 break;
  166.  
  167.                 case SDL_BUTTON_RIGHT:
  168.                 win_mousebuttons |= MOUSEB_RIGHT;
  169.                 break;
  170.             }
  171.             break;
  172.  
  173.             case SDL_MOUSEBUTTONUP:
  174.             switch(event.button.button)
  175.             {
  176.                 case SDL_BUTTON_LEFT:
  177.                 win_mousebuttons &= ~MOUSEB_LEFT;
  178.                 break;
  179.  
  180.                 case SDL_BUTTON_MIDDLE:
  181.                 win_mousebuttons &= ~MOUSEB_MIDDLE;
  182.                 break;
  183.  
  184.                 case SDL_BUTTON_RIGHT:
  185.                 win_mousebuttons &= ~MOUSEB_RIGHT;
  186.                 break;
  187.             }
  188.             break;
  189.  
  190.             case SDL_QUIT:
  191.             win_quitted = 1;
  192.             break;
  193.  
  194.             case SDL_KEYDOWN:
  195.             win_virtualkey = event.key.keysym.sym;
  196.             win_asciikey = event.key.keysym.unicode;
  197.             keynum = event.key.keysym.sym;
  198.             if (keynum < MAX_KEYS)
  199.             {
  200.                 win_keytable[keynum] = 1;
  201.                 win_keystate[keynum] = 1;
  202.                 if ((keynum == KEY_ENTER) && ((win_keystate[KEY_ALT]) || (win_keystate[KEY_RIGHTALT])))
  203.                 {
  204.                     win_fullscreen ^= 1;
  205.                     gfx_reinit();
  206.                 }
  207.             }
  208.             break;
  209.  
  210.             case SDL_KEYUP:
  211.             keynum = event.key.keysym.sym;
  212.             if (keynum < MAX_KEYS)
  213.             {
  214.                 win_keytable[keynum] = 0;
  215.                 win_keystate[keynum] = 0;
  216.             }
  217.             break;
  218.  
  219.             case SDL_VIDEORESIZE:
  220.             case SDL_VIDEOEXPOSE:
  221.             gfx_redraw = 1;
  222.             break;
  223.         }
  224.     }
  225. }
  226.  
  227. void win_setmousemode(int mode)
  228. {
  229.     win_mousemode = mode;
  230.  
  231.     switch(mode)
  232.     {
  233.         case MOUSE_ALWAYS_VISIBLE:
  234.         SDL_ShowCursor(SDL_ENABLE);
  235.         break;
  236.  
  237.         case MOUSE_FULLSCREEN_HIDDEN:
  238.         if (gfx_fullscreen)
  239.         {
  240.             SDL_ShowCursor(SDL_DISABLE);
  241.         }
  242.         else
  243.         {
  244.             SDL_ShowCursor(SDL_ENABLE);
  245.         }
  246.         break;
  247.  
  248.         case MOUSE_ALWAYS_HIDDEN:
  249.         SDL_ShowCursor(SDL_DISABLE);
  250.         break;
  251.     }
  252. }
  253.  
  254.