summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormajortom6 <tombarrett@siu.edu>2017-03-03 15:18:22 -0600
committerTom Barrett <tombarrett@siu.edu>2017-03-07 13:23:42 -0600
commitdfda3f2a5e555d3173359134f4994bcd12d129f8 (patch)
treeadc9d75f1e28332b8c5208fdb3c4685667816dda
parent3c78d411760beb9332e7282e4ec49a199998c67d (diff)
-removed the Graphics Data class
-replaced with already implemented Rectangle, and put color and side variables in dna (can potentially remove the side variable if it does nothing important)
-rw-r--r--inc/constants.hpp2
-rw-r--r--inc/dna.hpp7
-rw-r--r--inc/list.hpp2
-rw-r--r--inc/opengl/graphicsdata.hpp33
-rw-r--r--inc/opengl/spritebatch.hpp10
-rw-r--r--inc/organism.hpp6
-rw-r--r--inc/quadtree.hpp11
-rw-r--r--src/dna.cpp12
-rw-r--r--src/list.cpp15
-rw-r--r--src/main.cpp4
-rw-r--r--src/organism.cpp16
-rw-r--r--src/quadtree.cpp33
-rw-r--r--src/spritebatch.cpp35
13 files changed, 72 insertions, 114 deletions
diff --git a/inc/constants.hpp b/inc/constants.hpp
index 41fc082..e7d69b4 100644
--- a/inc/constants.hpp
+++ b/inc/constants.hpp
@@ -14,7 +14,7 @@ const int RESOURCE_TYPE = 2;
// Creatures
const int CREATURE_MAX_HEALTH = 1000;
-const int CREATURE_BEST_SENSE = 2;
+const int CREATURE_BEST_SENSE = 1.5;
const int CREATURE_BITE = 10;
const int CREATURE_EXP_PREG_TIME = 100;
const int CREATURE_EXP_AGE = 10000;
diff --git a/inc/dna.hpp b/inc/dna.hpp
index 8c4effa..1126638 100644
--- a/inc/dna.hpp
+++ b/inc/dna.hpp
@@ -26,6 +26,13 @@ class DNA
float speed;
float mutationPercent;
float mutationChance;
+
+ struct Visuals{
+ float sides;
+ float red;
+ float green;
+ float blue;
+ } appearance;
};
#endif
diff --git a/inc/list.hpp b/inc/list.hpp
index 5f1ba8e..b5da34c 100644
--- a/inc/list.hpp
+++ b/inc/list.hpp
@@ -22,7 +22,7 @@ class List
std::list<Organism> creatures;
Quadtree tree;
- std::vector<GraphicsData> drawQuadTree();
+ std::vector<Rectangle> drawQuadTree();
};
#endif
diff --git a/inc/opengl/graphicsdata.hpp b/inc/opengl/graphicsdata.hpp
deleted file mode 100644
index a0398dc..0000000
--- a/inc/opengl/graphicsdata.hpp
+++ /dev/null
@@ -1,33 +0,0 @@
-#ifndef graphicsdata_h
-#define graphicsdata_h
-
-class GraphicsData
-{
- 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/opengl/spritebatch.hpp b/inc/opengl/spritebatch.hpp
index 63e45eb..d1c4d7a 100644
--- a/inc/opengl/spritebatch.hpp
+++ b/inc/opengl/spritebatch.hpp
@@ -5,8 +5,8 @@
#include <vector>
#include <GL/glew.h>
-#include "graphicsdata.hpp"
#include "geoshader.hpp"
+#include "dna.hpp"
class RenderBatch {
public:
@@ -24,7 +24,7 @@ class SpriteBatch
void init();
void begin();
void end();
- void draw(const GraphicsData& gfxData);
+ void draw(Rectangle r, DNA::Visuals v);
void renderBatch();
private:
@@ -34,9 +34,9 @@ class SpriteBatch
GLuint _vbo;
GLuint _vao;
- std::vector<GraphicsData*> _gfxPtr;
- std::vector<GraphicsData> _gfx;
- std::vector<RenderBatch> _renderBatches;
+ std::vector<std::pair<Rectangle,DNA::Visuals>*> _gfxPtr;
+ std::vector<std::pair<Rectangle,DNA::Visuals>> _gfx;
+ std::vector<RenderBatch> _renderBatches;
GeoShader shader;
};
diff --git a/inc/organism.hpp b/inc/organism.hpp
index 9534fbb..2a7a67c 100644
--- a/inc/organism.hpp
+++ b/inc/organism.hpp
@@ -8,8 +8,6 @@
#include "rectangle.hpp"
#include "functions.hpp"
-#include "opengl/graphicsdata.hpp"
-
class Organism
{
public:
@@ -18,7 +16,6 @@ class Organism
void Behavior();
void Action();
void Priority();
- void Place();
void setTarget();
void checkTarget();
void moveTowards(Rectangle r);
@@ -30,7 +27,7 @@ class Organism
DNA getDNA() {return myDNA;};
DNA getChildsDNA() {return childsDNA;};
- GraphicsData getGFXD() {return gfxData;};
+ DNA::Visuals getVisuals() {return myDNA.appearance;};
Rectangle getRectangle() {return rect;};
int getHealth() {return health;};
int getBestSense() {return myDNA.bestSense;};
@@ -45,7 +42,6 @@ class Organism
DNA myDNA;
DNA childsDNA;
Rectangle rect;
- GraphicsData gfxData;
int health;
int pregnancyTime;
diff --git a/inc/quadtree.hpp b/inc/quadtree.hpp
index 8520584..eb50db8 100644
--- a/inc/quadtree.hpp
+++ b/inc/quadtree.hpp
@@ -7,31 +7,30 @@
#include "organism.hpp"
#include "rectangle.hpp"
-#include "opengl/graphicsdata.hpp"
#include "sdl/window.hpp"
class Quadtree {
public:
Quadtree();
- Quadtree(int pLevel,Rectangle pBounds);
+ Quadtree(int pLevel, Rectangle pBounds);
void clear();
void insert(Organism* iter);
- std::vector<Organism*> retrieve(std::vector<Organism*> returnObject, GraphicsData obj);
+ std::vector<Organism*> retrieve(std::vector<Organism*> returnObject, Rectangle obj);
std::vector<Organism*> objects;
Quadtree* nodes;
- std::vector<GraphicsData> Draw();
+ std::vector<Rectangle> Draw();
private:
void split();
- int getIndex(GraphicsData object);
+ int getIndex(Rectangle object);
int level;
bool isNull = true;
- GraphicsData gfxDataRect;
+ Rectangle rect;
Rectangle bounds;
};
diff --git a/src/dna.cpp b/src/dna.cpp
index 8cef6e0..ac2d500 100644
--- a/src/dna.cpp
+++ b/src/dna.cpp
@@ -14,6 +14,10 @@ DNA::DNA(std::string s)
speed = CREATURE_SPEED;
mutationPercent = CREATURE_MUTATION_PERCENT;
mutationChance = CREATURE_MUTATION_CHANCE;
+ appearance.sides = CREATURE_SIDES;
+ appearance.red = 0;
+ appearance.green = 1;
+ appearance.blue = 1;
}
else if(s == "resource"){
type = RESOURCE_TYPE;
@@ -27,6 +31,10 @@ DNA::DNA(std::string s)
speed = 0;
mutationPercent = 0;
mutationChance = 0;
+ appearance.sides = RESOURCE_SIDES;
+ appearance.red = 0;
+ appearance.green = 1;
+ appearance.blue = 0;
}
}
@@ -44,6 +52,10 @@ DNA DNA::combine(DNA D)
N.speed = (speed + D.speed)/2;
N.mutationPercent = (mutationPercent + D.mutationPercent)/2;
N.mutationChance = (mutationChance + D.mutationChance)/2;
+ N.appearance.sides = CREATURE_SIDES;
+ N.appearance.red = 0;
+ N.appearance.green = 1;
+ N.appearance.blue = 1;
if(roll(mutationChance)){
float pn;
diff --git a/src/list.cpp b/src/list.cpp
index 826208e..d4b9529 100644
--- a/src/list.cpp
+++ b/src/list.cpp
@@ -64,21 +64,18 @@ void List::Place()
resources.push_back(X);
}
- for(std::list<Organism>::iterator it = creatures.begin(); it!= creatures.end(); it++){
- it->Place();
+ for(std::list<Organism>::iterator it = creatures.begin(); it!= creatures.end(); it++)
tree.insert(&(*it));;
- }
-
- for(std::list<Organism>::iterator it = resources.begin(); it!=resources.end(); it++){
- it->Place();
+
+ for(std::list<Organism>::iterator it = resources.begin(); it!=resources.end(); it++)
tree.insert(&(*it));;
- }
+
}
std::vector<Organism*> List::getNear(Organism c)
{
std::vector<Organism*> near;
- near = tree.retrieve(near, c.getGFXD());
+ near = tree.retrieve(near, c.getRectangle());
for(std::vector<Organism*>::iterator it = near.begin(); it!= near.end(); it++)
if(c.getBestSense() < Distance(c.getRectangle(),(*it)->getRectangle()))
@@ -87,7 +84,7 @@ std::vector<Organism*> List::getNear(Organism c)
return near;
}
-std::vector<GraphicsData> List::drawQuadTree()
+std::vector<Rectangle> List::drawQuadTree()
{
return tree.Draw();
}
diff --git a/src/main.cpp b/src/main.cpp
index 43ec1e4..cacd612 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -48,10 +48,10 @@ int main()
_spriteBatch.begin();
for(std::list<Organism>::iterator it = L.creatures.begin(); it != L.creatures.end(); it++)
- _spriteBatch.draw(it->getGFXD());
+ _spriteBatch.draw(it->getRectangle(),it->getVisuals());
for(std::list<Organism>::iterator it = L.resources.begin(); it != L.resources.end(); it++)
- _spriteBatch.draw(it->getGFXD());
+ _spriteBatch.draw(it->getRectangle(),it->getVisuals());
_spriteBatch.end();
_spriteBatch.renderBatch();
diff --git a/src/organism.cpp b/src/organism.cpp
index 27f6f8a..ee9f849 100644
--- a/src/organism.cpp
+++ b/src/organism.cpp
@@ -18,15 +18,6 @@ Organism::Organism(Rectangle r, DNA d)
pregnate = false;
hasTarget = false;
wander = false;
-
- if(myDNA.type == CREATURE_TYPE){
- 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);
- }
- else
- gfxData = GraphicsData(rect.x, rect.y, 0, 1, 0, RESOURCE_SIDES);
}
void Organism::Behavior()
@@ -100,7 +91,6 @@ void Organism::checkTarget()
hasTarget = false;
}
-
void Organism::Action()
{
if(hasTarget){
@@ -185,9 +175,3 @@ void Organism::takeBite(int bite)
{
health-=bite;
}
-
-void Organism::Place()
-{
- gfxData.x = rect.x;
- gfxData.y = rect.y;
-}
diff --git a/src/quadtree.cpp b/src/quadtree.cpp
index ada839a..6366b4e 100644
--- a/src/quadtree.cpp
+++ b/src/quadtree.cpp
@@ -8,12 +8,8 @@ Quadtree::Quadtree(int pLevel,Rectangle pBounds){
bounds = pBounds;
isNull=false;
nodes = new Quadtree[4];
- gfxDataRect.x = pBounds.x * 1.01;
- gfxDataRect.y = pBounds.y * 1.05;
- gfxDataRect.r = 255;
- gfxDataRect.g = 0;
- gfxDataRect.b = 255;
- gfxDataRect.sides = pBounds.h;
+ rect.x = pBounds.x * 1.01;
+ rect.y = pBounds.y * 1.05;
}
void Quadtree::clear(){
@@ -53,8 +49,7 @@ void Quadtree::clear(){
nodes[3] = Q3;
}
-
-int Quadtree::getIndex(GraphicsData object) {
+int Quadtree::getIndex(Rectangle object) {
int index = -1;
bool topQuadrant = (object.y > bounds.y);
@@ -78,7 +73,7 @@ int Quadtree::getIndex(GraphicsData object) {
void Quadtree::insert(Organism* iter){
if (!nodes[0].isNull) {
- int index = getIndex((*iter).getGFXD());
+ int index = getIndex((*iter).getRectangle());
if (index != -1) {
nodes[index].insert(iter);
return;
@@ -93,7 +88,7 @@ void Quadtree::insert(Organism* iter){
int index;
for(std::vector<Organism*>::iterator it = objects.begin(); it!=objects.end();it++){
- index = getIndex((*it)->getGFXD());
+ index = getIndex((*it)->getRectangle());
if (index != -1) {
nodes[index].insert(*it);
objects.erase(it--);
@@ -102,31 +97,31 @@ void Quadtree::insert(Organism* iter){
}
}
-std::vector<GraphicsData> Quadtree::Draw(){
- std::vector<GraphicsData> retdat;
+std::vector<Rectangle> Quadtree::Draw(){
+ std::vector<Rectangle> retdat;
int i;
for (i = 0; i < 4; i++) {
if (!nodes[i].isNull) {
- std::vector<GraphicsData> temp = nodes[i].Draw();
+ std::vector<Rectangle> temp = nodes[i].Draw();
retdat.insert(retdat.end(), temp.begin(), temp.end());
}
}
if (!nodes[0].isNull)
- retdat.emplace_back(nodes[0].gfxDataRect);
+ retdat.emplace_back(nodes[0].rect);
if (!nodes[1].isNull)
- retdat.emplace_back(nodes[1].gfxDataRect);
+ retdat.emplace_back(nodes[1].rect);
if (!nodes[2].isNull)
- retdat.emplace_back(nodes[2].gfxDataRect);
+ retdat.emplace_back(nodes[2].rect);
if (!nodes[3].isNull)
- retdat.emplace_back(nodes[3].gfxDataRect);
+ retdat.emplace_back(nodes[3].rect);
- retdat.emplace_back(gfxDataRect);
+ retdat.emplace_back(rect);
return retdat;
}
-std::vector<Organism*> Quadtree::retrieve(std::vector<Organism*> returnObjects, GraphicsData obj) {
+std::vector<Organism*> Quadtree::retrieve(std::vector<Organism*> returnObjects, Rectangle obj) {
int index = getIndex(obj);
if (index != -1 && !nodes[0].isNull)
returnObjects = nodes[index].retrieve(returnObjects, obj);
diff --git a/src/spritebatch.cpp b/src/spritebatch.cpp
index f0a82e6..c756494 100644
--- a/src/spritebatch.cpp
+++ b/src/spritebatch.cpp
@@ -27,9 +27,9 @@ void SpriteBatch::end()
createRenderBatches();
}
-void SpriteBatch::draw(const GraphicsData& gfxData)
+void SpriteBatch::draw(Rectangle r, DNA::Visuals v)
{
- _gfx.emplace_back(gfxData);
+ _gfx.emplace_back(std::make_pair(r,v));
}
void SpriteBatch::renderBatch()
@@ -62,12 +62,12 @@ void SpriteBatch::createRenderBatches()
//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;
+ vertices[cv++] = _gfxPtr[0]->first.x;
+ vertices[cv++] = _gfxPtr[0]->first.y;
+ vertices[cv++] = _gfxPtr[0]->second.red;
+ vertices[cv++] = _gfxPtr[0]->second.green;
+ vertices[cv++] = _gfxPtr[0]->second.blue;
+ vertices[cv++] = _gfxPtr[0]->second.sides;
offset += 6;
@@ -82,12 +82,12 @@ void SpriteBatch::createRenderBatches()
// 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;
+ vertices[cv++] = _gfxPtr[cg]->first.x;
+ vertices[cv++] = _gfxPtr[cg]->first.y;
+ vertices[cv++] = _gfxPtr[cg]->second.red;
+ vertices[cv++] = _gfxPtr[cg]->second.green;
+ vertices[cv++] = _gfxPtr[cg]->second.blue;
+ vertices[cv++] = _gfxPtr[cg]->second.sides;
offset += 6;
}
@@ -98,7 +98,6 @@ void SpriteBatch::createRenderBatches()
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);
}
@@ -131,8 +130,10 @@ void SpriteBatch::createVertexArray()
//glEnableVertexAttribArray(2);
//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(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);
}