Browse Source

sdl2: add font rendering from a bitmap file

master
Lephenixnoir 4 months ago
parent
commit
b8299dbbf7
3 changed files with 69 additions and 23 deletions
  1. BIN
      font10x12.bmp
  2. BIN
      font5x7.bmp
  3. 69
    23
      src/platform/sdl2.c

BIN
font10x12.bmp View File


BIN
font5x7.bmp View File


+ 69
- 23
src/platform/sdl2.c View File

@@ -13,6 +13,31 @@
static SDL_Window *w = NULL;
static SDL_Renderer *r = NULL;

/* Font surface */
static SDL_Texture *font = NULL;

/* Font configuration */

#ifndef LARGE_FONT /* Small font */

#define FONT_FILE "font5x7.bmp"
#define FONT_WIDTH 5
#define FONT_HEIGHT 7
#define FONT_OUTER 1
#define FONT_INNER 2
#define FONT_OFFSET 0

#else /* Large font */

#define FONT_FILE "font10x12.bmp"
#define FONT_WIDTH 10
#define FONT_HEIGHT 12
#define FONT_OUTER 3
#define FONT_INNER 3
#define FONT_OFFSET 32

#endif /* Font size */

//---
// Interface functions
//---
@@ -42,8 +67,8 @@ void intf_line(int x1, int y1, int x2, int y2, int color)
/* intf_size() - Get the dimensions of a string */
void intf_size(char const *str, int *width, int *height)
{
*width = 6 * strlen(str) - 1;
*height = 7;
*width = (FONT_WIDTH + 1) * strlen(str) - 1;
*height = FONT_HEIGHT;
}

/* intf_text() - Draw variable-width text */
@@ -54,9 +79,27 @@ void intf_text(char const *str, int x, int y, int color)
int B = color & 0xff;
SDL_SetRenderDrawColor(r, R, G, B, 255);

SDL_Rect rect = { .x = x, .y = y };
intf_size(str, &rect.w, &rect.h);
SDL_RenderDrawRect(r, &rect);
int c;
while((c = *str++))
{
int row = (c - FONT_OFFSET) >> 4;
int col = (c - FONT_OFFSET) & 15;

SDL_Rect src = {
.x = FONT_OUTER + (FONT_WIDTH + FONT_INNER) * col,
.y = FONT_OUTER + (FONT_HEIGHT + FONT_INNER) * row,
.w = FONT_WIDTH,
.h = FONT_HEIGHT,
};
SDL_Rect dst = { .x = x, .y = y, .w = src.w, .h = src.h };

SDL_RenderCopy(r, font, &src, &dst);
x += FONT_WIDTH + 1;
}

// SDL_Rect rect = { .x = x, .y = y };
// intf_size(str, &rect.w, &rect.h);
// SDL_RenderDrawRect(r, &rect);
}

//---
@@ -66,32 +109,30 @@ void intf_text(char const *str, int x, int y, int color)
__attribute__((constructor))
static void init(void)
{
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
fprintf(stderr, "error: cannot initialize SDL backend: %s\n",
SDL_GetError());
exit(1);
#define fail(msg) { \
fprintf(stderr, "error: " msg ": %s\n", SDL_GetError()); \
exit(1); \
}

if(SDL_Init(SDL_INIT_VIDEO) < 0) fail("failed to initialize SDL");

int c = SDL_WINDOWPOS_CENTERED;
w = SDL_CreateWindow("2D rendering", c, c, 256, 128, 0);
if(!w)
{
fprintf(stderr, "error: cannot create SDL window: %s\n",
SDL_GetError());
exit(1);
}
if(!w) fail("cannot create SDL window");

r = SDL_CreateRenderer(w, -1, SDL_RENDERER_ACCELERATED);
if(!r)
{
fprintf(stderr, "error: cannot create SDL renderer: %s\n",
SDL_GetError());
exit(1);
}
if(!r) fail("cannot create SDL renderer");

SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");

/* Load the font */
SDL_Surface *font_surf = SDL_LoadBMP(FONT_FILE);
if(!font_surf) fail("cannot load font");

font = SDL_CreateTextureFromSurface(r, font_surf);
SDL_FreeSurface(font_surf);
if(!font) fail("cannot convert font to texture");

/* Make the window visible */
SDL_RenderPresent(r);

@@ -100,11 +141,15 @@ static void init(void)
TeX_intf_line(intf_line);
TeX_intf_size(intf_size);
TeX_intf_text(intf_text);

#undef fail
}

__attribute__((destructor))
static void fini(void)
{
if(font) SDL_DestroyTexture(font);
if(r) SDL_DestroyRenderer(r);
if(w) SDL_DestroyWindow(w);
SDL_Quit();
}
@@ -115,7 +160,8 @@ static void fini(void)

int main(void)
{
char const * formula = "\\frac{x_7}{"
char const * formula =
"\\frac{x_7\\left<X,Y\\right>+3\\left<\\frac{A}{B}\\right>}{"
"\\left\\{\\frac{\\frac{2}{3}}{27}\\right\\}^2"
"}";


Loading…
Cancel
Save