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 :-)
18 #include "font.h" /* The interface functions */
20 extern int *dgen_font
[];
23 // Put a glyph at the specified coordinates
24 // THIS IS REALLY A MACRO - SDL_LockSurface should have been called already if
26 static inline void _putglyph(char *p
, int Bpp
, int pitch
, char which
)
28 int *glyph
= dgen_font
[which
];
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());
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());
67 for(; n
> 0; p
+= (8 * Bpp
), ++message
, --n
)
68 _putglyph(p
, Bpp
, pitch
, *message
);
69 if(SDL_MUSTLOCK(surf
)) SDL_UnlockSurface(surf
);