2020-01-29 17:19:26 +09:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* OpenMiner
|
2020-02-25 01:42:10 +09:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
|
2020-02-25 01:42:10 +09:00
|
|
|
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
|
|
|
|
*
|
|
|
|
* This file is part of OpenMiner.
|
2020-01-29 17:19:26 +09:00
|
|
|
*
|
2020-02-25 01:42:10 +09:00
|
|
|
* OpenMiner is free software; you can redistribute it and/or
|
2020-02-08 18:34:26 +09:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2020-01-29 17:19:26 +09:00
|
|
|
*
|
2020-02-25 01:42:10 +09:00
|
|
|
* OpenMiner is distributed in the hope that it will be useful,
|
2020-02-08 18:34:26 +09:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2020-01-29 17:19:26 +09:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2020-02-25 01:42:10 +09:00
|
|
|
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
|
2020-02-08 18:34:26 +09:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2020-01-29 17:19:26 +09:00
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
#ifndef TEXTUREATLAS_HPP_
|
|
|
|
#define TEXTUREATLAS_HPP_
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
2020-01-30 15:31:49 +09:00
|
|
|
#include <vector>
|
2020-01-29 17:19:26 +09:00
|
|
|
|
|
|
|
#include <gk/core/IntTypes.hpp>
|
|
|
|
#include <gk/core/SDLHeaders.hpp>
|
2020-01-30 15:31:49 +09:00
|
|
|
#include <gk/core/Rect.hpp>
|
|
|
|
#include <gk/gl/Texture.hpp>
|
2020-01-29 17:19:26 +09:00
|
|
|
|
|
|
|
class TextureAtlas {
|
|
|
|
public:
|
|
|
|
TextureAtlas() = default;
|
|
|
|
|
2020-01-30 15:31:49 +09:00
|
|
|
void addFile(const std::string &path, const std::string &filename);
|
2020-01-29 17:19:26 +09:00
|
|
|
void packTextures();
|
|
|
|
|
2020-03-18 00:33:48 +01:00
|
|
|
void loadFromRegistry(const std::string &texturePack = "");
|
2020-01-29 17:19:26 +09:00
|
|
|
|
2020-01-30 15:31:49 +09:00
|
|
|
u16 getTextureID(const std::string &filename) const;
|
|
|
|
gk::FloatRect getTexCoords(const std::string &filename, bool normalized = true) const;
|
|
|
|
|
|
|
|
const gk::Texture &texture() const { return m_texture; }
|
|
|
|
|
|
|
|
bool isReady() const { return m_isReady; }
|
2020-01-29 17:19:26 +09:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Expected texture size
|
2020-01-30 15:31:49 +09:00
|
|
|
u16 m_tileSize = 0;
|
2020-01-29 17:19:26 +09:00
|
|
|
|
|
|
|
// Mapping between filename and internal texture ID
|
|
|
|
std::unordered_map<std::string, u16> m_textureMap;
|
|
|
|
|
|
|
|
// Textures to pack together
|
|
|
|
using SurfacePtr = std::unique_ptr<SDL_Surface, decltype(&SDL_FreeSurface)>;
|
2020-01-30 15:31:49 +09:00
|
|
|
std::vector<SurfacePtr> m_textures;
|
2020-01-29 17:19:26 +09:00
|
|
|
|
|
|
|
// Packed texture
|
2020-01-30 15:31:49 +09:00
|
|
|
gk::Texture m_texture;
|
|
|
|
|
|
|
|
// Is the texture atlas ready to use?
|
|
|
|
bool m_isReady = false;
|
2020-01-29 17:19:26 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // TEXTUREATLAS_HPP_
|