added FreeSans.ttf to lib folder; added Engine::renderText()

git-svn-id: svn://anubis/anaglym/trunk@156 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2009-11-08 03:03:52 +00:00
parent e8c5c7c5e4
commit 4831fd7658
3 changed files with 44 additions and 3 deletions

View File

@ -34,6 +34,7 @@ using namespace std;
#define WHITESPACE " \t\r\n\f" #define WHITESPACE " \t\r\n\f"
#define HOOK_TIMEOUT 10000u #define HOOK_TIMEOUT 10000u
#define HOOK_STEPS 250000000 #define HOOK_STEPS 250000000
#define FONT_NAME "FreeSans.ttf"
#define FP_EQ(a,b) (fabsf(a - b) < 0.0001) #define FP_EQ(a,b) (fabsf(a - b) < 0.0001)
@ -94,6 +95,7 @@ Engine::Engine(const string & path, Video & video)
m_autoDrawObjects = true; m_autoDrawObjects = true;
m_fileLoader = new EngineFileLoader(this); m_fileLoader = new EngineFileLoader(this);
m_event_time = 0; m_event_time = 0;
m_font = NULL;
size_t pos = path.find_last_of("\\/"); size_t pos = path.find_last_of("\\/");
m_engine_path = (pos != string::npos) ? string(path, 0, pos) : "."; m_engine_path = (pos != string::npos) ? string(path, 0, pos) : ".";
@ -110,6 +112,10 @@ Engine::Engine(const string & path, Video & video)
m_event_mousebutton_down_present = false; m_event_mousebutton_down_present = false;
m_event_mousebutton_up_present = false; m_event_mousebutton_up_present = false;
m_event_mouse_motion_present = false; m_event_mouse_motion_present = false;
m_luaState = lua_open();
registerLibraries();
} }
Engine::~Engine() Engine::~Engine()
@ -122,6 +128,8 @@ Engine::~Engine()
delete it->second; delete it->second;
} }
delete m_fileLoader; delete m_fileLoader;
if (m_font != NULL)
TTF_CloseFont(m_font);
} }
bool Engine::load(const char * program) bool Engine::load(const char * program)
@ -132,9 +140,17 @@ bool Engine::load(const char * program)
? m_program_path.substr(0, pos) ? m_program_path.substr(0, pos)
: "."; : ".";
m_luaState = lua_open(); string path = locateResource(FONT_NAME);
if (path == "")
registerLibraries(); {
cerr << "Couldn't locate " FONT_NAME << endl;
}
else
{
m_font = TTF_OpenFont(path.c_str(), 16);
if (m_font == NULL)
cerr << "Error loading " FONT_NAME "!" << endl;
}
int s = luaL_loadfile(m_luaState, program); int s = luaL_loadfile(m_luaState, program);
@ -446,6 +462,26 @@ void Engine::debug_hook(lua_Debug * debug)
} }
} }
int Engine::renderText(const char * text, int mode, float r, float g, float b,
float br, float bg, float bb)
{
SDL_Surface * surf = NULL;
SDL_Color color = {r, g, b};
SDL_Color bgcolor = {br, bg, bb};
switch (mode)
{
case 0:
surf = TTF_RenderText_Solid(m_font, text, color);
break;
case 1:
surf = TTF_RenderText_Shaded(m_font, text, color, bgcolor);
break;
case 2:
surf = TTF_RenderText_Blended(m_font, text, color);
break;
}
}
/* called by SDL when the update timer expires */ /* called by SDL when the update timer expires */
Uint32 Engine::updateCallback(Uint32 interval, void * param) Uint32 Engine::updateCallback(Uint32 interval, void * param)
{ {

View File

@ -12,6 +12,7 @@
#include "wfobj/WFObj.h" #include "wfobj/WFObj.h"
#include "Video.h" #include "Video.h"
#include "refptr/refptr.h" #include "refptr/refptr.h"
#include "SDL_ttf.h"
class Engine class Engine
{ {
@ -134,6 +135,8 @@ class Engine
bool import(const char * name); bool import(const char * name);
GLuint loadTexture(const char * name); GLuint loadTexture(const char * name);
void debug_hook(lua_Debug * debug); void debug_hook(lua_Debug * debug);
int renderText(const char * text, int mode, float r, float g, float b,
float br = 0.0, float bg = 0.0, float bb = 0.0);
/* lua services */ /* lua services */
int setCamera(lua_State * L); int setCamera(lua_State * L);
@ -166,6 +169,7 @@ class Engine
std::string m_engine_path; std::string m_engine_path;
OdeWorld m_world; OdeWorld m_world;
std::map<int, Object *> m_objects; std::map<int, Object *> m_objects;
std::map<int, SDL_Surface *> m_texts;
int m_next_object_index; int m_next_object_index;
GLdouble m_eye[3]; GLdouble m_eye[3];
GLdouble m_center[3]; GLdouble m_center[3];
@ -179,6 +183,7 @@ class Engine
SDL_Event m_updateEvent; SDL_Event m_updateEvent;
SDL_Event m_exitEvent; SDL_Event m_exitEvent;
Uint32 m_event_time; Uint32 m_event_time;
TTF_Font * m_font;
bool m_event_update_present; bool m_event_update_present;
bool m_event_key_down_present; bool m_event_key_down_present;

BIN
lib/FreeSans.ttf Normal file

Binary file not shown.