Metronome in thread, sdl 1.3
authorThomas Pietrzak <thomas.pietrzak@gmail.com>
Mon, 4 Apr 2011 21:24:41 +0000 (21:24 +0000)
committerThomas Pietrzak <thomas.pietrzak@gmail.com>
Mon, 4 Apr 2011 21:24:41 +0000 (21:24 +0000)
git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@8 47cf9a05-e0a8-4ed5-9e9b-101a649bc004

12 files changed:
Haptic Metronome/Haptic Metronome.suo
include/PaintGame.h
include/Tools.hpp
include/Track.hpp
include/Wiimote.hpp
src/MIDIReader.cpp
src/PaintGame.cpp
src/Texture.cpp
src/Tools.cpp
src/Track.cpp
src/Wiimote.cpp
src/main.cpp

index 42d219de8c34190fa86d00ba9ffdcb0d055d5658..6397c4f1d1d1e41d26151d2eeac91f536518acda 100644 (file)
Binary files a/Haptic Metronome/Haptic Metronome.suo and b/Haptic Metronome/Haptic Metronome.suo differ
index c472f95888697070ffa9dad720594ea2b4acee27..a4557cd372553334e6c73b2d8832ff6e0da032dc 100644 (file)
 
 void init();
 void initPaint();
-void draw(Track &t, char diff);\r
+void draw(Track &t, float currenttime, char diff);\r
 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);
 \r
-void handleKeyOn(keys k);\r
-void handleKeyOff(keys k);\r
-void handleKeys(bool a, bool b, bool c, bool d, bool e);
-void handleStrokeOn();\r
-void handleStrokeOff();\r
-bool processEvents();
+void handleKeyOn(keys k, float currenttime);\r
+void handleKeyOff(keys k, float currenttime);\r
+void handleKeys(bool a, bool b, bool c, bool d, bool e, float currenttime);
+void handleStrokeOn(float currenttime);\r
+void handleStrokeOff(float currenttime);\r
+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); }
 };
 \r
 #endif\r
index 923bfed99e4268e3511d012d6c17e824746c74b0..4054029ac82c264db7b618c88ecb81ba7585c904 100644 (file)
@@ -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<int> rotatePoint(const Vector2D<int> pos, const Vector2D<int> center, const double &angle);
index 3847f72b94892f94d2cec1403bc9266b81547c2f..501e3611c46c217d2d2f5f05804810443680bc17 100644 (file)
@@ -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<float, uint32_t>(timestamp, t); }
+    void addTimesignature(uint32_t tick, float timestamp, uint32_t t) { _timesignatures[tick] = Pair<float, uint32_t>(timestamp, t); }
+       map<uint32_t, Pair<float, uint32_t> > *getTempos() { return &_tempo; }  
+       map<uint32_t, Pair<float, uint32_t> > *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<Pair<uint32_t, double> > getBips() const { return _bips; }
-       void addBip(uint32_t tick, double timestamp) { _bips.push_back(Pair<uint32_t, double>(tick, timestamp)); }
+       list<Pair<uint32_t, float> > * getBips() { return &_bips; }
+       void addBip(uint32_t tick, float timestamp) { _bips.push_back(Pair<uint32_t, float>(tick, timestamp)); }
 
     //void computeBips();
 
@@ -66,11 +73,11 @@ class Track
   private:
     //notes : position ; configs
     map<uint32_t, Config> _notes[NBDIFFICULTIES];
-    map<uint32_t, uint32_t> _tempo;
-    map<uint32_t, uint32_t> _timesignatures;
+    map<uint32_t, Pair<float, uint32_t> > _tempo;
+    map<uint32_t, Pair<float, uint32_t> > _timesignatures;
 
 //  private:
-    list<Pair<uint32_t, double> > _bips;
+    list<Pair<uint32_t, float> > _bips;
     
     Config _currentconfig[NBDIFFICULTIES];
     
index 0fdc1cb4223cedc81dc3d888c89451fadaf5f270..4d5ad9c02a6445619e67ea34fb1f0d47d39f1e84 100644 (file)
@@ -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];
index 86c2f99a98c0d568b60630d058e149e3ff6550ad..407367673231962f05e0ff628184180d60c3a9b6 100644 (file)
@@ -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)
index 4f43850ed4c719ef9d48e735a595c58433a915b9..b11e10d1ad60fea451a92e8926df5c4d41c71b58 100644 (file)
@@ -6,15 +6,10 @@
 #include <GL/glew.h>
 
 #ifdef WIN32
