parent
0edd96fe17
commit
fcf2395d1d
@ -0,0 +1,47 @@
|
||||
#ifndef FONT_LOADER_INCLUDED
|
||||
#define FONT_LOADER_INCLUDED
|
||||
|
||||
#define FONT_PATH rsrc/font/
|
||||
|
||||
#define MACRO_FOR_ALL_FONTS(M) \
|
||||
M(retro)
|
||||
#define MACRO_FONT_ENUM_GEN(E) FONT_##E,
|
||||
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
|
||||
|
||||
typedef enum
|
||||
{
|
||||
MACRO_FOR_ALL_FONTS(MACRO_FONT_ENUM_GEN)
|
||||
NB_FONTS_DEFINED ///< Contains the number of fonts
|
||||
} EnumFont;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
TTF_Font* fonts[NB_FONTS_DEFINED]; ///< An array of texture (indexed with EnumTextures)
|
||||
} FontHandler;
|
||||
|
||||
/*
|
||||
* \brief Allows to load fonts located in rsrc/font/. Pleas note that all fonts must be in TTF format.
|
||||
* \author Allan Point
|
||||
* \date 03/01/22
|
||||
* \return A FontHandler filled with all fonts
|
||||
* \copyright CECIL
|
||||
*/
|
||||
FontHandler loadFonts(void);
|
||||
|
||||
/*
|
||||
* \brief Allows to free all fonts from a FontHandler.
|
||||
* \author Allan Point
|
||||
* \date 03/01/22
|
||||
* \return True on success. Else false. Normaly, this function will always retrun true.
|
||||
* \copyright CECIL
|
||||
*/
|
||||
bool freeFonts(FontHandler fontHandler);
|
||||
|
||||
|
||||
#endif
|
@ -0,0 +1,46 @@
|
||||
#include "engine/FontLoader.h"
|
||||
|
||||
#define MACRO_TO_FONT_NAME(N) #N".TTF",
|
||||
|
||||
const char* fontNames[] = {
|
||||
MACRO_FOR_ALL_FONTS(MACRO_TO_FONT_NAME)
|
||||
};
|
||||
|
||||
FontHandler loadFonts(void)
|
||||
{
|
||||
const char fontPath[] = "rsrc/font/";
|
||||
FontHandler fontHandler;
|
||||
char* fontPathFile = NULL;
|
||||
for(int i=0; i<NB_FONTS_DEFINED; ++i)
|
||||
{
|
||||
fontPathFile = (char*) malloc(strlen(fontPath)+strlen(fontNames[i]));
|
||||
if(fontPathFile == NULL)
|
||||
{
|
||||
fprintf(stderr, "Error: Internal memory error for font file name\n");
|
||||
continue;
|
||||
}
|
||||
strcpy(fontPathFile, fontPath);
|
||||
strcat(fontPathFile, fontNames[i]);
|
||||
fontHandler.fonts[i] = TTF_OpenFont(fontPathFile, 16);
|
||||
if(fontHandler.fonts[i] == NULL)
|
||||
{
|
||||
fprintf(stderr, "Warning: Can't load %s: %s\n", fontPathFile, TTF_GetError());
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Note: %s%s loaded\n", fontPath, fontNames[i]);
|
||||
}
|
||||
free(fontPathFile);
|
||||
}
|
||||
}
|
||||
|
||||
bool freeFonts(FontHandler fontHandler)
|
||||
{
|
||||
for(int i=0; i<NB_FONTS_DEFINED; ++i)
|
||||
{
|
||||
TTF_CloseFont(fontHandler.fonts[i]);
|
||||
fontHandler.fonts[i] = NULL;
|
||||
printf("Note: Free font n°%d\n", i);
|
||||
}
|
||||
return true;
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <stdio.h>
|
||||
#include "engine/TextureLoader.h"
|
||||
#include "engine/FontLoader.h"
|
||||
|
||||
//gcc test.c -I ../include $(sdl2-config --cflags --libs)
|
||||
|
||||
int testFontLoader(void) {
|
||||
SDL_Window *window = NULL;
|
||||
SDL_Renderer *renderer = NULL;
|
||||
SDL_Texture* picture;
|
||||
int statut = EXIT_FAILURE;
|
||||
char* path = "rsrc/img/Lenna.png";
|
||||
|
||||
if(0 != SDL_Init(SDL_INIT_VIDEO))
|
||||
{
|
||||
fprintf(stderr, "Erreur SDL_INIT: %s\n", SDL_GetError());
|
||||
goto Quit;
|
||||
}
|
||||
|
||||
//fenetre
|
||||
window = SDL_CreateWindow("Fenêtre", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640,480, SDL_WINDOW_SHOWN);
|
||||
if(window == NULL)
|
||||
{
|
||||
fprintf(stderr, "Erreur SDL_CreateWindow: %s\n", SDL_GetError());
|
||||
goto Quit;
|
||||
}
|
||||
|
||||
//rendu
|
||||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
if(renderer == NULL)
|
||||
{
|
||||
fprintf(stderr, "Erreur SDL_CreateRenderer: %s\n", SDL_GetError());
|
||||
goto Quit;
|
||||
}
|
||||
|
||||
//Fonts
|
||||
if(TTF_Init() == -1)
|
||||
{
|
||||
fprintf(stderr, "Erreur: TTF_Init: %s\n", TTF_GetError());
|
||||
goto Quit;
|
||||
}
|
||||
|
||||
if(0 != SDL_SetRenderDrawColor(renderer, 0,0,0,0)) //choisi la couleur avec laquelle travailler
|
||||
{
|
||||
fprintf(stderr, "Erreur SDL_SetRenderDrawColor: %s\n", SDL_GetError());
|
||||
goto Quit;
|
||||
}
|
||||
|
||||
if(0 != SDL_RenderClear(renderer)) //efface le rendu en le repeignant avec la couleur choisi
|
||||
{
|
||||
fprintf(stderr, "Erreur SDL_SetRenderDrawColor: %s\n", SDL_GetError());
|
||||
goto Quit;
|
||||
}
|
||||
|
||||
if(0 != SDL_SetRenderDrawColor(renderer, 255,255,255,255)) //choisi la couleur avec laquelle travailler
|
||||
{
|
||||
fprintf(stderr, "Erreur SDL_SetRenderDrawColor: %s\n", SDL_GetError());
|
||||
goto Quit;
|
||||
}
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
SDL_bool quit = SDL_FALSE;
|
||||
SDL_Event event;
|
||||
|
||||
|
||||
picture = createTextureFromPath(renderer, path);
|
||||
FontHandler fontHandler = loadFonts();
|
||||
while(!quit)
|
||||
{
|
||||
while(SDL_PollEvent(&event))
|
||||
{
|
||||
switch(event.type)
|
||||
{
|
||||
case SDL_QUIT:
|
||||
quit = SDL_TRUE;
|
||||
break;
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
break;
|
||||
}
|
||||
}
|
||||
SDL_RenderCopy(renderer, picture, NULL, NULL);
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
SDL_Delay(20);
|
||||
}
|
||||
|
||||
Quit:
|
||||
freeFonts(fontHandler);
|
||||
if(renderer != NULL)
|
||||
SDL_DestroyRenderer(renderer);
|
||||
if(window != NULL)
|
||||
SDL_DestroyWindow(window);
|
||||
TTF_Quit();
|
||||
SDL_Quit();
|
||||
return statut;
|
||||
}
|
Loading…
Reference in new issue