Skip to content

Commit

Permalink
Update font loading to use ResourceFinder
Browse files Browse the repository at this point in the history
  • Loading branch information
David-Estevez committed Jun 16, 2016
1 parent c76dd44 commit 00f62ce
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/libraries/RdUserInterfaceLib/DeadScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const std::string rd::DeadScreen::PARAM_LAST_CAMERA_FRAME = "last_camera_frame";

//-- Protected
const std::string rd::DeadScreen::SKULL_PATH = "../../share/images/skull.png";
const std::string rd::DeadScreen::FONT_PATH = "/usr/share/fonts/truetype/freefont/FreeMono.ttf";
const std::string rd::DeadScreen::FONT_PATH = "../../share/fonts/FreeMono.ttf";

//-- Private
const SDL_Color rd::DeadScreen::TEXT_COLOR = {0,255,0,0};
Expand All @@ -32,10 +32,10 @@ bool rd::DeadScreen::init()
}

//-- Load the font
font = TTF_OpenFont(FONT_PATH.c_str(), 32);
font = TTF_OpenFont(rf.findFileByName(FONT_PATH).c_str(), 32);
if (font == NULL)
{
RD_ERROR("Unable to load font: %s %s \n", FONT_PATH.c_str(), TTF_GetError());
RD_ERROR("Unable to load font: %s %s \n", rf.findFileByName(FONT_PATH).c_str(), TTF_GetError());
return false;
}

Expand Down
19 changes: 12 additions & 7 deletions src/libraries/RdUserInterfaceLib/GameScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,31 @@ rd::GameScreen::GameScreen()

bool rd::GameScreen::init()
{
//-- Configure Resourcefinder to ind the real path to the resources
yarp::os::ResourceFinder rf;
rf.setDefaultContext("robotDevastation");
rf.setDefaultConfigFile("robotDevastation.ini");

//-- Load the font(s)
const char * font_name = "/usr/share/fonts/truetype/freefont/FreeMono.ttf";
player_font = TTF_OpenFont(font_name, 12);
std::string font_name("../../share/fonts/FreeMono.ttf");
player_font = TTF_OpenFont(rf.findFileByName(font_name).c_str(), 12);
if (player_font == NULL)
{
RD_ERROR("Unable to load font: %s %s \n", font_name, TTF_GetError());
RD_ERROR("Unable to load font: %s %s \n", rf.findFileByName(font_name).c_str(), TTF_GetError());
return false;
}

target_font = TTF_OpenFont(font_name, 12);
target_font = TTF_OpenFont(rf.findFileByName(font_name).c_str(), 12);
if (target_font == NULL)
{
RD_ERROR("Unable to load font: %s %s \n", font_name, TTF_GetError());
RD_ERROR("Unable to load font: %s %s \n", rf.findFileByName(font_name).c_str(), TTF_GetError());
return false;
}

weapon_font = TTF_OpenFont(font_name, 12);
weapon_font = TTF_OpenFont(rf.findFileByName(font_name).c_str(), 12);
if (weapon_font == NULL)
{
RD_ERROR("Unable to load font: %s %s \n", font_name, TTF_GetError());
RD_ERROR("Unable to load font: %s %s \n", rf.findFileByName(font_name).c_str(), TTF_GetError());
return false;
}

Expand Down
1 change: 1 addition & 0 deletions src/libraries/RdUserInterfaceLib/GameScreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <iostream>
#include <string>
#include <vector>
#include <yarp/os/ResourceFinder.h>

#include "RdMentalMap.hpp"
#include "RdTarget.hpp"
Expand Down
6 changes: 3 additions & 3 deletions src/libraries/RdUserInterfaceLib/InitScreen.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "InitScreen.hpp"

const std::string rd::InitScreen::SPLASH_PATH = "../../share/images/800px-Devastation-thin.png";
const std::string rd::InitScreen::FONT_PATH = "/usr/share/fonts/truetype/freefont/FreeMono.ttf";
const std::string rd::InitScreen::FONT_PATH = "../../share/fonts/FreeMono.ttf";

rd::InitScreen::InitScreen()
{
Expand All @@ -24,10 +24,10 @@ bool rd::InitScreen::init()
}

//-- Load the font
font = TTF_OpenFont(FONT_PATH.c_str(), 28);
font = TTF_OpenFont(rf.findFileByName(FONT_PATH).c_str(), 28);
if (font == NULL)
{
RD_ERROR("Unable to load font: %s %s \n", FONT_PATH.c_str(), TTF_GetError());
RD_ERROR("Unable to load font: %s %s \n", rf.findFileByName(FONT_PATH).c_str(), TTF_GetError());
return false;
}

Expand Down
13 changes: 9 additions & 4 deletions test/testSDLTextDisplay.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <cstdlib> // For some useful functions such as atexit :)
#include <iostream>
#include <yarp/os/ResourceFinder.h>

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
Expand All @@ -13,6 +13,11 @@

int main(void)
{
//-- Configure Resourcefinder to ind the real path to the resources
yarp::os::ResourceFinder rf;
rf.setDefaultContext("robotDevastation");
rf.setDefaultConfigFile("robotDevastation.ini");

// Load SDL
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
Expand All @@ -35,10 +40,10 @@ int main(void)
}

//-- Load the font
const char * font_name = "/usr/share/fonts/truetype/freefont/FreeMono.ttf";
TTF_Font *font = TTF_OpenFont(font_name, 28);
std::string font_name("../../share/fonts/FreeMono.ttf");
TTF_Font *font = TTF_OpenFont(rf.findFileByName(font_name).c_str(), 28);
if (font == NULL){
printf("Unable to load font: %s %s \n", font_name, TTF_GetError());
printf("Unable to load font: %s %s \n", rf.findFileByName(font_name).c_str(), TTF_GetError());
return false;
}

Expand Down

0 comments on commit 00f62ce

Please sign in to comment.