1 #include "ArrayTexture.hpp"
6 #include "TextureBase.hpp"
7 #include "Viewport.hpp"
9 #include "../app/error.hpp"
20 Font::Font(const char *src, int size, long index)
21 : handle(TTF_OpenFontIndex(src, size, index)) {
23 throw TTFError("TTF_OpenFontIndex");
29 TTF_CloseFont(handle);
33 Font::Font(Font &&other) noexcept
34 : handle(other.handle) {
35 other.handle = nullptr;
38 Font &Font::operator =(Font &&other) noexcept {
39 std::swap(handle, other.handle);
44 int Font::Style() const noexcept {
45 return TTF_GetFontStyle(handle);
48 void Font::Style(int s) const noexcept {
49 TTF_SetFontStyle(handle, s);
52 int Font::Outline() const noexcept {
53 return TTF_GetFontOutline(handle);
56 void Font::Outline(int px) noexcept {
57 TTF_SetFontOutline(handle, px);
61 int Font::Hinting() const noexcept {
62 return TTF_GetFontHinting(handle);
65 void Font::Hinting(int h) const noexcept {
66 TTF_SetFontHinting(handle, h);
69 bool Font::Kerning() const noexcept {
70 return TTF_GetFontKerning(handle);
73 void Font::Kerning(bool b) noexcept {
74 TTF_SetFontKerning(handle, b);
78 int Font::Height() const noexcept {
79 return TTF_FontHeight(handle);
82 int Font::Ascent() const noexcept {
83 return TTF_FontAscent(handle);
86 int Font::Descent() const noexcept {
87 return TTF_FontDescent(handle);
90 int Font::LineSkip() const noexcept {
91 return TTF_FontLineSkip(handle);
95 const char *Font::FamilyName() const noexcept {
96 return TTF_FontFaceFamilyName(handle);
99 const char *Font::StyleName() const noexcept {
100 return TTF_FontFaceStyleName(handle);
104 bool Font::HasGlyph(Uint16 c) const noexcept {
105 return TTF_GlyphIsProvided(handle, c);
109 glm::ivec2 Font::TextSize(const char *text) const {
111 if (TTF_SizeUTF8(handle, text, &size.x, &size.y) != 0) {
112 throw TTFError("TTF_SizeUTF8");
117 Texture Font::Render(const char *text) const {
123 void Font::Render(const char *text, Texture &tex) const {
124 SDL_Surface *srf = TTF_RenderUTF8_Blended(handle, text, { 0xFF, 0xFF, 0xFF, 0xFF });
126 throw TTFError("TTF_RenderUTF8_Blended");
129 tex.Data(*srf, false);
131 SDL_FreeSurface(srf);
134 Format::Format() noexcept
136 , type(GL_UNSIGNED_INT_8_8_8_8_REV)
137 , internal(GL_RGBA8) {
138 sdl_format.format = SDL_PIXELFORMAT_ARGB8888;
139 sdl_format.palette = nullptr;
140 sdl_format.BitsPerPixel = 32;
141 sdl_format.BytesPerPixel = 4;
142 sdl_format.Rmask = 0x00FF0000;
143 sdl_format.Gmask = 0x0000FF00;
144 sdl_format.Bmask = 0x000000FF;
145 sdl_format.Amask = 0xFF000000;
146 sdl_format.Rloss = 0;
147 sdl_format.Gloss = 0;
148 sdl_format.Bloss = 0;
149 sdl_format.Aloss = 0;
150 sdl_format.Rshift = 16;
151 sdl_format.Gshift = 8;
152 sdl_format.Bshift = 0;
153 sdl_format.Ashift = 24;
154 sdl_format.refcount = 1;
155 sdl_format.next = nullptr;
158 Format::Format(const SDL_PixelFormat &fmt) noexcept
160 if (fmt.BytesPerPixel == 4) {
161 if (fmt.Amask == 0xFF) {
162 if (fmt.Rmask == 0xFF00) {
167 type = GL_UNSIGNED_INT_8_8_8_8;
169 if (fmt.Rmask == 0xFF) {
174 type = GL_UNSIGNED_INT_8_8_8_8_REV;
178 if (fmt.Rmask == 0xFF) {
183 type = GL_UNSIGNED_BYTE;
188 bool Format::Compatible(const Format &other) const noexcept {
189 return format == other.format && type == other.type && internal == other.internal;
193 template<GLenum TARGET, GLsizei COUNT>
194 TextureBase<TARGET, COUNT>::TextureBase() {
195 glGenTextures(COUNT, handle);
198 template<GLenum TARGET, GLsizei COUNT>
199 TextureBase<TARGET, COUNT>::~TextureBase() {
200 glDeleteTextures(COUNT, handle);
203 template<GLenum TARGET, GLsizei COUNT>
204 TextureBase<TARGET, COUNT>::TextureBase(TextureBase &&other) noexcept {
205 std::memcpy(handle, other.handle, sizeof(handle));
206 std::memset(other.handle, 0, sizeof(handle));
209 template<GLenum TARGET, GLsizei COUNT>
210 TextureBase<TARGET, COUNT> &TextureBase<TARGET, COUNT>::operator =(TextureBase &&other) noexcept {
211 std::swap(handle, other.handle);
223 Texture::~Texture() {
227 Texture::Texture(Texture &&other) noexcept
228 : TextureBase(std::move(other)) {
230 height = other.height;
233 Texture &Texture::operator =(Texture &&other) noexcept {
234 TextureBase::operator =(std::move(other));
236 height = other.height;
242 bool ispow2(unsigned int i) {
243 // don't care about i == 0 here
244 return !(i & (i - 1));
248 void Texture::Data(const SDL_Surface &srf, bool pad2) noexcept {
249 Format format(*srf.format);
251 if (!pad2 || (ispow2(srf.w) && ispow2(srf.h))) {
252 int align = UnpackAlignmentFromPitch(srf.pitch);
254 int pitch = (srf.w * srf.format->BytesPerPixel + align - 1) / align * align;
255 if (srf.pitch - pitch >= align) {
256 UnpackRowLength(srf.pitch / srf.format->BytesPerPixel);
261 Data(srf.w, srf.h, format, srf.pixels);
264 } else if (srf.w > (1 << 30) || srf.h > (1 << 30)) {
265 // That's at least one gigapixel in either or both dimensions.
266 // If this is not an error, that's an insanely large or high
267 // resolution texture.
269 std::cerr << "texture size exceeds 2^30, aborting data import" << std::endl;
272 GLsizei width = 1, height = 1;
273 while (width < srf.w) {
276 while (height < srf.h) {
279 size_t pitch = width * srf.format->BytesPerPixel;
280 size_t size = pitch * height;
281 size_t row_pad = pitch - srf.pitch;
282 std::unique_ptr<unsigned char[]> data(new unsigned char[size]);
283 unsigned char *src = reinterpret_cast<unsigned char *>(srf.pixels);
284 unsigned char *dst = data.get();
285 for (int row = 0; row < srf.h; ++row) {
286 std::memcpy(dst, src, srf.pitch);
289 std::memset(dst, 0, row_pad);
292 std::memset(dst, 0, (height - srf.h) * pitch);
293 UnpackAlignmentFromPitch(pitch);
294 Data(width, height, format, data.get());
300 void Texture::Data(GLsizei w, GLsizei h, const Format &format, GLvoid *data) noexcept {
306 format.format, format.type,
314 void Texture::UnpackAlignment(GLint i) noexcept {
315 glPixelStorei(GL_UNPACK_ALIGNMENT, i);
318 int Texture::UnpackAlignmentFromPitch(int pitch) noexcept {
320 while (pitch % align) {
323 UnpackAlignment(align);
327 void Texture::UnpackRowLength(GLint i) noexcept {
328 glPixelStorei(GL_UNPACK_ROW_LENGTH, i);
332 ArrayTexture::ArrayTexture()
340 ArrayTexture::~ArrayTexture() {
344 ArrayTexture::ArrayTexture(ArrayTexture &&other) noexcept
345 : TextureBase(std::move(other)) {
347 height = other.height;
351 ArrayTexture &ArrayTexture::operator =(ArrayTexture &&other) noexcept {
352 TextureBase::operator =(std::move(other));
354 height = other.height;
360 void ArrayTexture::Reserve(GLsizei w, GLsizei h, GLsizei d, const Format &f) noexcept {
362 GL_TEXTURE_2D_ARRAY, // which
364 f.internal, // format
374 void ArrayTexture::Data(GLsizei l, const SDL_Surface &srf) {
375 Format fmt(*srf.format);
376 if (format.Compatible(fmt)) {
377 Data(l, fmt, srf.pixels);
379 SDL_Surface *converted = SDL_ConvertSurface(
380 const_cast<SDL_Surface *>(&srf),
385 throw SDLError("SDL_ConvertSurface");
387 Format new_fmt(*converted->format);
388 if (!format.Compatible(new_fmt)) {
389 SDL_FreeSurface(converted);
390 throw std::runtime_error("unable to convert texture input");
392 Data(l, new_fmt, converted->pixels);
393 SDL_FreeSurface(converted);
397 void ArrayTexture::Data(GLsizei l, const Format &f, GLvoid *data) noexcept {
399 GL_TEXTURE_2D_ARRAY, // which
401 0, 0, // dest X and Y offset
416 CubeMap::~CubeMap() {
420 CubeMap::CubeMap(CubeMap &&other) noexcept
421 : TextureBase(std::move(other)) {
425 CubeMap &CubeMap::operator =(CubeMap &&other) noexcept {
426 TextureBase::operator =(std::move(other));
431 void CubeMap::Data(Face f, const SDL_Surface &srf) {
433 Format fmt(*srf.format);
434 if (format.Compatible(fmt)) {
435 Data(f, srf.w, srf.h, fmt, srf.pixels);
437 SDL_Surface *converted = SDL_ConvertSurface(
438 const_cast<SDL_Surface *>(&srf),
443 throw SDLError("SDL_ConvertSurface");
445 Format new_fmt(*converted->format);
446 if (!format.Compatible(new_fmt)) {
447 SDL_FreeSurface(converted);
448 throw std::runtime_error("unable to convert texture input");
450 Data(f, converted->w, converted->h, new_fmt, converted->pixels);
451 SDL_FreeSurface(converted);
455 void CubeMap::Data(Face face, GLsizei w, GLsizei h, const Format &f, GLvoid *data) noexcept {
459 f.internal, // internal format
462 f.format, f.type, // pixel format