diff --git a/game.hpp b/game.hpp index dc9c1a6..f6be04b 100644 --- a/game.hpp +++ b/game.hpp @@ -152,6 +152,7 @@ void dropShadowPiece(); { viewer = v; } + int numBlocksCleared; private: bool doesPieceFit(const Piece& p, int x, int y) const; @@ -181,7 +182,7 @@ void dropShadowPiece(); // Extra stuff int score_, linesCleared_; - int numBlocksCleared; + int numDeleted; double clearBarPos; Viewer *viewer; diff --git a/head.txt b/head.txt new file mode 100644 index 0000000..6f6d662 --- /dev/null +++ b/head.txt @@ -0,0 +1,46 @@ +1 2 1 0.88887 0.850980392 0.835294118 HEAD +8 5 0 4 4 0 0 0 0 2 +8 5 0 4 4 0 0 0 0 2 + +1 2 1 0.88887 0.850980392 0.835294118 LEYE +7 4 0.2 0.4 1.6 0 0 0 0 2 +7 4 0.2 0.4 1.6 0 0 0 0 2 + +1 2 1 0.88887 0.850980392 0.835294118 REYE +9 4 0.2 0.4 1.6 0 0 0 0 2 +9 4 0.2 0.4 1.6 0 0 0 0 2 + +1 3 1 0.88887 0.850980392 0.835294118 MOUTH +8 2 0.2 0.5 0.5 0 0 0 0 4 +8 2 0.2 0.7 0.3 0 0 0 0 5 +8 2 0.2 0.5 0.5 0 0 0 0 4 + +1 2 1 1 1 1 FACE +8 3 0.1 2 1.8 0 0 0 0 2 +8 3 0.1 2 1.8 0 0 0 0 2 + +1 9 1 1 1 1 RPUPIL +8.8 2.9 0.2 0.1 0.1 0 0 0 0 2 +8.9 3.1 0.2 0.08 0.12 0 0 0 0 2 +9 3.2 0.2 0.06 0.14 0 0 0 0 2 +9.1 3.1 0.2 0.08 0.12 0 0 0 0 2 +9.2 2.9 0.2 0.1 0.1 0 0 0 0 2 +9.1 3.1 0.2 0.08 0.12 0 0 0 0 2 +9 3.2 0.2 0.06 0.14 0 0 0 0 2 +8.9 3.1 0.2 0.08 0.12 0 0 0 0 2 +8.8 2.9 0.2 0.1 0.1 0 0 0 0 2 + +1 9 1 1 1 1 LPUPIL +6.8 2.9 0.2 0.1 0.1 0 0 0 0 2 +6.9 3.1 0.2 0.08 0.12 0 0 0 0 2 +7 3.2 0.2 0.06 0.14 0 0 0 0 2 +7.1 3.1 0.2 0.08 0.12 0 0 0 0 2 +7.2 2.9 0.2 0.1 0.1 0 0 0 0 2 +7.1 3.1 0.2 0.08 0.12 0 0 0 0 2 +7 3.2 0.2 0.06 0.14 0 0 0 0 2 +6.9 3.1 0.2 0.08 0.12 0 0 0 0 2 +6.8 2.9 0.2 0.1 0.1 0 0 0 0 2 + +0 2 1 0.88887 0.850980392 0.835294118 RECT +5 3.3 0.3 5 3.3 1 0 0 0 2 +5 3.3 0.3 5 3.3 1 0 0 0 2 \ No newline at end of file diff --git a/headHappy.txt b/headHappy.txt new file mode 100644 index 0000000..ae5bf2a --- /dev/null +++ b/headHappy.txt @@ -0,0 +1,87 @@ +1 9 0 0.88887 0.850980392 0.835294118 HEAD +8 5 0 4 4 0 0 0 0 5 +8 5 0 6 6 0 0 0 0 5 +7 6 0 6 6 0 0 0 0 5 +6 5 0 6 6 0 0 0 0 5 +7 6 0 6 6 0 0 0 0 5 +8 5 0 6 6 0 0 0 0 5 +9 6 0 6 6 0 0 0 0 5 +10 5 0 6 6 0 0 0 0 5 +8 5 0 4 4 0 0 0 0 5 + +1 9 0 0.88887 0.850980392 0.835294118 LEYE +7 4 0.2 0.4 1.6 0 0 0 0 5 +7 4 0.2 0.6 2.4 0 0 0 0 5 +6 5 0.2 0.6 2.4 0 0 0 0 5 +5 4 0.2 0.6 2.4 0 0 0 0 5 +6 5 0.2 0.6 2.4 0 0 0 0 5 +7 4 0.2 0.6 2.4 0 0 0 0 5 +8 5 0.2 0.6 2.4 0 0 0 0 5 +9 4 0.2 0.6 2.4 0 0 0 0 5 +7 4 0.2 0.4 1.6 0 0 0 0 5 + +1 9 0 0.88887 0.850980392 0.835294118 REYE +9 4 0.2 0.4 1.6 0 0 0 0 5 +9 4 0.2 0.6 2.4 0 0 0 0 5 +8 5 0.2 0.6 2.4 0 0 0 0 5 +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1 0.1 0 0 0 0 5 +6.8 2.9 0.2 0.15 0.15 0 0 0 0 5 +5.8 3.9 0.2 0.15 0.15 0 0 0 0 5 +4.8 2.9 0.2 0.15 0.15 0 0 0 0 5 +5.8 3.9 0.2 0.15 0.15 0 0 0 0 5 +6.8 2.9 0.2 0.15 0.15 0 0 0 0 5 +7.8 3.9 0.2 0.15 0.15 0 0 0 0 5 +8.6 2.9 0.2 0.15 0.15 0 0 0 0 5 +6.8 2.9 0.2 0.1 0.1 0 0 0 0 5 + +0 9 0 0.88887 0.850980392 0.835294118 RECT +5 3.3 0.3 5 3.3 1 0 0 0 5 +4.9 3.3 0.3 7.5 4.95 1 0 0 0 5 +3.9 4.3 0.3 7.5 4.95 1 0 0 0 5 +2.9 3.3 0.3 7.5 4.95 1 0 0 0 5 +3.9 4.3 0.3 7.5 4.95 1 0 0 0 5 +4.9 3.3 0.3 7.5 4.95 1 0 0 0 5 +5.9 4.3 0.3 7.5 4.95 1 0 0 0 5 +6.9 3.3 0.3 7.5 4.95 1 0 0 0 5 +5 3.3 0.3 5 3.3 1 0 0 0 5 \ No newline at end of file diff --git a/headSad.txt b/headSad.txt new file mode 100644 index 0000000..1688bed --- /dev/null +++ b/headSad.txt @@ -0,0 +1,43 @@ +1 2 1 0.88887 0.850980392 0.835294118 HEAD +8 5 0 4 4 0 0 0 0 2 +8 5 0 4 4 0 0 0 0 2 + +1 2 1 0.88887 0.850980392 0.835294118 LEYE +7 4 0.2 0.4 1.6 0 0 0 0 2 +7 4 0.2 0.4 1.6 0 0 0 0 2 + +1 2 1 0.88887 0.850980392 0.835294118 REYE +9 4 0.2 0.4 1.6 0 0 0 0 2 +9 4 0.2 0.4 1.6 0 0 0 0 2 + +1 2 1 0.88887 0.850980392 0.835294118 MOUTH +8 2 0.2 0.5 0.5 0 0 0 0 2 +8 2 0.2 0.5 0.5 0 0 0 0 2 + +1 2 1 1 1 1 FACE +8 3 0.1 2 1.8 0 0 0 0 2 +8 3 0.1 2 1.8 0 0 0 0 2 + +1 2 1 1 1 1 RPUPIL +9.0 2.9 0.2 0.1 0.1 0 0 0 0 2 +9.0 2.9 0.2 0.1 0.1 0 0 0 0 2 + +1 2 1 1 1 1 LPUPIL +7.0 2.9 0.2 0.1 0.1 0 0 0 0 2 +7.0 2.9 0.2 0.1 0.1 0 0 0 0 2 + +0 2 1 0.88887 0.850980392 0.835294118 RECT +5 3.3 0.3 5 3.3 1 0 0 0 2 +5 3.3 0.3 5 3.3 1 0 0 0 2 + +1 2 1 0.88887 0.850980392 0.835294118 LTEAR +9.2 2.9 0.2 0.1 0.17 0 0 0 0 10 +9.2 1.5 0.2 0.07 0.2 0 0 0 0 10 + +1 2 1 0.88887 0.850980392 0.835294118 RTEAR +6.7 2.9 0.2 0.07 0.2 0 0 0 0 10 +6.7 1.5 0.2 0.07 0.2 0 0 0 0 10 + +1 2 1 1 1 1 MAKESADFACE +8 1.8 0.21 0.72 0.32 0 0 0 0 2 +8 1.8 0.21 0.72 0.32 0 0 0 0 2 \ No newline at end of file diff --git a/viewer.cpp b/viewer.cpp index 1ae2350..a300dde 100644 --- a/viewer.cpp +++ b/viewer.cpp @@ -1,6 +1,7 @@ #include "viewer.hpp" #include #include +#include #include #include #include @@ -10,6 +11,7 @@ #define DEFAULT_GAME_SPEED 50 #define WIDTH 16 #define HEIGHT 10 +using namespace std; Viewer::Viewer() { @@ -148,6 +150,8 @@ void Viewer::on_realize() // Sphere for particles particleSphere = gluNewQuadric(); + + readFile("head.txt"); glClearDepth (1.0f); @@ -262,7 +266,7 @@ bool Viewer::on_expose_event(GdkEventExpose* event) // 10 and height 24 (game = 20, stripe = 4). Let's translate // the game so that we can draw it starting at (0,0) but have // it appear centered in the window. - glTranslated(-5.0, -10.0, 10.0); + glTranslated(-7.5, -10.0, 7.0); // Create one light source glEnable(GL_LIGHTING); @@ -293,79 +297,14 @@ bool Viewer::on_expose_event(GdkEventExpose* event) // just drew. This should only be done if double buffering is enabled. if (doubleBuffer) gldrawable->swap_buffers(); -/* else - glFlush(); */ + else + glFlush(); gldrawable->gl_end(); return true; } - -/* glColorMaterial(GL_FRONT, GL_AMBIENT); - drawFloor(); -// drawReflections(); - drawScene(0); -// drawGrid(); -// drawParticles(); -// drawBar(); - - glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); - glDepthMask(GL_FALSE); - glEnable(GL_CULL_FACE); - glEnable(GL_STENCIL_TEST); -// glEnable(GL_POLYGON_OFFSET_FILL); -// glPolygonOffset(0.0f, 100.0f); - - glCullFace(GL_FRONT); - glStencilFunc(GL_ALWAYS, 0x0, 0xffffff); - glStencilOp(GL_KEEP, GL_KEEP, GL_INCR); - drawShadowVolumes(); - - glCullFace(GL_BACK); - glStencilFunc(GL_ALWAYS, 0x0, 0xffffff); - glStencilOp(GL_KEEP, GL_KEEP, GL_DECR); - drawShadowVolumes(); - -// glDisable(GL_POLYGON_OFFSET_FILL); - glDisable(GL_CULL_FACE); - - glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); - glDepthMask(GL_TRUE); - glStencilFunc(GL_EQUAL, 0x0, 0xffffff); - glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); - glEnable(GL_LIGHTING); - glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); - drawFloor(); - //drawReflections(); - drawScene(0); - //drawGrid(); - //drawParticles(); - //drawBar(); - glDisable(GL_STENCIL_TEST); - silhouette.clear();*/ - - -/* glDisable(GL_DEPTH_TEST); - glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); - - glEnable(GL_STENCIL_TEST); - glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); - glStencilFunc(GL_ALWAYS, 1, 0xffffffff); - drawFloor(); - - glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); - glEnable(GL_DEPTH_TEST); - - glStencilFunc(GL_EQUAL, 1, 0xffffffff); - glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); - - drawShadowVolumes(); - silhouette.clear(); - drawFloor(); - - glDisable(GL_STENCIL_TEST); -*/ if (!motionBlur) { drawBackground(); @@ -375,6 +314,9 @@ bool Viewer::on_expose_event(GdkEventExpose* event) drawGrid(); drawParticles(); drawBar(); + glDisable(GL_LIGHTING); + drawAnimatables(); + glEnable(GL_LIGHTING); if (levelUpAnimation) { @@ -418,39 +360,6 @@ bool Viewer::on_expose_event(GdkEventExpose* event) gluSphere(quadratic,1.3f,32,32); glPopMatrix(); - - - -// drawShadowVolumes(); - - -/* drawReflections(); - drawScene(0); - drawGrid(); - drawParticles(); - drawBar();*/ - - /* - Semi-broken motion blur. need to figure out why the screen gets so dim and why it is so slow - if (game->counter == 0) - { - pauseGame(); - drawScene(0); - glAccum(GL_LOAD, 1.f); -// glClear(GL_ACCUM_BUFFER_BIT); - drawFallingBox(); - glAccum(GL_RETURN, 1.f); - - pauseGame(); - } - else - { - drawScene(0); - } - */ - - - // We pushed a matrix onto the PROJECTION stack earlier, we // need to pop it. @@ -469,6 +378,69 @@ bool Viewer::on_expose_event(GdkEventExpose* event) return true; } +void Viewer::drawAnimatables() +{ + glPushMatrix(); + glTranslatef(16, 3, 0); + glScalef(0.5, 0.5, 0.5); + Point3D frame; + float *scale; + float *rotate; + float *col; + int shapeType; + for (int i = 0;iset_text("Deleted:\t" + linesStream.str()); } + + if (game->getClearBarPos() >= WIDTH && game->numBlocksCleared > 5) + { + animatables.clear(); + readFile("headHappy.txt"); + } + else if (animatables.size() == 0) + { + readFile("head.txt"); + } + if (game->getLinesCleared() / 10 > (DEFAULT_GAME_SPEED - gameSpeed) / 50 && gameSpeed > 75) { // Increase the game speed @@ -1831,14 +1814,23 @@ bool Viewer::gameTick() if (returnVal < 0) { gameOver = true; + animatables.clear(); + readFile("headSad.txt"); tickTimer.disconnect(); std::cerr << "Boo!"; + gameOverAnimTimer = Glib::signal_timeout().connect(sigc::mem_fun(*this, &Viewer::forceRender), gameSpeed); } invalidate(); return true; } +bool Viewer::forceRender() +{ + invalidate(); + return true; +} + void Viewer::resetView() { // Reset all the rotations and scale factor @@ -1857,6 +1849,9 @@ void Viewer::resetView() void Viewer::newGame() { + gameOverAnimTimer.disconnect(); + animatables.clear(); + readFile("head.txt"); gameOver = false; game->reset(); @@ -2213,4 +2208,93 @@ void Viewer::toggleMoveLightSource() void Viewer::toggleMotionBlur() { motionBlur = !motionBlur; -} \ No newline at end of file +} +void Viewer::readFile(char *filename) +{ + double xPos, yPos, zPos; + double xScale, yScale, zScale; + int timeTillNextKeyframe; + int shapeType, numKeyFrames; + int numIntermediaryFrames, loop; + ifstream partsFile(filename); + std::string name; + if (partsFile.is_open() ) + { + while (! partsFile.eof() ) + { + float *col = (float *)malloc(sizeof(float) * 3); + partsFile >> shapeType; + partsFile >> numKeyFrames; + partsFile >> loop; + partsFile >> col[0]; + partsFile >> col[1]; + partsFile >> col[2]; + getline (partsFile, name); + for (int i = 0;i> xPos; + partsFile >> yPos; + partsFile >> zPos; + + partsFile >> scaleFactor[0]; + partsFile >> scaleFactor[1]; + partsFile >> scaleFactor[2]; + + partsFile >> rotateFactor[0]; + partsFile >> rotateFactor[1]; + partsFile >> rotateFactor[2]; + + partsFile >> numIntermediaryFrames; + Animatable anim; + if (i > 0) + { + + Point3D finalPos(xPos, yPos, zPos); + Point3D startPos = animatables.back().frames.back(); + float *startScale = animatables.back().scales.back(); + float *startRotate = animatables.back().rotates.back(); + + int n = 30; + float nFrac = 1.f/numIntermediaryFrames; + Vector3D diff = finalPos - startPos; + for (int j = 0;j<=numIntermediaryFrames;j++) + { + animatables.back().frames.push_back(startPos + j * nFrac * diff ); + + float *diffScale = (float *)malloc(sizeof(float) * 3); + float *diffRotate = (float *)malloc(sizeof(float) * 3); + diffScale[0] = startScale[0] + j * nFrac * (scaleFactor[0] - startScale[0]); + diffScale[1] = startScale[1] + j * nFrac * (scaleFactor[1] - startScale[1]); + diffScale[2] = startScale[2] + j * nFrac * (scaleFactor[2] - startScale[2]); + animatables.back().scales.push_back(diffScale); + + + diffRotate[0] = startRotate[0] + j * nFrac * (rotateFactor[0] - startRotate[0]); + diffRotate[1] = startRotate[1] + j * nFrac * (rotateFactor[1] - startRotate[1]); + diffRotate[2] = startRotate[2] + j * nFrac * (rotateFactor[2] - startRotate[2]); + + animatables.back().rotates.push_back(diffRotate); + } + } + else + { + anim.frames.push_back(Point3D(xPos, yPos, zPos)); + anim.scales.push_back(scaleFactor); + anim.rotates.push_back(rotateFactor); + anim.shapeType = shapeType; + if (loop) + anim.loop = true; + else + anim.loop = false; + anim.col = col; + animatables.push_back(anim); + } + + getline (partsFile, name); + } + getline (partsFile, name); + } + } +} diff --git a/viewer.hpp b/viewer.hpp index 15c1ab4..6cdbb29 100644 --- a/viewer.hpp +++ b/viewer.hpp @@ -83,7 +83,9 @@ class Viewer : public Gtk::GL::DrawingArea { // Bump mapping stuff int GenNormalizationCubeMap(unsigned int size, GLuint &texid); - + void readFile(char *filename); + bool forceRender(); + protected: // Events we implement @@ -121,9 +123,10 @@ class Viewer : public Gtk::GL::DrawingArea { void drawReflections(); void drawMoveBlur(int side); // 0 = right | 1 = left void drawBackground(); - + void drawAnimatables(); void drawCube(float y, float x, int colourId, GLenum mode, bool draw3D = true); void drawBumpCube(float y, float x, int colourId, bool draw3D = true); + DrawMode currentDrawMode; // The angle at which we are currently rotated @@ -151,7 +154,7 @@ class Viewer : public Gtk::GL::DrawingArea { bool doubleBuffer; // Timer used to call the tick method - sigc::connection tickTimer, clearBarTimer; + sigc::connection tickTimer, clearBarTimer, gameOverAnimTimer; // Timer for rotate sigc::connection rotateTimer; @@ -208,6 +211,17 @@ class Viewer : public Gtk::GL::DrawingArea { bool motionBlur; bool levelUpAnimation; GLUquadricObj *particleSphere; + + struct Animatable{ + std::vector frames; + std::vector scales; + std::vector rotates; + int shapeType; + bool loop; + float *col; + }; + + std::vector animatables; }; #endif