summaryrefslogtreecommitdiff
path: root/engine
diff options
context:
space:
mode:
Diffstat (limited to 'engine')
-rw-r--r--engine/application.cpp118
-rw-r--r--engine/application.h13
-rw-r--r--engine/stage.cpp116
-rw-r--r--engine/stage.h27
4 files changed, 156 insertions, 118 deletions
diff --git a/engine/application.cpp b/engine/application.cpp
index e814ac2..65b5cb1 100644
--- a/engine/application.cpp
+++ b/engine/application.cpp
@@ -11,70 +11,6 @@
#include <cmath>
-class BulletPattern1 : public BulletPattern {
- unsigned int base;
- public:
- BulletPattern1(bool reverse, unsigned int base) {
- num_bullets = 0;
- color_r = 1;
- color_g = 0;
- color_b = 1;
- bullets = new float[128];
- this->base = base;
-
- int j = 0;
- for(float i = 0; i < M_PI; i += 0.1, j++) {
- bullets[j*4] = 0.5 + sinf(!reverse ? M_PI_2 + i : M_PI - i + M_PI_2) * 0.09;
- bullets[j*4 + 1] = 0.8 + cosf(M_PI_2 + i) * 0.1;
- bullets[j*4 + 2] = sinf(!reverse ? M_PI_2 + i : M_PI - i + M_PI_2) / 15000.0;
- bullets[j*4 + 3] = -0.0001;
- }
- };
- void update(unsigned int time, unsigned int step) {
- if(num_bullets < 32 && base + num_bullets * 10 < time) {
- num_bullets += time / 10 - num_bullets - base / 10;
- if(num_bullets > 32) num_bullets = 32;
- }
- for(int i = 0; i < num_bullets; i++) {
- bullets[i*4] += bullets[i*4 + 2] * step;
- bullets[i*4 + 1] += bullets[i*4 + 3] * step;
- }
- }
-};
-
-class BulletPattern2 : public BulletPattern {
- public:
- BulletPattern2(unsigned int base) {
- num_bullets = 0;
- color_r = 0;
- color_g = 1;
- color_b = 0;
- stride = sizeof(float);
- bullets = new float[8*32*5];
-
- int k = 0;
- for(int j = 0; j < 8; j++) {
- for(float i = 0; i < M_PI; i += 0.1, k++) {
- bullets[k*5] = 0.5 + sinf(j % 2 ? M_PI_2 + i : M_PI - i + M_PI_2) * 0.09;
- bullets[k*5 + 1] = 0.8 + cosf(M_PI_2 + i) * 0.1;
- bullets[k*5 + 2] = sinf(j % 2 ? M_PI_2 + i : M_PI - i + M_PI_2) / 15000;
- bullets[k*5 + 3] = -0.0001;
- bullets[k*5 + 4] = base + j * 400 + i * 100;
- }
- }
-
- };
- void update(unsigned int time, unsigned int step) {
- while(num_bullets < 256 && (unsigned int)(bullets[num_bullets*5 + 4]) < time) {
- num_bullets++;
- }
- for(int i = 0; i < num_bullets; i++) {
- bullets[i*5] += bullets[i*5 + 2] * step;
- bullets[i*5 + 1] += bullets[i*5 + 3] * step;
- }
- }
-};
-
#include <boost/format.hpp>
Application::Application() {
@@ -127,6 +63,9 @@ Application::Application() {
glEnable(GL_POINT_SPRITE);
glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
+ glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
+
glClearColor(0, 0, 0, 0);
glClearDepth(1);
@@ -138,28 +77,11 @@ Application::~Application() {
}
void Application::run() {
-
- elapsed = 0;
paused = false;
font = new FTPixmapFont("fonts/VeraMono.ttf");
font->FaceSize(12);
background = new TextureSDL("textures/background.png");
- texture = new TextureSDL("textures/shot1.png");
- shader = new GLShaderProgram();
- player = new Player();
-
- patterns.push_back(new BulletPattern());
- for(int i = 0; i < 8; i++) {
- patterns.push_back(new BulletPattern1(i % 2 == 1, 5000 + i * 400));
- }
- patterns.push_back(new BulletPattern2(9000));
-
- GLFragmentShader shader1("shaders/bullet_fragment.glsl");
- GLVertexShader shader2("shaders/bullet_vertex.glsl");
- shader->attach(shader1);
- shader->attach(shader2);
- shader->link();
lasttick = SDL_GetTicks();
@@ -178,23 +100,17 @@ void Application::run() {
}
unsigned int tick = SDL_GetTicks();
- unsigned int step = tick - lasttick;
lasttick = tick;
- main_loop(tick, step);
+ main_loop(tick);
//SDL_Delay(10);
}
}
-void Application::main_loop(unsigned int tick, unsigned int step) {
+void Application::main_loop(unsigned int tick) {
if(!paused) {
- elapsed += step;
- for(std::vector<BulletPattern*>::iterator it = patterns.begin(); it < patterns.end(); it++) {
- (*it)->update(elapsed, step);
- }
-
- player->update();
+ stage->update();
}
glClearColor(0, 0, 0, 0);
@@ -259,7 +175,7 @@ void Application::main_loop(unsigned int tick, unsigned int step) {
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
- float f = elapsed * 0.0005;
+ float f = tick * 0.0005;
gluLookAt(
5 * sinf(f), 1, 5 * cosf(f),
@@ -290,26 +206,8 @@ void Application::main_loop(unsigned int tick, unsigned int step) {
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
-
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
-
- player->draw();
-
- shader->use();
- glPointSize(32.0);
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, texture->tex());
-
- glColor4f(1, 0, 0, 1);
-
- for(std::vector<BulletPattern*>::iterator it = patterns.begin(); it < patterns.end(); it++) {
- BulletPattern *bp = (*it);
- glColor3f(bp->color_r, bp->color_g, bp->color_b);
- bp->draw();
- }
- glUseProgram(0);
+ stage->draw();
glDisable(GL_SCISSOR_TEST);
diff --git a/engine/application.h b/engine/application.h
index 4bf9251..3f58f87 100644
--- a/engine/application.h
+++ b/engine/application.h
@@ -5,10 +5,11 @@
#include <FTGL/ftgl.h>
#include <vector>
+#include "stage.h"
+
#include "bulletpattern.h"
#include "shader.h"
#include "texture.h"
-#include "player.h"
class Application {
private:
@@ -17,20 +18,16 @@ class Application {
bool paused;
unsigned int lasttick;
- unsigned int elapsed;
unsigned int frames;
unsigned int lastframes;
float fps;
FTFont* font;
Texture* background;
- Texture* texture;
- GLShaderProgram* shader;
- Player *player;
- std::vector<BulletPattern*> patterns;
+ public:
+ Stage* stage;
- public:
Application();
~Application();
void run();
@@ -39,7 +36,7 @@ class Application {
protected:
virtual void event_keypress(SDLKey key);
- void main_loop(unsigned int tick, unsigned int step);
+ void main_loop(unsigned int tick);
};
#endif
diff --git a/engine/stage.cpp b/engine/stage.cpp
new file mode 100644
index 0000000..59377da
--- /dev/null
+++ b/engine/stage.cpp
@@ -0,0 +1,116 @@
+#include "stage.h"
+
+#include "texturesdl.h"
+
+#include <SDL/SDL_opengl.h>
+#include <cmath>
+
+class BulletPattern1 : public BulletPattern {
+ unsigned int base;
+ public:
+ BulletPattern1(bool reverse, unsigned int base) {
+ num_bullets = 0;
+ color_r = 1;
+ color_g = 0;
+ color_b = 1;
+ bullets = new float[128];
+ this->base = base;
+
+ int j = 0;
+ for(float i = 0; i < M_PI; i += 0.1, j++) {
+ bullets[j*4] = 0.5 + sinf(!reverse ? M_PI_2 + i : M_PI - i + M_PI_2) * 0.09;
+ bullets[j*4 + 1] = 0.8 + cosf(M_PI_2 + i) * 0.1;
+ bullets[j*4 + 2] = sinf(!reverse ? M_PI_2 + i : M_PI - i + M_PI_2) / 15000.0;
+ bullets[j*4 + 3] = -0.0001;
+ }
+ };
+ void update(unsigned int time, unsigned int step) {
+ if(num_bullets < 32 && base + num_bullets * 10 < time) {
+ num_bullets += time / 10 - num_bullets - base / 10;
+ if(num_bullets > 32) num_bullets = 32;
+ }
+ for(int i = 0; i < num_bullets; i++) {
+ bullets[i*4] += bullets[i*4 + 2] * step;
+ bullets[i*4 + 1] += bullets[i*4 + 3] * step;
+ }
+ }
+};
+
+class BulletPattern2 : public BulletPattern {
+ public:
+ BulletPattern2(unsigned int base) {
+ num_bullets = 0;
+ color_r = 0;
+ color_g = 1;
+ color_b = 0;
+ stride = sizeof(float);
+ bullets = new float[8*32*5];
+
+ int k = 0;
+ for(int j = 0; j < 8; j++) {
+ for(float i = 0; i < M_PI; i += 0.1, k++) {
+ bullets[k*5] = 0.5 + sinf(j % 2 ? M_PI_2 + i : M_PI - i + M_PI_2) * 0.09;
+ bullets[k*5 + 1] = 0.8 + cosf(M_PI_2 + i) * 0.1;
+ bullets[k*5 + 2] = sinf(j % 2 ? M_PI_2 + i : M_PI - i + M_PI_2) / 15000;
+ bullets[k*5 + 3] = -0.0001;
+ bullets[k*5 + 4] = base + j * 400 + i * 100;
+ }
+ }
+
+ };
+ void update(unsigned int time, unsigned int step) {
+ while(num_bullets < 256 && (unsigned int)(bullets[num_bullets*5 + 4]) < time) {
+ num_bullets++;
+ }
+ for(int i = 0; i < num_bullets; i++) {
+ bullets[i*5] += bullets[i*5 + 2] * step;
+ bullets[i*5 + 1] += bullets[i*5 + 3] * step;
+ }
+ }
+};
+
+Stage::Stage() {
+ player = new Player();
+
+ texture = new TextureSDL("textures/shot1.png");
+ shader = new GLShaderProgram();
+
+ patterns.push_back(new BulletPattern());
+ for(int i = 0; i < 8; i++) {
+ patterns.push_back(new BulletPattern1(i % 2 == 1, 5000 + i * 400));
+ }
+ patterns.push_back(new BulletPattern2(9000));
+
+ GLFragmentShader shader1("shaders/bullet_fragment.glsl");
+ GLVertexShader shader2("shaders/bullet_vertex.glsl");
+ shader->attach(shader1);
+ shader->attach(shader2);
+ shader->link();
+}
+
+void Stage::update() {
+ player->update();
+
+ static int i;
+
+ for(std::vector<BulletPattern*>::iterator it = patterns.begin(); it < patterns.end(); it++) {
+ (*it)->update(i++, 1);
+ }
+}
+
+void Stage::draw() {
+ player->draw();
+
+ shader->use();
+ glPointSize(32.0);
+ glEnable(GL_TEXTURE_2D);
+ glBindTexture(GL_TEXTURE_2D, texture->tex());
+
+ for(std::vector<BulletPattern*>::iterator it = patterns.begin(); it < patterns.end(); it++) {
+ BulletPattern *bp = (*it);
+ glColor3f(bp->color_r, bp->color_g, bp->color_b);
+ bp->draw();
+ }
+
+ glUseProgram(0);
+} \ No newline at end of file
diff --git a/engine/stage.h b/engine/stage.h
new file mode 100644
index 0000000..4972795
--- /dev/null
+++ b/engine/stage.h
@@ -0,0 +1,27 @@
+#ifndef STAGE_H
+#define STAGE_H
+
+#include "player.h"
+#include "shader.h"
+#include "texture.h"
+#include "bulletpattern.h"
+
+#include <vector>
+
+class Stage {
+ private:
+ Player* player;
+
+ GLShaderProgram* shader;
+ Texture* texture;
+
+ std::vector<BulletPattern*> patterns;
+
+ public:
+ Stage();
+
+ void update();
+ void draw();
+};
+
+#endif