Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define WIN32_LEAN_AND_MEAN
- #define WIN32_EXTRA_LEAN
- #include <windows.h>
- #include <windowsx.h>
- //#include <mmsystem.h>
- //#include <mmreg.h>
- #include "../fmod/fmod.hpp"
- #include "../rocket/sync.h"
- #include <GL/gl.h>
- #include <string.h>
- #include <stdio.h>
- #include <math.h>
- #include "../intro.h"
- #include "../4klang.h"
- #include "../intro.h"
- #include "../config.h"
- //#define USE_SOUND_THREAD
- //==============================================================================================
- typedef struct
- {
- //---------------
- HINSTANCE hInstance;
- HDC hDC;
- HGLRC hRC;
- HWND hWnd;
- //---------------
- int full;
- //---------------
- char wndclass[4]; // window class and title :)
- //---------------
- }WININFO;
- static PIXELFORMATDESCRIPTOR pfd =
- {
- sizeof(PIXELFORMATDESCRIPTOR),
- 1,
- PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,
- PFD_TYPE_RGBA,
- 32,
- 0, 0, 0, 0, 0, 0, 8, 0,
- 0, 0, 0, 0, 0, // accum
- 32, // zbuffer
- 0, // stencil!
- 0, // aux
- PFD_MAIN_PLANE,
- 0, 0, 0, 0
- };
- static WININFO wininfo = { 0,0,0,0,0,
- {'i','q','_',0}
- };
- //==============================================================================================
- SAMPLE_TYPE lpSoundBuffer[MAX_SAMPLES*2];
- //HWAVEOUT hWaveOut;
- //
- //#pragma data_seg(".wavefmt")
- //WAVEFORMATEX WaveFMT =
- //{
- //#ifdef FLOAT_32BIT
- // WAVE_FORMAT_IEEE_FLOAT,
- //#else
- // WAVE_FORMAT_PCM,
- //#endif
- // 2, // channels
- // SAMPLE_RATE, // samples per sec
- // SAMPLE_RATE*sizeof(SAMPLE_TYPE)*2, // bytes per sec
- // sizeof(SAMPLE_TYPE)*2, // block alignment;
- // sizeof(SAMPLE_TYPE)*8, // bits per sample
- // 0 // extension not needed
- //};
- //
- //#pragma data_seg(".wavehdr")
- //WAVEHDR WaveHDR =
- //{
- // (LPSTR)lpSoundBuffer,
- // MAX_SAMPLES*sizeof(SAMPLE_TYPE)*2, // MAX_SAMPLES*sizeof(float)*2(stereo)
- // 0,
- // 0,
- // 0,
- // 0,
- // 0,
- // 0
- //};
- //
- //MMTIME MMTime =
- //{
- // TIME_SAMPLES,
- // 0
- //};
- //==============================================================================================
- static LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
- {
- // salvapantallas
- if( uMsg==WM_SYSCOMMAND && (wParam==SC_SCREENSAVE || wParam==SC_MONITORPOWER) )
- return( 0 );
- // boton x o pulsacion de escape
- if( uMsg==WM_CLOSE || uMsg==WM_DESTROY || (uMsg==WM_KEYDOWN && wParam==VK_ESCAPE) )
- {
- PostQuitMessage(0);
- return( 0 );
- }
- if( uMsg==WM_SIZE )
- {
- glViewport( 0, 0, lParam&65535, lParam>>16 );
- }
- if( uMsg==WM_CHAR || uMsg==WM_KEYDOWN)
- {
- if( wParam==VK_ESCAPE )
- {
- PostQuitMessage(0);
- return( 0 );
- }
- }
- return( DefWindowProc(hWnd,uMsg,wParam,lParam) );
- }
- static void window_end( WININFO *info )
- {
- if( info->hRC )
- {
- wglMakeCurrent( 0, 0 );
- wglDeleteContext( info->hRC );
- }
- if( info->hDC ) ReleaseDC( info->hWnd, info->hDC );
- if( info->hWnd ) DestroyWindow( info->hWnd );
- UnregisterClass( info->wndclass, info->hInstance );
- if( info->full )
- {
- ChangeDisplaySettings( 0, 0 );
- while( ShowCursor( 1 )<0 ); // show cursor
- }
- }
- static int window_init( WININFO *info )
- {
- unsigned int PixelFormat;
- DWORD dwExStyle, dwStyle;
- DEVMODE dmScreenSettings;
- RECT rec;
- WNDCLASS wc;
- ZeroMemory( &wc, sizeof(WNDCLASS) );
- wc.style = CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
- wc.lpfnWndProc = WndProc;
- wc.hInstance = info->hInstance;
- wc.lpszClassName = info->wndclass;
- wc.hbrBackground =(HBRUSH)CreateSolidBrush(0x00102030);
- if( !RegisterClass(&wc) )
- return( 0 );
- if( info->full )
- {
- dmScreenSettings.dmSize = sizeof(DEVMODE);
- dmScreenSettings.dmFields = DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
- dmScreenSettings.dmBitsPerPel = 32;
- dmScreenSettings.dmPelsWidth = XRES;
- dmScreenSettings.dmPelsHeight = YRES;
- if( ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
- return( 0 );
- dwExStyle = WS_EX_APPWINDOW;
- dwStyle = WS_VISIBLE | WS_POPUP;
- while( ShowCursor( 0 )>=0 ); // hide cursor
- }
- else
- {
- dwExStyle = 0;
- dwStyle = WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MAXIMIZEBOX | WS_OVERLAPPED;
- dwStyle = WS_VISIBLE | WS_OVERLAPPEDWINDOW|WS_POPUP;
- }
- rec.left = 0;
- rec.top = 0;
- rec.right = XRES;
- rec.bottom = YRES;
- AdjustWindowRect( &rec, dwStyle, 0 );
- /*
- info->hWnd = CreateWindowEx( dwExStyle, wc.lpszClassName, "kkkk", dwStyle,
- (GetSystemMetrics(SM_CXSCREEN)-rec.right+rec.left)>>1,
- (GetSystemMetrics(SM_CYSCREEN)-rec.bottom+rec.top)>>1,
- rec.right-rec.left, rec.bottom-rec.top, 0, 0, info->hInstance, 0 );
- */
- info->hWnd = CreateWindowEx( dwExStyle, wc.lpszClassName, "kkkk", dwStyle,
- (GetSystemMetrics(SM_CXSCREEN)-rec.right+rec.left)>>1,
- (GetSystemMetrics(SM_CYSCREEN)-rec.bottom+rec.top)>>1,
- XRES, YRES, 0, 0, info->hInstance, 0 );
- if( !info->hWnd )
- return( 0 );
- if( !(info->hDC=GetDC(info->hWnd)) )
- return( 0 );
- if( !(PixelFormat=ChoosePixelFormat(info->hDC,&pfd)) )
- return( 0 );
- if( !SetPixelFormat(info->hDC,PixelFormat,&pfd) )
- return( 0 );
- if( !(info->hRC=wglCreateContext(info->hDC)) )
- return( 0 );
- if( !wglMakeCurrent(info->hDC,info->hRC) )
- return( 0 );
- return( 1 );
- }
- //==============================================================================================
- FMOD_CREATESOUNDEXINFO exinfo;
- FMOD::System *system = NULL;
- FMOD::Sound *sound = NULL;
- FMOD::Channel *channel = NULL;
- unsigned int curr_pos = 0;
- unsigned int maxlen = 0;
- FMOD_RESULT F_CALLBACK pcmreadcallback(FMOD_SOUND *_sound, void *_data, unsigned int _datalen)
- {
- if (curr_pos>=MAX_SAMPLES*2) return FMOD_ERR_FILE_EOF;
- memcpy(_data, lpSoundBuffer+curr_pos, _datalen);
- curr_pos += _datalen>>1;
- return FMOD_OK;
- }
- FMOD_TIMEUNIT postype = 0;
- FMOD_RESULT F_CALLBACK pcmsetposcallback(FMOD_SOUND *_sound, int _subsound, unsigned int _position, FMOD_TIMEUNIT _postype)
- {
- curr_pos = _position;
- return FMOD_OK;
- }
- void timer_start(){
- #ifdef USE_SOUND_THREAD
- HANDLE soundThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)_4klang_render, lpSoundBuffer, 0, 0);
- #else
- _4klang_render(lpSoundBuffer);
- #endif
- FMOD::System_Create(&system);
- system->init(1, FMOD_INIT_NORMAL, 0);
- memset(&exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));
- exinfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO); /* required. */
- exinfo.decodebuffersize = SAMPLE_RATE; /* Chunk size of stream update in samples. This will be the amount of data passed to the user callback. */
- exinfo.length = MAX_SAMPLES*2; /* Length of PCM data in bytes of whole song (for Sound::getLength) */
- exinfo.numchannels = 2; /* Number of channels in the sound. */
- exinfo.defaultfrequency = 44100; /* Default playback rate of sound. */
- exinfo.format = FMOD_SOUND_FORMAT_PCM16; /* Data format of sound. */
- exinfo.pcmreadcallback = pcmreadcallback; /* User callback for reading. */
- exinfo.pcmsetposcallback = pcmsetposcallback; /* User callback for seeking. */
- system->createStream(NULL, FMOD_OPENUSER, &exinfo, &sound);
- ///////////// 4KLANG /////////////
- /*waveOutOpen ( &hWaveOut, WAVE_MAPPER, &WaveFMT, NULL, 0, CALLBACK_NULL );
- waveOutPrepareHeader( hWaveOut, &WaveHDR, sizeof(WaveHDR) );
- waveOutWrite ( hWaveOut, &WaveHDR, sizeof(WaveHDR) );*/
- ///////////// 4KLANG /////////////
- sound->getLength(&maxlen, FMOD_TIMEUNIT_PCM);
- system->playSound(FMOD_CHANNEL_FREE, sound, false, &channel);
- }
- long getTime(){
- unsigned int t = 0;
- channel->getPosition(&t, FMOD_TIMEUNIT_PCM);
- return t;
- }
- #ifndef SYNC_PLAYER
- int pauseflag = 0;
- void timer_pause(void *d, int flag)
- {
- pauseflag = flag;
- if (flag){
- channel->setPaused(true);
- } else {
- channel->setPaused(false);
- }
- }
- void timer_set_row(void *d, int row)
- {
- unsigned int pos = (unsigned int)(row * (BEAT_LEN_SAMPLE/ROW_PER_BEAT));
- channel->setPosition(pos, FMOD_TIMEUNIT_PCM);
- }
- int timer_is_playing(void *d)
- {
- return pauseflag;
- }
- struct sync_cb timer_cb = {
- timer_pause,
- timer_set_row,
- timer_is_playing
- };
- #endif /* !defined(SYNC_PLAYER) */
- //==============================================================================================
- typedef const sync_track sync_track_t;
- sync_device *rocket = 0;
- int WINAPI WinMain( HINSTANCE instance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
- {
- MSG msg;
- int done=0;
- WININFO *info = &wininfo;
- info->hInstance = GetModuleHandle( 0 );
- //if( MessageBox( 0, "fullscreen?", info->wndclass, MB_YESNO|MB_ICONQUESTION)==IDYES ) info->full++;
- if( !window_init(info) )
- {
- window_end( info );
- MessageBox( 0, "window_init()!","error",MB_OK|MB_ICONEXCLAMATION );
- return( 0 );
- }
- /* <ROCKET SPECIFIC> */
- rocket = sync_create_device("script/sync");
- if (!rocket) return 0;
- #ifndef SYNC_PLAYER
- sync_set_callbacks(rocket, &timer_cb, NULL);
- if (sync_connect(rocket, "localhost", SYNC_DEFAULT_PORT))
- //if (sync_connect(rocket, "192.168.1.100", SYNC_DEFAULT_PORT))
- {
- MessageBox( 0, "sync_connect()!","error",MB_OK|MB_ICONEXCLAMATION );
- return( 0 );
- }
- #endif
- timer_start();
- if( !intro_init((void*)rocket) )
- {
- window_end( info );
- MessageBox( 0, "intro_init()!","error",MB_OK|MB_ICONEXCLAMATION );
- return( 0 );
- }
- while( !done )
- {
- //waveOutGetPosition(hWaveOut, &MMTime, sizeof(MMTIME));
- //long t = (long)((float)(MMTime.u.sample)/SAMPLE_RATE*1000);
- //long t = (long)(MMTime.u.sample);
- long s = getTime();
- long t = (long)((double)getTime()/SAMPLE_RATE*1000);
- while( PeekMessage(&msg,0,0,0,PM_REMOVE) )
- {
- if( msg.message==WM_QUIT ) done=1;
- TranslateMessage( &msg );
- DispatchMessage( &msg );
- }
- #ifndef SYNC_PLAYER
- /*
- double ts = (double)t/1000.0;
- double rowtime = ROWTIME;
- double row = ts*rowtime;
- */
- double row = ((double)s / (BEAT_LEN_SAMPLE * ROW_PER_BEAT));
- int roww = (int)floor(row);
- if (sync_update(rocket, roww))
- if (sync_connect(rocket, "localhost", SYNC_DEFAULT_PORT))
- {
- MessageBox( 0, "sync_connect()!","error",MB_OK|MB_ICONEXCLAMATION );
- return( 0 );
- }
- #endif
- intro_do( t );
- if( s>= (long)maxlen) done = 1;
- SwapBuffers( info->hDC );
- Sleep( 1 ); // give other processes some chance to do something
- }
- window_end( info );
- ExitProcess(0);
- return( 0 );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement