Guest User

GNU Rocket SDL+OpenGL example

a guest
Jan 12th, 2011
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.32 KB | None | 0 0
  1. /* Copyright (C) 2007-2008 Erik Faye-Lund and Egbert Teeselink
  2.  * For conditions of distribution and use, see copyright notice in COPYING
  3.  * sdl+opengl examle by rasmus/loonies http://visualizethis.tumblr.com 2011
  4.  */
  5.  
  6. #define WIN32_LEAN_AND_MEAN
  7. #include <windows.h>
  8. #include <memory>
  9. #include <exception>
  10. #include <cstdio>
  11. #include <string>
  12. #include <stdarg.h>
  13. #include <SDL.h>
  14. #include <SDL_opengl.h>
  15. #include "../sync/sync.h"
  16. #include "bass.h"
  17.  
  18. unsigned int width  = 800;
  19. unsigned int height = 600;
  20.  
  21. const float bpm = 150.0f; /* beats per minute */
  22. const int rpb = 8; /* rows per beat */
  23. const double row_rate = (double(bpm) / 60) * rpb;
  24.  
  25. void SDLReportIfError(int err)
  26. {
  27.     if (err != 0)
  28.     {
  29.         char buffer[1024];
  30.         sprintf_s(buffer, 1024, "Error: %s\n", SDL_GetError());
  31.         OutputDebugString(buffer);
  32.     }
  33. }
  34.  
  35.  
  36. SDL_Surface* SDLSetup()
  37. {
  38.  
  39.     SDLReportIfError(SDL_Init(SDL_INIT_EVERYTHING));
  40.     SDL_Surface * screen;
  41.  
  42.     screen = SDL_SetVideoMode(width, height, 32, SDL_OPENGL | SDL_RESIZABLE );
  43.  
  44.     SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  45.     SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
  46.     SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
  47.     SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
  48.     SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
  49.     SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
  50.     SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
  51.     SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
  52.     SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
  53.  
  54.     return screen;
  55. }
  56.  
  57. double bass_get_row(HSTREAM h)
  58. {
  59.     QWORD pos = BASS_ChannelGetPosition(h, BASS_POS_BYTE);
  60.     double time = BASS_ChannelBytes2Seconds(h, pos);
  61.     return time * row_rate;
  62. }
  63.  
  64. #ifndef SYNC_PLAYER
  65.  
  66. void bass_pause(void *d, int flag)
  67. {
  68.     if (flag)
  69.         BASS_ChannelPause((HSTREAM)d);
  70.     else
  71.         BASS_ChannelPlay((HSTREAM)d, false);
  72. }
  73.  
  74. void bass_set_row(void *d, int row)
  75. {
  76.     QWORD pos = BASS_ChannelSeconds2Bytes((HSTREAM)d, row / row_rate);
  77.     BASS_ChannelSetPosition((HSTREAM)d, pos, BASS_POS_BYTE);
  78. }
  79.  
  80. int bass_is_playing(void *d)
  81. {
  82.     return BASS_ChannelIsActive((HSTREAM)d) == BASS_ACTIVE_PLAYING;
  83. }
  84.  
  85. struct sync_cb bass_cb = {
  86.     bass_pause,
  87.     bass_set_row,
  88.     bass_is_playing
  89. };
  90.  
  91. #endif /* !defined(SYNC_PLAYER) */
  92.  
  93. void die(const char *fmt, ...)
  94. {
  95.     char temp[4096];
  96.     va_list va;
  97.     va_start(va, fmt);
  98.     vsnprintf(temp, sizeof(temp), fmt, va);
  99.     va_end(va);
  100.  
  101. #ifdef _CONSOLE
  102.     fprintf(stderr, "*** error: %s\n", temp);
  103. #else
  104.     MessageBox(NULL, temp, NULL, MB_OK | MB_ICONERROR);
  105. #endif
  106.  
  107.     exit(EXIT_FAILURE);
  108. }
  109.  
  110. //int main(int argc, char *argv[])
  111. int WINAPI WinMain(IN HINSTANCE hInstance, IN HINSTANCE hPrevInstance, IN LPSTR lpCmdLine, IN int nShowCmd )
  112. {
  113.     HSTREAM stream;
  114.  
  115.  
  116.     const struct sync_track *clear_r, *clear_g, *clear_b;
  117.     const struct sync_track *cam_rot, *cam_dist;
  118.  
  119.     SDL_Surface * screen = SDLSetup();
  120.  
  121.     /* init BASS */
  122.     if (!BASS_Init(-1, 44100, 0, 0, 0))
  123.         die("failed to init bass");
  124.     stream = BASS_StreamCreateFile(false, "tune.ogg", 0, 0, 0);
  125.     if (!stream)
  126.         die("failed to open tune");
  127.  
  128.     sync_device *rocket = sync_create_device("sync");
  129.     if (!rocket)
  130.         die("out of memory?");
  131.  
  132. #ifndef SYNC_PLAYER
  133.     sync_set_callbacks(rocket, &bass_cb, (void *)stream);
  134.     if (sync_connect(rocket, "localhost", SYNC_DEFAULT_PORT))
  135.         die("failed to connect to host");
  136. #endif
  137.  
  138.     /* get tracks */
  139.     clear_r = sync_get_track(rocket, "clear.r");
  140.     clear_g = sync_get_track(rocket, "clear.g");
  141.     clear_b = sync_get_track(rocket, "clear.b");
  142.     cam_rot = sync_get_track(rocket, "cam.rot"),
  143.     cam_dist = sync_get_track(rocket, "cam.dist");
  144.  
  145.  
  146.     /* let's roll! */
  147.     BASS_Start();
  148.     BASS_ChannelPlay(stream, false);
  149.  
  150.     bool done = false;
  151.     while (!done) {
  152.         double row = bass_get_row(stream);
  153. #ifndef SYNC_PLAYER
  154.         if (sync_update(rocket, (int)floor(row)))
  155.             sync_connect(rocket, "localhost", SYNC_DEFAULT_PORT);
  156. #endif
  157.  
  158.         /* draw */
  159.  
  160.         glClearColor(sync_get_val(clear_r, row), sync_get_val(clear_g, row), sync_get_val(clear_b, row), 1.0f);
  161.         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  162.  
  163.         float rot = sync_get_val(cam_rot, row);
  164.         float dist = sync_get_val(cam_dist, row);
  165.  
  166.         glViewport(0,0,width,height);              
  167.  
  168.         glMatrixMode(GL_PROJECTION);               
  169.         glLoadIdentity();                          
  170.  
  171.         gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
  172.  
  173.         glMatrixMode(GL_MODELVIEW);
  174.         glLoadIdentity();  
  175.  
  176.         gluLookAt(sin(rot) * dist, 0, cos(rot) * dist,
  177.                   0,0,0,
  178.                   0,1,0);
  179.  
  180.         glEnable(GL_DEPTH_TEST);
  181.  
  182.         glBegin(GL_QUADS);
  183.             // Front Face
  184.             glColor3ub(255,0,0);
  185.             glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);  // Bottom Left Of The Texture and Quad
  186.             glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);  // Bottom Right Of The Texture and Quad
  187.             glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);  // Top Right Of The Texture and Quad
  188.             glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);  // Top Left Of The Texture and Quad
  189.             // Back Face
  190.             glColor3ub(0,255,0);
  191.             glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);  // Bottom Right Of The Texture and Quad
  192.             glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);  // Top Right Of The Texture and Quad
  193.             glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);  // Top Left Of The Texture and Quad
  194.             glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);  // Bottom Left Of The Texture and Quad
  195.             // Top Face
  196.             glColor3ub(0,0,255);
  197.             glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);  // Top Left Of The Texture and Quad
  198.             glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);  // Bottom Left Of The Texture and Quad
  199.             glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);  // Bottom Right Of The Texture and Quad
  200.             glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);  // Top Right Of The Texture and Quad
  201.             // Bottom Face
  202.             glColor3ub(255,255,0);
  203.             glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);  // Top Right Of The Texture and Quad
  204.             glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);  // Top Left Of The Texture and Quad
  205.             glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);  // Bottom Left Of The Texture and Quad
  206.             glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);  // Bottom Right Of The Texture and Quad
  207.             // Right face
  208.             glColor3ub(255,0,255);
  209.             glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);  // Bottom Right Of The Texture and Quad
  210.             glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);  // Top Right Of The Texture and Quad
  211.             glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);  // Top Left Of The Texture and Quad
  212.             glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);  // Bottom Left Of The Texture and Quad
  213.             // Left Face
  214.             glColor3ub(255,255,255);
  215.             glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);  // Bottom Left Of The Texture and Quad
  216.             glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);  // Bottom Right Of The Texture and Quad
  217.             glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);  // Top Right Of The Texture and Quad
  218.             glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);  // Top Left Of The Texture and Quad    
  219.         glEnd();
  220.  
  221.         glDisable(GL_DEPTH_TEST);
  222.  
  223.  
  224.         BASS_Update(0); /* decrease the chance of missing vsync */
  225.  
  226.         SDL_GL_SwapBuffers();
  227.         SDL_Event e;
  228.         while (SDL_PollEvent(&e))
  229.         {
  230.             if (e.type == SDL_QUIT)
  231.             {
  232.                 done=true;
  233.             }
  234.         }
  235.     }
  236.  
  237.     BASS_StreamFree(stream);
  238.     BASS_Free();
  239.  
  240.  
  241.     return 0;
  242. }
Advertisement
Add Comment
Please, Sign In to add comment