Scene switching API

This commit is contained in:
John McCardle 2024-03-06 10:50:19 -05:00
parent 6de3f0e8cf
commit 30cfa5ca71
7 changed files with 96 additions and 10 deletions

View File

@ -3,7 +3,8 @@
//#include "UITestScene.h"
#include "ActionCode.h"
#include "McRFPy_API.h"
#include "PythonScene.h"
//#include "PythonScene.h"
#include "PyScene.h"
#include "UITestScene.h"
#include "Resources.h"
@ -11,7 +12,7 @@ GameEngine::GameEngine()
{
Resources::font.loadFromFile("./assets/JetbrainsMono.ttf");
Resources::game = this;
window_title = "McRogueFace - r/RoguelikeDev Tutorial Run";
window_title = "McRogueFace - 7DRL 2024 Engine Demo";
window.create(sf::VideoMode(1024, 768), window_title);
visible = window.getDefaultView();
window.setFramerateLimit(30);
@ -27,6 +28,7 @@ GameEngine::GameEngine()
McRFPy_API::game = this;
McRFPy_API::api_init();
McRFPy_API::executePyString("import mcrfpy");
McRFPy_API::executeScript("scripts/game.py");
//McRFPy_API::executePyString("from UIMenu import *");
//McRFPy_API::executePyString("from Grid import *");
@ -38,12 +40,14 @@ GameEngine::GameEngine()
}
Scene* GameEngine::currentScene() { return scenes[scene]; }
void GameEngine::changeScene(std::string s) { std::cout << "Current scene is now '" << s << "'\n"; scene = s; }
void GameEngine::changeScene(std::string s) { /*std::cout << "Current scene is now '" << s << "'\n";*/ scene = s; }
void GameEngine::quit() { running = false; }
void GameEngine::setPause(bool p) { paused = p; }
sf::Font & GameEngine::getFont() { /*return font; */ return Resources::font; }
sf::RenderWindow & GameEngine::getWindow() { return window; }
void GameEngine::createScene(std::string s) { scenes[s] = new PyScene(this); }
void GameEngine::run()
{
float fps = 0.0;
@ -72,6 +76,7 @@ void GameEngine::sUserInput()
int actionCode = 0;
if (event.type == sf::Event::Closed) { running = false; continue; }
// TODO: add resize event to Scene to react; call it after constructor too, maybe
else if (event.type == sf::Event::Resized) {
sf::FloatRect area(0.f, 0.f, event.size.width, event.size.height);
visible = sf::View(area);

View File

@ -11,7 +11,6 @@ class GameEngine
{
sf::RenderWindow window;
sf::Font font;
std::string scene;
std::map<std::string, Scene*> scenes;
bool running = true;
bool paused = false;
@ -22,9 +21,11 @@ class GameEngine
std::string window_title;
public:
std::string scene;
GameEngine();
Scene* currentScene();
void changeScene(std::string);
void createScene(std::string);
void quit();
void setPause(bool);
sf::Font & getFont();

View File

@ -21,7 +21,7 @@ bool McRFPy_API::do_camfollow;
EntityManager McRFPy_API::entities;
static PyMethodDef mcrfpyMethods[] = {
/*
{"createMenu", McRFPy_API::_createMenu, METH_VARARGS,
"Create a new uimenu (name_str, x, y, w, h)"},
@ -42,13 +42,13 @@ static PyMethodDef mcrfpyMethods[] = {
{"createTexture", McRFPy_API::_createTexture, METH_VARARGS,
"Create a new texture (filename_str, grid_size, width, height) - grid_size is in pixels (only square sprites for now), width and height are in tiles"},
*/
{"registerPyAction", McRFPy_API::_registerPyAction, METH_VARARGS,
"Register a callable Python object to correspond to an action string. (actionstr, callable)"},
{"registerInputAction", McRFPy_API::_registerInputAction, METH_VARARGS,
"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" },
@ -69,7 +69,7 @@ static PyMethodDef mcrfpyMethods[] = {
"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*);
@ -86,12 +86,14 @@ static PyMethodDef mcrfpyMethods[] = {
{"playSound", McRFPy_API::_playSound, METH_VARARGS, "(int)"},
{"getMusicVolume", McRFPy_API::_getMusicVolume, METH_VARARGS, ""},
{"getSoundVolume", McRFPy_API::_getSoundVolume, METH_VARARGS, ""},
/*
{"unlockPlayerInput", McRFPy_API::_unlockPlayerInput, METH_VARARGS, ""},
{"lockPlayerInput", McRFPy_API::_lockPlayerInput, METH_VARARGS, ""},
{"requestGridTarget", McRFPy_API::_requestGridTarget, METH_VARARGS, ""},
*/
{"activeGrid", McRFPy_API::_activeGrid, METH_VARARGS, ""},
{"setActiveGrid", McRFPy_API::_setActiveGrid, METH_VARARGS, ""},
/*
{"inputMode", McRFPy_API::_inputMode, METH_VARARGS, ""},
{"turnNumber", McRFPy_API::_turnNumber, METH_VARARGS, ""},
{"createEntity", McRFPy_API::_createEntity, METH_VARARGS, ""},
@ -99,9 +101,13 @@ static PyMethodDef mcrfpyMethods[] = {
{"refreshFov", McRFPy_API::_refreshFov, METH_VARARGS, ""},
{"camFollow", McRFPy_API::_camFollow, METH_VARARGS, ""},
*/
{"sceneUI", McRFPy_API::_sceneUI, METH_VARARGS, "sceneUI(scene) - Returns a list of UI elements"},
{"currentScene", McRFPy_API::_currentScene, METH_VARARGS, "currentScene() - Current scene's name. Returns a string"},
{"setScene", McRFPy_API::_setScene, METH_VARARGS, "setScene(scene) - transition to a different scene"},
{"createScene", McRFPy_API::_createScene, METH_VARARGS, "createScene(scene) - create a new blank scene with given name"},
{NULL, NULL, 0, NULL}
};
@ -1157,3 +1163,23 @@ PyObject* McRFPy_API::_sceneUI(PyObject* self, PyObject* args) {
o->data = ui;
return (PyObject*)o;
}
PyObject* McRFPy_API::_currentScene(PyObject* self, PyObject* args) {
return Py_BuildValue("s", game->scene.c_str());
}
PyObject* McRFPy_API::_setScene(PyObject* self, PyObject* args) {
const char* newscene;
if (!PyArg_ParseTuple(args, "s", &newscene)) return NULL;
game->changeScene(newscene);
Py_INCREF(Py_None);
return Py_None;
}
PyObject* McRFPy_API::_createScene(PyObject* self, PyObject* args) {
const char* newscene;
if (!PyArg_ParseTuple(args, "s", &newscene)) return NULL;
game->createScene(newscene);
Py_INCREF(Py_None);
return Py_None;
}

View File

@ -114,6 +114,11 @@ public:
static PyObject* _camFollow(PyObject*, PyObject*);
static PyObject* _sceneUI(PyObject*, PyObject*);
// scene control
static PyObject* _setScene(PyObject*, PyObject*);
static PyObject* _currentScene(PyObject*, PyObject*);
static PyObject* _createScene(PyObject*, PyObject*);
// accept keyboard input from scene
static sf::Vector2i cursor_position;
@ -130,6 +135,7 @@ public:
//static void playSound(const char * filename);
//static void playMusic(const char * filename);
static void doAction(std::string);
// McRFPy_API(GameEngine*);

31
src/PyScene.cpp Normal file
View File

@ -0,0 +1,31 @@
#include "PyScene.h"
#include "ActionCode.h"
#include "Resources.h"
PyScene::PyScene(GameEngine* g) : Scene(g)
{
}
void PyScene::update()
{
}
void PyScene::doAction(std::string name, std::string type)
{
}
void PyScene::sRender()
{
game->getWindow().clear();
auto vec = *ui_elements;
for (auto e: vec)
{
if (e)
e->render();
}
game->getWindow().display();
McRFPy_API::REPL();
}

15
src/PyScene.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include "Common.h"
#include "Scene.h"
#include "GameEngine.h"
class PyScene: public Scene
{
public:
PyScene(GameEngine*);
void update() override final;
void doAction(std::string, std::string) override final;
void sRender() override final;
};

2
src/scripts/game.py Normal file
View File

@ -0,0 +1,2 @@
print("Hello mcrogueface")