From e098aba421ebbd505ff2486c3e770711160833b9 Mon Sep 17 00:00:00 2001 From: Thomas Pietrzak Date: Mon, 4 Apr 2011 21:24:41 +0000 Subject: [PATCH] Metronome in thread, sdl 1.3 git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@8 47cf9a05-e0a8-4ed5-9e9b-101a649bc004 --- Haptic Metronome/Haptic Metronome.suo | Bin 45568 -> 66048 bytes include/PaintGame.h | 24 ++-- include/Tools.hpp | 2 + include/Track.hpp | 29 +++-- include/Wiimote.hpp | 14 +- src/MIDIReader.cpp | 18 +-- src/PaintGame.cpp | 178 +++++++++++--------------- src/Texture.cpp | 23 +++- src/Tools.cpp | 6 + src/Track.cpp | 80 ++++++++++-- src/Wiimote.cpp | 15 ++- src/main.cpp | 62 +++++++-- 12 files changed, 275 insertions(+), 176 deletions(-) diff --git a/Haptic Metronome/Haptic Metronome.suo b/Haptic Metronome/Haptic Metronome.suo index 42d219de8c34190fa86d00ba9ffdcb0d055d5658..6397c4f1d1d1e41d26151d2eeac91f536518acda 100644 GIT binary patch delta 8831 zcmcgx33yaRw!XL1-E@GEbP_^Vl5`dlAbSAWvor|-LLiBRXat*d2O2u*kaQ#qB!Mq3 z4-h2B1@)7u@@)GbV;;4{uMPwp6BBQ8iW=7E`??3nUZ8|J6pEKXw{{F72 zQ*~=ObQYVT0*->+($BnZMUmoHyVL7EDT2hxEIAQRxarAWsCw*a$%3BW`k z8<-4C0p;TWc7U@)=4yXs3 zfn?w=pap0Int+u6w`Z(Ez7=Q#(tu9&EHRQ!s-t{XkFEasyEjBH5W9+;&Fa~rItY2{ zO^>2fwT`j^)bxX9mYRE^>DpNF1XXJ@)RRR)aBkObSF4A5wAT8dkglfYK?U9-Gk|wN z=}I#Mg+J0IT?;t|pO_eck`{1C3mJ@@8GNvz%-ji-YOLV01&d@_wZIDx1uCh{=XFt_ zf+3@Pj-y1RZ`6gWf2Ld8O-45Q_Sw3n!i5|~f987&fJH%dU z3Yw|rB~!0L4#KPgCsX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44Al7b0F{-u zQQcS~X$Bj}>1v}3uOL^lnQ9&u z{oso%+p{f8D>vrs`MmC($h6Fjsh2+rj7bkcfCW?e_Twy0VZU0fiM1CdmGzn$8X4Oom2dVJt@EJU#pmq{&d%nd>fe*F0@ookLJze8>o z1c$?E_>h~v`gtfhTSST&chk<+2 zGB%%5PK})MW7D|T&E4!H?S8bJuFheequp^hTgw(fGj+Y$54$98ESf9%)o(xYvG*_@ z^&U#k(P9w;`YMkVi|LaE&#^;Hy$i^Be84L?ELO^TbC$LVZ35Im!^us~=7|4k$=yeG zF&06WC)_Lq#!Stf2-DGx1+0N&M>Oqv;QlZ3bhH2C;1v|#vX;l4vaK_P(I;ZO0e@Ps z;XI;v6l}oSbcIt}emGs+-$FO`86#}Io%ZI9Nv{`IZl}4)4wb{FT5bkMo)gFO+MW2S(h0wEH1Bx6 VMk2{lGSvqM;9nIB4#N>F`8T|_vOE9) 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; } -- 2.30.2