1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
/* Sprite.cpp
Copyright (c) 2014 by Michael Zahniser
Endless Sky is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Endless Sky is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "Sprite.h"
#include "ImageBuffer.h"
#include "../Preferences.h"
#include "../Screen.h"
#include "../opengl.h"
#include <SDL2/SDL.h>
#include <algorithm>
using namespace std;
namespace {
void AddBuffer(ImageBuffer &buffer, uint32_t *target)
{
// Check whether this sprite is large enough to require size reduction.
if(Preferences::Has("Reduce large graphics") && buffer.Width() * buffer.Height() >= 1000000)
buffer.ShrinkToHalfSize();
// Upload the images as a single array texture.
glGenTextures(1, target);
glBindTexture(GL_TEXTURE_2D_ARRAY, *target);
// Use linear interpolation and no wrapping.
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Upload the image data.
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, // target, mipmap level, internal format,
buffer.Width(), buffer.Height(), buffer.Frames(), // width, height, depth,
0, GL_RGBA, GL_UNSIGNED_BYTE, buffer.Pixels()); // border, input format, data type, data.
// Unbind the texture.
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
// Free the ImageBuffer memory.
buffer.Clear();
}
}
Sprite::Sprite(const string &name)
: name(name)
{
}
const string &Sprite::Name() const
{
return name;
}
// Add the given frames, optionally uploading them. The given buffer will be cleared afterwards.
void Sprite::AddFrames(ImageBuffer &buffer, bool is2x)
{
// If this is the 1x image, its dimensions determine the sprite's size.
if(!is2x)
{
width = buffer.Width();
height = buffer.Height();
frames = buffer.Frames();
}
// Only non-empty buffers need to be added to the sprite.
if(buffer.Pixels())
AddBuffer(buffer, &texture[is2x]);
}
// Upload the given frames. The given buffer will be cleared afterwards.
void Sprite::AddSwizzleMaskFrames(ImageBuffer &buffer, bool is2x)
{
// Do nothing if the buffer is empty.
if(!buffer.Pixels())
return;
AddBuffer(buffer, &swizzleMask[is2x]);
}
// Free up all textures loaded for this sprite.
void Sprite::Unload()
{
if(texture[0] || texture[1])
{
glDeleteTextures(2, texture);
texture[0] = texture[1] = 0;
}
if(swizzleMask[0] || swizzleMask[1])
{
glDeleteTextures(2, swizzleMask);
swizzleMask[0] = swizzleMask[1] = 0;
}
width = 0.f;
height = 0.f;
frames = 0;
}
// Get the width, in pixels, of the 1x image.
float Sprite::Width() const
{
return width;
}
// Get the height, in pixels, of the 1x image.
float Sprite::Height() const
{
return height;
}
// Get the number of frames in the animation.
int Sprite::Frames() const
{
return frames;
}
// Get the offset of the center from the top left corner; this is for easy
// shifting of corner to center coordinates.
Point Sprite::Center() const
{
return Point(.5 * width, .5 * height);
}
// Get the texture index, based on whether the screen is high DPI or not.
uint32_t Sprite::Texture() const
{
return Texture(Screen::IsHighResolution());
}
// Get the index of the texture for the given high DPI mode.
uint32_t Sprite::Texture(bool isHighDPI) const
{
return (isHighDPI && texture[1]) ? texture[1] : texture[0];
}
// Get the texture index, based on whether the screen is high DPI or not.
uint32_t Sprite::SwizzleMask() const
{
return SwizzleMask(Screen::IsHighResolution());
}
// Get the index of the texture for the given high DPI mode.
uint32_t Sprite::SwizzleMask(bool isHighDPI) const
{
return (isHighDPI && swizzleMask[1]) ? swizzleMask[1] : swizzleMask[0];
}
|