Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.  * OpenTyrian: A modern cross-platform port of Tyrian
  3.  * Copyright (C) 2007-2009  The OpenTyrian Development Team
  4.  *
  5.  * This program is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU General Public License
  7.  * as published by the Free Software Foundation; either version 2
  8.  * of the License, or (at your option) 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
  17.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  18.  */
  19. #include "nortsong.h"
  20.  
  21. #include "file.h"
  22. #include "joystick.h"
  23. #include "keyboard.h"
  24. #include "loudness.h"
  25. #include "musmast.h"
  26. #include "opentyr.h"
  27. #include "params.h"
  28. #include "sndmast.h"
  29. #include "vga256d.h"
  30.  
  31. #include "SDL.h"
  32.  
  33. Uint32 target, target2;
  34.  
  35. JE_boolean notYetLoadedSound = true;
  36.  
  37. JE_word frameCount, frameCount2, frameCountMax;
  38.  
  39. JE_byte *digiFx[SAMPLE_COUNT] = { NULL }; /* [1..soundnum + 9] */
  40. JE_word fxSize[SAMPLE_COUNT]; /* [1..soundnum + 9] */
  41.  
  42. JE_word tyrMusicVolume, fxVolume;
  43. JE_word fxPlayVol;
  44. JE_word tempVolume;
  45.  
  46. JE_word speed; /* JE: holds timer speed for 70Hz */
  47.  
  48. float jasondelay = 1000.0f / (1193180.0f / 0x4300);
  49.  
  50. void setdelay( JE_byte delay )
  51. {
  52.         target = (delay * 16) + SDL_GetTicks();
  53. }
  54.  
  55. void setjasondelay( int delay )
  56. {
  57.         target = SDL_GetTicks() + delay * jasondelay;
  58. }
  59.  
  60. void setjasondelay2( int delay )
  61. {
  62.         target2 = SDL_GetTicks() + delay * jasondelay;
  63. }
  64.  
  65. int delaycount( void )
  66. {
  67.         return (SDL_GetTicks() < target ? target - SDL_GetTicks() : 0);
  68. }
  69.  
  70. int delaycount2( void )
  71. {
  72.         return (SDL_GetTicks() < target2 ? target2 - SDL_GetTicks() : 0);
  73. }
  74.  
  75. void wait_delay( void )
  76. {
  77.         Sint32 delay = target - SDL_GetTicks();
  78.         if (delay > 0)
  79.                 uSDL_Delay(delay);
  80. }
  81.  
  82. void service_wait_delay( void )
  83. {
  84.         while (SDL_GetTicks() < target)
  85.         {
  86.                 uSDL_Delay(SDL_GetTicks() - target > SDL_POLL_INTERVAL ? SDL_POLL_INTERVAL : SDL_GetTicks() - target);
  87.                 service_SDL_events(false);
  88.         }
  89. }
  90.  
  91. void wait_delayorinput( JE_boolean keyboard, JE_boolean mouse, JE_boolean joystick )
  92. {
  93.         service_SDL_events(true);
  94.         while (SDL_GetTicks() < target && !((keyboard && keydown) || (mouse && mousedown) || (joystick && joydown)))
  95.         {
  96.                 uSDL_Delay(SDL_GetTicks() - target > SDL_POLL_INTERVAL ? SDL_POLL_INTERVAL : SDL_GetTicks() - target);
  97.                 push_joysticks_as_keyboard();
  98.                 service_SDL_events(false);
  99.         }
  100. }
  101.  
  102. void JE_loadSndFile( const char *effects_sndfile, const char *voices_sndfile )
  103. {
  104.         JE_byte y, z;
  105.         JE_word x;
  106.         JE_longint templ;
  107.         JE_longint sndPos[2][SAMPLE_COUNT + 1];
  108.         JE_word sndNum;
  109.  
  110.         FILE *fi;
  111.        
  112.         /* SYN: Loading offsets into TYRIAN.SND */
  113.         fi = dir_fopen_die(data_dir(), effects_sndfile, "rb");
  114.         efread(&sndNum, sizeof(sndNum), 1, fi);
  115.  
  116.         for (x = 0; x < sndNum; x++)
  117.         {
  118.                 efread(&sndPos[0][x], sizeof(sndPos[0][x]), 1, fi);
  119.         }
  120.         fseek(fi, 0, SEEK_END);
  121.         sndPos[0][sndNum] = ftell(fi); /* Store file size */
  122.  
  123.         for (z = 0; z < sndNum; z++)
  124.         {
  125.                 fseek(fi, sndPos[0][z], SEEK_SET);
  126.                 fxSize[z] = (sndPos[0][z+1] - sndPos[0][z]); /* Store sample sizes */
  127.                 free(digiFx[z]);
  128.                 digiFx[z] = malloc(fxSize[z]);
  129.                 efread(digiFx[z], 1, fxSize[z], fi); /* JE: Load sample to buffer */
  130.         }
  131.  
  132.         fclose(fi);
  133.  
  134.         /* SYN: Loading offsets into VOICES.SND */
  135.         fi = dir_fopen_die(data_dir(), voices_sndfile, "rb");
  136.        
  137.         efread(&sndNum, sizeof(sndNum), 1, fi);
  138.  
  139.         for (x = 0; x < sndNum; x++)
  140.         {
  141.                 efread(&sndPos[1][x], sizeof(sndPos[1][x]), 1, fi);
  142.         }
  143.         fseek(fi, 0, SEEK_END);
  144.         sndPos[1][sndNum] = ftell(fi); /* Store file size */
  145.  
  146.         z = SAMPLE_COUNT - 9;
  147.  
  148.         for (y = 0; y < sndNum; y++)
  149.         {
  150.                 fseek(fi, sndPos[1][y], SEEK_SET);
  151.  
  152.                 templ = (sndPos[1][y+1] - sndPos[1][y]) - 100; /* SYN: I'm not entirely sure what's going on here. */
  153.                 if (templ < 1) templ = 1;
  154.                 fxSize[z + y] = templ; /* Store sample sizes */
  155.                 digiFx[z + y] = malloc(fxSize[z + y]);
  156.                 efread(digiFx[z + y], 1, fxSize[z + y], fi); /* JE: Load sample to buffer */
  157.         }
  158.  
  159.         fclose(fi);
  160.  
  161.         notYetLoadedSound = false;
  162.  
  163. }
  164.  
  165. void JE_playSampleNum( JE_byte samplenum )
  166. {
  167.         JE_multiSamplePlay(digiFx[samplenum-1], fxSize[samplenum-1], 0, fxPlayVol);
  168. }
  169.  
  170. void JE_calcFXVol( void ) // TODO: not sure *exactly* what this does
  171. {
  172.         fxPlayVol = (fxVolume - 1) >> 5;
  173. }
  174.  
  175. void JE_setTimerInt( void )
  176. {
  177.         jasondelay = 1000.0f / (1193180.0f / speed);
  178. }
  179.  
  180. void JE_resetTimerInt( void )
  181. {
  182.         jasondelay = 1000.0f / (1193180.0f / 0x4300);
  183. }
  184.  
  185. void JE_changeVolume( JE_word *music, int music_delta, JE_word *sample, int sample_delta )
  186. {
  187.         int music_temp = *music + music_delta,
  188.             sample_temp = *sample + sample_delta;
  189.        
  190.         if (music_delta)
  191.         {
  192.                 if (music_temp > 255)
  193.                 {
  194.                         music_temp = 255;
  195.                         JE_playSampleNum(S_CLINK);
  196.                 }
  197.                 else if (music_temp < 0)
  198.                 {
  199.                         music_temp = 0;
  200.                         JE_playSampleNum(S_CLINK);
  201.                 }
  202.         }
  203.        
  204.         if (sample_delta)
  205.         {
  206.                 if (sample_temp > 255)
  207.                 {
  208.                         sample_temp = 255;
  209.                         JE_playSampleNum(S_CLINK);
  210.                 }
  211.                 else if (sample_temp < 0)
  212.                 {
  213.                         sample_temp = 0;
  214.                         JE_playSampleNum(S_CLINK);
  215.                 }
  216.         }
  217.        
  218.         *music = music_temp;
  219.         *sample = sample_temp;
  220.        
  221.         JE_calcFXVol();
  222.        
  223.         set_volume(*music, *sample);
  224. }
  225.  
  226.