]> git.localhorst.tv Git - blank.git/blobdiff - src/graphics/render.cpp
move common exceptions to app/error
[blank.git] / src / graphics / render.cpp
index fdf751e4248390e6f2b37a80a9707b139953e5cc..f0ead9ba78073f602d40bac8ffb6da04575ecf05 100644 (file)
@@ -6,10 +6,11 @@
 #include "TextureBase.hpp"
 #include "Viewport.hpp"
 
-#include "../app/init.hpp"
+#include "../app/error.hpp"
 
 #include <algorithm>
 #include <cstring>
+#include <iostream>
 #include <memory>
 #include <stdexcept>
 
@@ -19,7 +20,7 @@ namespace blank {
 Font::Font(const char *src, int size, long index)
 : handle(TTF_OpenFontIndex(src, size, index)) {
        if (!handle) {
-               throw std::runtime_error(TTF_GetError());
+               throw TTFError("TTF_OpenFontIndex");
        }
 }
 
@@ -108,7 +109,7 @@ bool Font::HasGlyph(Uint16 c) const noexcept {
 glm::ivec2 Font::TextSize(const char *text) const {
        glm::ivec2 size;
        if (TTF_SizeUTF8(handle, text, &size.x, &size.y) != 0) {
-               throw std::runtime_error(TTF_GetError());
+               throw TTFError("TTF_SizeUTF8");
        }
        return size;
 }
@@ -122,7 +123,7 @@ Texture Font::Render(const char *text) const {
 void Font::Render(const char *text, Texture &tex) const {
        SDL_Surface *srf = TTF_RenderUTF8_Blended(handle, text, { 0xFF, 0xFF, 0xFF, 0xFF });
        if (!srf) {
-               throw std::runtime_error(TTF_GetError());
+               throw TTFError("TTF_RenderUTF8_Blended");
        }
        tex.Bind();
        tex.Data(*srf, false);
@@ -261,8 +262,11 @@ void Texture::Data(const SDL_Surface &srf, bool pad2) noexcept {
 
                UnpackRowLength(0);
        } else if (srf.w > (1 << 30) || srf.h > (1 << 30)) {
+               // That's at least one gigapixel in either or both dimensions.
+               // If this is not an error, that's an insanely large or high
+               // resolution texture.
 #ifndef NDEBUG
-               throw std::runtime_error("texture too large");
+               std::cerr << "texture size exceeds 2^30, aborting data import" << std::endl;
 #endif
        } else {
                GLsizei width = 1, height = 1;
@@ -424,8 +428,28 @@ CubeMap &CubeMap::operator =(CubeMap &&other) noexcept {
 }
 
 
-void CubeMap::Data(Face f, const SDL_Surface &srf) noexcept {
-       Data(f, srf.w, srf.h, Format(*srf.format), srf.pixels);
+void CubeMap::Data(Face f, const SDL_Surface &srf) {
+       Format format;
+       Format fmt(*srf.format);
+       if (format.Compatible(fmt)) {
+               Data(f, srf.w, srf.h, fmt, srf.pixels);
+       } else {
+               SDL_Surface *converted = SDL_ConvertSurface(
+                       const_cast<SDL_Surface *>(&srf),
+                       &format.sdl_format,
+                       0
+               );
+               if (!converted) {
+                       throw SDLError("SDL_ConvertSurface");
+               }
+               Format new_fmt(*converted->format);
+               if (!format.Compatible(new_fmt)) {
+                       SDL_FreeSurface(converted);
+                       throw std::runtime_error("unable to convert texture input");
+               }
+               Data(f, converted->w, converted->h, new_fmt, converted->pixels);
+               SDL_FreeSurface(converted);
+       }
 }
 
 void CubeMap::Data(Face face, GLsizei w, GLsizei h, const Format &f, GLvoid *data) noexcept {