Remove SDL2 dependency from Font.cpp

This commit is contained in:
Clownacy 2019-04-21 21:28:18 +01:00
parent 83d444c8d4
commit f25df2ce07
3 changed files with 17 additions and 23 deletions

View file

@ -15,9 +15,7 @@
#undef RECT #undef RECT
#endif #endif
#include <SDL_render.h> #include "SDL.h"
#include <SDL_rwops.h>
#include <SDL_timer.h>
#include "WindowsWrapper.h" #include "WindowsWrapper.h"
@ -612,7 +610,7 @@ void PutText(int x, int y, const char *text, uint32_t color)
SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormat(0, surface_width, surface_height, 0, SDL_PIXELFORMAT_RGB24); SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormat(0, surface_width, surface_height, 0, SDL_PIXELFORMAT_RGB24);
SDL_RenderReadPixels(gRenderer, NULL, SDL_PIXELFORMAT_RGB24, surface->pixels, surface->pitch); SDL_RenderReadPixels(gRenderer, NULL, SDL_PIXELFORMAT_RGB24, surface->pixels, surface->pitch);
DrawText(gFont, surface, x * magnification, y * magnification, color, text, strlen(text)); DrawText(gFont, (unsigned char*)surface->pixels, surface->pitch, surface->w, surface->h, x * magnification, y * magnification, color, text, strlen(text));
SDL_Texture *screen_texture = SDL_CreateTextureFromSurface(gRenderer, surface); SDL_Texture *screen_texture = SDL_CreateTextureFromSurface(gRenderer, surface);
SDL_FreeSurface(surface); SDL_FreeSurface(surface);
@ -622,7 +620,7 @@ void PutText(int x, int y, const char *text, uint32_t color)
void PutText2(int x, int y, const char *text, uint32_t color, Surface_Ids surf_no) void PutText2(int x, int y, const char *text, uint32_t color, Surface_Ids surf_no)
{ {
DrawText(gFont, surf[surf_no].surface, x * magnification, y * magnification, color, text, strlen(text)); DrawText(gFont, (unsigned char*)surf[surf_no].surface->pixels, surf[surf_no].surface->pitch, surf[surf_no].surface->w, surf[surf_no].surface->h, x * magnification, y * magnification, color, text, strlen(text));
surf[surf_no].needs_updating = true; surf[surf_no].needs_updating = true;
} }

View file

