]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
mem track+, drop func. added
authorClyne Sullivan <tullivan99@gmail.com>
Sun, 30 Apr 2017 20:16:41 +0000 (16:16 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Sun, 30 Apr 2017 20:16:41 +0000 (16:16 -0400)
include/inventory.hpp
main.cpp
src/inventory.cpp
src/player.cpp
src/world.cpp

index 8329d72ac79baa38409e8cae8f45e0ad18eea644..1de22dc9f63a73c71edd5f7e3d7f24d08a61f1ee 100644 (file)
@@ -75,8 +75,8 @@ struct InventoryEntry {
 
        vec2 loc;   /**< Used by render, to determine slot location on screen */
 
-       InventoryEntry(void)
-               : item(nullptr), count(0) {}
+       InventoryEntry(Item* i = nullptr, int c = 0, vec2 l = vec2())
+               : item(i), count(c), loc(l) {}
 };
 
 struct UseItemEvent {
@@ -139,8 +139,11 @@ public:
         */
        static bool take(const std::string& name, int count);
 
-       static inline Item getItem(const std::string& s)
-       { return itemList[s]; }
+       static void makeDrop(const vec2& p, InventoryEntry& ie);
+       static void makeDrop(const vec2& p, const std::string& s, int c);
+
+       static inline Item* getItem(const std::string& s)
+       { return &itemList[s]; }
 };
 
 #endif // INVENTORY_HPP_
index b6e5e24f0508b18e5656598b704ce847b5682b88..df209c6f01d303daf165a072ec1654aa4825938a 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -35,6 +35,15 @@ std::string xmlFolder = "./xml/";
  */
 vec2 offset;
 
+std::size_t getUsedMem(void);
+void deleteRemaining(void);
+extern int balance;
+
+void print(void) {
+       std::cout << "Used memory: " << getUsedMem() << " bytes\n";
+       std::cout << "New/delete balance: " << balance << '\n';
+}
+
 /**
  * The main program.
  * Init, load, run. Die.
@@ -44,6 +53,10 @@ int main(int argc, char *argv[])
        static bool worldReset = false, worldDontReallyRun = false;
        std::string worldActuallyUseThisXMLFile;
 
+       print();
+       atexit(print);
+       //atexit(deleteRemaining);
+
        // handle command line args
        if (argc > 1) {
                for (int i = 1; i < argc; i++) {
@@ -191,7 +204,7 @@ int main(int argc, char *argv[])
        WorldSystem::die();
        WindowSystem::die();
 
-    return 0; // Calls everything passed to atexit
+       return 0; // Calls everything passed to atexit
 }
 
 constexpr int memEntries = 2048;
@@ -210,11 +223,24 @@ std::size_t getUsedMem(void)
        return total;
 }
 
+void deleteRemaining(void)
+{
+       for (int i = 0; i < memEntries; i++) {
+               if (mems[i] != nullptr) {
+                       balance--;
+                       std::free(mems[i]);
+                       mems[i] = nullptr;
+                       sizs[i] = 0;
+               }
+       }
+               
+}
+
 void *operator new(std::size_t n) throw (std::bad_alloc)
 {
-       auto buf = std::malloc(n);
        balance++;
 
+       auto buf = std::malloc(n);
        if (buf == nullptr)
                throw std::bad_alloc();
 
@@ -232,11 +258,10 @@ void *operator new(std::size_t n) throw (std::bad_alloc)
 void operator delete(void* p) throw ()
 {
        if (p != nullptr) {
-               std::free(p);
                balance--;
-
                for (int i = 0; i < memEntries; i++) {
                        if (mems[i] == p) {
+                               std::free(p);
                                mems[i] = nullptr;
                                sizs[i] = 0;
                                break;
@@ -250,11 +275,10 @@ void operator delete(void* p, std::size_t n) throw ()
        (void)n;
 
        if (p != nullptr) {
-               std::free(p);
                balance--;
-
                for (int i = 0; i < memEntries; i++) {
                        if (mems[i] == p) {
+                               std::free(p);
                                mems[i] = nullptr;
                                sizs[i] = 0;
                                break;
index 434eac6fbe7c29074f96cf032c046007f95183ef..96c1d6d9b66982d4e80ba2d0065c7b4f9cf02254 100644 (file)
@@ -219,25 +219,45 @@ void InventorySystem::receive(const MouseReleaseEvent &mre)
                        }
                }
 
-               auto e = game::entities.create();
-               e.assign<Position>(mre.position.x, mre.position.y);
-               e.assign<Direction>(0, 0.1f);
-               e.assign<ItemDrop>(items[movingItem]);
-               e.assign<Sprite>();
-               e.component<Sprite>()->addSpriteSegment(
-                       SpriteData(items[movingItem].item->sprite), vec2(0, 0));
-               auto dim = items[movingItem].item->sprite.getDim();
-               e.assign<Solid>(HLINES(dim.x), HLINES(dim.y));
-               e.assign<Visible>();
-               e.assign<Physics>();
-
+               makeDrop(mre.position, items[movingItem]);
                items[movingItem].item = nullptr;
                items[movingItem].count = 0;
-
                movingItem = -1;
        }
 }
 
+void InventorySystem::makeDrop(const vec2& p, InventoryEntry& ie)
+{
+       auto e = game::entities.create();
+       e.assign<Position>(p.x, p.y);
+       e.assign<Direction>(0, 0.1f);
+       e.assign<ItemDrop>(ie);
+       e.assign<Sprite>();
+       e.component<Sprite>()->addSpriteSegment(
+               SpriteData(ie.item->sprite), vec2(0, 0));
+       auto dim = ie.item->sprite.getDim();
+       e.assign<Solid>(HLINES(dim.x), HLINES(dim.y));
+       e.assign<Visible>();
+       e.assign<Physics>();
+}
+
+void InventorySystem::makeDrop(const vec2& p, const std::string& s, int c)
+{
+       auto item = getItem(s);
+       auto e = game::entities.create();
+       e.assign<Position>(p.x, p.y);
+       e.assign<Direction>(0, 0.1f);
+       InventoryEntry ie (item, c);
+       e.assign<ItemDrop>(ie);
+       e.assign<Sprite>();
+       e.component<Sprite>()->addSpriteSegment(
+               SpriteData(item->sprite), vec2(0, 0));
+       auto dim = item->sprite.getDim();
+       e.assign<Solid>(HLINES(dim.x), HLINES(dim.y));
+       e.assign<Visible>();
+       e.assign<Physics>();
+}
+
 void InventorySystem::receive(const KeyDownEvent &kde)
 {
     if (kde.keycode == SDLK_e) {
index 0256190ccd4a252e6b1f316a7c02c20144a0c26e..01b8a7c38696225d7018ffee99c65c0bc3eafc81 100644 (file)
@@ -245,7 +245,7 @@ void PlayerSystem::receive(const UseItemEvent& uie)
                                e.assign<Physics>();
                                auto sprite = e.assign<Sprite>();
                                auto tex = InventorySystem::getItem("Arrow");
-                               sprite->addSpriteSegment(SpriteData(tex.sprite), 0);
+                               sprite->addSpriteSegment(SpriteData(tex->sprite), 0);
                                auto dim = HLINES(sprite->getSpriteSize());
                                e.assign<Solid>(dim.x, dim.y);
                                e.assign<Hit>(10, false);
index bf349739ab9e29233901cb26794c995c286cbe9f..912a0c9a0d1c332882a0372c9409a0ae461c6837 100644 (file)
@@ -669,14 +669,13 @@ void WorldSystem::render(void)
        //glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.3 - static_cast<float>(alpha) / 255.0f);
        //makeWorldDrawingSimplerEvenThoughAndyDoesntThinkWeCanMakeItIntoFunctions(0, fron_tex_coord, tex_coord, 6);
 
-       // TODO make stars dynamic (make them particles??)
-       static const Texture starTex ("assets/style/classic/bg/star.png"); // TODO why in theme, not just std.?
-       const static float stardim = 24;
-       GLfloat* star_coord = new GLfloat[stars.size() * 5 * 6 + 1];
-    GLfloat* si = &star_coord[0];
-
        if (worldShade > 0) {
 
+               static const Texture starTex ("assets/style/classic/bg/star.png"); // TODO why in theme, not just std.?
+               static const float stardim = 24;
+
+               GLfloat* star_coord = new GLfloat[stars.size() * 5 * 6 + 1];
+       auto si = &star_coord;
                auto xcoord = offset.x * 0.9f;
 
                for (auto &s : stars) {
@@ -698,9 +697,9 @@ void WorldSystem::render(void)
                glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &star_coord[0]);
                glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &star_coord[3]);
                glDrawArrays(GL_TRIANGLES, 0, stars.size() * 6);
-       }
 
-       delete[] star_coord;
+               delete[] star_coord;
+       }
 
        Render::worldShader.disable();