]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
optimizations
authorClyne Sullivan <tullivan99@gmail.com>
Thu, 2 Jun 2016 11:20:09 +0000 (07:20 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Thu, 2 Jun 2016 11:20:09 +0000 (07:20 -0400)
Makefile
include/entities.hpp
include/world.hpp
main.cpp
src/entities.cpp
src/world.cpp
xml/playerSpawnHill1.xml

index 7c05c954d132cd6c790b2bd180b372d5bec86874..31899a51c8815a19adc7b022862c5b3f736bff3e 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -12,7 +12,7 @@ ifeq ($(TARGET_OS),win32)
               -lSDL2main -lSDL2 -lSDL2_image -lSDL2_mixer -lfreetype
 endif
 
-CXXFLAGS = -g -m$(TARGET_BITS) -std=c++14
+CXXFLAGS = -g -m$(TARGET_BITS) -std=c++14 -fext-numeric-literals
 CXXINC   = -Iinclude -Iinclude/freetype
 CXXWARN  = -Wall -Wextra -Werror #-pedantic-errors
 
index 5a8e19b96b12cf85af0f893c5b36da1b9f5ad907..61ecc437e668f5e9ed384e9d18e8a27f7e89fbb2 100644 (file)
@@ -106,64 +106,6 @@ extern const unsigned int NPC_INV_SIZE;
 // a prototype of the world class, necessary for some function prototypes
 class World;
 
-/**
- * The particle class, handles a single particle.
- */
-class Particles{
-public:
-       // the location of the particle
-       vec2 loc;
-       float zOffset;
-
-       // the width of the particle, in pixels
-       float width;
-
-       // the height of the particle, in pixels
-       float height;
-
-       // the velocity of the particle, in pixels
-       vec2 vel;
-
-       // the color of the particle
-       Color color;
-
-       // TODO
-       vec2 index;
-
-       // the amount of milliseconds left for the particle to live
-       float duration;
-
-       // when true, the particle will move
-       bool canMove;
-
-       // TODO
-       bool fountain;
-
-       // when true, the particle will be affected by gravity
-       bool gravity;
-
-       // when true, draws the particle behind structures
-       bool behind;
-
-       // when true, the particle will bounce on impact with ground
-       bool bounce;
-
-       // creates a particle with the desired characteristics
-       Particles(float x, float y, float w, float h, float vx, float vy, Color c, float d);
-
-       // allows the particle to be destroyed
-       ~Particles(void){}
-
-       // draws the particle
-       void draw(GLfloat*& p) const;
-
-       // updates a particle
-       void update(float _gravity, float ground_y);
-
-       // returns true if the particle should be killed
-       bool kill(float delta);
-};
-
 /**
  * The entity class.
  * This class contains common functions and variables for all types of
@@ -433,6 +375,66 @@ public:
        void createFromXML(XMLElement *e);
 };
 
+/**
+ * The particle class, handles a single particle.
+ */
+class Particles{
+public:
+       // the location of the particle
+       vec2 loc;
+       float zOffset;
+
+       // the width of the particle, in pixels
+       float width;
+
+       // the height of the particle, in pixels
+       float height;
+
+       // the velocity of the particle, in pixels
+       vec2 vel;
+
+       // the color of the particle
+       Color color;
+
+       // TODO
+       vec2 index;
+
+       // the amount of milliseconds left for the particle to live
+       float duration;
+
+       // when true, the particle will move
+       bool canMove;
+
+       // TODO
+       bool fountain;
+
+       // when true, the particle will be affected by gravity
+       bool gravity;
+
+       // when true, draws the particle behind structures
+       bool behind;
+
+       // when true, the particle will bounce on impact with ground
+       bool bounce;
+
+       Structures *stu;
+
+       // creates a particle with the desired characteristics
+       Particles(float x, float y, float w, float h, float vx, float vy, Color c, float d);
+
+       // allows the particle to be destroyed
+       ~Particles(void){}
+
+       // draws the particle
+       void draw(GLfloat*& p) const;
+
+       // updates a particle
+       void update(float _gravity, float ground_y);
+
+       // returns true if the particle should be killed
+       bool timeUp(void);
+};
+
 #include <mob.hpp>
 
 constexpr Object *Objectp(Entity *e) {
index f6a432de2900c17506c3240827ae628b117efec7..2494120385f1e7ff6a20a552790be26db888447b 100644 (file)
@@ -256,7 +256,7 @@ protected:
         *
         * @see addParticle()
         */
-       std::vector<Particles>    particles;
+       std::list<Particles>    particles;
 
        /**
         * A vector of all structures in the world.
index dc586df9b5d128c21268afa4740271e62d675c60..4cf0475924b76da23fe58f2a7b65f266c91dde59 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -25,7 +25,7 @@ using namespace tinyxml2;
 ** --------------------------------------------------------------------------*/
 
 // the game's window title name
-constexpr const char *GAME_NAME = "Independent Study v0.7 alpha - NOW WITH lights and snow and stuff";
+constexpr const char *GAME_NAME = "Independent Study v0.8 alpha - NOW WITH decent shaders";
 
 // SDL's window object
 SDL_Window *window = NULL;
@@ -90,6 +90,15 @@ GLuint mouseTex;
 // the center of the screen
 vec2 offset;
 
+/*
+ * fps contains the game's current FPS, debugY contains the player's
+ * y coordinates, updated at a certain interval. These are used in
+ * the debug menu (see below).
+ */
+
+static unsigned int fps=0;
+static float debugY=0;
+
 // handles all logic operations
 void logic(void);
 
@@ -270,8 +279,20 @@ int main(int argc, char *argv[]){
        // the main loop, in all of its gloriousness..
        gameRunning = true;
        std::thread([&]{
-               while (gameRunning)
+               while (gameRunning) {
                        mainLoop();
+                       std::this_thread::sleep_for(std::chrono::milliseconds(1));
+               }
+       }).detach();
+
+       // the debug loop, gets debug screen values
+       std::thread([&]{
+               while (gameRunning) {   
+                       fps = 1000 / game::time::getDeltaTime();
+                       debugY = player->loc.y;
+
+                       std::this_thread::sleep_for(std::chrono::seconds(1));
+               }
        }).detach();
 
        while (gameRunning)
@@ -299,18 +320,7 @@ int main(int argc, char *argv[]){
     return 0; // Calls everything passed to atexit
 }
 
-/*
- * fps contains the game's current FPS, debugY contains the player's
- * y coordinates, updated at a certain interval. These are used in
- * the debug menu (see below).
- */
-
-static unsigned int fps=0;
-static float debugY=0;
-
 void mainLoop(void){
-       static unsigned int debugDiv=0;                 // A divisor used to update the debug menu if it's open
-
        game::time::mainLoopHandler();
 
        if (currentMenu) {
@@ -324,16 +334,7 @@ void mainLoop(void){
 
                currentWorld->update(player, game::time::getDeltaTime(), game::time::getTickCount());
                currentWorld->detect(player);
-
-               if (++debugDiv == 20) {
-                       debugDiv=0;
-
-                       fps = 1000 / game::time::getDeltaTime();
-                       debugY = player->loc.y;
-               }
        }
-
-       SDL_Delay(1);
 }
 
 void render() {
@@ -354,10 +355,10 @@ void render() {
        offset.y = std::max(player->loc.y + player->height / 2, SCREEN_HEIGHT / 2.0f);
 
        // "setup"
-       glm::mat4 projection = glm::ortho(      static_cast<float>(floor(offset.x-SCREEN_WIDTH/2)),     //left
-                                                                               static_cast<float>(floor(offset.x+SCREEN_WIDTH/2)),     //right
-                                                                               static_cast<float>(floor(offset.y-SCREEN_HEIGHT/2)),    //bottom
-                                                                               static_cast<float>(floor(offset.y+SCREEN_HEIGHT/2)),    //top
+       glm::mat4 projection = glm::ortho(      floor(offset.x-SCREEN_WIDTH/2),         //left
+                                                                               floor(offset.x+SCREEN_WIDTH/2),         //right
+                                                                               floor(offset.y-SCREEN_HEIGHT/2),        //bottom
+                                                                               floor(offset.y+SCREEN_HEIGHT/2),        //top
                                                                                10.0f,                                                          //near
                                                                                -10.0f);                                                        //far
 
@@ -390,7 +391,7 @@ void render() {
         * Call the world's draw function, drawing the player, the world, the background, and entities. Also
         * draw the player's inventory if it exists.
         */
-       player->near = true; // allow player's name to be drawn
+       //player->near = true; // allow player's name to be drawn
        currentWorld->draw(player);
 
        // draw the player's inventory
index 18c91ed6671a51639d041c5ab01dc5126c222056..2d9de76dd69678790c290e04d16a27e57cf0a8cf 100644 (file)
@@ -941,11 +941,12 @@ Particles::Particles(float x, float y, float w, float h, float vx, float vy, Col
        bounce = false;
        index = Texture::getIndex(c);
        zOffset = ((rand()%20)-10)/1000.0f;
+       stu = nullptr;
 }
 
 void Particles::draw(GLfloat*& p) const
 {
-       vec2 tc = vec2(0.25f * this->index.x, 0.125f * (8.0f - this->index.y));
+       vec2 tc = vec2(0.25f * index.x, 0.125f * (8.0f - index.y));
 
     float z = 0.9;
        if (behind)
@@ -1004,6 +1005,8 @@ void Particles::draw(GLfloat*& p) const
 
 void Particles::update(float _gravity, float ground_y)
 {
+       auto delta = game::time::getDeltaTime();
+
        // handle ground collision
        if (loc.y < ground_y) {
                loc.y = ground_y;
@@ -1020,13 +1023,16 @@ void Particles::update(float _gravity, float ground_y)
 
        // handle gravity
        else if (gravity && vel.y > -1.0f) {
-               vel.y -= _gravity * game::time::getDeltaTime();
+               vel.y -= _gravity * delta;
        }
+
+       // handle lifetime
+       duration -= delta;
 }
 
-bool Particles::kill(float delta)
+bool Particles::timeUp(void)
 {
-       return (duration -= delta) <= 0;
+       return !(duration > 0);
 }
 
 void Player::save(void) {
index f13c3b7f7cc08b4670380309f5f62942f450975f..5ab20ede036643851bbfd3ed714bf7f3cc015b23 100644 (file)
@@ -311,7 +311,7 @@ void World::drawBackgrounds(void)
 
                
        static GLuint starTex = Texture::genColor(Color(255, 255, 255));
-       const static float stardim = 2;
+       constexpr const static float stardim = 2;
        GLfloat star_coord[star.size() * 5 * 6 + 1];
     GLfloat *si = &star_coord[0];              
 
@@ -718,15 +718,15 @@ void World::draw(Player *p)
        
        std::vector<GLfloat> partVec(pss);
        GLfloat *pIndex = &partVec[0];
-    
-       for (uint p = 0; p < ps; p++) {
+   
+       for (const auto &p : particles) {
         pc += 30;
                if (pc > pss) {
                        // TODO resize the vector or something better than breaking
-                       std::cout << "Whoops:" << pc << "," << partVec.size() << std::endl;
+                       //std::cout << "Whoops:" << pc << "," << partVec.size() << std::endl;
                        break;
                }
-               particles[p].draw(pIndex);
+               p.draw(pIndex);
     }
 
     glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &partVec[0]);
@@ -896,6 +896,7 @@ detect(Player *p)
                                                        2500                                                                                            // duration (ms)
                                                        );
                                particles.back().fountain = true;
+                               particles.back().stu = b;
                        }
                        break;
                case FIRE_PIT:
@@ -911,6 +912,7 @@ detect(Player *p)
                                                        );
                                particles.back().gravity = false;
                                particles.back().behind  = true;
+                               particles.back().stu = b;
                        }
                        break;
                default:
@@ -968,25 +970,20 @@ update(Player *p, unsigned int delta, unsigned int ticks)
         }
        }
     // iterate through particles
-    particles.erase(std::remove_if(particles.begin(), particles.end(), [&delta](Particles &part) {
-                        return part.kill(delta);
-                    }),
-                    particles.end());
-
-    for (auto part = particles.begin(); part != particles.end(); part++) {
-        auto pa = *part;
-
-               if (pa.canMove) {
-                       (*part).loc.y += pa.vel.y * delta;
-                       (*part).loc.x += pa.vel.x * delta;
-
-            if (std::any_of(std::begin(build), std::end(build), [pa](const Structures *s) {
-                    return (s->bsubtype == FOUNTAIN) &&
-                           (pa.loc.x >= s->loc.x) && (pa.loc.x <= s->loc.x + s->width) &&
-                           (pa.loc.y <= s->loc.y + s->height * 0.25f);
-                })) {
-                particles.erase(part);
-            }
+    particles.remove_if([](const Particles &part) {
+               return part.duration <= 0;
+    });
+
+    for (auto &pa : particles) {
+               if (pa.canMove) { // causes overhead
+                       pa.loc.y += pa.vel.y * delta;
+                       pa.loc.x += pa.vel.x * delta;
+
+                       if (pa.stu != nullptr) {
+                               if (pa.loc.x >= pa.stu->loc.x && pa.loc.x <= pa.stu->loc.x + pa.stu->width &&
+                                   pa.loc.y <= pa.stu->loc.y + pa.stu->height * 0.25f)
+                                       pa.duration = 0;
+                       }
                }
        }
 
index 13fa3dba03731c318e97b2fdc29ee6fbc6ace463..6a65fb13401134cd1f8ad64b0796d475a9e1a554 100644 (file)
@@ -8,7 +8,7 @@
     <rabbit x="300" aggressive="false" health="100" alive="0"/>
     <bird y="500"/>
     <cat/>
-    <trigger x="-300" id="Test"/>
+    <!--<trigger x="-300" id="Test"/>-->
     <npc name="Ralph" hasDialog="true" x="300"/>
     <npc name="Johnny" hasDialog="false" x="300"/>
     <npc name="Big Dave" hasDialog="true" x="300"/>