Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Copyright (C) 2007-2008 Erik Faye-Lund and Egbert Teeselink
- * For conditions of distribution and use, see copyright notice in COPYING
- * sdl+opengl examle by rasmus/loonies http://visualizethis.tumblr.com 2011
- */
- #define WIN32_LEAN_AND_MEAN
- #include <windows.h>
- #include <memory>
- #include <exception>
- #include <cstdio>
- #include <string>
- #include <stdarg.h>
- #include <SDL.h>
- #include <SDL_opengl.h>
- #include "../sync/sync.h"
- #include "bass.h"
- unsigned int width = 800;
- unsigned int height = 600;
- const float bpm = 150.0f; /* beats per minute */
- const int rpb = 8; /* rows per beat */
- const double row_rate = (double(bpm) / 60) * rpb;
- void SDLReportIfError(int err)
- {
- if (err != 0)
- {
- char buffer[1024];
- sprintf_s(buffer, 1024, "Error: %s\n", SDL_GetError());
- OutputDebugString(buffer);
- }
- }
- SDL_Surface* SDLSetup()
- {
- SDLReportIfError(SDL_Init(SDL_INIT_EVERYTHING));
- SDL_Surface * screen;
- screen = SDL_SetVideoMode(width, height, 32, SDL_OPENGL | SDL_RESIZABLE );
- SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
- SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
- SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
- SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
- SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
- SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
- SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
- SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
- SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
- return screen;
- }
- double bass_get_row(HSTREAM h)
- {
- QWORD pos = BASS_ChannelGetPosition(h, BASS_POS_BYTE);
- double time = BASS_ChannelBytes2Seconds(h, pos);
- return time * row_rate;
- }
- #ifndef SYNC_PLAYER
- void bass_pause(void *d, int flag)
- {
- if (flag)
- BASS_ChannelPause((HSTREAM)d);
- else
- BASS_ChannelPlay((HSTREAM)d, false);
- }
- void bass_set_row(void *d, int row)
- {
- QWORD pos = BASS_ChannelSeconds2Bytes((HSTREAM)d, row / row_rate);
- BASS_ChannelSetPosition((HSTREAM)d, pos, BASS_POS_BYTE);
- }
- int bass_is_playing(void *d)
- {
- return BASS_ChannelIsActive((HSTREAM)d) == BASS_ACTIVE_PLAYING;
- }
- struct sync_cb bass_cb = {
- bass_pause,
- bass_set_row,
- bass_is_playing
- };
- #endif /* !defined(SYNC_PLAYER) */
- void die(const char *fmt, ...)
- {
- char temp[4096];
- va_list va;
- va_start(va, fmt);
- vsnprintf(temp, sizeof(temp), fmt, va);
- va_end(va);
- #ifdef _CONSOLE
- fprintf(stderr, "*** error: %s\n", temp);
- #else
- MessageBox(NULL, temp, NULL, MB_OK | MB_ICONERROR);
- #endif
- exit(EXIT_FAILURE);
- }
- //int main(int argc, char *argv[])
- int WINAPI WinMain(IN HINSTANCE hInstance, IN HINSTANCE hPrevInstance, IN LPSTR lpCmdLine, IN int nShowCmd )
- {
- HSTREAM stream;
- const struct sync_track *clear_r, *clear_g, *clear_b;
- const struct sync_track *cam_rot, *cam_dist;
- SDL_Surface * screen = SDLSetup();
- /* init BASS */
- if (!BASS_Init(-1, 44100, 0, 0, 0))
- die("failed to init bass");
- stream = BASS_StreamCreateFile(false, "tune.ogg", 0, 0, 0);
- if (!stream)
- die("failed to open tune");
- sync_device *rocket = sync_create_device("sync");
- if (!rocket)
- die("out of memory?");
- #ifndef SYNC_PLAYER
- sync_set_callbacks(rocket, &bass_cb, (void *)stream);
- if (sync_connect(rocket, "localhost", SYNC_DEFAULT_PORT))
- die("failed to connect to host");
- #endif
- /* get tracks */
- clear_r = sync_get_track(rocket, "clear.r");
- clear_g = sync_get_track(rocket, "clear.g");
- clear_b = sync_get_track(rocket, "clear.b");
- cam_rot = sync_get_track(rocket, "cam.rot"),
- cam_dist = sync_get_track(rocket, "cam.dist");
- /* let's roll! */
- BASS_Start();
- BASS_ChannelPlay(stream, false);
- bool done = false;
- while (!done) {
- double row = bass_get_row(stream);
- #ifndef SYNC_PLAYER
- if (sync_update(rocket, (int)floor(row)))
- sync_connect(rocket, "localhost", SYNC_DEFAULT_PORT);
- #endif
- /* draw */
- glClearColor(sync_get_val(clear_r, row), sync_get_val(clear_g, row), sync_get_val(clear_b, row), 1.0f);
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- float rot = sync_get_val(cam_rot, row);
- float dist = sync_get_val(cam_dist, row);
- glViewport(0,0,width,height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(sin(rot) * dist, 0, cos(rot) * dist,
- 0,0,0,
- 0,1,0);
- glEnable(GL_DEPTH_TEST);
- glBegin(GL_QUADS);
- // Front Face
- glColor3ub(255,0,0);
- glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
- glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
- glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
- glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
- // Back Face
- glColor3ub(0,255,0);
- glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
- glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
- glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
- glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
- // Top Face
- glColor3ub(0,0,255);
- glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
- glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
- glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
- glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
- // Bottom Face
- glColor3ub(255,255,0);
- glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
- glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
- glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
- glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
- // Right face
- glColor3ub(255,0,255);
- glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
- glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
- glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
- glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
- // Left Face
- glColor3ub(255,255,255);
- glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
- glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
- glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
- glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
- glEnd();
- glDisable(GL_DEPTH_TEST);
- BASS_Update(0); /* decrease the chance of missing vsync */
- SDL_GL_SwapBuffers();
- SDL_Event e;
- while (SDL_PollEvent(&e))
- {
- if (e.type == SDL_QUIT)
- {
- done=true;
- }
- }
- }
- BASS_StreamFree(stream);
- BASS_Free();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment