diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Quest.cpp | 5 | ||||
-rw-r--r-- | src/Texture.cpp | 8 | ||||
-rw-r--r-- | src/common.cpp | 40 | ||||
-rw-r--r-- | src/entities.cpp | 131 | ||||
-rw-r--r-- | src/gameplay.cpp | 34 | ||||
-rw-r--r-- | src/inventory.cpp | 7 | ||||
-rw-r--r-- | src/ui.cpp | 7 | ||||
-rw-r--r-- | src/world.cpp | 81 |
8 files changed, 146 insertions, 167 deletions
diff --git a/src/Quest.cpp b/src/Quest.cpp index e59d79f..8f3c33b 100644 --- a/src/Quest.cpp +++ b/src/Quest.cpp @@ -1,5 +1,6 @@ -#include <Quest.h>
+#include <algorithm>
+#include <Quest.h>
#include <entities.h>
extern Player *player;
@@ -33,8 +34,6 @@ int QuestHandler::assign(std::string title,std::string desc,std::string req){ return 0;
}
-#include <algorithm>
-
int QuestHandler::drop(std::string title){
current.erase( std::remove_if( current.begin(),
current.end(),
diff --git a/src/Texture.cpp b/src/Texture.cpp index 731a1d6..ed93af9 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -7,11 +7,11 @@ * A structure for keeping track of loaded textures. */ -struct texture_t { +typedef struct { std::string name; /**< The file path of the texture. */ GLuint tex; /**< The GLuint for the loaded texture. */ dim2 dim; /**< The dimensions of the texture. */ -}; +} texture_t; struct index_t { Color color; @@ -26,7 +26,7 @@ struct index_t { * this array and reuse GLuint's to save memory. */ -static std::vector<struct texture_t> LoadedTexture; +static std::vector<texture_t> LoadedTexture; namespace Texture{ Color pixels[8][4]; @@ -81,7 +81,7 @@ namespace Texture{ ); // add texture to LoadedTexture - LoadedTexture.push_back((struct texture_t){fileName,object,{image->w,image->h}}); + LoadedTexture.push_back(texture_t{fileName,object,{image->w,image->h}}); // free the SDL_Surface SDL_FreeSurface(image); diff --git a/src/common.cpp b/src/common.cpp index 6dc2c46..1c9c2b5 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -1,15 +1,16 @@ -#include <common.h> #include <cstring> #include <cstdio> #include <chrono> #ifndef __WIN32__ -#include <sys/types.h> -#include <dirent.h> -#include <errno.h> -#include <vector> +# include <sys/types.h> +# include <dirent.h> +# include <errno.h> +# include <vector> #endif // __WIN32__ +#include <common.h> + #ifndef __WIN32__ unsigned int millis(void){ @@ -79,7 +80,7 @@ const char *readFile(const char *path){ std::ifstream in (path,std::ios::in); unsigned int size; GLchar *buf; - + if(!in.is_open()){ std::cout<<"Error reading file "<<path<<"!"<<std::endl; abort(); @@ -101,30 +102,3 @@ UserError( std::string reason ) std::cout << "User error: " << reason << "!" << std::endl; abort(); } - -/*int strCreateFunc(const char *equ){ - static unsigned int size; - static char *filebuf; - static FILE *file; - - size = 57 + strlen(equ) + 3; - - filebuf = new char[size]; - memset(filebuf,0,size); - - strcpy(filebuf,"#include <stdio.h>\n#include <math.h>\nint main(){return "); - strcat(filebuf,equ); - strcat(filebuf,";}"); - - if(!(file = fopen("gen.tmp","w"))){ - abort(); - } - - fwrite(filebuf,size,sizeof(char),file); - delete[] filebuf; - fclose(file); - - system(" - - return 0; -}*/ diff --git a/src/entities.cpp b/src/entities.cpp index cd534cd..329af63 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -1,20 +1,37 @@ -#include <entities.h> -#include <ui.h> - #include <istream> -#define RAND_DIALOG_COUNT 14 +#include <entities.h> +#include <ui.h> extern std::istream *names; -extern unsigned int loops; -extern World *currentWorld; +extern Player *player; // main.cpp +extern World *currentWorld; // main.cpp +extern unsigned int loops; // main.cpp -extern Player *player; +GLuint waterTex; -extern const char *itemName; +std::vector<int (*)(NPC *)> AIpreload; // A dynamic array of AI functions that are being preloaded +std::vector<NPC *> AIpreaddr; // A dynamic array of pointers to the NPC's that are being assigned the preloads -GLuint waterTex; +#define RAND_DIALOG_COUNT 14 + +const char *randomDialog[RAND_DIALOG_COUNT] = { + "What a beautiful day it is.", + "Have you ever went fast? I have.", + "I heard if you complete a quest, you'll get a special reward.", + "How much wood could a woodchuck chuck if a woodchuck could chuck wood?", + "I don\'t think anyone has ever been able to climb up that hill.", + "If you ever see a hole in the ground, watch out; it could mean the end for you.", + "Did you know this game has over 5000 lines of code? I didn\'t. I didn't even know I was in a game until now...", + "HELP MY CAPS LOCK IS STUCK", + "You know, if anyone ever asked me who I wanted to be when I grow up, I would say Abby Ross.", + "I want to have the wallpaper in our house changed. It doesn\'t really fit the environment.", + "Frig.", + "The sine of theta equals the opposite over the hypotenuese.", + "Did you know the developers spelt brazier as brazzier.", + "My dad once said to me, \"Boy, you are in a game.\" I never knew what he meant by that." +}; void initEntity(){ waterTex = Texture::loadTexture("assets/waterTex.png"); @@ -78,6 +95,8 @@ void Entity::spawn(float x, float y){ //spawns the entity you pass to it based o name = new char[32]; getRandomName(this); + + followee = NULL; } Player::Player(){ //sets all of the player specific traits on object creation @@ -362,43 +381,39 @@ NOPE: if(near)ui::putStringCentered(loc.x+width/2,loc.y-ui::fontSize-HLINE/2,name); } -/* - * 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 -*/ +/** + * Handles NPC movement, usually just random walking. + */ -void NPC::wander(int timeRun){ - - /* - * 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 - */ - +void NPC:: +wander( int timeRun ) +{ static int direction; - /* - * 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){ + if ( followee ) { + if ( loc.x < followee->loc.x - 40 ) + direction = 1; + else if ( loc.x > followee->loc.x + 40 ) + direction = -1; + else + direction = 0; + + vel.x = .018 * HLINE * direction; + } else if ( ticksToUse == 0 ) { ticksToUse = timeRun; - vel.x = .008*HLINE; //sets the inital velocity of the entity - 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; - vel.x *= direction; //changes the velocity based off of the direction + + vel.x = .008 * HLINE; + direction = (getRand() % 3 - 1); + + if ( direction == 0 ) + ticksToUse *= 2; + + vel.x *= direction; } - ticksToUse--; //removes one off of the entities timer + + ticksToUse--; } -std::vector<int (*)(NPC *)> AIpreload; // A dynamic array of AI functions that are being preloaded -std::vector<NPC *> AIpreaddr; // A dynamic array of pointers to the NPC's that are being assigned the preloads - void NPC::addAIFunc(int (*func)(NPC *),bool preload){ if(preload){ // Preload AI functions so that they're given after // the current dialog box is closed @@ -412,23 +427,6 @@ void NPC::clearAIFunc(void){ aiFunc.clear(); } -const char *randomDialog[RAND_DIALOG_COUNT] = { - "What a beautiful day it is.", - "Have you ever went fast? I have.", - "I heard if you complete a quest, you'll get a special reward.", - "How much wood could a woodchuck chuck if a woodchuck could chuck wood?", - "I don\'t think anyone has ever been able to climb up that hill.", - "If you ever see a hole in the ground, watch out; it could mean the end for you.", - "Did you know this game has over 5000 lines of code? I didn\'t. I didn't even know I was in a game until now...", - "HELP MY CAPS LOCK IS STUCK", - "You know, if anyone ever asked me who I wanted to be when I grow up, I would say Abby Ross.", - "I want to have the wallpaper in our house changed. It doesn\'t really fit the environment.", - "Frig.", - "The sine of theta equals the opposite over the hypotenuese.", - "Did you know the developers spelt brazier as brazzier.", - "My dad once said to me, \"Boy, you are in a game.\" I never knew what he meant by that." -}; - void NPC::interact(){ //have the npc's interact back to the player int (*func)(NPC *); loc.y += 5; @@ -476,6 +474,12 @@ void Object::interact(void){ } } +void Entity:: +follow( Entity *e ) +{ + followee = e; +} + /* * This spawns the structures * @@ -538,12 +542,25 @@ void Mob::wander(int timeRun){ static unsigned int heya=0,hi=0; static bool YAYA = false; + if ( followee ) { + if ( loc.x < followee->loc.x - 40 ) + direction = 1; + else if ( loc.x > followee->loc.x + 40 ) + direction = -1; + else + direction = 0; + + vel.x = .018 * HLINE * direction; + return; + } + if(aggressive && !YAYA && player->loc.x + (width / 2) > loc.x && player->loc.x + (width / 2) < loc.x + width && player->loc.y + (height / 3) > loc.y && player->loc.y + (height / 3) < loc.y + height ){ Arena *a = new Arena(currentWorld,player,this); a->setBackground( WorldBGType::Forest ); a->setBGM("assets/music/embark.wav"); + ui::toggleWhiteFast(); YAYA = true; ui::waitForCover(); @@ -654,7 +671,7 @@ void Player::sspawn(float x,float y){ std::getline(data,ddata); count = std::stoi(ddata); std::getline(data,ddata); - inv->items.push_back((item_t){count,(uint)std::stoi(ddata)}); + inv->items.push_back(item_t{count,(uint)std::stoi(ddata)}); } std::getline(data,ddata); diff --git a/src/gameplay.cpp b/src/gameplay.cpp index a286db9..0624067 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -4,30 +4,28 @@ #include <ui.h> #include <tinyxml2.h> - using namespace tinyxml2; -extern World *currentWorld; -extern Player *player; +extern Player *player; // main.cpp +extern World *currentWorld; // main.cpp -extern float shit; -extern Menu* currentMenu; -extern Menu pauseMenu; -extern Menu optionsMenu; +extern float shit; +extern Menu *currentMenu; +extern Menu pauseMenu; +extern Menu optionsMenu; -extern void mainLoop(void); +extern void mainLoop(void); // main.cpp -void segFault(){ - (*((int *)NULL))++; -} +extern std::vector<NPC *> AIpreaddr; // entities.cpp +extern std::vector<int (*)(NPC *)> AIpreload; // entities.cpp +std::vector<XMLElement *> dopt; -typedef struct { - NPC *npc; - unsigned int index; -} NPCDialog; +void destroyEverything(void); -std::vector<XMLElement *> dopt; +void segFault(){ + (*((int *)NULL))++; +} int commonAIFunc(NPC *speaker){ XMLDocument xml; @@ -252,7 +250,6 @@ void commonTriggerFunc(Mob *callee){ } } -void destroyEverything(void); void initEverything(void){ std::vector<std::string> xmlFiles; XMLDocument xml; @@ -314,9 +311,6 @@ void initEverything(void){ atexit(destroyEverything); } -extern std::vector<int (*)(NPC *)> AIpreload; -extern std::vector<NPC *> AIpreaddr; - void destroyEverything(void){ currentWorld->save(); delete currentWorld; diff --git a/src/inventory.cpp b/src/inventory.cpp index d75a1bf..e82d99e 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -14,6 +14,8 @@ static vec2 itemLoc; Mix_Chunk* swordSwing; static std::vector<Item *> itemMap; +static GLuint *itemtex; +void itemDraw(Player *p,uint id); void items(void){ XMLDocument xml; @@ -44,7 +46,7 @@ int Inventory::addItem(std::string name,uint count){ return 0; } } - items.push_back((item_t){count,i}); + items.push_back( item_t { count, i }); return 0; } } @@ -108,9 +110,6 @@ int Inventory::hasItem(std::string name){ return 0; } -static GLuint *itemtex; -void itemDraw(Player *p,uint id); - void initInventorySprites(void){ items(); @@ -191,7 +191,7 @@ namespace ui { void setFontSize(unsigned int size){ unsigned int i,j; - char *buf; + unsigned char *buf; fontSize=size; FT_Set_Pixel_Sizes(ftf,0,fontSize); @@ -231,7 +231,7 @@ namespace ui { * making it white-on-black. */ - buf = new char[ftf->glyph->bitmap.width * ftf->glyph->bitmap.rows * 4]; + buf = new unsigned char[ftf->glyph->bitmap.width * ftf->glyph->bitmap.rows * 4]; for(j=0;j<ftf->glyph->bitmap.width*ftf->glyph->bitmap.rows;j++){ buf[j*4 ]=255;//fontColor[0]; @@ -1131,7 +1131,8 @@ namespace ui { } void takeScreenshot(GLubyte* pixels){ - GLubyte bgr[SCREEN_WIDTH*SCREEN_HEIGHT*3]; + std::vector<GLubyte> bgr (SCREEN_WIDTH * SCREEN_HEIGHT * 3, 0); + for(uint x = 0; x < SCREEN_WIDTH*SCREEN_HEIGHT*3; x+=3){ bgr[x] = pixels[x+2]; bgr[x+1] = pixels[x+1]; diff --git a/src/world.cpp b/src/world.cpp index 8283e8c..a0ae0f3 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -1,8 +1,12 @@ #include <algorithm> +#include <sstream> #include <world.h> #include <ui.h> +#include <tinyxml2.h> +using namespace tinyxml2; + /** * Defines how many HLINEs tall a blade of grass can be. */ @@ -16,8 +20,22 @@ #define INDOOR_FLOOR_HEIGHT 100 -extern Player *player; +extern Player *player; // main.cpp? +extern World *currentWorld; // main.cpp +extern int commonAIFunc(NPC *); // entities.cpp +extern void commonTriggerFunc(Mob *); // entities.cpp +extern bool inBattle; + +extern unsigned int tickCount; // main.cpp + +int worldShade = 0; + +std::string currentXML; + +std::vector<std::string> inside; // tracks indoor worlds +std::vector<World *> battleNest; // tracks arenas +std::vector<vec2> battleNestLoc; // keeps arena locations /** * Contains the current weather, used in many other places/files. @@ -230,7 +248,7 @@ generate( unsigned int width ) UserError("Invalid world dimensions"); // allocate space for world - worldData = std::vector<WorldData> (width + GROUND_HILLINESS, (WorldData) { false, {0,0}, 0, 0 }); + worldData = std::vector<WorldData> (width + GROUND_HILLINESS, WorldData { false, {0,0}, 0, 0 }); lineCount = worldData.size(); // prepare for generation @@ -270,7 +288,11 @@ generate( unsigned int width ) worldStart = (width - GROUND_HILLINESS) * HLINE / 2 * -1; // create empty star array, should be filled here as well... - star = std::vector<vec2> (100, (vec2) { 0, 0 } ); + star = std::vector<vec2> (100, vec2 { 0, 400 } ); + for ( auto &s : star ) { + s.x = (getRand() % (-worldStart * 2)) + worldStart; + s.y = (getRand() % SCREEN_HEIGHT) + 100.0f; + } } /** @@ -364,20 +386,6 @@ bgmPlay( World *prev ) const } /** - * Variables used by World::draw(). - * @{ - */ - -extern vec2 offset; -extern unsigned int tickCount; - -int worldShade = 0; - -/** - * @} - */ - -/** * The world draw function. * * This function will draw the background layers, entities, and player to the @@ -436,7 +444,7 @@ draw( Player *p ) if (tickCount % DAY_CYCLE) { // The above if statement doesn't check for exact midnight. - safeSetColorA( 255, 255, 255, shadeBackground + getRand() % 30 - 15 ); + safeSetColorA( 255, 255, 255, 255 - (getRand() % 30 - 15) ); for ( i = 0; i < 100; i++ ) { glRectf(star[i].x + offset.x * .9, @@ -446,7 +454,7 @@ draw( Player *p ) ); } } - } + } // draw remaining background items @@ -464,7 +472,7 @@ draw( Player *p ) } glEnd(); - for (i = 0; i < 4; i++) { + for ( i = 0; i < 4; i++ ) { bgTex->bindNext(); safeSetColorA( bgDraw[i][0] - shadeBackground, bgDraw[i][0] - shadeBackground, bgDraw[i][0] - shadeBackground, bgDraw[i][1] ); @@ -515,7 +523,11 @@ draw( Player *p ) glActiveTexture( GL_TEXTURE0 ); bgTex->bindNext(); - GLfloat pointArray[ light.size() + (int)p->light ][2]; + // help me + std::unique_ptr<GLfloat[][2]> pointArrayBuf = std::make_unique<GLfloat[][2]> (light.size() + p->light); + auto pointArray = pointArrayBuf.get(); + + //GLfloat pointArray[ light.size() + (int)p->light ][2]; for ( i = 0; i < (int)light.size(); i++ ) { pointArray[i][0] = light[i].loc.x - offset.x; @@ -527,7 +539,7 @@ draw( Player *p ) glUseProgram( shaderProgram ); glUniform1i( glGetUniformLocation( shaderProgram, "sampler"), 0 ); - glUniform1f( glGetUniformLocation( shaderProgram, "amb" ), 1 ); + glUniform1f( glGetUniformLocation( shaderProgram, "amb" ), 0.5f - worldShade / 50.0f ); if ( p->light ) { pointArray[light.size() + 1][0] = (float)( p->loc.x + SCREEN_WIDTH / 2 ); @@ -984,8 +996,6 @@ World *World::goWorldRight(Player *p){ return this; } -std::vector<std::string> inside; - World *World:: goInsideStructure( Player *p ) { @@ -1072,11 +1082,6 @@ void World::save(void){ out.close(); } -#include <sstream> - -extern int commonAIFunc(NPC *); -extern void commonTriggerFunc(Mob *); - void World::load(void){ std::string save,data,line; const char *filedata; @@ -1143,7 +1148,7 @@ void IndoorWorld::generate(unsigned int width){ // Generates a flat area of wid lineCount=width+GROUND_HILLINESS; // Sets line count to the desired width plus GEN_INC to remove incorrect line calculations. if(lineCount<=0)abort(); - worldData = std::vector<WorldData> (lineCount, (WorldData) { false, {0,0}, INDOOR_FLOOR_HEIGHT, 0 }); + worldData = std::vector<WorldData> (lineCount, WorldData { false, {0,0}, INDOOR_FLOOR_HEIGHT, 0 }); worldStart = (width - GROUND_HILLINESS) * HLINE / 2 * -1; } @@ -1159,7 +1164,9 @@ void IndoorWorld::draw(Player *p){ glEnable(GL_TEXTURE_2D); - GLfloat pointArray[light.size()][2]; + std::unique_ptr<GLfloat[][2]> pointArrayBuf = std::make_unique<GLfloat[][2]> (light.size()); + auto pointArray = pointArrayBuf.get(); + for(uint w = 0; w < light.size(); w++){ pointArray[w][0] = light[w].loc.x - offset.x; pointArray[w][1] = light[w].loc.y; @@ -1240,11 +1247,6 @@ void IndoorWorld::draw(Player *p){ p->draw(); } -extern bool inBattle; - -std::vector<World *> battleNest; -std::vector<vec2> battleNestLoc; - Arena::Arena(World *leave,Player *p,Mob *m){ generate(800); addMob(MS_DOOR,100,100); @@ -1287,13 +1289,6 @@ World *Arena::exitArena(Player *p){ } } -#include <tinyxml2.h> -using namespace tinyxml2; - -std::string currentXML; - -extern World *currentWorld; - World *loadWorldFromXML(std::string path){ if ( !currentXML.empty() ) currentWorld->save(); |