SFont now uses T_GFX2_Surface instead of SDL_Surface
This commit is contained in:
135
src/SFont.c
135
src/SFont.c
@@ -3,100 +3,64 @@
|
||||
/* SFont: a simple font-library that uses special .pngs as fonts
|
||||
Copyright (C) 2003 Karl Bartel
|
||||
|
||||
GrafX2 Modification
|
||||
Copyright (c) 2018 Thomas Bernard
|
||||
|
||||
License: GPL or LGPL (at your choice)
|
||||
WWW: http://www.linux-games.com/sfont/
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Karl Bartel
|
||||
Cecilienstr. 14
|
||||
Cecilienstr. 14
|
||||
12307 Berlin
|
||||
GERMANY
|
||||
karlb@gmx.net
|
||||
*/
|
||||
#if defined(USE_SDL) || defined(USE_SDL2)
|
||||
#include <SDL.h>
|
||||
#include <SDL_video.h>
|
||||
karlb@gmx.net
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "SFont.h"
|
||||
|
||||
static Uint32 GetPixel(SDL_Surface *Surface, Sint32 X, Sint32 Y)
|
||||
{
|
||||
Uint8 *bits;
|
||||
Uint32 Bpp;
|
||||
|
||||
assert(X>=0);
|
||||
assert(X<Surface->w);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
SFont_Font* SFont_InitFont(SDL_Surface* Surface)
|
||||
SFont_Font* SFont_InitFont(T_GFX2_Surface * Surface)
|
||||
{
|
||||
int x = 0, i = 33;
|
||||
Uint32 pixel;
|
||||
byte pixel;
|
||||
SFont_Font* Font;
|
||||
Uint32 pink;
|
||||
byte pink;
|
||||
|
||||
if (Surface == NULL)
|
||||
return NULL;
|
||||
|
||||
Font = (SFont_Font *) malloc(sizeof(SFont_Font));
|
||||
memset(Font, 0, sizeof(SFont_Font));
|
||||
|
||||
|
||||
Font->Surface = Surface;
|
||||
|
||||
SDL_LockSurface(Surface);
|
||||
|
||||
pink = GetPixel(Surface, 0, 0);
|
||||
pink = Get_GFX2_Surface_pixel(Surface, 0, 0);
|
||||
while (x < Surface->w) {
|
||||
if (GetPixel(Surface, x, 0) != pink) {
|
||||
if (Get_GFX2_Surface_pixel(Surface, x, 0) != pink) {
|
||||
Font->CharBegin[i]=x;
|
||||
while((x < Surface->w) && (GetPixel(Surface, x, 0)!= pink))
|
||||
while((x < Surface->w) && (Get_GFX2_Surface_pixel(Surface, x, 0)!= pink))
|
||||
x++;
|
||||
Font->CharWidth[i]=x-Font->CharBegin[i];
|
||||
i++;
|
||||
}
|
||||
x++;
|
||||
}
|
||||
|
||||
|
||||
// Create lowercase characters, if not present
|
||||
for (i=0; i <26; i++)
|
||||
{
|
||||
@@ -106,8 +70,8 @@ SFont_Font* SFont_InitFont(SDL_Surface* Surface)
|
||||
Font->CharWidth['a'+i]=Font->CharWidth['A'+i];
|
||||
}
|
||||
}
|
||||
|
||||
// Determine space width.
|
||||
|
||||
// Determine space width.
|
||||
// This strange format doesn't allow font designer to write explicit
|
||||
// space as a character.
|
||||
// Rule: A space should be as large as the character " if available,
|
||||
@@ -115,11 +79,8 @@ SFont_Font* SFont_InitFont(SDL_Surface* Surface)
|
||||
Font->Space = Font->CharWidth[(int)'"'];
|
||||
if (Font->Space<2)
|
||||
Font->Space = Font->CharWidth[(int)'a'];
|
||||
|
||||
pixel = GetPixel(Surface, 0, Surface->h-1);
|
||||
SDL_UnlockSurface(Surface);
|
||||
// No longer use SDL color keying
|
||||
//SDL_SetColorKey(Surface, SDL_SRCCOLORKEY, pixel);
|
||||
|
||||
pixel = Get_GFX2_Surface_pixel(Surface, 0, Surface->h-1);
|
||||
Font->Transparent=pixel;
|
||||
|
||||
return Font;
|
||||
@@ -127,28 +88,26 @@ SFont_Font* SFont_InitFont(SDL_Surface* Surface)
|
||||
|
||||
void SFont_FreeFont(SFont_Font* FontInfo)
|
||||
{
|
||||
SDL_FreeSurface(FontInfo->Surface);
|
||||
Free_GFX2_Surface(FontInfo->Surface);
|
||||
free(FontInfo);
|
||||
}
|
||||
|
||||
void SFont_Write(SDL_Surface *Surface, const SFont_Font *Font,
|
||||
void SFont_Write(T_GFX2_Surface *Surface, const SFont_Font *Font,
|
||||
int x, int y, const char *text)
|
||||
{
|
||||
const char* c;
|
||||
SDL_Rect srcrect, dstrect;
|
||||
int line;
|
||||
int height;
|
||||
|
||||
if(text == NULL)
|
||||
return;
|
||||
|
||||
// these values won't change in the loop
|
||||
srcrect.y = 1;
|
||||
dstrect.y = y;
|
||||
srcrect.h = dstrect.h = Font->Surface->h - 1;
|
||||
height = Font->Surface->h - 1;
|
||||
|
||||
for(c = text; *c != '\0' && x <= Surface->w ; c++) {
|
||||
if (*c == '\n') {
|
||||
dstrect.y += Font->Surface->h-1;
|
||||
x=0;
|
||||
y += height;
|
||||
x = 0;
|
||||
continue;
|
||||
}
|
||||
// skip spaces and nonprintable characters
|
||||
@@ -157,12 +116,11 @@ void SFont_Write(SDL_Surface *Surface, const SFont_Font *Font,
|
||||
continue;
|
||||
}
|
||||
|
||||
srcrect.w = Font->CharWidth[(int)*c];
|
||||
dstrect.w = srcrect.w;
|
||||
srcrect.x = Font->CharBegin[(int)*c];
|
||||
dstrect.x = x;
|
||||
|
||||
SDL_BlitSurface(Font->Surface, &srcrect, Surface, &dstrect);
|
||||
for (line = 0; line < height && (y + line) < Surface->h; line++) {
|
||||
memcpy(Surface->pixels + (y + line) * Surface->w + x,
|
||||
Font->Surface->pixels + (line + 1) * Font->Surface->w + Font->CharBegin[(int)*c],
|
||||
Font->CharWidth[(int)*c]);
|
||||
}
|
||||
|
||||
x += Font->CharWidth[(int)*c];
|
||||
}
|
||||
@@ -192,7 +150,7 @@ int SFont_TextWidth(const SFont_Font *Font, const char *text)
|
||||
width += Font->Space;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
width += Font->CharWidth[(int)*c];
|
||||
}
|
||||
|
||||
@@ -209,7 +167,7 @@ int SFont_TextHeight(const SFont_Font* Font, const char *text)
|
||||
nb_cr++;
|
||||
text++;
|
||||
}
|
||||
|
||||
|
||||
return (Font->Surface->h - 1) * (nb_cr+1);
|
||||
}
|
||||
|
||||
@@ -223,4 +181,3 @@ void SFont_WriteCenter(SDL_Surface *Surface, const SFont_Font *Font,
|
||||
y, text);
|
||||
}
|
||||
*/
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user