AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
TexturePacker.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12#pragma once
   13
   14#include <glm/glm.hpp>
   15#include "AUI/Common/AVector.h"
   16#include "AUI/Common/SharedPtr.h"
   17
   18namespace Util {
   19    typedef int dim;
   20    class Rect {
   21    public:
   22        dim x, y, width, height;
   23        Rect(dim x, dim y, dim width, dim height);
   24        Rect() = default;
   25        bool hasPoint(dim x, dim y) const;
   26        bool collidesWith(const Rect& rect);
   27    };
   28    class TexturePacker_Lol {
   29    protected:
   30        dim side;
   31        AVector<Rect> mRects;
   32        bool noCollision(const Rect& r);
   33        bool check(Rect& res, Rect r);
   34        bool allocateRect(Rect& r, dim width, dim height);
   35    };
   36
   37    template<class T>
   38    class TexturePacker : public TexturePacker_Lol {
   39    protected:
   40        glm::vec4 insert(T& data, dim width, dim height)
   41        {
   42            if (side == 0)
   43                resize(data, 64);
   44            Rect r(0, 0, 0, 0);
   45            while (!allocateRect(r, width, height))
   46            {
   47                resize(data, side * 2);
   48            }
   49            this->onInsert(data, r.x, r.y);
   50            return { float(r.x), float(r.y), float(r.x + r.width), float(r.y + r.height) };
   51        }
   52
   53        virtual void onResize(T& data, dim side) = 0;
   54        virtual void onInsert(T& data, const dim& x, const dim& y) = 0;
   55
   56    public:
   57        TexturePacker()
   58        {
   59            side = 0;
   60        }
   61        virtual ~TexturePacker() {
   62
   63        }
   64
   65        virtual glm::vec4 insert(T& data) = 0;
   66
   67        void resize(T& data, dim size)
   68        {
   69            side = size;
   70            this->onResize(data, size);
   71        }
   72    };
   73}
   74
A std::vector with AUI extensions.
Definition AVector.h:39
Definition TexturePacker.h:20
Definition TexturePacker.h:28