bluecore/engine/ScriptSystem_Font.cpp

127 行
2.4 KiB
C++

#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)
{
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;
}
//------------------------------------------------------------------------------
static SQInteger _font_print(HSQUIRRELVM vm)
{
int argc = sq_gettop(vm);
if (argc < 4)
return 0;
const Font *font = 0;
sq_getinstanceup(vm, 1, ( void ** ) &font, 0);
if (font)
{
SQFloat x, y;
const char *text;
SQInteger halign = 0, valign = 0;
sq_getfloat(vm, 2, &x);
sq_getfloat(vm, 3, &y);
sq_getstring(vm, 4, &text);
if (argc > 4)
{
sq_getinteger(vm, 5, &halign);
}
if (argc > 5)
{
sq_getinteger(vm, 6, &valign);
}
font->print( (float)x, (float)y, text, halign, valign);
}
return 0;
}
//------------------------------------------------------------------------------
void setupScriptSystem_Font(ScriptSystem* scriptsystem, FontManager* fontmanager)
{
if (scriptsystem && fontmanager)
{
HSQUIRRELVM vm = scriptsystem->getVM();
gFontManager = fontmanager;
sq_pushroottable(vm);
// push class
sq_pushstring(vm, "Font", -1);
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);
sq_pushstring(vm, "print", -1);
sq_newclosure(vm, _font_print, 0);
sq_newslot(vm, -3, false);
// create class
sq_newslot(vm, -3, false);
}
sq_poptop(vm);
}
}
}