- 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
#ifndef QUEST_H\r
#define QUEST_H\r
\r
-#include <vector>\r
-#include <cstdlib>\r
#include <cstring>\r
\r
+#include <common.h>\r
#include <inventory.h>\r
\r
#define DEBUG\r
bool hasQuest(const char *t);\r
};\r
\r
+#include <entities.h>\r
+\r
#endif // QUEST_H\r
#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
#define ENTITIES_H
#include <common.h>
+#include <Quest.h>
#include <inventory.h>
#define DEBUG
#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:
#ifndef INVENTORY_H
#define INVENTORY_H
-#include <cstdlib>
+#include <common.h>
#define DEBUG
#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
#define WORLD_H
#include <common.h> // For HLINE, vec2, OpenGL utilities, etc.
+#include <entities.h>
typedef struct {
vec2 p1,p2;
-#include <common.h>\r
+#include <Quest.h>\r
\r
#define TITLE Quest(\r
#define DESC ,\r
#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
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;
loc.y = y;
type = t;
alive = true;
+ health = maxHealth = 1;
/*VILLAGE*/
//spawns a village
#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",
return -1;
}
-#include <entities.h>
-extern Player *player;
-
void Inventory::draw(void){
unsigned int i=0;
float y=SCREEN_HEIGHT/2,xoff;
#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 :)
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;
#include <world.h>
-#include <ui.h>
#define getWidth(w) ((w->lineCount-GEN_INC)*HLINE) // Calculates the width of world 'w'
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){
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))||