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 int Font::Style() const noexcept {
38 return TTF_GetFontStyle(handle);
41 void Font::Style(int s) const noexcept {
42 TTF_SetFontStyle(handle, s);
45 int Font::Outline() const noexcept {
46 return TTF_GetFontOutline(handle);
49 void Font::Outline(int px) noexcept {
50 TTF_SetFontOutline(handle, px);
54 int Font::Hinting() const noexcept {
55 return TTF_GetFontHinting(handle);
58 void Font::Hinting(int h) const noexcept {
59 TTF_SetFontHinting(handle, h);
62 bool Font::Kerning() const noexcept {
63 return TTF_GetFontKerning(handle);
66 void Font::Kerning(bool b) noexcept {
67 TTF_SetFontKerning(handle, b);
71 int Font::Height() const noexcept {
72 return TTF_FontHeight(handle);
75 int Font::Ascent() const noexcept {
76 return TTF_FontAscent(handle);
79 int Font::Descent() const noexcept {
80 return TTF_FontDescent(handle);
83 int Font::LineSkip() const noexcept {
84 return TTF_FontLineSkip(handle);
88 const char *Font::FamilyName() const noexcept {
89 return TTF_FontFaceFamilyName(handle);
92 const char *Font::StyleName() const noexcept {
93 return TTF_FontFaceStyleName(handle);
97 bool Font::HasGlyph(Uint16 c) const noexcept {
98 return TTF_GlyphIsProvided(handle, c);
102 glm::tvec2<int> Font::TextSize(const char *text) const {
103 glm::tvec2<int> size;
104 if (TTF_SizeUTF8(handle, text, &size.x, &size.y) != 0) {
105 throw std::runtime_error(TTF_GetError());
110 Texture Font::Render(const char *text, SDL_Color color) const {
115 void Font::Render(const char *text, SDL_Color color, Texture &tex) const {
116 SDL_Surface *srf = TTF_RenderUTF8_Blended(handle, text, color);
118 throw std::runtime_error(TTF_GetError());
121 tex.Data(*srf, false);
123 SDL_FreeSurface(srf);
127 void Format::ReadPixelFormat(const SDL_PixelFormat &fmt) {
128 if (fmt.BytesPerPixel == 4) {
129 if (fmt.Amask == 0xFF) {
130 if (fmt.Rmask == 0xFF00) {
135 type = GL_UNSIGNED_INT_8_8_8_8;
137 if (fmt.Rmask == 0xFF) {
142 type = GL_UNSIGNED_INT_8_8_8_8_REV;
146 if (fmt.Rmask == 0xFF) {
151 type = GL_UNSIGNED_BYTE;
161 glGenTextures(1, &handle);
164 Texture::~Texture() {
166 glDeleteTextures(1, &handle);
170 Texture::Texture(Texture &&other) noexcept
171 : handle(other.handle) {
174 height = other.height;
177 Texture &Texture::operator =(Texture &&other) noexcept {
178 std::swap(handle, other.handle);
180 height = other.height;
185 void Texture::Bind() noexcept {
186 glBindTexture(GL_TEXTURE_2D, handle);
190 bool ispow2(unsigned int i) {
191 // don't care about i == 0 here
192 return !(i & (i - 1));
196 void Texture::Data(const SDL_Surface &srf, bool pad2) noexcept {
198 format.ReadPixelFormat(*srf.format);
200 if (!pad2 || (ispow2(srf.w) && ispow2(srf.h))) {
201 int align = UnpackAlignmentFromPitch(srf.pitch);
203 int pitch = (srf.w * srf.format->BytesPerPixel + align - 1) / align * align;
204 if (srf.pitch - pitch >= align) {
205 UnpackRowLength(srf.pitch / srf.format->BytesPerPixel);
210 Data(srf.w, srf.h, format, srf.pixels);
213 } else if (srf.w > (1 << 30) || srf.h > (1 << 30)) {
215 throw std::runtime_error("texture too large");
218 GLsizei width = 1, height = 1;
219 while (width < srf.w) {
222 while (height < srf.h) {
225 size_t pitch = width * srf.format->BytesPerPixel;
226 size_t size = pitch * height;
227 size_t row_pad = pitch - srf.pitch;
228 std::unique_ptr<unsigned char[]> data(new unsigned char[size]);
229 unsigned char *src = reinterpret_cast<unsigned char *>(srf.pixels);
230 unsigned char *dst = data.get();
231 for (int row = 0; row < srf.h; ++row) {
232 std::memcpy(dst, src, srf.pitch);
235 std::memset(dst, 0, row_pad);
238 std::memset(dst, 0, (height - srf.h) * pitch);
239 UnpackAlignmentFromPitch(pitch);
240 Data(width, height, format, data.get());
246 void Texture::Data(GLsizei w, GLsizei h, const Format &format, GLvoid *data) noexcept {
252 format.format, format.type,
260 void Texture::FilterNearest() noexcept {
261 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
262 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
265 void Texture::FilterLinear() noexcept {
266 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
267 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
270 void Texture::FilterTrilinear() noexcept {
271 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
272 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
273 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
274 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
275 glGenerateMipmap(GL_TEXTURE_2D);
279 void Texture::UnpackAlignment(GLint i) noexcept {
280 glPixelStorei(GL_UNPACK_ALIGNMENT, i);
283 int Texture::UnpackAlignmentFromPitch(int pitch) noexcept {
285 while (pitch % align) {
288 UnpackAlignment(align);
292 void Texture::UnpackRowLength(GLint i) noexcept {
293 glPixelStorei(GL_UNPACK_ROW_LENGTH, i);