diff --git a/src/GameEngine.cpp b/src/GameEngine.cpp index 1e18194..65878a6 100644 --- a/src/GameEngine.cpp +++ b/src/GameEngine.cpp @@ -13,7 +13,7 @@ GameEngine::GameEngine() Resources::font.loadFromFile("./assets/JetbrainsMono.ttf"); Resources::game = this; window_title = "McRogueFace - 7DRL 2024 Engine Demo"; - window.create(sf::VideoMode(1024, 768), window_title); + window.create(sf::VideoMode(1024, 768), window_title, sf::Style::Titlebar | sf::Style::Close); visible = window.getDefaultView(); window.setFramerateLimit(30); scene = "uitest"; @@ -56,6 +56,12 @@ sf::RenderWindow & GameEngine::getWindow() { return window; } void GameEngine::createScene(std::string s) { scenes[s] = new PyScene(this); } +void GameEngine::setWindowScale(float multiplier) +{ + window.setSize(sf::Vector2u(1024 * multiplier, 768 * multiplier)); // 7DRL 2024: window scaling + //window.create(sf::VideoMode(1024 * multiplier, 768 * multiplier), window_title, sf::Style::Titlebar | sf::Style::Close); +} + void GameEngine::run() { float fps = 0.0; @@ -118,11 +124,18 @@ void GameEngine::sUserInput() 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) { + continue; // 7DRL short circuit. Resizing manually disabled + /* sf::FloatRect area(0.f, 0.f, event.size.width, event.size.height); + //sf::FloatRect area(0.f, 0.f, 1024.f, 768.f); // 7DRL 2024: attempt to set scale appropriately + //sf::FloatRect area(0.f, 0.f, event.size.width, event.size.width * 0.75); visible = sf::View(area); window.setView(visible); - //std::cout << "Visible area set to (0, 0, " << event.size.width << ", " << event.size.height <<")"< textures; diff --git a/src/McRFPy_API.cpp b/src/McRFPy_API.cpp index 47f0cbd..d655686 100644 --- a/src/McRFPy_API.cpp +++ b/src/McRFPy_API.cpp @@ -35,6 +35,7 @@ static PyMethodDef mcrfpyMethods[] = { {"setTimer", McRFPy_API::_setTimer, METH_VARARGS, "setTimer(name:str, callable:object, interval:int) - callable will be called with args (runtime:float) every `interval` milliseconds"}, {"delTimer", McRFPy_API::_delTimer, METH_VARARGS, "delTimer(name:str) - stop calling the timer labelled with `name`"}, {"exit", McRFPy_API::_exit, METH_VARARGS, "exit() - close down the game engine"}, + {"setScale", McRFPy_API::_setScale, METH_VARARGS, "setScale(multiplier:float) - resize the window (still 1024x768, but bigger)"}, {NULL, NULL, 0, NULL} }; @@ -486,3 +487,16 @@ PyObject* McRFPy_API::_exit(PyObject* self, PyObject* args) { Py_INCREF(Py_None); return Py_None; } + +PyObject* McRFPy_API::_setScale(PyObject* self, PyObject* args) { + float multiplier; + if (!PyArg_ParseTuple(args, "f", &multiplier)) return NULL; + if (multiplier < 0.2 || multiplier > 4) + { + PyErr_SetString(PyExc_ValueError, "Window scale must be between 0.2 and 4"); + return NULL; + } + game->setWindowScale(multiplier); + Py_INCREF(Py_None); + return Py_None; +} diff --git a/src/McRFPy_API.h b/src/McRFPy_API.h index 7148b11..ca4a404 100644 --- a/src/McRFPy_API.h +++ b/src/McRFPy_API.h @@ -80,6 +80,7 @@ public: static PyObject* _delTimer(PyObject*, PyObject*); static PyObject* _exit(PyObject*, PyObject*); + static PyObject* _setScale(PyObject*, PyObject*); // accept keyboard input from scene static sf::Vector2i cursor_position; diff --git a/src/PyScene.cpp b/src/PyScene.cpp index 3b5a872..2052091 100644 --- a/src/PyScene.cpp +++ b/src/PyScene.cpp @@ -19,14 +19,15 @@ void PyScene::update() void PyScene::do_mouse_input(std::string button, std::string type) { - auto mousepos = sf::Mouse::getPosition(game->getWindow()); + auto unscaledmousepos = sf::Mouse::getPosition(game->getWindow()); + auto mousepos = game->getWindow().mapPixelToCoords(unscaledmousepos); UIDrawable* target; for (auto d: *ui_elements) { target = d->click_at(sf::Vector2f(mousepos)); if (target) { - PyObject* args = Py_BuildValue("(iiss)", mousepos.x, mousepos.y, button.c_str(), type.c_str()); + PyObject* args = Py_BuildValue("(iiss)", (int)mousepos.x, (int)mousepos.y, button.c_str(), type.c_str()); PyObject* retval = PyObject_Call(target->click_callable, args, NULL); if (!retval) {