diff options
author | majortom6 <tombarrett@siu.edu> | 2017-02-25 12:42:41 -0600 |
---|---|---|
committer | Tom Barrett <tombarrett@siu.edu> | 2017-03-07 13:23:41 -0600 |
commit | d10f3df359cbccd1d7ee13b7981fee575a90637f (patch) | |
tree | c6faefd9c6f24663b0ae4bdee505d6456e8208a1 | |
parent | 86fba51a32de6528cead5fdf24846d1e0540cad9 (diff) |
-began refractoring src
-reduced area creatures and resources spawn
-everything should be on 8 spaces now
-reorganized creatures action function to make more logical sense
-stripped out unused parts of geoshader and spritebatch
-rw-r--r-- | inc/constants.hpp | 54 | ||||
-rw-r--r-- | inc/creature.hpp | 6 | ||||
-rw-r--r-- | inc/opengl/spritebatch.hpp | 5 | ||||
-rw-r--r-- | inc/resource.hpp | 2 | ||||
-rw-r--r-- | src/creature.cpp | 226 | ||||
-rw-r--r-- | src/dna.cpp | 40 | ||||
-rw-r--r-- | src/event.cpp | 2 | ||||
-rw-r--r-- | src/geoshader.cpp | 130 | ||||
-rw-r--r-- | src/list.cpp | 128 | ||||
-rw-r--r-- | src/quadtree.cpp | 178 | ||||
-rw-r--r-- | src/resource.cpp | 15 | ||||
-rw-r--r-- | src/spritebatch.cpp | 256 | ||||
-rw-r--r-- | src/timer.cpp | 50 |
13 files changed, 507 insertions, 585 deletions
diff --git a/inc/constants.hpp b/inc/constants.hpp index fbcb454..518f2ac 100644 --- a/inc/constants.hpp +++ b/inc/constants.hpp @@ -2,43 +2,45 @@ #define constants_h // General -const int CREATURES = 100; -const int RESOURCES = 1000; -const int MINIMUM_RESOURCES = 800; -const int WINDOW_X = 1000; -const int WINDOW_Y = 1000; +const int CREATURES = 100; +const int RESOURCES = 1000; +const int MINIMUM_RESOURCES = 800; +const int WINDOW_X = 1000; +const int WINDOW_Y = 1000; // Types -const int CREATURE_TYPE = 1; -const int RESOURCE_TYPE = 2; +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; -const int CREATURE_BITE = 10; -const int CREATURE_AMOUNT_TO_GROW = 50; -const int CREATURE_EXP_PREG_TIME = 100; -const int CREATURE_EXP_AGE = 1000000; -const int CREATURE_SIZE_MAX = 10; -const float CREATURE_SPEED = .1; +const int CREATURE_MAX_HEALTH = 1000; +const int CREATURE_REACH = 1; +const int CREATURE_BEST_SENSE = 1; +const int CREATURE_BITE = 10; +const int CREATURE_AMOUNT_TO_GROW = 50; +const int CREATURE_EXP_PREG_TIME = 100; +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; +const int RESOURCE_SIZE_START = 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; +const int NUM_SHADERS = 3; +const int NUM_UNIFORMS = 3; +const float CREATURE_SIDES = 4.0; +const float RESOURCE_SIDES = 10; // Quadtree -const int MAX_OBJECTS = 5; -const int MAX_LEVELS = 6; +const int MAX_OBJECTS = 5; +const int MAX_LEVELS = 6; // Camera -const float MOVE_AMOUNT = .2; +const float MOVE_AMOUNT = .2; #endif diff --git a/inc/creature.hpp b/inc/creature.hpp index 01f6557..f9cb979 100644 --- a/inc/creature.hpp +++ b/inc/creature.hpp @@ -12,15 +12,15 @@ class Creature: public Entity { public: - Creature(Rectangle t, DNA D); + Creature(Rectangle r, DNA d); void Behavior(); void Action(); void Priority(); void setTarget(); void checkTarget(); - void moveTowards(Rectangle t); - void impregnate(DNA D); + void moveTowards(Rectangle r); + void impregnate(DNA d); void giveNearMe(std::list<Entity*> n){nearMe = {std::begin(n),std::end(n)};}; DNA getDNA(){return myDNA;}; diff --git a/inc/opengl/spritebatch.hpp b/inc/opengl/spritebatch.hpp index 94ca97c..2618054 100644 --- a/inc/opengl/spritebatch.hpp +++ b/inc/opengl/spritebatch.hpp @@ -20,7 +20,6 @@ class SpriteBatch {
public:
SpriteBatch(GeoShader theshader);
- ~SpriteBatch();
void init();
void begin();
@@ -35,8 +34,8 @@ class SpriteBatch GLuint _vbo;
GLuint _vao;
- std::vector<GraphicsData*> _gfxPtr; ///< This is for sorting
- std::vector<GraphicsData> _gfx; ///< These are the actual glyphs
+ std::vector<GraphicsData*> _gfxPtr;
+ std::vector<GraphicsData> _gfx;
std::vector<RenderBatch> _renderBatches;
GeoShader shader;
diff --git a/inc/resource.hpp b/inc/resource.hpp index 07d42d4..e559463 100644 --- a/inc/resource.hpp +++ b/inc/resource.hpp @@ -9,7 +9,7 @@ class Resource: public Entity { public: - Resource(Rectangle t); + Resource(Rectangle r); void grow(); void eat(int bite); diff --git a/src/creature.cpp b/src/creature.cpp index bf0ad43..1564fc2 100644 --- a/src/creature.cpp +++ b/src/creature.cpp @@ -1,19 +1,16 @@ #include "creature.hpp" +#include <cstdio> -Creature::Creature(Rectangle t, DNA D) +Creature::Creature(Rectangle r, DNA d) { - rect = t; - myDNA = D; + rect = r; + myDNA = d; if(rect.x == 0 && rect.y == 0){ - rect.x = getRandom(50); - rect.y = getRandom(50); + rect.x = getRandom(30); + rect.y = getRandom(30); } - gfxData.sides = 4.0; - gfxData.x = rect.x; - gfxData.y = rect.y; - type = CREATURE_TYPE; health = myDNA.maxHealth/2; gender = rand() % 2; @@ -23,16 +20,10 @@ Creature::Creature(Rectangle t, DNA D) pregnate = false; hasTarget = false; - if(gender){ - gfxData.r = 1.0; - gfxData.g = 0.0; - gfxData.b = 0.0; - } - else{ - gfxData.r = 0.0; - gfxData.g = 0.0; - gfxData.b = 1.0; - } + if(gender) + gfxData = GraphicsData(rect.x, rect.y, 1, 0, 0, CREATURE_SIDES); + else + gfxData = GraphicsData(rect.x, rect.y, 0, 0, 1, CREATURE_SIDES); } void Creature::Behavior() @@ -61,126 +52,125 @@ void Creature::Behavior() void Creature::Priority() { - if(health < myDNA.maxHealth / 2){ - hungry = true; - able = false; - } - else{ - hungry = false; - able = true; - } + if(health < myDNA.maxHealth / 2){ + hungry = true; + able = false; + } + else{ + hungry = false; + able = true; + } } void Creature::setTarget() { - std::random_shuffle(nearMe.begin(),nearMe.end()); - - for(std::vector <Entity*>::iterator it = nearMe.begin(); it!=nearMe.end(); it++){ - if( (*it)->getType() == RESOURCE_TYPE && hungry){ - target = *it; - hasTarget = true; - wander = false; - break; - } - if( (*it)->getType() == CREATURE_TYPE && able && (*it)->getGender() != gender ){ - target = *it; - hasTarget = true; - wander = false; - break; - } - } - - if(!hasTarget&&!wander){ - wander = true; - float x = getRandom(50); - float y = getRandom(50); - Rectangle tmp; - tmp.x = x; - tmp.y = y; - wTarget = tmp; - } + std::random_shuffle(nearMe.begin(),nearMe.end()); + + for(std::vector <Entity*>::iterator it = nearMe.begin(); it!=nearMe.end(); it++){ + if( (*it)->getType() == RESOURCE_TYPE && hungry){ + target = *it; + hasTarget = true; + wander = false; + break; + } + if( (*it)->getType() == CREATURE_TYPE && able && (*it)->getGender() != gender ){ + target = *it; + hasTarget = true; + wander = false; + break; + } + } + + if(!hasTarget&&!wander){ + wander = true; + Rectangle tmp; + tmp.x = getRandom(30); + tmp.y = getRandom(30); + wTarget = tmp; + } } void Creature::checkTarget() { - for(std::vector <Entity*>::iterator it = nearMe.begin(); it!=nearMe.end(); it++) - if( target == *it ) - return; + for(std::vector <Entity*>::iterator it = nearMe.begin(); it!=nearMe.end(); it++) + if( target == *it ) + return; - hasTarget = false; + hasTarget = false; } void Creature::Action() { - if(hasTarget){ - if( Distance(rect,target->getRectangle()) < myDNA.reach && target->getType() == RESOURCE_TYPE){ - target->eat(myDNA.bite); - health+=myDNA.bite; - amountAte++; - //if(rect.w <= myDNA.sizeMax && myDNA.amountToGrow <= amountAte){ - // amountAte = 0; - // rect.w = rect.h = rect.w + 1; - //} - if(target->getAmount()<=0) - hasTarget = false; - } - else if( Distance(rect,target->getRectangle()) < myDNA.reach && target->getType() == CREATURE_TYPE && target->getGender() != gender ){ - target->impregnate(myDNA); - hasTarget = false; + if(hasTarget){ + if(Distance(rect,target->getRectangle()) < myDNA.reach){ + if(target->getType() == RESOURCE_TYPE){ + target->eat(myDNA.bite); + health+=myDNA.bite; + amountAte++; + if(target->getAmount()<=0) + hasTarget = false; + } + else if (target->getType() == CREATURE_TYPE){ + if(target->getGender() != gender){ + target->impregnate(myDNA); + } + hasTarget = false; + } + + } + else + moveTowards(target->getRectangle()); + } + else if(wander){ + if(Distance(rect,wTarget) < myDNA.reach) + wander = false; + else + moveTowards(wTarget); } - else - moveTowards(target->getRectangle()); - } - else if(wander){ - if(Distance(rect,wTarget) < myDNA.reach) - wander = false; - else - moveTowards(wTarget); - } } void Creature::moveTowards(Rectangle t) { - if( rect.x == t.x ){ - if( rect.y < t.y ) - rect.y+=myDNA.speed; - else - rect.y-=myDNA.speed; - } - else if( rect.y == t.y ){ - if( rect.x < t.x ) - rect.x+=myDNA.speed; - else - rect.x-=myDNA.speed; - } - else if( rect.x < t.x ){ - if( rect.y < t.y ){ - rect.x+=myDNA.speed; - rect.y+=myDNA.speed; - } - else{ - rect.x+=myDNA.speed; - rect.y-=myDNA.speed; - } - } - else if ( rect.x > t.x ){ - if( rect.y < t.y ){ - rect.x-=myDNA.speed; - rect.y+=myDNA.speed; - } - else{ - rect.x-=myDNA.speed; - rect.y-=myDNA.speed; - } - } + if( rect.x == t.x ){ + if( rect.y < t.y ) + rect.y+=myDNA.speed; + else + rect.y-=myDNA.speed; + } + else if( rect.y == t.y ){ + if( rect.x < t.x ) + rect.x+=myDNA.speed; + else + rect.x-=myDNA.speed; + } + else if( rect.x < t.x ){ + if( rect.y < t.y ){ + rect.x+=myDNA.speed; + rect.y+=myDNA.speed; + } + else{ + rect.x+=myDNA.speed; + rect.y-=myDNA.speed; + } + } + else if ( rect.x > t.x ){ + if( rect.y < t.y ){ + rect.x-=myDNA.speed; + rect.y+=myDNA.speed; + } + else{ + rect.x-=myDNA.speed; + rect.y-=myDNA.speed; + } + } } void Creature::impregnate(DNA D) { - if(!pregnate){ - pregnate = true; - pregnancyTime = 0; - childsDNA = myDNA.combine(D); - } + if(!pregnate){ + pregnate = true; + pregnancyTime = 0; + childsDNA = myDNA.combine(D); + } } diff --git a/src/dna.cpp b/src/dna.cpp index bc51fd9..c3ffd9c 100644 --- a/src/dna.cpp +++ b/src/dna.cpp @@ -2,30 +2,30 @@ DNA::DNA() { - maxHealth = CREATURE_MAX_HEALTH; - speed = CREATURE_SPEED; - reach = CREATURE_REACH; - bestSense = CREATURE_BEST_SENSE; - bite = CREATURE_BITE; - amountToGrow = CREATURE_AMOUNT_TO_GROW; - expectedPregnancyTime = CREATURE_EXP_PREG_TIME; - expectedAge = CREATURE_EXP_AGE; - sizeMax = CREATURE_SIZE_MAX; + maxHealth = CREATURE_MAX_HEALTH; + speed = CREATURE_SPEED; + reach = CREATURE_REACH; + bestSense = CREATURE_BEST_SENSE; + bite = CREATURE_BITE; + amountToGrow = CREATURE_AMOUNT_TO_GROW; + expectedPregnancyTime = CREATURE_EXP_PREG_TIME; + expectedAge = CREATURE_EXP_AGE; + sizeMax = CREATURE_SIZE_MAX; } DNA DNA::combine(DNA D) { - DNA N; + DNA N; - N.maxHealth = (this->maxHealth + D.maxHealth)/2; - N.speed = (this->speed + D.speed)/2; - N.reach = (this->reach + D.reach)/2; - N.bestSense = (this->bestSense + D.bestSense)/2; - N.bite = (this->bite + D.bite)/2; - N.amountToGrow = (this->amountToGrow + D.amountToGrow)/2; - N.expectedPregnancyTime = (this->expectedPregnancyTime + D.expectedPregnancyTime)/2; - N.expectedAge = (this->expectedAge + D.expectedAge)/2; - N.sizeMax = (this->sizeMax + D.sizeMax)/2; + N.maxHealth = (this->maxHealth + D.maxHealth)/2; + N.speed = (this->speed + D.speed)/2; + N.reach = (this->reach + D.reach)/2; + N.bestSense = (this->bestSense + D.bestSense)/2; + N.bite = (this->bite + D.bite)/2; + N.amountToGrow = (this->amountToGrow + D.amountToGrow)/2; + N.expectedPregnancyTime = (this->expectedPregnancyTime + D.expectedPregnancyTime)/2; + N.expectedAge = (this->expectedAge + D.expectedAge)/2; + N.sizeMax = (this->sizeMax + D.sizeMax)/2; - return N; + return N; } diff --git a/src/event.cpp b/src/event.cpp index 15358c7..c4f139e 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -2,7 +2,7 @@ Event::Event() { - run = true; + run = true; } int Event::Poll() diff --git a/src/geoshader.cpp b/src/geoshader.cpp index 3bd172b..4481a2e 100644 --- a/src/geoshader.cpp +++ b/src/geoshader.cpp @@ -4,112 +4,102 @@ GeoShader::GeoShader(const std::string& fileName) { - m_program = glCreateProgram(); + m_program = glCreateProgram(); - /* load shader from static vars - m_shaders[0] = CreateShader(GL_VERTEX_SHADER, vertexShaderSrc); - m_shaders[1] = CreateShader(GL_FRAGMENT_SHADER, fragmentShaderSrc); - m_shaders[2] = CreateShader(GL_GEOMETRY_SHADER, geometryShaderSrc); - */ + m_shaders[0] = CreateShader(LoadShader(fileName + ".vert"), GL_VERTEX_SHADER); + m_shaders[1] = CreateShader(LoadShader(fileName + ".frag"), GL_FRAGMENT_SHADER); + m_shaders[2] = CreateShader(LoadShader(fileName + ".geom"), GL_GEOMETRY_SHADER); - m_shaders[0] = CreateShader(LoadShader(fileName + ".vert"), GL_VERTEX_SHADER); - m_shaders[1] = CreateShader(LoadShader(fileName + ".frag"), GL_FRAGMENT_SHADER); - m_shaders[2] = CreateShader(LoadShader(fileName + ".geom"), GL_GEOMETRY_SHADER); + int i; + for(i=0; i < NUM_SHADERS; i++) + glAttachShader(m_program, m_shaders[i]); + glLinkProgram(m_program); + CheckShaderError(m_program, GL_LINK_STATUS, true, "error shader failed to link: "); - for(unsigned int i=0; i < NUM_SHADERS; i++) - glAttachShader(m_program, m_shaders[i]); + glValidateProgram(m_program); + CheckShaderError(m_program, GL_VALIDATE_STATUS, true, "error shader is invalid: "); - glLinkProgram(m_program); - CheckShaderError(m_program, GL_LINK_STATUS, true, "error shader failed to link: "); - - glValidateProgram(m_program); - CheckShaderError(m_program, GL_VALIDATE_STATUS, true, "error shader is invalid: "); - - glUseProgram(m_program); + glUseProgram(m_program); } GeoShader::~GeoShader() { - for(unsigned int i=0; i< NUM_SHADERS; i++){ - glDetachShader(m_program, m_shaders[i]); - glDeleteShader(m_shaders[i]); - } + int i; + for(i=0; i< NUM_SHADERS; i++){ + glDetachShader(m_program, m_shaders[i]); + glDeleteShader(m_shaders[i]); + } } void GeoShader::Bind() { - glUseProgram(m_program); + glUseProgram(m_program); } void GeoShader::Update(const Transform& transform, const Camera& camera) { - glm::mat4 MVP = transform.GetMVP(camera); - glm::mat4 Normal = transform.GetModel(); + glm::mat4 MVP = transform.GetMVP(camera); + glm::mat4 Normal = transform.GetModel(); - glUniformMatrix4fv(m_uniforms[0], 1, GL_FALSE, &MVP[0][0]); + glUniformMatrix4fv(m_uniforms[0], 1, GL_FALSE, &MVP[0][0]); } GLuint GeoShader::CreateShader(const std::string& text, GLenum shaderType) { - GLuint shader = glCreateShader(shaderType); - if(shader == 0) - std::cerr << "Error: Shader Creation Failed" << std::endl; - - const GLchar* shaderSource[1]; - GLint shaderSourceLengths[1]; - - shaderSource[0] = text.c_str(); - shaderSourceLengths[0] = text.length(); - - glShaderSource(shader, 1, shaderSource, shaderSourceLengths); - glCompileShader(shader); - - CheckShaderError(shader, GL_COMPILE_STATUS, false, "error shader compilation failed to link: "); - - return shader; + GLuint shader = glCreateShader(shaderType); + + const GLchar* shaderSource[1]; + GLint shaderSourceLengths[1]; + + shaderSource[0] = text.c_str(); + shaderSourceLengths[0] = text.length(); + + glShaderSource(shader, 1, shaderSource, shaderSourceLengths); + glCompileShader(shader); + + return shader; } void GeoShader::CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const std::string& errorMessage) { - GLint success = 0; - GLchar error[1024] = { 0 }; - - if(isProgram) - glGetProgramiv(shader, flag, &success); - else - glGetShaderiv(shader, flag, &success); + GLint success = 0; + GLchar error[1024] = { 0 }; - if(success == GL_FALSE) - { if(isProgram) - glGetProgramInfoLog(shader, sizeof(error), NULL, error); + glGetProgramiv(shader, flag, &success); else - glGetShaderInfoLog(shader, sizeof(error), NULL, error); - - std::cerr << errorMessage << ": '" << error << "'" << std::endl; - } + glGetShaderiv(shader, flag, &success); + + if(success == GL_FALSE){ + if(isProgram) + glGetProgramInfoLog(shader, sizeof(error), NULL, error); + else + glGetShaderInfoLog(shader, sizeof(error), NULL, error); + + std::cerr << errorMessage << ": '" << error << "'" << std::endl; + } } std::string GeoShader::LoadShader(const std::string& fileName) { - std::ifstream file; - file.open((fileName).c_str()); + std::ifstream file; + file.open((fileName).c_str()); - std::string output; - std::string line; + std::string output; + std::string line; - if(file.is_open()){ - while(file.good()){ - getline(file, line); - output.append(line + "\n"); + if(file.is_open()){ + while(file.good()){ + getline(file, line); + output.append(line + "\n"); + } } - } - else{ - std::cerr << "Unable to load shader: " << fileName << std::endl; - } - - return output; + else{ + std::cerr << "Unable to load shader: " << fileName << std::endl; + } + + return output; } diff --git a/src/list.cpp b/src/list.cpp index e91e1aa..2daed25 100644 --- a/src/list.cpp +++ b/src/list.cpp @@ -2,93 +2,87 @@ List::List() { - int i; - DNA defaultDNA; - Rectangle tmp; - tmp.x = tmp.y = 0; - for(i=0;i<CREATURES;i++){ - Creature X(tmp,defaultDNA); - creatures.push_back(X); - } - - for(i=0;i<RESOURCES;i++){ - Resource Y(tmp); - resources.push_back(Y); - } - - Rectangle R1 = Rectangle(0,0,60,60); - tree = Quadtree(0, R1); + int i; + DNA d; + Rectangle tmp; + for(i=0;i<CREATURES;i++){ + Creature X(tmp,d); + creatures.push_back(X); + } + + for(i=0;i<RESOURCES;i++){ + Resource Y(tmp); + resources.push_back(Y); + } + + Rectangle R1 = Rectangle(0,0,60,60); + tree = Quadtree(0, R1); } void List::Remove() { - for(std::list<Creature>::iterator it = creatures.begin(); it!= creatures.end(); it++) - if(it->getHealth()<=0){ - Rectangle tmp = it->getRectangle(); - Resource r = Resource(tmp); - resources.push_back(r); - creatures.erase(it--); - } - - for(std::list<Resource>::iterator it = resources.begin(); it!= resources.end(); it++) - if(it->getAmount()<=0) - resources.erase(it--); + for(std::list<Creature>::iterator it = creatures.begin(); it!= creatures.end(); it++) + if(it->getHealth()<=0){ + Resource Y(it->getRectangle()); + resources.push_back(Y); + creatures.erase(it--); + } + + for(std::list<Resource>::iterator it = resources.begin(); it!= resources.end(); it++) + if(it->getAmount()<=0) + resources.erase(it--); } void List::Behavior() { - for(std::list<Creature>::iterator it = creatures.begin(); it!= creatures.end(); it++){ - std::list<Entity*> N = getNear(*it); - it->giveNearMe(N); - it->Behavior(); - - if(it->getPregnancyReady()){ - DNA D = it->getChildsDNA(); - Rectangle tmp = it->getRectangle(); - Creature X(tmp,D); - creatures.push_back(X); - it->hadPregnancy(); + for(std::list<Creature>::iterator it = creatures.begin(); it!= creatures.end(); it++){ + std::list<Entity*> N = getNear(*it); + it->giveNearMe(N); + it->Behavior(); + + if(it->getPregnancyReady()){ + DNA d = it->getChildsDNA(); + Rectangle tmp = it->getRectangle(); + Creature X(tmp,d); + creatures.push_back(X); + it->hadPregnancy(); + } } - } - - for(std::list<Resource>::iterator it = resources.begin(); it!= resources.end(); it++) - it->grow(); + + for(std::list<Resource>::iterator it = resources.begin(); it!= resources.end(); it++) + it->grow(); } void List::Place() { - tree.clear(); - - Rectangle tmp; - tmp.x = tmp.y = 0; - while(resources.size() < MINIMUM_RESOURCES){ - Resource Y(tmp); - resources.push_back(Y); - } - - for(std::list<Creature>::iterator it = creatures.begin(); it!= creatures.end(); it++){ - it->Place(); - tree.insert(&(*it));; - } - - for(std::list<Resource>::iterator it = resources.begin(); it!=resources.end(); it++){ - it->Place(); - tree.insert(&(*it));; - } + tree.clear(); + + Rectangle tmp; + while(resources.size() < MINIMUM_RESOURCES){ + Resource Y(tmp); + resources.push_back(Y); + } + + for(std::list<Creature>::iterator it = creatures.begin(); it!= creatures.end(); it++){ + it->Place(); + tree.insert(&(*it));; + } + + for(std::list<Resource>::iterator it = resources.begin(); it!=resources.end(); it++){ + it->Place(); + tree.insert(&(*it));; + } } std::list<Entity*> List::getNear(Creature nC) { - std::list<Entity*> N; - N.clear(); - N = tree.retrieve(N, nC.getGFXD()); + std::list<Entity*> N; + N = tree.retrieve(N, nC.getGFXD()); - //std::vector<Entity*> x{std::begin(N),std::begin(N)}; - return N; + return N; } std::vector<GraphicsData> List::drawQuadTree(){ - return tree.Draw(); + return tree.Draw(); } - diff --git a/src/quadtree.cpp b/src/quadtree.cpp index 91cddf5..efd62c2 100644 --- a/src/quadtree.cpp +++ b/src/quadtree.cpp @@ -4,7 +4,7 @@ Quadtree::Quadtree(){}
Quadtree::Quadtree(int pLevel,Rectangle pBounds){
- level = pLevel;
+ level = pLevel;
bounds = pBounds;
isNull=false;
nodes = new Quadtree[4];
@@ -17,13 +17,13 @@ Quadtree::Quadtree(int pLevel,Rectangle pBounds){ }
void Quadtree::clear(){
- objects.clear();
+ objects.clear();
for (int i = 0; i < 4; i++)
- if (!nodes[i].isNull) {
+ if (!nodes[i].isNull){
nodes[i].objects.clear();
nodes[i].clear();
- }
+ }
delete[] nodes;
if(level == 0)
@@ -31,110 +31,108 @@ void Quadtree::clear(){ }
void Quadtree::split(){
- float subWidth = (bounds.w / 2);
- float subHeight = (bounds.h / 2);
- float x = bounds.x;
- float y = bounds.y;
-
-
- Rectangle R0(x + subWidth/2, y + subHeight/2, subWidth, subHeight);
- Rectangle R1(x - subWidth/2, y + subHeight/2, subWidth, subHeight);
- Rectangle R2(x - subWidth/2, y - subHeight/2, subWidth, subHeight);
- Rectangle R3(x + subWidth/2, y - subHeight/2, subWidth, subHeight);
-
- Quadtree Q0(level+1, R0);
- Quadtree Q1(level+1, R1);
- Quadtree Q2(level+1, R2);
- Quadtree Q3(level+1, R3);
-
- nodes[0] = Q0;
- nodes[1] = Q1;
- nodes[2] = Q2;
- nodes[3] = Q3;
+ float subWidth = (bounds.w / 2);
+ float subHeight = (bounds.h / 2);
+ float x = bounds.x;
+ float y = bounds.y;
+
+
+ Rectangle R0(x + subWidth/2, y + subHeight/2, subWidth, subHeight);
+ Rectangle R1(x - subWidth/2, y + subHeight/2, subWidth, subHeight);
+ Rectangle R2(x - subWidth/2, y - subHeight/2, subWidth, subHeight);
+ Rectangle R3(x + subWidth/2, y - subHeight/2, subWidth, subHeight);
+
+ Quadtree Q0(level+1, R0);
+ Quadtree Q1(level+1, R1);
+ Quadtree Q2(level+1, R2);
+ Quadtree Q3(level+1, R3);
+
+ nodes[0] = Q0;
+ nodes[1] = Q1;
+ nodes[2] = Q2;
+ nodes[3] = Q3;
}
int Quadtree::getIndex(GraphicsData object) {
- int index = -1;
-
- bool topQuadrant = (object.y > bounds.y);
- bool bottomQuadrant = (object.y < bounds.y);
-
- if (object.x < bounds.x) {
- if (topQuadrant)
- index = 1;
- else if (bottomQuadrant)
- index = 2;
- }
- else if (object.x > bounds.x) {
- if (topQuadrant)
- index = 0;
- else if (bottomQuadrant)
- index = 3;
- }
-
- return index;
+ int index = -1;
+
+ bool topQuadrant = (object.y > bounds.y);
+ bool bottomQuadrant = (object.y < bounds.y);
+
+ if (object.x < bounds.x) {
+ if (topQuadrant)
+ index = 1;
+ else if (bottomQuadrant)
+ index = 2;
+ }
+ else if (object.x > bounds.x) {
+ if (topQuadrant)
+ index = 0;
+ else if (bottomQuadrant)
+ index = 3;
+ }
+
+ return index;
}
void Quadtree::insert(Entity* iter){
- if (!nodes[0].isNull) {
- int index = getIndex((*iter).getGFXD());
- if (index != -1) {
- nodes[index].insert(iter);
- return;
+ if (!nodes[0].isNull) {
+ int index = getIndex((*iter).getGFXD());
+ if (index != -1) {
+ nodes[index].insert(iter);
+ return;
+ }
}
- }
- objects.emplace_back(iter);
+ objects.emplace_back(iter);
- if (objects.size() > MAX_OBJECTS && level < MAX_LEVELS) {
- if (nodes[0].isNull)
- split();
-
+ if (objects.size() > MAX_OBJECTS && level < MAX_LEVELS) {
+ if (nodes[0].isNull)
+ split();
- for(std::list <Entity*>::iterator it = objects.begin(); it!=objects.end();it++){
- int index = getIndex((*it)->getGFXD());
- if (index != -1) {
- nodes[index].insert(*it);
- objects.erase(it--);
- }
+ int index;
+ for(std::list <Entity*>::iterator it = objects.begin(); it!=objects.end();it++){
+ index = getIndex((*it)->getGFXD());
+ if (index != -1) {
+ nodes[index].insert(*it);
+ objects.erase(it--);
+ }
+ }
}
- }
}
-
std::vector<GraphicsData> Quadtree::Draw(){
- std::vector<GraphicsData> retdat;
-
- for (int i = 0; i < 4; i++) {
- if (!nodes[i].isNull) {
- std::vector<GraphicsData> temp = nodes[i].Draw();
- retdat.insert(retdat.end(), temp.begin(), temp.end());
- }
- }
-
- if (!nodes[0].isNull)
- retdat.emplace_back(nodes[0].gfxDataRect);
- if (!nodes[1].isNull)
- retdat.emplace_back(nodes[1].gfxDataRect);
- if (!nodes[2].isNull)
- retdat.emplace_back(nodes[2].gfxDataRect);
- if (!nodes[3].isNull)
- retdat.emplace_back(nodes[3].gfxDataRect);
-
- retdat.emplace_back(gfxDataRect);
+ std::vector<GraphicsData> retdat;
+
+ for (int i = 0; i < 4; i++) {
+ if (!nodes[i].isNull) {
+ std::vector<GraphicsData> temp = nodes[i].Draw();
+ retdat.insert(retdat.end(), temp.begin(), temp.end());
+ }
+ }
+
+ if (!nodes[0].isNull)
+ retdat.emplace_back(nodes[0].gfxDataRect);
+ if (!nodes[1].isNull)
+ retdat.emplace_back(nodes[1].gfxDataRect);
+ if (!nodes[2].isNull)
+ retdat.emplace_back(nodes[2].gfxDataRect);
+ if (!nodes[3].isNull)
+ retdat.emplace_back(nodes[3].gfxDataRect);
- return retdat;
+ retdat.emplace_back(gfxDataRect);
+
+ return retdat;
}
std::list<Entity*> Quadtree::retrieve(std::list<Entity*> returnObjects, GraphicsData obj) {
- int index = getIndex(obj);
- if (index != -1 && !nodes[0].isNull) {
- returnObjects = nodes[index].retrieve(returnObjects, obj);
- }
- for(std::list <Entity*>::iterator it = objects.begin(); it!=objects.end(); it++){
- returnObjects.emplace_back(*it);
- }
+ int index = getIndex(obj);
+ if (index != -1 && !nodes[0].isNull)
+ returnObjects = nodes[index].retrieve(returnObjects, obj);
+
+ for(std::list <Entity*>::iterator it = objects.begin(); it!=objects.end(); it++)
+ returnObjects.emplace_back(*it);
- return returnObjects;
+ return returnObjects;
}
diff --git a/src/resource.cpp b/src/resource.cpp index 81fd6b7..c3e61d6 100644 --- a/src/resource.cpp +++ b/src/resource.cpp @@ -1,20 +1,15 @@ #include "resource.hpp" -Resource::Resource(Rectangle t) +Resource::Resource(Rectangle r) { - rect = t; + rect = r; if(rect.x == 0 && rect.y == 0){ - rect.x = getRandom(50); - rect.y = getRandom(50); + rect.x = getRandom(30); + rect.y = getRandom(30); } - gfxData.x = rect.x; - gfxData.y = rect.y; - gfxData.r = 0.0; - gfxData.g = 1.0; - gfxData.b = 0.0; - gfxData.sides = 10.0; + gfxData = GraphicsData(rect.x, rect.y, 0, 1, 0, RESOURCE_SIDES); type = RESOURCE_TYPE; amount = RESOURCE_AMOUNT_START; diff --git a/src/spritebatch.cpp b/src/spritebatch.cpp index 2dd5463..f0a82e6 100644 --- a/src/spritebatch.cpp +++ b/src/spritebatch.cpp @@ -1,185 +1,139 @@ #include "opengl/spritebatch.hpp"
-SpriteBatch::SpriteBatch(GeoShader theshader) : _vbo(0), _vao(0), shader(theshader)
-{
- //shader = theshader;
-}
-
-SpriteBatch::~SpriteBatch()
-{
-
-}
+SpriteBatch::SpriteBatch(GeoShader theshader) : _vbo(0), _vao(0), shader(theshader){}
void SpriteBatch::init()
{
- createVertexArray();
+ createVertexArray();
}
+void SpriteBatch::begin()
+{
+ _renderBatches.clear();
-void SpriteBatch::begin() {
- //_sortType = sortType;
- _renderBatches.clear();
-
- // Makes _glpyhs.size() == 0, however it does not free internal memory.
- // So when we later call emplace_back it doesn't need to internally call new.
- _gfx.clear();
+ // Makes _glpyhs.size() == 0, however it does not free internal memory.
+ // So when we later call emplace_back it doesn't need to internally call new.
+ _gfx.clear();
}
-void SpriteBatch::end() {
- // Set up all pointers for fast sorting
- _gfxPtr.resize(_gfx.size());
- for (int i = 0; i < _gfx.size(); i++) {
- _gfxPtr[i] = &_gfx[i];
- }
-
- //sortGlyphs();
- createRenderBatches();
+void SpriteBatch::end()
+{
+ // Set up all pointers for fast sorting
+ _gfxPtr.resize(_gfx.size());
+ for (int i = 0; i < _gfx.size(); i++)
+ _gfxPtr[i] = &_gfx[i];
+
+ //sortGlyphs();
+ createRenderBatches();
}
-void SpriteBatch::draw(const GraphicsData& gfxData) {
- _gfx.emplace_back(gfxData);
+void SpriteBatch::draw(const GraphicsData& gfxData)
+{
+ _gfx.emplace_back(gfxData);
}
-void SpriteBatch::renderBatch() {
-
- // Bind our VAO. This sets up the opengl state we need, including the
- // vertex attribute pointers and it binds the VBO
- glBindVertexArray(_vao);
+void SpriteBatch::renderBatch()
+{
+ // Bind our VAO. This sets up the opengl state we need, including the
+ // vertex attribute pointers and it binds the VBO
+ glBindVertexArray(_vao);
- for (int i = 0; i < _renderBatches.size(); i++) {
//glBindTexture(GL_TEXTURE_2D, _renderBatches[i].texture);
+ for (int i = 0; i < _renderBatches.size(); i++)
+ glDrawArrays(GL_POINTS, _renderBatches[i].offset, _renderBatches[i].numVertices);
- glDrawArrays(GL_POINTS, _renderBatches[i].offset, _renderBatches[i].numVertices);
- }
- glBindVertexArray(0);
+ glBindVertexArray(0);
}
-void SpriteBatch::createRenderBatches() {
- // This will store all the vertices that we need to upload
- std::vector <float> vertices;
- // Resize the buffer to the exact size we need so we can treat
- // it like an array
- vertices.resize(_gfxPtr.size() * 6);
-
- if (_gfxPtr.empty()) {
- return;
- }
-
- int offset = 0; // current offset
- int cv = 0; // current vertex
-
- //Add the first batch
- _renderBatches.emplace_back(offset, 6);
- vertices[cv++] = _gfxPtr[0]->x;
- vertices[cv++] = _gfxPtr[0]->y;
- vertices[cv++] = _gfxPtr[0]->r;
- vertices[cv++] = _gfxPtr[0]->g;
- vertices[cv++] = _gfxPtr[0]->b;
- vertices[cv++] = _gfxPtr[0]->sides;
-
- offset += 6;
-
- //Add all the rest of the glyphs
- //std::cout << "ptr size = " << _gfxPtr.size() << std::endl;
- for (int cg = 1; cg < _gfxPtr.size(); cg++) {
-
- // Check if this glyph can be part of the current batch
- //if (_gfxPtr[cg]->texture != _gfxPtr[cg - 1]->texture) {
- // Make a new batch
- // _renderBatches.emplace_back(offset, 6);
- //} else {
- // If its part of the current batch, just increase numVertices
- _renderBatches.back().numVertices += 6;
- //}
- vertices[cv++] = _gfxPtr[cg]->x;
- vertices[cv++] = _gfxPtr[cg]->y;
- vertices[cv++] = _gfxPtr[cg]->r;
- vertices[cv++] = _gfxPtr[cg]->g;
- vertices[cv++] = _gfxPtr[cg]->b;
- vertices[cv++] = _gfxPtr[cg]->sides;
+void SpriteBatch::createRenderBatches()
+{
+ // This will store all the vertices that we need to upload
+ std::vector <float> vertices;
+ // Resize the buffer to the exact size we need so we can treat
+ // it like an array
+ vertices.resize(_gfxPtr.size() * 6);
+
+ if (_gfxPtr.empty()) {
+ return;
+ }
+
+ int offset = 0; // current offset
+ int cv = 0; // current vertex
+
+ //Add the first batch
+ _renderBatches.emplace_back(offset, 6);
+ vertices[cv++] = _gfxPtr[0]->x;
+ vertices[cv++] = _gfxPtr[0]->y;
+ vertices[cv++] = _gfxPtr[0]->r;
+ vertices[cv++] = _gfxPtr[0]->g;
+ vertices[cv++] = _gfxPtr[0]->b;
+ vertices[cv++] = _gfxPtr[0]->sides;
offset += 6;
- }
-
-
-
- // Bind our VBO
- glBindBuffer(GL_ARRAY_BUFFER, _vbo);
- // Orphan the buffer (for speed)
- glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), nullptr, GL_DYNAMIC_DRAW);
- // Upload the data
- glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(float), vertices.data());
-
- // Unbind the VBO
- glBindBuffer(GL_ARRAY_BUFFER, 0);
+ //Add all the rest of the glyphs
+ //std::cout << "ptr size = " << _gfxPtr.size() << std::endl;
+ for (int cg = 1; cg < _gfxPtr.size(); cg++) {
+ // Check if this glyph can be part of the current batch
+ //if (_gfxPtr[cg]->texture != _gfxPtr[cg - 1]->texture) {
+ // Make a new batch
+ // _renderBatches.emplace_back(offset, 6);
+ //} else {
+ // If its part of the current batch, just increase numVertices
+ _renderBatches.back().numVertices += 6;
+ //}
+ vertices[cv++] = _gfxPtr[cg]->x;
+ vertices[cv++] = _gfxPtr[cg]->y;
+ vertices[cv++] = _gfxPtr[cg]->r;
+ vertices[cv++] = _gfxPtr[cg]->g;
+ vertices[cv++] = _gfxPtr[cg]->b;
+ vertices[cv++] = _gfxPtr[cg]->sides;
+
+ offset += 6;
+ }
+
+ // Bind our VBO
+ glBindBuffer(GL_ARRAY_BUFFER, _vbo);
+ // Orphan the buffer (for speed)
+ glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), nullptr, GL_DYNAMIC_DRAW);
+ // Upload the data
+ glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(float), vertices.data());
+
+ // Unbind the VBO
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
}
-void SpriteBatch::createVertexArray() {
-
- // Generate the VAO if it isn't already generated
- if (_vao == 0) {
- glGenVertexArrays(1, &_vao);
- }
-
- // Bind the VAO. All subsequent opengl calls will modify it's state.
- glBindVertexArray(_vao);
-
- //G enerate the VBO if it isn't already generated
- if (_vbo == 0) {
- glGenBuffers(1, &_vbo);
- }
- glBindBuffer(GL_ARRAY_BUFFER, _vbo);
+void SpriteBatch::createVertexArray()
+{
+ // Generate the VAO if it isn't already generated
+ if (_vao == 0)
+ glGenVertexArrays(1, &_vao);
- //Tell opengl what attribute arrays we need
- GLint posAttrib = glGetAttribLocation(shader.m_program, "pos");
- glEnableVertexAttribArray(posAttrib);
+ // Bind the VAO. All subsequent opengl calls will modify it's state.
+ glBindVertexArray(_vao);
- GLint colAttrib = glGetAttribLocation(shader.m_program, "color");
- glEnableVertexAttribArray(colAttrib);
+ //G enerate the VBO if it isn't already generated
+ if (_vbo == 0)
+ glGenBuffers(1, &_vbo);
- GLint sidesAttrib = glGetAttribLocation(shader.m_program, "sides");
- glEnableVertexAttribArray(sidesAttrib);
+ glBindBuffer(GL_ARRAY_BUFFER, _vbo);
- // glEnableVertexAttribArray(2);
+ //Tell opengl what attribute arrays we need
+ GLint posAttrib = glGetAttribLocation(shader.m_program, "pos");
+ glEnableVertexAttribArray(posAttrib);
- //This is the position attribute pointer
- glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE,
- 6 * sizeof(float), 0); //This is the color attribute pointer
- glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE,
- 6 * sizeof(float), (void*) (2 * sizeof(float))); //This is the UV attribute pointer
- glVertexAttribPointer(sidesAttrib, 1, GL_FLOAT, GL_FALSE,
- 6 * sizeof(float), (void*) (5 * sizeof(float)));
- glBindVertexArray(0);
+ GLint colAttrib = glGetAttribLocation(shader.m_program, "color");
+ glEnableVertexAttribArray(colAttrib);
-}
+ GLint sidesAttrib = glGetAttribLocation(shader.m_program, "sides");
+ glEnableVertexAttribArray(sidesAttrib);
-/*
-void SpriteBatch::sortGlyphs() {
-
- switch (_sortType) {
- case GlyphSortType::BACK_TO_FRONT:
- std::stable_sort(_glyphPointers.begin(), _glyphPointers.end(), compareBackToFront);
- break;
- case GlyphSortType::FRONT_TO_BACK:
- std::stable_sort(_glyphPointers.begin(), _glyphPointers.end(), compareFrontToBack);
- break;
- case GlyphSortType::TEXTURE:
- std::stable_sort(_glyphPointers.begin(), _glyphPointers.end(), compareTexture);
- break;
- }
-}
-bool SpriteBatch::compareFrontToBack(Glyph* a, Glyph* b) {
- return (a->depth < b->depth);
-}
+ //glEnableVertexAttribArray(2);
-bool SpriteBatch::compareBackToFront(Glyph* a, Glyph* b) {
- return (a->depth > b->depth);
+ //This is the position attribute pointer
+ glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 6*sizeof(float), 0); //This is the color attribute pointer
+ glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*) (2 * sizeof(float))); //This is the UV attribute pointer
+ glVertexAttribPointer(sidesAttrib, 1, GL_FLOAT, GL_FALSE,6*sizeof(float), (void*) (5 * sizeof(float)));
+ glBindVertexArray(0);
}
-bool SpriteBatch::compareTexture(Glyph* a, Glyph* b) {
- return (a->texture < b->texture);
-}
-
-}
-*/
diff --git a/src/timer.cpp b/src/timer.cpp index 9a8e7c8..8aaab03 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -2,50 +2,50 @@ Timer::Timer() { - startTicks = 0; - pausedTicks= 0; - paused = false; - started = false; + startTicks = 0; + pausedTicks= 0; + paused = false; + started = false; } void Timer::Start() { - started = true; - paused = false; - startTicks = SDL_GetTicks(); + started = true; + paused = false; + startTicks = SDL_GetTicks(); } void Timer::Stop() { - started =false; - paused = false; + started =false; + paused = false; } void Timer::Pause() { - if ((started == true) && (paused == false )){ - paused = true; - pausedTicks = SDL_GetTicks() - startTicks; - } + if ((started == true) && (paused == false )){ + paused = true; + pausedTicks = SDL_GetTicks() - startTicks; + } } void Timer::unPause() { - if (paused){ - paused = false; - startTicks = SDL_GetTicks() - pausedTicks; - pausedTicks = 0; - } + if (paused){ + paused = false; + startTicks = SDL_GetTicks() - pausedTicks; + pausedTicks = 0; + } } int Timer::getTicks() { - if(started){ - if (paused) - return pausedTicks; + if(started){ + if (paused) + return pausedTicks; + else + return SDL_GetTicks() - startTicks; + } else - return SDL_GetTicks() - startTicks; - } - else - return 0; + return 0; } |