diff options
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | inc/constants.hpp | 14 | ||||
-rw-r--r-- | inc/creature.hpp | 3 | ||||
-rw-r--r-- | inc/entity.hpp | 9 | ||||
-rw-r--r-- | inc/functions.hpp | 2 | ||||
-rw-r--r-- | inc/list.hpp | 20 | ||||
-rw-r--r-- | inc/main.hpp | 13 | ||||
-rw-r--r-- | inc/opengl/camera.hpp | 39 | ||||
-rw-r--r-- | inc/opengl/geoshader.hpp | 9 | ||||
-rw-r--r-- | inc/opengl/graphicsdata.hpp | 66 | ||||
-rw-r--r-- | inc/quadtree.hpp | 48 | ||||
-rw-r--r-- | inc/resource.hpp | 20 | ||||
-rw-r--r-- | src/creature.cpp | 62 | ||||
-rw-r--r-- | src/entity.cpp | 4 | ||||
-rw-r--r-- | src/list.cpp | 32 | ||||
-rw-r--r-- | src/main.cpp | 4 | ||||
-rw-r--r-- | src/quadtree.cpp | 2 | ||||
-rw-r--r-- | src/resource.cpp | 14 |
18 files changed, 181 insertions, 183 deletions
@@ -14,3 +14,6 @@ ideas - DNA mutations - pick a c naming/coding standard and stick with it - show framerate in gui + +credit to iamn1ck for opengl and quadtree +credit to dakjos for assistance with theorycrafting the idea diff --git a/inc/constants.hpp b/inc/constants.hpp index 7d53134..8cf4edd 100644 --- a/inc/constants.hpp +++ b/inc/constants.hpp @@ -1,15 +1,18 @@ #ifndef constants_h #define constants_h +// General const int CREATURES = 10; const int RESOURCES = 100; -const int MINIMUM_RESOURCES = 800; +const int MINIMUM_RESOURCES = 80; const int WINDOW_X = 1000; const int WINDOW_Y = 1000; +// Types const int CREATURE_TYPE = 1; const int RESOURCE_TYPE = 2; +// Creatures const int CREATURE_MAX_HEALTH = 1000; const int CREATURE_REACH = 1; const int CREATURE_BEST_SENSE = 10; @@ -20,13 +23,22 @@ const int CREATURE_EXP_AGE = 1000000; const int CREATURE_SIZE_MAX = 10; const float CREATURE_SPEED = .1; +// Resource const int RESOURCE_SIZE_STAR = 1; const int RESOURCE_SIZE_MAX = 4; const int RESOURCE_AMOUNT_START = 100; const int RESOURCE_AMOUNT_MAX = 200; const int RESOURCE_GROW = 1; +// Opengl const int NUM_SHADERS = 3; const int NUM_UNIFORMS = 3; +// Quadtree +const int MAX_OBJECTS = 5; +const int MAX_LEVELS = 6; + +// Camera +const float MOVE_AMOUNT = .2; + #endif diff --git a/inc/creature.hpp b/inc/creature.hpp index 99226b3..0f9128f 100644 --- a/inc/creature.hpp +++ b/inc/creature.hpp @@ -1,12 +1,11 @@ #ifndef creature_h #define creature_h -#include <SDL2/SDL.h> +#include <cstdlib> #include <list> #include "entity.hpp" #include "functions.hpp" -#include "dna.hpp" class Creature: public Entity { diff --git a/inc/entity.hpp b/inc/entity.hpp index c47ef00..20c87aa 100644 --- a/inc/entity.hpp +++ b/inc/entity.hpp @@ -1,12 +1,11 @@ #ifndef entity_h #define entity_h -#include <SDL2/SDL.h> - #include "dna.hpp" -#include "opengl/graphicsdata.hpp" #include "rectangle.hpp" +#include "opengl/graphicsdata.hpp" + class Entity { public: @@ -18,7 +17,7 @@ class Entity int getType(){return type;}; virtual bool getGender(void){}; virtual int getAmount(void){}; - Rectangle getRectangle(){return L;}; + Rectangle getRectangle(){return rect;}; GraphicsData getGFXD(){return gfxData;}; @@ -26,7 +25,7 @@ class Entity int type; int gender; bool pregnate; - Rectangle L; + Rectangle rect; GraphicsData gfxData; }; diff --git a/inc/functions.hpp b/inc/functions.hpp index 15cb94a..7ce9f8d 100644 --- a/inc/functions.hpp +++ b/inc/functions.hpp @@ -1,6 +1,8 @@ #ifndef functions_h #define functions_h +#include <cmath> + #include "rectangle.hpp" static double Distance(Rectangle A, Rectangle B){ diff --git a/inc/list.hpp b/inc/list.hpp index aff57a2..872d5fa 100644 --- a/inc/list.hpp +++ b/inc/list.hpp @@ -1,16 +1,12 @@ #ifndef list_h #define list_h -#include <SDL2/SDL.h> #include <list> #include <vector> -#include "functions.hpp" +#include "constants.hpp" #include "creature.hpp" #include "resource.hpp" -#include "sdl/window.hpp" -#include "constants.hpp" -#include "rectangle.hpp" #include "quadtree.hpp" class List @@ -19,14 +15,14 @@ class List List(); void Behavior(); void Place(); - void Remove(); - std::list<Entity*> getNear(Creature C); - std::list<Resource> R; - std::list<Creature> C; + void Remove(); + + std::list<Entity*> getNear(Creature c); + std::list<Resource> resources; + std::list<Creature> creatures; - Quadtree tree; - std::vector<GraphicsData> drawQuadTree(); - Rectangle R1; + Quadtree tree; + std::vector<GraphicsData> drawQuadTree(); }; #endif diff --git a/inc/main.hpp b/inc/main.hpp index 3fe405f..2a8ac44 100644 --- a/inc/main.hpp +++ b/inc/main.hpp @@ -1,16 +1,15 @@ #ifndef main_h #define main_h -#include <time.h> -#include <vector> +#include "constants.hpp" +#include "list.hpp" + +#include "sdl/timer.hpp" +#include "sdl/event.hpp" +#include "sdl/window.hpp" #include "opengl/geoshader.hpp" #include "opengl/rectdrawer.hpp" #include "opengl/spritebatch.hpp" -#include "sdl/window.hpp" -#include "sdl/event.hpp" -#include "list.hpp" -#include "sdl/timer.hpp" -#include "constants.hpp" #endif diff --git a/inc/opengl/camera.hpp b/inc/opengl/camera.hpp index 62ae837..06a5a88 100644 --- a/inc/opengl/camera.hpp +++ b/inc/opengl/camera.hpp @@ -1,57 +1,58 @@ #ifndef camera_h #define camera_h +#include "constants.hpp" + struct Camera { public: Camera(const glm::vec3& pos, float fov, float aspect, float zNear, float zFar){ - this->pos = pos; - this->forward = glm::vec3(0.0f, 0.0f, -1.0f); - this->up = glm::vec3(0.0f, 1.0f, 0.0f); - this->projection = glm::perspective(fov, aspect, zNear, zFar); + this->pos = pos; + this->forward = glm::vec3(0.0f, 0.0f, -1.0f); + this->up = glm::vec3(0.0f, 1.0f, 0.0f); + this->projection = glm::perspective(fov, aspect, zNear, zFar); } inline glm::mat4 GetViewProjection() const { - return projection * glm::lookAt(pos, pos + forward, up); + return projection * glm::lookAt(pos, pos + forward, up); } void MoveForward(){ - pos += forward * amt; + pos += forward * MOVE_AMOUNT; } void MoveBackward(){ - pos += forward * -amt; + pos += forward * -MOVE_AMOUNT; } void MoveRight(){ - pos += -glm::cross(up, forward) * amt; + pos += -glm::cross(up, forward) * MOVE_AMOUNT; } void MoveLeft(){ - pos += glm::cross(up, forward) * amt; + pos += glm::cross(up, forward) * MOVE_AMOUNT; } void MoveUp(){ - pos += glm::vec3(0,.2,0); + pos += glm::vec3(0,MOVE_AMOUNT,0); } void MoveDown(){ - pos -= glm::vec3(0,.2,0); + pos -= glm::vec3(0,MOVE_AMOUNT,0); } void Pitch(float angle){ - glm::vec3 right = glm::normalize(glm::cross(up, forward)); + glm::vec3 right = glm::normalize(glm::cross(up, forward)); - forward = glm::vec3(glm::normalize(glm::rotate(angle, right) * glm::vec4(forward, 0.0))); - up = glm::normalize(glm::cross(forward, right)); + forward = glm::vec3(glm::normalize(glm::rotate(angle, right) * glm::vec4(forward, 0.0))); + up = glm::normalize(glm::cross(forward, right)); } private: - float amt = .2; - glm::mat4 projection; - glm::vec3 pos; - glm::vec3 forward; - glm::vec3 up; + glm::mat4 projection; + glm::vec3 pos; + glm::vec3 forward; + glm::vec3 up; }; #endif diff --git a/inc/opengl/geoshader.hpp b/inc/opengl/geoshader.hpp index 2619a92..d068a55 100644 --- a/inc/opengl/geoshader.hpp +++ b/inc/opengl/geoshader.hpp @@ -5,7 +5,6 @@ #include <GL/glew.h>
#include "constants.hpp"
-#include "graphicsdata.hpp"
#include "transform.hpp"
class GeoShader
@@ -13,10 +12,10 @@ class GeoShader public:
GeoShader(const std::string& fileName);
- void Bind();
- void Update(const Transform& transform, const Camera& camera);
- virtual ~GeoShader();
- GLuint m_program;
+ void Bind();
+ void Update(const Transform& transform, const Camera& camera);
+ virtual ~GeoShader();
+ GLuint m_program;
private:
std::string LoadShader(const std::string& fileName);
diff --git a/inc/opengl/graphicsdata.hpp b/inc/opengl/graphicsdata.hpp index df8d76e..a0398dc 100644 --- a/inc/opengl/graphicsdata.hpp +++ b/inc/opengl/graphicsdata.hpp @@ -1,49 +1,33 @@ #ifndef graphicsdata_h
#define graphicsdata_h
-#include <glm/glm.hpp>
-#include <glm/gtx/transform.hpp>
-
-#include "camera.hpp"
-
class GraphicsData
{
- public:
-
- float x;
- float y;
- float r;
- float g;
- float b;
- float sides;
-
- GraphicsData(){
- this->x = 0;
- this->y = 0;
- this->r = 0;
- this->g = 0;
- this->b = 0;
- this->sides = 0;
- }
-
-
- GraphicsData(float x,float y,float r,float g,float b,float sides){
- this->x = 0;
- this->y = 0;
- this->r = 0;
- this->g = 0;
- this->b = 0;
- this->sides = 0;
- }
-
- float getX(){
- return x;
- }
-
- float getY(){
- return y;
- }
-
+ public:
+ GraphicsData(){
+ this->x = 0;
+ this->y = 0;
+ this->r = 0;
+ this->g = 0;
+ this->b = 0;
+ this->sides = 0;
+ }
+
+ GraphicsData(float x1, float y1, float r1, float g1, float b1, float sides1){
+ this->x = x1;
+ this->y = y1;
+ this->r = r1;
+ this->g = g1;
+ this->b = b1;
+ this->sides = sides1;
+ }
+
+ float x;
+ float y;
+ float r;
+ float g;
+ float b;
+ float sides;
};
#endif
diff --git a/inc/quadtree.hpp b/inc/quadtree.hpp index 8da34aa..2b07103 100644 --- a/inc/quadtree.hpp +++ b/inc/quadtree.hpp @@ -4,38 +4,38 @@ #include <list>
#include <vector>
-#include "sdl/window.hpp"
+#include "constants.hpp"
#include "creature.hpp"
#include "resource.hpp"
#include "entity.hpp"
#include "rectangle.hpp"
+
#include "opengl/graphicsdata.hpp"
+#include "sdl/window.hpp"
class Quadtree {
+ public:
+ Quadtree();
+ Quadtree(int pLevel,Rectangle pBounds);
+
+ void clear();
+ void insert(Entity* iter);
+
+ std::list<Entity*> retrieve(std::list<Entity*> returnObject, GraphicsData obj);
+ std::list<Entity*> objects;
+
+ Quadtree* nodes;
+
+ std::vector<GraphicsData> Draw();
+
+ private:
+ void split();
+ int getIndex(GraphicsData object);
+ int level;
+ bool isNull = true;
- public:
- Quadtree();
- Quadtree(int pLevel, Rectangle pBounds);
- void clear();
- void insert(Entity* iter);
- std::list<Entity*> retrieve(std::list<Entity*> returnObject, GraphicsData obj);
-
- std::list<Entity*> objects;
-
- Quadtree* nodes;
-
- std::vector<GraphicsData> Draw();
-
- private:
- void split();
- int getIndex(GraphicsData object);
- int MAX_OBJECTS = 5;
- int MAX_LEVELS = 6;
- int level;
- bool isNull = true;
-
- GraphicsData gfxDataRect;
- Rectangle bounds;
+ GraphicsData gfxDataRect;
+ Rectangle bounds;
};
#endif
diff --git a/inc/resource.hpp b/inc/resource.hpp index 252b086..07d42d4 100644 --- a/inc/resource.hpp +++ b/inc/resource.hpp @@ -1,20 +1,24 @@ #ifndef resource_h #define resource_h +#include <cstdlib> + #include "entity.hpp" #include "functions.hpp" class Resource: public Entity { - public: - Resource(Rectangle t); - int getAmount(){return amount;}; - void grow(); - void eat(int bite); + public: + Resource(Rectangle t); + + void grow(); + void eat(int bite); + + int getAmount(){return amount;}; - private: - int amount; - int growAmount; + private: + int amount; + int growAmount; }; #endif diff --git a/src/creature.cpp b/src/creature.cpp index 7e5d0f3..9f8204b 100644 --- a/src/creature.cpp +++ b/src/creature.cpp @@ -2,17 +2,17 @@ Creature::Creature(Rectangle t, DNA D) { - L = t; + rect = t; myDNA = D; - if(L.x == 0 && L.y == 0){ - L.x = -30.0 + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/(30-(-30)))); - L.y = -30.0 + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/(30-(-30)))); + if(rect.x == 0 && rect.y == 0){ + rect.x = -30.0 + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/(30-(-30)))); + rect.y = -30.0 + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/(30-(-30)))); } gfxData.sides = 4.0; - gfxData.x = L.x; - gfxData.y = L.y; + gfxData.x = rect.x; + gfxData.y = rect.y; type = CREATURE_TYPE; health = myDNA.maxHealth/2; @@ -118,18 +118,18 @@ void Creature::checkTarget() void Creature::Action() { if(hasTarget){ - if( Distance(L,target->getRectangle()) < myDNA.reach && target->getType() == RESOURCE_TYPE){ + if( Distance(rect,target->getRectangle()) < myDNA.reach && target->getType() == RESOURCE_TYPE){ target->eat(myDNA.bite); health+=myDNA.bite; amountAte++; - //if(L.w <= myDNA.sizeMax && myDNA.amountToGrow <= amountAte){ + //if(rect.w <= myDNA.sizeMax && myDNA.amountToGrow <= amountAte){ // amountAte = 0; - // L.w = L.h = L.w + 1; + // rect.w = rect.h = rect.w + 1; //} if(target->getAmount()<=0) hasTarget = false; } - else if( Distance(L,target->getRectangle()) < myDNA.reach && target->getType() == CREATURE_TYPE && target->getGender() != gender ){ + else if( Distance(rect,target->getRectangle()) < myDNA.reach && target->getType() == CREATURE_TYPE && target->getGender() != gender ){ target->impregnate(myDNA); hasTarget = false; } @@ -137,7 +137,7 @@ void Creature::Action() moveTowards(target->getRectangle()); } else if(wander){ - if(Distance(L,wTarget) < myDNA.reach) + if(Distance(rect,wTarget) < myDNA.reach) wander = false; else moveTowards(wTarget); @@ -146,36 +146,36 @@ void Creature::Action() void Creature::moveTowards(Rectangle t) { - if( L.x == t.x ){ - if( L.y < t.y ) - L.y+=myDNA.speed; + if( rect.x == t.x ){ + if( rect.y < t.y ) + rect.y+=myDNA.speed; else - L.y-=myDNA.speed; + rect.y-=myDNA.speed; } - else if( L.y == t.y ){ - if( L.x < t.x ) - L.x+=myDNA.speed; + else if( rect.y == t.y ){ + if( rect.x < t.x ) + rect.x+=myDNA.speed; else - L.x-=myDNA.speed; + rect.x-=myDNA.speed; } - else if( L.x < t.x ){ - if( L.y < t.y ){ - L.x+=myDNA.speed; - L.y+=myDNA.speed; + else if( rect.x < t.x ){ + if( rect.y < t.y ){ + rect.x+=myDNA.speed; + rect.y+=myDNA.speed; } else{ - L.x+=myDNA.speed; - L.y-=myDNA.speed; + rect.x+=myDNA.speed; + rect.y-=myDNA.speed; } } - else if ( L.x > t.x ){ - if( L.y < t.y ){ - L.x-=myDNA.speed; - L.y+=myDNA.speed; + else if ( rect.x > t.x ){ + if( rect.y < t.y ){ + rect.x-=myDNA.speed; + rect.y+=myDNA.speed; } else{ - L.x-=myDNA.speed; - L.y-=myDNA.speed; + rect.x-=myDNA.speed; + rect.y-=myDNA.speed; } } } diff --git a/src/entity.cpp b/src/entity.cpp index 13bd290..ab94d23 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -2,6 +2,6 @@ void Entity::Place() { - gfxData.x = L.x; - gfxData.y = L.y; + gfxData.x = rect.x; + gfxData.y = rect.y; } diff --git a/src/list.cpp b/src/list.cpp index e83822f..71c0ba9 100644 --- a/src/list.cpp +++ b/src/list.cpp @@ -8,37 +8,37 @@ List::List() tmp.x = tmp.y = 0; for(i=0;i<CREATURES;i++){ Creature X(tmp,defaultDNA); - C.push_back(X); + creatures.push_back(X); } //rect = {0,0,RESOURCE_SIZE_START,RESOURCE_SIZE_START}; for(i=0;i<RESOURCES;i++){ Resource Y(tmp); - R.push_back(Y); + resources.push_back(Y); } - R1 = Rectangle(0,0,60,60); - tree = Quadtree(0,R1); + Rectangle R1 = Rectangle(0,0,60,60); + tree = Quadtree(0, R1); } void List::Remove() { - for(std::list<Creature>::iterator it = C.begin(); it!=C.end(); it++) + for(std::list<Creature>::iterator it = creatures.begin(); it!= creatures.end(); it++) if(it->getHealth()<=0){ Rectangle tmp = it->getRectangle(); Resource r = Resource(tmp); - R.push_back(r); - C.erase(it--); + resources.push_back(r); + creatures.erase(it--); } - for(std::list<Resource>::iterator it = R.begin(); it!=R.end(); it++) + for(std::list<Resource>::iterator it = resources.begin(); it!= resources.end(); it++) if(it->getAmount()<=0) - R.erase(it--); + resources.erase(it--); } void List::Behavior() { - for(std::list<Creature>::iterator it = C.begin(); it!=C.end(); it++){ + for(std::list<Creature>::iterator it = creatures.begin(); it!= creatures.end(); it++){ std::list<Entity*> N = getNear(*it); it->giveNearMe(N); it->Behavior(); @@ -47,12 +47,12 @@ void List::Behavior() DNA D = it->getChildsDNA(); Rectangle tmp = it->getRectangle(); Creature X(tmp,D); - C.push_back(X); + creatures.push_back(X); it->hadPregnancy(); } } - for(std::list<Resource>::iterator it = R.begin(); it!=R.end(); it++) + for(std::list<Resource>::iterator it = resources.begin(); it!= resources.end(); it++) it->grow(); } @@ -62,17 +62,17 @@ void List::Place() Rectangle tmp; tmp.x = tmp.y = 0; - while(R.size() < MINIMUM_RESOURCES){ + while(resources.size() < MINIMUM_RESOURCES){ Resource Y(tmp); - R.push_back(Y); + resources.push_back(Y); } - for(std::list<Creature>::iterator it = C.begin(); it!=C.end(); it++){ + for(std::list<Creature>::iterator it = creatures.begin(); it!= creatures.end(); it++){ it->Place(); tree.insert(&(*it));; } - for(std::list<Resource>::iterator it = R.begin(); it!=R.end(); it++){ + for(std::list<Resource>::iterator it = resources.begin(); it!=resources.end(); it++){ it->Place(); tree.insert(&(*it));; } diff --git a/src/main.cpp b/src/main.cpp index c7ce9e7..1033651 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,10 +54,10 @@ int main() shader.Bind();
_spriteBatch.begin();
- for(std::list<Creature>::iterator it = L.C.begin(); it != L.C.end(); it++)
+ for(std::list<Creature>::iterator it = L.creatures.begin(); it != L.creatures.end(); it++)
_spriteBatch.draw(it->getGFXD());;
- for(std::list<Resource>::iterator it = L.R.begin(); it != L.R.end(); it++)
+ for(std::list<Resource>::iterator it = L.resources.begin(); it != L.resources.end(); it++)
_spriteBatch.draw(it->getGFXD());;
_spriteBatch.end();
diff --git a/src/quadtree.cpp b/src/quadtree.cpp index 9fd4f0c..91cddf5 100644 --- a/src/quadtree.cpp +++ b/src/quadtree.cpp @@ -3,7 +3,7 @@ Quadtree::Quadtree(){}
-Quadtree::Quadtree(int pLevel, Rectangle pBounds){
+Quadtree::Quadtree(int pLevel,Rectangle pBounds){
level = pLevel;
bounds = pBounds;
isNull=false;
diff --git a/src/resource.cpp b/src/resource.cpp index fdb4bf0..9933c4f 100644 --- a/src/resource.cpp +++ b/src/resource.cpp @@ -2,15 +2,15 @@ Resource::Resource(Rectangle t) { - L = t; + rect = t; - if(L.x == 0 && L.y == 0){ - L.x = -30 + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/(30.0-(-30.0)))); - L.y = -30 + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/(30.0-(-30.0)))); + if(rect.x == 0 && rect.y == 0){ + rect.x = -30 + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/(30.0-(-30.0)))); + rect.y = -30 + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/(30.0-(-30.0)))); } - gfxData.x = L.x; - gfxData.y = L.y; + gfxData.x = rect.x; + gfxData.y = rect.y; gfxData.r = 0.0; gfxData.g = 1.0; gfxData.b = 0.0; @@ -30,6 +30,6 @@ void Resource::grow() { if(amount < RESOURCE_AMOUNT_MAX){ amount+=growAmount; - L.h = L.w = map(amount,0,RESOURCE_AMOUNT_MAX,0,RESOURCE_SIZE_MAX); + rect.h = rect.w = map(amount,0,RESOURCE_AMOUNT_MAX,0,RESOURCE_SIZE_MAX); } } |