+++ /dev/null
-/*
- * Copyright (C) Karl Bartel <karlb@gmx.net> WWW: http://www.linux-games.com
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
-
-/*Modified 2003, 2006 by Andre Noll */
-
-#include <SDL/SDL.h>
-#include "SFont.h"
-#include <stdlib.h> /* exit */
-#include <string.h> /* strlen */
-SFont_FontInfo InternalFont;
-
-static Uint32 GetPixel(SDL_Surface *Surface, Sint32 X, Sint32 Y)
-{
-
- Uint8 *bits;
- Uint32 Bpp;
-
- if (X < 0)
- puts("SFONT ERROR: x too small in GetPixel. Report this "
- "to <karlb@gmx.net>");
- if (X >= Surface->w)
- puts("SFONT ERROR: x too big in GetPixel. Report this to "
- "<karlb@gmx.net>");
- Bpp = Surface->format->BytesPerPixel;
- bits = ((Uint8 *) Surface->pixels) + Y * Surface->pitch + X * Bpp;
-
- /* Get the pixel */
- switch (Bpp) {
- case 1:
- return *((Uint8 *) Surface->pixels + Y * Surface->pitch + X);
- break;
- case 2:
- return *((Uint16 *) Surface->pixels + Y * Surface->pitch / 2 + X);
- break;
- case 3:{ /* Format/endian independent */
- Uint8 r, g, b;
- r = *((bits) + Surface->format->Rshift / 8);
- g = *((bits) + Surface->format->Gshift / 8);
- b = *((bits) + Surface->format->Bshift / 8);
- return SDL_MapRGB(Surface->format, r, g, b);
- }
- break;
- case 4:
- return *((Uint32 *) Surface->pixels + Y * Surface->pitch / 4 + X);
- break;
- }
-
- return -1;
-}
-
-void InitFont2(SFont_FontInfo * Font)
-{
- int x = 0, i = 0;
-
- if (!Font->Surface) {
- printf("The font has not been loaded!\n");
- exit(EXIT_FAILURE);
- }
-
- if (SDL_MUSTLOCK(Font->Surface))
- SDL_LockSurface(Font->Surface);
-
- while (x < Font->Surface->w) {
- if (GetPixel(Font->Surface, x, 0) ==
- SDL_MapRGB(Font->Surface->format, 255, 0, 255)) {
- Font->CharPos[i++] = x;
- while ((x < Font->Surface->w - 1) &&
- (GetPixel(Font->Surface, x, 0) ==
- SDL_MapRGB(Font->Surface->format, 255, 0, 255)))
- x++;
- Font->CharPos[i++] = x;
- }
- x++;
- }
- if (SDL_MUSTLOCK(Font->Surface))
- SDL_UnlockSurface(Font->Surface);
-
- Font->h = Font->Surface->h;
- SDL_SetColorKey(Font->Surface, SDL_SRCCOLORKEY,
- GetPixel(Font->Surface, 0, Font->Surface->h - 1));
-}
-
-void PutString2(SDL_Surface * Surface, SFont_FontInfo * Font, int x, int y,
- const char *text)
-{
- int ofs;
- int i = 0;
- SDL_Rect srcrect, dstrect;
-
- while (text[i] != '\0') {
- if (text[i] == ' ') {
- x += Font->CharPos[2] - Font->CharPos[1];
- i++;
- } else {
- ofs = ((unsigned char) text[i] - 33) * 2 + 1;
- srcrect.w = dstrect.w = (Font->CharPos[ofs + 2]
- + Font->CharPos[ofs + 1]) / 2
- - (Font->CharPos[ofs]
- + Font->CharPos[ofs - 1]) / 2;
- srcrect.h = dstrect.h = Font->Surface->h - 1;
- srcrect.x = (Font->CharPos[ofs]
- + Font->CharPos[ofs - 1]) / 2;
- srcrect.y = 1;
- dstrect.x = x - (float) (Font->CharPos[ofs]
- - Font->CharPos[ofs - 1]) / 2;
- dstrect.y = y;
- SDL_BlitSurface(Font->Surface, &srcrect, Surface,
- &dstrect);
- x += Font->CharPos[ofs + 1] - Font->CharPos[ofs];
- i++;
- }
- }
-}
-
-int TextWidth2(SFont_FontInfo * Font, char *text)
-{
- int ofs = 0;
- int i = 0, x = 0;
-
- while (text[i] != '\0') {
- if (text[i] == ' ') {
- x += Font->CharPos[2] - Font->CharPos[1];
- i++;
- } else {
- ofs = ((unsigned char) text[i] - 33) * 2 + 1;
- x += Font->CharPos[ofs + 1] - Font->CharPos[ofs];
- i++;
- }
- }
- return x;
-}
-
-static void SFont_InternalInput(SDL_Surface * Dest, SFont_FontInfo * Font, int x,
- int y, int PixelWidth, char *text)
-{
- SDL_Event event;
- int ch = -1, blink = 0;
- long blinktimer = 0;
- SDL_Surface *Back;
- SDL_Rect rect;
- int previous;
-
- Back = SDL_AllocSurface(Dest->flags,
- Dest->w,
- Font->h,
- Dest->format->BitsPerPixel,
- Dest->format->Rmask,
- Dest->format->Gmask, Dest->format->Bmask, 0);
- rect.x = 0;
- rect.y = y;
- rect.w = Dest->w;
- rect.h = Font->Surface->h;
- SDL_BlitSurface(Dest, &rect, Back, NULL);
- PutString2(Dest, Font, x, y, text);
- SDL_UpdateRects(Dest, 1, &rect);
-
- /* start input */
- previous = SDL_EnableUNICODE(1);
- blinktimer = SDL_GetTicks();
- while (ch != SDLK_RETURN) {
- if (event.type == SDL_KEYDOWN) {
- ch = event.key.keysym.unicode;
- if (((ch > 31) || (ch == '\b')) && (ch < 128)) {
- if ((ch == '\b') && (strlen(text) > 0))
- text[strlen(text) - 1] = '\0';
- else if (ch != '\b')
- sprintf(text, "%s%c", text, ch);
- if (TextWidth2(Font, text) > PixelWidth)
- text[strlen(text) - 1] = '\0';
- SDL_BlitSurface(Back, NULL, Dest, &rect);
- PutString2(Dest, Font, x, y, text);
- SDL_UpdateRects(Dest, 1, &rect);
- SDL_WaitEvent(&event);
- }
- }
- if (SDL_GetTicks() > blinktimer) {
- blink = 1 - blink;
- blinktimer = SDL_GetTicks() + 500;
- if (blink) {
- PutString2(Dest, Font,
- x + TextWidth2(Font, text), y, "|");
- SDL_UpdateRects(Dest, 1, &rect);
- } else {
- SDL_BlitSurface(Back, NULL, Dest, &rect);
- PutString2(Dest, Font, x, y, text);
- SDL_UpdateRects(Dest, 1, &rect);
- }
- }
- SDL_Delay(1);
- SDL_PollEvent(&event);
- }
- text[strlen(text)] = '\0';
- SDL_FreeSurface(Back);
- /* restore the previous state */
- SDL_EnableUNICODE(previous);
-}
-
-void SFont_Input2(SDL_Surface * Dest, SFont_FontInfo * Font, int x, int y,
- int PixelWidth, char *text)
-{
- SFont_InternalInput(Dest, Font, x, y, PixelWidth, text);
-}
+++ /dev/null
-/************************************************************************
-* SFONT - SDL Font Library by Karl Bartel <karlb@gmx.net> *
-* *
-* All functions are explained below. There are two versions of each *
-* funtction. The first is the normal one, the function with the *
-* 2 at the end can be used when you want to handle more than one font *
-* in your program. *
-* *
-************************************************************************/
-
-#ifndef SFONT_H
-#define SFONT_H
-
-#include <SDL/SDL.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-// Delcare one variable of this type for each font you are using.
-// To load the fonts, load the font image into YourFont->Surface
-// and call InitFont( YourFont );
-typedef struct {
- SDL_Surface *Surface;
- int CharPos[512];
- int h;
-} SFont_FontInfo;
-
-// Initializes the font
-// Font: this contains the suface with the font.
-// The font must be loaded before using this function.
-void InitFont (SDL_Surface *Font);
-void InitFont2(SFont_FontInfo *Font);
-
-// Blits a string to a surface
-// Destination: the suface you want to blit to
-// text: a string containing the text you want to blit.
-void PutString (SDL_Surface *Surface, int x, int y, char *text);
-void PutString2(SDL_Surface *Surface, SFont_FontInfo *Font, int x, int y, const char *text);
-
-// Returns the width of "text" in pixels
-int TextWidth(char *text);
-int TextWidth2(SFont_FontInfo *Font, char *text);
-
-// Blits a string to with centered x position
-void XCenteredString (SDL_Surface *Surface, int y, char *text);
-void XCenteredString2(SDL_Surface *Surface, SFont_FontInfo *Font, int y, char *text);
-
-// Allows the user to enter text
-// Width: What is the maximum width of the text (in pixels)
-// text: This string contains the text which was entered by the user
-void SFont_Input ( SDL_Surface *Destination, int x, int y, int Width, char *text);
-void SFont_Input2( SDL_Surface *Destination, SFont_FontInfo *Font, int x, int y, int Width, char *text);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* SFONT_H */