Another checkpoint. Compiling/building/running - Python API tests from in-engine REPL are not passing, though

This commit is contained in:
John McCardle 2023-03-01 21:37:42 -05:00
parent 1e9fd77a13
commit f23dfbe4ba
12 changed files with 127 additions and 22 deletions

View File

@ -6,11 +6,14 @@ class Button
{ {
protected: protected:
public:
// TODO / JankMode: setter & getter for these three fields
// were protected, but directly changing them should be...fine?
sf::RectangleShape rect; sf::RectangleShape rect;
sf::Text caption; sf::Text caption;
std::string action; std::string action;
public:
Button() {}; Button() {};
Button(int x, int y, int w, int h, Button(int x, int y, int w, int h,
sf::Color _background, sf::Color _textcolor, sf::Color _background, sf::Color _textcolor,
@ -24,5 +27,6 @@ public:
void render(sf::RenderWindow & window); void render(sf::RenderWindow & window);
auto contains(sf::Vector2i p) { return rect.getGlobalBounds().contains(p.x, p.y); } auto contains(sf::Vector2i p) { return rect.getGlobalBounds().contains(p.x, p.y); }
auto getAction() { return action; } auto getAction() { return action; }
private: private:
}; };

View File

@ -20,6 +20,8 @@ GameEngine::GameEngine()
McRFPy_API::api_init(); McRFPy_API::api_init();
McRFPy_API::executePyString("import mcrfpy"); McRFPy_API::executePyString("import mcrfpy");
McRFPy_API::executePyString("from UIMenu import *"); McRFPy_API::executePyString("from UIMenu import *");
IndexSprite::game = this;
} }
Scene* GameEngine::currentScene() { return scenes[scene]; } Scene* GameEngine::currentScene() { return scenes[scene]; }

View File

@ -5,6 +5,7 @@
#include "EntityManager.h" #include "EntityManager.h"
#include "Scene.h" #include "Scene.h"
#include "McRFPy_API.h" #include "McRFPy_API.h"
#include "IndexTexture.h"
class GameEngine class GameEngine
{ {
@ -16,7 +17,6 @@ class GameEngine
bool paused = false; bool paused = false;
int currentFrame = 0; int currentFrame = 0;
sf::View visible; sf::View visible;
//McRFPy_API* api;
public: public:
GameEngine(); GameEngine();
@ -30,4 +30,12 @@ public:
void sUserInput(); void sUserInput();
int getFrame() { return currentFrame; } int getFrame() { return currentFrame; }
sf::View getView() { return visible; } sf::View getView() { return visible; }
// global textures for scripts to access
std::vector<IndexTexture> textures;
// global audio storage
std::vector<sf::SoundBuffer> sfxbuffers;
sf::Music music;
sf::Sound sfx;
}; };

View File

@ -1,3 +1,4 @@
#pragma once
#include "Common.h" #include "Common.h"
class GridPoint class GridPoint

16
src/IndexSprite.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "IndexSprite.h"
#include "GameEngine.h"
//int texture_index, sprite_index, x, y;
GameEngine* IndexSprite::game;
sf::Sprite IndexSprite::drawable()
{
sf::Sprite s;
auto& tex = IndexSprite::game->textures[texture_index];
s.setTexture(tex.texture);
s.setPosition(sf::Vector2f(x, y));
s.setTextureRect(tex.spriteCoordinates(sprite_index));
return s;
}

10
src/IndexSprite.h Normal file
View File

@ -0,0 +1,10 @@
#pragma once
#include "Common.h"
class GameEngine; // forward declare
class IndexSprite {
public:
int texture_index, sprite_index, x, y;
static GameEngine* game;
sf::Sprite drawable();
};

6
src/IndexTexture.cpp Normal file
View File

@ -0,0 +1,6 @@
#include "IndexTexture.h"
sf::IntRect IndexTexture::spriteCoordinates(int index) {
int tx = index % grid_width, ty = index / grid_width;
return sf::IntRect(tx * grid_size, ty * grid_size, grid_size, grid_size);
}

11
src/IndexTexture.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include "Common.h"
class GameEngine; // forward declare
class IndexTexture {
public:
sf::Texture texture;
int grid_size, grid_width, grid_height;
static GameEngine* game;
sf::IntRect spriteCoordinates(int);
};

View File

