Initial commit

This commit is contained in:
Jonas Kulla 2013-09-01 16:27:21 +02:00
commit ff25887f41
119 changed files with 24901 additions and 0 deletions

View file

@ -0,0 +1,88 @@
/*
** audio-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "audio.h"
#include "globalstate.h"
#include "binding-util.h"
#include "exception.h"
#define DEF_PLAY_STOP(entity) \
RB_METHOD(audio_##entity##Play) \
{ \
RB_UNUSED_PARAM; \
const char *filename; \
int volume = 100; \
int pitch = 100; \
rb_get_args(argc, argv, "z|ii", &filename, &volume, &pitch); \
GUARD_EXC( gState->audio().entity##Play(filename, volume, pitch); ) \
return Qnil; \
} \
RB_METHOD(audio_##entity##Stop) \
{ \
RB_UNUSED_PARAM; \
gState->audio().entity##Stop(); \
return Qnil; \
}
#define DEF_FADE(entity) \
RB_METHOD(audio_##entity##Fade) \
{ \
RB_UNUSED_PARAM; \
int time; \
rb_get_args(argc, argv, "i", &time); \
gState->audio().bgmFade(time); \
return Qnil; \
}
#define DEF_PLAY_STOP_FADE(entity) \
DEF_PLAY_STOP(entity) \
DEF_FADE(entity)
DEF_PLAY_STOP_FADE( bgm )
DEF_PLAY_STOP_FADE( bgs )
DEF_PLAY_STOP_FADE( me )
DEF_PLAY_STOP( se )
#define BIND_PLAY_STOP(entity) \
_rb_define_module_function(module, #entity "_play", audio_##entity##Play); \
_rb_define_module_function(module, #entity "_stop", audio_##entity##Stop);
#define BIND_FADE(entity) \
_rb_define_module_function(module, #entity "_fade", audio_##entity##Fade);
#define BIND_PLAY_STOP_FADE(entity) \
BIND_PLAY_STOP(entity) \
BIND_FADE(entity)
void
audioBindingInit()
{
VALUE module = rb_define_module("Audio");
BIND_PLAY_STOP_FADE( bgm )
BIND_PLAY_STOP_FADE( bgs )
BIND_PLAY_STOP_FADE( me )
BIND_PLAY_STOP( se )
}

313
binding-mri/binding-mri.cpp Normal file
View file

@ -0,0 +1,313 @@
/*
** binding-mri.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "binding.h"
#include "binding-util.h"
#include "globalstate.h"
#include "eventthread.h"
#include "filesystem.h"
#include "./ruby/include/ruby.h"
#include "zlib.h"
#include <QFile>
#include <QByteArray>
#include <QDebug>
extern const char module_rpg[];
static void mriBindingExecute();
static void mriBindingTerminate();
ScriptBinding scriptBindingImpl =
{
mriBindingExecute,
mriBindingTerminate
};
ScriptBinding *scriptBinding = &scriptBindingImpl;
void tableBindingInit();
void etcBindingInit();
void fontBindingInit();
void bitmapBindingInit();
void spriteBindingInit();
void viewportBindingInit();
void planeBindingInit();
void windowBindingInit();
void tilemapBindingInit();
void inputBindingInit();
void audioBindingInit();
void graphicsBindingInit();
void fileIntBindingInit();
static VALUE mriPrint(int, VALUE*, VALUE);
static VALUE mriP(int, VALUE*, VALUE);
static void mriBindingInit()
{
tableBindingInit();
etcBindingInit();
fontBindingInit();
bitmapBindingInit();
spriteBindingInit();
viewportBindingInit();
planeBindingInit();
windowBindingInit();
tilemapBindingInit();
inputBindingInit();
audioBindingInit();
graphicsBindingInit();
fileIntBindingInit();
_rb_define_module_function(rb_mKernel, "print", mriPrint);
_rb_define_module_function(rb_mKernel, "p", mriP);
rb_eval_string(module_rpg);
rb_define_global_const("MKXP", Qtrue);
}
static void
showMsg(const QByteArray &msg)
{
gState->eThread().showMessageBox(msg.constData());
}
static void printP(int argc, VALUE *argv,
const char *convMethod, const char *sep)
{
VALUE dispString = rb_str_buf_new(128);
ID conv = rb_intern(convMethod);
for (int i = 0; i < argc; ++i)
{
VALUE str = rb_funcall(argv[i], conv, 0);
rb_str_buf_append(dispString, str);
if (i < argc)
rb_str_buf_cat2(dispString, sep);
}
showMsg(RSTRING_PTR(dispString));
rb_str_free(dispString);
}
RB_METHOD(mriPrint)
{
RB_UNUSED_PARAM;
printP(argc, argv, "to_s", "");
return Qnil;
}
RB_METHOD(mriP)
{
RB_UNUSED_PARAM;
printP(argc, argv, "inspect", "\n");
return Qnil;
}
static void runCustomScript(const char *filename)
{
QFile scriptFile(filename);
if (!scriptFile.open(QFile::ReadOnly))
{
showMsg(QByteArray("Unable to open '") + filename + "'");
return;
}
QByteArray scriptData = scriptFile.readAll();
scriptFile.close();
rb_eval_string_protect(scriptData.constData(), 0);
}
VALUE kernelLoadDataInt(const char *filename);
struct Script
{
QByteArray name;
QByteArray encData;
uint32_t unknown;
QByteArray decData;
};
static void runRMXPScripts()
{
const QByteArray &scriptPack = gState->rtData().config.game.scripts;
if (!gState->fileSystem().exists(scriptPack.constData()))
{
showMsg("Unable to open '" + scriptPack + "'");
return;
}
VALUE scriptArray = kernelLoadDataInt(scriptPack.constData());
if (rb_type(scriptArray) != RUBY_T_ARRAY)
{
showMsg("Failed to read script data");
return;
}
int scriptCount = RARRAY_LEN(scriptArray);
QByteArray decodeBuffer;
decodeBuffer.resize(0x1000);
QVector<Script> encScripts(scriptCount);
for (int i = 0; i < scriptCount; ++i)
{
VALUE script = rb_ary_entry(scriptArray, i);
if (rb_type(script) != RUBY_T_ARRAY)
{
continue;
}
VALUE scriptUnknown = rb_ary_entry(script, 0);
VALUE scriptName = rb_ary_entry(script, 1);
VALUE scriptString = rb_ary_entry(script, 2);
Script &sc = encScripts[i];
sc.name = RSTRING_PTR(scriptName);
sc.encData = QByteArray(RSTRING_PTR(scriptString), RSTRING_LEN(scriptString));
sc.unknown = FIX2UINT(scriptUnknown);
}
for (int i = 0; i < scriptCount; ++i)
{
Script &sc = encScripts[i];
int result = Z_OK;
ulong bufferLen;
while (true)
{
unsigned char *bufferPtr =
reinterpret_cast<unsigned char*>(const_cast<char*>(decodeBuffer.constData()));
const unsigned char *sourcePtr =
reinterpret_cast<const unsigned char*>(sc.encData.constData());
bufferLen = decodeBuffer.length();
result = uncompress(bufferPtr, &bufferLen,
sourcePtr, sc.encData.length());
bufferPtr[bufferLen] = '\0';
if (result != Z_BUF_ERROR)
break;
decodeBuffer.resize(decodeBuffer.size()*2);
}
if (result != Z_OK)
{
static char buffer[256];
snprintf(buffer, sizeof(buffer), "Error decoding script %d: '%s'",
i, sc.name.constData());
showMsg(buffer);
break;
}
// QFile file(QString("/home/Ancurio/programming/C++/mkxp/dump/%1.rb").arg(i, 3, 10, QChar('0')));
// if (file.open(QFile::WriteOnly))
// file.write(decodeBuffer.constData(), bufferLen);
sc.decData = QByteArray(decodeBuffer.constData(), bufferLen);
ruby_script(sc.name.constData());
rb_gc_start();
qDebug() << "Executing script:" << QString("%1").arg(i, 3, 10, QChar('0'));
/* Execute code */
rb_eval_string_protect(decodeBuffer.constData(), 0);
VALUE exc = rb_gv_get("$!");
if (rb_type(exc) != RUBY_T_NIL)
break;
}
// QFile file("/home/Ancurio/programming/C++/mkxp/dump/index");
// if (file.open(QFile::WriteOnly))
// {
// for (int i = 0; i < encScripts.size(); ++i)
// {
// const Script &sc = encScripts[i];
// file.write(sc.name);
// }
// }
}
static void mriBindingExecute()
{
ruby_setup();
RbData rbData;
gState->setBindingData(&rbData);
mriBindingInit();
QByteArray &customScript = gState->rtData().config.customScript;
if (!customScript.isEmpty())
runCustomScript(customScript.constData());
else
runRMXPScripts();
VALUE exc = rb_gv_get("$!");
if (rb_type(exc) != RUBY_T_NIL && !rb_eql(rb_obj_class(exc), rb_eSystemExit))
{
qDebug() << "Had exception:" << rb_class2name(rb_obj_class(exc));
VALUE bt = rb_funcall(exc, rb_intern("backtrace"), 0);
rb_p(bt);
VALUE msg = rb_funcall(exc, rb_intern("message"), 0);
if (RSTRING_LEN(msg) < 256)
showMsg(RSTRING_PTR(msg));
else
qDebug() << (RSTRING_PTR(msg));
}
ruby_cleanup(0);
gState->rtData().rqTermAck = true;
}
static void mriBindingTerminate()
{
rb_raise(rb_eSystemExit, " ");
}

View file

