aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/Texture.h22
-rw-r--r--include/common.h7
-rw-r--r--include/entities.h1
-rw-r--r--main.cpp5
-rw-r--r--src/Texture.cpp40
-rw-r--r--src/common.cpp25
-rw-r--r--src/entities.cpp122
-rw-r--r--src/inventory.cpp2
8 files changed, 162 insertions, 62 deletions
diff --git a/include/Texture.h b/include/Texture.h
new file mode 100644
index 0000000..45c8df1
--- /dev/null
+++ b/include/Texture.h
@@ -0,0 +1,22 @@
+#ifndef TEXTURE_H
+#define TEXTURE_H
+
+#include <common.h>
+
+namespace Texture{
+ GLuint loadTexture(const char *fileName);
+}
+
+class Texturec{
+public:
+ Texturec(uint amt, ...);
+ void bindNext();
+ void bindPrev();
+
+ GLuint *image;
+private:
+ int texState;
+
+};
+
+#endif //TEXTURE_H \ No newline at end of file
diff --git a/include/common.h b/include/common.h
index d5f1456..e46cf86 100644
--- a/include/common.h
+++ b/include/common.h
@@ -22,6 +22,11 @@
#include <SDL2/SDL_mixer.h>
/*
+ * Include file headers
+*/
+#include <Texture.h>
+
+/*
* Create a basic 2-point structure for coordinate saving
*/
@@ -88,7 +93,7 @@ extern unsigned int deltaTime;
*
*/
-GLuint loadTexture(const char *fileName);
+//GLuint loadTexture(const char *fileName);
/*
* Prints a formatted debug message to the console, along with the callee's file and line
diff --git a/include/entities.h b/include/entities.h
index 30a0b0c..e71789a 100644
--- a/include/entities.h
+++ b/include/entities.h
@@ -59,6 +59,7 @@ public:
char* name;
GENDER gender;
GLuint texture[3]; //TODO: ADD TEXTURES
+ Texturec* tex;
void spawn(float, float);
diff --git a/main.cpp b/main.cpp
index 9fc2c01..69cad20 100644
--- a/main.cpp
+++ b/main.cpp
@@ -381,7 +381,7 @@ int main(int argc, char *argv[]){
* Load a temporary background image.
*/
- bgImage=loadTexture("assets/bg.png");
+ bgImage=Texture::loadTexture("assets/bg.png");
/*
* Load sprites used in the inventory menu. See src/inventory.cpp
@@ -625,7 +625,7 @@ void render(){
**************************/
/*
- * These next two function finish the rendering\
+ * These next two function finish the rendering
*
* glPopMatrix This anchors all of the matrices and blends them to a single
* matrix so the renderer can draw this to the screen, since screens
@@ -659,6 +659,7 @@ void logic(){
* click detection is done as well for NPC/player interaction.
*
*/
+ std::cout << "Game Loop: "<< loops << std::endl;
for(int i=0;i<=entity.size();i++){
diff --git a/src/Texture.cpp b/src/Texture.cpp
new file mode 100644
index 0000000..93a3792
--- /dev/null
+++ b/src/Texture.cpp
@@ -0,0 +1,40 @@
+#include <Texture.h>
+
+namespace Texture{
+ GLuint loadTexture(const char *fileName){
+ SDL_Surface *image = IMG_Load(fileName);
+
+ if(!image)return 0;
+ DEBUG_printf("Loaded image file: %s\n", fileName);
+ unsigned object = 0; //creates a new unsigned variable for the texture
+
+ glGenTextures(1, &object); //turns "object" into a texture
+ glBindTexture(GL_TEXTURE_2D, object); //binds "object" to the top of the stack
+
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //sets the "min" filter
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //the the "max" filter of the stack
+
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); //Wrap the texture to the matrix
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); //Wrap the texutre to the matrix
+
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); //sets the texture to the image file loaded above
+
+ SDL_FreeSurface(image); //Free surface
+ return object;
+ }
+}
+
+Texturec::Texturec(uint amt, ...){
+ image = new GLuint(amt);
+ va_list fNames;
+ va_start(fNames, amt);
+ for(int i = 0; i < amt; i++){
+ char* f = va_arg(fNames, char*);
+ image[i] = Texture::loadTexture(f);
+ }
+ va_end(fNames);
+}
+
+void Texturec::bindNext(){
+ //glBindTexture(GL_TEXTURE_2D);
+} \ No newline at end of file
diff --git a/src/common.cpp b/src/common.cpp
index 660ed9d..cd568ad 100644
--- a/src/common.cpp
+++ b/src/common.cpp
@@ -1,31 +1,8 @@
#include <common.h>
+#include <cstdio>
#define DEBUG
-GLuint loadTexture(const char *fileName){
- SDL_Surface *image = IMG_Load(fileName);
-
- if(!image)return 0;
-#ifdef DEBUG
- DEBUG_printf("Loaded image file: %s\n", fileName);
-#endif // DEBUG
- unsigned object = 0; //creates a new unsigned variable for the texture
-
- glGenTextures(1, &object); //turns "object" into a texture
- glBindTexture(GL_TEXTURE_2D, object); //binds "object" to the top of the stack
-
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //sets the "min" filter
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //the the "max" filter of the stack
-
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); //Wrap the texture to the matrix
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); //Wrap the texutre to the matrix
-
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); //sets the texture to the image file loaded above
-
- SDL_FreeSurface(image); //Free surface
- return object;
-}
-
void DEBUG_prints(const char* file, int line, const char *s,...){
va_list args;
printf("%s:%d: ",file,line);
diff --git a/src/entities.cpp b/src/entities.cpp
index ce983e8..7a4da65 100644
--- a/src/entities.cpp
+++ b/src/entities.cpp
@@ -33,10 +33,11 @@ Player::Player(){ //sets all of the player specific traits on object creation
subtype = 0;
maxHealth = 100;
health = maxHealth;
- texture[0] = loadTexture("assets/player.png");
- texture[1] = loadTexture("assets/player1.png");
- texture[2] = loadTexture("assets/player2.png");
+ alive = true;
+ ground = false;
+ near = true;
inv = new Inventory(PLAYER_INV_SIZE);
+ tex = new Texturec(3, "assets/player.png", "assets/player1.png", "assets/player2.png");
}
NPC::NPC(){ //sets all of the NPC specific traits on object creation
@@ -45,16 +46,22 @@ NPC::NPC(){ //sets all of the NPC specific traits on object creation
speed = 1;
type = NPCT; //sets type to npc
subtype = 0;
- texture[0] = loadTexture("assets/NPC.png");
+ alive = true;
+ canMove = true;
+ near = false;
+ texture[0] = Texture::loadTexture("assets/NPC.png");
texture[1] = 0;
texture[2] = 0;
+ //tex = new Texturec("assets/NPC.png");
inv = new Inventory(NPC_INV_SIZE);
}
Structures::Structures(){ //sets the structure type
type = STRUCTURET;
speed = 0;
- texture[0] = loadTexture("assets/house1.png");
+ alive = true;
+ near = false;
+ texture[0] = Texture::loadTexture("assets/house1.png");
texture[1] = 0;
texture[2] = 0;
}
@@ -65,8 +72,11 @@ Mob::Mob(){
speed = 1;
type = MOBT; //sets type to MOB
subtype = 1; //SKIRL
- texture[0] = loadTexture("assets/rabbit.png");
- texture[1] = loadTexture("assets/rabbit1.png");
+ alive = true;
+ canMove = true;
+ near = false;
+ texture[0] = Texture::loadTexture("assets/rabbit.png");
+ texture[1] = Texture::loadTexture("assets/rabbit1.png");
texture[2] = 0;
inv = new Inventory(NPC_INV_SIZE);
}
@@ -105,25 +115,22 @@ void Entity::draw(void){ //draws the entities
}
}
if(ground == 0){
- glBindTexture(GL_TEXTURE_2D, texture[1]);
- }
- if(ground == 0){
- glBindTexture(GL_TEXTURE_2D, texture[1]);
+ glBindTexture(GL_TEXTURE_2D, tex->image[0]);
}else if(vel.x != 0){
switch(texState){
case 0:
- glBindTexture(GL_TEXTURE_2D,texture[1]);
+ glBindTexture(GL_TEXTURE_2D,tex->image[1]);
break;
case 1:
- glBindTexture(GL_TEXTURE_2D,texture[0]);
+ glBindTexture(GL_TEXTURE_2D,tex->image[0]);
break;
case 2:
- glBindTexture(GL_TEXTURE_2D,texture[2]);
+ glBindTexture(GL_TEXTURE_2D,tex->image[2]);
break;
}
}
else{
- glBindTexture(GL_TEXTURE_2D,texture[0]);
+ glBindTexture(GL_TEXTURE_2D,tex->image[0]);
}
}else if(type == MOBT){
switch(subtype){
@@ -137,7 +144,9 @@ void Entity::draw(void){ //draws the entities
default:
break;
}
- }else{
+ }/*else if(type == NPCT){
+ glBindTexture(GL_TEXTURE_2D, tex->image);
+ }*/else{
glBindTexture(GL_TEXTURE_2D,texture[0]);
}
glColor3ub(255,255,255);
@@ -192,8 +201,29 @@ void Player::interact(){ //the function that will cause the player to search for
}
+/*
+ * NPC::wander, this makes the npcs wander around the near area
+ *
+ * timeRun This variable is the amount of gameloop ticks the entity will wander for
+ *
+ * *v This is a pointer to whatever vec2 value is passed to it, usually the value
+ * passed is the entities vec2 for coordinates. Since the value is a pointer
+ * the memory address passed to it is directly modified.
+*/
+
void NPC::wander(int timeRun, vec2 *v){ //this makes the entites wander about
+ /*
+ * Direction is the variable that decides what direction the entity will travel in
+ * the value is either -1, 0, or 1. -1 being left, 0 means that the npc will stay still
+ * and a value of 1 makes the entity move to the right
+ */
static int direction; //variable to decide what direction the entity moves
+ /*
+ * Ticks to use is a variable in the entity class that counts the total ticks that need to be used
+ *
+ * This loop only runs when ticksToUse is 0, this means that the speed, direction, etc... Will be
+ * calculated only after the npc has finished his current walking state
+ */
if(ticksToUse == 0){
ticksToUse = timeRun;
v->x = .008*HLINE; //sets the inital velocity of the entity
@@ -232,6 +262,16 @@ void NPC::interact(){ //have the npc's interact back to the player
}
}
+/*
+ * This spawns the structures
+ *
+ * Structures::spawn This allows us to spawn buildings and large amounts of entities with it.
+ * Passed are the type and x and y coordinates. These set the x and y coords
+ * of the structure being spawned, the type pass just determines what rules to follow
+ * when spawing the structure and entities. In theory this function can also spawn
+ * void spawn points for large amounts of entities. This would allow for the spawn
+ * point to have non-normal traits so it could be invisible or invincible...
+*/
unsigned int Structures::spawn(_TYPE t, float x, float y){ //spawns a structure based off of type and coords
loc.x = x;
loc.y = y;
@@ -240,15 +280,21 @@ unsigned int Structures::spawn(_TYPE t, float x, float y){ //spawns a structure
health = maxHealth = 1;
/*VILLAGE*/
- //spawns a village
- //spawns between 1 and 5 villagers around the village
if(type == STRUCTURET){
loc.y=100;
width = 50 * HLINE;
height = 40 * HLINE;
+ /*
+ * tempN is the amount of entities that will be spawned in the village. As of 10/21/2015 the village
+ * can spawn bewteen 2 and 7 villagers for the starting hut.
+ */
int tempN = (getRand() % 5 + 2); //amount of villagers that will spawn
for(int i=0;i<tempN;i++){
+ /*
+ * This is where the entities actually spawn.
+ * A new entity is created with type NPC so polymorphism can be used
+ */
entity.push_back(new NPC()); //create a new entity of NPC type
NPCp(entity[entity.size()-1])->spawn(loc.x + (float)(i - 5),100); //sets the position of the villager around the village
}
@@ -256,22 +302,30 @@ unsigned int Structures::spawn(_TYPE t, float x, float y){ //spawns a structure
}
}
+/*
+ * Mob::wander this is the function that makes the mobs wander around
+ *
+ * See NPC::wander for the explaination of the arguments variables
+*/
void Mob::wander(int timeRun, vec2* v){
- if(subtype == 1){ //SKIRL
- static int direction; //variable to decide what direction the entity moves
- if(ticksToUse == 0){
- ticksToUse = timeRun;
- direction = (getRand() % 3 - 1); //sets the direction to either -1, 0, 1
- //this lets the entity move left, right, or stay still
- if(direction==0)ticksToUse/=2;
- v->x *= direction; //changes the velocity based off of the direction
- }
- if(ground && direction != 0){
- v->y=.15;
- loc.y+=HLINE*.25;
- ground=false;
- v->x = (.07*HLINE)*direction; //sets the inital velocity of the entity
- }
- ticksToUse--; //removes one off of the entities timer
+ switch(subtype){ //SKIRL
+ case 1:
+ static int direction; //variable to decide what direction the entity moves
+ if(ticksToUse == 0){
+ ticksToUse = timeRun;
+ direction = (getRand() % 3 - 1); //sets the direction to either -1, 0, 1
+ //this lets the entity move left, right, or stay still
+ if(direction==0)ticksToUse/=2;
+ v->x *= direction; //changes the velocity based off of the direction
+ }
+ if(ground && direction != 0){
+ v->y=.15;
+ loc.y+=HLINE*.25;
+ ground=false;
+ v->x = (.07*HLINE)*direction; //sets the inital velocity of the entity
+ }
+ ticksToUse--; //removes one off of the entities timer
+ break;
+ default:break;
}
}
diff --git a/src/inventory.cpp b/src/inventory.cpp
index b9b4859..1adcbab 100644
--- a/src/inventory.cpp
+++ b/src/inventory.cpp
@@ -24,7 +24,7 @@ unsigned int initInventorySprites(void){
unsigned int i,loadCount=0;
ITEM_TEX=(GLuint *)calloc(ITEM_COUNT,sizeof(GLuint));
for(i=0;i<ITEM_COUNT;i++){
- if((ITEM_TEX[i]=loadTexture(ITEM_SPRITE[i+1])))loadCount++;
+ if((ITEM_TEX[i]=Texture::loadTexture(ITEM_SPRITE[i+1])))loadCount++;
}
#ifdef DEBUG
DEBUG_printf("Loaded %u/%u item texture(s).\n",loadCount,ITEM_COUNT);