From: Thomas Pietrzak Date: Mon, 4 Apr 2011 21:24:41 +0000 (+0000) Subject: Metronome in thread, sdl 1.3 X-Git-Url: https://git.thomaspietrzak.com/?a=commitdiff_plain;h=e098aba421ebbd505ff2486c3e770711160833b9;p=hapticmetronome.git Metronome in thread, sdl 1.3 git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@8 47cf9a05-e0a8-4ed5-9e9b-101a649bc004 --- diff --git a/Haptic Metronome/Haptic Metronome.suo b/Haptic Metronome/Haptic Metronome.suo index 42d219d..6397c4f 100644 Binary files a/Haptic Metronome/Haptic Metronome.suo and b/Haptic Metronome/Haptic Metronome.suo differ diff --git a/include/PaintGame.h b/include/PaintGame.h index c472f95..a4557cd 100644 --- a/include/PaintGame.h +++ b/include/PaintGame.h @@ -12,27 +12,27 @@ void init(); void initPaint(); -void draw(Track &t, char diff); +void draw(Track &t, float currenttime, char diff); void drawCube(float pos, float size, float colorx, float colory, float colorz); void drawLong(float pos, float size, float length, float colorx, float colory, float colorz); -void handleKeyOn(keys k); -void handleKeyOff(keys k); -void handleKeys(bool a, bool b, bool c, bool d, bool e); -void handleStrokeOn(); -void handleStrokeOff(); -bool processEvents(); +void handleKeyOn(keys k, float currenttime); +void handleKeyOff(keys k, float currenttime); +void handleKeys(bool a, bool b, bool c, bool d, bool e, float currenttime); +void handleStrokeOn(float currenttime); +void handleStrokeOff(float currenttime); +bool processEvents(float currenttime); class MyWiimote: public Wiimote { public: MyWiimote():Wiimote(1){} - void handleStrumOn() { handleStrokeOn(); } - void handleStrumOff() { handleStrokeOff(); } - void handleButtonOn(int k) { handleKeyOn(keys(k)); } - void handleButtonOff(int k) { handleKeyOff(keys(k)); } - void handleButtons(bool a, bool b, bool c, bool d, bool e) { handleKeys(a, b, c, d, e); } + void handleStrumOn(float currenttime) { handleStrokeOn(currenttime); } + void handleStrumOff(float currenttime) { handleStrokeOff(currenttime); } + void handleButtonOn(int k, float currenttime) { handleKeyOn(keys(k), currenttime); } + void handleButtonOff(int k, float currenttime) { handleKeyOff(keys(k), currenttime); } + void handleButtons(bool a, bool b, bool c, bool d, bool e, float currenttime) { handleKeys(a, b, c, d, e, currenttime); } }; #endif diff --git a/include/Tools.hpp b/include/Tools.hpp index 923bfed..4054029 100644 --- a/include/Tools.hpp +++ b/include/Tools.hpp @@ -28,6 +28,8 @@ #define MAX(X,Y) (X)>(Y)?(X):(Y) #define MIN(X,Y) (X)<(Y)?(X):(Y) +#define EPSILON 0.00001f + bool fileExists(const String &f); bool dirExists(const String &d); Vector2D rotatePoint(const Vector2D pos, const Vector2D center, const double &angle); diff --git a/include/Track.hpp b/include/Track.hpp index 3847f72..501e361 100644 --- a/include/Track.hpp +++ b/include/Track.hpp @@ -19,6 +19,8 @@ using namespace std; #include "Config.hpp" #include "Tools.hpp" +#include "Pair.hpp" +//#include "Metronome.hpp" class Track { @@ -30,9 +32,11 @@ class Track void addKey(uint32_t time, uint8_t note); void remKey(uint32_t time, uint8_t note); - void addTempo(uint32_t pos, uint32_t t) { _tempo[pos] = t; } - void addTimesignature(uint32_t pos, uint32_t t) { _timesignatures[pos] = t; } - + void addTempo(uint32_t tick, float timestamp, uint32_t t) { _tempo[tick] = Pair(timestamp, t); } + void addTimesignature(uint32_t tick, float timestamp, uint32_t t) { _timesignatures[tick] = Pair(timestamp, t); } + map > *getTempos() { return &_tempo; } + map > *getTimeSignatures() { return &_timesignatures; } + // int getTempo() const { return _currenttempo; } // void setTempo(uint32 t) { _currenttempo = t; } @@ -42,8 +46,9 @@ class Track uint32_t ticksToBeats(uint32_t time) { return 60000000 / time; } void displayTracks(); - void drawFrets(double postime) const; - void drawNotes(double postime, uint8_t diff) const; + void drawFrets(float postime) const; + void drawNotes(float postime, uint8_t difficulty) const; +// void checkMetronome(float postime, Metronome *m) const; uint32_t getNbNotes(uint32_t k) const { return _nbnotes[k]; } uint32_t getTotalLength(uint32_t k) const { return _totallength[k]; } @@ -54,10 +59,12 @@ class Track Config getNote(uint32_t pos, uint8_t difficulty); uint32_t getTempo(uint32_t pos); - uint32_t getTimeSignatures(uint32_t pos); + uint32_t getTimeSignature(uint32_t pos); + uint32_t getTempo(float pos); + uint32_t getTimeSignature(float pos); - list > getBips() const { return _bips; } - void addBip(uint32_t tick, double timestamp) { _bips.push_back(Pair(tick, timestamp)); } + list > * getBips() { return &_bips; } + void addBip(uint32_t tick, float timestamp) { _bips.push_back(Pair(tick, timestamp)); } //void computeBips(); @@ -66,11 +73,11 @@ class Track private: //notes : position ; configs map _notes[NBDIFFICULTIES]; - map _tempo; - map _timesignatures; + map > _tempo; + map > _timesignatures; // private: - list > _bips; + list > _bips; Config _currentconfig[NBDIFFICULTIES]; diff --git a/include/Wiimote.hpp b/include/Wiimote.hpp index 0fdc1cb..4d5ad9c 100644 --- a/include/Wiimote.hpp +++ b/include/Wiimote.hpp @@ -14,18 +14,18 @@ class Wiimote{ public: Wiimote(int nb=1); - void handleEvents(); + void handleEvents(float currenttime); void vibration(int wiimote, int time); - virtual void handleStrumOn() = 0; - virtual void handleStrumOff() = 0; - virtual void handleButtonOn(int k) = 0; - virtual void handleButtonOff(int k) = 0; - virtual void handleButtons(bool a, bool b, bool c, bool d, bool e) = 0; + virtual void handleStrumOn(float currenttime) = 0; + virtual void handleStrumOff(float currenttime) = 0; + virtual void handleButtonOn(int k, float currenttime) = 0; + virtual void handleButtonOff(int k, float currenttime) = 0; + virtual void handleButtons(bool a, bool b, bool c, bool d, bool e, float currenttime) = 0; private: - void handleKey(guitar_hero_3_t* gh3, int key, int button); + void handleKey(guitar_hero_3_t* gh3, int key, int button, float currenttime); wiimote **_wiimotes; int _nb; bool _strumstate, _keystate[5]; diff --git a/src/MIDIReader.cpp b/src/MIDIReader.cpp index 86c2f99..4073676 100644 --- a/src/MIDIReader.cpp +++ b/src/MIDIReader.cpp @@ -134,6 +134,9 @@ void MIDIReader::readTrack(Track &tr, bool addbips) float timestamp = 0; uint32_t currenttempo = 60000000/120; + if (addbips) + tr.addBip(0, 0.0); + // Track example; uint32_t delta = 0; @@ -149,8 +152,7 @@ void MIDIReader::readTrack(Track &tr, bool addbips) if (addbips) { uint32_t lastbiptick = 0; - if (!tr.getBips().empty()) - lastbiptick = tr.getBips().back().getX(); + lastbiptick = tr.getBips()->back().getX(); float temptimestamp = timestamp; //manage 1st bip, taking into account the time elapsed since the last event if (lastbiptick > 0 && ticks >= lastbiptick + 480) @@ -160,10 +162,10 @@ void MIDIReader::readTrack(Track &tr, bool addbips) tr.addBip(lastbiptick, temptimestamp); } //add bips with the current tempo - for (uint32_t i = lastbiptick ; i < ticks ; i += 480 ) + for (uint32_t i = lastbiptick + 480 ; i < ticks ; i += 480 ) { - tr.addBip(i, temptimestamp); temptimestamp += currenttempo / 1000.0f; + tr.addBip(i, temptimestamp); } } @@ -246,11 +248,11 @@ void MIDIReader::readTrack(Track &tr, bool addbips) tempo = ((int(data[2]) << 16) + (int(data[1]) << 8) + int(data[0])); cout << "Tempo at " << ticks << " : x=" << tempo << "=" << 60000000 / tempo << "bpm" << endl; currenttempo = tempo; - tr.addTempo(ticks, tempo); + tr.addTempo(ticks, timestamp, tempo); break; case 0x58: cout << "Time signature at " << ticks<< " : " << int(data[3]) << "|" << int(pow(float(2), int(data[2]))) << " " << int(data[1]) << " ticks " << int(data[0]) << "x" << endl; - tr.addTimesignature(ticks, int(data[3])); + tr.addTimesignature(ticks, timestamp, int(data[3])); break; case 0x59: cout << "Key signature: " << int(data[1]) << " " << int(data[0]) << endl; @@ -342,8 +344,8 @@ void MIDIReader::readTrack(Track &tr, bool addbips) if (addbips) { uint32_t lastbiptick = 0; - if (!tr.getBips().empty()) - lastbiptick = tr.getBips().back().getX(); + if (!tr.getBips()->empty()) + lastbiptick = tr.getBips()->back().getX(); float temptimestamp = timestamp; //manage 1st bip, taking into account the time elapsed since the last event if (lastbiptick > 0) diff --git a/src/PaintGame.cpp b/src/PaintGame.cpp index 4f43850..b11e10d 100644 --- a/src/PaintGame.cpp +++ b/src/PaintGame.cpp @@ -6,15 +6,10 @@ #include #ifdef WIN32 -#include -#include - #include #include #include #else -#include - #include #include #include @@ -31,12 +26,10 @@ int resolution_x, resolution_y; Config fromKeyboard; extern FILE*logfile; - -#ifdef WIN32 - struct { int tv_sec, tv_usec; } start; - DWORD t; -#else - struct timeval start; + +#if SDL_VERSION_ATLEAST(1,3,0) +SDL_Window *window; +SDL_Renderer *renderer; #endif void init() @@ -92,10 +85,32 @@ void init() resolution_y = 768; } } - + +#if 0//SDL_VERSION_ATLEAST(1,3,0) + if ((window = SDL_CreateWindow("Haptic Metronome", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + resolution_x, resolution_y, + SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN)) == NULL) + throw "Unable to create the window"; + + if ((renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED)) == NULL) + throw "unable to create the renderer"; + /* + GLdouble ratio = (GLdouble) window->w / window->h; + + printf("infos : %d %d %d %d\n", window->flags, window->w, window->h, window->pitch); + printf("Video resolution: %dx%dx%d (ratio = %3.2f)\n", window->w, window->h, window->surface->format->BitsPerPixel, ratio);*/ + printf("OpenGL infos\n"); + printf("------------\n"); + printf("Vendor : %s\n", glGetString(GL_VENDOR)); + printf("Renderer : %s\n", glGetString(GL_RENDERER)); + printf("Version : %s\n", glGetString(GL_VERSION)); + printf("Extensions: %s\n", glGetString(GL_EXTENSIONS)); +#else //initializes Opengl - if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) - throw "Couldn't initialise Video SubSystem: %s\n"; +// if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) +// throw "Couldn't initialise Video SubSystem: %s\n"; SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); @@ -108,7 +123,7 @@ void init() SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 8); - int flags = SDL_OPENGL | SDL_OPENGLBLIT | SDL_HWSURFACE;// | SDL_FULLSCREEN; + int flags = SDL_OPENGL | SDL_OPENGL | SDL_HWSURFACE;// | SDL_FULLSCREEN; // int flags = SDL_OPENGL | SDL_OPENGLBLIT | SDL_SWSURFACE | SDL_FULLSCREEN; SDL_Surface * screen; @@ -133,6 +148,7 @@ void init() printf("Renderer : %s\n", glGetString(GL_RENDERER)); printf("Version : %s\n", glGetString(GL_VERSION)); printf("Extensions: %s\n", glGetString(GL_EXTENSIONS)); +#endif glClearColor(1.0f, 1.0f, 1.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -166,40 +182,17 @@ void initPaint() texturefond = new Texture("back.jpg"); texturesteel = new Texture("steel.jpg"); - -#ifdef WIN32 - t = timeGetTime (); - start.tv_sec = t / 1000; - start.tv_usec = (t % 1000) * 1000; -#else - gettimeofday(&start, NULL); -#endif } -void draw(Track &t, char diff) +void draw(Track &t, float currenttime, char diff) { -#ifdef WIN32 - struct { int tv_sec, tv_usec; } now; - DWORD tim; -#else - struct timeval now; -#endif - -#ifdef WIN32 - tim = timeGetTime (); - now.tv_sec = tim / 1000; - now.tv_usec = (tim % 1000) * 1000; -#else - gettimeofday(&now, NULL); -#endif - float postime = (now.tv_sec - start.tv_sec) * 1000.0f + (now.tv_usec - start.tv_usec) / 1000.0; - posz = -SECONDSIZE * postime; + posz = -SECONDSIZE * currenttime; // cout << "draw screen" << endl; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // glColor(1.0, 1.0, 1.0, 0.0); - //Draw the back + //Draw the background glDisable(GL_LIGHTING); glMatrixMode(GL_PROJECTION); glLoadIdentity(); @@ -238,16 +231,16 @@ void draw(Track &t, char diff) texturebois->useTexture(); glBegin(GL_QUADS); glColor3f(1.0f, 1.0f, 1.0f); // Set The Color To Green - glTexCoord2d(0.0f, 0.0f); glVertex3f( NECKWIDTH,0, 0); - glTexCoord2d(0.0f, 20000); glVertex3f( NECKWIDTH,0,-1500000); - glTexCoord2d(1.0f, 20000); glVertex3f(-NECKWIDTH,0,-1500000); - glTexCoord2d(1.0f, 0.0f); glVertex3f(-NECKWIDTH,0, 0); + glTexCoord2d(0, 0); glVertex3f( NECKWIDTH,0, 0); + glTexCoord2d(0, 20000); glVertex3f( NECKWIDTH,0,-1500000); + glTexCoord2d(1, 20000); glVertex3f(-NECKWIDTH,0,-1500000); + glTexCoord2d(1, 0); glVertex3f(-NECKWIDTH,0, 0); glEnd(); glDisable(GL_TEXTURE_2D); glMatrixMode(GL_MODELVIEW); glPushMatrix(); - glTranslatef(0,0,posz); + glTranslatef(0, 0, posz); //draw the strings for (int i = 0 ; i < 5 ; i++) { @@ -292,10 +285,10 @@ void draw(Track &t, char diff) glPopMatrix(); //draw the frets - t.drawFrets(postime); + t.drawFrets(currenttime); // Draw the notes - t.drawNotes(postime, diff); + t.drawNotes(currenttime, diff); glMatrixMode(GL_MODELVIEW); glPushMatrix(); @@ -416,7 +409,7 @@ void drawLong(float pos, float size, float length, float colorx, float colory, f glEnd(); // Done Drawing The Quad } -void handleKeyOn(keys k) +void handleKeyOn(keys k, float currenttime) { fromKeyboard.addKey(k); /* switch(k) @@ -439,74 +432,43 @@ void handleKeyOn(keys k) }*/ } -void handleKeyOff(keys k) +void handleKeyOff(keys k, float currenttime) { // fromKeyboard.remKey(k); if (fromKeyboard.getPosition() == -1) return; - -#ifdef WIN32 - struct { int tv_sec, tv_usec; } now; - DWORD t; -#else - struct timeval now; -#endif - -#ifdef WIN32 - t = timeGetTime (); - now.tv_sec = t / 1000; - now.tv_usec = (t % 1000) * 1000; -#else - gettimeofday(&now, NULL); -#endif - - double postime = (now.tv_sec - start.tv_sec) * 1000 + (now.tv_usec - start.tv_usec) / 1000.0; - fromKeyboard.setEnd(postime); + fromKeyboard.setEnd(currenttime); cout << fromKeyboard << endl; fromKeyboard.log("Play", logfile); - fromKeyboard.setPosition(postime); + fromKeyboard.setPosition(currenttime); fromKeyboard.setEnd(-1); } -void handleKeys(bool a, bool b, bool c, bool d, bool e) +void handleKeys(bool a, bool b, bool c, bool d, bool e, float currenttime) { if (fromKeyboard.isNone()) fromKeyboard.setPosition(-1); fromKeyboard.setKeys(a, b, c, d, e); } -void handleStrokeOn() +void handleStrokeOn(float currenttime) { -#ifdef WIN32 - struct { int tv_sec, tv_usec; } now; - DWORD t; -#else - struct timeval now; -#endif - -#ifdef WIN32 - t = timeGetTime (); - now.tv_sec = t / 1000; - now.tv_usec = (t % 1000) * 1000; -#else - gettimeofday(&now, NULL); -#endif - - double postime = (now.tv_sec - start.tv_sec) * 1000 + (now.tv_usec - start.tv_usec) / 1000.0; - //cout << postime << endl; - if (fromKeyboard.getPosition() != -1) { - fromKeyboard.setEnd(postime); + fromKeyboard.setEnd(currenttime); cout << fromKeyboard << endl; fromKeyboard.log("Play", logfile); } - fromKeyboard.setPosition(postime); + fromKeyboard.setPosition(currenttime); fromKeyboard.setEnd(-1); } -bool processEvents() +void handleStrokeOff(float currenttime) +{ +} + +bool processEvents(float currenttime) { SDL_Event event; // bool quitProgram = false; @@ -521,17 +483,22 @@ bool processEvents() break; case SDL_KEYDOWN: +#if SDL_VERSION_ATLEAST(1,3,0) + keystate = SDL_GetKeyboardState(NULL); + handleKeys(keystate[SDL_SCANCODE_F1], keystate[SDL_SCANCODE_F2], keystate[SDL_SCANCODE_F3], keystate[SDL_SCANCODE_F4], keystate[SDL_SCANCODE_F5], currenttime); +#else keystate = SDL_GetKeyState(NULL); - handleKeys(keystate[SDLK_F1], keystate[SDLK_F2], keystate[SDLK_F3], keystate[SDLK_F4], keystate[SDLK_F5]); + handleKeys(keystate[SDLK_F1], keystate[SDLK_F2], keystate[SDLK_F3], keystate[SDLK_F4], keystate[SDLK_F5], currenttime); +#endif switch(((SDL_KeyboardEvent *)(&event))->keysym.sym) { case SDLK_ESCAPE: return true; case SDLK_UP: - SECONDSIZE +=10; + SECONDSIZE += 0.01f; break; case SDLK_DOWN: - SECONDSIZE -=1; + SECONDSIZE -= 0.01f; break; /* case SDLK_LEFT: posy +=1; @@ -563,7 +530,7 @@ bool processEvents() handleKeyOn(KEY4); break;*/ case SDLK_SPACE: - handleStrokeOn(); + handleStrokeOn(currenttime); break; default: break; @@ -574,28 +541,33 @@ bool processEvents() switch(((SDL_KeyboardEvent *)(&event))->keysym.sym) { case SDLK_F1: - handleKeyOff(KEY0); + handleKeyOff(KEY0, currenttime); break; case SDLK_F2: - handleKeyOff(KEY1); + handleKeyOff(KEY1, currenttime); break; case SDLK_F3: - handleKeyOff(KEY2); + handleKeyOff(KEY2, currenttime); break; case SDLK_F4: - handleKeyOff(KEY3); + handleKeyOff(KEY3, currenttime); break; case SDLK_F5: - handleKeyOff(KEY4); + handleKeyOff(KEY4, currenttime); break; case SDLK_SPACE: - handleStrokeOff(); + handleStrokeOff(currenttime); break; default: break; } +#if SDL_VERSION_ATLEAST(1,3,0) + keystate = SDL_GetKeyboardState(NULL); + handleKeys(keystate[SDL_SCANCODE_F1], keystate[SDL_SCANCODE_F2], keystate[SDL_SCANCODE_F3], keystate[SDL_SCANCODE_F4], keystate[SDL_SCANCODE_F5], currenttime); +#else keystate = SDL_GetKeyState(NULL); - handleKeys(keystate[SDLK_F1], keystate[SDLK_F2], keystate[SDLK_F3], keystate[SDLK_F4], keystate[SDLK_F5]); + handleKeys(keystate[SDLK_F1], keystate[SDLK_F2], keystate[SDLK_F3], keystate[SDLK_F4], keystate[SDLK_F5], currenttime); +#endif break; default: // lasteventtype = OTHER; diff --git a/src/Texture.cpp b/src/Texture.cpp index 00d8cbe..9da81f5 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -78,21 +78,42 @@ Texture::Texture(const String &filename, const bool &rep) size[C_X] = surface->w; size[C_Y] = surface->h; SDL_Surface *temp = NULL; +#if SDL_VERSION_ATLEAST(1,3,0) +/* SDL_PixelFormat format = { + SDL_PIXELFORMAT_RGBA8888, + NULL, + 32, + 4, + 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000, + 0, 0, 0, 0, + 0, 8, 16, 24, + surface->format->refcount, + surface->format->next};*/ +#else SDL_PixelFormat format = {NULL, 32, 4, 0, 0, 0, 0, 0, 8, 16, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000, 0, 255}; +#endif switch(surface->format->BitsPerPixel) { case 24: //convert to 32bits +#if SDL_VERSION_ATLEAST(1,3,0) + temp = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ABGR8888, SDL_SWSURFACE); +#else temp = SDL_ConvertSurface(surface, &format, SDL_SWSURFACE); +#endif SDL_FreeSurface(surface); surface = temp; gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, surface->w, surface->h, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels); - break; + break; case 32: //convert if it's ABGR if (surface->format->Rshift > surface->format->Bshift) { +#if SDL_VERSION_ATLEAST(1,3,0) + temp = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ABGR8888, SDL_SWSURFACE); +#else temp = SDL_ConvertSurface(surface, &format, SDL_SWSURFACE); +#endif SDL_FreeSurface(surface); surface = temp; } diff --git a/src/Tools.cpp b/src/Tools.cpp index 4b674a4..a09e915 100644 --- a/src/Tools.cpp +++ b/src/Tools.cpp @@ -20,6 +20,12 @@ */ #include "Tools.hpp" +#ifdef WIN32 +#include +#else +#include +#endif + #include #include #include diff --git a/src/Track.cpp b/src/Track.cpp index 77ce890..d3e8117 100644 --- a/src/Track.cpp +++ b/src/Track.cpp @@ -1,10 +1,14 @@ #include "Track.hpp" -#include "Mixer.hpp" #include "Texture.hpp" #include +#include +#include +#include +using namespace std; + extern Texture* texturesteel; extern FILE *logfile; extern float SECONDSIZE; @@ -162,13 +166,13 @@ void Track::remKey(uint32_t time, uint8_t note) _currentconfig[difficulty].setTicks(time - _currentconfig[difficulty].getTime()); //compute the note duration and position uint32_t currenttempo = 0, lasttick = 0; - map::iterator ti = _tempo.begin(); + map >::iterator ti = _tempo.begin(); uint32_t tickpos = _currentconfig[difficulty].getTime(); while (ti != _tempo.end() && (*ti).first <= tickpos) { _currentconfig[difficulty].setPosition(_currentconfig[difficulty].getPosition() + ((*ti).first - lasttick) * (currenttempo / 480000.0)); lasttick = (*ti).first; - currenttempo = (*ti).second; + currenttempo = (*ti).second.getY(); ti++; } if (tickpos > lasttick) @@ -180,7 +184,7 @@ void Track::remKey(uint32_t time, uint8_t note) { _currentconfig[difficulty].setEnd(_currentconfig[difficulty].getPosition() + ((*ti).first - tickpos) * (currenttempo / 480000.0)); tickpos = (*ti).first; - currenttempo = (*ti).second; + currenttempo = (*ti).second.getY(); ti++; } else @@ -247,15 +251,33 @@ uint32_t Track::getTempo(uint32_t pos) { if (_tempo.count(pos) == 0) throw 0; - return _tempo[pos]; + return _tempo[pos].getY(); } -uint32_t Track::getTimeSignatures(uint32_t pos) +uint32_t Track::getTimeSignature(uint32_t pos) { if (_timesignatures.count(pos) == 0) throw 0; - return _timesignatures[pos]; + return _timesignatures[pos].getY(); +} + + +uint32_t Track::getTempo(float pos) +{ + for( map >::iterator ii=_tempo.begin(); ii!=_tempo.end(); ++ii) + if ((*ii).second.getX() - pos < EPSILON) + return (*ii).second.getY(); + return 0; } + +uint32_t Track::getTimeSignature(float pos) +{ + for( map >::iterator ii=_timesignatures.begin(); ii!=_timesignatures.end(); ++ii) + if ((*ii).second.getX() - pos < EPSILON) + return (*ii).second.getY(); + return 0; +} + /* void Track::computeBips() { @@ -292,24 +314,25 @@ void Track::displayTracks() void Track::debugTempo() { cout << "Tempo changes: " << endl; - for( map::iterator ii=_tempo.begin(); ii!=_tempo.end(); ++ii) - cout << (*ii).first << ": " << (*ii).second << " (" << ticksToBeats((*ii).second) << ")" << endl; + for( map >::iterator ii=_tempo.begin(); ii!=_tempo.end(); ++ii) + cout << (*ii).first << ": " << (*ii).second.getX() << " (" << (*ii).second.getY() << ")" << endl; + //ticksToBeats((*ii).second) << ")" << endl; } -void Track::drawFrets(double postime) const +void Track::drawFrets(float postime) const { - static list >::const_iterator startingbips = _bips.begin(); + static list >::const_iterator startingbips = _bips.begin(); // on avance tant que les bips sont dépassées while (startingbips != _bips.end() && (*startingbips).getY() < postime) { - Mixer::getInstance()->playSound("bip"); +// Mixer::getInstance()->playSound("bip"); if (logfile) fprintf(logfile, "Bip;%f;%f;%f\n", (*startingbips).getY(), postime, postime - (*startingbips).getY()); startingbips++; } - list >::const_iterator b = startingbips; + list >::const_iterator b = startingbips; glMatrixMode(GL_MODELVIEW); //on dessine les frettes jusqu'à 10s @@ -335,7 +358,7 @@ void Track::drawFrets(double postime) const } } -void Track::drawNotes(double postime, uint8_t diff) const +void Track::drawNotes(float postime, uint8_t diff) const { static map::const_iterator startingn = _notes[diff].begin(); static double startingpos = 0.0; @@ -368,3 +391,32 @@ void Track::drawNotes(double postime, uint8_t diff) const n++; } } +/* +void Track::checkMetronome(float postime, Metronome *m) const +{ + static map >::const_iterator cTempo = _tempo.begin(); + static map >::const_iterator cTimesignature = _timesignatures.begin(); + static double startingpos = 0.0; + + // on avance tant que les tempos sont dépassées + while (cTempo != _tempo.end() && (*cTempo).second.getX() < postime) + { + //tempo change + stringstream s; + s << "Tempo change at " << postime << " : tick=" << (*cTempo).first << " at " << (*cTempo).second.getX() << " for " << (60000000/(*cTempo).second.getY()) << " bpm " << endl; + OutputDebugString(s.str().c_str()); + m->setTempo((*cTempo).second.getY()); + cTempo++; + } + + // on avance tant que les tempos sont dépassées + while (cTimesignature != _timesignatures.end() && (*cTimesignature).second.getX() < postime) + { + //tempo change + stringstream s; + s << "Time signature change at " << postime << " : tick=" << (*cTimesignature).first << " at " << (*cTimesignature).second.getX() << " for " << (*cTimesignature).second.getY() << " beats " << endl; + m->setNbBeats((*cTimesignature).second.getY()); + OutputDebugString(s.str().c_str()); + cTimesignature++; + } +}*/ \ No newline at end of file diff --git a/src/Wiimote.cpp b/src/Wiimote.cpp index 9698669..4e8132f 100644 --- a/src/Wiimote.cpp +++ b/src/Wiimote.cpp @@ -38,23 +38,23 @@ Wiimote::Wiimote(int nb) } } -void Wiimote::handleKey(guitar_hero_3_t* gh3, int key, int button) +void Wiimote::handleKey(guitar_hero_3_t* gh3, int key, int button, float currenttime) { if (_keystate[key]) { if (!IS_PRESSED(gh3, button)) - handleButtonOff(key); + handleButtonOff(key, currenttime); _keystate[key] = false; } else { if (IS_PRESSED(gh3, button)) - handleButtonOn(key); + handleButtonOn(key, currenttime); _keystate[key] = true; } } -void Wiimote::handleEvents() +void Wiimote::handleEvents(float currenttime) { if (!wiiuse_poll(_wiimotes, _nb)) return; @@ -72,7 +72,7 @@ void Wiimote::handleEvents() { if (!IS_PRESSED(gh3, GUITAR_HERO_3_BUTTON_STRUM_UP) && !IS_PRESSED(gh3, GUITAR_HERO_3_BUTTON_STRUM_DOWN)) { - handleStrumOff(); + handleStrumOff(currenttime); _strumstate = false; } } @@ -80,7 +80,7 @@ void Wiimote::handleEvents() { if (IS_PRESSED(gh3, GUITAR_HERO_3_BUTTON_STRUM_UP) || IS_PRESSED(gh3, GUITAR_HERO_3_BUTTON_STRUM_DOWN)) { - handleStrumOn(); + handleStrumOn(currenttime); _strumstate = true; } } @@ -89,7 +89,8 @@ void Wiimote::handleEvents() IS_PRESSED(gh3, GUITAR_HERO_3_BUTTON_RED), IS_PRESSED(gh3, GUITAR_HERO_3_BUTTON_YELLOW), IS_PRESSED(gh3, GUITAR_HERO_3_BUTTON_BLUE), - IS_PRESSED(gh3, GUITAR_HERO_3_BUTTON_ORANGE)); + IS_PRESSED(gh3, GUITAR_HERO_3_BUTTON_ORANGE), + currenttime); /* handleKey(gh3, 0, GUITAR_HERO_3_BUTTON_GREEN); handleKey(gh3, 1, GUITAR_HERO_3_BUTTON_RED); handleKey(gh3, 2, GUITAR_HERO_3_BUTTON_YELLOW); diff --git a/src/main.cpp b/src/main.cpp index f06c20d..8218b04 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,11 +4,16 @@ #include "Track.hpp" #include "Mixer.hpp" #include "PaintGame.h" +#include "Metronome.hpp" +#include "MetronomeAudio.hpp" #ifdef WIN32 +#include #include - +#include #else +#include + #include #endif @@ -90,10 +95,9 @@ int main(int argc, char *argv[]) cout << "Loading sounds" << endl; - Mixer::getInstance()->addSound("guitar", songname + "/guitar.ogg"); - bool rhythm = Mixer::getInstance()->addSound("rhythm", songname + "/rhythm.ogg"); +// Mixer::getInstance()->addSound("guitar", songname + "/guitar.ogg"); +// bool rhythm = Mixer::getInstance()->addSound("rhythm", songname + "/rhythm.ogg"); - Mixer::getInstance()->addSound("bip","bip.ogg"); Mixer::getInstance()->setMusicVolume(30); Mixer::getInstance()->setSoundVolume(30); @@ -101,13 +105,29 @@ int main(int argc, char *argv[]) cout << "Init paint" << endl; initPaint(); + Metronome *m = new MetronomeAudio(&tr); + + +#ifdef WIN32 + struct { int tv_sec, tv_usec; } start; + DWORD t = timeGetTime (); + start.tv_sec = t / 1000; + start.tv_usec = (t % 1000) * 1000; +#else + struct timeval start; + gettimeofday(&start, NULL); +#endif + cout << "LET'S ROCK!" << endl; + //m->setActive(true); - Mixer::getInstance()->playMusic(songname + "/song.ogg"); - Mixer::getInstance()->playSound("guitar"); + Mixer::getInstance()->playMusic(songname + "/tout.ogg"); + m->run(); +/* Mixer::getInstance()->playSound("guitar"); if (rhythm) Mixer::getInstance()->playSound("rhythm"); -/* + */ + /* Sound music; cout << "Load sounds" << endl; music.addSound(songname + "/song.ogg"); @@ -128,18 +148,34 @@ int main(int argc, char *argv[]) while(!quitProgram) { + //compute current time in the loop +#ifdef WIN32 + struct { int tv_sec, tv_usec; } now; + DWORD tim = timeGetTime (); + now.tv_sec = tim / 1000; + now.tv_usec = (tim % 1000) * 1000; +#else + struct timeval now; + gettimeofday(&now, NULL); +#endif + float currenttime = (now.tv_sec - start.tv_sec) * 1000.0f + (now.tv_usec - start.tv_usec) / 1000.0f; + + // cout << "draw" << endl; + draw(tr, currenttime, NORMAL); // cout << "events" << endl; - quitProgram = processEvents(); + quitProgram = processEvents(currenttime); if (iswiimoteconnected) - mw->handleEvents(); -// cout << "draw" << endl; - draw(tr, NORMAL); + mw->handleEvents(currenttime); // cout << "pause" << endl; // SDL_Delay(10); + // check metronome events +// tr.checkMetronome(currenttime, m); } - if (logfile) - fclose(logfile); + delete m; + + if (logfile) + fclose(logfile); return 1; }