]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
documentation part 2
authorClyne Sullivan <tullivan99@gmail.com>
Tue, 20 Oct 2015 12:49:52 +0000 (08:49 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Tue, 20 Oct 2015 12:49:52 +0000 (08:49 -0400)
Changelog
include/common.h
main.cpp

index 77edf73223fc76737946122f2dc437974579c054..3a491eb0b5b2200b0488dd26491bd8b4607224ee 100644 (file)
--- a/Changelog
+++ b/Changelog
        - fixed malloc/strlen bug that crashes the game on some linux systems
        - broke andy's linux system on his laptop, allowing for test game build on Windows
        - began extensive documentation in main.cpp
+       
+10/20/2015:
+===========
+
+       - andy's laptop 'can' boot
+       - added 200+ lines of documentation to main.cpp
+       
index 2cf98e445f37b6072b34b02d41afd321c1a44166..183b1cba849b9fa1fb7fc84c0b74a2f0c25ae94c 100644 (file)
@@ -31,8 +31,10 @@ enum GENDER{
 #include <Quest.h>
 #include <entities.h>
 
-#define SCREEN_WIDTH  1280
-#define SCREEN_HEIGHT 720
+#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
index 828c0b2d1ec7c13369f5e4204b5460ed12d0ffde..0b9f20e03471eedda6166a4c34e08ef328b40e9a 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -144,74 +144,187 @@ Mix_Chunk *horn;
 
 unsigned int loops = 0;        // Used for texture animation
 
-extern void initEverything(void);      // Sets up the worlds and NPCs, see gameplay.cpp
+/*
+ *     initEverything
+ * 
+ *     Before the main loop, things like the player, entities, and worlds should
+ *     be created. This game has not reached the point that these can be scripted
+ *     or programmed, so this function substitues for that. It is defined in
+ *     src/gameplay.cpp.
+ * 
+*/
+
+extern void initEverything(void);
+
+/*
+ *     mainLoop is in fact the main loop, which runs 'infinitely' (as long as gameRunning
+ *     is set). Each loop updates timing values (tickCount and deltaTime), runs logic()
+ *     if MSEC_PER_TICK milliseconds have passed, and then runs render().
+ * 
+ *     logic handles all user input and entity/world physics.
+ * 
+ *     render handles all drawing to the window, calling draw functions for everything.
+ * 
+*/
 
-void logic();                  // Handles entity logic and input events
-void render();                 // Draws everything to the screen
-void mainLoop(void);   // Runs render() and logic()
+void logic(void);
+void render(void);
+void mainLoop(void);
 
-// Used for millisecond timing
+/*
+ *     millis
+ * 
+ *     We've encountered many problems when attempting to create delays for triggering
+ *     the logic function. As a result, we decided on using the timing libraries given
+ *     by <chrono> in the standard C++ library. This function simply returns the amount
+ *     of milliseconds that have passed sine the epoch.
+ * 
+*/
 unsigned int millis(void){
        std::chrono::system_clock::time_point now=std::chrono::system_clock::now();
        return std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
 }
 
+/*******************************************************************************
+ * MAIN ************************************************************************
+ *******************************************************************************/
 int main(int argc, char *argv[]){
-       // Initialize SDL
+       /*
+        *      Initialize GLEW libraries, and exit if there was an error.
+        *      Not sure what they're for yet.
+        * 
+       */
+       
        if(glewInit() < 0){
                std::cout << "GLEW was not able to initialize! Error: " << std::endl;
                return -1;
        }
+       
+       /*
+        *      (Attempt to) Initialize SDL libraries so that we can use SDL facilities and eventually
+        *      make openGL calls. Exit if there was an error.
+        * 
+       */
+       
        if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0){
                std::cout << "SDL was not able to initialize! Error: " << SDL_GetError() << std::endl;
                return -1;
        }
+       
+       // Run SDL_Quit when main returns
     atexit(SDL_Quit);
-    // Initialize SDL_image
-    if(!(IMG_Init(IMG_INIT_PNG|IMG_INIT_JPG)&(IMG_INIT_PNG|IMG_INIT_JPG))){
-               std::cout<<"Could not init image libraries!\n"<<std::endl;
+    
+    /*
+     * (Attempt to) Initialize SDL_image libraries with IMG_INIT_PNG so that we can load PNG
+     * textures for the entities and stuff.
+     * 
+    */
+    
+    if(!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)){
+               std::cout << "Could not init image libraries! Error: " << IMG_GetError() << std::endl;
                return -1;
        }
+       
+       // Run IMG_Quit when main returns
        atexit(IMG_Quit);
-
-        //Initialize SDL_mixer 
+       
+       /*
+        *      (Attempt to) Initialize SDL_mixer libraries for loading and playing music/sound files.
+        * 
+       */
+       
        if(Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0){
-               std::cout << "SDL_mixer could not initialize! SDL_mixer Error: " << Mix_GetError() << std::endl;
+               std::cout << "SDL_mixer could not initialize! Error: " << Mix_GetError() << std::endl;
        }
+       
+       // Run Mix_Quit when main returns
        atexit(Mix_Quit);
-       // Enable double buffering
-    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
-    // Create the window
-    window = SDL_CreateWindow("Independent Study v.0.2 alpha", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL
+    
+    /*
+     * Create a window for SDL to draw to. Most parameters are the default, except for the
+     * following which are defined in include/common.h:
+     * 
+     * GAME_NAME               the name of the game that is displayed in the window title bar
+     * SCREEN_WIDTH    the width of the created window
+     * SCREEN_HEIGHT   the height of the created window
+     * FULLSCREEN              makes the window fullscreen
+     * 
+    */
+    
+    window = SDL_CreateWindow(GAME_NAME,
+                                                         SDL_WINDOWPOS_UNDEFINED,      // Spawn the window at random (undefined) x and y coordinates
+                                                         SDL_WINDOWPOS_UNDEFINED,      //
+                                                         SCREEN_WIDTH,
+                                                         SCREEN_HEIGHT,
+                                                         SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL
 #ifdef FULLSCREEN
                                                          | SDL_WINDOW_FULLSCREEN
 #endif // FULLSCREEN
                               );
+    
+    /*
+     * Exit if the window cannot be created
+    */
+    
     if(window==NULL){
-               std::cout << "The window failed to generate! Error: " << SDL_GetError() << std::endl;
-               std::cout << "Window address: "<<window<<std::endl;
+               std::cout << "The window failed to generate! SDL_Error: " << SDL_GetError() << std::endl;
         return -1;
     }
-    // Set OpenGL context
+    
+    /*
+     * Create the SDL OpenGL context. Once created, we are allowed to use OpenGL functions.
+     * Saving this context to mainGLContext does not appear to be necessary as mainGLContext
+     * is never referenced again.
+     * 
+    */
+    
     if((mainGLContext = SDL_GL_CreateContext(window)) == NULL){
-               std::cout << "The OpenGL context failed to initialize! Error: " << SDL_GetError() << std::endl;
+               std::cout << "The OpenGL context failed to initialize! SDL_Error: " << SDL_GetError() << std::endl;
         return -1;
     }
+       
+       /*
+        *      Initialize the FreeType libraries and select what font to use using functions from the ui
+        *      namespace, defined in include/ui.h and src/ui.cpp. These functions should abort with errors
+        *      if they have error.
+        * 
+       */
+       
+       ui::initFonts();
+       ui::setFontFace("ttf/Perfect DOS VGA 437.ttf");         // as in gamedev/ttf/<font>
+       
+       /*
+        *      Initialize the random number generator. At the moment, initRand is a macro pointing to libc's
+        *      srand, and its partner getRand points to rand. This is because having our own random number
+        *      generator may be favorable in the future, but at the moment is not implemented.
+        * 
+       */
+       
+       initRand(millis());
+
+       /*
+        *      Do some basic setup for openGL. Enable double buffering, switch to by-pixel coordinates,
+        *      setup the alpha channel for textures/transparency, and finally hide the system's mouse
+        *      cursor so that we may draw our own.
+        * 
+       */
+       
+       SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+       
+       glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
+       
+       glEnable(GL_BLEND);
+       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+       
+       SDL_ShowCursor(SDL_DISABLE);
 
-       ui::initFonts();                                                                        // Initialize text rendering with a font from ttf/
-       ui::setFontFace("ttf/Perfect DOS VGA 437.ttf");
-       initRand(millis());                                                                     // Initialize the random number generator with millis()
-
-       glViewport(0,0,SCREEN_WIDTH, SCREEN_HEIGHT);            // Switch to pixel-based rendering, not coordinates (the -1 to 1 stuff)
-       glEnable(GL_BLEND);                                                                     // Allow transparency
-       glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
-       SDL_ShowCursor(SDL_DISABLE);                                            // Hide mouse cursor so we can draw our own
-
-       /**
-       *               SHADERS
-       **/
+       /*
+        *      TODO - Initialize shaders n' stuff
+       */
 
-       /*GLuint fragShader;
+       /*
+       
+       GLuint fragShader;
        GLuint shaderProgram;
 
        const GLchar *shaderSource = "shader.frag";
@@ -219,35 +332,63 @@ int main(int argc, char *argv[]){
 
        shaderProgram = glCreateProgram();
        fragShader = glCreateShader(GL_FRAGMENT_SHADER);
+       
        glShaderSource(fragShader, 1, &shaderSource, NULL);
        glCompileShader(fragShader);
        glGetShaderiv(fragShader, GL_COMPILE_STATUS, &bufferln);
-       if (bufferln == GL_TRUE){
+       
+       if(bufferln == GL_TRUE){
                std::cout << "Error compiling shader" << std::endl;
        }
+       
        glAttachShader(shaderProgram, fragShader);
        glLinkProgram(shaderProgram);
        glValidateProgram(shaderProgram);
 
        //glEnable(GL_DEPTH_TEST);
-       //glEnable(GL_MULTISAMPLE);*/
+       //glEnable(GL_MULTISAMPLE);
+       
+       */
 
-       names = fopen("assets/names_en-us", "r+");      // Open the names file
+       /*
+        *      Open the names file containing potential names for NPCs and store it in the names file
+        *      pointer. This will be reference by getName in src/entities.cpp when NPCs are spawned. 
+        * 
+       */
        
-       initEverything();                                                       // Run world maker thing in src/gameplay.cpp
+       names = fopen("assets/names_en-us", "r+");
        
-       //Load music
-       music = Mix_LoadMUS("assets/BennyHillTheme.wav");
-       horn = Mix_LoadWAV("assets/air-horn-club-sample_1.wav");
-       if( music == NULL ){
-               printf( "Failed to load beat music! SDL_mixer Error: %s\n", Mix_GetError() );
-       }
-       Mix_VolumeMusic(15);
-       Mix_PlayMusic( music, -1 );
+       /*
+        *      Create all the worlds, entities, mobs, and the player. This function is defined in
+        *      src/gameplay.cpp
+        * 
+       */
+       
+       initEverything();
+       
+       /*
+        *      Open a test background music file and sound. The background music is then played indefinitely
+        *      while the sound is never referenced again.
+        * 
+       */
+       
+       music = Mix_LoadMUS("assets/BennyHillTheme.wav");                       // as in gamedev/assets/<sound>
+       horn  = Mix_LoadWAV("assets/air-horn-club-sample_1.wav");       //
+       
+       Mix_VolumeMusic(15);            // Set the volume
+       Mix_PlayMusic( music, -1 );     // Play music forever
        
-       bgImage=loadTexture("assets/bg.png");   // Load a background image
+       /*
+        *      Load a temporary background image.
+       */
        
-       initInventorySprites(); // Load sprites used for items and the inventory
+       bgImage=loadTexture("assets/bg.png");
+       
+       /*
+        *      Load sprites used in the inventory menu. See src/inventory.cpp
+       */
+       
+       initInventorySprites();
        
        /**************************
        ****     GAMELOOP      ****
@@ -261,55 +402,96 @@ int main(int argc, char *argv[]){
        ****   CLOSE PROGRAM   ****
        **************************/
        
-    // Close the window and free resources
+    /*
+     *  Close the window and free resources
+    */
+    
     fclose(names);
+    
     SDL_GL_DeleteContext(mainGLContext);
     SDL_DestroyWindow(window);
-    return 0;
+    
+    return 0;  // Calls everything passed to atexit
 }
 
+/*
+ *     fps contains the game's current FPS, debugY contains the player's
+ *     y coordinates, updated at a certain interval. These are used in
+ *     the debug menu (see below).
+ * 
+*/
+
 static unsigned int fps=0;
 static float debugY=0;
 
 void mainLoop(void){
-       static unsigned int debugDiv=0;
-       unsigned int i;
-       static unsigned int prevTime    = 0,
-                                               currentTime = 0;
+       static unsigned int debugDiv=0;                 // A divisor used to update the debug menu if it's open
+       
+       static unsigned int prevTime    = 0,    // Used for timing operations
+                                               currentTime = 0,        //
+                                               prevPrevTime= 0;        // shit
+                                               
+       unsigned int i;                                                 // Used for `for` loops
+       
+       if(!currentTime){                                               // Initialize currentTime if it hasn't been
+               currentTime=millis();
+               prevPrevTime=currentTime;
+       }
+       
+       /*
+        *      Update timing values. This is crucial to calling logic and updating the window (basically
+        *      the entire game).
+       */
        
-       if(!currentTime)currentTime=millis();
-       prevTime = currentTime;
+       prevTime        = currentTime;
        currentTime = millis();
-       deltaTime = currentTime - prevTime;
+       deltaTime       = currentTime - prevTime;
 
-       if(prevTime + MSEC_PER_TICK >= millis()){
+       /*
+        *      Run the logic handler if MSEC_PER_TICK milliseconds have passed.
+       */
+       
+       if(prevPrevTime + MSEC_PER_TICK >= currentTime){
                logic();
-               prevTime = millis();
+               prevPrevTime = currentTime;
        }
 
-       player->loc.y+=player->vel.y*deltaTime;
+       /*
+        *      Update player and entity coordinates.
+       */
+       
+       player->loc.y+= player->vel.y                           *deltaTime;
        player->loc.x+=(player->vel.x*player->speed)*deltaTime;
+       
        for(int i=0;i<=entity.size();i++){
+               
                entity[i]->loc.x += entity[i]->vel.x * deltaTime;
                entity[i]->loc.y += entity[i]->vel.y * deltaTime;
-               if(entity[i]->vel.x<0)entity[i]->left=true;
-               if(entity[i]->vel.x>0)entity[i]->left=false;
+               
+                        if(entity[i]->vel.x<0)entity[i]->left=true;
+               else if(entity[i]->vel.x>0)entity[i]->left=false;
        }
        
+       /*
+        *      Update debug variables if necessary
+       */
+       
        if(++debugDiv==20){
-               fps=1000/deltaTime;
                debugDiv=0;
+               
+               fps=1000/deltaTime;
+               
        }else if(!(debugDiv%10)){
                debugY = player->loc.y;
        }
        
-       //mx = (ui::mouse.x+player->loc.x) - (SCREEN_WIDTH/2);
-       //my = SCREEN_HEIGHT - ui::mouse.y;
-       
-       render();
+       render();       // Call the render loop
 }
 
 void render(){
+       /*
+        *      TODO fix this dense garbage ANDY :D lol hahahaha XDSSDSDDDDD
+       */
                                                                                                        //a matrix is a blank canvas for the computer to draw on, the matrices are stored in a "stack"
                                                                                                        //GL_PROJECTION has 2 matrices
                                                                                                        //GL_MODELVIEW has 32 matrices
@@ -328,26 +510,60 @@ void render(){
        **** RENDER STUFF HERE ****
        **************************/
 
-       glEnable(GL_TEXTURE_2D);                                                                                        // Draw the background image
-       glBindTexture(GL_TEXTURE_2D,bgImage);                                                           //
-       glBegin(GL_QUADS);                                                                                                      //
-               glTexCoord2i(0,1);glVertex2i(-SCREEN_WIDTH*2,0);                                //
-               glTexCoord2i(1,1);glVertex2i( SCREEN_WIDTH*2,0);                                //
-               glTexCoord2i(1,0);glVertex2i( SCREEN_WIDTH*2,SCREEN_HEIGHT);    //
-               glTexCoord2i(0,0);glVertex2i(-SCREEN_WIDTH*2,SCREEN_HEIGHT);    //
-       glEnd();                                                                                                                        //
-       glDisable(GL_TEXTURE_2D);                                                                                       //
+       /*
+        *  Draw a temporary background image
+       */
 
-       player->near=true;                      // Ensure the player's name is always drawn
-       currentWorld->draw(player);     // Draw the world & the player
-       player->inv->draw();            //
+       glEnable(GL_TEXTURE_2D);
+       glBindTexture(GL_TEXTURE_2D,bgImage);
+       
+       glBegin(GL_QUADS);
+               glTexCoord2i(0,1);glVertex2i(-SCREEN_WIDTH*2,0);
+               glTexCoord2i(1,1);glVertex2i( SCREEN_WIDTH*2,0);
+               glTexCoord2i(1,0);glVertex2i( SCREEN_WIDTH*2,SCREEN_HEIGHT);
+               glTexCoord2i(0,0);glVertex2i(-SCREEN_WIDTH*2,SCREEN_HEIGHT);
+       glEnd();
+       
+       glDisable(GL_TEXTURE_2D);
+       
+       /*
+        *      Call the world's draw function, drawing the player, the world, and entities. Also
+        *      draw the player's inventory if it exists.
+       */
+
+       player->near=true;                      // Draw the player's name
+       
+       currentWorld->draw(player);
+       
+       player->inv->draw();
+
+       /*
+        *      Draw UI elements. As of 10/20/2015 this includes the player's health bar and the dialog box.
+       */
+       
+       ui::draw();
 
-       ui::draw();                                     // Draw any UI elements if they need to be
+       /*
+        *      Draw the debug overlay if it has been enabled.
+       */
 
-       if(ui::debug){                          // Draw the debug screen if it has been enabled
+       if(ui::debug){
+               
                ui::setFontSize(16);
-               ui::putText(player->loc.x-SCREEN_WIDTH/2,SCREEN_HEIGHT-ui::fontSize,"FPS: %d\nG:%d\nRes: %ux%u\nE: %d\nPOS: (x)%+.2f\n     (y)%+.2f\nQc: %u",
-                                       fps,player->ground,SCREEN_WIDTH,SCREEN_HEIGHT,entity.size(),player->loc.x,debugY,player->qh.current.size());
+               
+               ui::putText(player->loc.x-SCREEN_WIDTH/2,
+                                       SCREEN_HEIGHT-ui::fontSize,
+                                       "FPS: %d\nG:%d\nRes: %ux%u\nE: %d\nPOS: (x)%+.2f\n     (y)%+.2f\nQc: %u",
+                                       fps,
+                                       player->ground,
+                                       SCREEN_WIDTH,
+                                       SCREEN_HEIGHT,
+                                       entity.size(),
+                                       player->loc.x,
+                                       debugY,                 // The player's y coordinate
+                                       player->qh.current.size()
+                                       );
+                                       
        }
        
        glColor3ub(255,255,255);                                                        // Draw the mouse