Factor out main scene and shader loading
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
- Detect64BitPortabilityProblems="true"
DebugInformationFormat="4"
/>
<Tool
>
</File>
<File
+ RelativePath=".\src\Pads.cpp"
+ >
+ </File>
+ <File
RelativePath=".\src\RButton.cpp"
>
</File>
>
</File>
<File
+ RelativePath=".\src\Scene.cpp"
+ >
+ </File>
+ <File
RelativePath=".\src\Sequencer.cpp"
>
</File>
>
</File>
<File
+ RelativePath=".\src\ShaderDepot.cpp"
+ >
+ </File>
+ <File
RelativePath=".\src\Texture.cpp"
>
</File>
>
</File>
<File
+ RelativePath=".\src\Pads.h"
+ >
+ </File>
+ <File
RelativePath=".\src\RButton.h"
>
</File>
>
</File>
<File
+ RelativePath=".\src\Scene.h"
+ >
+ </File>
+ <File
RelativePath=".\src\Sequencer.h"
>
</File>
>
</File>
<File
+ RelativePath=".\src\ShaderDepot.h"
+ >
+ </File>
+ <File
RelativePath=".\src\Texture.h"
>
</File>
set DEBUG=0
@rem List your source files here
-set SRC=src/KickMan.cpp src/RButton.cpp src/RShader.cpp src/Clock.cpp src/BeatChart.cpp src/Sequencer.cpp src/Shader.cpp src/DecalShader.cpp src/Component.cpp src/BPMDisplay.cpp src/Texture.cpp
+set SRC=src/KickMan.cpp src/RButton.cpp src/RShader.cpp src/Clock.cpp src/BeatChart.cpp src/Sequencer.cpp src/Shader.cpp src/DecalShader.cpp src/Component.cpp src/BPMDisplay.cpp src/Texture.cpp src/Scene.cpp src/Pads.cpp src/ShaderDepot.cpp
@rem List the libraries needed
set LIBS=-lSDL -lGLESv2 -lpdl -lSDL_mixer -lSDL_image
#include "BPMDisplay.h"
+#include "ShaderDepot.h"
-BPMDisplay::BPMDisplay(void) {
+BPMDisplay::BPMDisplay() {
this->BPMtex = new Texture("img/bpm.png");
this->setSize(BPMtex->getWidth(), BPMtex->getHeight());
numberVertices[5] = 14;
numberVertices[6] = 0;
numberVertices[7] = 14;
+ this->shader = (DecalShader*) loadShader("DecalShader");
}
-BPMDisplay::~BPMDisplay(void) {
+BPMDisplay::~BPMDisplay() {
}
void BPMDisplay::draw(int t, int bpm) {
#include <GLES2/gl2.h>
#include <string.h>
+#include "ShaderDepot.h"
BeatChart::BeatChart(Clock* clock) : Component() {
this->clock = clock;
this->beat = 0;
this->beat_start = 0;
setSize(320, 10);
+ this->shader = (RShader*) loadShader("RShader");
}
BeatChart::~BeatChart(void) {
this->beat_start = t;
}
int delta = t - this->beat_start;
- this->fade_time = 60000.0 / clock->getBPM();
+ this->fade_time = (int)(60000.0 / clock->getBPM());
if (delta < this->fade_time) {
- shader->setFade(1.0 - (GLfloat)delta / (GLfloat)this->fade_time);
+ shader->setFade(1.0f - (GLfloat)delta / (GLfloat)this->fade_time);
} else {
- shader->setFade(0.0);
+ shader->setFade(0.0f);
}
shader->setColor(0, 0, 0, 1);
shader->setFadeColor(0.75, 0.75, 0.75, 1.0);
shader->setPositions(4, this->blipVertices);
- shader->setTranslation(this->x + blip_w * (beat % this->beats_per_measure), this->y);
+ shader->setTranslation(this->x + (int)(blip_w * (beat % this->beats_per_measure)), this->y);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
shader->resetTransform();
#include <math.h>
#include <GLES2/gl2.h>
-#include "SDL.h"
-#include "PDL.h"
-#include "SDL_mixer.h"
+#include <SDL.h>
+#include <PDL.h>
+#include <SDL_mixer.h>
-#include "RButton.h"
-#include "Clock.h"
-#include "BeatChart.h"
-#include "Sequencer.h"
-#include "BPMDisplay.h"
+#include "Pads.h"
-SDL_Surface *screen;
-RShader* rshader;
-DecalShader* decalshader;
-RButton* button[9];
-Clock* clock;
-BeatChart* beatChart;
-Sequencer* sequencer;
-BPMDisplay* bpmDisplay;
-Mix_Chunk* samples[9];
-int last_press[9];
GLfloat Projection[4][4];
-int BPM_change[2];
-
+SDL_Surface* screen;
+Pads* pads;
#ifdef WIN32
extern "C"
#endif
GL_API int GL_APIENTRY _dgles_load_library(void *, void *(*)(void *, const char *));
-static void *proc_loader(void *h, const char *name)
-{
+static void *proc_loader(void *h, const char *name) {
(void) h;
return SDL_GL_GetProcAddress(name);
}
-int event_filter(const SDL_Event* event) {
- return (int)(event->type == SDL_MOUSEBUTTONDOWN || event->type == SDL_MOUSEBUTTONUP ||
- event->type == SDL_KEYDOWN || event->type == SDL_KEYUP ||
- event->type == SDL_ACTIVEEVENT || event->type == SDL_QUIT);
-}
-
void InitProjection() {
// Create an orthographic projection with screen coordinates
/*
// Lord knows why this works and the above fails.
memset(Projection, 0, sizeof(Projection));
- Projection[0][0] = 2.0 / (GLfloat)screen->w;
- Projection[3][0] = -1.0;
- Projection[1][1] = -2.0 / (GLfloat)screen->h;
+ Projection[0][0] = 2.0f / (GLfloat)screen->w;
+ Projection[3][0] = -1.0f;
+ Projection[1][1] = -2.0f / (GLfloat)screen->h;
Projection[3][1] = 1.0f;
Projection[2][2] = 1.0f;
Projection[3][3] = 1.0f;
}
-void press_button(int channel, int t) {
- Mix_PlayChannel(-1, samples[channel], 0);
- sequencer->set(clock->getSlice() % 16, channel);
- last_press[channel] = t;
-}
-
-void draw(int t) {
- glClear (GL_COLOR_BUFFER_BIT);
-
- for (int i = 0; i < 9; i++)
- button[i]->draw(t);
- beatChart->draw(t);
- bpmDisplay->draw(t, clock->getBPM());
-
- SDL_GL_SwapBuffers();
-}
-
-int main(int argc, char** argv)
-{
+int main(int argc, char** argv) {
// Initialize the SDL library with the Video subsystem
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
atexit(SDL_Quit);
//SDL_SetEventFilter(event_filter);
InitProjection();
- rshader = new RShader();
- rshader->setProjection((GLfloat*)Projection);
- decalshader = new DecalShader();
- decalshader->setProjection((GLfloat*)Projection);
-
- for (int x = 0; x < 3; x++) {
- for (int y = 0; y < 3; y++) {
- int i = y * 3 + x;
- button[i] = new RButton(x * 100 + (x + 1) * 5, y * 100 + (y + 1) * 5, 100, 100);
- button[i]->shader = rshader;
- }
- }
-
- for (int i = 0; i < 9; i++) {
- last_press[i] = 0;
- }
if (Mix_OpenAudio(44100, AUDIO_S16SYS, 1, 1024) == -1) {
printf("Could not initialize audio\n");
int channels = Mix_AllocateChannels(9);
//printf("Allocated %d channels\n", channels);
- samples[0] = Mix_LoadWAV("kits/Lunchbox-Detroit/HHt9.wav");
- samples[1] = Mix_LoadWAV("kits/Lunchbox-Detroit/HHo029.wav");
- samples[2] = Mix_LoadWAV("kits/Lunchbox-Detroit/TT019.wav");
- samples[3] = Mix_LoadWAV("kits/Lunchbox-Detroit/TT139.wav");
- samples[4] = Mix_LoadWAV("kits/Lunchbox-Detroit/SD299.wav");
- samples[5] = Mix_LoadWAV("kits/Lunchbox-Detroit/RIM019.wav");
- samples[6] = Mix_LoadWAV("kits/Lunchbox-Detroit/BD179.wav");
- samples[7] = Mix_LoadWAV("kits/Lunchbox-Detroit/SD019.wav");
- samples[8] = Mix_LoadWAV("kits/Lunchbox-Detroit/CLAP029.wav");
-
- clock = new Clock(128, 4);
- beatChart = new BeatChart(clock);
- beatChart->shader = rshader;
- beatChart->setPosition(0, screen->h - 10);
- sequencer = new Sequencer(4, 4);
- bpmDisplay = new BPMDisplay();
- bpmDisplay->shader = decalshader;
- bpmDisplay->setPosition(5, 325);
-
+ pads = new Pads(screen);
// Event descriptor
SDL_Event Event;
}
while (gotEvent) {
- switch (Event.type) {
- // List of keys that have been pressed
- case SDL_KEYDOWN:
- switch (Event.key.keysym.sym) {
- // Escape forces us to quit the app
- // this is also sent when the user makes a back gesture
- case SDLK_ESCAPE:
- Event.type = SDL_QUIT;
- break;
- case SDLK_SPACE:
- clock->start();
- break;
- case SDLK_q:
- clock->setBPM(clock->getBPM() + 1);
- BPM_change[1] = t;
- break;
- case SDLK_a:
- clock->setBPM(clock->getBPM() - 1);
- BPM_change[0] = t;
- break;
- case SDLK_e:
- button[0]->press();
- press_button(0, t);
- break;
- case SDLK_r:
- button[1]->press();
- press_button(1, t);
- break;
- case SDLK_t:
- button[2]->press();
- press_button(2, t);
- break;
- case SDLK_d:
- button[3]->press();
- press_button(3, t);
- break;
- case SDLK_f:
- button[4]->press();
- press_button(4, t);
- break;
- case SDLK_g:
- button[5]->press();
- press_button(5, t);
- break;
- case SDLK_x:
- button[6]->press();
- press_button(6, t);
- break;
- case SDLK_c:
- button[7]->press();
- press_button(7, t);
- break;
- case SDLK_v:
- button[8]->press();
- press_button(8, t);
- break;
- case SDLK_BACKSPACE:
- sequencer->clearAll();
- break;
- default:
- break;
- }
- break;
-
- case SDL_KEYUP:
- switch (Event.key.keysym.sym) {
- case SDLK_q:
- BPM_change[1] = 0;
- break;
- case SDLK_a:
- BPM_change[0] = 0;
- break;
- case SDLK_e:
- last_press[0] = 0;
- break;
- case SDLK_r:
- last_press[1] = 0;
- break;
- case SDLK_t:
- last_press[2] = 0;
- break;
- case SDLK_d:
- last_press[3] = 0;
- break;
- case SDLK_f:
- last_press[4] = 0;
- break;
- case SDLK_g:
- last_press[5] = 0;
- break;
- case SDLK_x:
- last_press[6] = 0;
- break;
- case SDLK_c:
- last_press[7] = 0;
- break;
- case SDLK_v:
- last_press[8] = 0;
- break;
- }
- break;
-
- case SDL_ACTIVEEVENT:
- if (Event.active.state == SDL_APPACTIVE) {
- paused = !Event.active.gain;
- }
- break;
-
- case SDL_MOUSEBUTTONDOWN:
- for (int i = 0; i < 9; i++) {
- if (button[i]->testHit(Event.button.x, Event.button.y))
- press_button(i, t);
- }
- break;
-
- case SDL_MOUSEBUTTONUP:
- for (int i = 0; i < 9; i++)
- last_press[i] = 0;
- break;
+ if (Event.type == SDL_ACTIVEEVENT) {
+ if (Event.active.state == SDL_APPACTIVE) {
+ paused = !Event.active.gain;
+ }
+ }
- case SDL_QUIT:
- exit(0);
- break;
- }
+ pads->handleEvents(t, Event);
gotEvent = SDL_PollEvent(&Event);
t = SDL_GetTicks();
}
- int tick = clock->tick(t);
- int slice = clock->getSlice();
-
- for (int i = 0; i < 9; i++) {
- if (last_press[i] != 0 && t - last_press[i] > 500) {
- sequencer->clearChannel(i);
- last_press[i] = 0;
- }
- }
-
- if (tick > 0) {
- unsigned int c = sequencer->get(slice % 16);
- if (c != 0) {
- for (int i = 0; i < 9; i++) {
- if (c & 1) {
- button[i]->press();
- Mix_PlayChannel(-1, samples[i], 0);
- }
- c >>= 1;
- }
- }
- }
+ pads->update(t);
// Skip drawing if we drew less than 50ms ago
if (t - last_t < 50)
continue;
last_t = t;
- if (BPM_change[0] != 0 && t - BPM_change[0] > 250) {
- clock->setBPM(clock->getBPM() - 1);
- } else if (BPM_change[1] != 0 && t - BPM_change[1] > 250) {
- clock->setBPM(clock->getBPM() + 1);
- }
-
- for (int i = 0; i < 9; i++)
- button[i]->update(t);
+ pads->draw(t);
- draw(t);
+ SDL_GL_SwapBuffers();
}
return 0;
+#include "Pads.h"
+
+Pads::Pads(SDL_Surface* screen) {
+ this->screen = screen;
+
+ for (int x = 0; x < 3; x++) {
+ for (int y = 0; y < 3; y++) {
+ int i = y * 3 + x;
+ button[i] = new RButton(x * 100 + (x + 1) * 5, y * 100 + (y + 1) * 5, 100, 100);
+ }
+ }
+
+ for (int i = 0; i < 9; i++) {
+ last_press[i] = 0;
+ }
+
+ samples[0] = Mix_LoadWAV("kits/Lunchbox-Detroit/HHt9.wav");
+ samples[1] = Mix_LoadWAV("kits/Lunchbox-Detroit/HHo029.wav");
+ samples[2] = Mix_LoadWAV("kits/Lunchbox-Detroit/TT019.wav");
+ samples[3] = Mix_LoadWAV("kits/Lunchbox-Detroit/TT139.wav");
+ samples[4] = Mix_LoadWAV("kits/Lunchbox-Detroit/SD299.wav");
+ samples[5] = Mix_LoadWAV("kits/Lunchbox-Detroit/RIM019.wav");
+ samples[6] = Mix_LoadWAV("kits/Lunchbox-Detroit/BD179.wav");
+ samples[7] = Mix_LoadWAV("kits/Lunchbox-Detroit/SD019.wav");
+ samples[8] = Mix_LoadWAV("kits/Lunchbox-Detroit/CLAP029.wav");
+
+ clock = new Clock(128, 4);
+ beatChart = new BeatChart(clock);
+ beatChart->setPosition(0, screen->h - 10);
+ sequencer = new Sequencer(4, 4);
+ bpmDisplay = new BPMDisplay();
+ bpmDisplay->setPosition(5, 325);
+
+ BPM_change[0] = BPM_change[1] = 0;
+}
+
+Pads::~Pads() {
+}
+
+void Pads::handleEvents(int t, const SDL_Event& Event) {
+ switch (Event.type) {
+ // List of keys that have been pressed
+ case SDL_KEYDOWN:
+ switch (Event.key.keysym.sym) {
+ // Escape forces us to quit the app
+ // this is also sent when the user makes a back gesture
+ case SDLK_ESCAPE:
+ sequencer->clearAll();
+ break;
+ case SDLK_SPACE:
+ clock->start();
+ break;
+ case SDLK_q:
+ clock->setBPM(clock->getBPM() + 1);
+ BPM_change[1] = t;
+ break;
+ case SDLK_a:
+ clock->setBPM(clock->getBPM() - 1);
+ BPM_change[0] = t;
+ break;
+ case SDLK_e:
+ button[0]->press();
+ press_button(0, t);
+ break;
+ case SDLK_r:
+ button[1]->press();
+ press_button(1, t);
+ break;
+ case SDLK_t:
+ button[2]->press();
+ press_button(2, t);
+ break;
+ case SDLK_d:
+ button[3]->press();
+ press_button(3, t);
+ break;
+ case SDLK_f:
+ button[4]->press();
+ press_button(4, t);
+ break;
+ case SDLK_g:
+ button[5]->press();
+ press_button(5, t);
+ break;
+ case SDLK_x:
+ button[6]->press();
+ press_button(6, t);
+ break;
+ case SDLK_c:
+ button[7]->press();
+ press_button(7, t);
+ break;
+ case SDLK_v:
+ button[8]->press();
+ press_button(8, t);
+ break;
+ }
+ break;
+
+ case SDL_KEYUP:
+ switch (Event.key.keysym.sym) {
+ case SDLK_q:
+ BPM_change[1] = 0;
+ break;
+ case SDLK_a:
+ BPM_change[0] = 0;
+ break;
+ case SDLK_e:
+ last_press[0] = 0;
+ break;
+ case SDLK_r:
+ last_press[1] = 0;
+ break;
+ case SDLK_t:
+ last_press[2] = 0;
+ break;
+ case SDLK_d:
+ last_press[3] = 0;
+ break;
+ case SDLK_f:
+ last_press[4] = 0;
+ break;
+ case SDLK_g:
+ last_press[5] = 0;
+ break;
+ case SDLK_x:
+ last_press[6] = 0;
+ break;
+ case SDLK_c:
+ last_press[7] = 0;
+ break;
+ case SDLK_v:
+ last_press[8] = 0;
+ break;
+ }
+ break;
+
+ case SDL_MOUSEBUTTONDOWN:
+ for (int i = 0; i < 9; i++) {
+ if (button[i]->testHit(Event.button.x, Event.button.y))
+ press_button(i, t);
+ }
+ break;
+
+ case SDL_MOUSEBUTTONUP:
+ for (int i = 0; i < 9; i++)
+ last_press[i] = 0;
+ break;
+
+ case SDL_QUIT:
+ exit(0);
+ break;
+ }
+}
+
+void Pads::update(int t) {
+ int tick = clock->tick(t);
+ int slice = clock->getSlice();
+
+ for (int i = 0; i < 9; i++) {
+ if (last_press[i] != 0 && t - last_press[i] > 500) {
+ sequencer->clearChannel(i);
+ last_press[i] = 0;
+ }
+ }
+
+ if (tick > 0) {
+ unsigned int c = sequencer->get(slice % 16);
+ if (c != 0) {
+ for (int i = 0; i < 9; i++) {
+ if (c & 1) {
+ button[i]->press();
+ Mix_PlayChannel(-1, samples[i], 0);
+ }
+ c >>= 1;
+ }
+ }
+ }
+}
+
+void Pads::draw(int t) {
+ if (BPM_change[0] != 0 && t - BPM_change[0] > 250) {
+ clock->setBPM(clock->getBPM() - 1);
+ } else if (BPM_change[1] != 0 && t - BPM_change[1] > 250) {
+ clock->setBPM(clock->getBPM() + 1);
+ }
+
+ for (int i = 0; i < 9; i++)
+ button[i]->update(t);
+
+ glClear (GL_COLOR_BUFFER_BIT);
+
+ for (int i = 0; i < 9; i++)
+ button[i]->draw(t);
+ beatChart->draw(t);
+ bpmDisplay->draw(t, clock->getBPM());
+}
+
+void Pads::press_button(int channel, int t) {
+ Mix_PlayChannel(-1, samples[channel], 0);
+ sequencer->set(clock->getSlice() % 16, channel);
+ last_press[channel] = t;
+}
\ No newline at end of file
+#ifndef _PADS_H
+#define _PADS_H
+
+#include <SDL.h>
+#include <SDL_mixer.h>
+
+#include "Scene.h"
+#include "RButton.h"
+#include "Clock.h"
+#include "BeatChart.h"
+#include "Sequencer.h"
+#include "BPMDisplay.h"
+
+class Pads : public Scene {
+public:
+ Pads(SDL_Surface* screen);
+ ~Pads();
+
+ virtual void handleEvents(int t, const SDL_Event& Event);
+ virtual void update(int t);
+ virtual void draw(int t);
+private:
+ SDL_Surface* screen;
+ RShader* rshader;
+ DecalShader* decalshader;
+ RButton* button[9];
+ Clock* clock;
+ BeatChart* beatChart;
+ Sequencer* sequencer;
+ BPMDisplay* bpmDisplay;
+ Mix_Chunk* samples[9];
+ int last_press[9];
+ int BPM_change[2];
+
+ void press_button(int channel, int t);
+};
+
+#endif //_PADS_H
\ No newline at end of file
#include <SDL.h>
#include "RButton.h"
+#include "ShaderDepot.h"
RButton::RButton(int x, int y, int w, int h) {
setRect(x, y, w, h);
this->setColor(0.0, 0.0, 1.0);
this->setFadeColor(1.0, 1.0, 1.0);
this->lastPress = 0;
+ this->shader = (RShader*) loadShader("RShader");
}
RButton::~RButton(void) {
+#include "Scene.h"
+
+Scene::Scene() {
+}
+
+Scene::~Scene(void) {
+}
+#ifndef _SCENE_H
+#define _SCENE_H
+
+#include <SDL.h>
+
+class Scene {
+public:
+ Scene(void);
+ ~Scene(void);
+
+ virtual void handleEvents(int t, const SDL_Event& Event) = 0;
+ virtual void update(int t) = 0;
+ virtual void draw(int t) = 0;
+};
+
+#endif //_SCENE_H
\ No newline at end of file
#include <GLES2/gl2.h>
-class Shader
-{
+class Shader {
public:
Shader();
~Shader();
+#include <string.h>
+
+#include "RShader.h"
+#include "DecalShader.h"
+
+extern GLfloat Projection[4][4];
+RShader* rShader;
+DecalShader* decalShader;
+
+Shader* loadShader(const char *name) {
+ if (strncmp(name, "RShader", 8) == 0) {
+ if (rShader == NULL) {
+ rShader = new RShader();
+ rShader->setProjection((GLfloat*)Projection);
+ }
+ return (Shader*) rShader;
+ } else if (strncmp(name, "DecalShader", 12) == 0) {
+ if (decalShader == NULL) {
+ decalShader = new DecalShader();
+ decalShader->setProjection((GLfloat*)Projection);
+ }
+ return (Shader*) decalShader;
+ }
+
+ return NULL;
+}
\ No newline at end of file
+#ifndef _SHADERDEPOT_H
+#define _SHADERDEPOT_H
+
+#include "Shader.h"
+
+Shader* loadShader(const char *name);
+
+#endif /* _SHADERDEPOT_H */
\ No newline at end of file