Advertisement
Caiwan

Untitled

Mar 30th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.26 KB | None | 0 0
  1. #define WIN32_LEAN_AND_MEAN
  2. #define WIN32_EXTRA_LEAN
  3. #include <windows.h>
  4. #include <windowsx.h>
  5.  
  6. //#include <mmsystem.h>
  7. //#include <mmreg.h>
  8.  
  9. #include "../fmod/fmod.hpp"
  10. #include "../rocket/sync.h"
  11.  
  12. #include <GL/gl.h>
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <math.h>
  16. #include "../intro.h"
  17. #include "../4klang.h"
  18. #include "../intro.h"
  19. #include "../config.h"
  20.  
  21. //#define USE_SOUND_THREAD
  22.  
  23. //==============================================================================================
  24.  
  25. typedef struct
  26. {
  27.     //---------------
  28.     HINSTANCE   hInstance;
  29.     HDC         hDC;
  30.     HGLRC       hRC;
  31.     HWND        hWnd;
  32.     //---------------
  33.     int         full;
  34.     //---------------
  35.     char        wndclass[4];    // window class and title :)
  36.     //---------------
  37. }WININFO;
  38.  
  39. static PIXELFORMATDESCRIPTOR pfd =
  40.     {
  41.     sizeof(PIXELFORMATDESCRIPTOR),
  42.     1,
  43.     PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,
  44.     PFD_TYPE_RGBA,
  45.     32,
  46.     0, 0, 0, 0, 0, 0, 8, 0,
  47.     0, 0, 0, 0, 0,  // accum
  48.     32,             // zbuffer
  49.     0,              // stencil!
  50.     0,              // aux
  51.     PFD_MAIN_PLANE,
  52.     0, 0, 0, 0
  53.     };
  54.  
  55. static WININFO wininfo = {  0,0,0,0,0,
  56.                             {'i','q','_',0}
  57.                             };
  58. //==============================================================================================
  59. SAMPLE_TYPE lpSoundBuffer[MAX_SAMPLES*2];  
  60. //HWAVEOUT  hWaveOut;
  61. //
  62. //#pragma data_seg(".wavefmt")
  63. //WAVEFORMATEX WaveFMT =
  64. //{
  65. //#ifdef FLOAT_32BIT   
  66. //  WAVE_FORMAT_IEEE_FLOAT,
  67. //#else
  68. //  WAVE_FORMAT_PCM,
  69. //#endif       
  70. //    2, // channels
  71. //    SAMPLE_RATE, // samples per sec
  72. //    SAMPLE_RATE*sizeof(SAMPLE_TYPE)*2, // bytes per sec
  73. //    sizeof(SAMPLE_TYPE)*2, // block alignment;
  74. //    sizeof(SAMPLE_TYPE)*8, // bits per sample
  75. //    0 // extension not needed
  76. //};
  77. //
  78. //#pragma data_seg(".wavehdr")
  79. //WAVEHDR WaveHDR =
  80. //{
  81. //  (LPSTR)lpSoundBuffer,
  82. //  MAX_SAMPLES*sizeof(SAMPLE_TYPE)*2,          // MAX_SAMPLES*sizeof(float)*2(stereo)
  83. //  0,
  84. //  0,
  85. //  0,
  86. //  0,
  87. //  0,
  88. //  0
  89. //};
  90. //
  91. //MMTIME MMTime =
  92. //{
  93. //  TIME_SAMPLES,
  94. //  0
  95. //};
  96. //==============================================================================================
  97.  
  98. static LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  99. {
  100.     // salvapantallas
  101.     if( uMsg==WM_SYSCOMMAND && (wParam==SC_SCREENSAVE || wParam==SC_MONITORPOWER) )
  102.         return( 0 );
  103.  
  104.     // boton x o pulsacion de escape
  105.     if( uMsg==WM_CLOSE || uMsg==WM_DESTROY || (uMsg==WM_KEYDOWN && wParam==VK_ESCAPE) )
  106.     {
  107.         PostQuitMessage(0);
  108.         return( 0 );
  109.     }
  110.  
  111.     if( uMsg==WM_SIZE )
  112.     {
  113.         glViewport( 0, 0, lParam&65535, lParam>>16 );
  114.     }
  115.  
  116.     if( uMsg==WM_CHAR || uMsg==WM_KEYDOWN)
  117.     {
  118.         if( wParam==VK_ESCAPE )
  119.         {
  120.             PostQuitMessage(0);
  121.             return( 0 );
  122.         }
  123.     }
  124.  
  125.     return( DefWindowProc(hWnd,uMsg,wParam,lParam) );
  126. }
  127.  
  128.  
  129. static void window_end( WININFO *info )
  130. {
  131.     if( info->hRC )
  132.     {
  133.         wglMakeCurrent( 0, 0 );
  134.         wglDeleteContext( info->hRC );
  135.     }
  136.  
  137.     if( info->hDC  ) ReleaseDC( info->hWnd, info->hDC );
  138.     if( info->hWnd ) DestroyWindow( info->hWnd );
  139.  
  140.     UnregisterClass( info->wndclass, info->hInstance );
  141.  
  142.     if( info->full )
  143.     {
  144.         ChangeDisplaySettings( 0, 0 );
  145.         while( ShowCursor( 1 )<0 ); // show cursor
  146.     }
  147. }
  148.  
  149. static int window_init( WININFO *info )
  150. {
  151.     unsigned int    PixelFormat;
  152.     DWORD           dwExStyle, dwStyle;
  153.     DEVMODE         dmScreenSettings;
  154.     RECT            rec;
  155.  
  156.     WNDCLASS        wc;
  157.  
  158.     ZeroMemory( &wc, sizeof(WNDCLASS) );
  159.     wc.style         = CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
  160.     wc.lpfnWndProc   = WndProc;
  161.     wc.hInstance     = info->hInstance;
  162.     wc.lpszClassName = info->wndclass;
  163.     wc.hbrBackground =(HBRUSH)CreateSolidBrush(0x00102030);
  164.    
  165.     if( !RegisterClass(&wc) )
  166.         return( 0 );
  167.  
  168.     if( info->full )
  169.     {
  170.         dmScreenSettings.dmSize       = sizeof(DEVMODE);
  171.         dmScreenSettings.dmFields     = DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
  172.         dmScreenSettings.dmBitsPerPel = 32;
  173.         dmScreenSettings.dmPelsWidth  = XRES;
  174.         dmScreenSettings.dmPelsHeight = YRES;
  175.  
  176.         if( ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
  177.             return( 0 );
  178.  
  179.         dwExStyle = WS_EX_APPWINDOW;
  180.         dwStyle   = WS_VISIBLE | WS_POPUP;
  181.  
  182.         while( ShowCursor( 0 )>=0 );    // hide cursor
  183.     }
  184.     else
  185.     {
  186.         dwExStyle = 0;
  187.         dwStyle   = WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MAXIMIZEBOX | WS_OVERLAPPED;
  188.         dwStyle   = WS_VISIBLE | WS_OVERLAPPEDWINDOW|WS_POPUP;
  189.  
  190.     }
  191.  
  192.     rec.left   = 0;
  193.     rec.top    = 0;
  194.     rec.right  = XRES;
  195.     rec.bottom = YRES;
  196.  
  197.     AdjustWindowRect( &rec, dwStyle, 0 );
  198.     /*
  199.     info->hWnd = CreateWindowEx( dwExStyle, wc.lpszClassName, "kkkk", dwStyle,
  200.                                (GetSystemMetrics(SM_CXSCREEN)-rec.right+rec.left)>>1,
  201.                                (GetSystemMetrics(SM_CYSCREEN)-rec.bottom+rec.top)>>1,
  202.                                rec.right-rec.left, rec.bottom-rec.top, 0, 0, info->hInstance, 0 );
  203.     */
  204.  
  205.     info->hWnd = CreateWindowEx( dwExStyle, wc.lpszClassName, "kkkk", dwStyle,
  206.                                (GetSystemMetrics(SM_CXSCREEN)-rec.right+rec.left)>>1,
  207.                                (GetSystemMetrics(SM_CYSCREEN)-rec.bottom+rec.top)>>1,
  208.                                XRES, YRES, 0, 0, info->hInstance, 0 );
  209.  
  210.     if( !info->hWnd )
  211.         return( 0 );
  212.  
  213.     if( !(info->hDC=GetDC(info->hWnd)) )
  214.         return( 0 );
  215.  
  216.     if( !(PixelFormat=ChoosePixelFormat(info->hDC,&pfd)) )
  217.         return( 0 );
  218.  
  219.     if( !SetPixelFormat(info->hDC,PixelFormat,&pfd) )
  220.         return( 0 );
  221.  
  222.     if( !(info->hRC=wglCreateContext(info->hDC)) )
  223.         return( 0 );
  224.  
  225.     if( !wglMakeCurrent(info->hDC,info->hRC) )
  226.         return( 0 );
  227.  
  228.     return( 1 );
  229. }
  230.  
  231. //==============================================================================================
  232. FMOD_CREATESOUNDEXINFO exinfo;
  233.  
  234. FMOD::System *system = NULL;
  235. FMOD::Sound *sound = NULL;
  236. FMOD::Channel *channel = NULL;
  237.  
  238. unsigned int curr_pos = 0;
  239. unsigned int maxlen = 0;
  240.  
  241. FMOD_RESULT F_CALLBACK pcmreadcallback(FMOD_SOUND *_sound, void *_data, unsigned int _datalen)
  242. {
  243.     if (curr_pos>=MAX_SAMPLES*2) return FMOD_ERR_FILE_EOF;
  244.     memcpy(_data, lpSoundBuffer+curr_pos, _datalen);
  245.     curr_pos += _datalen>>1;
  246.     return FMOD_OK;
  247. }
  248.  
  249. FMOD_TIMEUNIT postype = 0;
  250.  
  251. FMOD_RESULT F_CALLBACK pcmsetposcallback(FMOD_SOUND *_sound, int _subsound, unsigned int _position, FMOD_TIMEUNIT _postype)
  252. {
  253.     curr_pos = _position;
  254.     return FMOD_OK;
  255. }
  256.  
  257. void timer_start(){
  258. #ifdef USE_SOUND_THREAD
  259.         HANDLE soundThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)_4klang_render, lpSoundBuffer, 0, 0);
  260. #else
  261.         _4klang_render(lpSoundBuffer);
  262. #endif
  263.  
  264.         FMOD::System_Create(&system);
  265.         system->init(1, FMOD_INIT_NORMAL, 0);
  266.        
  267.         memset(&exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));
  268.  
  269.         exinfo.cbsize            = sizeof(FMOD_CREATESOUNDEXINFO);              /* required. */
  270.         exinfo.decodebuffersize  = SAMPLE_RATE;                                 /* Chunk size of stream update in samples.  This will be the amount of data passed to the user callback. */
  271.         exinfo.length            = MAX_SAMPLES*2;                               /* Length of PCM data in bytes of whole song (for Sound::getLength) */
  272.         exinfo.numchannels       = 2;                                           /* Number of channels in the sound. */
  273.         exinfo.defaultfrequency  = 44100;                                       /* Default playback rate of sound. */
  274.         exinfo.format            = FMOD_SOUND_FORMAT_PCM16;                     /* Data format of sound. */
  275.         exinfo.pcmreadcallback   = pcmreadcallback;                             /* User callback for reading. */
  276.         exinfo.pcmsetposcallback = pcmsetposcallback;                           /* User callback for seeking. */
  277.  
  278.         system->createStream(NULL, FMOD_OPENUSER, &exinfo, &sound);
  279.        
  280. ///////////// 4KLANG /////////////
  281.         /*waveOutOpen           ( &hWaveOut, WAVE_MAPPER, &WaveFMT, NULL, 0, CALLBACK_NULL );
  282.         waveOutPrepareHeader( hWaveOut, &WaveHDR, sizeof(WaveHDR) );
  283.         waveOutWrite        ( hWaveOut, &WaveHDR, sizeof(WaveHDR) );*/ 
  284.     ///////////// 4KLANG /////////////
  285.    
  286.     sound->getLength(&maxlen, FMOD_TIMEUNIT_PCM);
  287.     system->playSound(FMOD_CHANNEL_FREE, sound, false, &channel);
  288. }
  289.  
  290. long getTime(){
  291.     unsigned int t = 0;
  292.     channel->getPosition(&t, FMOD_TIMEUNIT_PCM);
  293.     return t;
  294. }
  295.  
  296. #ifndef SYNC_PLAYER
  297.     int pauseflag = 0;
  298.     void timer_pause(void *d, int flag)
  299.     {
  300.         pauseflag = flag;
  301.         if (flag){
  302.             channel->setPaused(true);
  303.         } else {
  304.             channel->setPaused(false);
  305.         }
  306.     }
  307.  
  308.     void timer_set_row(void *d, int row)
  309.     {
  310.         unsigned int pos = (unsigned int)(row * (BEAT_LEN_SAMPLE/ROW_PER_BEAT));
  311.         channel->setPosition(pos, FMOD_TIMEUNIT_PCM);
  312.     }
  313.  
  314.     int timer_is_playing(void *d)
  315.     {
  316.         return pauseflag;
  317.     }
  318.    
  319.     struct sync_cb timer_cb = {
  320.         timer_pause,
  321.         timer_set_row,
  322.         timer_is_playing
  323.     };
  324.    
  325. #endif /* !defined(SYNC_PLAYER) */
  326. //==============================================================================================
  327.  
  328. typedef const sync_track sync_track_t;
  329. sync_device *rocket = 0;
  330.  
  331. int WINAPI WinMain( HINSTANCE instance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
  332. {
  333.     MSG         msg;
  334.     int         done=0;
  335.     WININFO     *info = &wininfo;
  336.  
  337.     info->hInstance = GetModuleHandle( 0 );
  338.  
  339.   //if( MessageBox( 0, "fullscreen?", info->wndclass, MB_YESNO|MB_ICONQUESTION)==IDYES ) info->full++;
  340.  
  341.     if( !window_init(info) )
  342.     {
  343.         window_end( info );
  344.         MessageBox( 0, "window_init()!","error",MB_OK|MB_ICONEXCLAMATION );
  345.         return( 0 );
  346.     }
  347.    
  348.     /* <ROCKET SPECIFIC> */
  349.     rocket = sync_create_device("script/sync");
  350.     if (!rocket) return 0;
  351.  
  352. #ifndef SYNC_PLAYER
  353.     sync_set_callbacks(rocket, &timer_cb, NULL);
  354.     if (sync_connect(rocket, "localhost", SYNC_DEFAULT_PORT))
  355.     //if (sync_connect(rocket, "192.168.1.100", SYNC_DEFAULT_PORT))
  356.     {
  357.         MessageBox( 0, "sync_connect()!","error",MB_OK|MB_ICONEXCLAMATION );
  358.         return( 0 );
  359.     }
  360. #endif
  361.    
  362.     timer_start();
  363.    
  364.     if( !intro_init((void*)rocket) )
  365.     {
  366.         window_end( info );
  367.         MessageBox( 0, "intro_init()!","error",MB_OK|MB_ICONEXCLAMATION );
  368.         return( 0 );
  369.     }
  370.  
  371.     while( !done )
  372.     {
  373.         //waveOutGetPosition(hWaveOut, &MMTime, sizeof(MMTIME));
  374.        
  375.         //long t = (long)((float)(MMTime.u.sample)/SAMPLE_RATE*1000);
  376.         //long t = (long)(MMTime.u.sample);
  377.  
  378.         long s = getTime();
  379.         long t = (long)((double)getTime()/SAMPLE_RATE*1000);
  380.  
  381.         while( PeekMessage(&msg,0,0,0,PM_REMOVE) )
  382.         {
  383.             if( msg.message==WM_QUIT ) done=1;
  384.             TranslateMessage( &msg );
  385.  
  386.             DispatchMessage( &msg );
  387.         }
  388.  
  389. #ifndef SYNC_PLAYER
  390.         /*
  391.         double ts = (double)t/1000.0;
  392.         double rowtime = ROWTIME;
  393.         double row = ts*rowtime;
  394.         */
  395.         double row = ((double)s / (BEAT_LEN_SAMPLE * ROW_PER_BEAT));
  396.         int roww = (int)floor(row);
  397.         if (sync_update(rocket, roww))
  398.             if (sync_connect(rocket, "localhost", SYNC_DEFAULT_PORT))
  399.             {
  400.                 MessageBox( 0, "sync_connect()!","error",MB_OK|MB_ICONEXCLAMATION );
  401.                 return( 0 );
  402.             }
  403. #endif
  404.  
  405.         intro_do( t );
  406.  
  407.         if( s>= (long)maxlen) done = 1;
  408.  
  409.         SwapBuffers( info->hDC );
  410.         Sleep( 1 ); // give other processes some chance to do something
  411.     }
  412.  
  413.     window_end( info );
  414.     ExitProcess(0);
  415.     return( 0 );
  416. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement