1 #include "ArrayTexture.hpp"
6 #include "TextureBase.hpp"
7 #include "Viewport.hpp"
9 #include "../app/error.hpp"
21 Font::Font(const char *src, int size, long index)
22 : handle(TTF_OpenFontIndex(src, size, index)) {
24 throw app::TTFError("TTF_OpenFontIndex");
30 TTF_CloseFont(handle);
34 Font::Font(Font &&other) noexcept
35 : handle(other.handle) {
36 other.handle = nullptr;
39 Font &Font::operator =(Font &&other) noexcept {
40 std::swap(handle, other.handle);
45 int Font::Style() const noexcept {
46 return TTF_GetFontStyle(handle);
49 void Font::Style(int s) const noexcept {
50 TTF_SetFontStyle(handle, s);
53 int Font::Outline() const noexcept {
54 return TTF_GetFontOutline(handle);
57 void Font::Outline(int px) noexcept {
58 TTF_SetFontOutline(handle, px);
62 int Font::Hinting() const noexcept {
63 return TTF_GetFontHinting(handle);
66 void Font::Hinting(int h) const noexcept {
67 TTF_SetFontHinting(handle, h);
70 bool Font::Kerning() const noexcept {
71 return TTF_GetFontKerning(handle);
74 void Font::Kerning(bool b) noexcept {
75 TTF_SetFontKerning(handle, b);
79 int Font::Height() const noexcept {
80 return TTF_FontHeight(handle);
83 int Font::Ascent() const noexcept {
84 return TTF_FontAscent(handle);
87 int Font::Descent() const noexcept {
88 return TTF_FontDescent(handle);
91 int Font::LineSkip() const noexcept {
92 return TTF_FontLineSkip(handle);
96 const char *Font::FamilyName() const noexcept {
97 return TTF_FontFaceFamilyName(handle);
100 const char *Font::StyleName() const noexcept {
101 return TTF_FontFaceStyleName(handle);
105 bool Font::HasGlyph(Uint16 c) const noexcept {
106 return TTF_GlyphIsProvided(handle, c);
110 glm::ivec2 Font::TextSize(const char *text) const {
112 if (TTF_SizeUTF8(handle, text, &size.x, &size.y) != 0) {
113 throw app::TTFError("TTF_SizeUTF8");
118 Texture Font::Render(const char *text) const {
124 void Font::Render(const char *text, Texture &tex) const {
125 SDL_Surface *srf = TTF_RenderUTF8_Blended(handle, text, { 0xFF, 0xFF, 0xFF, 0xFF });
127 throw app::TTFError("TTF_RenderUTF8_Blended");
130 tex.Data(*srf, false);
132 SDL_FreeSurface(srf);
135 Format::Format() noexcept
137 , type(GL_UNSIGNED_INT_8_8_8_8_REV)
138 , internal(GL_RGBA8) {
139 sdl_format.format = SDL_PIXELFORMAT_ARGB8888;
140 sdl_format.palette = nullptr;
141 sdl_format.BitsPerPixel = 32;
142 sdl_format.BytesPerPixel = 4;
143 sdl_format.Rmask = 0x00FF0000;
144 sdl_format.Gmask = 0x0000FF00;
145 sdl_format.Bmask = 0x000000FF;
146 sdl_format.Amask = 0xFF000000;
147 sdl_format.Rloss = 0;
148 sdl_format.Gloss = 0;
149 sdl_format.Bloss = 0;
150 sdl_format.Aloss = 0;
151 sdl_format.Rshift = 16;
152 sdl_format.Gshift = 8;
153 sdl_format.Bshift = 0;
154 sdl_format.Ashift = 24;
155 sdl_format.refcount = 1;
156 sdl_format.next = nullptr;
159 Format::Format(const SDL_PixelFormat &fmt) noexcept
161 if (fmt.BytesPerPixel == 4) {
162 if (fmt.Amask == 0xFF) {
163 if (fmt.Rmask == 0xFF00) {
168 type = GL_UNSIGNED_INT_8_8_8_8;
170 if (fmt.Rmask == 0xFF) {
175 type = GL_UNSIGNED_INT_8_8_8_8_REV;
179 if (fmt.Rmask == 0xFF) {
184 type = GL_UNSIGNED_BYTE;
189 bool Format::Compatible(const Format &other) const noexcept {
190 return format == other.format && type == other.type && internal == other.internal;
194 template<GLenum TARGET, GLsizei COUNT>
195 TextureBase<TARGET, COUNT>::TextureBase() {
196 glGenTextures(COUNT, handle);
199 template<GLenum TARGET, GLsizei COUNT>
200 TextureBase<TARGET, COUNT>::~TextureBase() {
201 glDeleteTextures(COUNT, handle);
204 template<GLenum TARGET, GLsizei COUNT>
205 TextureBase<TARGET, COUNT>::TextureBase(TextureBase &&other) noexcept {
206 std::memcpy(handle, other.handle, sizeof(handle));
207 std::memset(other.handle, 0, sizeof(handle));
210 template<GLenum TARGET, GLsizei COUNT>
211 TextureBase<TARGET, COUNT> &TextureBase<TARGET, COUNT>::operator =(TextureBase &&other) noexcept {
212 std::swap(handle, other.handle);
224 Texture::~Texture() {
228 Texture::Texture(Texture &&other) noexcept
229 : TextureBase(std::move(other)) {
231 height = other.height;
234 Texture &Texture::operator =(Texture &&other) noexcept {
235 TextureBase::operator =(std::move(other));
237 height = other.height;
243 bool ispow2(unsigned int i) {
244 // don't care about i == 0 here
245 return !(i & (i - 1));
249 void Texture::Data(const SDL_Surface &srf, bool pad2) noexcept {
250 Format format(*srf.format);
252 if (!pad2 || (ispow2(srf.w) && ispow2(srf.h))) {
253 int align = UnpackAlignmentFromPitch(srf.pitch);
255 int pitch = (srf.w * srf.format->BytesPerPixel + align - 1) / align * align;
256 if (srf.pitch - pitch >= align) {
257 UnpackRowLength(srf.pitch / srf.format->BytesPerPixel);
262 Data(srf.w, srf.h, format, srf.pixels);
265 } else if (srf.w > (1 << 30) || srf.h > (1 << 30)) {
266 // That's at least one gigapixel in either or both dimensions.
267 // If this is not an error, that's an insanely large or high
268 // resolution texture.
270 std::cerr << "texture size exceeds 2^30, aborting data import" << std::endl;
273 GLsizei width = 1, height = 1;
274 while (width < srf.w) {
277 while (height < srf.h) {
280 size_t pitch = width * srf.format->BytesPerPixel;
281 size_t size = pitch * height;
282 size_t row_pad = pitch - srf.pitch;
283 std::unique_ptr<unsigned char[]> data(new unsigned char[size]);
284 unsigned char *src = reinterpret_cast<unsigned char *>(srf.pixels);
285 unsigned char *dst = data.get();
286 for (int row = 0; row < srf.h; ++row) {
287 std::memcpy(dst, src, srf.pitch);
290 std::memset(dst, 0, row_pad);
293 std::memset(dst, 0, (height - srf.h) * pitch);
294 UnpackAlignmentFromPitch(pitch);
295 Data(width, height, format, data.get());
301 void Texture::Data(GLsizei w, GLsizei h, const Format &format, GLvoid *data) noexcept {
307 format.format, format.type,
315 void Texture::UnpackAlignment(GLint i) noexcept {
316 glPixelStorei(GL_UNPACK_ALIGNMENT, i);
319 int Texture::UnpackAlignmentFromPitch(int pitch) noexcept {
321 while (pitch % align) {
324 UnpackAlignment(align);
328 void Texture::UnpackRowLength(GLint i) noexcept {
329 glPixelStorei(GL_UNPACK_ROW_LENGTH, i);
333 ArrayTexture::ArrayTexture()
341 ArrayTexture::~ArrayTexture() {
345 ArrayTexture::ArrayTexture(ArrayTexture &&other) noexcept
346 : TextureBase(std::move(other)) {
348 height = other.height;
352 ArrayTexture &ArrayTexture::operator =(ArrayTexture &&other) noexcept {
353 TextureBase::operator =(std::move(other));
355 height = other.height;
361 void ArrayTexture::Reserve(GLsizei w, GLsizei h, GLsizei d, const Format &f) noexcept {
363 GL_TEXTURE_2D_ARRAY, // which
365 f.internal, // format
375 void ArrayTexture::Data(GLsizei l, const SDL_Surface &srf) {
376 Format fmt(*srf.format);
377 if (format.Compatible(fmt)) {
378 Data(l, fmt, srf.pixels);
380 SDL_Surface *converted = SDL_ConvertSurface(
381 const_cast<SDL_Surface *>(&srf),
386 throw app::SDLError("SDL_ConvertSurface");
388 Format new_fmt(*converted->format);
389 if (!format.Compatible(new_fmt)) {
390 SDL_FreeSurface(converted);
391 throw std::runtime_error("unable to convert texture input");
393 Data(l, new_fmt, converted->pixels);
394 SDL_FreeSurface(converted);
398 void ArrayTexture::Data(GLsizei l, const Format &f, GLvoid *data) noexcept {
400 GL_TEXTURE_2D_ARRAY, // which
402 0, 0, // dest X and Y offset
417 CubeMap::~CubeMap() {
421 CubeMap::CubeMap(CubeMap &&other) noexcept
422 : TextureBase(std::move(other)) {
426 CubeMap &CubeMap::operator =(CubeMap &&other) noexcept {
427 TextureBase::operator =(std::move(other));
432 void CubeMap::Data(Face f, const SDL_Surface &srf) {
434 Format fmt(*srf.format);
435 if (format.Compatible(fmt)) {
436 Data(f, srf.w, srf.h, fmt, srf.pixels);
438 SDL_Surface *converted = SDL_ConvertSurface(
439 const_cast<SDL_Surface *>(&srf),
444 throw app::SDLError("SDL_ConvertSurface");
446 Format new_fmt(*converted->format);
447 if (!format.Compatible(new_fmt)) {
448 SDL_FreeSurface(converted);
449 throw std::runtime_error("unable to convert texture input");
451 Data(f, converted->w, converted->h, new_fmt, converted->pixels);
452 SDL_FreeSurface(converted);
456 void CubeMap::Data(Face face, GLsizei w, GLsizei h, const Format &f, GLvoid *data) noexcept {
460 f.internal, // internal format
463 f.format, f.type, // pixel format