From 1e9fd77a13e239709b9d7a77d4f362e4b16d3720 Mon Sep 17 00:00:00 2001 From: John McCardle Date: Tue, 28 Feb 2023 23:19:43 -0500 Subject: [PATCH] JANK MODE: Messy / broken commit - in progress Needed to make a checkpoint, gods forgive me for committing known broken code straight to master. The jam has in a sense already begun. I tested a smaller solution in the xplat_concept repo earlier today. In short, I'm going to build a janky method to add new + report existing UI elements. After that's done, the UI building should be done from python modules, hastening the UI design. This is ugly and bad, I am truly sorry. We just need to get through 7DRL, so I can't make it pretty today. --- src/GameEngine.cpp | 3 +- src/McRFPy_API.cpp | 99 +++++++++++++++++++++++++++++++++++++------ src/McRFPy_API.h | 31 +++++++++++++- src/MenuScene.cpp | 4 +- src/UIMenu.cpp | 8 ++++ src/UIMenu.h | 3 +- src/UITestScene.cpp | 100 +++++++------------------------------------- 7 files changed, 147 insertions(+), 101 deletions(-) diff --git a/src/GameEngine.cpp b/src/GameEngine.cpp index 0d54e72..2cff97c 100644 --- a/src/GameEngine.cpp +++ b/src/GameEngine.cpp @@ -7,7 +7,7 @@ GameEngine::GameEngine() { font.loadFromFile("./assets/JetbrainsMono.ttf"); - window.create(sf::VideoMode(640, 480), "McRogueFace Engine by John McCardle"); + window.create(sf::VideoMode(1024, 768), "McRogueFace Engine by John McCardle"); visible = window.getDefaultView(); window.setFramerateLimit(30); scene = "menu"; @@ -19,6 +19,7 @@ GameEngine::GameEngine() McRFPy_API::game = this; McRFPy_API::api_init(); McRFPy_API::executePyString("import mcrfpy"); + McRFPy_API::executePyString("from UIMenu import *"); } Scene* GameEngine::currentScene() { return scenes[scene]; } diff --git a/src/McRFPy_API.cpp b/src/McRFPy_API.cpp index 5c1a5ee..0aa174d 100644 --- a/src/McRFPy_API.cpp +++ b/src/McRFPy_API.cpp @@ -1,6 +1,7 @@ #include "McRFPy_API.h" #include "platform.h" #include "GameEngine.h" +#include "Grid.h" static PyMethodDef mcrfpyMethods[] = { {"drawSprite", McRFPy_API::_drawSprite, METH_VARARGS, @@ -79,6 +80,9 @@ void McRFPy_API::setSpriteTexture(int ti) texture_size, texture_size)); } +// functionality +//void McRFPy_API:: + // functionality void McRFPy_API::drawSprite(int tex_index, int grid_x, int grid_y) { @@ -104,23 +108,31 @@ PyObject* McRFPy_API::_drawSprite(PyObject *self, PyObject *args) 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()); +/* + // 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_sprite_count = texture_width * texture_height; @@ -140,7 +152,70 @@ void McRFPy_API::executeScript(std::string filename) } } +void McRFPy_API::api_shutdown() +{ + Py_Finalize(); +} + + void McRFPy_API::executePyString(std::string pycode) { PyRun_SimpleString(pycode.c_str()); } + +void McRFPy_API::REPL() +{ + PyRun_InteractiveLoop(stdin, ""); +} + +void McRFPy_API::REPL_device(FILE * fp, const char *filename) +{ + PyRun_InteractiveLoop(fp, filename); +} + +PyObject* _createMenu(PyObject *self, PyObject *args) { + + int sizex, sizey; + if (!PyArg_ParseTuple(args, "ii", &sizex, &sizey)) return NULL; + menus.push_back(createMenu(sizex, sizey)); + Py_INCREF(Py_None); + return Py_None; +} + +PyObject* _listMenus(PyObject*, PyObject*); { + // todo - get the (Py) classes UIMenu, Button, Caption, Sprite + // and call BuildValue (tuples) -> their constructors + PyObject* menulist = PyList_New(menus.size()); + for (int i = 0; i < menus.size(); i++) { + auto p = menus[i].box.getPosition(); + auto s = menus[i].box.getSize(); + PyObject* menu_args = Py_BuildValue("(iiii)", p.x, p.y, s.x, s.y); + // * need uimenu_type (imported already to __main__) + PyObject* menuobj = PyObject_CallObject((PyObject*) uimenu_type, menu_args); + PyObject* button_list = PyObject_GetAttrString(menuobj, "buttons"); + for(auto& b : menus[i].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)", + bp.x, bp.y, bs.x, bs.y, + bg.r, bg.g, bg.b, + bf.r, bf.g, bf.b, + b.caption.getString.toAnsiString().c_str(), + b.action.c_str()); + // * need btn_type + PyObject buttonobj = PyObject_CallObject((PyObject*) btn_type, btn_args); + PyList_Append(button_list, buttonobj); + } + + PyObject* caption_list = PyObject_GetAttrString(menuobj, "captions"); + for () { } + + PyObject* sprite_list = PyObject_GetAttrString(menuobj, "sprites"); + for () { } + + PyList_SET_ITEM(menulist, i, menuobj); + } + return menulist; +} diff --git a/src/McRFPy_API.h b/src/McRFPy_API.h index fa7b39a..2419fdb 100644 --- a/src/McRFPy_API.h +++ b/src/McRFPy_API.h @@ -30,10 +30,39 @@ public: static void setSpriteTexture(int); inline static GameEngine* game; static void api_init(); + static void api_shutdown(); // Python API functionality - use mcrfpy.* in scripts static PyObject* _drawSprite(PyObject*, PyObject*); + static void REPL_device(FILE * fp, const char *filename); + static void REPL(); -// McRFPy_API(GameEngine*); + // Jank mode engage: let the API hold data for Python to hack on + std::vector menus; + EntityManager entities; // this is also kinda good, entities not on the current grid can still act (like monsters following you through doors??) + std::vector grids; + + // 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* _createCaption(PyObject*, PyObject*); // calls menu.add_caption + //static PyObject* _listCaptions(PyObject*, PyObject*); + //static PyObject* _createButton(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* _createSprite(PyObject*, PyObject*); + + // Jank Functionality + static UIMenu createMenu(int sizex, int sizey); + //static Button createButton(UIMenu & menu, int x, int y, int w, int h); + //static sf::Sprite createSprite(int tex_index, int x, int y); + //static void playSound(const char * filename); + //static void playMusic(const char * filename); + + + // McRFPy_API(GameEngine*); // API functionality - use from C++ directly diff --git a/src/MenuScene.cpp b/src/MenuScene.cpp index 998b054..c1af2a8 100644 --- a/src/MenuScene.cpp +++ b/src/MenuScene.cpp @@ -31,9 +31,9 @@ void MenuScene::doAction(std::string name, std::string type) if(ACTION("start_game", "start")) game->changeScene("play"); else if(ACTIONONCE("up")) - game->getWindow().setSize(sf::Vector2u(1024, 768)); + game->getWindow().setSize(sf::Vector2u(1280, 800)); else if(ACTIONONCE("down")) - game->getWindow().setSize(sf::Vector2u(640, 480)); + game->getWindow().setSize(sf::Vector2u(1024, 768)); } void MenuScene::sRender() diff --git a/src/UIMenu.cpp b/src/UIMenu.cpp index c16452a..206ef56 100644 --- a/src/UIMenu.cpp +++ b/src/UIMenu.cpp @@ -1,4 +1,5 @@ #include "UIMenu.h" +#include "Common.h" UIMenu::UIMenu(sf::Font & _font) : font(_font) @@ -18,6 +19,7 @@ void UIMenu::render(sf::RenderWindow & window) window.draw(c); } for (auto& b : buttons) { b.render(window); } + for (auto& s: sprites) { window.draw(s); } } void UIMenu::refresh() @@ -46,3 +48,9 @@ void UIMenu::add_button(Button b) next_button += 50; buttons.push_back(b); } + +void UIMenu::add_sprite(sf::Sprite s) +{ + sprites.push_back(s); +} + diff --git a/src/UIMenu.h b/src/UIMenu.h index 1b2b0b1..c4f7896 100644 --- a/src/UIMenu.h +++ b/src/UIMenu.h @@ -11,6 +11,7 @@ public: UIMenu(sf::Font & _font); std::vector captions; std::vector