Music tester & animation work

This commit is contained in:
John McCardle 2023-03-09 20:40:47 -05:00
parent f1798189f0
commit 8d9148b88d
5 changed files with 134 additions and 17 deletions

View File

@ -24,7 +24,7 @@ GameEngine::GameEngine()
McRFPy_API::executePyString("from UIMenu import *"); McRFPy_API::executePyString("from UIMenu import *");
McRFPy_API::executePyString("from Grid import *"); McRFPy_API::executePyString("from Grid import *");
scenes["py"] = new PythonScene(this, "TestScene"); scenes["py"] = new PythonScene(this, "MusicScene");
IndexSprite::game = this; IndexSprite::game = this;

View File

@ -4,7 +4,8 @@ class GameEngine; // forward declare
class IndexSprite { class IndexSprite {
public: public:
int texture_index, sprite_index, x, y; int texture_index, sprite_index;
float x, y;
float scale; float scale;
static GameEngine* game; static GameEngine* game;
sf::Sprite drawable(); sf::Sprite drawable();

View File

@ -90,7 +90,7 @@ static PyMethodDef mcrfpyMethods[] = {
{"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, ""},
{"listEntities", McRFPy_API::_listEntities, METH_VARARGS, ""}, //{"listEntities", McRFPy_API::_listEntities, METH_VARARGS, ""},
{NULL, NULL, 0, NULL} {NULL, NULL, 0, NULL}
}; };
@ -560,10 +560,12 @@ PyObject* McRFPy_API::_listGrids(PyObject*, PyObject*) {
PyObject* gridmodule = PyImport_AddModule("Grid"); //already imported PyObject* gridmodule = PyImport_AddModule("Grid"); //already imported
PyObject* grid_type = PyObject_GetAttrString(gridmodule, "Grid"); PyObject* grid_type = PyObject_GetAttrString(gridmodule, "Grid");
PyObject* gridpoint_type = PyObject_GetAttrString(gridmodule, "GridPoint"); 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(gridmodule)) << std::endl;
std::cout << PyUnicode_AsUTF8(PyObject_Repr(grid_type)) << 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(gridpoint_type)) << std::endl;
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(entity_type)) << std::endl;
PyObject* gridlist = PyList_New(grids.size()); PyObject* gridlist = PyList_New(grids.size());
std::map<std::string, Grid*>::iterator it = grids.begin(); std::map<std::string, Grid*>::iterator it = grids.begin();
@ -578,12 +580,12 @@ PyObject* McRFPy_API::_listGrids(PyObject*, PyObject*) {
(int)grid->grid_x, (int)grid->grid_y, (int)grid->grid_size, (int)grid->grid_x, (int)grid->grid_y, (int)grid->grid_size,
(int)p.x, (int)p.y, (int)s.x, (int)s.y); (int)p.x, (int)p.y, (int)s.x, (int)s.y);
std::cout << PyUnicode_AsUTF8(PyObject_Repr(grid_args)) << std::endl; //std::cout << PyUnicode_AsUTF8(PyObject_Repr(grid_args)) << std::endl;
PyObject* gridobj = PyObject_CallObject((PyObject*) grid_type, grid_args); PyObject* gridobj = PyObject_CallObject((PyObject*) grid_type, grid_args);
std::cout << (long)gridobj << std::flush <<std::endl; //std::cout << (long)gridobj << std::flush <<std::endl;
std::cout << PyUnicode_AsUTF8(PyObject_Repr(gridobj)) << std::endl; //std::cout << PyUnicode_AsUTF8(PyObject_Repr(gridobj)) << std::endl;
// Loop: Convert GridPoint objects to Python Objects // Loop: Convert GridPoint objects to Python Objects
PyObject* gridp_list = PyObject_GetAttrString(gridobj, "points"); PyObject* gridp_list = PyObject_GetAttrString(gridobj, "points");
for(auto& p : grid->points) { for(auto& p : grid->points) {
@ -604,6 +606,24 @@ PyObject* McRFPy_API::_listGrids(PyObject*, PyObject*) {
PyObject* gridpobj = PyObject_CallObject((PyObject*) gridpoint_type, gridp_args); PyObject* gridpobj = PyObject_CallObject((PyObject*) gridpoint_type, gridp_args);
PyList_Append(gridp_list, gridpobj); 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); PyList_SET_ITEM(gridlist, i, gridobj);
i++; // count iterator steps i++; // count iterator steps
} }
@ -672,10 +692,10 @@ PyObject* _test_createAnimation(PyObject *self, PyObject *args) {
McRFPy_API::menus[menu_key]->box.getPosition(), McRFPy_API::menus[menu_key]->box.getPosition(),
[](){McRFPy_API::executePyString("print('animation callback')");}, [](){McRFPy_API::executePyString("print('animation callback')");},
[=](sf::Vector2f v) { [=](sf::Vector2f v) {
std::cout << "write lambda!" << std::endl; //std::cout << "write lambda!" << std::endl;
McRFPy_API::menus[menu_key]->box.setPosition(v); McRFPy_API::menus[menu_key]->box.setPosition(v);
std::cout << "Position set to" << McRFPy_API::menus[menu_key]->box.getPosition().x //std::cout << "Position set to" << McRFPy_API::menus[menu_key]->box.getPosition().x
<< ", " << McRFPy_API::menus[menu_key]->box.getPosition().y << std::endl; //<< ", " << McRFPy_API::menus[menu_key]->box.getPosition().y << std::endl;
}, },
false) false)
); );
@ -687,7 +707,7 @@ PyObject* _test_createAnimation(PyObject *self, PyObject *args) {
#define CEQ(A, B) (std::string(A).compare(B) == 0) #define CEQ(A, B) (std::string(A).compare(B) == 0)
PyObject* McRFPy_API::_createAnimation(PyObject *self, PyObject *args) { PyObject* McRFPy_API::_createAnimation(PyObject *self, PyObject *args) {
std::cout << "Creating animation called..." << std::endl; //std::cout << "Creating animation called..." << std::endl;
float duration; float duration;
const char* parent; const char* parent;
const char* target_type; const char* target_type;
@ -702,6 +722,7 @@ PyObject* McRFPy_API::_createAnimation(PyObject *self, PyObject *args) {
bool loop = PyObject_IsTrue(loop_obj); bool loop = PyObject_IsTrue(loop_obj);
int target_id = PyLong_AsLong(target_id_obj); int target_id = PyLong_AsLong(target_id_obj);
Py_INCREF(callback); Py_INCREF(callback);
/*
std::cout << "Animation fields received:" << std::cout << "Animation fields received:" <<
"\nduration: " << duration << "\nduration: " << duration <<
"\nparent: " << parent << "\nparent: " << parent <<
@ -711,6 +732,7 @@ PyObject* McRFPy_API::_createAnimation(PyObject *self, PyObject *args) {
"\ncallback: " << PyUnicode_AsUTF8(PyObject_Repr(callback)) << "\ncallback: " << PyUnicode_AsUTF8(PyObject_Repr(callback)) <<
"\nloop: " << loop << "\nloop: " << loop <<
"\nvalues: " << PyUnicode_AsUTF8(PyObject_Repr(values_obj)) << std::endl; "\nvalues: " << PyUnicode_AsUTF8(PyObject_Repr(values_obj)) << std::endl;
*/
/* Jank alert: /* Jank alert:
* The following block is meant to raise an exception when index is missing from object animations that require one, * 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. * but accept the target_id_obj error (and accept None as an index) for menus/grids.
@ -765,8 +787,8 @@ PyObject* McRFPy_API::_createAnimation(PyObject *self, PyObject *args) {
// else if (CEQ(field, "bgcolor")) { ) // else if (CEQ(field, "bgcolor")) { )
} }
else if (CEQ(target_type, "sprite")) { else if (CEQ(target_type, "sprite")) {
auto obj = menus[std::string(parent)]->sprites[target_id];
if (CEQ(field, "position")) { if (CEQ(field, "position")) {
auto obj = menus[std::string(parent)]->sprites[target_id];
PyObject* evdata; PyObject* evdata;
if (PyList_Check(values_obj)) evdata = PyList_AsTuple(values_obj); else evdata = values_obj; 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)), auto end_value = sf::Vector2f(PyFloat_AsDouble(PyTuple_GetItem(evdata, 0)),
@ -798,8 +820,20 @@ PyObject* McRFPy_API::_createAnimation(PyObject *self, PyObject *args) {
} }
} }
else if (CEQ(target_type, "entity")) { else if (CEQ(target_type, "entity")) {
//auto obj = grids[std::string(parent)];//->entities[target_id]; if (CEQ(field, "position")) {
if (CEQ(field, "sprite")) { 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)]; auto obj = grids[std::string(parent)];
PyObject* evdata; PyObject* evdata;
if (PyList_Check(values_obj)) evdata = PyList_AsTuple(values_obj); else evdata = values_obj; if (PyList_Check(values_obj)) evdata = PyList_AsTuple(values_obj); else evdata = values_obj;
@ -923,7 +957,10 @@ PyObject* McRFPy_API::_createEntity(PyObject* self, PyObject* args) {
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
/*
PyObject* McRFPy_API::_listEntities(PyObject* self, PyObject* args) { PyObject* McRFPy_API::_listEntities(PyObject* self, PyObject* args) {
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
*/

View File

@ -73,7 +73,7 @@ public:
//static PyObject* _listButtons(PyObject*, PyObject*); //static PyObject* _listButtons(PyObject*, PyObject*);
static PyObject* _createEntity(PyObject*, PyObject*); static PyObject* _createEntity(PyObject*, PyObject*);
static PyObject* _listEntities(PyObject*, PyObject*); //static PyObject* _listEntities(PyObject*, PyObject*);
static PyObject* _createGrid(PyObject*, PyObject*); static PyObject* _createGrid(PyObject*, PyObject*);
static PyObject* _listGrids(PyObject*, PyObject*); static PyObject* _listGrids(PyObject*, PyObject*);

79
src/scripts/MusicScene.py Normal file
View File

@ -0,0 +1,79 @@
import mcrfpy
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED, GREEN, BLUE = (255, 0, 0), (0, 255, 0), (0, 0, 255)
DARKRED, DARKGREEN, DARKBLUE = (192, 0, 0), (0, 192, 0), (0, 0, 192)
class MusicScene:
def __init__(self, ui_name = "demobox1", grid_name = "demogrid"):
# Texture & Sound Loading
print("Load textures")
mcrfpy.createTexture("./assets/test_portraits.png", 32, 8, 8) #0 - portraits
mcrfpy.createTexture("./assets/alives_other.png", 16, 64, 64) #1 - TinyWorld NPCs
mcrfpy.createTexture("./assets/alives_other.png", 32, 32, 32) #2 - TinyWorld NPCs - 2x2 sprite
# {"createSoundBuffer", McRFPy_API::_createSoundBuffer, METH_VARARGS, "(filename)"},
#{"loadMusic", McRFPy_API::_loadMusic, METH_VARARGS, "(filename)"},
#{"setMusicVolume", McRFPy_API::_setMusicVolume, METH_VARARGS, "(int)"},
#{"setSoundVolume", McRFPy_API::_setSoundVolume, METH_VARARGS, "(int)"},
#{"playSound", McRFPy_API::_playSound, METH_VARARGS, "(int)"},
#{"getMusicVolume", McRFPy_API::_getMusicVolume, METH_VARARGS, ""},
#{"getSoundVolume", McRFPy_API::_getSoundVolume, METH_VARARGS, ""},
mcrfpy.loadMusic("./assets/ultima.ogg")
mcrfpy.createSoundBuffer("./assets/boom.wav")
self.ui_name = ui_name
self.grid_name = grid_name
print("Create UI")
# Create dialog UI
mcrfpy.createMenu(ui_name, 20, 540, 500, 200)
mcrfpy.createCaption(ui_name, f"Music Volume: {mcrfpy.getMusicVolume()}", 24, RED)
mcrfpy.createCaption(ui_name, f"SFX Volume: {mcrfpy.getSoundVolume()}", 24, RED)
#mcrfpy.createButton(ui_name, 250, 20, 100, 50, DARKBLUE, (0, 0, 0), "clicky", "testaction")
mcrfpy.createButton(ui_name, 250, 0, 130, 40, DARKRED, (0, 0, 0), "Music+", "mvol+")
mcrfpy.createButton(ui_name, 250, 0, 130, 40, DARKGREEN, (0, 0, 0), "Music-", "mvol-")
mcrfpy.createButton(ui_name, 250, 0, 130, 40, DARKBLUE, GREEN, "SFX+", "svol+")
mcrfpy.createButton(ui_name, 250, 0, 130, 40, DARKBLUE, RED, "SFX-", "svol-")
mcrfpy.createButton(ui_name, 250, 0, 130, 40, DARKRED, (0, 0, 0), "REPL", "startrepl")
mcrfpy.createSprite(ui_name, 1, 0, 20, 40, 3.0)
print("Create UI 2")
entitymenu = "entitytestmenu"
mcrfpy.createMenu(entitymenu, 840, 20, 20, 500)
mcrfpy.createButton(entitymenu, 0, 10, 150, 40, DARKBLUE, BLACK, "PlayM", "playm")
mcrfpy.createButton(entitymenu, 0, 60, 150, 40, DARKBLUE, BLACK, "StopM", "stopm")
mcrfpy.createButton(entitymenu, 0, 110, 150, 40, DARKBLUE, BLACK, "SFX", "boom")
print("Make UIs visible")
self.menus = mcrfpy.listMenus()
self.menus[0].visible = True
self.menus[1].w = 170
self.menus[1].visible = True
mcrfpy.modMenu(self.menus[0])
mcrfpy.modMenu(self.menus[1])
self.mvol = mcrfpy.getMusicVolume()
self.svol = mcrfpy.getSoundVolume()
mcrfpy.registerPyAction("mvol+", lambda: self.setmvol(self.mvol+10))
mcrfpy.registerPyAction("mvol-", lambda: self.setmvol(self.mvol-10))
mcrfpy.registerPyAction("svol+", lambda: self.setsvol(self.svol+10))
mcrfpy.registerPyAction("svol-", lambda: self.setsvol(self.svol-10))
mcrfpy.registerPyAction("playm", lambda: None)
mcrfpy.registerPyAction("stopm", lambda: None)
mcrfpy.registerPyAction("boom", lambda: mcrfpy.playSound(0))
def setmvol(self, v):
mcrfpy.setMusicVolume(int(v))
self.menus[0].captions[0].text = f"Music Volume: {mcrfpy.getMusicVolume():.1f}"
mcrfpy.modMenu(self.menus[0])
self.mvol = mcrfpy.getMusicVolume()
def setsvol(self, v):
mcrfpy.setSoundVolume(int(v))
self.menus[0].captions[1].text = f"Sound Volume: {mcrfpy.getSoundVolume():.1f}"
mcrfpy.modMenu(self.menus[0])
self.svol = mcrfpy.getSoundVolume()
scene = None
def start():
global scene
scene = MusicScene()