summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVegard Storheil Eriksen <zyp@jvnv.net>2010-05-23 22:10:50 +0200
committerVegard Storheil Eriksen <zyp@jvnv.net>2010-05-23 22:10:50 +0200
commitf2d3072643e4140ef3dd144b7509eda8d4a323d6 (patch)
treee85cb04d777fc0a6b6d3697ae1b8879cccd68d32
parent9620ed1c2c0c40400b638743b19462dfdc6b0c55 (diff)
Made bullet-lists configurable by template parameter.
-rw-r--r--engine/bulletpattern.cpp74
-rw-r--r--engine/bulletpattern.h39
2 files changed, 56 insertions, 57 deletions
diff --git a/engine/bulletpattern.cpp b/engine/bulletpattern.cpp
index f73ced5..195caf9 100644
--- a/engine/bulletpattern.cpp
+++ b/engine/bulletpattern.cpp
@@ -1,50 +1,32 @@
-#ifndef __APPLE__
-#include <GL/gl.h>
-#else
-#include <OpenGL/gl.h>
-#endif
#include <cmath>
#include "bulletpattern.h"
-void BulletPattern::draw() {
- glEnableClientState(GL_VERTEX_ARRAY);
-
- glVertexPointer(4, GL_FLOAT, 4 * sizeof(float) + stride, bullets);
-
- glDrawArrays(GL_POINTS, 0, num_bullets);
-
- glDisableClientState(GL_VERTEX_ARRAY);
-
-}
-
BulletPattern1::BulletPattern1(const Vector2& initpos) {
num_bullets = steps = 0;
color_r = 0;
color_g = 1;
color_b = 0;
- stride = sizeof(float);
- bullets = new float[8*32*5];
+ bullets = new Bullet[8*32];
int k = 0;
for(int j = 0; j < 8; j++) {
for(float i = 0; i < M_PI; i += 0.1, k++) {
- bullets[k*5] = initpos.x + sinf(j % 2 ? M_PI_2 + i : M_PI - i + M_PI_2) * 0.09;
- bullets[k*5 + 1] = initpos.y + cosf(M_PI_2 + i) * 0.1;
- bullets[k*5 + 2] = sinf(j % 2 ? M_PI_2 + i : M_PI - i + M_PI_2) / 1000;
- bullets[k*5 + 3] = -0.002;
- bullets[k*5 + 4] = j * 24 + i * 6;
+ bullets[k].pos.x = initpos.x + sinf(j % 2 ? M_PI_2 + i : M_PI - i + M_PI_2) * 0.09;
+ bullets[k].pos.y = initpos.y + cosf(M_PI_2 + i) * 0.1;
+ bullets[k].dir.x = sinf(j % 2 ? M_PI_2 + i : M_PI - i + M_PI_2) / 1000;
+ bullets[k].dir.y = -0.002;
+ bullets[k].data = j * 24 + i * 6;
}
}
};
void BulletPattern1::update() {
steps++;
- while(num_bullets < 256 && (unsigned int)(bullets[num_bullets*5 + 4]) < steps) {
+ while(num_bullets < 256 && (unsigned int)(bullets[num_bullets].data) < steps) {
num_bullets++;
}
for(int i = 0; i < num_bullets; i++) {
- bullets[i*5] += bullets[i*5 + 2];
- bullets[i*5 + 1] += bullets[i*5 + 3];
+ bullets[i].pos += bullets[i].dir;
}
}
@@ -53,48 +35,45 @@ BulletPattern2::BulletPattern2(const Vector2& initpos) {
color_r = 0.3;
color_g = 0.3;
color_b = 1.0;
- stride = sizeof(float)*2;
- bullets = new float[24*16*6];
+ bullets = new Bullet[24*16];
int k = 0;
for(int j = 0; j < 24; j++) {
for(float i = 0; i < 2 * M_PI; i += 0.4, k++) {
- bullets[k*6] = initpos.x;
- bullets[k*6+1] = initpos.y;
- bullets[k*6+2] = cosf(i) / 40;
- bullets[k*6+3] = sinf(i) / 40;
- bullets[k*6+4] = j * 10;
- bullets[k*6+5] = i;
+ bullets[k].pos = initpos;
+ bullets[k].dir.x = cosf(i) / 40;
+ bullets[k].dir.y = sinf(i) / 40;
+ bullets[k].data.a = j * 10;
+ bullets[k].data.b = i;
}
}
};
void BulletPattern2::update() {
steps++;
- while(num_bullets < 384 && (unsigned int)(bullets[num_bullets*6 + 4]) < steps) {
+ while(num_bullets < 384 && (unsigned int)(bullets[num_bullets].data.a) < steps) {
num_bullets++;
}
for(int i = 0; i < num_bullets; i++) {
- float l = bullets[i*6 + 5] - log1pf((steps - bullets[i*6+4]));
- bullets[i*6+2] = cosf(l) / 500;
- bullets[i*6+3] = sinf(l) / 500;
- bullets[i*6] += bullets[i*6 + 2];
- bullets[i*6+1] += bullets[i*6 + 3];
+ float l = bullets[i].data.b - log1pf((steps - bullets[i].data.a));
+ bullets[i].dir.x = cosf(l) / 500;
+ bullets[i].dir.y = sinf(l) / 500;
+ bullets[i].pos += bullets[i].dir;
}
}
BulletPattern3::BulletPattern3(const Vector2& initpos) {
- num_bullets = stride = steps = color_g = color_b = 0;
+ num_bullets = steps = color_g = color_b = 0;
color_r = 1;
- bullets = new float[2048];
+ bullets = new Bullet[512];
int k = 0;
for(float i = 0; i < M_PI * 16; i += 0.1) {
- bullets[k++] = initpos.x + cosf(i) * 0.05;
- bullets[k++] = initpos.y + sinf(i) * 0.05;
- bullets[k++] = cosf(i) / 1000.0;
- bullets[k++] = sinf(i) / 1000.0;
+ bullets[k].pos.x = initpos.x + cosf(i) * 0.05;
+ bullets[k].pos.y = initpos.y + sinf(i) * 0.05;
+ bullets[k].dir.x = cosf(i) / 1000.0;
+ bullets[k++].dir.y = sinf(i) / 1000.0;
}
}
@@ -104,7 +83,6 @@ void BulletPattern3::update() {
num_bullets++;
}
for(int i = 0; i < num_bullets; i++) {
- bullets[i*4] += bullets[i*4 + 2];
- bullets[i*4 + 1] += bullets[i*4 + 3];
+ bullets[i].pos += bullets[i].dir;
}
}
diff --git a/engine/bulletpattern.h b/engine/bulletpattern.h
index 8df7945..f550c2c 100644
--- a/engine/bulletpattern.h
+++ b/engine/bulletpattern.h
@@ -2,33 +2,54 @@
#define BULLETPATTERN_H
#include "vector.h"
+#include "struct.h"
+
+#include <SDL/SDL_opengl.h>
class BulletPattern {
- protected:
- float* bullets;
- unsigned int num_bullets;
- unsigned int stride;
- unsigned int steps;
public:
float color_r, color_g, color_b;
virtual void update() = 0;
- void draw();
+ virtual void draw() = 0;
+};
+
+template<class Data>
+class BulletPatternImpl : public BulletPattern {
+ protected:
+ struct Bullet {
+ Vector2 pos;
+ Vector2 dir;
+ Data data;
+ };
+
+ Bullet* bullets;
+
+ unsigned int num_bullets;
+ unsigned int steps;
+
+ public:
+ void draw() {
+ glEnableClientState(GL_VERTEX_ARRAY);
+ glVertexPointer(4, GL_FLOAT, sizeof(*bullets), bullets);
+ glDrawArrays(GL_POINTS, 0, num_bullets);
+ glDisableClientState(GL_VERTEX_ARRAY);
+ }
};
-class BulletPattern1 : public BulletPattern {
+class BulletPattern1 : public BulletPatternImpl<float> {
public:
BulletPattern1(const Vector2& initpos);
void update();
};
-class BulletPattern2 : public BulletPattern {
+class BulletPattern2 : public BulletPatternImpl<Struct<float, float> > {
public:
BulletPattern2(const Vector2& initpos);
void update();
};
-class BulletPattern3 : public BulletPattern {
+class BulletPattern3 : public BulletPatternImpl<Struct<> > {
public:
BulletPattern3(const Vector2& initpos);
void update();