13 Font::Font(const char *src, int size, long index)
14 : handle(TTF_OpenFontIndex(src, size, index)) {
16 throw std::runtime_error(TTF_GetError());
22 TTF_CloseFont(handle);
26 Font::Font(Font &&other) noexcept
27 : handle(other.handle) {
28 other.handle = nullptr;
31 Font &Font::operator =(Font &&other) noexcept {
32 std::swap(handle, other.handle);
37 bool Font::Kerning() const noexcept {
38 return TTF_GetFontKerning(handle);
41 void Font::Kerning(bool b) noexcept {
42 TTF_SetFontKerning(handle, b);
46 int Font::Height() const noexcept {
47 return TTF_FontHeight(handle);
50 int Font::Ascent() const noexcept {
51 return TTF_FontAscent(handle);
54 int Font::Descent() const noexcept {
55 return TTF_FontDescent(handle);
58 int Font::LineSkip() const noexcept {
59 return TTF_FontLineSkip(handle);
63 bool Font::HasGlyph(Uint16 c) const noexcept {
64 return TTF_GlyphIsProvided(handle, c);
68 glm::tvec2<int> Font::TextSize(const char *text) const {
70 if (TTF_SizeUTF8(handle, text, &size.x, &size.y) != 0) {
71 throw std::runtime_error(TTF_GetError());
76 Texture Font::Render(const char *text, SDL_Color color) const {
77 SDL_Surface *srf = TTF_RenderUTF8_Blended(handle, text, color);
79 throw std::runtime_error(TTF_GetError());
83 tex.Data(*srf, false);
90 void Format::ReadPixelFormat(const SDL_PixelFormat &fmt) {
91 if (fmt.BytesPerPixel == 4) {
92 if (fmt.Amask == 0xFF) {
93 if (fmt.Rmask == 0xFF00) {
98 type = GL_UNSIGNED_INT_8_8_8_8;
100 if (fmt.Rmask == 0xFF) {
105 type = GL_UNSIGNED_INT_8_8_8_8_REV;
109 if (fmt.Rmask == 0xFF) {
114 type = GL_UNSIGNED_BYTE;
124 glGenTextures(1, &handle);
127 Texture::~Texture() {
129 glDeleteTextures(1, &handle);
133 Texture::Texture(Texture &&other) noexcept
134 : handle(other.handle) {
137 height = other.height;
140 Texture &Texture::operator =(Texture &&other) noexcept {
141 std::swap(handle, other.handle);
143 height = other.height;
148 void Texture::Bind() noexcept {
149 glBindTexture(GL_TEXTURE_2D, handle);
153 bool ispow2(unsigned int i) {
154 // don't care about i == 0 here
155 return !(i & (i - 1));
159 void Texture::Data(const SDL_Surface &srf, bool pad2) noexcept {
161 format.ReadPixelFormat(*srf.format);
163 if (!pad2 || (ispow2(srf.w) && ispow2(srf.h))) {
164 int align = UnpackAlignmentFromPitch(srf.pitch);
166 int pitch = (srf.w * srf.format->BytesPerPixel + align - 1) / align * align;
167 if (srf.pitch - pitch >= align) {
168 UnpackRowLength(srf.pitch / srf.format->BytesPerPixel);
173 Data(srf.w, srf.h, format, srf.pixels);
176 } else if (srf.w > (1 << 30) || srf.h > (1 << 30)) {
178 throw std::runtime_error("texture too large");
181 GLsizei width = 1, height = 1;
182 while (width < srf.w) {
185 while (height < srf.h) {
188 size_t pitch = width * srf.format->BytesPerPixel;
189 size_t size = pitch * height;
190 size_t row_pad = pitch - srf.pitch;
191 std::unique_ptr<unsigned char[]> data(new unsigned char[size]);
192 unsigned char *src = reinterpret_cast<unsigned char *>(srf.pixels);
193 unsigned char *dst = data.get();
194 for (int row = 0; row < srf.h; ++row) {
195 std::memcpy(dst, src, srf.pitch);
198 std::memset(dst, 0, row_pad);
201 std::memset(dst, 0, (height - srf.h) * pitch);
202 UnpackAlignmentFromPitch(pitch);
203 Data(width, height, format, data.get());
209 void Texture::Data(GLsizei w, GLsizei h, const Format &format, GLvoid *data) noexcept {
215 format.format, format.type,
223 void Texture::FilterNearest() noexcept {
224 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
225 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
228 void Texture::FilterLinear() noexcept {
229 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
230 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
233 void Texture::FilterTrilinear() noexcept {
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
235 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
236 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
237 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
238 glGenerateMipmap(GL_TEXTURE_2D);
242 void Texture::UnpackAlignment(GLint i) noexcept {
243 glPixelStorei(GL_UNPACK_ALIGNMENT, i);
246 int Texture::UnpackAlignmentFromPitch(int pitch) noexcept {
248 while (pitch % align) {
251 UnpackAlignment(align);
255 void Texture::UnpackRowLength(GLint i) noexcept {
256 glPixelStorei(GL_UNPACK_ROW_LENGTH, i);