Import Upstream version 1.23
[debian-dgen.git] / sdl / font.cpp
blobcc793ef583f023de4d17d4ffb4e5b9070b4f586c
1 // DGen/SDL v1.14+
2 // by Joe Groff
3 // How's my programming? E-mail <joe@pknet.com>
5 /* DGen's font renderer.
6 * I hope it's pretty well detached from the DGen core, so you can use it in
7 * any other SDL app you like. */
9 /* Also note that these font renderers do no error detection, and absolutely
10 * NO clipping whatsoever, so try to keep the glyphs on-screen. Thank you :-)
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <SDL.h>
18 #include "font.h" /* The interface functions */
20 extern int *dgen_font[];
22 // Support function
23 // Put a glyph at the specified coordinates
24 // THIS IS REALLY A MACRO - SDL_LockSurface should have been called already if
25 // necessary
26 static inline void _putglyph(char *p, int Bpp, int pitch, char which)
28 int *glyph = dgen_font[which];
29 int x = 0, i;
31 for(; *glyph != -1; ++glyph)
33 p += (((x += *glyph) >> 3) * pitch); x &= 7;
34 for(i = 0; i < Bpp; ++i) p[(x * Bpp) + i] = 0xff;
38 // This writes a string of text at the given x and y coordinates
39 void font_text(SDL_Surface *surf, int x, int y, const char *message)
41 int pitch = surf->pitch, Bpp = surf->format->BytesPerPixel;
42 char *p = (char*)surf->pixels + (pitch * y) + (Bpp * x);
44 if(SDL_MUSTLOCK(surf))
45 if(SDL_LockSurface(surf) < 0)
47 fprintf(stderr, "font: Couldn't lock screen: %s!", SDL_GetError());
48 return;
50 for(; *message; p += (8 * Bpp), ++message)
51 _putglyph(p, Bpp, pitch, *message);
52 if(SDL_MUSTLOCK(surf)) SDL_UnlockSurface(surf);
55 // This writes a string of text of fixed length n
56 void font_text_n(SDL_Surface *surf, int x, int y, const char *message, int n)
58 int pitch = surf->pitch, Bpp = surf->format->BytesPerPixel;
59 char *p = (char*)surf->pixels + (pitch * y) + (Bpp * x);
61 if(SDL_MUSTLOCK(surf))
62 if(SDL_LockSurface(surf) < 0)
64 fprintf(stderr, "font: Couldn't lock screen: %s!", SDL_GetError());
65 return;
67 for(; n > 0; p += (8 * Bpp), ++message, --n)
68 _putglyph(p, Bpp, pitch, *message);
69 if(SDL_MUSTLOCK(surf)) SDL_UnlockSurface(surf);