@ -3,9 +3,20 @@
#include "GameEngine.h" #include "GameEngine.h"
#include "Grid.h" #include "Grid.h"
// static class members...?
std::vector<UIMenu> McRFPy_API::menus;
std::vector<Grid> McRFPy_API::grids;
static PyMethodDef mcrfpyMethods[] = { static PyMethodDef mcrfpyMethods[] = {
{"drawSprite", McRFPy_API::_drawSprite, METH_VARARGS, {"drawSprite", McRFPy_API::_drawSprite, METH_VARARGS,
"Draw a sprite (index, x, y)"}, "Draw a sprite (index, x, y)"},
{"createMenu", McRFPy_API::_createMenu, METH_VARARGS,
"Create a new uimenu (x, y, w, h)"},
{"listMenus", McRFPy_API::_listMenus, METH_VARARGS,
"return a list of existing menus"},
{NULL, NULL, 0, NULL} {NULL, NULL, 0, NULL}
}; };
@ -173,18 +184,24 @@ void McRFPy_API::REPL_device(FILE * fp, const char *filename)
PyRun_InteractiveLoop(fp, filename); PyRun_InteractiveLoop(fp, filename);
} }
PyObject* _createMenu(PyObject *self, PyObject *args) { PyObject* McRFPy_API::_createMenu(PyObject *self, PyObject *args) {
int sizex, sizey; int posx, posy, sizex, sizey;
if (!PyArg_ParseTuple(args, "ii", &sizex, &sizey)) return NULL; if (!PyArg_ParseTuple(args, "iiii", &posx, &posy, &sizex, &sizey)) return NULL;
menus.push_back(createMenu(sizex, sizey)); menus.push_back(createMenu(posx, posy, sizex, sizey));
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
PyObject* _listMenus(PyObject*, PyObject*); { PyObject* McRFPy_API::_listMenus(PyObject*, PyObject*) {
// todo - get the (Py) classes UIMenu, Button, Caption, Sprite // todo - get the (Py) classes UIMenu, Button, Caption, Sprite
// and call BuildValue (tuples) -> their constructors // 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()); PyObject* menulist = PyList_New(menus.size());
for (int i = 0; i < menus.size(); i++) { for (int i = 0; i < menus.size(); i++) {
auto p = menus[i].box.getPosition(); auto p = menus[i].box.getPosition();
@ -192,6 +209,8 @@ PyObject* _listMenus(PyObject*, PyObject*); {
PyObject* menu_args = Py_BuildValue("(iiii)", p.x, p.y, s.x, s.y); PyObject* menu_args = Py_BuildValue("(iiii)", p.x, p.y, s.x, s.y);
// * need uimenu_type (imported already to __main__) // * need uimenu_type (imported already to __main__)
PyObject* menuobj = PyObject_CallObject((PyObject*) uimenu_type, menu_args); PyObject* menuobj = PyObject_CallObject((PyObject*) uimenu_type, menu_args);
// Loop: Convert Button objects to Python Objects
PyObject* button_list = PyObject_GetAttrString(menuobj, "buttons"); PyObject* button_list = PyObject_GetAttrString(menuobj, "buttons");
for(auto& b : menus[i].buttons) { for(auto& b : menus[i].buttons) {
auto bp = b.rect.getPosition(); auto bp = b.rect.getPosition();
@ -199,23 +218,43 @@ PyObject* _listMenus(PyObject*, PyObject*); {
auto bg = b.rect.getFillColor(); auto bg = b.rect.getFillColor();
auto bf = b.caption.getFillColor(); auto bf = b.caption.getFillColor();
PyObject* btn_args = Py_BuildValue("(iiii(iii)(iii)ss)", PyObject* btn_args = Py_BuildValue("(iiii(iii)(iii)ss)",
bp.x, bp.y, bs.x, bs.y, bp.x, bp.y, bs.x, bs.y,
bg.r, bg.g, bg.b, bg.r, bg.g, bg.b,
bf.r, bf.g, bf.b, bf.r, bf.g, bf.b,
b.caption.getString.toAnsiString().c_str(), b.caption.getString().toAnsiString().c_str(),
b.action.c_str()); b.action.c_str());
// * need btn_type // * need btn_type
PyObject buttonobj = PyObject_CallObject((PyObject*) btn_type, btn_args); PyObject* buttonobj = PyObject_CallObject((PyObject*) btn_type, btn_args);
PyList_Append(button_list, buttonobj); PyList_Append(button_list, buttonobj);
} }
// Loop: Convert Caption objects to Python Objects
PyObject* caption_list = PyObject_GetAttrString(menuobj, "captions"); PyObject* caption_list = PyObject_GetAttrString(menuobj, "captions");
for () { } for (auto& c : menus[i].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"); PyObject* sprite_list = PyObject_GetAttrString(menuobj, "sprites");
for () { } for (auto& s : menus[i].sprites) {
PyObject* spr_args = Py_BuildValue("iiii",
s.texture_index, s.sprite_index, s.x, s.y);
}
PyList_SET_ITEM(menulist, i, menuobj); PyList_SET_ITEM(menulist, i, menuobj);
} }
return menulist; return menulist;
} }
UIMenu McRFPy_API::createMenu(int posx, int posy, int sizex, int sizey) {
auto m = UIMenu(game->getFont());
m.box.setPosition(sf::Vector2f(posx, posy));
m.box.setSize(sf::Vector2f(sizex, sizey));
return m;
}

View File

@ -6,6 +6,10 @@
//#include "GameEngine.h" // can't - need forward declaration //#include "GameEngine.h" // can't - need forward declaration
//#include "ActionCode.h" //#include "ActionCode.h"
#include "Python.h" #include "Python.h"
#include "UIMenu.h"
#include "Grid.h"
#include "IndexSprite.h"
#include "EntityManager.h"
class GameEngine; // forward declared (circular members) class GameEngine; // forward declared (circular members)
@ -37,9 +41,9 @@ public:
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
std::vector<UIMenu> menus; static std::vector<UIMenu> menus;
EntityManager entities; // this is also kinda good, entities not on the current grid can still act (like monsters following you through doors??) EntityManager entities; // this is also kinda good, entities not on the current grid can still act (like monsters following you through doors??)
std::vector<Grid> grids; static std::vector<Grid> grids;
// Jank Python Method Exposures // Jank Python Method Exposures
static PyObject* _createMenu(PyObject*, PyObject*); // creates a new menu object in McRFPy_API::menus static PyObject* _createMenu(PyObject*, PyObject*); // creates a new menu object in McRFPy_API::menus
@ -55,7 +59,7 @@ public:
//static PyObject* _createSprite(PyObject*, PyObject*); //static PyObject* _createSprite(PyObject*, PyObject*);
// Jank Functionality // Jank Functionality
static UIMenu createMenu(int sizex, int sizey); static UIMenu createMenu(int posx, int posy, int sizex, int sizey);
//static Button createButton(UIMenu & menu, int x, int y, int w, int h); //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 sf::Sprite createSprite(int tex_index, int x, int y);
//static void playSound(const char * filename); //static void playSound(const char * filename);

View File

@ -19,7 +19,10 @@ void UIMenu::render(sf::RenderWindow & window)
window.draw(c); window.draw(c);
} }
for (auto& b : buttons) { b.render(window); } for (auto& b : buttons) { b.render(window); }
for (auto& s: sprites) { window.draw(s); } for (auto& s: sprites) {
auto _s = s.drawable();
// TODO: s.move or whatever to make it's 0,0 relative to menu box
window.draw(_s); }
} }
void UIMenu::refresh() void UIMenu::refresh()
@ -49,7 +52,7 @@ void UIMenu::add_button(Button b)
buttons.push_back(b); buttons.push_back(b);
} }
void UIMenu::add_sprite(sf::Sprite s) void UIMenu::add_sprite(IndexSprite s)
{ {
sprites.push_back(s); sprites.push_back(s);
} }

View File

@ -2,6 +2,7 @@
#include "Common.h" #include "Common.h"
#include "Button.h" #include "Button.h"
#include "IndexSprite.h"
class UIMenu class UIMenu
{ {
@ -11,7 +12,7 @@ public:
UIMenu(sf::Font & _font); UIMenu(sf::Font & _font);
std::vector<sf::Text> captions; std::vector<sf::Text> captions;
std::vector<Button> buttons; std::vector<Button> buttons;
std::vector<sf::Sprite> sprites; std::vector<IndexSprite> sprites;
sf::RectangleShape box; sf::RectangleShape box;
bool visible = false; bool visible = false;
int next_text = 10; int next_text = 10;
@ -21,7 +22,7 @@ public:
void refresh(); void refresh();
void add_caption(const char* text, int size, sf::Color color); void add_caption(const char* text, int size, sf::Color color);
void add_button(Button b); void add_button(Button b);
void add_sprite(sf::Sprite s); void add_sprite(IndexSprite s);
protected: protected: