Remove cruft from API. New style objects are now the only objects

This commit is contained in:
John McCardle 2024-03-06 11:04:50 -05:00
parent 30cfa5ca71
commit 145012074c
4 changed files with 23 additions and 1104 deletions

View File

@ -6,19 +6,19 @@
#include "Resources.h" #include "Resources.h"
// static class members...? // static class members...?
std::map<std::string, UIMenu*> McRFPy_API::menus; //std::map<std::string, UIMenu*> McRFPy_API::menus;
std::map<std::string, Grid*> McRFPy_API::grids; //std::map<std::string, Grid*> McRFPy_API::grids;
std::map<std::string, PyObject*> McRFPy_API::callbacks; std::map<std::string, PyObject*> McRFPy_API::callbacks;
std::list<Animation*> McRFPy_API::animations; //std::list<Animation*> McRFPy_API::animations;
std::vector<sf::SoundBuffer> McRFPy_API::soundbuffers; std::vector<sf::SoundBuffer> McRFPy_API::soundbuffers;
sf::Music McRFPy_API::music; sf::Music McRFPy_API::music;
sf::Sound McRFPy_API::sfx; sf::Sound McRFPy_API::sfx;
std::string McRFPy_API::input_mode; //std::string McRFPy_API::input_mode;
int McRFPy_API::turn_number; //int McRFPy_API::turn_number;
std::string McRFPy_API::active_grid; //std::string McRFPy_API::active_grid;
bool McRFPy_API::do_camfollow; //bool McRFPy_API::do_camfollow;
EntityManager McRFPy_API::entities; //EntityManager McRFPy_API::entities;
static PyMethodDef mcrfpyMethods[] = { static PyMethodDef mcrfpyMethods[] = {
/* /*
@ -48,38 +48,8 @@ static PyMethodDef mcrfpyMethods[] = {
{"registerInputAction", McRFPy_API::_registerInputAction, METH_VARARGS, {"registerInputAction", McRFPy_API::_registerInputAction, METH_VARARGS,
"Register a SFML input code to correspond to an action string. (input_code, actionstr)"}, "Register a SFML input code to correspond to an action string. (input_code, actionstr)"},
/*
{"createGrid", McRFPy_API::_createGrid, METH_VARARGS,
"create a new grid (title, grid_x, grid_y, grid_size, x, y, w, h). grid_x and grid_y are the width and height in squares. grid_size is the pixel w/h of sprites on the grid. x,y are the grid's screen position. w,h are the grid's screen size" },
{"listGrids", McRFPy_API::_listGrids, METH_VARARGS, {"createSoundBuffer", McRFPy_API::_createSoundBuffer, METH_VARARGS, "(filename)"},
"return grid objects and all points" },
{"modGrid", McRFPy_API::_modGrid, METH_VARARGS,
"call with a Grid object to update all fields"},
{"createAnimation", McRFPy_API::_createAnimation, METH_VARARGS,
"Create a new animation:\n"
"createAnimation(duration:float, parent:string, target_type:string, target_id:string or int, field:string, callback:function, loop:bool, frames:list)\n"
"duration: total animation time in seconds\n"
"parent: the name of a UI menu or grid\n"
"target_type: 'caption', 'button', 'sprite', or 'entity'\n"
"target_id: integer index of the caption or button, or string ID of entity\n"
"field: what to animate. 'position', 'size', 'bgcolor', 'textcolor' or 'sprite'\n"
"callback: called when the animation completes\n"
"loop: if True, animation repeats; if False, animation is deleted\n"
"frames: if animating a sprite, list the frames. For other data types, the value will change in discrete steps at a rate of duration/len(frames).\n"},
*/
/*
static PyObject* _createSoundBuffer(PyObject*, PyObject*);
static PyObject* _loadMusic(PyObject*, PyObject*);
static PyObject* _setMusicVolume(PyObject*, PyObject*);
static PyObject* _setSoundVolume(PyObject*, PyObject*);
static PyObject* _playSound(PyObject*, PyObject*);
static PyObject* _getMusicVolume(PyObject*, PyObject*);
static PyObject* _getSoundVolume(PyObject*, PyObject*);
*/
{"createSoundBuffer", McRFPy_API::_createSoundBuffer, METH_VARARGS, "(filename)"},
{"loadMusic", McRFPy_API::_loadMusic, METH_VARARGS, "(filename)"}, {"loadMusic", McRFPy_API::_loadMusic, METH_VARARGS, "(filename)"},
{"setMusicVolume", McRFPy_API::_setMusicVolume, METH_VARARGS, "(int)"}, {"setMusicVolume", McRFPy_API::_setMusicVolume, METH_VARARGS, "(int)"},
{"setSoundVolume", McRFPy_API::_setSoundVolume, METH_VARARGS, "(int)"}, {"setSoundVolume", McRFPy_API::_setSoundVolume, METH_VARARGS, "(int)"},
@ -90,10 +60,8 @@ static PyMethodDef mcrfpyMethods[] = {
{"unlockPlayerInput", McRFPy_API::_unlockPlayerInput, METH_VARARGS, ""}, {"unlockPlayerInput", McRFPy_API::_unlockPlayerInput, METH_VARARGS, ""},
{"lockPlayerInput", McRFPy_API::_lockPlayerInput, METH_VARARGS, ""}, {"lockPlayerInput", McRFPy_API::_lockPlayerInput, METH_VARARGS, ""},
{"requestGridTarget", McRFPy_API::_requestGridTarget, METH_VARARGS, ""}, {"requestGridTarget", McRFPy_API::_requestGridTarget, METH_VARARGS, ""},
*/
{"activeGrid", McRFPy_API::_activeGrid, METH_VARARGS, ""}, {"activeGrid", McRFPy_API::_activeGrid, METH_VARARGS, ""},
{"setActiveGrid", McRFPy_API::_setActiveGrid, METH_VARARGS, ""}, {"setActiveGrid", McRFPy_API::_setActiveGrid, METH_VARARGS, ""},
/*
{"inputMode", McRFPy_API::_inputMode, METH_VARARGS, ""}, {"inputMode", McRFPy_API::_inputMode, METH_VARARGS, ""},
{"turnNumber", McRFPy_API::_turnNumber, METH_VARARGS, ""}, {"turnNumber", McRFPy_API::_turnNumber, METH_VARARGS, ""},
{"createEntity", McRFPy_API::_createEntity, METH_VARARGS, ""}, {"createEntity", McRFPy_API::_createEntity, METH_VARARGS, ""},
@ -227,34 +195,14 @@ void McRFPy_API::api_init() {
// use full path version of argv[0] from OS to init python // use full path version of argv[0] from OS to init python
init_python(narrow_string(executable_filename()).c_str()); init_python(narrow_string(executable_filename()).c_str());
/* //texture.loadFromFile("./assets/kenney_tinydungeon.png");
// Create Python translations of types
PyTypeObject * gridpoint_pytype = new PyTypeObject;
gridpoint_pytype->tp_name = "GridPoint";
gridpoint_pytype->tp_basicsize = sizeof(GridPoint);
gridpoint_pytype->tp_dealloc = [](PyObject* obj) {
delete ((GridPoint*) obj);
};
gridpoint_pytype->tp_flags = Py_TPFLAGS_DEFAULT;
gridpoint_pytype->tp_doc = "GridPoint";
gridpoint_pytype->tp_new = [](PyTypeObject* type, PyObject* args, PyObject* kwds) {
return (PyObject*) new GridPoint();
};
PyType_Ready(gridpoint_pytype);
PyModule_AddObject(
PyImport_AddModule("__main__"), "GridPoint", (PyObject*) gridpoint_pytype);
*/
texture.loadFromFile("./assets/kenney_tinydungeon.png");
//texture_size = 16, texture_width = 12, texture_height= 11; //texture_size = 16, texture_width = 12, texture_height= 11;
//texture_sprite_count = texture_width * texture_height; //texture_sprite_count = texture_width * texture_height;
texture.setSmooth(false); //texture.setSmooth(false);
sprite.setTexture(texture); //sprite.setTexture(texture);
sprite.setScale(sf::Vector2f(4.0f, 4.0f)); //sprite.setScale(sf::Vector2f(4.0f, 4.0f));
setSpriteTexture(0); //setSpriteTexture(0);
} }
void McRFPy_API::executeScript(std::string filename) void McRFPy_API::executeScript(std::string filename)
@ -271,7 +219,6 @@ void McRFPy_API::api_shutdown()
Py_Finalize(); Py_Finalize();
} }
void McRFPy_API::executePyString(std::string pycode) void McRFPy_API::executePyString(std::string pycode)
{ {
PyRun_SimpleString(pycode.c_str()); PyRun_SimpleString(pycode.c_str());
@ -287,273 +234,6 @@ void McRFPy_API::REPL_device(FILE * fp, const char *filename)
PyRun_InteractiveLoop(fp, filename); PyRun_InteractiveLoop(fp, filename);
} }
PyObject* McRFPy_API::_createMenu(PyObject *self, PyObject *args) {
const char* title_cstr;
int posx, posy, sizex, sizey;
if (!PyArg_ParseTuple(args, "siiii", &title_cstr, &posx, &posy, &sizex, &sizey)) return NULL;
std::string title = title_cstr;
//TODO (Bug 2) check for and free existing key before overwriting ptr
menus[title] = createMenu(posx, posy, sizex, sizey);
Py_INCREF(Py_None);
return Py_None;
}
PyObject* McRFPy_API::_listMenus(PyObject*, PyObject*) {
// todo - get the (Py) classes UIMenu, Button, Caption, Sprite
// and call BuildValue (tuples) -> their constructors
PyObject* uimodule = PyImport_AddModule("UIMenu"); //already imported
PyObject* uimenu_type = PyObject_GetAttrString(uimodule, "UIMenu");
PyObject* btn_type = PyObject_GetAttrString(uimodule, "Button");
PyObject* cap_type = PyObject_GetAttrString(uimodule, "Caption");
PyObject* spr_type = PyObject_GetAttrString(uimodule, "Sprite");
PyObject* menulist = PyList_New(menus.size());
std::map<std::string, UIMenu*>::iterator it = menus.begin();
//for (int i = 0; i < menus.size(); i++) {
int i = 0;
for (auto it = menus.begin(); it != menus.end(); it++) {
std::string title = it->first;
auto menu = it->second;
auto p = menu->box.getPosition();
auto s = menu->box.getSize();
auto g = menu->box.getFillColor();
PyObject* menu_args = Py_BuildValue("(siiii(iii)O)",
title.c_str(),
(int)p.x, (int)p.y, (int)s.x, (int)s.y,
(int)g.r, (int)g.g, (int)g.b,
menu->visible ? Py_True: Py_False);
menu->visible ? Py_INCREF(Py_True) : Py_INCREF(Py_False);
PyObject* menuobj = PyObject_CallObject((PyObject*) uimenu_type, menu_args);
// Loop: Convert Button objects to Python Objects
PyObject* button_list = PyObject_GetAttrString(menuobj, "buttons");
for(auto& b : menu->buttons) {
auto bp = b.rect.getPosition();
auto bs = b.rect.getSize();
auto bg = b.rect.getFillColor();
auto bf = b.caption.getFillColor();
PyObject* btn_args = Py_BuildValue("(iiii(iii)(iii)ss)",
(int)bp.x, (int)bp.y, (int)bs.x, (int)bs.y,
(int)bg.r, (int)bg.g, (int)bg.b,
(int)bf.r, (int)bf.g, (int)bf.b,
b.caption.getString().toAnsiString().c_str(),
b.action.c_str());
PyObject* buttonobj = PyObject_CallObject((PyObject*) btn_type, btn_args);
PyList_Append(button_list, buttonobj);
}
// Loop: Convert Caption objects to Python Objects
PyObject* caption_list = PyObject_GetAttrString(menuobj, "captions");
for (auto& c : menu->captions) {
auto cc = c.getFillColor();
PyObject* cap_args = Py_BuildValue("si(iii)",
c.getString().toAnsiString().c_str(),
c.getCharacterSize(),
cc.r, cc.g, cc.b);
PyObject* capobj = PyObject_CallObject((PyObject*) cap_type, cap_args);
PyList_Append(caption_list, capobj);
}
// Loop: Convert Sprite objects to Python Objects
PyObject* sprite_list = PyObject_GetAttrString(menuobj, "sprites");
for (auto& s : menu->sprites) {
PyObject* spr_args = Py_BuildValue("(iiff)",
s.texture_index, s.sprite_index, s.x, s.y);
PyObject* sprobj = PyObject_CallObject((PyObject*) spr_type, spr_args);
PyList_Append(sprite_list, sprobj);
}
PyList_SET_ITEM(menulist, i, menuobj);
i++; // count iterator steps
}
return menulist;
}
PyObject* McRFPy_API::_modMenu(PyObject* self, PyObject* args) {
PyObject* o;
if (!PyArg_ParseTuple(args, "O", &o)) return NULL;
std::string title = PyUnicode_AsUTF8(PyObject_GetAttrString(o, "title"));
int x = PyLong_AsLong(PyObject_GetAttrString(o, "x"));
int y = PyLong_AsLong(PyObject_GetAttrString(o, "y"));
int w = PyLong_AsLong(PyObject_GetAttrString(o, "w"));
int h = PyLong_AsLong(PyObject_GetAttrString(o, "h"));
PyObject* bgtuple = PyObject_GetAttrString(o, "bgcolor");
auto bgcolor = sf::Color(
PyLong_AsLong(PyTuple_GetItem(bgtuple, 0)),
PyLong_AsLong(PyTuple_GetItem(bgtuple, 1)),
PyLong_AsLong(PyTuple_GetItem(bgtuple, 2))
);
bool visible = PyObject_IsTrue(PyObject_GetAttrString(o, "visible"));
auto menu = menus[title];
if (menu == NULL) return NULL;
menu->box.setPosition(sf::Vector2f(x, y));
menu->box.setSize(sf::Vector2f(w, h));
menu->box.setFillColor(bgcolor);
menu->visible = visible;
// jank, or dank? iterate over .captions, .buttons, .sprites to modify them
// captions
PyObject* captionlist = PyObject_GetAttrString(o, "captions");
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(captionlist)) << std::endl;
for (int i = 0; i < menu->captions.size(); i++) {
PyObject* captionobj = PyList_GetItem(captionlist, i);
menu->captions[i].setString(
PyUnicode_AsUTF8(PyObject_GetAttrString(captionobj, "text")));
//menu->captions[i].setCharacterSize(
// PyLong_AsLong(PyObject_GetAttrString(captionobj, "textsize")));
PyObject* fgtuple = PyObject_GetAttrString(captionobj, "color");
menu->captions[i].setFillColor(
sf::Color(
PyLong_AsLong(PyTuple_GetItem(fgtuple, 0)),
PyLong_AsLong(PyTuple_GetItem(fgtuple, 1)),
PyLong_AsLong(PyTuple_GetItem(fgtuple, 2))
));
}
// buttons
PyObject* buttonlist = PyObject_GetAttrString(o, "buttons");
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(buttonlist)) << std::endl;
for (int i = 0; i < menu->buttons.size(); i++) {
PyObject* buttonobj = PyList_GetItem(buttonlist, i);
menu->buttons[i].setPosition(sf::Vector2f(
PyLong_AsLong(PyObject_GetAttrString(buttonobj, "x")),
PyLong_AsLong(PyObject_GetAttrString(buttonobj, "y"))
));
auto sizevec = sf::Vector2f(
PyLong_AsLong(PyObject_GetAttrString(buttonobj, "w")),
PyLong_AsLong(PyObject_GetAttrString(buttonobj, "h"))
);
menu->buttons[i].setSize(sizevec);
PyObject* btncolor = PyObject_GetAttrString(buttonobj, "bgcolor");
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(btncolor)) << std::endl;
menu->buttons[i].setBackground(
sf::Color(
PyLong_AsLong(PyTuple_GetItem(btncolor, 0)),
PyLong_AsLong(PyTuple_GetItem(btncolor, 1)),
PyLong_AsLong(PyTuple_GetItem(btncolor, 2))
));
PyObject* btxtcolor = PyObject_GetAttrString(buttonobj, "textcolor");
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(btxtcolor)) << std::endl;
menu->buttons[i].setTextColor(
sf::Color(
PyLong_AsLong(PyTuple_GetItem(btxtcolor, 0)),
PyLong_AsLong(PyTuple_GetItem(btxtcolor, 1)),
PyLong_AsLong(PyTuple_GetItem(btxtcolor, 2))
));
//std::cout << PyObject_Repr(PyObject_GetAttrString(buttonobj, "text")) << std::endl;
menu->buttons[i].caption.setString(
PyUnicode_AsUTF8(PyObject_GetAttrString(buttonobj, "text")));
//std::cout << PyObject_Repr(PyObject_GetAttrString(buttonobj, "actioncode")) << std::endl;
menu->buttons[i].action =
PyUnicode_AsUTF8(PyObject_GetAttrString(buttonobj, "actioncode"));
}
// sprites
PyObject* spriteslist = PyObject_GetAttrString(o, "sprites");
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(spriteslist)) << std::endl;
for (int i = 0; i < menu->sprites.size(); i++) {
PyObject* spriteobj = PyList_GetItem(spriteslist, i);
menu->sprites[i].texture_index =
PyLong_AsLong(PyObject_GetAttrString(spriteobj, "tex_index"));
menu->sprites[i].sprite_index =
PyLong_AsLong(PyObject_GetAttrString(spriteobj, "sprite_index"));
menu->sprites[i].x =
PyFloat_AsDouble(PyObject_GetAttrString(spriteobj, "x"));
menu->sprites[i].y =
PyFloat_AsDouble(PyObject_GetAttrString(spriteobj, "y"));
}
Py_INCREF(Py_None);
return Py_None;
}
PyObject* McRFPy_API::_createCaption(PyObject* self, PyObject* args) {
const char* menukey_cstr, *text_cstr;
int fontsize, cr, cg, cb;
if (!PyArg_ParseTuple(args, "ssi(iii)",
&menukey_cstr, &text_cstr,
&fontsize, &cr, &cg, &cb)) return NULL;
createCaption(std::string(menukey_cstr), std::string(text_cstr), fontsize, sf::Color(cr, cg, cb));
Py_INCREF(Py_None);
return Py_None;
}
PyObject* McRFPy_API::_createButton(PyObject* self, PyObject* args) {
const char *menukey_cstr, *caption_cstr, *action_cstr;
int x, y, w, h, bgr, bgg, bgb, fgr, fgg, fgb;
if (!PyArg_ParseTuple(args, "siiii(iii)(iii)ss",
&menukey_cstr, &x, &y, &w, &h,
&bgr, &bgg, &bgb, &fgr, &fgg, &fgb,
&caption_cstr, &action_cstr
)) return NULL;
createButton(std::string(menukey_cstr), x, y, w, h, sf::Color(bgr, bgg, bgb), sf::Color(fgr, fgg, fgb), std::string(caption_cstr), std::string(action_cstr));
Py_INCREF(Py_None);
return Py_None;
}
PyObject* McRFPy_API::_createTexture(PyObject* self, PyObject* args) {
const char *fn_cstr;
int gs, gw, gh;
if (!PyArg_ParseTuple(args, "siii", &fn_cstr, &gs, &gw, &gh)) return NULL;
createTexture(std::string(fn_cstr), gs, gw, gh);
Py_INCREF(Py_None);
return Py_None;
}
PyObject* McRFPy_API::_listTextures(PyObject*, PyObject*) {
Py_INCREF(Py_None);
return Py_None;
}
PyObject* McRFPy_API::_createSprite(PyObject* self, PyObject* args) {
const char * menu_cstr;
int ti, si;
float x, y;
float s;
if (!PyArg_ParseTuple(args, "siifff", &menu_cstr, &ti, &si, &x, &y, &s)) return NULL;
//std::cout << "Creating uisprite " << ti << " " << si << " " << x << " " << y << " " << s << " " << std::endl;
createSprite(std::string(menu_cstr), ti, si, x, y, s);
Py_INCREF(Py_None);
return Py_None;
}
UIMenu *McRFPy_API::createMenu(int posx, int posy, int sizex, int sizey) {
auto m = new UIMenu(game->getFont());
m->box.setPosition(sf::Vector2f(posx, posy));
m->box.setSize(sf::Vector2f(sizex, sizey));
return m;
}
void McRFPy_API::createCaption(std::string menukey, std::string text, int fontsize, sf::Color textcolor) {
auto menu = menus[menukey];
menu->add_caption(text.c_str(), fontsize, textcolor);
}
void McRFPy_API::createButton(std::string menukey, int x, int y, int w, int h, sf::Color bgcolor, sf::Color textcolor, std::string caption, std::string action) {
auto menu = menus[menukey];
auto b = Button(x, y, w, h, bgcolor, textcolor, caption.c_str(), game->getFont(), action.c_str());
menu->add_button(b);
}
void McRFPy_API::createSprite(std::string menukey, int ti, int si, float x, float y, float scale) {
auto menu = menus[menukey];
auto s = IndexSprite(ti, si, x, y, scale);
menu->add_sprite(s);
//std::cout << "indexsprite just created has values x,y " << s.x << ", " << s.y << std::endl;
}
int McRFPy_API::createTexture(std::string filename, int grid_size, int grid_width, int grid_height) {
sf::Texture t;
t.loadFromFile(filename.c_str());
t.setSmooth(false);
auto indextex = IndexTexture(t, grid_size, grid_width, grid_height);
game->textures.push_back(indextex);
return game->textures.size() - 1;
}
// python connection // python connection
PyObject* McRFPy_API::_registerPyAction(PyObject *self, PyObject *args) PyObject* McRFPy_API::_registerPyAction(PyObject *self, PyObject *args)
{ {
@ -604,164 +284,7 @@ void McRFPy_API::doAction(std::string actionstr) {
PyObject_Call(callbacks[actionstr], PyTuple_New(0), NULL); PyObject_Call(callbacks[actionstr], PyTuple_New(0), NULL);
} }
PyObject* McRFPy_API::_createGrid(PyObject *self, PyObject *args) { /*
const char* title_cstr;
int gx, gy, gs, x, y, w, h;
if (!PyArg_ParseTuple(args, "siiiiiii", &title_cstr, &gx, &gy, &gs, &x, &y, &w, &h)) return NULL;
std::string title = title_cstr;
//TODO - (Bug 2) check for key existing, and free if overwriting
grids[title] = new Grid(gx, gy, gs, x, y, w, h);
Py_INCREF(Py_None);
return Py_None;
}
PyObject* McRFPy_API::_listGrids(PyObject*, PyObject*) {
PyObject* gridmodule = PyImport_AddModule("Grid"); //already imported
PyObject* grid_type = PyObject_GetAttrString(gridmodule, "Grid");
PyObject* gridpoint_type = PyObject_GetAttrString(gridmodule, "GridPoint");
PyObject* entity_type = PyObject_GetAttrString(gridmodule, "Entity");
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(gridmodule)) << std::endl;
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(grid_type)) << std::endl;
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(gridpoint_type)) << std::endl;
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(entity_type)) << std::endl;
PyObject* gridlist = PyList_New(grids.size());
std::map<std::string, Grid*>::iterator it = grids.begin();
int i = 0;
for (auto it = grids.begin(); it != grids.end(); it++) {
std::string title = it->first;
auto grid = it->second;
auto p = grid->box.getPosition();
auto s = grid->box.getSize();
PyObject* grid_args = Py_BuildValue("(siiiiiiiO)",
title.c_str(),
(int)grid->grid_x, (int)grid->grid_y, (int)grid->grid_size,
(int)p.x, (int)p.y, (int)s.x, (int)s.y,
grid->visible? Py_True: Py_False);
grid->visible ? Py_INCREF(Py_True) : Py_INCREF(Py_False);
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(grid_args)) << std::endl;
PyObject* gridobj = PyObject_CallObject((PyObject*) grid_type, grid_args);
//std::cout << (long)gridobj << std::flush <<std::endl;
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(gridobj)) << std::endl;
// Loop: Convert GridPoint objects to Python Objects
PyObject* gridp_list = PyObject_GetAttrString(gridobj, "points");
for(auto& p : grid->points) {
PyObject* gridp_args = Py_BuildValue("((iii)OiOOO(iii)ii)",
(int)p.color.r, (int)p.color.g, (int)p.color.b,
p.walkable ? Py_True: Py_False,
p.tilesprite,
p.transparent ? Py_True: Py_False,
p.visible ? Py_True: Py_False,
p.discovered ? Py_True: Py_False,
(int)p.color_overlay.r, (int)p.color_overlay.g, (int)p.color_overlay.b,
p.tile_overlay,
p.uisprite);
p.walkable ? Py_INCREF(Py_True) : Py_INCREF(Py_False);
p.transparent ? Py_INCREF(Py_True) : Py_INCREF(Py_False);
p.visible ? Py_INCREF(Py_True) : Py_INCREF(Py_False);
p.discovered ? Py_INCREF(Py_True) : Py_INCREF(Py_False);
PyObject* gridpobj = PyObject_CallObject((PyObject*) gridpoint_type, gridp_args);
PyList_Append(gridp_list, gridpobj);
}
PyObject* ent_list = PyObject_GetAttrString(gridobj, "entities");
for (auto e : grid->entities) {
//def __init__(self, parent, tex_index, sprite_index, x, y, visible=True):
PyObject* ent_args = Py_BuildValue("siiiiO",
title.c_str(),
e->cGrid->indexsprite.texture_index,
e->cGrid->indexsprite.sprite_index,
e->cGrid->x,
e->cGrid->y,
e->cGrid->visible ? Py_True: Py_False);
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(ent_args)) << std::endl;
e->cGrid->visible ? Py_INCREF(Py_True) : Py_INCREF(Py_False);
PyObject* entobj = PyObject_CallObject((PyObject*) entity_type, ent_args);
PyList_Append(ent_list, entobj);
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(ent_list)) << std::endl;
}
PyList_SET_ITEM(gridlist, i, gridobj);
i++; // count iterator steps
}
return gridlist;
}
PyObject* McRFPy_API::_modGrid(PyObject* self, PyObject* args) {
PyObject* o;
PyObject* bool_is_entityonly = Py_False;
if (!PyArg_ParseTuple(args, "O|O", &o, &bool_is_entityonly)) return NULL;
//std::cout << "EntOnly Flag: " << PyUnicode_AsUTF8(PyObject_Repr(bool_is_entityonly)) << std::endl;
std::string title = PyUnicode_AsUTF8(PyObject_GetAttrString(o, "title"));
int grid_x = PyLong_AsLong(PyObject_GetAttrString(o, "grid_x"));
int grid_y = PyLong_AsLong(PyObject_GetAttrString(o, "grid_y"));
int grid_size = PyLong_AsLong(PyObject_GetAttrString(o, "grid_size"));
int x = PyLong_AsLong(PyObject_GetAttrString(o, "x"));
int y = PyLong_AsLong(PyObject_GetAttrString(o, "y"));
int w = PyLong_AsLong(PyObject_GetAttrString(o, "w"));
int h = PyLong_AsLong(PyObject_GetAttrString(o, "h"));
bool visible = PyObject_IsTrue(PyObject_GetAttrString(o, "visible"));
auto grid = grids[title];
if (grid == NULL) return NULL;
grid->box.setPosition(sf::Vector2f(x, y));
grid->box.setSize(sf::Vector2f(w, h));
grid->visible = visible;
//iterate over gridpoints
if (!PyObject_IsTrue(bool_is_entityonly)) {
PyObject* gpointlist = PyObject_GetAttrString(o, "points");
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(gpointlist)) << std::endl;
for (int i = 0; i < grid->points.size(); i++) {
PyObject* gpointobj = PyList_GetItem(gpointlist, i);
PyObject* colortuple = PyObject_GetAttrString(gpointobj, "color");
grid->points[i].color =
sf::Color(
PyLong_AsLong(PyTuple_GetItem(colortuple, 0)),
PyLong_AsLong(PyTuple_GetItem(colortuple, 1)),
PyLong_AsLong(PyTuple_GetItem(colortuple, 2))
);
grid->points[i].walkable = PyObject_IsTrue(PyObject_GetAttrString(gpointobj, "walkable"));
grid->points[i].tilesprite = PyLong_AsLong(PyObject_GetAttrString(gpointobj, "tilesprite"));
grid->points[i].transparent = PyObject_IsTrue(PyObject_GetAttrString(gpointobj, "transparent"));
grid->points[i].visible = PyObject_IsTrue(PyObject_GetAttrString(gpointobj, "visible"));
grid->points[i].discovered = PyObject_IsTrue(PyObject_GetAttrString(gpointobj, "discovered"));
PyObject* overlaycolortuple = PyObject_GetAttrString(gpointobj, "color_overlay");
grid->points[i].color_overlay =
sf::Color(
PyLong_AsLong(PyTuple_GetItem(overlaycolortuple, 0)),
PyLong_AsLong(PyTuple_GetItem(overlaycolortuple, 1)),
PyLong_AsLong(PyTuple_GetItem(overlaycolortuple, 2))
);
grid->points[i].tile_overlay = PyLong_AsLong(PyObject_GetAttrString(gpointobj, "tile_overlay"));
grid->points[i].uisprite = PyLong_AsLong(PyObject_GetAttrString(gpointobj, "uisprite"));
}
// update grid pathfinding & visibility
grid->refreshTCODmap();
for (auto e : McRFPy_API::entities.getEntities("player")) {
grid->refreshTCODsight(e->cGrid->x, e->cGrid->y);
}
}
PyObject* entlist = PyObject_GetAttrString(o, "entities");
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(entlist)) << std::endl;
for (int i = 0; i < grid->entities.size(); i++) {
PyObject* entobj = PyList_GetItem(entlist, i);
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(entobj)) << std::endl;
grid->entities[i]->cGrid->x = PyLong_AsLong(PyObject_GetAttrString(entobj, "x"));
grid->entities[i]->cGrid->y = PyLong_AsLong(PyObject_GetAttrString(entobj, "y"));
grid->entities[i]->cGrid->indexsprite.texture_index = PyLong_AsLong(PyObject_GetAttrString(entobj, "tex_index"));
grid->entities[i]->cGrid->indexsprite.sprite_index = PyLong_AsLong(PyObject_GetAttrString(entobj, "sprite_index"));
}
Py_INCREF(Py_None);
return Py_None;
}
PyObject* McRFPy_API::_refreshFov(PyObject* self, PyObject* args) { PyObject* McRFPy_API::_refreshFov(PyObject* self, PyObject* args) {
for (auto e : McRFPy_API::entities.getEntities("player")) { for (auto e : McRFPy_API::entities.getEntities("player")) {
e->cGrid->grid->refreshTCODsight(e->cGrid->x, e->cGrid->y); e->cGrid->grid->refreshTCODsight(e->cGrid->x, e->cGrid->y);
@ -769,180 +292,7 @@ PyObject* McRFPy_API::_refreshFov(PyObject* self, PyObject* args) {
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
*/
PyObject* _test_createAnimation(PyObject *self, PyObject *args) {
//LerpAnimation<T>::LerpAnimation(float _d, T _ev, T* _t, std::function<void()> _cb, std::function<void(T)> _w, bool _l)
std::string menu_key = "demobox1";
McRFPy_API::animations.push_back(
new LerpAnimation<sf::Vector2f>(
3.0,
sf::Vector2f(100, 100),
McRFPy_API::menus[menu_key]->box.getPosition(),
[](){McRFPy_API::executePyString("print('animation callback')");},
[=](sf::Vector2f v) {
//std::cout << "write lambda!" << std::endl;
McRFPy_API::menus[menu_key]->box.setPosition(v);
//std::cout << "Position set to" << McRFPy_API::menus[menu_key]->box.getPosition().x
//<< ", " << McRFPy_API::menus[menu_key]->box.getPosition().y << std::endl;
},
false)
);
Py_INCREF(Py_None);
return Py_None;
}
#define CEQ(A, B) (std::string(A).compare(B) == 0)
PyObject* McRFPy_API::_createAnimation(PyObject *self, PyObject *args) {
//std::cout << "Creating animation called..." << std::endl;
float duration;
const char* parent;
const char* target_type;
PyObject* target_id_obj;
const char* field;
PyObject* callback;
PyObject* loop_obj;
PyObject* values_obj;
PyObject* evdata; // for decoding values_obj
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(args)) << std::endl;
if (!PyArg_ParseTuple(args, "fssOsOOO", &duration, &parent, &target_type, &target_id_obj, &field, &callback, &loop_obj, &values_obj)) { return NULL; }
bool loop = PyObject_IsTrue(loop_obj);
int target_id = PyLong_AsLong(target_id_obj);
Py_INCREF(callback);
/*
std::cout << "Animation fields received:" <<
"\nduration: " << duration <<
"\nparent: " << parent <<
"\ntarget type: " << target_type <<
"\ntarget id: " << PyUnicode_AsUTF8(PyObject_Repr(target_id_obj)) <<
"\nfield: " << field <<
"\ncallback: " << PyUnicode_AsUTF8(PyObject_Repr(callback)) <<
"\nloop: " << loop <<
"\nvalues: " << PyUnicode_AsUTF8(PyObject_Repr(values_obj)) << std::endl;
*/
/* Jank alert:
* The following block is meant to raise an exception when index is missing from object animations that require one,
* but accept the target_id_obj error (and accept None as an index) for menus/grids.
* Instead, I get a "latent" exception, not properly raised, when the index is None.
* That not-really-raised exception causes other scripts to silently fail to execute
* until I go into the REPL and run any code, and get a bizarre, botched traceback.
* So, Grid/Menu can just take an index of 0 in the scripts until this is dejankified
*/
if (!CEQ(target_type, "menu") && !CEQ(target_type, "grid") && target_id == -1) {
PyErr_SetObject(PyExc_TypeError, target_id_obj);
PyErr_SetString(PyExc_TypeError, "target_id (integer, index value) is required for targets other than 'menu' or 'grid'");
return NULL;
}
// at this point, `values` needs to be decoded based on the `field` provided
// 3.0, # duration, seconds
// "demobox1", # parent: a UIMenu or Grid key
// "menu", # target type: 'menu', 'grid', 'caption', 'button', 'sprite', or 'entity'
// None, # target id: integer index for menu or grid objs; None for grid/menu
// "position", # field: 'position', 'size', 'bgcolor', 'textcolor', or 'sprite'
// lambda: self.animation_done("demobox1"), #callback: callable once animation is complete
// False, #loop: repeat indefinitely
// [100, 100] # values: iterable of frames for 'sprite', lerp target for others
//LerpAnimation<T>::LerpAnimation(float _d, T _ev, T _sv, std::function<void()> _cb, std::function<void(T)> _w, bool _l)
if (CEQ(target_type, "menu")) {
auto obj = menus[std::string(parent)];
if (CEQ(field, "position")) {
if (PyList_Check(values_obj)) evdata = PyList_AsTuple(values_obj); else evdata = values_obj;
auto end_value = sf::Vector2f(PyFloat_AsDouble(PyTuple_GetItem(evdata, 0)),
PyFloat_AsDouble(PyTuple_GetItem(evdata, 1)));
McRFPy_API::animations.push_back(new LerpAnimation<sf::Vector2f>(
duration, end_value,
obj->box.getPosition(),
[=](){PyObject_Call(callback, PyTuple_New(0), NULL);},
[=](sf::Vector2f v){obj->box.setPosition(v);},
loop)
);
}
else if (CEQ(field, "size")) {
if (PyList_Check(values_obj)) evdata = PyList_AsTuple(values_obj); else evdata = values_obj;
auto end_value = sf::Vector2f(PyFloat_AsDouble(PyTuple_GetItem(evdata, 0)),
PyFloat_AsDouble(PyTuple_GetItem(evdata, 1)));
McRFPy_API::animations.push_back(new LerpAnimation<sf::Vector2f>(
duration, end_value,
obj->box.getSize(),
[=](){PyObject_Call(callback, PyTuple_New(0), NULL);},
[=](sf::Vector2f v){obj->box.setSize(v);},
loop)
);
}
// else if (CEQ(field, "bgcolor")) { )
}
else if (CEQ(target_type, "sprite")) {
if (CEQ(field, "position")) {
auto obj = menus[std::string(parent)]->sprites[target_id];
PyObject* evdata;
if (PyList_Check(values_obj)) evdata = PyList_AsTuple(values_obj); else evdata = values_obj;
auto end_value = sf::Vector2f(PyFloat_AsDouble(PyTuple_GetItem(evdata, 0)),
PyFloat_AsDouble(PyTuple_GetItem(evdata, 1)));
McRFPy_API::animations.push_back(new LerpAnimation<sf::Vector2f>(duration, end_value,
sf::Vector2f(obj.x, obj.y),
[=](){PyObject_Call(callback, PyTuple_New(0), NULL);},
[&](sf::Vector2f v){obj.x = v.x; obj.y = v.y;},
loop)
);
}
else if (CEQ(field, "sprite")) {
auto obj = menus[std::string(parent)];
PyObject* evdata;
if (PyList_Check(values_obj)) evdata = PyList_AsTuple(values_obj); else evdata = values_obj;
std::vector<int> frames;
for (int i = 0; i < PyTuple_Size(evdata); i++) {
frames.push_back(PyLong_AsLong(PyTuple_GetItem(evdata, i)));
}
//DiscreteAnimation(float _d, std::vector<T> _v, std::function<void()> _cb, std::function<void(T)> _w, bool _l)
McRFPy_API::animations.push_back(new DiscreteAnimation<int>(
duration,
frames,
[=](){PyObject_Call(callback, PyTuple_New(0), NULL);},
[=](int s){obj->sprites[target_id].sprite_index = s;},
loop)
);
//std::cout << "Frame animation constructed, there are now " <<McRFPy_API::animations.size() << std::endl;
}
}
else if (CEQ(target_type, "entity")) {
if (CEQ(field, "position")) {
auto obj = grids[std::string(parent)]->entities[target_id];
PyObject* evdata;
if (PyList_Check(values_obj)) evdata = PyList_AsTuple(values_obj); else evdata = values_obj;
auto end_value = sf::Vector2f(PyFloat_AsDouble(PyTuple_GetItem(evdata, 0)),
PyFloat_AsDouble(PyTuple_GetItem(evdata, 1)));
McRFPy_API::animations.push_back(new LerpAnimation<sf::Vector2f>(duration, end_value,
sf::Vector2f(obj->cGrid->indexsprite.x, obj->cGrid->indexsprite.y),
[=](){PyObject_Call(callback, PyTuple_New(0), NULL);},
[=](sf::Vector2f v){obj->cGrid->indexsprite.x = v.x; obj->cGrid->indexsprite.y = v.y;},
loop)
);
}
else if (CEQ(field, "sprite")) {
auto obj = grids[std::string(parent)];
PyObject* evdata;
if (PyList_Check(values_obj)) evdata = PyList_AsTuple(values_obj); else evdata = values_obj;
std::vector<int> frames;
for (int i = 0; i < PyTuple_Size(evdata); i++) {
frames.push_back(PyLong_AsLong(PyTuple_GetItem(evdata, i)));
}
//DiscreteAnimation(float _d, std::vector<T> _v, std::function<void()> _cb, std::function<void(T)> _w, bool _l)
McRFPy_API::animations.push_back(new DiscreteAnimation<int>(
duration,
frames,
[=](){PyObject_Call(callback, PyTuple_New(0), NULL);},
[=](int s){obj->entities[target_id]->cGrid->indexsprite.sprite_index = s;},
loop)
);
}
}
Py_INCREF(Py_None);
return Py_None;
}
PyObject* McRFPy_API::_createSoundBuffer(PyObject* self, PyObject* args) { PyObject* McRFPy_API::_createSoundBuffer(PyObject* self, PyObject* args) {
const char *fn_cstr; const char *fn_cstr;
@ -1005,61 +355,7 @@ PyObject* McRFPy_API::_getSoundVolume(PyObject* self, PyObject* args) {
return Py_BuildValue("f", McRFPy_API::sfx.getVolume()); return Py_BuildValue("f", McRFPy_API::sfx.getVolume());
} }
PyObject* McRFPy_API::_unlockPlayerInput(PyObject* self, PyObject* args) {
McRFPy_API::input_mode = "playerturn";
Py_INCREF(Py_None);
return Py_None;
}
PyObject* McRFPy_API::_lockPlayerInput(PyObject* self, PyObject* args) {
McRFPy_API::input_mode = "computerturnwait";
Py_INCREF(Py_None);
return Py_None;
}
PyObject* McRFPy_API::_requestGridTarget(PyObject* self, PyObject* args) {
const char* requestmode;
if (!PyArg_ParseTuple(args, "s", &requestmode)) return NULL;
McRFPy_API::input_mode = requestmode;
Py_INCREF(Py_None);
return Py_None;
}
PyObject* McRFPy_API::_activeGrid(PyObject* self, PyObject* args) {
return Py_BuildValue("s", McRFPy_API::active_grid.c_str());
}
PyObject* McRFPy_API::_setActiveGrid(PyObject* self, PyObject* args) {
const char* newactivegrid;
if (!PyArg_ParseTuple(args, "s", &newactivegrid)) return NULL;
McRFPy_API::active_grid = newactivegrid;
Py_INCREF(Py_None);
return Py_None;
}
PyObject* McRFPy_API::_inputMode(PyObject* self, PyObject* args) {
return Py_BuildValue("s", McRFPy_API::input_mode.c_str());
}
PyObject* McRFPy_API::_turnNumber(PyObject* self, PyObject* args) {
return Py_BuildValue("i", McRFPy_API::turn_number);
}
PyObject* McRFPy_API::_createEntity(PyObject* self, PyObject* args) {
const char * grid_cstr, *entity_tag;
int ti, si, x, y;
PyObject* behavior_obj;
if (!PyArg_ParseTuple(args, "ssiiii|O", &grid_cstr, &entity_tag, &ti, &si, &x, &y, &behavior_obj)) return NULL;
auto e = entities.addEntity(std::string(entity_tag));
Grid* grid_ptr = grids[grid_cstr];
grid_ptr->entities.push_back(e);
e->cGrid = std::make_shared<CGrid>(grid_ptr, ti, si, x, y, true);
e->cBehavior = std::make_shared<CBehavior>(behavior_obj);
Py_INCREF(behavior_obj);
Py_INCREF(Py_None);
return Py_None;
}
/* /*
PyObject* McRFPy_API::_listEntities(PyObject* self, PyObject* args) {
Py_INCREF(Py_None);
return Py_None;
}
*/
void McRFPy_API::player_input(int dx, int dy) { void McRFPy_API::player_input(int dx, int dy) {
//std::cout << "# entities tagged 'player': " << McRFPy_API::entities.getEntities("player").size() << std::endl; //std::cout << "# entities tagged 'player': " << McRFPy_API::entities.getEntities("player").size() << std::endl;
auto player_entity = McRFPy_API::entities.getEntities("player")[0]; auto player_entity = McRFPy_API::entities.getEntities("player")[0];
@ -1095,6 +391,7 @@ void McRFPy_API::player_input(int dx, int dy) {
} }
} }
void McRFPy_API::computerTurn() { void McRFPy_API::computerTurn() {
McRFPy_API::input_mode = "computerturnrunning"; McRFPy_API::input_mode = "computerturnrunning";
for (auto e : McRFPy_API::grids[McRFPy_API::active_grid]->entities) { for (auto e : McRFPy_API::grids[McRFPy_API::active_grid]->entities) {
@ -1142,6 +439,7 @@ PyObject* McRFPy_API::_camFollow(PyObject* self, PyObject* args) {
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
*/
//McRFPy_API::_sceneUI //McRFPy_API::_sceneUI
PyObject* McRFPy_API::_sceneUI(PyObject* self, PyObject* args) { PyObject* McRFPy_API::_sceneUI(PyObject* self, PyObject* args) {

View File

@ -40,15 +40,15 @@ public:
static void api_init(); static void api_init();
static void api_shutdown(); static void api_shutdown();
// Python API functionality - use mcrfpy.* in scripts // Python API functionality - use mcrfpy.* in scripts
static PyObject* _drawSprite(PyObject*, PyObject*); //static PyObject* _drawSprite(PyObject*, PyObject*);
static void REPL_device(FILE * fp, const char *filename); static void REPL_device(FILE * fp, const char *filename);
static void REPL(); static void REPL();
// Jank mode engage: let the API hold data for Python to hack on // Jank mode engage: let the API hold data for Python to hack on
static std::map<std::string, UIMenu*> menus; //static std::map<std::string, UIMenu*> menus;
static EntityManager entities; // this is also kinda good, entities not on the current grid can still act (like monsters following you through doors??) //static EntityManager entities; // this is also kinda good, entities not on the current grid can still act (like monsters following you through doors??)
static std::map<std::string, Grid*> grids; //static std::map<std::string, Grid*> grids;
static std::list<Animation*> animations; //static std::list<Animation*> animations;
static std::vector<sf::SoundBuffer> soundbuffers; static std::vector<sf::SoundBuffer> soundbuffers;
static sf::Music music; static sf::Music music;
static sf::Sound sfx; static sf::Sound sfx;
@ -56,31 +56,6 @@ public:
static std::shared_ptr<Entity> player; static std::shared_ptr<Entity> player;
static std::map<std::string, PyObject*> callbacks; static std::map<std::string, PyObject*> callbacks;
// Jank Python Method Exposures
static PyObject* _createMenu(PyObject*, PyObject*); // creates a new menu object in McRFPy_API::menus
static PyObject* _listMenus(PyObject*, PyObject*);
static PyObject* _modMenu(PyObject*, PyObject*);
static PyObject* _createCaption(PyObject*, PyObject*); // calls menu.add_caption
static PyObject* _createButton(PyObject*, PyObject*);
static PyObject* _createTexture(PyObject*, PyObject*);
static PyObject* _listTextures(PyObject*, PyObject*);
static PyObject* _createSprite(PyObject*, PyObject*);
// use _listMenus, probably will not implement
//static PyObject* _listCaptions(PyObject*, PyObject*);
//static PyObject* _listButtons(PyObject*, PyObject*);
static PyObject* _createEntity(PyObject*, PyObject*);
//static PyObject* _listEntities(PyObject*, PyObject*);
static PyObject* _createGrid(PyObject*, PyObject*);
static PyObject* _listGrids(PyObject*, PyObject*);
static PyObject* _modGrid(PyObject*, PyObject*);
static PyObject* _createAnimation(PyObject*, PyObject*);
static PyObject* _registerPyAction(PyObject*, PyObject*); static PyObject* _registerPyAction(PyObject*, PyObject*);
static PyObject* _registerInputAction(PyObject*, PyObject*); static PyObject* _registerInputAction(PyObject*, PyObject*);
@ -92,27 +67,6 @@ public:
static PyObject* _getMusicVolume(PyObject*, PyObject*); static PyObject* _getMusicVolume(PyObject*, PyObject*);
static PyObject* _getSoundVolume(PyObject*, PyObject*); static PyObject* _getSoundVolume(PyObject*, PyObject*);
// allow all player actions (items, menus, movement, combat)
static PyObject* _unlockPlayerInput(PyObject*, PyObject*);
// disallow player actions (animating enemy actions)
static PyObject* _lockPlayerInput(PyObject*, PyObject*);
// prompt C++/Grid Objects to callback with a target Entity or grid space
static PyObject* _requestGridTarget(PyObject*, PyObject*);
// string for labeling the map
static std::string active_grid;
static PyObject* _activeGrid(PyObject*, PyObject*);
static PyObject* _setActiveGrid(PyObject*, PyObject*);
// string for prompting input
static std::string input_mode;
static PyObject* _inputMode(PyObject*, PyObject*);
// turn cycle
static int turn_number;
static PyObject* _turnNumber(PyObject*, PyObject*);
static PyObject* _refreshFov(PyObject*, PyObject*);
static bool do_camfollow;
static void camFollow();
static PyObject* _camFollow(PyObject*, PyObject*);
static PyObject* _sceneUI(PyObject*, PyObject*); static PyObject* _sceneUI(PyObject*, PyObject*);
// scene control // scene control
@ -125,44 +79,9 @@ public:
static void player_input(int, int); static void player_input(int, int);
static void computerTurn(); static void computerTurn();
static void playerTurn(); static void playerTurn();
// Jank Functionality
static UIMenu* createMenu(int posx, int posy, int sizex, int sizey);
static void createCaption(std::string menukey, std::string text, int fontsize, sf::Color textcolor);
static void createButton(std::string menukey, int x, int y, int w, int h, sf::Color bgcolor, sf::Color textcolor, std::string caption, std::string action);
static void createSprite(std::string menukey, int ti, int si, float x, float y, float scale);
static int createTexture(std::string filename, int grid_size, int grid_width, int grid_height);
//static void playSound(const char * filename);
//static void playMusic(const char * filename);
static void doAction(std::string); static void doAction(std::string);
// McRFPy_API(GameEngine*);
// API functionality - use from C++ directly
//void spawnEntity(int tex_index, int grid_x, int grid_y, PyObject* script);
static void executeScript(std::string); static void executeScript(std::string);
static void executePyString(std::string); static void executePyString(std::string);
}; };
/*
static PyMethodDef mcrfpyMethods[] = {
{"drawSprite", McRFPy_API::_drawSprite, METH_VARARGS,
"Draw a sprite (index, x, y)"},
{NULL, NULL, 0, NULL}
};
static PyModuleDef mcrfpyModule = {
PyModuleDef_HEAD_INIT, "mcrfpy", NULL, -1, mcrfpyMethods,
NULL, NULL, NULL, NULL
};
// Module initializer fn, passed to PyImport_AppendInittab
PyObject* PyInit_mcrfpy()
{
return PyModule_Create(&mcrfpyModule);
}
*/

View File

@ -1,269 +0,0 @@
#include "PythonScene.h"
#include "ActionCode.h"
#include "McRFPy_API.h"
//#include "Animation.h"
PythonScene::PythonScene(GameEngine* g, std::string pymodule)
: Scene(g) {
// mouse events
registerAction(ActionCode::MOUSEBUTTON + sf::Mouse::Left, "click");
registerAction(ActionCode::MOUSEBUTTON + sf::Mouse::Right, "rclick");
registerAction(ActionCode::MOUSEWHEEL + ActionCode::WHEEL_DEL, "wheel_up");
registerAction(ActionCode::MOUSEWHEEL + ActionCode::WHEEL_NEG + ActionCode::WHEEL_DEL, "wheel_down");
// keyboard events
/*
registerAction(ActionCode::KEY + sf::Keyboard::Q, "upleft");
registerAction(ActionCode::KEY + sf::Keyboard::W, "up");
registerAction(ActionCode::KEY + sf::Keyboard::E, "upright");
registerAction(ActionCode::KEY + sf::Keyboard::A, "left");
registerAction(ActionCode::KEY + sf::Keyboard::S, "down");
registerAction(ActionCode::KEY + sf::Keyboard::D, "right");
registerAction(ActionCode::KEY + sf::Keyboard::Z, "downleft");
registerAction(ActionCode::KEY + sf::Keyboard::X, "wait");
registerAction(ActionCode::KEY + sf::Keyboard::C, "downright");
registerAction(ActionCode::KEY + sf::Keyboard::Numpad7, "upleft");
registerAction(ActionCode::KEY + sf::Keyboard::Numpad8, "up");
registerAction(ActionCode::KEY + sf::Keyboard::Numpad9, "upright");
registerAction(ActionCode::KEY + sf::Keyboard::Numpad4, "left");
registerAction(ActionCode::KEY + sf::Keyboard::Numpad5, "wait");
registerAction(ActionCode::KEY + sf::Keyboard::Numpad6, "right");
registerAction(ActionCode::KEY + sf::Keyboard::Numpad1, "downleft");
registerAction(ActionCode::KEY + sf::Keyboard::Numpad2, "down");
registerAction(ActionCode::KEY + sf::Keyboard::Numpad3, "downright");
*/
// window resize
registerAction(0, "event");
dragging = false;
drag_grid = NULL;
// import pymodule and call start()
McRFPy_API::executePyString("import " + pymodule);
McRFPy_API::executePyString(pymodule + ".start()");
}
void PythonScene::animate() {
//std::cout << "Number of animations: " << McRFPy_API::animations.size() << std::endl;
auto frametime = game->getFrameTime();
auto it = McRFPy_API::animations.begin();
while (it != McRFPy_API::animations.end()) {
//std::cout << "Iterating" << std::endl;
(*it)->step(frametime);
//std::cout << "Step complete" << std::endl;
if ((*it)->isDone()) {
//std::cout << "Cleaning up Animation" << std::endl;
auto prev = it;
it++;
McRFPy_API::animations.erase(prev);
} else it++;
}
/* // workin on it
for (auto p : animations) {
if (p.first == "int") {
((Animation<int>)p.second).step(frametime);
} else if (p.first == "string") {
((Animation<std::string>)p.second).step(frametime);
} else if (p.first == "float") {
((Animation<float>)p.second).step(frametime);
} else if (p.first == "vector2f") {
((Animation<sf::Vector2f>)p.second).step(frametime);
} else if (p.first == "vector2i") {
((Animation<sf::Vector2i>)p.second).step(frametime);
} else if (p.first == "color") {
((Animation<int>)p.second).step(frametime); // TODO
} else {
std::cout << "Animation has label " << p.first << "; no type found" << std::endl;
}
}
auto it = animations.begin();
while (it != animations.end()) {
bool done = false;
if (p.first == "int") {
((Animation<int>)p.second).step(frametime);
} else if (p.first == "string") {
if ((Animation<std::string>)p.second).isDone()
delete (Animation<std::string>)p.second
} else if (p.first == "float") {
((Animation<float>)p.second).step(frametime);
} else if (p.first == "vector2f") {
((Animation<sf::Vector2f>)p.second).step(frametime);
} else if (p.first == "vector2i") {
((Animation<sf::Vector2i>)p.second).step(frametime);
} else if (p.first == "color") {
((Animation<int>)p.second).step(frametime); // TODO
if ((*it).second.isDone()) {
animations.erase(it++);
} else { it++; }
}
*/
}
void PythonScene::update() {
// turn cycle: If player's input made the state "computerturnwait", finish
// all animations and then let the NPCs act
if (McRFPy_API::animations.size() == 0 && McRFPy_API::input_mode.compare("computerturnwait") == 0) {
McRFPy_API::input_mode = "computerturn";
}
else if (McRFPy_API::animations.size() == 0 && McRFPy_API::input_mode.compare("computerturnrunning") == 0) {
McRFPy_API::input_mode = "playerturnstart";
}
McRFPy_API::entities.update();
// check if left click is still down & mouse has moved
// continue the drag motion
if (dragging && drag_grid) {
//std::cout << "Compute dragging" << std::endl;
auto mousepos = sf::Mouse::getPosition(game->getWindow());
auto dx = mouseprev.x - mousepos.x,
dy = mouseprev.y - mousepos.y;
if (dx != 0 || dy != 0) { McRFPy_API::do_camfollow = false; }
drag_grid->center_x += (dx / drag_grid->zoom);
drag_grid->center_y += (dy / drag_grid->zoom);
mouseprev = mousepos;
}
animate();
McRFPy_API::camFollow();
if (McRFPy_API::input_mode.compare(std::string("computerturn")) == 0) McRFPy_API::computerTurn();
if (McRFPy_API::input_mode.compare(std::string("playerturnstart")) == 0) McRFPy_API::playerTurn();
}
void PythonScene::doLClick(sf::Vector2i mousepos) {
// UI buttons get first chance
for (auto pair : McRFPy_API::menus) {
if (!pair.second->visible) continue;
for (auto b : pair.second->buttons) {
//std::cout << "Box: " << pair.second->box.getPosition().x << ", "
//<< pair.second->box.getPosition().y << "; Button:" << b.rect.getPosition().x <<
//", " << b.rect.getPosition().y << "; Mouse: " << mousepos.x << ", " <<
//mousepos.y << std::endl;
// JANK: provide the button a relative coordinate.
if (b.contains(pair.second->box.getPosition(), mousepos)) {
McRFPy_API::doAction(b.getAction());
return;
}
}
}
// left clicking a grid to select a square
for (auto pair : McRFPy_API::grids) {
if (!pair.second->visible) continue;
if (pair.second->contains(mousepos)) {
// grid was clicked
return;
}
}
}
void PythonScene::doRClick(sf::Vector2i mousepos) {
// just grids for right click
for (auto pair : McRFPy_API::grids) {
if (!pair.second->visible) continue;
if (pair.second->contains(mousepos)) {
// grid was clicked
return;
}
}
}
void PythonScene::doZoom(sf::Vector2i mousepos, int value) {
// just grids for right click
for (auto pair : McRFPy_API::grids) {
if (!pair.second->visible) continue;
if (pair.second->contains(mousepos)) {
// grid was zoomed
float new_zoom = pair.second->zoom + (value * 0.25);
if (new_zoom >= 0.5 && new_zoom <= 5.0) {
pair.second->zoom = new_zoom;
}
}
}
}
void PythonScene::doAction(std::string name, std::string type) {
auto mousepos = sf::Mouse::getPosition(game->getWindow());
//std::cout << "name: " << name << ", type: " << type << std::endl;
if (ACTIONPY) {
McRFPy_API::doAction(name.substr(0, name.size() - 3));
}
else if (ACTIONONCE("click")) {
// left click start
//std::cout << "LClick started at (" << mousepos.x << ", " << mousepos.y << ")" << std::endl;
dragstart = mousepos;
mouseprev = mousepos;
dragging = true;
// determine the grid that contains the cursor
for (auto pair : McRFPy_API::grids) {
if (!pair.second->visible) continue;
if (pair.second->contains(mousepos)) {
// grid was clicked
drag_grid = pair.second;
}
}
}
else if (ACTIONAFTER("click")) {
// left click end
//std::cout << "LClick ended at (" << mousepos.x << ", " << mousepos.y << ")" << std::endl;
// if click ended without starting a drag event, try lclick?
if (dragstart == mousepos) {
// mouse did not move, do click
//std::cout << "(did not move)" << std::endl;
doLClick(mousepos);
}
dragging = false;
drag_grid = NULL;
}
else if (ACTIONONCE("rclick")) {
// not going to test for right click drag - just rclick
doRClick(mousepos);
}
else if (ACTIONONCE("wheel_up")) {
// try zoom in
doZoom(mousepos, 1);
}
else if (ACTIONONCE("wheel_down")) {
// try zoom out
doZoom(mousepos, -1);
}
else if (ACTIONONCE("up")) { McRFPy_API::player_input(+0, -1); }
else if (ACTIONONCE("upright")) { McRFPy_API::player_input(+1, -1); }
else if (ACTIONONCE("right")) { McRFPy_API::player_input(+1, +0); }
else if (ACTIONONCE("downright")) { McRFPy_API::player_input(+1, +1); }
else if (ACTIONONCE("down")) { McRFPy_API::player_input(+0, +1); }
else if (ACTIONONCE("downleft")) { McRFPy_API::player_input(-1, +1); }
else if (ACTIONONCE("left")) { McRFPy_API::player_input(-1, +0); }
else if (ACTIONONCE("upleft")) { McRFPy_API::player_input(-1, -1); }
else if (ACTIONONCE("wait")) { McRFPy_API::player_input(+0, +0); }
}
bool PythonScene::registerActionInjected(int code, std::string name) {
std::cout << "Registering injected action (PythonScene): " << code << " (" << ActionCode::KEY + code << ")\n";
registerAction(ActionCode::KEY + code, name);
//return false;
return true;
}
bool PythonScene::unregisterActionInjected(int code, std::string name) {
return false;
}
void PythonScene::sRender() {
game->getWindow().clear();
for (auto pair: McRFPy_API::grids) {
if (!pair.second->visible) continue;
pair.second->render(game->getWindow());
}
for (auto pair: McRFPy_API::menus) {
if (!pair.second->visible) continue;
pair.second->render(game->getWindow());
}
game->getWindow().display();
}

View File

@ -1,29 +0,0 @@
#pragma once
#include "Common.h"
#include "Scene.h"
#include "GameEngine.h"
#include "Grid.h"
//#include "Animation.h"
//#include <list>
class PythonScene: public Scene
{
sf::Vector2i dragstart, mouseprev;
bool dragging;
Grid* drag_grid;
void doLClick(sf::Vector2i);
void doRClick(sf::Vector2i);
void doZoom(sf::Vector2i, int);
//std::list<Animation*> animations;
void animate();
std::map<std::string, bool> actionInjected;
public:
PythonScene(GameEngine*, std::string);
void update() override final;
void doAction(std::string, std::string) override final;
void sRender() override final;
bool registerActionInjected(int, std::string) override;
bool unregisterActionInjected(int, std::string) override;
};