@ -11,8 +11,6 @@
#include FT_LCD_FILTER_H #include FT_LCD_FILTER_H
#include FT_BITMAP_H #include FT_BITMAP_H
#include "SDL.h"
#include "File.h" #include "File.h"
// Uncomment for that authentic pre-Windows Vista feel // Uncomment for that authentic pre-Windows Vista feel
@ -1765,7 +1763,7 @@ FontObject* LoadFont(const char *font_filename, unsigned int cell_width, unsigne
return font_object; return font_object;
} }
void DrawText(FontObject *font_object, SDL_Surface *surface, int x, int y, unsigned long colour, const char *string, size_t string_length) void DrawText(FontObject *font_object, unsigned char *bitmap_buffer, size_t bitmap_pitch, int bitmap_width, int bitmap_height, int x, int y, unsigned long colour, const char *string, size_t string_length)
{ {
if (font_object != NULL) if (font_object != NULL)
{ {
@ -1806,20 +1804,20 @@ void DrawText(FontObject *font_object, SDL_Surface *surface, int x, int y, unsig
switch (face->glyph->bitmap.pixel_mode) switch (face->glyph->bitmap.pixel_mode)
{ {
case FT_PIXEL_MODE_LCD: case FT_PIXEL_MODE_LCD:
for (int iy = MAX(-letter_y, 0); letter_y + iy < MIN(letter_y + (int)converted.rows, surface->h); ++iy) for (int iy = MAX(-letter_y, 0); letter_y + iy < MIN(letter_y + (int)converted.rows, bitmap_height); ++iy)
{ {
for (int ix = MAX(-letter_x, 0); letter_x + ix < MIN(letter_x + (int)converted.width / 3, surface->w); ++ix) for (int ix = MAX(-letter_x, 0); letter_x + ix < MIN(letter_x + (int)converted.width / 3, bitmap_width); ++ix)
{ {
const unsigned char *font_pixel = converted.buffer + iy * converted.pitch + ix * 3; const unsigned char *font_pixel = converted.buffer + iy * converted.pitch + ix * 3;
if (font_pixel[0] || font_pixel[1] || font_pixel[2]) if (font_pixel[0] || font_pixel[1] || font_pixel[2])
{ {
unsigned char *surface_pixel = (unsigned char*)surface->pixels + (letter_y + iy) * surface->pitch + (letter_x + ix) * 3; unsigned char *bitmap_pixel = bitmap_buffer + (letter_y + iy) * bitmap_pitch + (letter_x + ix) * 3;
for (unsigned int j = 0; j < 3; ++j) for (unsigned int j = 0; j < 3; ++j)
{ {
const double alpha = pow((font_pixel[j] / 255.0), 1.0 / 1.8); // Gamma correction const double alpha = pow((font_pixel[j] / 255.0), 1.0 / 1.8); // Gamma correction
surface_pixel[j] = (unsigned char)((colours[j] * alpha) + (surface_pixel[j] * (1.0 - alpha))); // Alpha blending bitmap_pixel[j] = (unsigned char)((colours[j] * alpha) + (bitmap_pixel[j] * (1.0 - alpha))); // Alpha blending
} }
} }
} }
@ -1828,9 +1826,9 @@ void DrawText(FontObject *font_object, SDL_Surface *surface, int x, int y, unsig
break; break;
case FT_PIXEL_MODE_GRAY: case FT_PIXEL_MODE_GRAY:
for (int iy = MAX(-letter_y, 0); letter_y + iy < MIN(letter_y + (int)converted.rows, surface->h); ++iy) for (int iy = MAX(-letter_y, 0); letter_y + iy < MIN(letter_y + (int)converted.rows, bitmap_height); ++iy)
{ {
for (int ix = MAX(-letter_x, 0); letter_x + ix < MIN(letter_x + (int)converted.width, surface->w); ++ix) for (int ix = MAX(-letter_x, 0); letter_x + ix < MIN(letter_x + (int)converted.width, bitmap_width); ++ix)
{ {
const unsigned char font_pixel = converted.buffer[iy * converted.pitch + ix]; const unsigned char font_pixel = converted.buffer[iy * converted.pitch + ix];
@ -1838,10 +1836,10 @@ void DrawText(FontObject *font_object, SDL_Surface *surface, int x, int y, unsig
{ {
const double alpha = pow((double)font_pixel / (converted.num_grays - 1), 1.0 / 1.8); // Gamma-corrected const double alpha = pow((double)font_pixel / (converted.num_grays - 1), 1.0 / 1.8); // Gamma-corrected
unsigned char *surface_pixel = (unsigned char*)surface->pixels + (letter_y + iy) * surface->pitch + (letter_x + ix) * 3; unsigned char *bitmap_pixel = bitmap_buffer + (letter_y + iy) * bitmap_pitch + (letter_x + ix) * 3;
for (unsigned int j = 0; j < 3; ++j) for (unsigned int j = 0; j < 3; ++j)
surface_pixel[j] = (unsigned char)((colours[j] * alpha) + (surface_pixel[j] * (1.0 - alpha))); // Alpha blending bitmap_pixel[j] = (unsigned char)((colours[j] * alpha) + (bitmap_pixel[j] * (1.0 - alpha))); // Alpha blending
} }
} }
} }
@ -1849,16 +1847,16 @@ void DrawText(FontObject *font_object, SDL_Surface *surface, int x, int y, unsig
break; break;
case FT_PIXEL_MODE_MONO: case FT_PIXEL_MODE_MONO:
for (int iy = MAX(-letter_y, 0); letter_y + iy < MIN(letter_y + (int)converted.rows, surface->h); ++iy) for (int iy = MAX(-letter_y, 0); letter_y + iy < MIN(letter_y + (int)converted.rows, bitmap_height); ++iy)
{ {
for (int ix = MAX(-letter_x, 0); letter_x + ix < MIN(letter_x + (int)converted.width, surface->w); ++ix) for (int ix = MAX(-letter_x, 0); letter_x + ix < MIN(letter_x + (int)converted.width, bitmap_width); ++ix)
{ {
if (converted.buffer[iy * converted.pitch + ix]) if (converted.buffer[iy * converted.pitch + ix])
{ {
unsigned char *surface_pixel = (unsigned char*)surface->pixels + (letter_y + iy) * surface->pitch + (letter_x + ix) * 3; unsigned char *bitmap_pixel = bitmap_buffer + (letter_y + iy) * bitmap_pitch + (letter_x + ix) * 3;
for (unsigned int j = 0; j < 3; ++j) for (unsigned int j = 0; j < 3; ++j)
surface_pixel[j] = colours[j]; bitmap_pixel[j] = colours[j];
} }
} }
} }

View file

@ -2,11 +2,9 @@
#include <stddef.h> #include <stddef.h>
#include "SDL.h"
typedef struct FontObject FontObject; typedef struct FontObject FontObject;
FontObject* LoadFontFromData(const unsigned char *data, size_t data_size, unsigned int cell_width, unsigned int cell_height); FontObject* LoadFontFromData(const unsigned char *data, size_t data_size, unsigned int cell_width, unsigned int cell_height);
FontObject* LoadFont(const char *font_filename, unsigned int cell_width, unsigned int cell_height); FontObject* LoadFont(const char *font_filename, unsigned int cell_width, unsigned int cell_height);
void DrawText(FontObject *font_object, SDL_Surface *surface, int x, int y, unsigned long colour, const char *string, size_t string_length); void DrawText(FontObject *font_object, unsigned char *bitmap_buffer, size_t bitmap_pitch, int bitmap_width, int bitmap_height, int x, int y, unsigned long colour, const char *string, size_t string_length);
void UnloadFont(FontObject *font_object); void UnloadFont(FontObject *font_object);