aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Changelog8
-rw-r--r--include/Quest.h5
-rw-r--r--include/common.h94
-rw-r--r--include/entities.h14
-rw-r--r--include/inventory.h2
-rw-r--r--include/ui.h4
-rw-r--r--include/world.h1
-rw-r--r--src/Quest.cpp2
-rw-r--r--src/common.cpp4
-rw-r--r--src/entities.cpp4
-rw-r--r--src/inventory.cpp6
-rw-r--r--src/ui.cpp5
-rw-r--r--src/world.cpp127
13 files changed, 206 insertions, 70 deletions
diff --git a/Changelog b/Changelog
index 3a491eb..f21582b 100644
--- a/Changelog
+++ b/Changelog
@@ -139,3 +139,11 @@
- andy's laptop 'can' boot
- added 200+ lines of documentation to main.cpp
+10/21/2015:
+===========
+
+ - andy's laptop works :)
+ - finished documenting main.cpp, bringing it to 759 lines
+ - began documenting entities.h/.cpp and world.h/.cpp
+ - fixed structure physics
+ - improved include locations
diff --git a/include/Quest.h b/include/Quest.h
index bbcf7ee..75de64c 100644
--- a/include/Quest.h
+++ b/include/Quest.h
@@ -1,10 +1,9 @@
#ifndef QUEST_H
#define QUEST_H
-#include <vector>
-#include <cstdlib>
#include <cstring>
+#include <common.h>
#include <inventory.h>
#define DEBUG
@@ -28,4 +27,6 @@ public:
bool hasQuest(const char *t);
};
+#include <entities.h>
+
#endif // QUEST_H
diff --git a/include/common.h b/include/common.h
index 183b1cb..d5f1456 100644
--- a/include/common.h
+++ b/include/common.h
@@ -1,63 +1,101 @@
#ifndef COMMON_H
#define COMMON_H
-///THIS FILE IS USED FOR VARIABLES THAT WILL BE ACCESED BY MULTIPLE CLASSES/FILES
+/*
+ * Include basic C/C++ facilities
+*/
#include <iostream>
+#include <cstdlib>
#include <vector>
#include <math.h>
-#include <cstdlib>
-#include <SDL2/SDL.h>
+
+/*
+ * Include GLEW and the SDL 2 headers
+*/
+
#include <GL/glew.h>
+
+#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
-typedef struct { float x; float y; }vec2;
+/*
+ * Create a basic 2-point structure for coordinate saving
+*/
-enum _TYPE { //these are the main types of entities
- STRUCTURET = -1,
- PLAYERT = 0,
- NPCT = 1,
- MOBT = 2
-};
+typedef struct {
+ float x;
+ float y;
+} vec2;
-enum GENDER{
- MALE,
- FEMALE,
- NONE
-};
-
-#include <Quest.h>
-#include <entities.h>
+/*
+ * Define the game's name (displayed in the window title),
+ * the desired window dimensions,
+ * and whether or not we want the window to be fullscreen.
+*/
#define GAME_NAME "Independent Study v.0.2 alpha"
#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 720
+
//#define FULLSCREEN
-#define HLINE 3 //base unit of the world
+/*
+ * Define the length of a single HLINE.
+ *
+ * The game has a great amount of elements that need to be drawn or detected, and having each
+ * of them use specific hard-coded numbers would be painful to debug. As a solution, this
+ * definition was made. Every item being drawn to the screen and most object detection/physic
+ * handling is done based off of this number. Increasing it will give the game a zoomed-in
+ * feel, while decreasing it will do the opposite.
+ *
+*/
+
+#define HLINE 3 // 3 as in 3 pixels
+
+/*
+ * Define 'our' random number generation library. Eventually these macros will be replaced
+ * with actual functions.
+ *
+*/
#define initRand(s) srand(s)
#define getRand() rand()
+/*
+ * At the bottom of this header is the prototype for DEBUG_prints, which writes a formatted
+ * string to the console containing the callee's file and line number. This macro simplifies
+ * it to a simple printf call.
+ *
+ * DEBUG must be defined for this macro to function.
+ *
+*/
+
#define DEBUG_printf( message, ...) DEBUG_prints(__FILE__, __LINE__, message, __VA_ARGS__ )
-template<typename T, size_t N> //this fuction returns the size of any array
-int eAmt(T (&)[N]){return N;}
+/*
+ * References the variable in main.cpp, used for smoother drawing.
+*/
-extern bool gameRunning;
extern unsigned int deltaTime;
-extern unsigned int loops;
-extern FILE* config;
-extern FILE* names;
-
-extern Mix_Music *music;
-extern Mix_Chunk *horn;
+/*
+ * Loads an image from the given file path and attempts to make a texture out of it. The
+ * resulting GLuint is returned (used to recall the texture in glBindTexture).
+ *
+*/
GLuint loadTexture(const char *fileName);
+
+/*
+ * Prints a formatted debug message to the console, along with the callee's file and line
+ * number.
+ *
+*/
+
void DEBUG_prints(const char* file, int line, const char *s,...);
#endif // COMMON_H
diff --git a/include/entities.h b/include/entities.h
index 655390b..fab2ca5 100644
--- a/include/entities.h
+++ b/include/entities.h
@@ -2,6 +2,7 @@
#define ENTITIES_H
#include <common.h>
+#include <Quest.h>
#include <inventory.h>
#define DEBUG
@@ -11,7 +12,18 @@
#define PLAYER_INV_SIZE 30 // The size of the player's inventory
#define NPC_INV_SIZE 3 // Size of an NPC's inventory
-extern FILE* names;
+enum _TYPE { //these are the main types of entities
+ STRUCTURET = -1,
+ PLAYERT = 0,
+ NPCT = 1,
+ MOBT = 2
+};
+
+enum GENDER{
+ MALE,
+ FEMALE,
+ NONE
+};
class Entity{
public:
diff --git a/include/inventory.h b/include/inventory.h
index c477b8e..cb3e59d 100644
--- a/include/inventory.h
+++ b/include/inventory.h
@@ -1,7 +1,7 @@
#ifndef INVENTORY_H
#define INVENTORY_H
-#include <cstdlib>
+#include <common.h>
#define DEBUG
diff --git a/include/ui.h b/include/ui.h
index 6f23a78..739aba0 100644
--- a/include/ui.h
+++ b/include/ui.h
@@ -4,6 +4,10 @@
#include <common.h>
#include <cstdarg> // For putText()
+#include <world.h> // World-switching stuff
+#include <ft2build.h> // FreeType stuff
+#include FT_FREETYPE_H
+
#define DEBUG
namespace ui { // Functions are kept in a namespace simply
diff --git a/include/world.h b/include/world.h
index 4acb9ee..29c7822 100644
--- a/include/world.h
+++ b/include/world.h
@@ -2,6 +2,7 @@
#define WORLD_H
#include <common.h> // For HLINE, vec2, OpenGL utilities, etc.
+#include <entities.h>
typedef struct {
vec2 p1,p2;
diff --git a/src/Quest.cpp b/src/Quest.cpp
index 2eed1ae..a361418 100644
--- a/src/Quest.cpp
+++ b/src/Quest.cpp
@@ -1,4 +1,4 @@
-#include <common.h>
+#include <Quest.h>
#define TITLE Quest(
#define DESC ,
diff --git a/src/common.cpp b/src/common.cpp
index 8dcbd11..660ed9d 100644
--- a/src/common.cpp
+++ b/src/common.cpp
@@ -1,11 +1,13 @@
#include <common.h>
+#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);
+ DEBUG_printf("Loaded image file: %s\n", fileName);
#endif // DEBUG
unsigned object = 0; //creates a new unsigned variable for the texture
diff --git a/src/entities.cpp b/src/entities.cpp
index 2bc5a61..9b87150 100644
--- a/src/entities.cpp
+++ b/src/entities.cpp
@@ -5,6 +5,9 @@ extern std::vector<Entity*>entity;
extern std::vector<NPC>npc;
extern std::vector<Structures>build;
+extern FILE* names;
+extern unsigned int loops;
+
void Entity::spawn(float x, float y){ //spawns the entity you pass to it based off of coords and global entity settings
loc.x = x;
loc.y = y;
@@ -245,6 +248,7 @@ unsigned int Structures::spawn(_TYPE t, float x, float y){ //spawns a structure
loc.y = y;
type = t;
alive = true;
+ health = maxHealth = 1;
/*VILLAGE*/
//spawns a village
diff --git a/src/inventory.cpp b/src/inventory.cpp
index 3690de5..b9b4859 100644
--- a/src/inventory.cpp
+++ b/src/inventory.cpp
@@ -1,8 +1,11 @@
#include <inventory.h>
+#include <entities.h>
#include <ui.h>
#define ITEM_COUNT 2 // Total number of items that actually exist
+extern Player *player;
+
const char *itemName[]={
"\0",
"Dank Maymay",
@@ -80,9 +83,6 @@ int Inventory::takeItem(ITEM_ID id,unsigned char count){
return -1;
}
-#include <entities.h>
-extern Player *player;
-
void Inventory::draw(void){
unsigned int i=0;
float y=SCREEN_HEIGHT/2,xoff;
diff --git a/src/ui.cpp b/src/ui.cpp
index a62086e..256f5d7 100644
--- a/src/ui.cpp
+++ b/src/ui.cpp
@@ -1,7 +1,4 @@
#include <ui.h>
-#include <world.h> // World-switching stuff
-#include <ft2build.h> // FreeType stuff
-#include FT_FREETYPE_H
#define SDL_KEY e.key.keysym.sym // Keeps the code neater :)
@@ -11,6 +8,8 @@ extern World *currentWorld; // should/must also be defined in main.cpp
extern std::vector<int (*)(NPC *)> AIpreload; // see entities.cpp
extern std::vector<void *> AIpreaddr; //
+extern bool gameRunning;
+
static FT_Library ftl; // Variables for the FreeType library and stuff
static FT_Face ftf;
static GLuint ftex;
diff --git a/src/world.cpp b/src/world.cpp
index 3b1fd87..bd58573 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -1,5 +1,4 @@
#include <world.h>
-#include <ui.h>
#define getWidth(w) ((w->lineCount-GEN_INC)*HLINE) // Calculates the width of world 'w'
@@ -33,38 +32,110 @@ World::World(void){
void World::generate(unsigned int width){ // Generates the world and sets all variables contained in the World class.
unsigned int i; // Used for 'for' loops
float inc; // See line 40
- lineCount=width+GEN_INC; // Sets line count to the desired width plus GEN_INC to remove incorrect line calculations.
- if(lineCount<=0)abort();
- line=(struct line_t *)calloc(lineCount,sizeof(struct line_t)); // Allocate memory for the array 'line'
+ /*
+ * Calculate the world's real width. The current form of generation fails to generate
+ * the last GEN_INC lines, so we offset those making the real real width what was passed
+ * to this function.
+ *
+ * Abort if the width is invalid.
+ *
+ */
+
+ if((lineCount = width + GEN_INC) <= 0)
+ abort();
+
+ /*
+ * Allocate enough memory for the world to be stored.
+ */
+
+ line=(struct line_t *)calloc(lineCount,sizeof(struct line_t));
- line[0].y=80; // Sets a starting value for the world generation to be based off of
- for(i=GEN_INC;i<lineCount;i+=GEN_INC){ // For every GEN_INCth line in the array 'line'
- line[i].y=rand()%8-4+line[i-GEN_INC].y; // Generate a y value for it, and correct it if it is too high or low.
- if(line[i].y<60)line[i].y=60; // y value minimum
- if(line[i].y>110)line[i].y=110; // y value maximum
+ /*
+ * Set an initial y to base generation off of, as generation references previous lines.
+ */
+
+ line[0].y=80;
+
+ /*
+ * Populate every GEN_INCth line structure. The remaining lines will be based off of these.
+ */
+
+ for(i=GEN_INC;i<lineCount;i+=GEN_INC){
+
+ /*
+ * Generate a y value, ensuring it stays within a reasonable range.
+ */
+
+ line[i].y=rand() % 8 - 4 + line[i-GEN_INC].y; // Add +/- 4 to the previous line
+ if(line[i].y < 60)line[i].y = 60; // Minimum bound
+ else if(line[i].y > 110)line[i].y = 110; // Maximum bound
+
}
- for(i=0;i<lineCount-GEN_INC;i++){ // Calculate the rest of the lines as well as set color values for them.
- if(!i||!(i%GEN_INC)){ // If this is one of the GEN_INCth lines that are already calculated
- inc=(line[i+GEN_INC].y-line[i].y)/(float)GEN_INC; // Calculate the slope between this line and the line ahead of it, then
- // divide it by the number of lines inbetween the two.
- }else{ // If this line's y hasn't been set yet
- line[i].y=line[i-1].y+inc; // Set it by incrementing the previous line's y by 'inc'.
+
+ /*
+ * Generate values for the remaining lines here.
+ */
+
+ for(i=0;i<lineCount-GEN_INC;i++){
+
+ /*
+ * Every GEN_INCth line calculate the slope between the current line and the one
+ * GEN_INC lines before it. This value is then divided into an increment that is
+ * added to lines between these two points resulting in a smooth slope.
+ *
+ */
+
+ if(!i||!(i%GEN_INC)){
+
+ inc=(line[i + GEN_INC].y - line[i].y) / (float)GEN_INC;
+
+ }else{
+
+ /*
+ * Add the increment to create the smooth slope.
+ */
+
+ line[i].y=line[i - 1].y + inc;
+
}
- line[i].color=rand()%20+100; // Generate a color for the dirt area of this line. This value will be used
- // in the form (where n represents the color) glColor3ub(n,n-50,n-100)
-
- line[i].gh[0]=(getRand()%16)/3.5+2; // Create a random grass height so it looks cool
- line[i].gh[1]=(getRand()%16)/3.5+2;
- line[i].gs=true;
+
+ /*
+ * Generate a color value for the line. This will be referenced in World->draw(),
+ * by setting an RGB value of color (red), color - 50 (green), color - 100 (blue).
+ */
+
+ line[i].color=rand() % 20 + 100; // 100 to 120
+
+ /*
+ * Each line has two 'blades' of grass, here we generate random heights for them.
+ */
+
+ line[i].gh[0]=(getRand() % 16) / 3.5 + 2; // Not sure what the range resolves to here...
+ line[i].gh[1]=(getRand() % 16) / 3.5 + 2; //
+
+ line[i].gs=true; // Show the blades of grass (modified by the player)
+
}
- x_start=0-getWidth(this)/2+GEN_INC/2*HLINE; // Calculate x_start (explained in world.h)
- behind=infront=NULL; // Set pointers to other worlds to NULL
- toLeft=toRight=NULL; // to avoid accidental calls to goWorld... functions
+
+ /*
+ * Calculate the x coordinate to start drawing this world from so that it is centered at (0,0).
+ */
+
+ x_start=0 - getWidth(this) / 2 + GEN_INC / 2 * HLINE;
+
+ /*
+ * Nullify pointers to other worlds.
+ */
+
+ behind =
+ infront =
+ toLeft =
+ toRight = NULL;
}
World::~World(void){
- free(line); // Free (de-allocate) the array 'line'
+ free(line);
}
void World::draw(Player *p){
@@ -161,14 +232,10 @@ void World::singleDetect(Entity *e){
e->alive=false;
}else if(e->alive){
i=(e->loc.x+e->width/2-x_start)/HLINE; // Calculate what line the player is currently on
- if(e->type==STRUCTURET||e->loc.y<line[i].y){
+ if(e->loc.y<line[i].y){
e->vel.y=0;
e->ground=true;
e->loc.y=line[i].y-.001*deltaTime;
- if(e->type==STRUCTURET){
- //std::cout<<e->loc.x<<" "<<e->loc.y<<std::endl;
- return;
- }
}else if(e->loc.y>line[i].y-.002*deltaTime){
for(i=0;i<platform.size();i++){
if(((e->loc.x+e->width>platform[i].p1.x)&(e->loc.x+e->width<platform[i].p2.x))||