-#include <time.h>
-#include <windows.h>
-
 #include <SDL.h>
 #include <SDL_mixer.h>
 #include <SDL_image.h>
 #else
-#include <sys/time.h>
-
 #include <SDL/SDL.h>
 #include <SDL/SDL_mixer.h>
 #include <SDL/SDL_image.h>
@@ -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;
+\r
+#if SDL_VERSION_ATLEAST(1,3,0)\r
+SDL_Window *window;
+SDL_Renderer *renderer;
 #endif
 
 void init()
@@ -92,10 +85,32 @@ void init()
       resolution_y = 768;
     }
   }
-
+  \r
+#if 0//SDL_VERSION_ATLEAST(1,3,0)\r
+  if ((window = SDL_CreateWindow("Haptic Metronome",\r
+       SDL_WINDOWPOS_CENTERED,\r
+       SDL_WINDOWPOS_CENTERED,\r
+       resolution_x, resolution_y,\r
+       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)\r
+                               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)\r
+                               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;
index 00d8cbefb55cd3854bb0a34fe13cdbbda953c088..9da81f5b7f23f77d7839568b4554b811bbd4fe8b 100644 (file)
@@ -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)\r
+/*    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)\r
+               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)\r
+          temp = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ABGR8888, SDL_SWSURFACE);
+#else
           temp = SDL_ConvertSurface(surface, &format, SDL_SWSURFACE);
+#endif
           SDL_FreeSurface(surface);
           surface = temp;
         }
index 4b674a4c7fd1a5bdee3a9e5dac382d71355c113c..a09e915c4931901ae551fa2667faebe408be1143 100644 (file)
  */
 #include "Tools.hpp"
 
+#ifdef WIN32
+#include <SDL.h>
+#else
+#include <SDL/SDL.h>
+#endif
+
 #include <cmath>
 #include <cstdio>
 #include <sys/types.h>
index 77ce890dbd30d48a92419b90fb0a079de0c1c605..d3e8117dc08978c0954d11de29b56a4e43b22c11 100644 (file)
@@ -1,10 +1,14 @@
 #include "Track.hpp"
 
-#include "Mixer.hpp"
 #include "Texture.hpp"
 
 #include <map>
 
+#include <windows.h>
+#include <iostream>
+#include <sstream>
+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<uint32_t, uint32_t>::iterator ti = _tempo.begin();
+               map<uint32_t, Pair<float, uint32_t> >::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<uint32_t, Pair<float, uint32_t> >::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<uint32_t, Pair<float, uint32_t> >::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<uint32_t,uint32_t>::iterator ii=_tempo.begin(); ii!=_tempo.end(); ++ii)
-       cout << (*ii).first << ": " << (*ii).second << " (" << ticksToBeats((*ii).second) << ")" << endl;
+   for( map<uint32_t, Pair<float, uint32_t> >::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<Pair<uint32_t, double> >::const_iterator startingbips = _bips.begin();
+       static list<Pair<uint32_t, float> >::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<Pair<uint32_t, double> >::const_iterator b = startingbips;
+       list<Pair<uint32_t, float> >::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<uint32_t,Config>::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<uint32_t, Pair<float, uint32_t> >::const_iterator cTempo = _tempo.begin();
+       static map<uint32_t, Pair<float, uint32_t> >::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
index 9698669440d2f850fe45e45eea39606d55842e67..4e8132f80e00c80b5dede004df96db09b550c0c0 100644 (file)
@@ -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);
index f06c20dc027f23835a191fd03610772fd84087a2..8218b048374379e61603a2dbe541905ee9166b53 100644 (file)
@@ -4,11 +4,16 @@
 #include "Track.hpp"
 #include "Mixer.hpp"
 #include "PaintGame.h"
+#include "Metronome.hpp"
+#include "MetronomeAudio.hpp"
 
 #ifdef WIN32
+#include <time.h>
 #include <SDL.h>
-
+#include <windows.h>
 #else
+#include <sys/time.h>
+
 #include <SDL/SDL.h>
 #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;
 }