]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
fixed bugs
authorClyne Sullivan <tullivan99@gmail.com>
Mon, 25 Apr 2016 12:48:39 +0000 (08:48 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Mon, 25 Apr 2016 12:48:39 +0000 (08:48 -0400)
include/common.hpp
include/inventory.hpp
include/world.hpp
src/inventory.cpp
src/ui.cpp
src/world.cpp

index 7f36be246dbbffb7c2d1867f18702141238445ca..5a0fcb26d5522ae2cfb8b7ff2868ff6d2ae811dc 100644 (file)
@@ -103,6 +103,10 @@ struct _vec2 {
                x = y = n;
                return *this;
        }
+       template<typename T>
+       const _vec2 operator+(const T &n) {
+               return _vec2 {x + n, y + n};
+       }
 };
 typedef struct _vec2 vec2;
 
index 4fb45f996de104faccfe32a891320cceab996b89..ce887c3530e89b1e7ae4844a63ac9704f70f8fff 100644 (file)
 class Item{
 public:
        std::string name, type;
+       std::string texloc;
+
+       Texturec *tex;
 
        float width;
        float height;
        int   maxStackSize;
        float attribValue;
 
-       std::string texloc;
-       Texturec *tex;
-
-       GLuint rtex()
-       {
-               return tex->image[0];
-       }
 };
 
 class Currency{
@@ -44,7 +40,7 @@ public:
        }
 };
 