@ -0,0 +1,40 @@
/*
** binding-types.h
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BINDINGTYPES_H
#define BINDINGTYPES_H
#include "binding-util.h"
DECL_TYPE(Table);
DECL_TYPE(Rect);
DECL_TYPE(Color);
DECL_TYPE(Tone);
DECL_TYPE(Font);
DECL_TYPE(Bitmap);
DECL_TYPE(Sprite);
DECL_TYPE(Plane);
DECL_TYPE(Viewport);
DECL_TYPE(Tilemap);
DECL_TYPE(Window);
#endif // BINDINGTYPES_H

View file

@ -0,0 +1,306 @@
/*
** binding-util.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "binding-util.h"
#include "globalstate.h"
#include "exception.h"
#include "util.h"
#include <stdarg.h>
#include <QDebug>
void initType(rb_data_type_struct &type,
const char *name,
void (*freeInst)(void *))
{
type.wrap_struct_name = name;
type.function.dmark = 0;
type.function.dsize = 0;
type.function.dfree = freeInst;
type.function.reserved[0] =
type.function.reserved[1] = 0;
type.parent = 0;
}
RbData *getRbData()
{
return static_cast<RbData*>(gState->bindingData());
}
//enum RbException
//{
// RGSS = 0,
// PHYSFS,
// SDL,
// ErrnoENOENT,
// IOError,
// TypeError,
// ArgumentError,
// RbExceptionsMax
//};
struct
{
RbException id;
const char *name;
} static customExc[] =
{
{ RGSS, "RGSSError" },
{ PHYSFS, "PHYSFSError" },
{ SDL, "SDLError" }
};
RbData::RbData()
{
for (size_t i = 0; i < ARRAY_SIZE(customExc); ++i)
exc[customExc[i].id] = rb_define_class(customExc[i].name, rb_eException);
exc[ErrnoENOENT] = rb_const_get(rb_const_get(rb_cObject, rb_intern("Errno")), rb_intern("ENOENT"));
exc[IOError] = rb_eIOError;
exc[TypeError] = rb_eTypeError;
exc[ArgumentError] = rb_eArgError;
}
RbData::~RbData()
{
}
/* Indexed with Exception::Type */
static const RbException excToRbExc[] =
{
RGSS, /* RGSSError */
ErrnoENOENT, /* NoFileError */
IOError,
TypeError,
ArgumentError,
PHYSFS, /* PHYSFSError */
SDL /* SDLError */
};
void raiseRbExc(const Exception &exc)
{
RbData *data = getRbData();
VALUE excClass = data->exc[excToRbExc[exc.type]];
static char buffer[512];
exc.snprintf(buffer, sizeof(buffer));
rb_raise(excClass, buffer);
}
int
rb_get_args(int argc, VALUE *argv, const char *format, ...)
{
char c;
VALUE *arg = argv;
va_list ap;
bool opt = false;
int argI = 0;
va_start(ap, format);
while ((c = *format++))
{
switch (c)
{
case '|' :
break;
default:
// FIXME print num of needed args vs provided
if (argc <= argI && !opt)
rb_raise(rb_eArgError, "wrong number of arguments");
break;
}
if (argI >= argc)
break;
switch (c)
{
case 'o' :
{
if (argI >= argc)
break;
VALUE *obj = va_arg(ap, VALUE*);
*obj = *arg++;
++argI;
break;
}
case 'S' :
{
if (argI >= argc)
break;
VALUE *str = va_arg(ap, VALUE*);
VALUE tmp = *arg;
if (!rb_type(tmp) == RUBY_T_STRING)
rb_raise(rb_eTypeError, "Argument %d: Expected string", argI);
*str = tmp;
++argI;
break;
}
case 's' :
{
if (argI >= argc)
break;
const char **s = va_arg(ap, const char**);
int *len = va_arg(ap, int*);
VALUE tmp = *arg;
if (!rb_type(tmp) == RUBY_T_STRING)
rb_raise(rb_eTypeError, "Argument %d: Expected string", argI);
*s = RSTRING_PTR(tmp);
*len = RSTRING_LEN(tmp);
++argI;
break;
}
case 'z' :
{
if (argI >= argc)
break;
const char **s = va_arg(ap, const char**);
VALUE tmp = *arg++;
if (!rb_type(tmp) == RUBY_T_STRING)
rb_raise(rb_eTypeError, "Argument %d: Expected string", argI);
*s = RSTRING_PTR(tmp);
++argI;
break;
}
case 'f' :
{
if (argI >= argc)
break;
double *f = va_arg(ap, double*);
VALUE fVal = *arg++;
switch (rb_type(fVal))
{
case RUBY_T_FLOAT :
*f = rb_float_value(fVal);
break;
case RUBY_T_FIXNUM :
*f = rb_fix2int(fVal);
break;
default:
rb_raise(rb_eTypeError, "Argument %d: Expected float", argI);
}
++argI;
break;
}
case 'i' :
{
if (argI >= argc)
break;
int *i = va_arg(ap, int*);
VALUE iVal = *arg++;
switch (rb_type(iVal))
{
case RUBY_T_FLOAT :
// FIXME check int range?
*i = rb_num2long(iVal);
break;
case RUBY_T_FIXNUM :
*i = rb_fix2int(iVal);
break;
default:
rb_raise(rb_eTypeError, "Argument %d: Expected fixnum", argI);
}
++argI;
break;
}
case 'b' :
{
if (argI >= argc)
break;
bool *b = va_arg(ap, bool*);
VALUE bVal = *arg++;
switch (rb_type(bVal))
{
case RUBY_T_TRUE :
*b = true;
break;
case RUBY_T_FALSE :
case RUBY_T_NIL :
*b = false;
break;
default:
rb_raise(rb_eTypeError, "Argument %d: Expected bool", argI);
}
++argI;
break;
}
case '|' :
opt = true;
break;
default:
rb_raise(rb_eFatal, "invalid argument specifier %c", c);
}
}
// FIXME print num of needed args vs provided
if (!c && argc > argI)
rb_raise(rb_eArgError, "wrong number of arguments");
va_end(ap);
return argI;
}

297
binding-mri/binding-util.h Normal file
View file

