From cd0236b94d5ecd2378518876820be201b9c635be Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 26 Feb 2016 08:47:49 -0500 Subject: c++'d texture loading --- src/Texture.cpp | 79 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 34 deletions(-) (limited to 'src/Texture.cpp') diff --git a/src/Texture.cpp b/src/Texture.cpp index 1487dcd..1aaadf6 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -1,45 +1,62 @@ #include #include +/** + * A structure for keeping track of loaded textures. + */ + struct texture_t { - char *name; - GLuint tex; - dim2 dim; -} __attribute__ ((packed)); + std::string name; /**< The file path of the texture. */ + GLuint tex; /**< The GLuint for the loaded texture. */ + dim2 dim; /**< The dimensions of the texture. */ +}; -struct index_t{ +struct index_t { Color color; int indexx; int indexy; }; -struct texture_t *LoadedTexture[256]; -unsigned int LoadedTextureCounter = 0; +/** + * A vector of all loaded textures. + * + * Should a texture be asked to be loaded twice, loadTexture() can reference + * this array and reuse GLuint's to save memory. + */ + +static std::vector LoadedTexture; namespace Texture{ Color pixels[8][4]; - GLuint loadTexture(const char *fileName){ + + GLuint loadTexture(std::string fileName){ SDL_Surface *image; GLuint object = 0; - for(unsigned int i=0;iname,fileName)){ + // check if texture is already loaded + for(auto &t : LoadedTexture){ + if(t.name == fileName){ + #ifdef DEBUG - DEBUG_printf("Reusing loaded texture for %s\n",fileName); + DEBUG_printf("Reusing loaded texture for %s\n", fileName.c_str()); #endif // DEBUG - return LoadedTexture[i]->tex; + + return t.tex; } } - if(!fileName) - return 0; - - if(!(image = IMG_Load(fileName))) + // load SDL_surface of texture + if(!(image = IMG_Load(fileName.c_str()))) return 0; + #ifdef DEBUG - DEBUG_printf("Loaded image file: %s\n", fileName); + DEBUG_printf("Loaded image file: %s\n", fileName.c_str()); #endif // DEBUG + /* + * Load texture through OpenGL. + */ + glGenTextures(1,&object); // Turns "object" into a texture glBindTexture(GL_TEXTURE_2D,object); // Binds "object" to the top of the stack glPixelStoref(GL_UNPACK_ALIGNMENT,1); @@ -61,33 +78,27 @@ namespace Texture{ image->pixels ); - LoadedTexture[LoadedTextureCounter] = new struct texture_t; //(struct texture_t *)malloc(sizeof(struct texture_t)); - LoadedTexture[LoadedTextureCounter]->name = new char[strlen(fileName)+1]; //(char *)malloc(safe_strlen(fileName)); - LoadedTexture[LoadedTextureCounter]->tex = object; - strcpy(LoadedTexture[LoadedTextureCounter]->name,fileName); - LoadedTexture[LoadedTextureCounter]->dim.x = image->w; - LoadedTexture[LoadedTextureCounter]->dim.y = image->h; - LoadedTextureCounter++; + // add texture to LoadedTexture + LoadedTexture.push_back((struct texture_t){fileName,object,{image->w,image->h}}); - SDL_FreeSurface(image); // Free the surface + // free the SDL_Surface + SDL_FreeSurface(image); return object; } - dim2 imageDim(const char *fileName){ - for(unsigned int i=0;iname,fileName)){ - return LoadedTexture[i]->dim; - } + dim2 imageDim(std::string fileName){ + for(auto &t : LoadedTexture){ + if(t.name == fileName) + return t.dim; } return {0,0}; } void freeTextures(void){ - for(unsigned int i=0;itex); - delete[] LoadedTexture[i]->name; - delete LoadedTexture[i]; + while(!LoadedTexture.empty()){ + glDeleteTextures(1, &LoadedTexture.back().tex); + LoadedTexture.pop_back(); } } -- cgit v1.2.3 From 90f3d2ff9fe7297dae0bdcee9c441e53177a0fb7 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 3 Mar 2016 09:26:06 -0500 Subject: efficiency stuffs --- Changelog | 10 ++++ include/Texture.h | 3 +- include/entities.h | 4 +- include/tinyxml2.h | 7 +++ include/world.h | 11 ++-- main.cpp | 1 - src/Texture.cpp | 28 +++++----- src/entities.cpp | 15 +++--- src/tinyxml2.cpp | 13 ++++- src/world.cpp | 154 ++++++++++++++++++++++------------------------------- 10 files changed, 125 insertions(+), 121 deletions(-) (limited to 'src/Texture.cpp') diff --git a/Changelog b/Changelog index 0384181..f6309bf 100644 --- a/Changelog +++ b/Changelog @@ -712,3 +712,13 @@ - merged 'remake' with 'master', fixed font issues and world stuffs - continued work on merchant dialog + +3/2/2016, +3/3/2016: +========= + + - made particle vector not pointers + - improved efficiency of code using newly learned C++11 tricks + - added StrAttribute() to TinyXML2 + - more merchant stuff, trading???? + - improved village XML loading diff --git a/include/Texture.h b/include/Texture.h index 659c32d..7201a4c 100644 --- a/include/Texture.h +++ b/include/Texture.h @@ -58,7 +58,7 @@ public: * Contains an array of the GLuints returned from Texture::loadTexture(). */ - GLuint *image = NULL; + std::vector image; /** * Populates the image array from a list of strings, with each string as a @@ -73,6 +73,7 @@ public: Texturec(uint amt,const char **paths); Texturec(std::vectorvec); + Texturec( std::initializer_list l ); /** * Frees memory taken by the image array. diff --git a/include/entities.h b/include/entities.h index 545f0a4..3b8a19b 100644 --- a/include/entities.h +++ b/include/entities.h @@ -228,8 +228,8 @@ class Structures : public Entity{ public: BUILD_SUB bsubtype; World *inWorld; - char *inside; - char *textureLoc; + std::string inside; + std::string textureLoc; Structures(); ~Structures(); diff --git a/include/tinyxml2.h b/include/tinyxml2.h index fb7464a..4282642 100755 --- a/include/tinyxml2.h +++ b/include/tinyxml2.h @@ -38,6 +38,8 @@ distribution. # include #endif +#include + /* TODO: intern strings instead of allocation. */ @@ -1187,6 +1189,11 @@ public: */ const char* Attribute( const char* name, const char* value=0 ) const; + /** Functions the same as Attribute(), but returns the result + as a std::string. + */ + std::string StrAttribute( const char* name, const char* value=0 ) const; + /** Given an attribute name, IntAttribute() returns the value of the attribute interpreted as an integer. 0 will be returned if there is an error. For a method with error diff --git a/include/world.h b/include/world.h index b3d1071..7b9383a 100644 --- a/include/world.h +++ b/include/world.h @@ -279,7 +279,7 @@ public: * A vector of all particles in this world. */ - std::vector particles; + std::vector particles; std::vector village; @@ -294,7 +294,7 @@ public: * Vector of all building textures for the current world style */ - std::vector sTexLoc; + std::vector sTexLoc; /** * NULLifies pointers and allocates necessary memory. This should be @@ -317,8 +317,7 @@ public: * the structure. */ - void addStructure(BUILD_SUB subtype,float x,float y, char* tex, const char *inside); - //void addVillage(int buildingCount, int npcMin, int npcMax,const char *inside); + void addStructure(BUILD_SUB subtype,float x,float y, std::string tex, std::string inside); /** * Adds a Mob to the world with the specified type and coordinates. @@ -346,7 +345,7 @@ public: * upon object interaction. */ - void addObject(/*ITEM_ID id*/std::string in,const char *pickupDialog, float x, float y); + void addObject(std::string in, const char *pickupDialog, float x, float y); /** * Adds a particle to the world with the specified coordinates, dimensions, @@ -479,8 +478,6 @@ public: class Arena : public World { private: - //vec2 pxy; - //World *exit; Mob *mmob; public: Arena(World *leave,Player *p,Mob *m); diff --git a/main.cpp b/main.cpp index abc4f3c..3769379 100644 --- a/main.cpp +++ b/main.cpp @@ -117,7 +117,6 @@ std::mutex mtx; std::condition_variable cv; ThreadPool pool(10); - /* * loops is used for texture animation. It is believed to be passed to entity * draw functions, although it may be externally referenced instead. diff --git a/src/Texture.cpp b/src/Texture.cpp index 1aaadf6..731a1d6 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -1,5 +1,7 @@ +#include +#include + #include -#include /** * A structure for keeping track of loaded textures. @@ -173,32 +175,30 @@ namespace Texture{ Texturec::Texturec(uint amt, ...){ va_list fNames; texState = 0; - image = new GLuint[amt]; va_start(fNames, amt); - for(unsigned int i = 0; i < amt; i++){ - image[i] = Texture::loadTexture(va_arg(fNames, char *)); - } + for(unsigned int i = 0; i < amt; i++) + image.push_back( Texture::loadTexture(va_arg(fNames, char *)) ); va_end(fNames); } +Texturec::Texturec( std::initializer_list l ) +{ + texState = 0; + std::for_each( l.begin(), l.end(), [&](std::string s){ image.push_back( Texture::loadTexture( s ) ); }); +} + Texturec::Texturec(std::vectorv){ texState = 0; - image = new GLuint[v.size()]; - for(unsigned int i = 0; i < v.size(); i++){ - image[i] = Texture::loadTexture(v[i].c_str()); - } + std::for_each( v.begin(), v.end(), [&](std::string s){ image.push_back( Texture::loadTexture( s ) ); }); } Texturec::Texturec(uint amt,const char **paths){ texState = 0; - image = new GLuint[amt]; - for(unsigned int i = 0; i < amt; i++){ - image[i] = Texture::loadTexture(paths[i]); - } + for(unsigned int i = 0; i < amt; i++) + image.push_back( Texture::loadTexture(paths[i]) ); } Texturec::~Texturec(){ - delete[] image; } void Texturec::bind(unsigned int bn){ diff --git a/src/entities.cpp b/src/entities.cpp index 1c364a4..4291b0a 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -179,10 +179,9 @@ Structures::Structures(){ //sets the structure type } Structures::~Structures(){ delete tex; + if(name) delete[] name; - if(inside) - delete[] inside; } Mob::Mob(int sub){ @@ -505,16 +504,20 @@ unsigned int Structures::spawn(BUILD_SUB sub, float x, float y){ */ //unsigned int tempN = (getRand() % 5 + 2); + + if ( textureLoc.empty() ) + textureLoc = inWorld->sTexLoc[sub]; + switch(sub){ case STALL_MARKET: - tex = new Texturec(1, textureLoc ? textureLoc : inWorld->sTexLoc[sub].c_str()); - dim = Texture::imageDim(textureLoc ? textureLoc : inWorld->sTexLoc[sub].c_str()); + tex = new Texturec({ textureLoc }); + dim = Texture::imageDim( textureLoc ); width = dim.x; height = dim.y; break; default: - tex = new Texturec(1, textureLoc ? textureLoc : inWorld->sTexLoc[sub].c_str()); - dim = Texture::imageDim(textureLoc ? textureLoc : inWorld->sTexLoc[sub].c_str()); + tex = new Texturec({ textureLoc }); + dim = Texture::imageDim( textureLoc ); width = dim.x; height = dim.y; inv = NULL; diff --git a/src/tinyxml2.cpp b/src/tinyxml2.cpp index c4ea7cd..6198418 100755 --- a/src/tinyxml2.cpp +++ b/src/tinyxml2.cpp @@ -1400,6 +1400,18 @@ const XMLAttribute* XMLElement::FindAttribute( const char* name ) const return 0; } +std::string XMLElement::StrAttribute( const char* name, const char* value ) const +{ + std::string str; + const XMLAttribute* a = FindAttribute( name ); + if ( a ) { + if ( !value || XMLUtil::StringEqual( a->Value(), value )) { + str = a->Value(); + return str; + } + } + return str; +} const char* XMLElement::Attribute( const char* name, const char* value ) const { @@ -1413,7 +1425,6 @@ const char* XMLElement::Attribute( const char* name, const char* value ) const return 0; } - const char* XMLElement::GetText() const { if ( FirstChild() && FirstChild()->ToText() ) { diff --git a/src/world.cpp b/src/world.cpp index 80ddc5a..a9e0fd9 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -1,3 +1,5 @@ +#include + #include #include @@ -149,9 +151,7 @@ deleteEntities( void ) mob.pop_back(); } - while(!merchant.empty()){ - merchant.pop_back(); - } + merchant.clear(); while(!npc.empty()){ delete npc.back(); npc.pop_back(); @@ -170,19 +170,13 @@ deleteEntities( void ) } // clear entity array - while ( !entity.empty() ) { - entity.pop_back(); - } + entity.clear(); // free particles - while ( !particles.empty() ) { - delete particles.back(); - particles.pop_back(); - } + particles.clear(); // clear light array - while ( !light.empty() ) - light.pop_back(); + light.clear(); // free villages while ( !village.empty() ) { @@ -311,21 +305,17 @@ update( Player *p, unsigned int delta ) } // iterate through particles - for(unsigned int i=0;ikill(deltaTime)){ - delete particles[i]; - particles.erase(particles.begin()+i); - }else if(particles[i]->canMove){ - particles[i]->loc.y += particles[i]->vely * deltaTime; - particles[i]->loc.x += particles[i]->velx * deltaTime; - - for(auto &b : build){ - if(b->bsubtype==FOUNTAIN){ - if(particles[i]->loc.x >= b->loc.x && particles[i]->loc.x <= b->loc.x + b->width){ - if(particles[i]->loc.y <= b->loc.y + b->height * .25){ - delete particles[i]; - particles.erase(particles.begin()+i); - } + particles.erase( std::remove_if( particles.begin(), particles.end(), [&delta](Particles part){ return part.kill( delta ); }), particles.end()); + for ( auto part = particles.begin(); part != particles.end(); part++ ) { + if ( (*part).canMove ) { + (*part).loc.y += (*part).vely * delta; + (*part).loc.x += (*part).velx * delta; + + for ( auto &b : build ) { + if ( b->bsubtype == FOUNTAIN ) { + if ( (*part).loc.x >= b->loc.x && (*part).loc.x <= b->loc.x + b->width ) { + if ( (*part).loc.y <= b->loc.y + b->height * .25) + particles.erase( part ); } } } @@ -475,7 +465,7 @@ draw( Player *p ) } glEnd(); - for ( i = 0; i < 4; i++ ) { + for ( i = 4; i--; ) { bgTex->bindNext(); safeSetColorA( bgDraw[i][0] - shadeBackground, bgDraw[i][0] - shadeBackground, bgDraw[i][0] - shadeBackground, bgDraw[i][1] ); @@ -513,11 +503,8 @@ draw( Player *p ) iEnd = GROUND_HILLINESS; // draw particles and buildings - - for ( auto &part : particles ) { - if ( part->behind ) - part->draw(); - } + + std::for_each( particles.begin(), particles.end(), [](Particles part) { if ( part.behind ) part.draw(); }); for ( auto &b : build ) b->draw(); @@ -640,10 +627,7 @@ draw( Player *p ) * Draw remaining entities. */ - for ( auto &part : particles ) { - if( !part->behind ) - part->draw(); - } + std::for_each( particles.begin(), particles.end(), [](Particles part) { if ( !part.behind ) part.draw(); }); for ( auto &n : npc ) n->draw(); @@ -757,7 +741,8 @@ singleDetect( Entity *e ) if(e->alive){ - if(e->type == MOBT && Mobp(e)->subtype == MS_TRIGGER)return; + if ( e->type == MOBT && Mobp(e)->subtype == MS_TRIGGER ) + return; /* * Calculate the line that this entity is currently standing on. @@ -825,17 +810,18 @@ detect( Player *p ) for ( auto &part : particles ) { int l; unsigned int i; - l=(part->loc.x + part->width / 2 - worldStart) / HLINE; + l=(part.loc.x + part.width / 2 - worldStart) / HLINE; if(l < 0) l=0; i = l; if(i > lineCount-1) i=lineCount-1; - if(part->loc.y < worldData[i].groundHeight){ - part->loc.y = worldData[i].groundHeight; - part->vely = 0; - part->velx = 0; - part->canMove = false; + if(part.loc.y < worldData[i].groundHeight){ + part.loc.y = worldData[i].groundHeight; + part.vely = 0; + part.velx = 0; + part.canMove = false; }else{ - if(part->gravity && part->vely > -2)part->vely-=.003 * deltaTime; + if(part.gravity && part.vely > -2) + part.vely-=.003 * deltaTime; } } for(auto &b : build){ @@ -851,14 +837,14 @@ detect( Player *p ) {0,0,255}, 2500); - particles.back()->fountain = true; + particles.back().fountain = true; } break; case FIRE_PIT: for(int r = 0; r < (rand()%20)+10;r++){ addParticle(rand()%(int)(b->width/2) + b->loc.x+b->width/4, b->loc.y+3*HLINE, HLINE, HLINE, rand()%2 == 0?-(rand()%3)*.01:(rand()%3)*.01,((4+rand()%6)*.005), {255,0,0}, 400); - particles.back()->gravity = false; - particles.back()->behind = true; + particles.back().gravity = false; + particles.back().behind = true; } break; default: break; @@ -882,36 +868,16 @@ detect( Player *p ) }*/ } -void World::addStructure(BUILD_SUB sub, float x,float y, char *tex, const char *inside){ +void World::addStructure(BUILD_SUB sub, float x,float y, std::string tex, std::string inside){ build.push_back(new Structures()); build.back()->inWorld = this; build.back()->textureLoc = tex; build.back()->spawn(sub,x,y); - - if(inside) - strcpy((build.back()->inside = new char[1 + strlen(inside)]),inside); - else - strcpy((build.back()->inside = new char[1]),"\0"); + + build.back()->inside = inside; entity.push_back(build.back()); } - -/*void World::addVillage(int bCount, int npcMin, int npcMax,const char *inside){ - std::cout << npcMin << ", " << npcMax << std::endl; - //int xwasd; - for(int i = 0; i < bCount; i++){ - addStructure(HOUSE,x_start + (i * 300),100,inside); - std::cout<<"1\n"; - HERE: - xwasd = (rand()%(int)x+1000*HLINE); - for(auto &bu : build){ - if(xwasd > bu->loc.x && xwasd < bu->loc.x+bu->width)goto HERE; - } - std::cout<<"2\n"; - addStructure(t,HOUSE,xwasd,y,inside); - std::cout<<"3\n"; - } -}*/ void World::addMob(int t,float x,float y){ mob.push_back(new Mob(t)); @@ -950,9 +916,11 @@ void World::addObject(/*ITEM_ID i*/std::string in,const char *p, float x, float entity.push_back(object.back()); } -void World::addParticle(float x, float y, float w, float h, float vx, float vy, Color color, int d){ - particles.push_back(new Particles(x,y,w,h,vx,vy,color,d)); - particles.back()->canMove = true; +void World:: +addParticle( float x, float y, float w, float h, float vx, float vy, Color color, int d ) +{ + particles.emplace_back( x, y, w, h, vx, vy, color, d ); + particles.back().canMove = true; } void World::addLight(vec2 loc, Color color){ @@ -1037,7 +1005,7 @@ goInsideStructure( Player *p ) p->loc.x + p->width < b->loc.x + b->width ){ inside.push_back((std::string)(currentXML.c_str() + 4)); - tmp = loadWorldFromXML(b->inside); + tmp = loadWorldFromXML(b->inside.c_str()); ui::toggleBlackFast(); ui::waitForCover(); @@ -1050,7 +1018,7 @@ goInsideStructure( Player *p ) strcpy((current = new char[strlen((const char *)(currentXML.c_str() + 4)) + 1]),(const char *)(currentXML.c_str() + 4)); tmp = loadWorldFromXML(inside.back().c_str()); for(auto &b : tmp->build){ - if(!strcmp(current,b->inside)){ + if(!strcmp(current,b->inside.c_str())){ inside.pop_back(); ui::toggleBlackFast(); @@ -1269,8 +1237,12 @@ void IndoorWorld::draw(Player *p){ * Draw all entities. */ - for(auto &part : particles) part->draw(); - for(auto &e : entity) e->draw(); + for ( auto &part : particles ) + part.draw(); + + for ( auto &e : entity ) + e->draw(); + p->draw(); } @@ -1324,18 +1296,23 @@ World *Arena::exitArena(Player *p){ #include using namespace tinyxml2; -std::string currentXML = "\0"; +std::string currentXML; extern World *currentWorld; World *loadWorldFromXML(const char *path){ - if ( currentXML != "\0" ) + if ( !currentXML.empty() ) currentWorld->save(); return loadWorldFromXMLNoSave(path); } -World *loadWorldFromXMLNoSave(const char *path){ +/** + * Loads a world from the given XML file. + */ + +World * +loadWorldFromXMLNoSave( const char *path ) { XMLDocument xml; XMLElement *wxml; XMLElement *vil; @@ -1451,13 +1428,12 @@ World *loadWorldFromXMLNoSave(const char *path){ * READS DATA ABOUT STRUCTURE CONTAINED IN VILLAGE */ - if(!strcmp(name,"structure")){ - ptr = vil->Attribute("inside"); + if(!strcmp(name,"structure")){ tmp->addStructure((BUILD_SUB)vil->UnsignedAttribute("type"), vil->QueryFloatAttribute("x", &spawnx) != XML_NO_ERROR ? randx : spawnx, 100, - (char*)vil->Attribute("texture"), - ptr); + vil->StrAttribute("texture"), + vil->StrAttribute("inside")); }else if(!strcmp(name, "stall")){ if(!strcmp(vil->Attribute("type"),"market")){ @@ -1466,8 +1442,8 @@ World *loadWorldFromXMLNoSave(const char *path){ vil->QueryFloatAttribute("x", &spawnx) != XML_NO_ERROR ? randx : spawnx, 100, - (char*)vil->Attribute("texture"), - ptr); + vil->StrAttribute("texture"), + vil->StrAttribute("inside")); tmp->addMerchant(0,100); if(!strcmp(name,"buy")){ std::cout << "Buying"; @@ -1482,8 +1458,8 @@ World *loadWorldFromXMLNoSave(const char *path){ vil->QueryFloatAttribute("x", &spawnx) != XML_NO_ERROR ? randx : spawnx, 100, - (char*)vil->Attribute("texture"), - ptr); + vil->StrAttribute("texture"), + vil->StrAttribute("inside")); } } vptr->build.push_back(tmp->build.back()); -- cgit v1.2.3 From 5fadac5d0f37f574ca160107d832d11b421ad559 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 4 Mar 2016 08:45:53 -0500 Subject: entity following --- Changelog | 11 ++++ Makefile | 9 ++-- include/common.h | 7 +-- include/entities.h | 3 ++ main.cpp | 46 +++++------------ src/Quest.cpp | 5 +- src/Texture.cpp | 8 +-- src/common.cpp | 40 +++------------ src/entities.cpp | 131 ++++++++++++++++++++++++++--------------------- src/gameplay.cpp | 34 +++++------- src/inventory.cpp | 7 ++- src/ui.cpp | 7 +-- src/world.cpp | 81 ++++++++++++++--------------- xml/playerSpawnHill1.xml | 2 +- 14 files changed, 183 insertions(+), 208 deletions(-) (limited to 'src/Texture.cpp') diff --git a/Changelog b/Changelog index f6309bf..624281e 100644 --- a/Changelog +++ b/Changelog @@ -722,3 +722,14 @@ - added StrAttribute() to TinyXML2 - more merchant stuff, trading???? - improved village XML loading + +3/4/2016: +========= + + - added following functionality to entities + - fixed some world shading issues + - re-added stars, but stars draw midday + - restarted work on arenas + - began implementing currency system + - made the moon! + - merchants are a-o-kay diff --git a/Makefile b/Makefile index 1df683c..db92260 100644 --- a/Makefile +++ b/Makefile @@ -12,9 +12,9 @@ ifeq ($(TARGET_OS),win32) -lSDL2main -lSDL2 -lSDL2_image -lSDL2_mixer -lfreetype endif -CXXFLAGS = -m$(TARGET_BITS) -std=c++11 +CXXFLAGS = -m$(TARGET_BITS) -std=c++14 CXXINC = -Iinclude -Iinclude/freetype2 -CXXWARN = -Wall -Wextra -Werror +CXXWARN = -Wall -Wextra -Werror -pedantic-errors CXXSRCDIR = src CXXOUTDIR = out @@ -32,7 +32,10 @@ clean: rm -f $(EXEC) rm -f out/*.o -$(EXEC): $(CXXOUTDIR)/$(CXXOBJ) +cleandata: + rm -rf xml/*.dat + +$(EXEC): $(CXXOUTDIR)/$(CXXOBJ) main.cpp @echo " CXX/LD main" @$(CXX) $(CXXFLAGS) $(CXXINC) $(CXXWARN) -o $(EXEC) main.cpp out/*.o $(LIBS) diff --git a/include/common.h b/include/common.h index 9650f49..58d561f 100644 --- a/include/common.h +++ b/include/common.h @@ -8,16 +8,17 @@ #ifndef COMMON_H #define COMMON_H +// holy moly #include #include #include #include -#include #include #include #include #include #include +#include #include #define GLEW_STATIC @@ -47,7 +48,7 @@ N abso(N v){ return v; } -extern GLuint colorIndex; +extern GLuint colorIndex; // Texture.cpp? /** * This structure contains a set of coordinates for ease of coding. @@ -104,7 +105,7 @@ typedef col Color; * Define the game's name (displayed in the window title). */ -#define GAME_NAME "Independent Study v.0.5 alpha - NOW WITH SOUND!" +#define GAME_NAME "Independent Study v.0.6 alpha - NOW WITH more c++" /** * The desired width of the game window. diff --git a/include/entities.h b/include/entities.h index 334bfeb..c10bcd4 100644 --- a/include/entities.h +++ b/include/entities.h @@ -132,6 +132,7 @@ void initEntity(); class Entity{ public: + Entity *followee; Inventory *inv; /* @@ -186,6 +187,8 @@ public: virtual void wander(int){} virtual void interact(){} + + void follow(Entity *e); virtual ~Entity(){} }; diff --git a/main.cpp b/main.cpp index e96a555..a9b172b 100644 --- a/main.cpp +++ b/main.cpp @@ -164,10 +164,10 @@ Menu pauseMenu; extern WorldWeather weather; -extern int fadeIntensity; -extern bool fadeEnable; -extern bool fadeWhite; -extern bool fadeFast; +extern int fadeIntensity; // ui.cpp +extern bool fadeEnable; // ui.cpp +extern bool fadeWhite; // ui.cpp +extern bool fadeFast; // ui.cpp unsigned int SCREEN_WIDTH; unsigned int SCREEN_HEIGHT; @@ -216,7 +216,7 @@ int main(int argc, char *argv[]){ static SDL_GLContext mainGLContext = NULL; - gameRunning=false; + gameRunning = false; /** * (Attempt to) Initialize SDL libraries so that we can use SDL facilities and eventually @@ -420,6 +420,8 @@ int main(int argc, char *argv[]){ **** GAMELOOP **** **************************/ + //currentWorld->mob.back()->followee = player; + gameRunning = true; while(gameRunning){ mainLoop(); @@ -482,6 +484,7 @@ void mainLoop(void){ */ prev = currentWorld; + //pool.Enqueue(ui::handleEvents); ui::handleEvents(); @@ -490,13 +493,13 @@ void mainLoop(void){ currentWorld->bgmPlay(prev); ui::dialogBoxExists = false; } - + if(prevPrevTime + MSEC_PER_TICK <= currentTime){ //pool.Enqueue(logic); logic(); prevPrevTime = currentTime; } - + /* * Update player and entity coordinates. */ @@ -902,36 +905,11 @@ void logic(){ } } } - /*for(auto &b : currentWorld->build){ - switch(b->bsubtype){ - case FOUNTAIN: - for(int r = 0; r < (rand()%25)+10;r++){ - currentWorld->addParticle( rand()%HLINE*3 + b->loc.x + b->width/2, - b->loc.y + b->height, - HLINE*1.25, - HLINE*1.25, - rand()%2 == 0?-(rand()%7)*.01:(rand()%7)*.01, - ((4+rand()%6)*.05), - {0,0,255}, - 2500); - - currentWorld->particles.back()->fountain = true; - } - break; - case FIRE_PIT: - for(int r = 0; r < (rand()%20)+10;r++){ - currentWorld->addParticle(rand()%(int)(b->width/2) + b->loc.x+b->width/4, b->loc.y+3*HLINE, HLINE, HLINE, rand()%2 == 0?-(rand()%3)*.01:(rand()%3)*.01,((4+rand()%6)*.005), {255,0,0}, 400); - currentWorld->particles.back()->gravity = false; - currentWorld->particles.back()->behind = true; - } - break; - default: break; - } - }*/ /* * Switch between day and night (SUNNY and DARK) if necessary. - */ + */ + if(!(tickCount%DAY_CYCLE)||!tickCount){ if ( weather == WorldWeather::Sunny ) weather = WorldWeather::Dark; 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 +#include +#include #include extern Player *player; @@ -33,8 +34,6 @@ int QuestHandler::assign(std::string title,std::string desc,std::string req){ return 0; } -#include - 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 LoadedTexture; +static std::vector 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 #include #include #include #ifndef __WIN32__ -#include -#include -#include -#include +# include +# include +# include +# include #endif // __WIN32__ +#include + #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 "<\n#include \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 5c60386..73105f1 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -1,20 +1,37 @@ -#include -#include - #include -#define RAND_DIALOG_COUNT 14 +#include +#include 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 AIpreload; // A dynamic array of AI functions that are being preloaded +std::vector 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 @@ -360,43 +379,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 AIpreload; // A dynamic array of AI functions that are being preloaded -std::vector 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 @@ -410,23 +425,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; @@ -468,6 +466,12 @@ void Object::interact(void){ } } +void Entity:: +follow( Entity *e ) +{ + followee = e; +} + /* * This spawns the structures * @@ -530,12 +534,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(); @@ -646,7 +663,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 #include - 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 AIpreaddr; // entities.cpp +extern std::vector AIpreload; // entities.cpp +std::vector dopt; -typedef struct { - NPC *npc; - unsigned int index; -} NPCDialog; +void destroyEverything(void); -std::vector 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 xmlFiles; XMLDocument xml; @@ -314,9 +311,6 @@ void initEverything(void){ atexit(destroyEverything); } -extern std::vector AIpreload; -extern std::vector AIpreaddr; - void destroyEverything(void){ currentWorld->save(); delete currentWorld; diff --git a/src/inventory.cpp b/src/inventory.cpp index 203e707..7400f89 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -14,6 +14,8 @@ static vec2 itemLoc; Mix_Chunk* swordSwing; static std::vector 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(); diff --git a/src/ui.cpp b/src/ui.cpp index c755a59..7333fc4 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -190,7 +190,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); @@ -230,7 +230,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;jglyph->bitmap.width*ftf->glyph->bitmap.rows;j++){ buf[j*4 ]=255;//fontColor[0]; @@ -1084,7 +1084,8 @@ namespace ui { } void takeScreenshot(GLubyte* pixels){ - GLubyte bgr[SCREEN_WIDTH*SCREEN_HEIGHT*3]; + std::vector 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 bef6fc0..5dbbe21 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -1,8 +1,12 @@ #include +#include #include #include +#include +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 inside; // tracks indoor worlds +std::vector battleNest; // tracks arenas +std::vector 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 (width + GROUND_HILLINESS, (WorldData) { false, {0,0}, 0, 0 }); + worldData = std::vector (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 (100, (vec2) { 0, 0 } ); + star = std::vector (100, vec2 { 0, 400 } ); + for ( auto &s : star ) { + s.x = (getRand() % (-worldStart * 2)) + worldStart; + s.y = (getRand() % SCREEN_HEIGHT) + 100.0f; + } } /** @@ -363,20 +385,6 @@ bgmPlay( World *prev ) const } } -/** - * Variables used by World::draw(). - * @{ - */ - -extern vec2 offset; -extern unsigned int tickCount; - -int worldShade = 0; - -/** - * @} - */ - /** * The world draw function. * @@ -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 = 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 pointArrayBuf = std::make_unique (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 inside; - World *World:: goInsideStructure( Player *p ) { @@ -1072,11 +1082,6 @@ void World::save(void){ out.close(); } -#include - -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 (lineCount, (WorldData) { false, {0,0}, INDOOR_FLOOR_HEIGHT, 0 }); + worldData = std::vector (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 pointArrayBuf = std::make_unique (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 battleNest; -std::vector 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 -using namespace tinyxml2; - -std::string currentXML; - -extern World *currentWorld; - World *loadWorldFromXML(std::string path){ if ( !currentXML.empty() ) currentWorld->save(); diff --git a/xml/playerSpawnHill1.xml b/xml/playerSpawnHill1.xml index 2cac743..c770e0f 100644 --- a/xml/playerSpawnHill1.xml +++ b/xml/playerSpawnHill1.xml @@ -4,7 +4,7 @@ - + -- cgit v1.2.3