77 lines
1.3 KiB
C++
77 lines
1.3 KiB
C++
#include "sdl_helpers.h"
|
|
|
|
#include <SDL2/SDL.h>
|
|
#include <SDL2/SDL_image.h>
|
|
|
|
#include <map>
|
|
#include <utility>
|
|
|
|
#include <stdio.h>
|
|
|
|
using namespace std;
|
|
|
|
map<pair<string, int>, TTF_Font*> font_map;
|
|
|
|
bool init_sdl()
|
|
{
|
|
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
|
{
|
|
printf("SDL could not initialize! SDL - %s\n", SDL_GetError());
|
|
return false;
|
|
}
|
|
|
|
if (0 != TTF_Init())
|
|
{
|
|
printf("Could not init TTF! SDL - %s\n", SDL_GetError());
|
|
SDL_Quit();
|
|
return false;
|
|
}
|
|
|
|
int img_flags = IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF);
|
|
if (img_flags != (IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF))
|
|
{
|
|
printf("Could not init IMG! SDL - %s\n", SDL_GetError());
|
|
TTF_Quit();
|
|
SDL_Quit();
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void clean_sdl()
|
|
{
|
|
// Clean up all fonts
|
|
for (auto it = font_map.begin(); it != font_map.end(); ++it)
|
|
{
|
|
TTF_CloseFont(it->second);
|
|
}
|
|
font_map.clear();
|
|
|
|
IMG_Quit();
|
|
TTF_Quit();
|
|
SDL_Quit();
|
|
}
|
|
|
|
TTF_Font* get_font(const string& filename, int size)
|
|
{
|
|
pair<string, int> key(filename, size);
|
|
if (0 != font_map.count(key))
|
|
{
|
|
return font_map[key];
|
|
}
|
|
else
|
|
{
|
|
TTF_Font* font = TTF_OpenFont(filename.c_str(), size);
|
|
if (nullptr != font)
|
|
{
|
|
font_map[key] = font;
|
|
}
|
|
else
|
|
{
|
|
printf("Could not open font '%s' with size %d\n", filename.c_str(), size);
|
|
}
|
|
return font;
|
|
}
|
|
}
|