@ -0,0 +1,297 @@
/*
** binding-util.h
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BINDING_UTIL_H
#define BINDING_UTIL_H
#include "./ruby/ruby.h"
#define PRIV_IV "priv"
enum RbException
{
RGSS = 0,
PHYSFS,
SDL,
ErrnoENOENT,
IOError,
TypeError,
ArgumentError,
RbExceptionsMax
};
struct RbData
{
VALUE exc[RbExceptionsMax];
RbData();
~RbData();
};
RbData *getRbData();
struct Exception;
void
raiseRbExc(const Exception &exc);
#define DECL_TYPE(Klass) \
extern rb_data_type_struct Klass##Type
#define DEF_TYPE(Klass) \
rb_data_type_struct Klass##Type
void initType(rb_data_type_struct &type,
const char *name,
void (*freeInst)(void*));
template<class C>
static inline void freeInstance(void *inst)
{
delete static_cast<C*>(inst);
}
#define INIT_TYPE(Klass) initType(Klass##Type, #Klass, freeInstance<Klass>)
template<class C>
static inline C *
getPrivateData(VALUE self)
{
VALUE priv = rb_iv_get(self, PRIV_IV);
return static_cast<C*>(RTYPEDDATA_DATA(priv));
}
template<class C>
static inline C *
getPrivateDataCheck(VALUE self, const rb_data_type_struct &type)
{
VALUE priv = rb_iv_get(self, PRIV_IV);
void *obj = rb_check_typeddata(priv, &type);
return static_cast<C*>(obj);
}
static inline void
setPrivateData(VALUE self, void *p, const rb_data_type_struct &type)
{
VALUE priv = rb_data_typed_object_alloc(rb_cData, p, &type);
rb_iv_set(self, PRIV_IV, priv);
}
inline VALUE
wrapObject(void *p, const rb_data_type_struct &type)
{
VALUE klass = rb_const_get(rb_cObject, rb_intern(type.wrap_struct_name));
VALUE obj = rb_obj_alloc(klass);
setPrivateData(obj, p, type);
return obj;
}
inline VALUE
wrapProperty(VALUE self, void *prop, const char *iv,
const rb_data_type_struct &type)
{
VALUE propObj = wrapObject(prop, type);
rb_iv_set(self, iv, propObj);
return propObj;
}
inline void
wrapNilProperty(VALUE self, const char *iv)
{
rb_iv_set(self, iv, Qnil);
}
/* Implemented: oSszfib| */
int
rb_get_args(int argc, VALUE *argv, const char *format, ...);
typedef VALUE (*RubyMethod)(int argc, VALUE *argv, VALUE self);
static inline void
_rb_define_method(VALUE klass, const char *name, RubyMethod func)
{
rb_define_method(klass, name, RUBY_METHOD_FUNC(func), -1);
}
static inline void
rb_define_class_method(VALUE klass, const char *name, RubyMethod func)
{
rb_define_singleton_method(klass, name, RUBY_METHOD_FUNC(func), -1);
}
static inline void
_rb_define_module_function(VALUE module, const char *name, RubyMethod func)
{
rb_define_module_function(module, name, RUBY_METHOD_FUNC(func), -1);
}
template<class C>
static inline VALUE
objectLoad(int argc, VALUE *argv, VALUE self, rb_data_type_struct &type)
{
const char *data;
int dataLen;
rb_get_args(argc, argv, "s", &data, &dataLen);
VALUE obj = rb_obj_alloc(self);
C *c = C::deserialize(data, dataLen);
setPrivateData(obj, c, type);
return obj;
}
static inline VALUE
rb_bool_new(bool value)
{
return value ? Qtrue : Qfalse;
}
#define RB_METHOD(name) \
static VALUE name(int argc, VALUE *argv, VALUE self)
#define RB_UNUSED_PARAM \
{ (void) argc; (void) argv; (void) self; }
#define MARSH_LOAD_FUN(Typ) \
RB_METHOD(Typ##Load) \
{ \
return objectLoad<Typ>(argc, argv, self, Typ##Type); \
}
#define CLONE_FUNC(Klass) \
static mrb_value \
Klass##Clone(mrb_state *mrb, mrb_value self) \
{ \
Klass *k = getPrivateData<Klass>(mrb, self); \
mrb_value dupObj = mrb_obj_clone(mrb, self); \
Klass *dupK = new Klass(*k); \
setPrivateData(mrb, dupObj, dupK, Klass##Type); \
return dupObj; \
}
#define CLONE_FUN(Klass) \
RB_METHOD(Klass##Clone) \
{ \
RB_UNUSED_PARAM \
Klass *k = getPrivateData<Klass>(self); \
VALUE dupObj = rb_obj_clone(self); \
Klass *dupK = new Klass(*k); \
setPrivateData(dupObj, dupK, Klass##Type); \
return dupObj; \
}
/* If we're not binding a disposable class,
* we want to #undef DEF_PROP_CHK_DISP */
#define DEF_PROP_CHK_DISP \
checkDisposed(k, DISP_CLASS_NAME);
#define DEF_PROP_OBJ(Klass, PropKlass, PropName, prop_iv) \
RB_METHOD(Klass##Get##PropName) \
{ \
RB_UNUSED_PARAM; \
Klass *k = getPrivateData<Klass>(self); (void) k; \
DEF_PROP_CHK_DISP \
return rb_iv_get(self, prop_iv); \
} \
RB_METHOD(Klass##Set##PropName) \
{ \
Klass *k = getPrivateData<Klass>(self); \
VALUE propObj; \
PropKlass *prop; \
rb_get_args(argc, argv, "o", &propObj); \
prop = getPrivateDataCheck<PropKlass>(propObj, PropKlass##Type); \
GUARD_EXC( k->set##PropName(prop); ) \
rb_iv_set(self, prop_iv, propObj); \
return propObj; \
}
/* Object property with allowed NIL */
#define DEF_PROP_OBJ_NIL(Klass, PropKlass, PropName, prop_iv) \
RB_METHOD(Klass##Get##PropName) \
{ \
RB_UNUSED_PARAM; \
Klass *k = getPrivateData<Klass>(self); (void) k; \
DEF_PROP_CHK_DISP \
return rb_iv_get(self, prop_iv); \
} \
RB_METHOD(Klass##Set##PropName) \
{ \
RB_UNUSED_PARAM; \
Klass *k = getPrivateData<Klass>(self); \
VALUE propObj; \
PropKlass *prop; \
rb_get_args(argc, argv, "o", &propObj); \
if (rb_type(propObj) == RUBY_T_NIL) \
prop = 0; \
else \
prop = getPrivateDataCheck<PropKlass>(propObj, PropKlass##Type); \
GUARD_EXC( k->set##PropName(prop); ) \
rb_iv_set(self, prop_iv, propObj); \
return propObj; \
}
#define DEF_PROP(Klass, type, PropName, param_t_s, value_fun) \
RB_METHOD(Klass##Get##PropName) \
{ \
RB_UNUSED_PARAM; \
Klass *k = getPrivateData<Klass>(self); \
DEF_PROP_CHK_DISP \
return value_fun(k->get##PropName()); \
} \
RB_METHOD(Klass##Set##PropName) \
{ \
Klass *k = getPrivateData<Klass>(self); \
type value; \
rb_get_args(argc, argv, param_t_s, &value); \
GUARD_EXC( k->set##PropName(value); ) \
return value_fun(value); \
}
#define DEF_PROP_I(Klass, PropName) \
DEF_PROP(Klass, int, PropName, "i", rb_fix_new)
#define DEF_PROP_F(Klass, PropName) \
DEF_PROP(Klass, double, PropName, "f", rb_float_new)
#define DEF_PROP_B(Klass, PropName) \
DEF_PROP(Klass, bool, PropName, "b", rb_bool_new)
#define INIT_PROP_BIND(Klass, PropName, prop_name_s) \
{ \
_rb_define_method(klass, prop_name_s, Klass##Get##PropName); \
_rb_define_method(klass, prop_name_s "=", Klass##Set##PropName); \
}
#define GUARD_EXC(exp) \
{ try { exp } catch (const Exception &exc) { raiseRbExc(exc); } }
#endif // BINDING_UTIL_H

View file

@ -0,0 +1,324 @@
/*
** bitmap-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "bitmap.h"
#include "font.h"
#include "exception.h"
#include "disposable-binding.h"
#include "binding-util.h"
#include "binding-types.h"
#include <QDebug>
#define DISP_CLASS_NAME "bitmap"
DEF_TYPE(Bitmap);
RB_METHOD(bitmapInitialize)
{
Bitmap *b = 0;
if (argc == 1)
{
char *filename;
rb_get_args(argc, argv, "z", &filename);
GUARD_EXC( b = new Bitmap(filename); )
}
else
{
int width, height;
rb_get_args(argc, argv, "ii", &width, &height);
b = new Bitmap(width, height);
}
setPrivateData(self, b, BitmapType);
/* Wrap properties */
Font *font = new Font();
b->setFont(font);
font->setColor(new Color(*font->getColor()));
VALUE fontProp = wrapProperty(self, font, "font", FontType);
wrapProperty(fontProp, font->getColor(), "color", ColorType);
return self;
}
RB_METHOD(bitmapWidth)
{
RB_UNUSED_PARAM;
Bitmap *b = getPrivateData<Bitmap>(self);
int value = 0;
GUARD_EXC( value = b->width(); );
return INT2FIX(value);
}
RB_METHOD(bitmapHeight)
{
RB_UNUSED_PARAM;
Bitmap *b = getPrivateData<Bitmap>(self);
int value = 0;
GUARD_EXC( value = b->height(); );
return INT2FIX(value);
}
RB_METHOD(bitmapRect)
{
RB_UNUSED_PARAM;
Bitmap *b = getPrivateData<Bitmap>(self);
IntRect rect;
GUARD_EXC( rect = b->rect(); );
Rect *r = new Rect(rect);
return wrapObject(r, RectType);
}
RB_METHOD(bitmapBlt)
{
Bitmap *b = getPrivateData<Bitmap>(self);
int x, y;
VALUE srcObj;
VALUE srcRectObj;
int opacity = 255;
Bitmap *src;
Rect *srcRect;
rb_get_args(argc, argv, "iioo|i", &x, &y, &srcObj, &srcRectObj, &opacity);
src = getPrivateDataCheck<Bitmap>(srcObj, BitmapType);
srcRect = getPrivateDataCheck<Rect>(srcRectObj, RectType);
GUARD_EXC( b->blt(x, y, *src, srcRect->toIntRect(), opacity); );
return self;
}
RB_METHOD(bitmapStretchBlt)
{
Bitmap *b = getPrivateData<Bitmap>(self);
VALUE destRectObj;
VALUE srcObj;
VALUE srcRectObj;
int opacity = 255;
Bitmap *src;
Rect *destRect, *srcRect;
rb_get_args(argc, argv, "ooo|i", &destRectObj, &srcObj, &srcRectObj);
src = getPrivateDataCheck<Bitmap>(srcObj, BitmapType);
destRect = getPrivateDataCheck<Rect>(destRectObj, RectType);
srcRect = getPrivateDataCheck<Rect>(srcRectObj, RectType);
GUARD_EXC( b->stretchBlt(destRect->toIntRect(), *src, srcRect->toIntRect(), opacity); );
return self;
}
RB_METHOD(bitmapFillRect)
{
Bitmap *b = getPrivateData<Bitmap>(self);
VALUE colorObj;
Color *color;
if (argc == 2)
{
VALUE rectObj;
Rect *rect;
rb_get_args(argc, argv, "oo", &rectObj, &colorObj);
rect = getPrivateDataCheck<Rect>(rectObj, RectType);
color = getPrivateDataCheck<Color>(colorObj, ColorType);
GUARD_EXC( b->fillRect(rect->toIntRect(), color->norm); );
}
else
{
int x, y, width, height;
rb_get_args(argc, argv, "iiiio", &x, &y, &width, &height, &colorObj);
color = getPrivateDataCheck<Color>(colorObj, ColorType);
GUARD_EXC( b->fillRect(x, y, width, height, color->norm); );
}
return self;
}
RB_METHOD(bitmapClear)
{
RB_UNUSED_PARAM;
Bitmap *b = getPrivateData<Bitmap>(self);
GUARD_EXC( b->clear(); )
return self;
}
RB_METHOD(bitmapGetPixel)
{
Bitmap *b = getPrivateData<Bitmap>(self);
int x, y;
rb_get_args(argc, argv, "ii", &x, &y);
GUARD_EXC(
if (x < 0 || y < 0 || x >= b->width() || y >= b->height())
return Qnil;
)
Vec4 value;
GUARD_EXC( value = b->getPixel(x, y); );
Color *color = new Color(value);
return wrapObject(color, ColorType);
}
RB_METHOD(bitmapSetPixel)
{
Bitmap *b = getPrivateData<Bitmap>(self);
int x, y;
VALUE colorObj;
Color *color;
rb_get_args(argc, argv, "iio", &x, &y, &colorObj);
color = getPrivateDataCheck<Color>(colorObj, ColorType);
GUARD_EXC( b->setPixel(x, y, color->norm); );
return self;
}
RB_METHOD(bitmapHueChange)
{
Bitmap *b = getPrivateData<Bitmap>(self);
int hue;
rb_get_args(argc, argv, "i", &hue);
GUARD_EXC( b->hueChange(hue); );
return self;
}
RB_METHOD(bitmapDrawText)
{
Bitmap *b = getPrivateData<Bitmap>(self);
const char *str;
int align = Bitmap::Left;
if (argc == 2 || argc == 3)
{
VALUE rectObj;
Rect *rect;
rb_get_args(argc, argv, "oz|i", &rectObj, &str, &align);
rect = getPrivateDataCheck<Rect>(rectObj, RectType);
GUARD_EXC( b->drawText(rect->toIntRect(), str, align); );
}
else
{
int x, y, width, height;
rb_get_args(argc, argv, "iiiiz|i", &x, &y, &width, &height, &str, &align);
GUARD_EXC( b->drawText(x, y, width, height, str, align); );
}
return self;
}
RB_METHOD(bitmapTextSize)
{
Bitmap *b = getPrivateData<Bitmap>(self);
const char *str;
rb_get_args(argc, argv, "z", &str);
IntRect value;
GUARD_EXC( value = b->textSize(str); );
Rect *rect = new Rect(value);
return wrapObject(rect, RectType);
}
DEF_PROP_OBJ(Bitmap, Font, Font, "font")
CLONE_FUN(Bitmap)
void
bitmapBindingInit()
{
INIT_TYPE(Bitmap);
VALUE klass = rb_define_class("Bitmap", rb_cObject);
disposableBindingInit<Bitmap>(klass);
_rb_define_method(klass, "initialize", bitmapInitialize);
_rb_define_method(klass, "width", bitmapWidth);
_rb_define_method(klass, "height", bitmapHeight);
_rb_define_method(klass, "rect", bitmapRect);
_rb_define_method(klass, "blt", bitmapBlt);
_rb_define_method(klass, "stretch_blt", bitmapStretchBlt);
_rb_define_method(klass, "fill_rect", bitmapFillRect);
_rb_define_method(klass, "clear", bitmapClear);
_rb_define_method(klass, "get_pixel", bitmapGetPixel);
_rb_define_method(klass, "set_pixel", bitmapSetPixel);
_rb_define_method(klass, "hue_change", bitmapHueChange);
_rb_define_method(klass, "draw_text", bitmapDrawText);
_rb_define_method(klass, "text_size", bitmapTextSize);
INIT_PROP_BIND(Bitmap, Font, "font");
_rb_define_method(klass, "clone", BitmapClone);
}

View file

@ -0,0 +1,65 @@
/*
** disposable-binding.h
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DISPOSABLEBINDING_H
#define DISPOSABLEBINDING_H
#include "disposable.h"
#include "binding-util.h"
template<class C>
RB_METHOD(disposableDispose)
{
RB_UNUSED_PARAM;
Disposable *d = getPrivateData<C>(self);
d->dispose();
return Qnil;
}
template<class C>
RB_METHOD(disposableIsDisposed)
{
RB_UNUSED_PARAM;
Disposable *d = getPrivateData<C>(self);
return rb_bool_new(d->isDisposed());
}
template<class C>
static void disposableBindingInit(VALUE klass)
{
_rb_define_method(klass, "dispose", disposableDispose<C>);
_rb_define_method(klass, "disposed?", disposableIsDisposed<C>);
}
inline void checkDisposed(Disposable *d, const char *klassName)
{
RbData *data = getRbData(); (void) data;
if (d->isDisposed())
rb_raise(getRbData()->exc[RGSS], "disposed %s", klassName);
}
#endif // DISPOSABLEBINDING_H

204
binding-mri/etc-binding.cpp Normal file
View file

@ -0,0 +1,204 @@
/*
** etc-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "etc.h"
#include "binding-util.h"
#include "serializable-binding.h"
#include <QDebug>
DEF_TYPE(Color);
DEF_TYPE(Tone);
DEF_TYPE(Rect);
#define ATTR_RW(Klass, Attr, arg_type, arg_t_s, value_fun) \
RB_METHOD(Klass##Get##Attr) \
{ \
RB_UNUSED_PARAM \
Klass *p = getPrivateData<Klass>(self); \
return value_fun(p->get##Attr()); \
} \
RB_METHOD(Klass##Set##Attr) \
{ \
Klass *p = getPrivateData<Klass>(self); \
arg_type arg; \
rb_get_args(argc, argv, arg_t_s, &arg); \
p->set##Attr(arg); \
return *argv; \
}
#define ATTR_DOUBLE_RW(Klass, Attr) ATTR_RW(Klass, Attr, double, "f", rb_float_new)
#define ATTR_INT_RW(Klass, Attr) ATTR_RW(Klass, Attr, int, "i", rb_fix_new)
ATTR_DOUBLE_RW(Color, Red)
ATTR_DOUBLE_RW(Color, Green)
ATTR_DOUBLE_RW(Color, Blue)
ATTR_DOUBLE_RW(Color, Alpha)
ATTR_DOUBLE_RW(Tone, Red)
ATTR_DOUBLE_RW(Tone, Green)
ATTR_DOUBLE_RW(Tone, Blue)
ATTR_DOUBLE_RW(Tone, Gray)
ATTR_INT_RW(Rect, X)
ATTR_INT_RW(Rect, Y)
ATTR_INT_RW(Rect, Width)
ATTR_INT_RW(Rect, Height)
#define EQUAL_FUN(Klass) \
RB_METHOD(Klass##Equal) \
{ \
Klass *p = getPrivateData<Klass>(self); \
VALUE otherObj; \
Klass *other; \
rb_get_args(argc, argv, "o", &otherObj); \
other = getPrivateDataCheck<Klass>(otherObj, Klass##Type); \
return rb_bool_new(*p == *other); \
}
EQUAL_FUN(Color)
EQUAL_FUN(Tone)
EQUAL_FUN(Rect)
#define INIT_FUN(Klass, param_type, param_t_s, last_param_def) \
RB_METHOD(Klass##Initialize) \
{ \
param_type p1, p2, p3, p4 = last_param_def; \
rb_get_args(argc, argv, param_t_s, &p1, &p2, &p3, &p4); \
Klass *k = new Klass(p1, p2, p3, p4); \
setPrivateData(self, k, Klass##Type); \
return self; \
}
INIT_FUN(Color, double, "fff|f", 255)
INIT_FUN(Tone, double, "fff|f", 0)
INIT_FUN(Rect, int, "iiii", 0)
#define SET_FUN(Klass, param_type, param_t_s, last_param_def) \
RB_METHOD(Klass##Set) \
{ \
param_type p1, p2, p3, p4 = last_param_def; \
rb_get_args(argc, argv, param_t_s, &p1, &p2, &p3, &p4); \
Klass *k = getPrivateData<Klass>(self); \
k->set(p1, p2, p3, p4); \
return self; \
}
SET_FUN(Color, double, "fff|f", 255)
SET_FUN(Tone, double, "fff|f", 0)
SET_FUN(Rect, int, "iiii", 0)
RB_METHOD(rectEmpty)
{
RB_UNUSED_PARAM;
Rect *r = getPrivateData<Rect>(self);
r->empty();
return self;
}
RB_METHOD(ColorStringify)
{
RB_UNUSED_PARAM;
Color *c = getPrivateData<Color>(self);
return rb_sprintf("(%f, %f, %f, %f)",
c->red, c->green, c->blue, c->alpha);
}
RB_METHOD(ToneStringify)
{
RB_UNUSED_PARAM;
Tone *t = getPrivateData<Tone>(self);
return rb_sprintf("(%f, %f, %f, %f)",
t->red, t->green, t->blue, t->gray);
}
RB_METHOD(RectStringify)
{
RB_UNUSED_PARAM;
Rect *r = getPrivateData<Rect>(self);
return rb_sprintf("(%d, %d, %d, %d)",
r->x, r->y, r->width, r->height);
}
MARSH_LOAD_FUN(Color)
MARSH_LOAD_FUN(Tone)
MARSH_LOAD_FUN(Rect)
CLONE_FUN(Tone)
CLONE_FUN(Color)
CLONE_FUN(Rect)
#define INIT_BIND(Klass) \
{ \
INIT_TYPE(Klass); \
klass = rb_define_class(#Klass, rb_cObject); \
rb_define_class_method(klass, "_load", Klass##Load); \
serializableBindingInit<Klass>(klass); \
_rb_define_method(klass, "initialize", Klass##Initialize); \
_rb_define_method(klass, "set", Klass##Set); \
_rb_define_method(klass, "clone", Klass##Clone); \
_rb_define_method(klass, "==", Klass##Equal); \
_rb_define_method(klass, "to_s", Klass##Stringify); \
_rb_define_method(klass, "inspect", Klass##Stringify); \
}
#define MRB_ATTR_R(Class, attr) mrb_define_method(mrb, klass, #attr, Class##Get_##attr, MRB_ARGS_NONE())
#define MRB_ATTR_W(Class, attr) mrb_define_method(mrb, klass, #attr "=", Class##Set_##attr, MRB_ARGS_REQ(1))
#define MRB_ATTR_RW(Class, attr) { MRB_ATTR_R(Class, attr); MRB_ATTR_W(Class, attr); }
#define RB_ATTR_R(Klass, Attr, attr) _rb_define_method(klass, #attr, Klass##Get##Attr)
#define RB_ATTR_W(Klass, Attr, attr) _rb_define_method(klass, #attr "=", Klass##Set##Attr)
#define RB_ATTR_RW(Klass, Attr, attr) \
{ RB_ATTR_R(Klass, Attr, attr); RB_ATTR_W(Klass, Attr, attr); }
void
etcBindingInit()
{
VALUE klass;
INIT_BIND(Color);
RB_ATTR_RW(Color, Red, red);
RB_ATTR_RW(Color, Green, green);
RB_ATTR_RW(Color, Blue, blue);
RB_ATTR_RW(Color, Alpha, alpha);
INIT_BIND(Tone);
RB_ATTR_RW(Tone, Red, red);
RB_ATTR_RW(Tone, Green, green);
RB_ATTR_RW(Tone, Blue, blue);
RB_ATTR_RW(Tone, Gray, gray);
INIT_BIND(Rect);
RB_ATTR_RW(Rect, X, x);
RB_ATTR_RW(Rect, Y, y);
RB_ATTR_RW(Rect, Width, width);
RB_ATTR_RW(Rect, Height, height);
_rb_define_method(klass, "empty", rectEmpty);
}

View file

@ -0,0 +1,173 @@
/*
** filesystem-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "binding-util.h"
#include "globalstate.h"
#include "filesystem.h"
#include <QDebug>
DEF_TYPE(FileInt);
static VALUE
fileIntForPath(const char *path)
{
SDL_RWops *ops = SDL_AllocRW();
gState->fileSystem().openRead(*ops, path);
VALUE klass = rb_const_get(rb_cObject, rb_intern("FileInt"));
VALUE obj = rb_obj_alloc(klass);
setPrivateData(obj, ops, FileIntType);
return obj;
}
RB_METHOD(fileIntRead)
{
int length = -1;
rb_get_args(argc, argv, "i", &length);
SDL_RWops *ops = getPrivateData<SDL_RWops>(self);
if (length == -1)
{
Sint64 cur = SDL_RWtell(ops);
Sint64 end = SDL_RWseek(ops, 0, SEEK_END);
length = end - cur;
SDL_RWseek(ops, cur, SEEK_SET);
}
if (length == 0)
return Qnil;
VALUE data = rb_str_new(0, length);
SDL_RWread(ops, RSTRING_PTR(data), 1, length);
return data;
}
RB_METHOD(fileIntClose)
{
RB_UNUSED_PARAM;
SDL_RWops *ops = getPrivateData<SDL_RWops>(self);
SDL_RWclose(ops);
return Qnil;
}
RB_METHOD(fileIntGetByte)
{
RB_UNUSED_PARAM;
SDL_RWops *ops = getPrivateData<SDL_RWops>(self);
unsigned char byte;
size_t result = SDL_RWread(ops, &byte, 1, 1);
return (result == 1) ? rb_fix_new(byte) : Qnil;
}
RB_METHOD(fileIntBinmode)
{
RB_UNUSED_PARAM;
return Qnil;
}
static void
fileIntFreeInstance(void *inst)
{
SDL_RWops *ops = static_cast<SDL_RWops*>(inst);
SDL_RWclose(ops);
SDL_FreeRW(ops);
}
VALUE
kernelLoadDataInt(const char *filename)
{
rb_gc_start();
VALUE port = fileIntForPath(filename);
VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal"));
VALUE result = rb_funcall(marsh, rb_intern("load"), 1, port);
rb_funcall(port, rb_intern("close"), 0);
return result;
}
RB_METHOD(kernelLoadData)
{
RB_UNUSED_PARAM;
const char *filename;
rb_get_args(argc, argv, "z", &filename);
return kernelLoadDataInt(filename);
}
RB_METHOD(kernelSaveData)
{
RB_UNUSED_PARAM;
VALUE obj;
VALUE filename;
rb_get_args(argc, argv, "oS", &obj, &filename);
VALUE file = rb_funcall(rb_cFile, rb_intern("open"), 2, filename, rb_str_new_cstr("w"));
VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal"));
rb_funcall(marsh, rb_intern("dump"), 2, obj, file);
rb_funcall(file, rb_intern("close"), 0);
return Qnil;
}
void
fileIntBindingInit()
{
initType(FileIntType, "FileInt", fileIntFreeInstance);
VALUE klass = rb_define_class("FileInt", rb_cIO);
_rb_define_method(klass, "read", fileIntRead);
// _rb_define_method(klass, "eof?", fileIntIsEof);
// _rb_define_method(klass, "pos", fileIntGetPos);
// _rb_define_method(klass, "pos=", fileIntSetPos);
_rb_define_method(klass, "getbyte", fileIntGetByte);
_rb_define_method(klass, "binmode", fileIntBinmode);
_rb_define_method(klass, "close", fileIntClose);
_rb_define_module_function(rb_mKernel, "load_data", kernelLoadData);
_rb_define_module_function(rb_mKernel, "save_data", kernelSaveData);
}

View file

@ -0,0 +1,73 @@
/*
** flashable-binding.h
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FLASHABLEBINDING_H
#define FLASHABLEBINDING_H
#include "flashable.h"
#include "binding-util.h"
#include "binding-types.h"
template<class C>
RB_METHOD(flashableFlash)
{
Flashable *f = getPrivateData<C>(self);
VALUE colorObj;
int duration;
Color *color;
rb_get_args(argc, argv, "oi", &colorObj, &duration);
if (rb_type(colorObj) == RUBY_T_NIL)
{
f->flash(0, duration);
return Qnil;
}
color = getPrivateDataCheck<Color>(colorObj, ColorType);
f->flash(&color->norm, duration);
return Qnil;
}
template<class C>
RB_METHOD(flashableUpdate)
{
RB_UNUSED_PARAM;
Flashable *f = getPrivateData<C>(self);
f->update();
return Qnil;
}
template<class C>
static void flashableBindingInit(VALUE klass)
{
_rb_define_method(klass, "flash", flashableFlash<C>);
_rb_define_method(klass, "update", flashableUpdate<C>);
}
#endif // FLASHABLEBINDING_H

View file

@ -0,0 +1,173 @@
/*
** font-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "font.h"
#include "binding-util.h"
#include "binding-types.h"
#include "exception.h"
DEF_TYPE(Font);
RB_METHOD(fontDoesExist)
{
RB_UNUSED_PARAM;
const char *name;
rb_get_args(argc, argv, "z", &name);
return rb_bool_new(Font::doesExist(name));
}
RB_METHOD(fontInitialize)
{
const char *name = 0;
int size = 0;
rb_get_args(argc, argv, "|zi", &name, &size);
Font *f = new Font(name, size);
setPrivateData(self, f, FontType);
/* Wrap property objects */
f->setColor(new Color(*f->getColor()));
wrapProperty(self, f->getColor(), "color", ColorType);
return self;
}
RB_METHOD(FontGetName)
{
RB_UNUSED_PARAM;
Font *f = getPrivateData<Font>(self);
return rb_str_new_cstr(f->getName());
}
RB_METHOD(FontSetName)
{
Font *f = getPrivateData<Font>(self);
VALUE name;
rb_get_args(argc, argv, "S", &name);
f->setName(RSTRING_PTR(name));
return name;
}
#undef DEF_PROP_CHK_DISP
#define DEF_PROP_CHK_DISP
DEF_PROP_I(Font, Size)
DEF_PROP_B(Font, Bold)
DEF_PROP_B(Font, Italic)
DEF_PROP_OBJ(Font, Color, Color, "color")
#define DEF_KLASS_PROP(Klass, type, PropName, param_t_s, value_fun) \
RB_METHOD(Klass##Get##PropName) \
{ \
RB_UNUSED_PARAM; \
return value_fun(Klass::get##PropName()); \
} \
RB_METHOD(Klass##Set##PropName) \
{ \
RB_UNUSED_PARAM; \
type value; \
rb_get_args(argc, argv, param_t_s, &value); \
Klass::set##PropName(value); \
return value_fun(value); \
}
DEF_KLASS_PROP(Font, int, DefaultSize, "i", rb_fix_new)
DEF_KLASS_PROP(Font, bool, DefaultBold, "b", rb_bool_new)
DEF_KLASS_PROP(Font, bool, DefaultItalic, "b", rb_bool_new)
RB_METHOD(FontGetDefaultName)
{
RB_UNUSED_PARAM;
return rb_str_new_cstr(Font::getDefaultName());
}
RB_METHOD(FontSetDefaultName)
{
RB_UNUSED_PARAM;
VALUE nameObj;
rb_get_args(argc, argv, "S", &nameObj);
Font::setDefaultName(RSTRING_PTR(nameObj));
return nameObj;
}
RB_METHOD(FontGetDefaultColor)
{
RB_UNUSED_PARAM;
return rb_iv_get(self, "default_color");
}
RB_METHOD(FontSetDefaultColor)
{
VALUE colorObj;
rb_get_args(argc, argv, "o", &colorObj);
Color *c = getPrivateDataCheck<Color>(colorObj, ColorType);
Font::setDefaultColor(c);
rb_iv_set(self, "default_color", colorObj);
return colorObj;
}
#define INIT_KLASS_PROP_BIND(Klass, PropName, prop_name_s) \
{ \
rb_define_class_method(klass, prop_name_s, Klass##Get##PropName); \
rb_define_class_method(klass, prop_name_s "=", Klass##Set##PropName); \
}
void
fontBindingInit()
{
INIT_TYPE(Font);
VALUE klass = rb_define_class("Font", rb_cObject);
Font::setDefaultColor(new Color(*Font::getDefaultColor()));
wrapProperty(klass, Font::getDefaultColor(), "default_color", ColorType);
INIT_KLASS_PROP_BIND(Font, DefaultName, "default_name");
INIT_KLASS_PROP_BIND(Font, DefaultSize, "default_size");
INIT_KLASS_PROP_BIND(Font, DefaultBold, "default_bold");
INIT_KLASS_PROP_BIND(Font, DefaultItalic, "default_italic");
INIT_KLASS_PROP_BIND(Font, DefaultColor, "default_color");
rb_define_class_method(klass, "exist?", fontDoesExist);
_rb_define_method(klass, "initialize", fontInitialize);
INIT_PROP_BIND(Font, Name, "name");
INIT_PROP_BIND(Font, Size, "size");
INIT_PROP_BIND(Font, Bold, "bold");
INIT_PROP_BIND(Font, Italic, "italic");
INIT_PROP_BIND(Font, Color, "color");
}

View file

@ -0,0 +1,124 @@
/*
** graphics-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "graphics.h"
#include "globalstate.h"
#include "binding-util.h"
#include "exception.h"
RB_METHOD(graphicsUpdate)
{
RB_UNUSED_PARAM;
gState->graphics().update();
return Qnil;
}
RB_METHOD(graphicsFreeze)
{
RB_UNUSED_PARAM;
gState->graphics().freeze();
return Qnil;
}
RB_METHOD(graphicsTransition)
{
RB_UNUSED_PARAM;
int duration = 8;
const char *filename = 0;
int vague = 40;
rb_get_args(argc, argv, "|izi", &duration, &filename, &vague);
GUARD_EXC( gState->graphics().transition(duration, filename, vague); )
return Qnil;
}
RB_METHOD(graphicsFrameReset)
{
RB_UNUSED_PARAM;
gState->graphics().frameReset();
return Qnil;
}
#define DEF_GRA_PROP_I(PropName) \
RB_METHOD(graphics##Get##PropName) \
{ \
RB_UNUSED_PARAM; \
return rb_fix_new(gState->graphics().get##PropName()); \
} \
RB_METHOD(graphics##Set##PropName) \
{ \
RB_UNUSED_PARAM; \
int value; \
rb_get_args(argc, argv, "i", &value); \
gState->graphics().set##PropName(value); \
return rb_fix_new(value); \
}
DEF_GRA_PROP_I(FrameRate)
DEF_GRA_PROP_I(FrameCount)
RB_METHOD(graphicsGetFullscreen)
{
RB_UNUSED_PARAM;
return rb_bool_new(gState->graphics().getFullscreen());
}
RB_METHOD(graphicsSetFullscreen)
{
RB_UNUSED_PARAM;
bool mode;
rb_get_args(argc, argv, "b", &mode);
gState->graphics().setFullscreen(mode);
return rb_bool_new(mode);
}
#define INIT_GRA_PROP_BIND(PropName, prop_name_s) \
{ \
_rb_define_module_function(module, prop_name_s, graphics##Get##PropName); \
_rb_define_module_function(module, prop_name_s "=", graphics##Set##PropName); \
}
void graphicsBindingInit()
{
VALUE module = rb_define_module("Graphics");
_rb_define_module_function(module, "update", graphicsUpdate);
_rb_define_module_function(module, "freeze", graphicsFreeze);
_rb_define_module_function(module, "transition", graphicsTransition);
_rb_define_module_function(module, "frame_reset", graphicsFrameReset);
INIT_GRA_PROP_BIND( FrameRate, "frame_rate" );
INIT_GRA_PROP_BIND( FrameCount, "frame_count" );
INIT_GRA_PROP_BIND( Fullscreen, "fullscreen" );
}

View file

@ -0,0 +1,146 @@
/*
** input-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "input.h"
#include "globalstate.h"
#include "exception.h"
#include "binding-util.h"
RB_METHOD(inputUpdate)
{
RB_UNUSED_PARAM;
gState->input().update();
return Qnil;
}
RB_METHOD(inputPress)
{
RB_UNUSED_PARAM;
int num;
rb_get_args(argc, argv, "i", &num);
Input::ButtonCode bc = (Input::ButtonCode) num;
return rb_bool_new(gState->input().isPressed(bc));
}
RB_METHOD(inputTrigger)
{
RB_UNUSED_PARAM;
int num;
rb_get_args(argc, argv, "i", &num);
Input::ButtonCode bc = (Input::ButtonCode) num;
return rb_bool_new(gState->input().isTriggered(bc));
}
RB_METHOD(inputRepeat)
{
RB_UNUSED_PARAM;
int num;
rb_get_args(argc, argv, "i", &num);
Input::ButtonCode bc = (Input::ButtonCode) num;
return rb_bool_new(gState->input().isRepeated(bc));
}
RB_METHOD(inputDir4)
{
RB_UNUSED_PARAM;
return rb_fix_new(gState->input().dir4Value());
}
RB_METHOD(inputDir8)
{
RB_UNUSED_PARAM;
return rb_fix_new(gState->input().dir8Value());
}
/* Non-standard extensions */
RB_METHOD(inputMouseX)
{
RB_UNUSED_PARAM;
return rb_fix_new(gState->input().mouseX());
}
RB_METHOD(inputMouseY)
{
RB_UNUSED_PARAM;
return rb_fix_new(gState->input().mouseY());
}
#define DEF_CONST_I(name, value) \
rb_const_set(module, rb_intern(name), rb_fix_new(value))
void
inputBindingInit()
{
VALUE module = rb_define_module("Input");
_rb_define_module_function(module, "update", inputUpdate);
_rb_define_module_function(module, "press?", inputPress);
_rb_define_module_function(module, "trigger?", inputTrigger);
_rb_define_module_function(module, "repeat?", inputRepeat);
_rb_define_module_function(module, "dir4", inputDir4);
_rb_define_module_function(module, "dir8", inputDir8);
DEF_CONST_I("DOWN", Input::Down );
DEF_CONST_I("LEFT", Input::Left );
DEF_CONST_I("RIGHT", Input::Right);
DEF_CONST_I("UP", Input::Up );
DEF_CONST_I("A", Input::A );
DEF_CONST_I("B", Input::B );
DEF_CONST_I("C", Input::C );
DEF_CONST_I("X", Input::X );
DEF_CONST_I("Y", Input::Y );
DEF_CONST_I("Z", Input::Z );
DEF_CONST_I("L", Input::L );
DEF_CONST_I("R", Input::R );
DEF_CONST_I("SHIFT", Input::Shift);
DEF_CONST_I("CTRL", Input::Ctrl );
DEF_CONST_I("ALT", Input::Alt );
DEF_CONST_I("F5", Input::F5 );
DEF_CONST_I("F6", Input::F6 );
DEF_CONST_I("F7", Input::F7 );
DEF_CONST_I("F8", Input::F8 );
DEF_CONST_I("F9", Input::F9 );
_rb_define_module_function(module, "mouse_x", inputMouseX);
_rb_define_module_function(module, "mouse_y", inputMouseY);
DEF_CONST_I("MOUSELEFT", Input::MouseLeft );
DEF_CONST_I("MOUSEMIDDLE", Input::MouseMiddle);
DEF_CONST_I("MOUSERIGHT", Input::MouseRight );
}

View file

@ -0,0 +1 @@
#include "module_rpg.rb.xxd"

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,82 @@
/*
** plane-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "plane.h"
#include "disposable-binding.h"
#include "viewportelement-binding.h"
#include "binding-util.h"
#include "binding-types.h"
DEF_TYPE(Plane);
RB_METHOD(planeInitialize)
{
Plane *p = viewportElementInitialize<Plane>(argc, argv, self);
setPrivateData(self, p, PlaneType);
p->setColor(new Color);
p->setTone(new Tone);
wrapNilProperty(self, "bitmap");
wrapProperty(self, p->getColor(), "color", ColorType);
wrapProperty(self, p->getTone(), "tone", ToneType);
return self;
}
#define DISP_CLASS_NAME "plane"
DEF_PROP_OBJ(Plane, Bitmap, Bitmap, "bitmap")
DEF_PROP_OBJ(Plane, Color, Color, "color")
DEF_PROP_OBJ(Plane, Tone, Tone, "tone")
DEF_PROP_I(Plane, OX)
DEF_PROP_I(Plane, OY)
DEF_PROP_I(Plane, Opacity)
DEF_PROP_I(Plane, BlendType)
DEF_PROP_F(Plane, ZoomX)
DEF_PROP_F(Plane, ZoomY)
void
planeBindingInit()
{
INIT_TYPE(Plane);
VALUE klass = rb_define_class("Plane", rb_cObject);
disposableBindingInit<Plane> (klass);
viewportElementBindingInit<Plane>(klass);
_rb_define_method(klass, "initialize", planeInitialize);
INIT_PROP_BIND( Plane, Bitmap, "bitmap" );
INIT_PROP_BIND( Plane, OX, "ox" );
INIT_PROP_BIND( Plane, OY, "oy" );
INIT_PROP_BIND( Plane, ZoomX, "zoom_x" );
INIT_PROP_BIND( Plane, ZoomY, "zoom_y" );
INIT_PROP_BIND( Plane, Opacity, "opacity" );
INIT_PROP_BIND( Plane, BlendType, "blend_type" );
INIT_PROP_BIND( Plane, Color, "color" );
INIT_PROP_BIND( Plane, Tone, "tone" );
}

View file

@ -0,0 +1,90 @@
/*
** sceneelement-binding.h
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SCENEELEMENTBINDING_H
#define SCENEELEMENTBINDING_H
#include "scene.h"
#include "binding-util.h"
template<class C>
RB_METHOD(sceneElementGetZ)
{
RB_UNUSED_PARAM;
SceneElement *se = getPrivateData<C>(self);
int value = 0;
GUARD_EXC( value = se->getZ(); );
return rb_fix_new(value);
}
template<class C>
RB_METHOD(sceneElementSetZ)
{
SceneElement *se = getPrivateData<C>(self);
int z;
rb_get_args(argc, argv, "i", &z);
GUARD_EXC( se->setZ(z); );
return rb_fix_new(z);
}
template<class C>
RB_METHOD(sceneElementGetVisible)
{
RB_UNUSED_PARAM;
SceneElement *se = getPrivateData<C>(self);
bool value = false;
GUARD_EXC( value = se->getVisible(); );
return rb_bool_new(value);
}
template<class C>
RB_METHOD(sceneElementSetVisible)
{
SceneElement *se = getPrivateData<C>(self);
bool visible;
rb_get_args(argc, argv, "b", &visible);
GUARD_EXC( se->setVisible(visible); );
return rb_bool_new(visible);
}
template<class C>
void
sceneElementBindingInit(VALUE klass)
{
_rb_define_method(klass, "z", sceneElementGetZ<C>);
_rb_define_method(klass, "z=", sceneElementSetZ<C>);
_rb_define_method(klass, "visible", sceneElementGetVisible<C>);
_rb_define_method(klass, "visible=", sceneElementSetVisible<C>);
}
#endif // SCENEELEMENTBINDING_H

View file

@ -0,0 +1,51 @@
/*
** serializable-binding.h
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SERIALIZABLEBINDING_H
#define SERIALIZABLEBINDING_H
#include "serializable.h"
#include "binding-util.h"
#include "exception.h"
template<class C>
static VALUE
serializableDump(int, VALUE *, VALUE self)
{
Serializable *s = getPrivateData<C>(self);
int dataSize = s->serialSize();
VALUE data = rb_str_new(0, dataSize);
GUARD_EXC( s->serialize(RSTRING_PTR(data)); );
return data;
}
template<class C>
void
serializableBindingInit(VALUE klass)
{
_rb_define_method(klass, "_dump", serializableDump<C>);
}
#endif // SERIALIZABLEBINDING_H

View file

@ -0,0 +1,100 @@
/*
** sprite-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "sprite.h"
#include "disposable-binding.h"
#include "flashable-binding.h"
#include "sceneelement-binding.h"
#include "viewportelement-binding.h"
#include "binding-util.h"
#include "binding-types.h"
DEF_TYPE(Sprite);
RB_METHOD(spriteInitialize)
{
Sprite *s = viewportElementInitialize<Sprite>(argc, argv, self);
setPrivateData(self, s, SpriteType);
/* Wrap property objects */
s->setSrcRect(new Rect);
s->setColor(new Color);
s->setTone(new Tone);
wrapNilProperty(self, "bitmap");
wrapProperty(self, s->getSrcRect(), "src_rect", RectType);
wrapProperty(self, s->getColor(), "color", ColorType);
wrapProperty(self, s->getTone(), "tone", ToneType);
return self;
}
#define DISP_CLASS_NAME "sprite"
DEF_PROP_OBJ_NIL(Sprite, Bitmap, Bitmap, "bitmap")
DEF_PROP_OBJ(Sprite, Rect, SrcRect, "src_rect")
DEF_PROP_OBJ(Sprite, Color, Color, "color")
DEF_PROP_OBJ(Sprite, Tone, Tone, "tone")
DEF_PROP_I(Sprite, X)
DEF_PROP_I(Sprite, Y)
DEF_PROP_I(Sprite, OX)
DEF_PROP_I(Sprite, OY)
DEF_PROP_I(Sprite, BushDepth)
DEF_PROP_I(Sprite, Opacity)
DEF_PROP_I(Sprite, BlendType)
DEF_PROP_F(Sprite, ZoomX)
DEF_PROP_F(Sprite, ZoomY)
DEF_PROP_F(Sprite, Angle)
DEF_PROP_B(Sprite, Mirror)
void
spriteBindingInit()
{
INIT_TYPE(Sprite);
VALUE klass = rb_define_class("Sprite", rb_cObject);
disposableBindingInit <Sprite>(klass);
flashableBindingInit <Sprite>(klass);
viewportElementBindingInit<Sprite>(klass);
_rb_define_method(klass, "initialize", spriteInitialize);
INIT_PROP_BIND( Sprite, Bitmap, "bitmap" );
INIT_PROP_BIND( Sprite, SrcRect, "src_rect" );
INIT_PROP_BIND( Sprite, X, "x" );
INIT_PROP_BIND( Sprite, Y, "y" );
INIT_PROP_BIND( Sprite, OX, "ox" );
INIT_PROP_BIND( Sprite, OY, "oy" );
INIT_PROP_BIND( Sprite, ZoomX, "zoom_x" );
INIT_PROP_BIND( Sprite, ZoomY, "zoom_y" );
INIT_PROP_BIND( Sprite, Angle, "angle" );
INIT_PROP_BIND( Sprite, Mirror, "mirror" );
INIT_PROP_BIND( Sprite, BushDepth, "bush_depth" );
INIT_PROP_BIND( Sprite, Opacity, "opacity" );
INIT_PROP_BIND( Sprite, BlendType, "blend_type" );
INIT_PROP_BIND( Sprite, Color, "color" );
INIT_PROP_BIND( Sprite, Tone, "tone" );
}

View file

@ -0,0 +1,162 @@
/*
** table-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "table.h"
#include "binding-util.h"
#include "serializable-binding.h"
DEF_TYPE(Table);
RB_METHOD(tableInitialize)
{
int x, y, z;
x = y = z = 1;
rb_get_args(argc, argv, "i|ii", &x, &y, &z);
Table *t = new Table(x, y, z);
setPrivateData(self, t, TableType);
return self;
}
RB_METHOD(tableResize)
{
Table *t = getPrivateData<Table>(self);
switch (argc)
{
default:
case 1:
t->resize(rb_fix2int(argv[0]));
return Qnil;
case 2:
t->resize(rb_fix2int(argv[0]),
rb_fix2int(argv[1]));
return Qnil;
case 3:
t->resize(rb_fix2int(argv[0]),
rb_fix2int(argv[1]),
rb_fix2int(argv[2]));
return Qnil;
}
}
#define TABLE_SIZE(d, D) \
RB_METHOD(table##D##Size) \
{ \
RB_UNUSED_PARAM \
Table *t = getPrivateData<Table>(self); \
return rb_int2inum(t->d##Size()); \
}
TABLE_SIZE(x, X)
TABLE_SIZE(y, Y)
TABLE_SIZE(z, Z)
RB_METHOD(tableGetAt)
{
Table *t = getPrivateData<Table>(self);
int x, y, z;
x = y = z = 0;
x = rb_num2int(argv[0]);
if (argc > 1)
y = rb_num2int(argv[1]);
if (argc > 2)
z = rb_num2int(argv[2]);
if (argc > 3)
rb_raise(rb_eArgError, "wrong number of arguments");
if (x < 0 || x >= t->xSize()
|| y < 0 || y >= t->ySize()
|| z < 0 || z >= t->zSize())
{
return Qnil;
}
int result = t->get(x, y, z);
return rb_int2inum(result);
}
RB_METHOD(tableSetAt)
{
Table *t = getPrivateData<Table>(self);
int x, y, z, value;
x = y = z = 0;
if (argc < 2)
rb_raise(rb_eArgError, "wrong number of arguments");
switch (argc)
{
default:
case 2 :
x = rb_fix2int(argv[0]);
value = rb_fix2int(argv[1]);
break;
case 3 :
x = rb_fix2int(argv[0]);
y = rb_fix2int(argv[1]);
value = rb_fix2int(argv[2]);
break;
case 4 :
x = rb_fix2int(argv[0]);
y = rb_fix2int(argv[1]);
z = rb_fix2int(argv[2]);
value = rb_fix2int(argv[3]);
break;
}
t->set(value, x, y, z);
return rb_int2inum(value);
}
MARSH_LOAD_FUN(Table)
void
tableBindingInit()
{
INIT_TYPE(Table);
VALUE klass = rb_define_class("Table", rb_cObject);
serializableBindingInit<Table>(klass);
rb_define_class_method(klass, "_load", TableLoad);
_rb_define_method(klass, "initialize", tableInitialize);
_rb_define_method(klass, "resize", tableResize);
_rb_define_method(klass, "xsize", tableXSize);
_rb_define_method(klass, "ysize", tableYSize);
_rb_define_method(klass, "zsize", tableZSize);
_rb_define_method(klass, "[]", tableGetAt);
_rb_define_method(klass, "[]=", tableSetAt);
}

View file

@ -0,0 +1,160 @@
/*
** tilemap-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tilemap.h"
#include "viewport.h"
#include "bitmap.h"
#include "table.h"
#include "disposable-binding.h"
#include "binding-util.h"
#include "binding-types.h"
static rb_data_type_struct TilemapAutotilesType;
RB_METHOD(tilemapAutotilesSet)
{
Tilemap::Autotiles *a = getPrivateData<Tilemap::Autotiles>(self);
int i;
VALUE bitmapObj;
rb_get_args(argc, argv, "io", &i, &bitmapObj);
Bitmap *bitmap = getPrivateDataCheck<Bitmap>(bitmapObj, BitmapType);
a->set(i, bitmap);
VALUE ary = rb_iv_get(self, "array");
rb_ary_store(ary, i, bitmapObj);
return self;
}
RB_METHOD(tilemapAutotilesGet)
{
int i;
rb_get_args (argc, argv, "i", &i);
if (i < 0 || i > 6)
return Qnil;
VALUE ary = rb_iv_get(self, "array");
return rb_ary_entry(ary, i);
}
DEF_TYPE(Tilemap);
RB_METHOD(tilemapInitialize)
{
Tilemap *t;
/* Get parameters */
VALUE viewportObj = Qnil;
Viewport *viewport = 0;
rb_get_args(argc, argv, "|o", &viewportObj);
if (rb_type(viewportObj) != RUBY_T_NIL)
viewport = getPrivateDataCheck<Viewport>(viewportObj, ViewportType);
/* Construct object */
t = new Tilemap(viewport);
setPrivateData(self, t, TilemapType);
rb_iv_set(self, "viewport", viewportObj);
wrapProperty(self, &t->getAutotiles(), "autotiles", TilemapAutotilesType);
VALUE autotilesObj = rb_iv_get(self, "autotiles");
VALUE ary = rb_ary_new2(7);
for (int i = 0; i < 7; ++i)
rb_ary_push(ary, Qnil);
rb_iv_set(autotilesObj, "array", ary);
return self;
}
RB_METHOD(tilemapGetAutotiles)
{
RB_UNUSED_PARAM;
return rb_iv_get(self, "autotiles");
}
RB_METHOD(tilemapUpdate)
{
RB_UNUSED_PARAM;
Tilemap *t = getPrivateData<Tilemap>(self);
t->update();
return Qnil;
}
#define DISP_CLASS_NAME "tilemap"
DEF_PROP_OBJ(Tilemap, Viewport, Viewport, "viewport")
DEF_PROP_OBJ(Tilemap, Bitmap, Tileset, "tileset")
DEF_PROP_OBJ(Tilemap, Table, MapData, "map_data")
DEF_PROP_OBJ(Tilemap, Table, FlashData, "flash_data")
DEF_PROP_OBJ(Tilemap, Table, Priorities, "priorities")
DEF_PROP_B(Tilemap, Visible)
DEF_PROP_I(Tilemap, OX)
DEF_PROP_I(Tilemap, OY)
void
tilemapBindingInit()
{
initType(TilemapAutotilesType, "TilemapAutotiles", 0);
VALUE klass = rb_define_class("TilemapAutotiles", rb_cObject);
_rb_define_method(klass, "[]=", tilemapAutotilesSet);
_rb_define_method(klass, "[]", tilemapAutotilesGet);
INIT_TYPE(Tilemap);
klass = rb_define_class("Tilemap", rb_cObject);
disposableBindingInit<Tilemap>(klass);
_rb_define_method(klass, "initialize", tilemapInitialize);
_rb_define_method(klass, "autotiles", tilemapGetAutotiles);
_rb_define_method(klass, "update", tilemapUpdate);
INIT_PROP_BIND( Tilemap, Viewport, "viewport" );
INIT_PROP_BIND( Tilemap, Tileset, "tileset" );
INIT_PROP_BIND( Tilemap, MapData, "map_data" );
INIT_PROP_BIND( Tilemap, FlashData, "flash_data" );
INIT_PROP_BIND( Tilemap, Priorities, "priorities" );
INIT_PROP_BIND( Tilemap, Visible, "visible" );
INIT_PROP_BIND( Tilemap, OX, "ox" );
INIT_PROP_BIND( Tilemap, OY, "oy" );
}

View file

@ -0,0 +1,100 @@
/*
** viewport-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "viewport.h"
#include "disposable-binding.h"
#include "flashable-binding.h"
#include "sceneelement-binding.h"
#include "binding-util.h"
#include "binding-types.h"
DEF_TYPE(Viewport);
RB_METHOD(viewportInitialize)
{
Viewport *v;
if (argc == 1)
{
/* The rect arg is only used to init the viewport,
* and does NOT replace its 'rect' property */
VALUE rectObj;
Rect *rect;
rb_get_args(argc, argv, "o", &rectObj);
rect = getPrivateDataCheck<Rect>(rectObj, RectType);
v = new Viewport(rect);
}
else
{
int x, y, width, height;
rb_get_args(argc, argv, "iiii", &x, &y, &width, &height);
v = new Viewport(x, y, width, height);
}
setPrivateData(self, v, ViewportType);
/* Wrap property objects */
v->setRect(new Rect(*v->getRect()));
v->setColor(new Color);
v->setTone(new Tone);
wrapProperty(self, v->getRect(), "rect", RectType);
wrapProperty(self, v->getColor(), "color", ColorType);
wrapProperty(self, v->getTone(), "tone", ToneType);
return self;
}
#define DISP_CLASS_NAME "viewport"
DEF_PROP_OBJ(Viewport, Rect, Rect, "rect")
DEF_PROP_OBJ(Viewport, Color, Color, "color")
DEF_PROP_OBJ(Viewport, Tone, Tone, "tone")
DEF_PROP_I(Viewport, OX)
DEF_PROP_I(Viewport, OY)
void
viewportBindingInit()
{
INIT_TYPE(Viewport);
VALUE klass = rb_define_class("Viewport", rb_cObject);
disposableBindingInit <Viewport>(klass);
flashableBindingInit <Viewport>(klass);
sceneElementBindingInit<Viewport>(klass);
_rb_define_method(klass, "initialize", viewportInitialize);
INIT_PROP_BIND( Viewport, Rect, "rect" );
INIT_PROP_BIND( Viewport, OX, "ox" );
INIT_PROP_BIND( Viewport, OY, "oy" );
INIT_PROP_BIND( Viewport, Color, "color" );
INIT_PROP_BIND( Viewport, Tone, "tone" );
}

View file

@ -0,0 +1,74 @@
/*
** viewportelement-binding.h
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef VIEWPORTELEMENTBINDING_H
#define VIEWPORTELEMENTBINDING_H
#include "viewport.h"
#include "binding-util.h"
#include "binding-types.h"
#include "sceneelement-binding.h"
template<class C>
RB_METHOD(viewportElementGetViewport)
{
RB_UNUSED_PARAM;
ViewportElement *ve = getPrivateData<C>(self);
GUARD_EXC( ve->aboutToAccess(); );
return rb_iv_get(self, "viewport");
}
template<class C>
static C *
viewportElementInitialize(int argc, VALUE *argv, VALUE self)
{
/* Get parameters */
VALUE viewportObj = Qnil;
Viewport *viewport = 0;
rb_get_args(argc, argv, "|o", &viewportObj);
if (rb_type(viewportObj) != RUBY_T_NIL)
viewport = getPrivateDataCheck<Viewport>(viewportObj, ViewportType);
/* Construct object */
C *ve = new C(viewport);
/* Set property objects */
rb_iv_set(self, "viewport", viewportObj);
return ve;
}
template<class C>
void
viewportElementBindingInit(VALUE klass)
{
sceneElementBindingInit<C>(klass);
_rb_define_method(klass, "viewport", viewportElementGetViewport<C>);
}
#endif // VIEWPORTELEMENTBINDING_H

View file

@ -0,0 +1,103 @@
/*
** window-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "window.h"
#include "disposable-binding.h"
#include "viewportelement-binding.h"
#include "binding-util.h"
DEF_TYPE(Window);
RB_METHOD(windowInitialize)
{
Window *w = viewportElementInitialize<Window>(argc, argv, self);
setPrivateData(self, w, WindowType);
w->setCursorRect(new Rect);
wrapNilProperty(self, "windowskin");
wrapNilProperty(self, "contents");
wrapProperty(self, w->getCursorRect(), "cursor_rect", RectType);
return self;
}
RB_METHOD(windowUpdate)
{
RB_UNUSED_PARAM;
Window *w = getPrivateData<Window>(self);
w->update();
return Qnil;
}
#define DISP_CLASS_NAME "window"
DEF_PROP_OBJ_NIL(Window, Bitmap, Windowskin, "windowskin")
DEF_PROP_OBJ_NIL(Window, Bitmap, Contents, "contents")
DEF_PROP_OBJ(Window, Rect, CursorRect, "cursor_rect")
DEF_PROP_B(Window, Stretch)
DEF_PROP_B(Window, Active)
DEF_PROP_B(Window, Pause)
DEF_PROP_I(Window, X)
DEF_PROP_I(Window, Y)
DEF_PROP_I(Window, Width)
DEF_PROP_I(Window, Height)
DEF_PROP_I(Window, OX)
DEF_PROP_I(Window, OY)
DEF_PROP_I(Window, Opacity)
DEF_PROP_I(Window, BackOpacity)
DEF_PROP_I(Window, ContentsOpacity)
void
windowBindingInit()
{
INIT_TYPE(Window);
VALUE klass = rb_define_class("Window", rb_cObject);
disposableBindingInit <Window>(klass);
viewportElementBindingInit<Window>(klass);
_rb_define_method(klass, "initialize", windowInitialize);
_rb_define_method(klass, "update", windowUpdate);
INIT_PROP_BIND( Window, Windowskin, "windowskin" );
INIT_PROP_BIND( Window, Contents, "contents" );
INIT_PROP_BIND( Window, Stretch, "stretch" );
INIT_PROP_BIND( Window, CursorRect, "cursor_rect" );
INIT_PROP_BIND( Window, Active, "active" );
INIT_PROP_BIND( Window, Pause, "pause" );
INIT_PROP_BIND( Window, X, "x" );
INIT_PROP_BIND( Window, Y, "y" );
INIT_PROP_BIND( Window, Width, "width" );
INIT_PROP_BIND( Window, Height, "height" );
INIT_PROP_BIND( Window, OX, "ox" );
INIT_PROP_BIND( Window, OY, "oy" );
INIT_PROP_BIND( Window, Opacity, "opacity" );
INIT_PROP_BIND( Window, BackOpacity, "back_opacity" );
INIT_PROP_BIND( Window, ContentsOpacity, "contents_opacity" );
}