-struct item_t{
+struct item_t {
        uint count;
        uint id;
 } __attribute__((packed));
index c6eaf0647647940e2db50aa2d7c1afb9973fa40b..dc8d4972e2720a72bacda5b3d5a697699917d59e 100644 (file)
@@ -220,7 +220,7 @@ public:
        bool goWorldLeft(NPC *e);
 
        // attempts to enter a structure that the player would be standing in front of
-       World *goInsideStructure(Player *p);
+       std::pair<World *, float> goInsideStructure(Player *p);
 
        // adds a hole at the specified start and end x-coordinates
        void addHole(unsigned int start,unsigned int end);
index 34d99edb27e1de1497e6573f8ad1b78bfd0a5732..f8b8c3e36c2ef529c073b6ff551ab37a9e78dfa6 100644 (file)
@@ -1,4 +1,6 @@
 #include <inventory.hpp>
+
+#include <common.hpp>
 #include <entities.hpp>
 #include <ui.hpp>
 #include <gametime.hpp>
@@ -16,43 +18,94 @@ Mix_Chunk* swordSwing;
 
 static std::vector<Item *> itemMap;
 static std::vector<Currency *> currencyMap;
-static GLuint *itemtex;
 void itemDraw(Player *p,uint id);
 
-void items(void) {
+void initInventorySprites(void) {
        XMLDocument xml;
        xml.LoadFile("config/items.xml");
        XMLElement *exml = xml.FirstChildElement("item");
        XMLElement *cxml = xml.FirstChildElement("currency");
-       while (cxml) {
 
+       while (cxml) {
                currencyMap.push_back(new Currency());
 
-               currencyMap.back()->width  = 7*HLINE;
-               currencyMap.back()->height = 7*HLINE;
-
-               currencyMap.back()->name = cxml->Attribute("name");
+               currencyMap.back()->width  = HLINES(7);
+               currencyMap.back()->height = HLINES(7);
+               currencyMap.back()->name = cxml->StrAttribute("name");
                currencyMap.back()->value = cxml->FloatAttribute("value");
 
-
                cxml = cxml->NextSiblingElement();
        }
-       while (exml) {
 
+       while (exml) {
                itemMap.push_back(new Item());
 
+               itemMap.back()->name = exml->StrAttribute("name");
+               itemMap.back()->type = exml->StrAttribute("type");
+               itemMap.back()->texloc = exml->StrAttribute("sprite");
                itemMap.back()->width  = exml->FloatAttribute("width") * HLINE;
                itemMap.back()->height = exml->FloatAttribute("height") * HLINE;
                itemMap.back()->maxStackSize = exml->UnsignedAttribute("maxstack");
                itemMap.back()->attribValue = exml->FloatAttribute("value");
+               itemMap.back()->tex = new Texturec({ itemMap.back()->texloc });
 
+               exml = exml->NextSiblingElement();
+       }
+
+       swordSwing = Mix_LoadWAV("assets/sounds/shortSwing.wav");
+       Mix_Volume(2, 100);
+}
 
-               itemMap.back()->name = exml->Attribute("name");
-               itemMap.back()->type = exml->Attribute("type");
-               itemMap.back()->texloc = exml->Attribute("sprite");
+void destroyInventory(void) {
 
-               exml = exml->NextSiblingElement();
+       while(!itemMap.empty()) {
+               delete itemMap.front();
+               itemMap.erase(itemMap.begin());
+       }
+
+       Mix_FreeChunk(swordSwing);
+}
+
+
+const char *getItemTexturePath(std::string name){
+       for (auto &i : itemMap) {
+               if (i->name == name)
+                       return i->texloc.c_str();
        }
+       return NULL;
+}
+
+GLuint getItemTexture(std::string name) {
+       for (auto &i : itemMap) {
+               if (i->name == name)
+                       return i->tex->image[0];
+       }
+
+       return 0;
+}
+
+float getItemWidth(std::string name) {
+       for(auto &i : itemMap) {
+               if (i->name == name)
+                       return i->width;
+       }
+       return 0;
+}
+
+float getItemHeight(std::string name) {
+       for(auto &i : itemMap) {
+               if (i->name == name)
+                       return i->height;
+       }
+       return 0;
+}
+
+Inventory::Inventory(unsigned int s) {
+       sel=0;
+       size=s;
+}
+
+Inventory::~Inventory(void) {
 }
 
 int Inventory::addItem(std::string name,uint count) {
@@ -128,71 +181,6 @@ int Inventory::hasItem(std::string name) {
        return 0;
 }
 
-void initInventorySprites(void) {
-
-       items();
-       itemtex = new GLuint[itemMap.size()];
-
-       for(unsigned int i = 0;i<itemMap.size();i++) {
-               itemtex[i] = Texture::loadTexture(getItemTexturePath(itemMap[i]->name));
-       }
-
-       swordSwing = Mix_LoadWAV("assets/sounds/shortSwing.wav");
-       Mix_Volume(2,100);
-}
-
-void destroyInventory(void) {
-
-       while(!itemMap.empty()) {
-               delete itemMap.front();
-               itemMap.erase(itemMap.begin());
-       }
-
-       Mix_FreeChunk(swordSwing);
-}
-
-
-const char *getItemTexturePath(std::string name){
-       for (auto &i : itemMap) {
-               if (i->name == name)
-                       return i->texloc.c_str();
-       }
-       return NULL;
-}
-
-GLuint getItemTexture(std::string name) {
-       for (int i = itemMap.size(); i--;) {
-               if (itemMap[i]->name == name)
-                       return itemtex[i];
-       }
-       DEBUG_printf("Failed to find texture for item %s!", name.c_str());
-       return 0;
-}
-
-float getItemWidth(std::string name) {
-       for(auto &i : itemMap) {
-               if (i->name == name)
-                       return i->width;
-       }
-       return 0;
-}
-
-float getItemHeight(std::string name) {
-       for(auto &i : itemMap) {
-               if (i->name == name)
-                       return i->height;
-       }
-       return 0;
-}
-
-Inventory::Inventory(unsigned int s) {
-       sel=0;
-       size=s;
-}
-
-Inventory::~Inventory(void) {
-}
-
 void Inventory::setSelection(unsigned int s) {
        sel=s;
 }
@@ -247,8 +235,9 @@ void Inventory::draw(void) {
 
        for (int r = 0; r < 4; r++) {
                for (int c = 0; c < 8; c++) {
-                       massRay[a  ].x = ((offset.x - SCREEN_WIDTH  / 2) + itemWide) + c * itemWide * 1.5f;
-                       massRay[a++].y = ((offset.y + SCREEN_HEIGHT / 2) - itemWide * 1.5f) - r * itemWide * 1.5f;
+                       massRay[a].x = ((offset.x - SCREEN_WIDTH  / 2) + itemWide) + c * itemWide * 1.5f;
+                       massRay[a].y = ((offset.y + SCREEN_HEIGHT / 2) - itemWide * 1.5f) - r * itemWide * 1.5f;
+                       a++;
                }
        } a = 0;
 
@@ -330,28 +319,31 @@ void Inventory::draw(void) {
                        glEnd();
                        if (!items.empty() && a+numSlot < items.size() && items[a+numSlot].count) {
                                glEnable(GL_TEXTURE_2D);
-                               glBindTexture(GL_TEXTURE_2D, itemtex[items[a+numSlot].id]);
+                               itemMap[items[a + numSlot].id]->tex->bind(0);
                                glColor4f(1.0f, 1.0f, 1.0f, ((float)massDfp[a]/(float)(massRange?massRange:1))*0.8f);
                                glBegin(GL_QUADS);
-                                       if (itemMap[items[a+numSlot].id]->height > itemMap[items[a+numSlot].id]->width) {
-                                               glTexCoord2i(0,1);glVertex2i(mr.x-((itemWide/2)*((float)itemMap[items[a+numSlot].id]->width/(float)itemMap[items[a+numSlot].id]->height)),      mr.y-(itemWide/2));
-                                               glTexCoord2i(1,1);glVertex2i(mr.x+((itemWide/2)*((float)itemMap[items[a+numSlot].id]->width/(float)itemMap[items[a+numSlot].id]->height)),      mr.y-(itemWide/2));
-                                               glTexCoord2i(1,0);glVertex2i(mr.x+((itemWide/2)*((float)itemMap[items[a+numSlot].id]->width/(float)itemMap[items[a+numSlot].id]->height)),      mr.y+(itemWide/2));
-                                               glTexCoord2i(0,0);glVertex2i(mr.x-((itemWide/2)*((float)itemMap[items[a+numSlot].id]->width/(float)itemMap[items[a+numSlot].id]->height)),      mr.y+(itemWide/2));
-                                       }else{
-                                               glTexCoord2i(0,1);glVertex2i(mr.x-(itemWide/2),mr.y-(itemWide/2)*((float)itemMap[items[a+numSlot].id]->height/(float)itemMap[items[a+numSlot].id]->width));
-                                               glTexCoord2i(1,1);glVertex2i(mr.x+(itemWide/2),mr.y-(itemWide/2)*((float)itemMap[items[a+numSlot].id]->height/(float)itemMap[items[a+numSlot].id]->width));
-                                               glTexCoord2i(1,0);glVertex2i(mr.x+(itemWide/2),mr.y+(itemWide/2)*((float)itemMap[items[a+numSlot].id]->height/(float)itemMap[items[a+numSlot].id]->width));
-                                               glTexCoord2i(0,0);glVertex2i(mr.x-(itemWide/2),mr.y+(itemWide/2)*((float)itemMap[items[a+numSlot].id]->height/(float)itemMap[items[a+numSlot].id]->width));
+                                       ivec2 q = {(int)(itemWide / 2 * itemMap[items[a + numSlot].id]->width / itemMap[items[a + numSlot].id]->height), itemWide / 2};
+                                       if (itemMap[items[a + numSlot].id]->height > itemMap[items[a + numSlot].id]->width) {
+                                               glTexCoord2i(0, 1);glVertex2i(mr.x - q.x, mr.y - q.y);
+                                               glTexCoord2i(1, 1);glVertex2i(mr.x + q.x, mr.y - q.y);
+                                               glTexCoord2i(1, 0);glVertex2i(mr.x + q.x, mr.y + q.y);
+                                               glTexCoord2i(0, 0);glVertex2i(mr.x - q.x, mr.y + q.y);
+                                       } else {
+                                               q.x = itemWide / 2 * itemMap[items[a + numSlot].id]->height / itemMap[items[a+numSlot].id]->width;
+                                               glTexCoord2i(0, 1);glVertex2i(mr.x - q.y, mr.y - q.x);
+                                               glTexCoord2i(1, 1);glVertex2i(mr.x + q.y, mr.y - q.x);
+                                               glTexCoord2i(1, 0);glVertex2i(mr.x + q.y, mr.y + q.x);
+                                               glTexCoord2i(0, 0);glVertex2i(mr.x - q.y, mr.y + q.x);
                                        }
                                glEnd();
                                glDisable(GL_TEXTURE_2D);
-                               ui::setFontColor(255,255,255,((float)massDfp[a]/(float)(massRange?massRange:1))*255);
-                               ui::putText(mr.x-(itemWide/2)+(itemWide*.85),mr.y-(itemWide/2),"%d",items[a+numSlot].count);
-                               ui::setFontColor(255,255,255,255);
+
+                               ui::setFontColor(255, 255, 255, (float)massDfp[a] / (float)(massRange ? massRange : 1) * 255);
+                               ui::putText(mr.x - itemWide / 2 + itemWide * 0.85f, mr.y - itemWide / 2, "%d", items[a + numSlot].count);
+                               ui::setFontColor(255, 255, 255, 255);
                        }
                        a++;
-               }a=0;
+               } a = 0;
 
                for(auto &cr : curRay) {
                        curCurCoord[a].x -= float((curdfp[a]) * cos(-1));
@@ -368,6 +360,7 @@ void Inventory::draw(void) {
                        a++;
                }a=0;
 
+               bool done = false;
                for(auto &r : iray) {
                        angle = 180 - (angleB * a) - angleB / 2.0f;
                        curCoord[a].x += float((dfp[a]) * cos(angle*PI/180));
@@ -382,9 +375,9 @@ void Inventory::draw(void) {
                                glVertex2i(r.end.x-(itemWide/2),                 r.end.y-(itemWide/2)+itemWide);
                        glEnd();
 
-                       if (!items.empty() && a < numSlot && items[a].count) {
+                       if (!done && !items.empty() && a < numSlot && items[a].count) {
                                glEnable(GL_TEXTURE_2D);
-                               glBindTexture(GL_TEXTURE_2D, itemtex[items[a].id]);
+                               itemMap[items[a].id]->tex->bind(0);
                                glColor4f(1.0f, 1.0f, 1.0f, ((float)dfp[a]/(float)(range?range:1))*0.8f);
                                glBegin(GL_QUADS);
                                        if (itemMap[items[a].id]->height > itemMap[items[a].id]->width) {
@@ -404,6 +397,8 @@ void Inventory::draw(void) {
                                ui::putStringCentered(r.end.x,r.end.y-(itemWide*.9),itemMap[items[a].id]->name);
                                ui::putText(r.end.x-(itemWide/2)+(itemWide*.85),r.end.y-(itemWide/2),"%d",items[a].count);
                                ui::setFontColor(255,255,255,255);
+                       } else {
+                               done = true;
                        }
 
                        if (sel == a) {
@@ -499,7 +494,7 @@ void Inventory::draw(void) {
 
                        if (a < items.size() && items[a].count) {
                                glEnable(GL_TEXTURE_2D);
-                               glBindTexture(GL_TEXTURE_2D, itemtex[items[a].id]);
+                               itemMap[items[a + numSlot].id]->tex->bind(0);
                                glColor4f(1.0f, 1.0f, 1.0f, a == highlight ? 0.8f : 0.2f);
                                glBegin(GL_QUADS);
                                        if(itemMap[items[a].id]->height > itemMap[items[a].id]->width){
@@ -568,7 +563,9 @@ void itemDraw(Player *p,uint id) {
        glRotatef(hangle, 0.0f, 0.0f, 1.0f);
        glTranslatef(-itemLoc.x,-itemLoc.y,0);
        glEnable(GL_TEXTURE_2D);
-       glBindTexture(GL_TEXTURE_2D,itemtex[id]);
+
+       itemMap[id]->tex->bind(0);
+
        glColor4ub(255,255,255,255);
        glBegin(GL_QUADS);
                glTexCoord2i(0,1);glVertex2f(itemLoc.x,                                   itemLoc.y);
index 8d3762d1909493998eaeab005132a686dcb54f1d..e30993a2b01f32e7094a97848ad2da458aa55609 100644 (file)
@@ -1059,8 +1059,20 @@ EXIT:
                                                        currentWorld = ((Arena *)currentWorld)->exitArena(player);
                                                        if (tmp != currentWorld)
                                                                toggleBlackFast();
-                                               } else if ((tmp = currentWorld->goInsideStructure(player)) != currentWorld)
-                                                       currentWorld = tmp;
+                                               } else {
+                                                       auto tmpp = currentWorld->goInsideStructure(player);
+
+                                                       if (tmpp.first != currentWorld) {
+                                                               ui::toggleBlackFast();
+                                                               ui::waitForCover();
+
+                                                               currentWorld = tmpp.first;
+                                                               if (tmpp.second)
+                                                                       player->loc.x = tmpp.second;
+
+                                                               ui::toggleBlackFast();
+                                                       }
+                                               }
                                                break;
                                        case SDLK_LSHIFT:
                                                if (debug) {
index c5ed9d00532c18398d0b89ba3ef7f4eef12ebb49..a1d0fc5f20d417cc1e1412cfdb56cc66cf8ecaf0 100644 (file)
@@ -1102,7 +1102,7 @@ goWorldLeft(NPC *e)
 /**
  * Attempts to enter a building that the player is standing in front of.
  */
-World *World::
+std::pair<World *, float> World::
 goInsideStructure(Player *p)
 {
        World *tmp;
@@ -1114,52 +1114,42 @@ goInsideStructure(Player *p)
         auto b = *d;
 
         if ((d == std::end(build)) || b->inside.empty())
-            return this;
+            return std::make_pair(this, 0);
 
         // +size cuts folder prefix
                inside.push_back(&currentXML[xmlFolder.size()]);
                tmp = loadWorldFromXML(b->inside);
 
-        // make the fade, as we let it fade back the worlds should be switched
-               ui::toggleBlackFast();
-               ui::waitForCover();
-               ui::toggleBlackFast();
-        glClearColor(0, 0, 0, 1);
-
-               return tmp;
+               return std::make_pair(tmp, 0);
        } else {
         std::string current = &currentXML[xmlFolder.size()];
                tmp = loadWorldFromXML(inside.back());
         inside.clear();
 
-        Structures *b;
-        for (auto &s : build) {
+        Structures *b = nullptr;
+        for (auto &s : tmp->build) {
             if (s->inside == current) {
                 b = s;
                 break;
             }
         }
-        //auto b = *std::find_if(std::begin(build), std::end(build), [&](const Structures *s) {
-        //    return (s->inside == current);
-        //});
 
-               ui::toggleBlackFast();
-               ui::waitForCover();
-               p->loc.x = b->loc.x + (b->width / 2);
-               ui::toggleBlackFast();
-        glClearColor(1, 1, 1, 1);
+        if (b == nullptr)
+            return std::make_pair(this, 0);
 
-               return tmp;
+        return std::make_pair(tmp, b->loc.x + (b->width / 2));
        }
 
-       return this;
+       return std::make_pair(this, 0);
 }
 
-void World::addStructure(BUILD_SUB sub, float x,float y, std::string tex, std::string 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);
+       build.back()->spawn(sub, x, y);
        build.back()->inside = inside;
        entity.push_back(build.back());
 }
@@ -1171,31 +1161,39 @@ addVillage(std::string name, World *world)
     return &village.back();
 }
 
-void World::addMob(int t,float x,float y){
+void World::
+addMob(int t, float x, float y)
+{
        mob.push_back(new Mob(t));
-       mob.back()->spawn(x,y);
+       mob.back()->spawn(x, y);
 
        entity.push_back(mob.back());
 }
 
-void World::addMob(int t,float x,float y,void (*hey)(Mob *)){
+void World::
+addMob(int t, float x, float y, void (*hey)(Mob *))
+{
        mob.push_back(new Mob(t));
-       mob.back()->spawn(x,y);
+       mob.back()->spawn(x, y);
        mob.back()->hey = hey;
 
        entity.push_back(mob.back());
 }
 
-void World::addNPC(float x,float y){
+void World::
+addNPC(float x, float y)
+{
        npc.push_back(new NPC());
-       npc.back()->spawn(x,y);
+       npc.back()->spawn(x, y);
 
        entity.push_back(npc.back());
 }
 
-void World::addMerchant(float x, float y, bool housed){
+void World::
+addMerchant(float x, float y, bool housed)
+{
        merchant.push_back(new Merchant());
-       merchant.back()->spawn(x,y);
+       merchant.back()->spawn(x, y);
 
     if (housed)
         merchant.back()->inside = build.back();
@@ -1204,7 +1202,9 @@ void World::addMerchant(float x, float y, bool housed){
        entity.push_back(npc.back());
 }
 
-void World::addObject(std::string in, std::string p, float x, float y){
+void World::
+addObject(std::string in, std::string p, float x, float y)
+{
        object.emplace_back(in, p);
        object.back().spawn(x, y);