From: Thomas Pietrzak Date: Thu, 5 May 2011 19:28:20 +0000 (+0000) Subject: Minor stuff X-Git-Url: https://git.thomaspietrzak.com/?a=commitdiff_plain;h=b5900329cfec9fcd28b8a8aa5618736ff1114d39;p=hapticmetronome.git Minor stuff git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@10 47cf9a05-e0a8-4ed5-9e9b-101a649bc004 --- diff --git a/Haptic Metronome/Haptic Metronome.suo b/Haptic Metronome/Haptic Metronome.suo index 51f3482..5f9e0c5 100644 Binary files a/Haptic Metronome/Haptic Metronome.suo and b/Haptic Metronome/Haptic Metronome.suo differ diff --git a/include/Metronome.hpp b/include/Metronome.hpp index 1ec54d5..69a55e0 100644 --- a/include/Metronome.hpp +++ b/include/Metronome.hpp @@ -7,7 +7,7 @@ class Metronome { public: - Metronome(Track *track, uint32_t nbbeats=4, uint32_t tempo=0, bool activated=false); + Metronome(const Track *track, uint32_t nbbeats=4, uint32_t tempo=0, bool activated=false); virtual ~Metronome(){} virtual void setActive(bool activated) { _activated = activated; } @@ -20,7 +20,7 @@ class Metronome uint32_t _nbbeats; uint32_t _tempo; bool _activated; - Track *_track; + const Track *_track; }; #endif diff --git a/include/MetronomeAudio.hpp b/include/MetronomeAudio.hpp index a652b3d..6d6bb67 100644 --- a/include/MetronomeAudio.hpp +++ b/include/MetronomeAudio.hpp @@ -15,7 +15,7 @@ class MetronomeAudio : public Metronome { public: - MetronomeAudio(Track *track, uint32_t nbbeats=4, uint32_t tempo=0, bool activated=true); + MetronomeAudio(const Track *track, uint32_t nbbeats=4, uint32_t tempo=0, bool activated=true); ~MetronomeAudio(); diff --git a/include/MetronomeHaptic.hpp b/include/MetronomeHaptic.hpp index 92db869..361a005 100644 --- a/include/MetronomeHaptic.hpp +++ b/include/MetronomeHaptic.hpp @@ -17,7 +17,7 @@ class MetronomeHaptic : public Metronome { public: - MetronomeHaptic(Track *track, uint32_t nbbeats=4, uint32_t tempo=0, bool activated=true); + MetronomeHaptic(const Track *track, uint32_t nbbeats=4, uint32_t tempo=0, bool activated=true); ~MetronomeHaptic(); diff --git a/include/PaintGame.h b/include/PaintGame.h index 1a89b5c..a223b76 100644 --- a/include/PaintGame.h +++ b/include/PaintGame.h @@ -4,15 +4,15 @@ #define POSITION_HEIGHT 35 #define POSITION_EYE 45 #define POSITION_CENTER (-70) -#define VIEWLENGTH 250 #include "Track.hpp" +#include "String.hpp" #include "Wiimote.hpp" void init(); void initPaint(); -void draw(Track &t, uint32_t currenttime, char diff); +void draw(const Track &t, 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); @@ -21,7 +21,8 @@ void handleKeyOff(keys k, uint32_t currenttime); void handleKeys(bool a, bool b, bool c, bool d, bool e, uint32_t currenttime); void handleStrokeOn(uint32_t currenttime); void handleStrokeOff(uint32_t currenttime); -bool processEvents(uint32_t currenttime); +bool processEvents(); +void loop(const Track &tr, const String &songname); class MyWiimote: public Wiimote { diff --git a/include/Tools.hpp b/include/Tools.hpp index 4054029..3dde2e4 100644 --- a/include/Tools.hpp +++ b/include/Tools.hpp @@ -25,6 +25,11 @@ #include "Vector2D.hpp" #include +#include +#include +#include +using namespace std; + #define MAX(X,Y) (X)>(Y)?(X):(Y) #define MIN(X,Y) (X)<(Y)?(X):(Y) diff --git a/include/Track.hpp b/include/Track.hpp index 4ecfd8d..c5679f6 100644 --- a/include/Track.hpp +++ b/include/Track.hpp @@ -40,6 +40,8 @@ class Track public: Track(); Track(const Track &); + //create an empty track with the specified tempo and specified duration + Track(uint32_t tempo, uint32_t duration); ~Track(); void addKey(uint32_t time, uint8_t note); @@ -47,16 +49,16 @@ class Track void addTempo(uint32_t tick, uint32_t timestamp, uint32_t t) { _tempo[tick] = Pair(timestamp, t); } void addTimesignature(uint32_t tick, uint32_t timestamp, uint32_t t) { _timesignatures[tick] = Pair(timestamp, t); } - map > *getTempos() { return &_tempo; } - map > *getTimeSignatures() { return &_timesignatures; } + const map > *getTempos() const { return &_tempo; } + const map > *getTimeSignatures() const { return &_timesignatures; } // int getTempo() const { return _currenttempo; } // void setTempo(uint32 t) { _currenttempo = t; } // uint32 beatsToTicks(uint32 time) { return (time * _currenttempo * 480) / 60000.0; } - uint32_t beatsToTicks(uint32_t time, uint32_t tempo) { return (time * tempo * 480) / 60000; } - uint32_t ticksToBeats(uint32_t time) { return 60000000 / time; } +// uint32_t beatsToTicks(uint32_t time, uint32_t tempo) { return (time * tempo * 480) / 60000; } +// uint32_t ticksToBeats(uint32_t time) { return 60000000 / time; } void displayTracks(); void drawFrets(uint32_t postime) const; @@ -74,7 +76,7 @@ class Track uint32_t getTempo(uint32_t tick); uint32_t getTimeSignature(uint32_t tick); - list > * getBips() { return &_bips; } + const list > * getBips() const { return &_bips; } void addBip(uint32_t tick, uint32_t timestamp) { _bips.push_back(Pair(tick, timestamp)); } //void computeBips(); @@ -84,7 +86,7 @@ class Track private: //notes : position ; configs map _notes[NBDIFFICULTIES]; - map > _tempo; + map > _tempo; //microseconds per quarter-note map > _timesignatures; // (beat, timestamp) diff --git a/include/Wiimote.hpp b/include/Wiimote.hpp index 9802af5..87b9a78 100644 --- a/include/Wiimote.hpp +++ b/include/Wiimote.hpp @@ -16,7 +16,7 @@ class Wiimote{ public: Wiimote(int nb=1); - void handleEvents(uint32_t currenttime); + void handleEvents(); void vibration(int wiimote, int time); diff --git a/src/Metronome.cpp b/src/Metronome.cpp index d4f304d..e07be39 100644 --- a/src/Metronome.cpp +++ b/src/Metronome.cpp @@ -1,6 +1,6 @@ #include "Metronome.hpp" -Metronome::Metronome(Track *track, uint32_t nbbeats, uint32_t tempo, bool activated) +Metronome::Metronome(const Track *track, uint32_t nbbeats, uint32_t tempo, bool activated) :_track(track), _nbbeats(nbbeats), _tempo(tempo), _activated(activated) { } diff --git a/src/MetronomeAudio.cpp b/src/MetronomeAudio.cpp index c3fcae2..0f2bea0 100644 --- a/src/MetronomeAudio.cpp +++ b/src/MetronomeAudio.cpp @@ -5,14 +5,20 @@ #ifdef WIN32 #include #include + extern struct { int tv_sec, tv_usec; } start; #else #include + extern struct timeval start; #endif -MetronomeAudio::MetronomeAudio(Track *track, uint32_t nbbeats, uint32_t tempo, bool activated) +#define BIPLENGTH 10 + +extern FILE*logfile; + +MetronomeAudio::MetronomeAudio(const Track *track, uint32_t nbbeats, uint32_t tempo, bool activated) :Metronome(track, nbbeats, tempo, activated), thread(NULL) { - Mixer::getInstance()->addSound("bip","bip.ogg"); + Mixer::getInstance()->addSound("bip","bip10.ogg"); Mixer::getInstance()->addSound("bip2","bip2.ogg"); } @@ -43,13 +49,13 @@ int MetronomeAudio::thread_func(void *obj) int currentbeat = 0; #ifdef WIN32 - struct { int tv_sec, tv_usec; } now, start; + struct { int tv_sec, tv_usec; } now;//, start; DWORD t = timeGetTime (); - start.tv_sec = t / 1000; - start.tv_usec = (t % 1000) * 1000; +/* start.tv_sec = t / 1000; + start.tv_usec = (t % 1000) * 1000;*/ #else - struct timeval now, start; - gettimeofday(&start, NULL); + struct timeval now;/*, start; + gettimeofday(&start, NULL);*/ #endif uint32_t currenttime = 0; float lag = 0.0; @@ -57,7 +63,7 @@ int MetronomeAudio::thread_func(void *obj) while(thismetronome->_activated) { #ifdef WIN32 - t = timeGetTime (); + t = timeGetTime () + BIPLENGTH; now.tv_sec = t / 1000; now.tv_usec = (t % 1000) * 1000; #else @@ -65,12 +71,15 @@ int MetronomeAudio::thread_func(void *obj) #endif uint32_t currenttime = (now.tv_sec - start.tv_sec) * 1000 + (now.tv_usec - start.tv_usec) / 1000; - while (cBips != bips->end() && currenttime >= (*cBips).getY()) + while (cBips != bips->end() && currenttime >= BIPTIMESTAMP(*cBips)) { if (currentbeat == 0) Mixer::getInstance()->playSound("bip"); else Mixer::getInstance()->playSound("bip2"); + if (logfile) + fprintf(logfile, "BIP;%d;%d;%d\n", BIPTICK(*cBips), BIPTIMESTAMP(*cBips), currenttime); + currentbeat = (currentbeat + 1) % thismetronome->_nbbeats; cBips++; } diff --git a/src/MetronomeHaptic.cpp b/src/MetronomeHaptic.cpp index c1086b8..91af41d 100644 --- a/src/MetronomeHaptic.cpp +++ b/src/MetronomeHaptic.cpp @@ -6,14 +6,18 @@ #include #include #include "SerialWindows.hpp" + extern struct { int tv_sec, tv_usec; } start; #else #include #include "SerialLinux.hpp" + extern struct timeval start; #endif -#define BEFOREDELAY 20 +#define BEFOREDELAY 150 -MetronomeHaptic::MetronomeHaptic(Track *track, uint32_t nbbeats, uint32_t tempo, bool activated) +extern FILE *logfile; + +MetronomeHaptic::MetronomeHaptic(const Track *track, uint32_t nbbeats, uint32_t tempo, bool activated) :Metronome(track, nbbeats, tempo, activated), thread(NULL), _comport(new SerialWindows("COM5")) { } @@ -89,16 +93,16 @@ int MetronomeHaptic::thread_func(void *obj) static list >::const_iterator cBips = bips->begin(); //static map >::const_iterator cTempo = tempo->begin(); static map >::const_iterator cTimesignature = timesignatures->begin(); - + #ifdef WIN32 - struct { int tv_sec, tv_usec; } now, start; + struct { int tv_sec, tv_usec; } now;//, start DWORD t = timeGetTime () - BEFOREDELAY; - start.tv_sec = t / 1000; - start.tv_usec = (t % 1000) * 1000; +/* start.tv_sec = t / 1000; + start.tv_usec = (t % 1000) * 1000;*/ #else - struct timeval now, start; - gettimeofday(&start, NULL); -#endif + struct timeval now;/*, start; + gettimeofday(&start, NULL);*/ +#endif*/ //float currenttime = 0; char outBuffer[5]; @@ -112,7 +116,7 @@ int MetronomeHaptic::thread_func(void *obj) while(thismetronome->_activated) { #ifdef WIN32 - t = timeGetTime (); + t = timeGetTime () + BEFOREDELAY; now.tv_sec = t / 1000; now.tv_usec = (t % 1000) * 1000; #else @@ -124,25 +128,27 @@ int MetronomeHaptic::thread_func(void *obj) { char outBuffer[5]; outBuffer[0] = 'N'; - outBuffer[1] = ((*cBips).getY() & 0xff000000) >> 24; - outBuffer[2] = ((*cBips).getY() & 0x00ff0000) >> 16; - outBuffer[3] = ((*cBips).getY() & 0x0000ff00) >> 8; - outBuffer[4] = (*cBips).getY() & 0x000000ff; + outBuffer[1] = (BIPTIMESTAMP(*cBips) & 0xff000000) >> 24; + outBuffer[2] = (BIPTIMESTAMP(*cBips) & 0x00ff0000) >> 16; + outBuffer[3] = (BIPTIMESTAMP(*cBips) & 0x0000ff00) >> 8; + outBuffer[4] = BIPTIMESTAMP(*cBips) & 0x000000ff; bool written = thismetronome->_comport->WriteData(outBuffer, 5); + if (logfile) + fprintf(logfile, "BIP;%d;%d;%d\n", BIPTICK(*cBips), BIPTIMESTAMP(*cBips), currenttime); cBips++; } // on avance tant que les tempos sont dépassées - while (cTimesignature != timesignatures->end() && TIMESTAMP(*cTimesignature) < currenttime - BEFOREDELAY) + while (cTimesignature != timesignatures->end() && currenttime >= TIMESTAMP(*cTimesignature)) { char outBuffer[5]; - outBuffer[0] = 'T'; - outBuffer[1] = ((*cTimesignature).second.getY() & 0xff000000) >> 24; - outBuffer[2] = ((*cTimesignature).second.getY() & 0x00ff0000) >> 16; - outBuffer[3] = ((*cTimesignature).second.getY() & 0x0000ff00) >> 8; - outBuffer[4] = (*cTimesignature).second.getY() & 0x000000ff; + outBuffer[0] = 'B'; + outBuffer[1] = (TIMESTAMP(*cTimesignature) & 0xff000000) >> 24; + outBuffer[2] = (TIMESTAMP(*cTimesignature) & 0x00ff0000) >> 16; + outBuffer[3] = (TIMESTAMP(*cTimesignature) & 0x0000ff00) >> 8; + outBuffer[4] = TIMESTAMP(*cTimesignature) & 0x000000ff; bool written = thismetronome->_comport->WriteData(outBuffer, 5); diff --git a/src/PaintGame.cpp b/src/PaintGame.cpp index 206b7e9..a64f167 100644 --- a/src/PaintGame.cpp +++ b/src/PaintGame.cpp @@ -2,6 +2,9 @@ #include "Texture.hpp" #include "Mixer.hpp" +#include "Metronome.hpp" +#include "MetronomeAudio.hpp" +#include "MetronomeHaptic.hpp" #include @@ -9,7 +12,9 @@ #include #include #include + struct { int tv_sec, tv_usec; } start; #else + struct timeval start; #include #include #include @@ -18,9 +23,10 @@ #include using namespace std; -float SECONDSIZE = 0.050f; +int VIEWLENGTH = 250; +extern float SECONDSIZE; float posz = 0; -Texture *texturebois, *texturefond, *texturesteel; +Texture *texturebois, *texturefond, *texturesteel, *texturefrett; int resolution_x, resolution_y; Config fromKeyboard; @@ -34,57 +40,57 @@ SDL_Renderer *renderer; void init() { - //initializes SDL - cout << "init SDL" << endl; - const SDL_VideoInfo* info = NULL; + //initializes SDL + cout << "init SDL" << endl; + const SDL_VideoInfo* info = NULL; - atexit(SDL_Quit); + atexit(SDL_Quit); - if( SDL_Init(SDL_INIT_EVERYTHING) <0 ) - throw "Error while loading SDL"; + if( SDL_Init(SDL_INIT_EVERYTHING) <0 ) + throw "Error while loading SDL"; - //Initialize SDL_mixer - if (Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 1024 ) == -1 ) - throw String("Can't initialize sound"); + //Initialize SDL_mixer + if (Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 1024 ) == -1 ) + throw String("Can't initialize sound"); - cout << "get video info" << endl; - info = SDL_GetVideoInfo(); - if(!info) - throw "Impossible to get information from SDL"; + cout << "get video info" << endl; + info = SDL_GetVideoInfo(); + if(!info) + throw "Impossible to get information from SDL"; - SDL_EnableUNICODE(1); + SDL_EnableUNICODE(1); -// int width, height; - // we search for the available screen sizes - int searchflags = SDL_HWSURFACE;// | SDL_FULLSCREEN; - SDL_Rect **modes = SDL_ListModes(NULL, searchflags); - if (modes == NULL) - throw "Impossible to get the screen resolution !"; - else if (modes == (SDL_Rect **)-1) - { - resolution_x = 1280; - resolution_y = 1024; - } - else - { - int i; - for (i=0 ; modes[i] ; i++) - { - cout << "Resolution available: " << modes[i]->w << "x" << modes[i]->h << endl; - if (modes[i]->w == 1280 && modes[i]->h == 1024) - { - resolution_x = modes[i]->w; - resolution_y = modes[i]->h; - break; - } - } - if (!modes[i]) - { - cout << "Resolution wanted not available" << endl; - resolution_x = 1024; - resolution_y = 768; - } - } + // int width, height; + // we search for the available screen sizes + int searchflags = SDL_HWSURFACE;// | SDL_FULLSCREEN; + SDL_Rect **modes = SDL_ListModes(NULL, searchflags); + if (modes == NULL) + throw "Impossible to get the screen resolution !"; + else if (modes == (SDL_Rect **)-1) + { + resolution_x = 1280; + resolution_y = 1024; + } + else + { + int i; + for (i=0 ; modes[i] ; i++) + { + cout << "Resolution available: " << modes[i]->w << "x" << modes[i]->h << endl; + if (modes[i]->w == 1280 && modes[i]->h == 1024) + { + resolution_x = modes[i]->w; + resolution_y = modes[i]->h; + break; + } + } + if (!modes[i]) + { + cout << "Resolution wanted not available" << endl; + resolution_x = 1024; + resolution_y = 768; + } + } #if 0//SDL_VERSION_ATLEAST(1,3,0) if ((window = SDL_CreateWindow("Haptic Metronome", @@ -124,20 +130,22 @@ void init() SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 8); int flags = SDL_OPENGL | SDL_OPENGL | SDL_HWSURFACE;// | SDL_FULLSCREEN; - -// int flags = SDL_OPENGL | SDL_OPENGLBLIT | SDL_SWSURFACE | SDL_FULLSCREEN; - SDL_Surface * screen; - if (!(screen = SDL_SetVideoMode(resolution_x, resolution_y, 0, flags))) - throw "Impossible to change the video mode"; + + glEnable(GL_MULTISAMPLE); // Utilisation de l'anti-aliasing possible ? if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) == -1) throw "Impossible to initialize SDL_GL_MULTISAMPLEBUFFERS"; // Nombre de tampons utilisés pour l'anti-aliasing (la valeur utilisée dépend de la carte graphique) - if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 6) == -1) + if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8) == -1) throw "Impossible to initialize SDL_GL_MULTISAMPLESAMPLES"; +// int flags = SDL_OPENGL | SDL_OPENGLBLIT | SDL_SWSURFACE | SDL_FULLSCREEN; + SDL_Surface * screen; + if (!(screen = SDL_SetVideoMode(resolution_x, resolution_y, 0, flags))) + throw "Impossible to change the video mode"; + GLdouble ratio = (GLdouble) screen->w / screen->h; printf("infos : %d %d %d %d\n", screen->flags, screen->w, screen->h, screen->pitch); @@ -151,13 +159,17 @@ void init() #endif glClearColor(1.0f, 1.0f, 1.0f, 0.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT);// | GL_DEPTH_BUFFER_BIT); glViewport(0, 0, resolution_x, resolution_y); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_BLEND); - glShadeModel(GL_SMOOTH); +// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glDisable(GL_BLEND); +// glShadeModel(GL_SMOOTH); glEnable(GL_NORMALIZE); + //glDisable(GL_DEPTH_TEST); + glDepthMask(GL_TRUE); + glDepthFunc(GL_ALWAYS); + //glDepthFunc(GL_LEQUAL); // glEnable(GL_POLYGON_SMOOTH); GLfloat light_ambient[] = { 0.8f, 0.7f, 0.9f, 1.0f }; @@ -182,15 +194,26 @@ void initPaint() texturebois = new Texture("wood.jpg", true); texturefond = new Texture("back.jpg"); texturesteel = new Texture("steel.jpg"); - + texturefrett = new Texture("frett.png"); } -void draw(Track &t, uint32_t currenttime, char diff) +void draw(const Track &t, char diff) { +#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 + uint32_t currenttime = (now.tv_sec - start.tv_sec) * 1000 + (now.tv_usec - start.tv_usec) / 1000; + posz = -SECONDSIZE * currenttime; // cout << "draw screen" << endl; - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT);// | GL_DEPTH_BUFFER_BIT); // glColor(1.0, 1.0, 1.0, 0.0); //Draw the background @@ -211,7 +234,7 @@ void draw(Track &t, uint32_t currenttime, char diff) glTexCoord2f(1.0, 0.0); glVertex2f(float(resolution_x), 0.0f); glEnd(); glDisable(GL_TEXTURE_2D); - + glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective( 45.0f, double(resolution_x) / double(resolution_y), 1/*POSITION_EYE+3*/, VIEWLENGTH ); @@ -226,10 +249,10 @@ void draw(Track &t, uint32_t currenttime, char diff) // cout << "End: " << t.getEndOfTrack() << endl; //glEnable(GL_LIGHTING); - glPolygonOffset(1.0f, 4.0f); +// glPolygonOffset(1.0f, 4.0f); // Draw the guitar neck - glEnable(GL_POLYGON_OFFSET_FILL); +// glEnable(GL_POLYGON_OFFSET_FILL); glEnable(GL_TEXTURE_2D); texturebois->useTexture(); glBegin(GL_QUADS); @@ -240,7 +263,7 @@ void draw(Track &t, uint32_t currenttime, char diff) glTexCoord2f(1.0, 0.0); glVertex3f(-NECKWIDTH, 0.0, 0.0); glEnd(); glDisable(GL_TEXTURE_2D); - glDisable(GL_POLYGON_OFFSET_FILL); +// glDisable(GL_POLYGON_OFFSET_FILL); glMatrixMode(GL_MODELVIEW); glPushMatrix(); @@ -472,8 +495,19 @@ void handleStrokeOff(uint32_t currenttime) { } -bool processEvents(uint32_t currenttime) +bool processEvents() { +#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 + uint32_t currenttime = (now.tv_sec - start.tv_sec) * 1000 + (now.tv_usec - start.tv_usec) / 1000; + SDL_Event event; // bool quitProgram = false; @@ -536,6 +570,10 @@ bool processEvents(uint32_t currenttime) case SDLK_SPACE: handleStrokeOn(currenttime); break; + case SDLK_RCTRL: + if (logfile) + fprintf(logfile, "PRESS;%d\n", currenttime); + break; default: break; } @@ -580,3 +618,98 @@ bool processEvents(uint32_t currenttime) } return false; } + +void loop(const Track &tr, const String &songname) +{ + bool iswiimoteconnected = false; + Wiimote *mw; + try + { + mw = new MyWiimote(); + iswiimoteconnected = true; + } + catch(...){} + + init(); + cout << "Create Metronome" << endl; + Metronome *m = new MetronomeAudio(&tr); +// Metronome *m = new MetronomeHaptic(&tr); + + bool quitProgram = false; + + cout << "Loading sounds" << endl; + +// Mixer::getInstance()->addSound("guitar", songname + "/guitar.ogg"); +// bool rhythm = Mixer::getInstance()->addSound("rhythm", songname + "/rhythm.ogg"); + + Mixer::getInstance()->setMusicVolume(30); + Mixer::getInstance()->setSoundVolume(30); + + cout << "Init paint" << endl; + initPaint(); + +//must be done before paint and metronomes +#ifdef WIN32 + DWORD t = timeGetTime (); + start.tv_sec = t / 1000; + start.tv_usec = (t % 1000) * 1000; +#else + gettimeofday(&start, NULL); +#endif + + cout << "LET'S ROCK!" << endl; + //m->setActive(true); + + 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"); + music.addSound(songname + "/guitar.ogg"); + music.addSound(songname + "/rhythm.ogg"); + cout << "Mixing sounds" << endl; + Mix_Chunk *m_chunk = music.mix(); + cout << "Play music" << endl; + Mix_PlayChannel(-1, m_chunk, 1); +*/ +/* + Mix_Chunk *Mix_LoadWAV(char *file) x2/3 + for(...) + a/3+b/3+c/3; + int Mix_SetPanning(int channel, Uint8 left, Uint8 right) left + right + int Mix_PlayChannel(int channel, Mix_Chunk *chunk, int loops) +*/ + + 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 + uint32_t currenttime = (now.tv_sec - start.tv_sec) * 1000 + (now.tv_usec - start.tv_usec) / 1000; + */ + // cout << "draw" << endl; + draw(tr, NORMAL); +// cout << "events" << endl; + quitProgram = processEvents(); + if (iswiimoteconnected) + mw->handleEvents(); +// cout << "pause" << endl; +// SDL_Delay(10); + // check metronome events +// tr.checkMetronome(currenttime, m); + } + + delete m; +} \ No newline at end of file diff --git a/src/Track.cpp b/src/Track.cpp index 925885d..8b709b8 100644 --- a/src/Track.cpp +++ b/src/Track.cpp @@ -9,9 +9,10 @@ #include using namespace std; -extern Texture* texturesteel; +extern Texture* texturesteel, *texturefrett; extern FILE *logfile; extern float SECONDSIZE; +extern int VIEWLENGTH; Track::Track() :/*_currenttempo(120)*,_currentpos(0),*/ _trackSize(0), _active(true) @@ -38,6 +39,21 @@ Track::Track(const Track &tr) } } +/** + Click track + tempo: beats per minute + duration: track length in ms +*/ +Track::Track(uint32_t tempo, uint32_t duration) +{ + uint32_t t = 60000000 / tempo; + addTempo(0, 0, tempo); + addTimesignature(0, 0, 1); + + for (uint32_t i = 0 ; i < (duration * 1000) / t ; i++) + _bips.push_back(Pair(i, i * t / 1000 + 2000)); +} + Track::~Track() { } @@ -325,20 +341,23 @@ void Track::drawFrets(uint32_t postime) const list >::const_iterator b = startingbips; glMatrixMode(GL_MODELVIEW); + //glDisable(GL_DEPTH_TEST); //on dessine les frettes jusqu'à 10s - while (b != _bips.end() && BIPTIMESTAMP(*b) < postime + 10000) + while (b != _bips.end() && BIPTIMESTAMP(*b) < postime + VIEWLENGTH / SECONDSIZE) { glPushMatrix(); glTranslatef(0, 0, static_cast(- SECONDSIZE * BIPTIMESTAMP(*b))); - glEnable(GL_POLYGON_OFFSET_FILL); - glPolygonOffset(-2.0f, -20.0f); + //glEnable(GL_POLYGON_OFFSET_FILL); + //glPolygonOffset(-2.0f, -20.0f); + glEnable(GL_TEXTURE_2D); + texturefrett->useTexture(); glBegin(GL_QUADS); glColor3f(0.8f, 0.8f, 0.0f); // Set The Color To Green glNormal3f(0, 1.0f, 0); - glVertex3f( NECKWIDTH, 0.0f, 0.2f); - glVertex3f( NECKWIDTH, 0.0f, -0.2f); - glVertex3f(-NECKWIDTH, 0.0f, -0.2f); - glVertex3f(-NECKWIDTH, 0.0f, 0.2f); + glTexCoord2f(0.0, 0.0); glVertex3f(-NECKWIDTH, 0.0f, .2f); + glTexCoord2f(1.0, 0.0); glVertex3f( NECKWIDTH, 0.0f, .2f); + glTexCoord2f(1.0, 1.0); glVertex3f( NECKWIDTH, 0.02f, -.2f); + glTexCoord2f(0.0, 1.0); glVertex3f(-NECKWIDTH, 0.02f, -.2f); /* glVertex3f( NECKWIDTH,0, 0.1f); glVertex3f( NECKWIDTH,0.1f, 0); glVertex3f(-NECKWIDTH,0.1f, 0); @@ -349,7 +368,8 @@ void Track::drawFrets(uint32_t postime) const glVertex3f(-NECKWIDTH,0, -0.1f); glVertex3f(-NECKWIDTH,0.1f, 0);*/ glEnd(); - glDisable(GL_POLYGON_OFFSET_FILL); + glDisable(GL_TEXTURE_2D); + //glDisable(GL_POLYGON_OFFSET_FILL); glPopMatrix(); b++; } @@ -379,7 +399,7 @@ void Track::drawNotes(uint32_t postime, uint8_t diff) const glMatrixMode(GL_MODELVIEW); //on dessine les notes jusqu'à 10s - while (n != _notes[diff].end() && NOTEENDTIMESTAMP(*n) < postime + 10000) + while (n != _notes[diff].end() && NOTEENDTIMESTAMP(*n) < postime + VIEWLENGTH / SECONDSIZE) { glPushMatrix(); glTranslatef(0, 0, static_cast(- SECONDSIZE * NOTETIMESTAMP(*n))); diff --git a/src/Wiimote.cpp b/src/Wiimote.cpp index 962c40b..eb500d9 100644 --- a/src/Wiimote.cpp +++ b/src/Wiimote.cpp @@ -6,6 +6,12 @@ using namespace std; #define MAX(X,Y) (((X)>(Y))?(X):(Y)) #define MIN(X,Y) (((X)<(Y))?(X):(Y)) +#ifdef WIN32 + extern struct { int tv_sec, tv_usec; } start; +#else + extern struct timeval start; +#endif + Wiimote::Wiimote(int nb) :_wiimotes(wiiuse_init(nb)), _nb(nb), _strumstate(false) { @@ -54,8 +60,19 @@ void Wiimote::handleKey(guitar_hero_3_t* gh3, int key, int button, uint32_t curr } } -void Wiimote::handleEvents(uint32_t currenttime) +void Wiimote::handleEvents() { +#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 + uint32_t currenttime = (now.tv_sec - start.tv_sec) * 1000 + (now.tv_usec - start.tv_usec) / 1000; + if (!wiiuse_poll(_wiimotes, _nb)) return; for (int i = 0 ; i < _nb ; i++) diff --git a/src/main.cpp b/src/main.cpp index 167f656..5df6946 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,9 +4,6 @@ #include "Track.hpp" #include "Mixer.hpp" #include "PaintGame.h" -#include "Metronome.hpp" -#include "MetronomeAudio.hpp" -#include "MetronomeHaptic.hpp" #ifdef WIN32 #include @@ -32,6 +29,7 @@ #include #endif*/ +float SECONDSIZE = 0.050f; FILE *logfile = NULL; //list bips; //list::iterator cb; @@ -42,137 +40,61 @@ void handleStrokeOff() int main(int argc, char *argv[]) { - bool iswiimoteconnected = false; - Wiimote *mw; - try - { - mw = new MyWiimote(); - iswiimoteconnected = true; - } - catch(...){} - if (argc < 2) { - cout << "Usage : " << argv[0] << "songname" << endl; + cout << "Usage : " << argv[0] << "songname [logname] [tempo] [duration] [beatsize]" << endl; exit(0); } String songname(argv[1]); - if (argc >= 3) - { - time_t tim = time(NULL); - struct tm *t = gmtime(&tim); - String logfilename = String(argv[2]) + "-" + String(1900 + t->tm_year) + "-" + String(t->tm_mon) + "-" + String(t->tm_mday) + "-" + String(t->tm_hour) + "-" + String(t->tm_min) + "-" + String(t->tm_sec) + "-" + String(argv[1]); - logfile = fopen(logfilename.c_str(), "w"); - } + time_t tim = time(NULL); + struct tm *t = gmtime(&tim); cout << "Loading song" << endl; - MIDIReader *test; - try + if (argc >= 6) { - test = new MIDIReader(songname + "/notes.mid"); + Track tr(atoi(argv[3]), atoi(argv[4])); + SECONDSIZE = atof(argv[5]); + + String logfilename = String(argv[1]) + "-" + String(argv[2]) + "-" + \ + String(1900 + t->tm_year) + "-" + String(t->tm_mon) + "-" + String(t->tm_mday) + "-" + \ + String(t->tm_hour) + "-" + String(t->tm_min) + "-" + String(t->tm_sec) + "-" + \ + String(int(floor(SECONDSIZE * 1000 + 0.5))) + "-" + argv[3]; + logfile = fopen(logfilename.c_str(), "w"); + //run the game with a click track + loop(tr, songname); } - catch(...) + else { - cerr << "Unable to load the song file" << endl; - exit(0); + if (argc >= 3) + { + String logfilename = String(argv[1]) + "-" + String(argv[2]) + "-" + String(1900 + t->tm_year) + "-" + String(t->tm_mon) + "-" + String(t->tm_mday) + "-" + String(t->tm_hour) + "-" + String(t->tm_min) + "-" + String(t->tm_sec) + "-" + String(argv[1]); + logfile = fopen(logfilename.c_str(), "w"); + } + + Track tr; + MIDIReader *test; + try + { + test = new MIDIReader(songname + "/notes.mid"); + } + catch(...) + { + cerr << "Unable to load the song file" << endl; + exit(0); + } + + cout << "Loading notes" << endl; + test->readHeader(); + cout << "Reading tracks" << endl; + test->readTracks(tr); + + //run the game with a song + loop(tr, songname); } - Track tr; - - cout << "Loading notes" << endl; - test->readHeader(); - cout << "Reading tracks" << endl; - test->readTracks(tr); - - init(); - cout << "Create Metronome" << endl; -// Metronome *m = new MetronomeAudio(&tr); - Metronome *m = new MetronomeHaptic(&tr); - - bool quitProgram = false; + if (logfile) + fclose(logfile); - cout << "Loading sounds" << endl; - -// Mixer::getInstance()->addSound("guitar", songname + "/guitar.ogg"); -// bool rhythm = Mixer::getInstance()->addSound("rhythm", songname + "/rhythm.ogg"); - - Mixer::getInstance()->setMusicVolume(30); - Mixer::getInstance()->setSoundVolume(30); - - - cout << "Init paint" << endl; - initPaint(); - - -#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 + "/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"); - music.addSound(songname + "/guitar.ogg"); - music.addSound(songname + "/rhythm.ogg"); - cout << "Mixing sounds" << endl; - Mix_Chunk *m_chunk = music.mix(); - cout << "Play music" << endl; - Mix_PlayChannel(-1, m_chunk, 1); -*/ -/* - Mix_Chunk *Mix_LoadWAV(char *file) x2/3 - for(...) - a/3+b/3+c/3; - int Mix_SetPanning(int channel, Uint8 left, Uint8 right) left + right - int Mix_PlayChannel(int channel, Mix_Chunk *chunk, int loops) -*/ - - 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 - uint32_t currenttime = (now.tv_sec - start.tv_sec) * 1000 + (now.tv_usec - start.tv_usec) / 1000; - - // cout << "draw" << endl; - draw(tr, currenttime, NORMAL); -// cout << "events" << endl; - quitProgram = processEvents(currenttime); - if (iswiimoteconnected) - mw->handleEvents(currenttime); -// cout << "pause" << endl; -// SDL_Delay(10); - // check metronome events -// tr.checkMetronome(currenttime, m); - } - - delete m; - - if (logfile) - fclose(logfile); - - return 1; + return 1; }