Commits

Nikola Cvetko committed 4b4dc7b

Explosion, srand().

Comments (0)

Files changed (6)

 		<Option compiler="gcc" />
 		<Build>
 			<Target title="Debug">
-				<Option output="bin\Debug\Pucaljka" prefix_auto="1" extension_auto="1" />
-				<Option object_output="obj\Debug\" />
+				<Option output="bin/Debug/Pucaljka" prefix_auto="1" extension_auto="1" />
+				<Option object_output="obj/Debug/" />
 				<Option type="1" />
 				<Option compiler="gcc" />
 				<Compiler>
 				</Compiler>
 			</Target>
 			<Target title="Release">
-				<Option output="bin\Release\Pucaljka" prefix_auto="1" extension_auto="1" />
-				<Option object_output="obj\Release\" />
+				<Option output="bin/Release/Pucaljka" prefix_auto="1" extension_auto="1" />
+				<Option object_output="obj/Release/" />
 				<Option type="1" />
 				<Option compiler="gcc" />
 				<Compiler>
 		<Unit filename="button.h" />
 		<Unit filename="enemyship.cpp" />
 		<Unit filename="enemyship.h" />
+		<Unit filename="explosion.cpp" />
+		<Unit filename="explosion.h" />
 		<Unit filename="gamemode.cpp" />
 		<Unit filename="gamemode.h" />
 		<Unit filename="gameobject.cpp" />
+#include "explosion.h"
+#include "texture_manager.h"
+
+Explosion::Explosion(const std::string& textureFile,
+                     const double argX,
+                     const double argY) : texture(TextureManager::getInstance()->getTexture(textureFile)),
+                                            x(argX), y(argY), t(0)
+{
+    this->texture->retain();
+}
+Explosion::~Explosion()
+{
+    this->texture->release();
+}
+void
+Explosion::calcTexCoords(int spriteOffX, int spriteOffY,
+                         float wImg, float hImg,
+                         int wSprite, int hSprite,
+                         int animation, int frame,
+                         float *txTop, float *txBottom, float *txLeft, float *txRight)
+{
+    int animX = animation % 3, animY = animation / 3;
+    int pxAnimX = animX * wSprite * 4, pxAnimY = animY * hSprite * 4;
+
+    int frameX = frame % 4, frameY = frame / 4;
+    int pxFrameX = frameX * wSprite, pxFrameY = frameY * hSprite;
+
+    int pxLeft = spriteOffX + pxAnimX + pxFrameX;
+    int pxRight = pxLeft + wSprite;
+    int pxTop = spriteOffY + pxAnimY + pxFrameY;
+    int pxBottom = pxTop + hSprite;
+
+    //printf("PX L: %d R: %d T: %d B: %d\n", pxLeft, pxRight, pxTop, pxBottom);
+
+    float left = pxLeft / (float)wImg;
+    float right = pxRight / (float)wImg;
+    float top = pxTop / (float)hImg;
+    float bottom = pxBottom / (float)hImg;
+
+    //printf("TX L: %g R: %g T: %g B: %g\n", left, right, top, bottom);
+
+    *txLeft = left;
+    *txRight = right;
+    *txTop = top;
+    *txBottom = bottom;
+}
+
+void
+Explosion::paintSelf()
+{
+    if(t >= 1)
+        return;
+
+    ///////////
+
+    float txTop = 0, txBottom = 0, txLeft = 0, txRight = 0;
+
+    calcTexCoords(0, 0, 128, 128, 32, 32, 0, t * 16, &txLeft, &txRight, &txTop, &txBottom);
+
+    this->texture->bind();
+    glEnable(GL_TEXTURE_2D);
+
+    glMatrixMode(GL_MODELVIEW);
+    glPushMatrix();
+    glTranslatef(this->x, this->y, 0);
+    glScalef(50, 50, 1);
+    glBegin(GL_TRIANGLES);
+
+
+    glTexCoord2f(txLeft, txBottom);
+    glVertex2f(-0.5, -0.5);
+    glTexCoord2f(txLeft, txTop);
+    glVertex2f(-0.5, 0.5);
+    glTexCoord2f(txRight, txTop);
+    glVertex2f(0.5, 0.5);
+
+    glTexCoord2f(txRight, txTop);
+    glVertex2f(0.5, 0.5);
+    glTexCoord2f(txRight, txBottom);
+    glVertex2f(0.5, -0.5);
+    glTexCoord2f(txLeft, txBottom);
+    glVertex2f(-0.5, -0.5);
+
+    glEnd();
+    glPopMatrix();
+
+}
+void
+Explosion::updateSelf(const double timeDelta)
+{
+    GameObject::updateSelf(timeDelta);
+    t += timeDelta;
+    if(t >= 1)
+        die();
+}
+
+
+#ifndef EXPLOSION_H
+#define EXPLOSION_H
+
+#include <SDL/SDL_events.h>
+#include <string>
+#include "texture.h"
+#include "gameobject.h"
+
+class Explosion : public GameObject
+{
+    public:
+    Explosion(const std::string& textureFile,
+               const double x,
+               const double y);
+    ~Explosion();
+    void calcTexCoords(int spriteOffX, int spriteOffY,
+                       float wImg, float hImg,
+                       int wSprite, int hSprite,
+                       int animation, int frame,
+                       float *txTop, float *txBottom, float *txLeft, float *txRight);
+   virtual void paintSelf();
+   virtual void updateSelf(const double timeDelta);
+
+    private:
+    Texture * texture;
+    double x, y;
+    double t;
+};
+
+
+#endif
 #include "bullet.h"
 #include "asteroid.h"
 #include "levelbackground.h"
+#include "explosion.h"
 
 
 extern bool gRunning;
         YellowEnemyShip *yellowEnemyShip(new YellowEnemyShip(320 + i * 60, -120));
         this->addGameObject(yellowEnemyShip);
     }
+
+
+    Explosion *explosion(new Explosion("text.png", 50, 50));
+    this->addGameObject(explosion);
+
 }
 
 void
         (*it)->paintSelf();
     }
 
-
     playership->paintSelf();
     for(std::vector<GameObject*>::iterator it = gameObjects.begin(); it != gameObjects.end(); it++)
     {
 #include "gm_mainmenu.h"
 #include "gm_level.h"
 #include "audiomgr.h"
+#include <time.h>
 
 SDL_Surface * gScreen = NULL;
 bool gRunning = true;
 main(int argc,
      char ** argv)
 {
+    srand(time(NULL));
+
     SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK);
     SDL_WM_SetCaption("Pucaljka", "Pucaljka");
     gScreen = SDL_SetVideoMode(1024, 768, 0, SDL_OPENGL);
-    
+
     if(SDL_NumJoysticks() > 0)
     {
         printf("Opening joystick %s\n", SDL_JoystickName(0));
         gJoystick = SDL_JoystickOpen(0);
     }
-    
+
     AudioMgr::getInstance()->initAudio();
-    
+
     gGame = new GM_MainMenu();
     uint32_t OldTime = SDL_GetTicks();
-    
+
     glEnable(GL_BLEND);
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
Added
New image