bluecore/engine/ScriptSystem_Font.cpp

127 lines
2.4 KiB
C++
Raw Permalink Normal View History

2008-01-16 12:45:17 +01:00
#include "ScriptSystem_Font.h"
namespace BlueCore
{
//------------------------------------------------------------------------------
static weak_ptr<FontManager> gFontManager;
//------------------------------------------------------------------------------
static SQInteger _font_releasehook(SQUserPointer p, SQInteger size)
{
Font *font = (Font *)p;
if (font)
font->removeReference();
return 1;
}
//------------------------------------------------------------------------------
static SQInteger _font_constructor(HSQUIRRELVM vm)
{
2008-01-20 11:16:37 +01:00
SQInteger argc = sq_gettop(vm);
Font *font = 0;
if (argc < 3)
{
if (gFontManager.valid())
font = gFontManager->getDefaultFont();
}
else
{
const SQChar *name = 0;
SQInteger size = 1;
SQBool hinting = SQFalse;
sq_getstring(vm, 2, &name);
sq_getinteger(vm, 3, &size);
if (argc > 3)
sq_getbool(vm, 4, &hinting);
if (gFontManager.valid())
{
font = gFontManager->loadFont(name, size, hinting == SQTrue);
if (font)
font->addReference();
}
}
sq_setinstanceup(vm, 1, (void *)font );
sq_setreleasehook(vm, 1, _font_releasehook);
return 0;
2008-01-16 12:45:17 +01:00
}
//------------------------------------------------------------------------------
static SQInteger _font_print(HSQUIRRELVM vm)
{
2008-01-20 11:16:37 +01:00
int argc = sq_gettop(vm);
2008-01-16 12:45:17 +01:00
2008-01-20 11:16:37 +01:00
if (argc < 4)
return 0;
2008-01-16 12:45:17 +01:00
2008-01-20 11:16:37 +01:00
const Font *font = 0;
sq_getinstanceup(vm, 1, ( void ** ) &font, 0);
2008-01-16 12:45:17 +01:00
2008-01-20 11:16:37 +01:00
if (font)
{
SQFloat x, y;
const char *text;
SQInteger halign = 0, valign = 0;
2008-01-16 12:45:17 +01:00
2008-01-20 11:16:37 +01:00
sq_getfloat(vm, 2, &x);
sq_getfloat(vm, 3, &y);
sq_getstring(vm, 4, &text);
2008-01-16 12:45:17 +01:00
2008-01-20 11:16:37 +01:00
if (argc > 4)
{
sq_getinteger(vm, 5, &halign);
}
2008-01-16 12:45:17 +01:00
2008-01-20 11:16:37 +01:00
if (argc > 5)
{
sq_getinteger(vm, 6, &valign);
}
2008-01-16 12:45:17 +01:00
2008-01-20 11:16:37 +01:00
font->print( (float)x, (float)y, text, halign, valign);
}
2008-01-16 12:45:17 +01:00
2008-01-20 11:16:37 +01:00
return 0;
2008-01-16 12:45:17 +01:00
}
//------------------------------------------------------------------------------
2008-01-20 11:16:37 +01:00
void setupScriptSystem_Font(ScriptSystem* scriptsystem, FontManager* fontmanager)
2008-01-16 12:45:17 +01:00
{
2008-01-20 11:16:37 +01:00
if (scriptsystem && fontmanager)
{
HSQUIRRELVM vm = scriptsystem->getVM();
2008-01-16 12:45:17 +01:00
2008-01-20 11:16:37 +01:00
gFontManager = fontmanager;
2008-01-16 12:45:17 +01:00
2008-01-20 11:16:37 +01:00
sq_pushroottable(vm);
2008-01-16 12:45:17 +01:00
2008-01-20 11:16:37 +01:00
// push class
sq_pushstring(vm, "Font", -1);
2008-01-16 12:45:17 +01:00
2008-01-20 11:16:37 +01:00
if (SQ_SUCCEEDED(sq_newclass(vm, SQFalse) ) )
{
// register constructor
sq_pushstring(vm, "constructor", -1);
sq_newclosure(vm, _font_constructor, 0);
sq_newslot(vm, -3, false);
2008-01-16 12:45:17 +01:00
2008-01-20 11:16:37 +01:00
sq_pushstring(vm, "print", -1);
sq_newclosure(vm, _font_print, 0);
sq_newslot(vm, -3, false);
2008-01-16 12:45:17 +01:00
2008-01-20 11:16:37 +01:00
// create class
sq_newslot(vm, -3, false);
}
2008-01-16 12:45:17 +01:00
2008-01-20 11:16:37 +01:00
sq_poptop(vm);
}
2008-01-16 12:45:17 +01:00
}
}