diff --git a/platform/linux/platform.h b/platform/linux/platform.h index aebc959..955e3e8 100644 --- a/platform/linux/platform.h +++ b/platform/linux/platform.h @@ -15,10 +15,26 @@ std::wstring executable_path() } +std::wstring executable_filename() +{ + auto exec_path = std::filesystem::canonical("/proc/self/exe"); + return exec_path.wstring(); +} + std::wstring working_path() { auto cwd = std::filesystem::current_path(); return cwd.wstring(); } +std::string narrow_string(std::wstring convertme) +{ + //setup converter + using convert_type = std::codecvt_utf8; + std::wstring_convert converter; + + //use converter (.to_bytes: wstr->str, .from_bytes: str->wstr) + return converter.to_bytes(convertme); +} + #endif diff --git a/src/Common.h b/src/Common.h index 91c777a..2bdc3ba 100644 --- a/src/Common.h +++ b/src/Common.h @@ -8,3 +8,9 @@ #include #include #include + +// wstring<->string conversion +#include +#include +#include + diff --git a/src/GameEngine.cpp b/src/GameEngine.cpp index 8c5db53..0d54e72 100644 --- a/src/GameEngine.cpp +++ b/src/GameEngine.cpp @@ -2,6 +2,7 @@ #include "MenuScene.h" #include "UITestScene.h" #include "ActionCode.h" +#include "McRFPy_API.h" GameEngine::GameEngine() { @@ -14,6 +15,10 @@ GameEngine::GameEngine() scenes["menu"] = new MenuScene(this); //std::cout << "Constructed MenuScene" <getWindow().draw(sprite); +} + +// python connection +PyObject* McRFPy_API::_drawSprite(PyObject *self, PyObject *args) +{ + int ti, gx, gy; + if (!PyArg_ParseTuple(args, "iii", &ti, &gx, &gy)) return NULL; + drawSprite(ti, gx, gy); + return NULL; +} + +void McRFPy_API::api_init() { + + // initialization time build of Py API elements + /* + mcrfpyMethodsVector.push_back( + {"drawSprite", _drawSprite, METH_VARARGS, + "Draw a sprite (index, x, y)"} + ); + mcrfpyMethodsVector.push_back( + {NULL, NULL, 0, NULL} + ); + mcrfpyMethods = &mcrfpyMethodsVector[0]; + */ + + // build API exposure before python initialization + PyImport_AppendInittab("mcrfpy", PyInit_mcrfpy); + // use full path version of argv[0] from OS to init python + init_python(narrow_string(executable_filename()).c_str()); + + texture.loadFromFile("./assets/kenney_tinydungeon.png"); + //texture_size = 16, texture_width = 12, texture_height= 11; + //texture_sprite_count = texture_width * texture_height; + texture.setSmooth(false); + + sprite.setTexture(texture); + sprite.setScale(sf::Vector2f(4.0f, 4.0f)); + setSpriteTexture(0); +} + +void McRFPy_API::executeScript(std::string filename) +{ + FILE* PScriptFile = fopen(filename.c_str(), "r"); + if(PScriptFile) { + PyRun_SimpleFile(PScriptFile, filename.c_str()); + fclose(PScriptFile); + } +} + +void McRFPy_API::executePyString(std::string pycode) +{ + PyRun_SimpleString(pycode.c_str()); +} diff --git a/src/McRFPy_API.h b/src/McRFPy_API.h new file mode 100644 index 0000000..fa7b39a --- /dev/null +++ b/src/McRFPy_API.h @@ -0,0 +1,66 @@ +#pragma once +#include "Common.h" +#include "Entity.h" +//#include "EntityManager.h" +//#include "Scene.h" +//#include "GameEngine.h" // can't - need forward declaration +//#include "ActionCode.h" +#include "Python.h" + +class GameEngine; // forward declared (circular members) + +class McRFPy_API +{ +private: + static const int texture_size = 16, // w & h (pixels) of one sprite in the tex + texture_width = 12, texture_height = 11, // w & h sprite/frame count + texture_sprite_count = 11 * 12; // t_width * t_height, minus blanks? + + // TODO: this is wrong, load resources @ GameEngineSprite sprite; + // sf::Texture texture; + + //std::vector mcrfpyMethodsVector; + //static PyObject* PyInit_mcrfpy(); + + McRFPy_API(); + +public: + inline static sf::Sprite sprite; + inline static sf::Texture texture; + static void setSpriteTexture(int); + inline static GameEngine* game; + static void api_init(); + // Python API functionality - use mcrfpy.* in scripts + static PyObject* _drawSprite(PyObject*, PyObject*); + +// McRFPy_API(GameEngine*); + + // API functionality - use from C++ directly + + //void spawnEntity(int tex_index, int grid_x, int grid_y, PyObject* script); + + // test function, do not use in production + static void drawSprite(int tex_index, int grid_x, int grid_y); + + static void executeScript(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); +} +*/ diff --git a/src/UITestScene.cpp b/src/UITestScene.cpp index b59a050..be5c721 100644 --- a/src/UITestScene.cpp +++ b/src/UITestScene.cpp @@ -285,6 +285,13 @@ void UITestScene::sRender() } + // test Python sprite code + McRFPy_API::executePyString("mcrfpy.drawSprite(123, 10, 10)"); + McRFPy_API::executePyString("mcrfpy.drawSprite(121, 15, 15)"); + + //game->api->executePyString("mcrfpy.drawSprite(123, 10, 10)") + //game->api->executePyString("mcrfpy.drawSprite(121, 15, 15)") + // draw test sprite on top of everything game->getWindow().draw(test_sprite);