]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Fire flickering
authordrumsetmonkey <abelleisle@roadrunner.com>
Thu, 24 Mar 2016 12:25:56 +0000 (08:25 -0400)
committerdrumsetmonkey <abelleisle@roadrunner.com>
Thu, 24 Mar 2016 12:25:56 +0000 (08:25 -0400)
include/world.h
src/ui.cpp
src/world.cpp

index 85b5370e1cfbd661c331e495f4c446953c328600..51fbbfd27561f67a8a3eba271bc475abb8cd3a1f 100644 (file)
@@ -79,7 +79,8 @@ class World;
  * The light structure, used to store light coordinates and color.
  */
 
-typedef struct {
+class Light{
+public:
        vec2 loc;               /**< Light location */
        Color color;    /**< Light color */
        float radius;   /**< Light radius */
@@ -91,14 +92,26 @@ typedef struct {
        float fireFlicker;
        vec2 fireLoc;
 
-       // Light(vec2 l, Color c, float r){
-       //      loc = l;
-       //      color = c;
-       //      radius = r;
-       //      belongsTo = false;
-       //      following = nullptr;
-       // }
-} Light;
+       Light(vec2 l, Color c, float r){
+               loc = l;
+               color = c;
+               radius = r;
+
+               belongsTo = false;
+               following = nullptr;
+
+               flame = false;
+       }
+
+       void makeFlame(void){
+               flame = true;
+       }
+
+       void follow(Entity *f){
+               following=f;
+               belongsTo = true;
+       }
+};
 
 
 /**
index f0736a545c4b9e9693adfb57de98938ccd7f7b10..e3e3e7d2b702433f1ec9acdf2788f76f27052656 100644 (file)
@@ -858,7 +858,6 @@ namespace ui {
        void quitGame(){
                dialogBoxExists = false;
                currentMenu = NULL;
-               delete[] currentMenu;
                gameRunning = false;
                updateConfig();
                saveConfig();
@@ -1446,17 +1445,11 @@ EXIT:
                                        break;
                                case SDLK_l:
                                        currentWorld->addLight({player->loc.x + SCREEN_WIDTH/2, player->loc.y},{1.0f,1.0f,1.0f});
-                                       currentWorld->light.back().belongsTo = true;
-                                       currentWorld->light.back().following = player;
-                                       currentWorld->light.back().flame = true;
+                                       currentWorld->light.back().follow(player);
+                                       currentWorld->light.back().makeFlame();
                                        break;
                                case SDLK_f:
                                        currentWorld->addLight({player->loc.x + SCREEN_WIDTH/2, player->loc.y},{1.0f,1.0f,1.0f});
-                                       std::cout << currentWorld->light.back().belongsTo << std::endl;
-                                       currentWorld->light.back().belongsTo = false;
-                                       std::cout << currentWorld->light.back().belongsTo << std::endl;
-                                       currentWorld->light.back().following = nullptr;
-                                       currentWorld->light.back().flame = true;
                                        break;
                                case SDLK_g:
                                        //currentWorld->addStructure(LAMP_POST, player->loc.x, player->loc.y, NULL);
@@ -1473,9 +1466,8 @@ EXIT:
                                case SDLK_b:
                                        currentWorld->addStructure(FIRE_PIT, player->loc.x, player->loc.y, "", "");
                                        currentWorld->addLight({player->loc.x + SCREEN_WIDTH/2, player->loc.y},{1.0f,1.0f,1.0f});
-                                       currentWorld->light.back().belongsTo = false;
-                                       currentWorld->light.back().following = nullptr;
-                                       currentWorld->light.back().flame = true;
+                                       currentWorld->light.back().follow(currentWorld->build.back());
+                                       currentWorld->light.back().makeFlame();
                                        break;
                                case SDLK_F12:
                                        // Make the BYTE array, factor of 3 because it's RBG.
index ca070a84e99c08d751968d7d3580f9b72e42bf65..0e0b8aa48e78dcb21e9a9f65c79fcf61b3f13c55 100644 (file)
@@ -532,17 +532,38 @@ draw( Player *p )
     for(auto &l : light){
         if(l.belongsTo){
             l.loc.x = l.following->loc.x + SCREEN_WIDTH/2;
+            l.loc.y = l.following->loc.y;
+        }
+        if(l.flame){
+            l.fireFlicker = .9+((rand()%2)/10.0f);
+            l.fireLoc.x = l.loc.x + (rand()%2-1)*3;
+            l.fireLoc.y = l.loc.y + (rand()%2-1)*3;
+
+            //std::cout << l.fireLoc.x << "," << l.fireLoc.y << std::endl;
+            //std::cout << l.loc.x << "," << l.loc.y << std::endl << std::endl;
+        }else{
+            l.fireFlicker = 1.0f;
         }
     }
 
-       std::unique_ptr<GLfloat[]> pointArrayBuf = std::make_unique<GLfloat[]> (2 * (light.size()));
+    std::unique_ptr<GLfloat[]> pointArrayBuf = std::make_unique<GLfloat[]> (2 * (light.size()));
        auto pointArray = pointArrayBuf.get();
+    GLfloat flameArray[64];
 
-       for ( i = 0; i < (int)light.size(); i++ ) {
-               pointArray[2 * i    ] = light[i].loc.x - offset.x;
-               pointArray[2 * i + 1] = light[i].loc.y;
+       for (uint i = 0; i < light.size(); i++) {
+        if(light[i].flame){
+               pointArray[2 * i    ] = light[i].fireLoc.x - offset.x;
+               pointArray[2 * i + 1] = light[i].fireLoc.y;
+        }else{
+            pointArray[2 * i    ] = light[i].loc.x - offset.x;
+            pointArray[2 * i + 1] = light[i].loc.y;
+        }
        }
 
+    for(uint i = 0; i < light.size(); i++){
+        flameArray[i] = light[i].fireFlicker;
+    }
+
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
 
@@ -556,6 +577,7 @@ draw( Player *p )
                glUniform1i ( glGetUniformLocation( shaderProgram, "numLight"     ), light.size());
                glUniform2fv( glGetUniformLocation( shaderProgram, "lightLocation"), light.size(), pointArray );
                glUniform3f ( glGetUniformLocation( shaderProgram, "lightColor"   ), 1.0f, 1.0f, 1.0f );
+        glUniform1fv(glGetUniformLocation(shaderProgram,"fireFlicker"), light.size(),flameArray);
        }
 
     /*
@@ -966,11 +988,8 @@ addParticle( float x, float y, float w, float h, float vx, float vy, Color color
 }
 
 void World::addLight(vec2 loc, Color color){
-       Light l;
-       if ( light.size() < 64 ) {
-               l.loc = loc;
-               l.color = color;
-               light.push_back(l);
+       if(light.size() < 64){
+               light.push_back(Light(loc,color,1));
        }
 }
 
@@ -1222,10 +1241,8 @@ void IndoorWorld::draw(Player *p){
 
        //glEnable(GL_TEXTURE_2D);
 
-    std::cout << "Lights and shit" << std::endl;
     for(auto &l : light){
         if(l.belongsTo){
-            std::cout << "Is following" << std::endl;
             l.loc.x = l.following->loc.x + SCREEN_WIDTH/2;
             l.loc.y = l.following->loc.y;
         }
@@ -1234,19 +1251,17 @@ void IndoorWorld::draw(Player *p){
             l.fireLoc.x = l.loc.x + (rand()%2-1)*3;
             l.fireLoc.y = l.loc.y + (rand()%2-1)*3;
 
-            std::cout << l.fireLoc.x << "," << l.fireLoc.y << std::endl;
-            std::cout << l.loc.x << "," << l.loc.y << std::endl << std::endl;
+            //std::cout << l.fireLoc.x << "," << l.fireLoc.y << std::endl;
+            //std::cout << l.loc.x << "," << l.loc.y << std::endl << std::endl;
         }else{
             l.fireFlicker = 1.0f;
         }
     }
 
-    std::cout << "Making light arrays" << std::endl;
     std::unique_ptr<GLfloat[]> pointArrayBuf = std::make_unique<GLfloat[]> (2 * (light.size()));
        auto pointArray = pointArrayBuf.get();
     GLfloat flameArray[64];
 
-    std::cout << "Setting array locations" << std::endl;
        for (i = 0; i < light.size(); i++) {
         if(light[i].flame){
                pointArray[2 * i    ] = light[i].fireLoc.x - offset.x;
@@ -1257,7 +1272,6 @@ void IndoorWorld::draw(Player *p){
         }
        }
 
-    std::cout << "Flame array" << std::endl;
     for(i = 0; i < light.size(); i++){
         flameArray[i] = light[i].fireFlicker;
     }
@@ -1268,9 +1282,7 @@ void IndoorWorld::draw(Player *p){
        glUseProgram( shaderProgram );
        glUniform1i(glGetUniformLocation(shaderProgram, "sampler"), 0);
        glUniform1f(glGetUniformLocation(shaderProgram, "amb"    ), 0.02f + light.size()/50.0f);
-    glUniform1i(glGetUniformLocation(shaderProgram, "fire"   ), 1);
 
-    std::cout << "Uniform sending" << std::endl;
        if ( light.size() == 0)
                glUniform1i(glGetUniformLocation(shaderProgram, "numLight"), 0);
        else {
@@ -1282,8 +1294,6 @@ void IndoorWorld::draw(Player *p){
 
     //delete[] flameArray;
 
-    std::cout << "Done shading" << std::endl;
-
        bgTex->bind(0);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //for the s direction
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //for the t direction
@@ -1299,8 +1309,6 @@ void IndoorWorld::draw(Player *p){
        glUseProgram(0);
        //glDisable(GL_TEXTURE_2D);
 
-    std::cout << "Faggot" << std::endl;
-
        /*
         *      Calculate the starting and ending points to draw the ground from.
        */
@@ -1334,8 +1342,6 @@ void IndoorWorld::draw(Player *p){
        glEnd();
        glUseProgram(0);
 
-    std::cout << "Queer" << std::endl;
-
        /*
         *      Draw all entities.
        */
@@ -1347,8 +1353,6 @@ void IndoorWorld::draw(Player *p){
                e->draw();
 
        p->draw();
-
-    std::cout << "Cranmore Tubing park" << std::endl;
 }
 
 Arena::Arena(World *leave,Player *p,Mob *m){