home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / src / gtk / system.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-06  |  6.2 KB  |  344 lines

  1. // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
  2. // Copyright (C) 1999-2003 Forgotten
  3. // Copyright (C) 2004 Forgotten and the VBA development team
  4.  
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2, or(at your option)
  8. // any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software Foundation,
  17. // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  
  19. #include <stdio.h>
  20. #include <stdarg.h>
  21. #include <string.h>
  22.  
  23. #include <SDL.h>
  24. #include <SDL_thread.h>
  25.  
  26. #include "../GBA.h"
  27. #include "../gb/GB.h"
  28. #include "../gb/gbGlobals.h"
  29. #include "../Util.h"
  30. #include "../Sound.h"
  31.  
  32. #include "window.h"
  33. #include "intl.h"
  34.  
  35. // Required vars, used by the emulator core
  36. //
  37. int  systemRedShift;
  38. int  systemGreenShift;
  39. int  systemBlueShift;
  40. int  systemColorDepth;
  41. int  systemDebug;
  42. int  systemVerbose;
  43. int  systemSaveUpdateCounter;
  44. int  systemFrameSkip;
  45. u32  systemColorMap32[0x10000];
  46. u16  systemColorMap16[0x10000];
  47. u16  systemGbPalette[24];
  48. bool systemSoundOn;
  49.  
  50. int  emulating;
  51. bool debugger;
  52. int  RGB_LOW_BITS_MASK;
  53.  
  54. // Extra vars, only used for the GUI
  55. //
  56. int systemRenderedFrames;
  57. int systemFPS;
  58.  
  59. // Sound stuff
  60. //
  61. const  int         iSoundSamples  = 2048;
  62. const  int         iSoundTotalLen = iSoundSamples * 4;
  63. static u8          auiSoundBuffer[iSoundTotalLen];
  64. static int         iSoundLen;
  65. static SDL_cond *  pstSoundCond;
  66. static SDL_mutex * pstSoundMutex;
  67.  
  68. inline VBA::Window * GUI()
  69. {
  70.   return VBA::Window::poGetInstance();
  71. }
  72.  
  73. void systemMessage(int _iId, const char * _csFormat, ...)
  74. {
  75.   va_list args;
  76.   va_start(args, _csFormat);
  77.  
  78.   GUI()->vPopupErrorV(_(_csFormat), args);
  79.  
  80.   va_end(args);
  81. }
  82.  
  83. void systemDrawScreen()
  84. {
  85.   GUI()->vDrawScreen();
  86.   systemRenderedFrames++;
  87. }
  88.  
  89. bool systemReadJoypads()
  90. {
  91.   return true;
  92. }
  93.  
  94. u32 systemReadJoypad(int)
  95. {
  96.   return GUI()->uiReadJoypad();
  97. }
  98.  
  99. void systemShowSpeed(int _iSpeed)
  100. {
  101.   systemFPS = systemRenderedFrames;
  102.   systemRenderedFrames = 0;
  103.  
  104.   GUI()->vShowSpeed(_iSpeed);
  105. }
  106.  
  107. void system10Frames(int _iRate)
  108. {
  109.   GUI()->vComputeFrameskip(_iRate);
  110. }
  111.  
  112. void systemFrame()
  113. {
  114. }
  115.  
  116. void systemSetTitle(const char * _csTitle)
  117. {
  118.   GUI()->set_title(_csTitle);
  119. }
  120.  
  121. void systemScreenCapture(int _iNum)
  122. {
  123.   GUI()->vCaptureScreen(_iNum);
  124. }
  125.  
  126. void systemWriteDataToSoundBuffer()
  127. {
  128.   if (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING)
  129.   {
  130.     SDL_PauseAudio(0);
  131.   }
  132.  
  133.   bool bWait = true;
  134.   while (bWait && ! speedup && GUI()->iGetThrottle() == 0)
  135.   {
  136.     SDL_mutexP(pstSoundMutex);
  137.     if (iSoundLen < iSoundTotalLen)
  138.     {
  139.       bWait = false;
  140.     }
  141.     SDL_mutexV(pstSoundMutex);
  142.   }
  143.  
  144.   int iLen = soundBufferLen;
  145.   int iCopied = 0;
  146.   if (iSoundLen + iLen >= iSoundTotalLen)
  147.   {
  148.     iCopied = iSoundTotalLen - iSoundLen;
  149.     memcpy(&auiSoundBuffer[iSoundLen], soundFinalWave, iCopied);
  150.  
  151.     iSoundLen = iSoundTotalLen;
  152.     SDL_CondSignal(pstSoundCond);
  153.  
  154.     bWait = true;
  155.     if (! speedup && GUI()->iGetThrottle() == 0)
  156.     {
  157.       while(bWait)
  158.       {
  159.         SDL_mutexP(pstSoundMutex);
  160.         if (iSoundLen < iSoundTotalLen)
  161.         {
  162.           bWait = false;
  163.         }
  164.         SDL_mutexV(pstSoundMutex);
  165.       }
  166.  
  167.       memcpy(auiSoundBuffer, ((u8 *)soundFinalWave) + iCopied,
  168.              soundBufferLen - iCopied);
  169.  
  170.       iSoundLen = soundBufferLen - iCopied;
  171.     }
  172.     else
  173.     {
  174.       memcpy(auiSoundBuffer, ((u8 *)soundFinalWave) + iCopied,
  175.              soundBufferLen);
  176.     }
  177.   }
  178.   else
  179.   {
  180.     memcpy(&auiSoundBuffer[iSoundLen], soundFinalWave, soundBufferLen);
  181.     iSoundLen += soundBufferLen;
  182.   }
  183. }
  184.  
  185. static void vSoundCallback(void * _pvUserData, u8 * _puiStream, int _iLen)
  186. {
  187.   if (! emulating)
  188.   {
  189.     return;
  190.   }
  191.  
  192.   SDL_mutexP(pstSoundMutex);
  193.   if (! speedup && GUI()->iGetThrottle() == 0)
  194.   {
  195.     while (iSoundLen < iSoundTotalLen && emulating)
  196.     {
  197.       SDL_CondWait(pstSoundCond, pstSoundMutex);
  198.     }
  199.   }
  200.   if (emulating)
  201.   {
  202.     memcpy(_puiStream, auiSoundBuffer, _iLen);
  203.   }
  204.   iSoundLen = 0;
  205.   SDL_mutexV(pstSoundMutex);
  206. }
  207.  
  208. bool systemSoundInit()
  209. {
  210.   SDL_AudioSpec stAudio;
  211.  
  212.   switch (soundQuality)
  213.   {
  214.   case 1:
  215.     stAudio.freq = 44100;
  216.     soundBufferLen = 1470 * 2;
  217.     break;
  218.   case 2:
  219.     stAudio.freq = 22050;
  220.     soundBufferLen = 736 * 2;
  221.     break;
  222.   case 4:
  223.     stAudio.freq = 11025;
  224.     soundBufferLen = 368 * 2;
  225.     break;
  226.   }
  227.  
  228.   stAudio.format   = AUDIO_S16SYS;
  229.   stAudio.channels = 2;
  230.   stAudio.samples  = iSoundSamples;
  231.   stAudio.callback = vSoundCallback;
  232.   stAudio.userdata = NULL;
  233.  
  234.   if (SDL_OpenAudio(&stAudio, NULL) < 0)
  235.   {
  236.     fprintf(stderr, "Failed to open audio: %s\n", SDL_GetError());
  237.     return false;
  238.   }
  239.  
  240.   pstSoundCond  = SDL_CreateCond();
  241.   pstSoundMutex = SDL_CreateMutex();
  242.  
  243.   soundBufferTotalLen = soundBufferLen * 10;
  244.   iSoundLen = 0;
  245.   systemSoundOn = true;
  246.  
  247.   return true;
  248. }
  249.  
  250. void systemSoundShutdown()
  251. {
  252.   SDL_mutexP(pstSoundMutex);
  253.   int iSave = emulating;
  254.   emulating = 0;
  255.   SDL_CondSignal(pstSoundCond);
  256.   SDL_mutexV(pstSoundMutex);
  257.  
  258.   SDL_DestroyCond(pstSoundCond);
  259.   pstSoundCond = NULL;
  260.  
  261.   SDL_DestroyMutex(pstSoundMutex);
  262.   pstSoundMutex = NULL;
  263.  
  264.   SDL_CloseAudio();
  265.  
  266.   emulating = iSave;
  267.   systemSoundOn = false;
  268. }
  269.  
  270. void systemSoundPause()
  271. {
  272.   SDL_PauseAudio(1);
  273. }
  274.  
  275. void systemSoundResume()
  276. {
  277.   SDL_PauseAudio(0);
  278. }
  279.  
  280. void systemSoundReset()
  281. {
  282. }
  283.  
  284. u32 systemGetClock()
  285. {
  286.   return SDL_GetTicks();
  287. }
  288.  
  289. void systemUpdateMotionSensor()
  290. {
  291. }
  292.  
  293. int systemGetSensorX()
  294. {
  295.   return 0;
  296. }
  297.  
  298. int systemGetSensorY()
  299. {
  300.   return 0;
  301. }
  302.  
  303. void systemGbPrint(u8 * _puiData,
  304.                    int  _iPages,
  305.                    int  _iFeed,
  306.                    int  _iPalette,
  307.                    int  _iContrast)
  308. {
  309. }
  310.  
  311. void systemScreenMessage(const char * _csMsg)
  312. {
  313. }
  314.  
  315. bool systemCanChangeSoundQuality()
  316. {
  317.   return true;
  318. }
  319.  
  320. bool systemPauseOnFrame()
  321. {
  322.   return false;
  323. }
  324.  
  325. void systemGbBorderOn()
  326. {
  327. }
  328.  
  329. void debuggerMain()
  330. {
  331. }
  332.  
  333. void debuggerSignal(int, int)
  334. {
  335. }
  336.  
  337. void debuggerOutput(char *, u32)
  338. {
  339. }
  340.  
  341. void (*dbgMain)() = debuggerMain;
  342. void (*dbgSignal)(int, int) = debuggerSignal;
  343. void (*dbgOutput)(char *, u32) = debuggerOutput;
  344.