Remove the Nb_fonts global variable.

+ do not return pointers to static buffers
This commit is contained in:
Thomas Bernard 2019-12-23 22:16:20 +01:00
parent e14edb10d6
commit 50ac812f6a
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C
3 changed files with 43 additions and 27 deletions

View File

@ -5056,7 +5056,7 @@ void Button_Text(int btn)
Print_in_window_underscore(206,89,"Italic", MC_Dark, MC_Light,1); Print_in_window_underscore(206,89,"Italic", MC_Dark, MC_Light,1);
// Scroller des fontes // Scroller des fontes
font_scroller = Window_set_scroller_button(165,35,NB_FONTS*8,Nb_fonts,NB_FONTS,list_start); // 5 font_scroller = Window_set_scroller_button(165,35,NB_FONTS*8,Font_count(),NB_FONTS,list_start); // 5
// Liste des fontes disponibles // Liste des fontes disponibles
font_list_button = Window_set_special_button(8,35,152,NB_FONTS*8,0); // 6 font_list_button = Window_set_special_button(8,35,152,NB_FONTS*8,0); // 6
Window_display_frame_in(7, 33, 154, NB_FONTS*8+4); Window_display_frame_in(7, 33, 154, NB_FONTS*8+4);

View File

@ -92,7 +92,6 @@ typedef struct T_Font
* Head of the font linked list * Head of the font linked list
*/ */
T_Font * font_list_start; T_Font * font_list_start;
int Nb_fonts;
// Inspiré par Allegro // Inspiré par Allegro
#define EXTID(a,b,c) ((((a)&255)<<16) | (((b)&255)<<8) | (((c)&255))) #define EXTID(a,b,c) ((((a)&255)<<16) | (((b)&255)<<8) | (((c)&255)))
@ -116,7 +115,6 @@ static void Insert_font(T_Font * font)
{ {
// Premiere (liste vide) // Premiere (liste vide)
font_list_start = font; font_list_start = font;
Nb_fonts++;
} }
else else
{ {
@ -134,7 +132,6 @@ static void Insert_font(T_Font * font)
// Avant la premiere // Avant la premiere
font->Next=font_list_start; font->Next=font_list_start;
font_list_start=font; font_list_start=font;
Nb_fonts++;
} }
else else
{ {
@ -152,7 +149,6 @@ static void Insert_font(T_Font * font)
} }
font->Next=searched_font->Next; font->Next=searched_font->Next;
searched_font->Next=font; searched_font->Next=font;
Nb_fonts++;
} }
} }
} }
@ -248,36 +244,37 @@ static void Add_font(const char *name, const char * font_name)
// Trouve le nom d'une fonte par son numéro // Trouve le nom d'une fonte par son numéro
char * Font_name(int index) const char * Font_name(int index)
{ {
T_Font *font = font_list_start; T_Font *font = font_list_start;
if (index<0 ||index>=Nb_fonts) if (index < 0 || font == NULL)
return ""; return "";
while (index--) while (index--)
{
font = font->Next; font = font->Next;
if (font == NULL)
return "";
}
return font->Name; return font->Name;
} }
// Trouve le libellé d'affichage d'une fonte par son numéro // Trouve le libellé d'affichage d'une fonte par son numéro
// Renvoie un pointeur sur un buffer statique de 20 caracteres. // Renvoie un pointeur sur un buffer statique de 20 caracteres.
char * Font_label(int index) const char * Font_label(int index)
{ {
T_Font *font; T_Font *font = font_list_start;
static char label[20];
strcpy(label, " ");
// Recherche de la fonte
font = font_list_start; font = font_list_start;
if (index<0 ||index>=Nb_fonts) if (index < 0 || font == NULL)
return label; return " ";
while (index--) while (index--)
{
font = font->Next; font = font->Next;
if (font == NULL)
// Libellé return " ";
strcpy(label, font->Label); }
return label; return font->Label;
} }
@ -285,13 +282,30 @@ char * Font_label(int index)
int TrueType_font(int index) int TrueType_font(int index)
{ {
T_Font *font = font_list_start; T_Font *font = font_list_start;
if (index<0 ||index>=Nb_fonts) if (index < 0 || font == NULL)
return 0; return 0;
while (index--) while (index--)
{
font = font->Next; font = font->Next;
if (font == NULL)
return 0;
}
return font->Is_truetype; return font->Is_truetype;
} }
int Font_count(void)
{
T_Font *font = font_list_start;
int count = 0;
while (font != NULL)
{
count++;
font = font->Next;
}
return count;
}
#if defined(WIN32) && defined(NOTTF) #if defined(WIN32) && defined(NOTTF)
static int CALLBACK EnumFontFamCallback(CONST LOGFONTA *lpelf, CONST TEXTMETRICA *lpntm, DWORD FontType, LPARAM lParam) static int CALLBACK EnumFontFamCallback(CONST LOGFONTA *lpelf, CONST TEXTMETRICA *lpntm, DWORD FontType, LPARAM lParam)
{ {
@ -328,7 +342,6 @@ void Init_text(void)
// Initialisation des fontes // Initialisation des fontes
font_list_start = NULL; font_list_start = NULL;
Nb_fonts=0;
// Parcours du répertoire "fonts" // Parcours du répertoire "fonts"
directory_name = Filepath_append_to_dir(Data_directory, FONTS_SUBDIRECTORY); directory_name = Filepath_append_to_dir(Data_directory, FONTS_SUBDIRECTORY);
For_each_file(directory_name, Add_font); For_each_file(directory_name, Add_font);
@ -793,11 +806,15 @@ byte *Render_text(const char *str, int font_number, int size, int antialias, int
#endif #endif
// Verification type de la fonte // Verification type de la fonte
if (font_number<0 ||font_number>=Nb_fonts) if (font_number < 0 || font == NULL)
return NULL; return NULL;
while (index--) while (index--)
{
font = font->Next; font = font->Next;
if (font == NULL)
return NULL;
}
if (font->Is_truetype) if (font->Is_truetype)
{ {
#if !defined(NOTTF) #if !defined(NOTTF)

View File

@ -48,12 +48,11 @@ void Add_font(const char *name);
byte *Render_text(const char *str, int font_number, int size, int antialias, int bold, int italic, int *width, int *height, T_Palette palette); byte *Render_text(const char *str, int font_number, int size, int antialias, int bold, int italic, int *width, int *height, T_Palette palette);
/// Finds a label to display for a font declared with ::Add_font(). /// Finds a label to display for a font declared with ::Add_font().
char * Font_label(int index); const char * Font_label(int index);
/// Finds the filename of a font declared with ::Add_font(). /// Finds the filename of a font declared with ::Add_font().
char * Font_name(int index); const char * Font_name(int index);
/// Returns true if the font of this number is TrueType, false if it's a SFont bitmap. /// Returns true if the font of this number is TrueType, false if it's a SFont bitmap.
int TrueType_font(int index); int TrueType_font(int index);
/// ///
/// Number of fonts declared with a series of ::Add_font(). This is public for /// Number of fonts declared with a series of ::Add_font()
/// convenience, but functionaly it is read-only. int Font_count(void);
extern int Nb_fonts;