aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2016-06-02 07:20:09 -0400
committerClyne Sullivan <tullivan99@gmail.com>2016-06-02 07:20:09 -0400
commit2e6369f4dbe2b49a3cb8bec3bacd6559c9733a55 (patch)
tree5171ff776a2a1d8a94ae1752d01f755ce3097b8f
parentb22860234ff7991c851211042a9832d88ccbb958 (diff)
optimizations
-rw-r--r--Makefile2
-rw-r--r--include/entities.hpp118
-rw-r--r--include/world.hpp2
-rw-r--r--main.cpp55
-rw-r--r--src/entities.cpp14
-rw-r--r--src/world.cpp45
-rw-r--r--xml/playerSpawnHill1.xml2
7 files changed, 122 insertions, 116 deletions
diff --git a/Makefile b/Makefile
index 7c05c95..31899a5 100644
--- 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
diff --git a/include/entities.hpp b/include/entities.hpp
index 5a8e19b..61ecc43 100644
--- a/include/entities.hpp
+++ b/include/entities.hpp
@@ -107,64 +107,6 @@ extern const unsigned int NPC_INV_SIZE;
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
* entities, i.e. a common structure.
@@ -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) {
diff --git a/include/world.hpp b/include/world.hpp
index f6a432d..2494120 100644
--- a/include/world.hpp
+++ b/include/world.hpp
@@ -256,7 +256,7 @@ protected:
*
* @see addParticle()
*/
- std::vector<Particles> particles;
+ std::list<Particles> particles;
/**
* A vector of all structures in the world.
diff --git a/main.cpp b/main.cpp
index dc586df..4cf0475 100644
--- 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
diff --git a/src/entities.cpp b/src/entities.cpp
index 18c91ed..2d9de76 100644
--- a/src/entities.cpp
+++ b/src/entities.cpp
@@ -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) {
diff --git a/src/world.cpp b/src/world.cpp
index f13c3b7..5ab20ed 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -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;
+ }
}
}
diff --git a/xml/playerSpawnHill1.xml b/xml/playerSpawnHill1.xml
index 13fa3db..6a65fb1 100644
--- a/xml/playerSpawnHill1.xml
+++ b/xml/playerSpawnHill1.xml
@@ -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"/>