]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
world rewrite
authorClyne Sullivan <tullivan99@gmail.com>
Tue, 1 Mar 2016 13:33:27 +0000 (08:33 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Tue, 1 Mar 2016 13:33:27 +0000 (08:33 -0500)
Changelog
include/world.h
main.cpp
src/inventory.cpp
src/ui.cpp
src/world.cpp

index b85c19577d8fa119b06f2d6178c7752832f86a8e..9a9b9277487d758b9430509d528773b4e12c5331 100644 (file)
--- a/Changelog
+++ b/Changelog
        - looked into better automated Makefiles
        - improving villages, began work on shops
 
-2/26/2015:
+2/26/2016:
 ==========
 
        - made fonts more memory-efficient
        - C++-ified loaded texture handlers
        - documented more stuff
        - made merchant menu
+
+2/29/2016:
+==========
+
+       - renewed branch 'remake': re-wrote world.cpp and pushed
+       - fixed world linkage errors
+       - considered more formal coding? (as in documentation and indentation)
+       - fixed dialog boxes and options
+       - broke screenshots
index 05b2a10b21b6860a325077b0106221feffc86780..9833bb2993282a5e09f9c97061579ba00d6ff832 100644 (file)
@@ -281,7 +281,7 @@ public:
         * A vector of all light elements in this world.
         */
        
-       std::vector<Light        >  light;
+       std::vector<Light>  light;
 
        /**
         * Vector of all building textures for the current world style
index 885c6103baf5723f7bff8709c5f23de4f75b4d46..fd02cc1d7c232e84fd91f67bfb97f4c31258b63f 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -185,9 +185,11 @@ void mainLoop(void);
 /*******************************************************************************
  * MAIN ************************************************************************
  *******************************************************************************/
-int main(/*int argc, char *argv[]*/){
-       // *argv = (char *)argc;
-       SDL_GLContext mainGLContext = NULL;
+int main(int argc, char *argv[]){
+       (void)argc;
+       (void)argv;
+       
+       static SDL_GLContext mainGLContext = NULL;
        
        gameRunning=false;
 
@@ -290,16 +292,7 @@ int main(/*int argc, char *argv[]*/){
        if((err=glewInit()) != GLEW_OK){
                std::cout << "GLEW was not able to initialize! Error: " << glewGetErrorString(err) << 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/FreePixel.ttf");         // as in gamedev/ttf/<font>
+       }
        
        /*
         * Initialize the random number generator. At the moment, initRand is a macro pointing to libc's
index b32b56fd76762010ffdd31567a1ff14ccbaa4607..203e7076b5d6a37c702e65645175ccfe3a8fb882 100644 (file)
@@ -407,6 +407,8 @@ int Inventory::useItem(void){
 }
 
 bool Inventory::detectCollision(vec2 one, vec2 two){
+       (void)one;
+       (void)two;
        //float i = 0.0f;
        
        /*if(items.empty() || !items[sel].count)
@@ -433,6 +435,6 @@ bool Inventory::detectCollision(vec2 one, vec2 two){
                        i+=HLINE;
                }
        }*/
-       return !(one.x == two.y);
+       return false;
 }
 
index 6e616a7576908df733b0307d68b55843c1c4d6e4..47110cc54a70d28e9c490057c7b07b1d48cfcb5d 100644 (file)
@@ -1039,42 +1039,48 @@ DONE:
                vec2 oldpos,tmppos;
                SDL_Event e;
                
-               mouse.x=premouse.x+offset.x-(SCREEN_WIDTH/2);
-               mouse.y=(offset.y+SCREEN_HEIGHT/2)-premouse.y;
+               // update mouse coords
+               mouse.x = premouse.x + offset.x - ( SCREEN_WIDTH / 2 );
+               mouse.y = ( offset.y + SCREEN_HEIGHT / 2 ) - premouse.y;
                
                while(SDL_PollEvent(&e)){
                        switch(e.type){
+                               
+                       // escape - quit game
                        case SDL_QUIT:
                                gameRunning=false;
                                break;
+                               
+                       // mouse movement - update mouse vector
                        case SDL_MOUSEMOTION:
                                premouse.x=e.motion.x;
                                premouse.y=e.motion.y;
                                break;
+                               
+                       // mouse clicks
                        case SDL_MOUSEBUTTONDOWN:
-                               if((e.button.button & SDL_BUTTON_RIGHT) && dialogBoxExists)
+                               // right click advances dialog
+                               if ( ( e.button.button & SDL_BUTTON_RIGHT ) && dialogBoxExists )
                                        dialogAdvance();
-                               if((e.button.button & SDL_BUTTON_LEFT) && !dialogBoxExists)
+                               // left click uses item
+                               if ( ( e.button.button & SDL_BUTTON_LEFT ) && !dialogBoxExists )
                                        player->inv->usingi = true;
                                break;
-                       /*
-                               KEYDOWN
-                       */
+                       
+                       // key presses
                        case SDL_KEYDOWN:
-                               /*if(SDL_KEY == SDLK_ESCAPE){
-                                       //gameRunning = false;
-                                       pMenu = true;
-                                       return;
-                               }else */if(SDL_KEY == SDLK_SPACE){
-                                       /*if(dialogBoxExists)
-                                               dialogAdvance();
-                                       else */if(player->ground){
-                                               player->vel.y=.4;
-                                               player->loc.y+=HLINE*2;
-                                               player->ground=false;
+                       
+                               // space - make player jump
+                               if ( SDL_KEY == SDLK_SPACE ) {
+                                       if ( player->ground ) {
+                                               player->loc.y += HLINE * 2;
+                                               player->vel.y = .4;
+                                               player->ground = false;
                                        }
                                        break;
-                               }else if(!dialogBoxExists || dialogPassive){
+
+                               // only let other keys be handled if dialog allows it
+                               } else if ( !dialogBoxExists || dialogPassive ) {
                                        tmp = currentWorld;
                                        switch(SDL_KEY){
                                        case SDLK_a:
index 76dccb8e335f1b89bb278908effa75c63468f3e5..4fa71d8585b6328daf88546d6fa4464ec2083069 100644 (file)
@@ -252,6 +252,7 @@ generate( unsigned int width )
     
     for(wditer = worldData.begin(); wditer != worldData.end(); wditer++){
         if ((*wditer).groundHeight)
+                       // wditer + GROUND_HILLINESS can go out of bounds (invalid read)
             geninc = ( (*(wditer + GROUND_HILLINESS)).groundHeight - (*wditer).groundHeight ) / (float)GROUND_HILLINESS;
         else
             (*wditer).groundHeight = (*(wditer - 1)).groundHeight + geninc;
@@ -260,7 +261,7 @@ generate( unsigned int width )
         (*wditer).grassUnpressed = true;
         (*wditer).grassHeight[0] = (randGet() % 16) / 3 + 2;
         (*wditer).grassHeight[1] = (randGet() % 16) / 3 + 2;
-        
+
         // bound checks
         if ( (*wditer).groundHeight < GROUND_HEIGHT_MINIMUM )
             (*wditer).groundHeight = GROUND_HEIGHT_MINIMUM;
@@ -269,6 +270,7 @@ generate( unsigned int width )
                
                if( (*wditer).groundHeight <= 0 )
                        (*wditer).groundHeight = GROUND_HEIGHT_MINIMUM;
+
     }
     
     // define x-coordinate of world's leftmost 'line'
@@ -674,11 +676,17 @@ draw( Player *p )
        p->draw();
 }
 
-/*
- * TODO
+/**
+ * Handles physics and such for a single entity.
+ * 
+ * This function is kept private, as World::detect() should be used instead to
+ * handle stuffs for all entities at once.
  */
 
-void World::singleDetect(Entity *e){
+void World::
+singleDetect( Entity *e )
+{
+       std::string killed;
        unsigned int i,j;
        int l;
        
@@ -686,12 +694,12 @@ void World::singleDetect(Entity *e){
         *      Kill any dead entities.
        */
        
-       if(!e->alive||e->health<=0){
-               for(i=0;i<entity.size();i++){
-                       if(entity[i]==e){
-                               switch(e->type){
+       if ( !e->alive || e->health <= 0 ) {
+               for ( i = 0; i < entity.size(); i++) {
+                       if ( entity[i] == e ){
+                               switch ( e->type ) {
                                case STRUCTURET:
-                                       std::cout<<"Killed a building..."<<std::endl;
+                                       killed = "structure";
                                        for(j=0;j<build.size();j++){
                                                if(build[j]==e){
                                                        delete build[j];
@@ -701,7 +709,7 @@ void World::singleDetect(Entity *e){
                                        }
                                        break;
                                case NPCT:
-                                       std::cout<<"Killed an NPC..."<<std::endl;
+                                       killed = "NPC";
                                        for(j=0;j<npc.size();j++){
                                                if(npc[j]==e){
                                                        delete npc[j];
@@ -711,7 +719,7 @@ void World::singleDetect(Entity *e){
                                        }
                                        break;
                                case MOBT:
-                                       std::cout<<"Killed a mob..."<<std::endl;
+                                       killed = "mob";
                                        /*for(j=0;j<mob.size();j++){
                                                if(mob[j]==e){
                                                        delete mob[j];
@@ -721,7 +729,7 @@ void World::singleDetect(Entity *e){
                                        }*/
                                        break;
                                case OBJECTT:
-                                       std::cout<<"Killed an object..."<<std::endl;
+                                       killed = "object";
                                        for(j=0;j<object.size();j++){
                                                if(object[j]==e){
                                                        delete object[j];
@@ -733,6 +741,7 @@ void World::singleDetect(Entity *e){
                                default:
                                        break;
                                }
+                               std::cout << "Killed a " << killed << "..." << std::endl;
                                entity.erase(entity.begin()+i);
                                return;
                        }
@@ -884,8 +893,6 @@ void World::addStructure(BUILD_SUB sub, float x,float y, char *tex, const char *
        else
                strcpy((build.back()->inside = new char[1]),"\0");
                
-       //strcpy((build.back()->outside = new char[1 + strlen((char *)(currentXML+4))]),(char *)(currentXML+4));
-       
        entity.push_back(build.back());
 }
        
@@ -1010,6 +1017,7 @@ World *World::goWorldRight(Player *p){
 }
 
 std::vector<std::string> inside;
+
 World *World::
 goInsideStructure( Player *p )
 {
@@ -1035,11 +1043,13 @@ goInsideStructure( Player *p )
                tmp = loadWorldFromXML(inside.back().c_str());
                for(auto &b : tmp->build){
                        if(!strcmp(current,b->inside)){
-                               p->loc.x = b->loc.x + (b->width / 2);
                                inside.pop_back();
 
                                ui::toggleBlackFast();
                                ui::waitForCover();
+                               
+                               p->loc.x = b->loc.x + (b->width / 2);
+                               
                                ui::toggleBlackFast();
                                
                                return tmp;