Window scaling functionality

This commit is contained in:
John McCardle 2024-03-08 16:55:58 -05:00
parent 8e1552eec5
commit 5ada446360
5 changed files with 34 additions and 4 deletions

View File

@ -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 <<")"<<std::endl;
//window.setSize(sf::Vector2u(event.size.width, event.size.width * 0.75)); // 7DRL 2024: window scaling
std::cout << "Visible area set to (0, 0, " << event.size.width << ", " << event.size.height <<")"<<std::endl;
actionType = "resize";
//window.setSize(sf::Vector2u(event.size.width, event.size.width * 0.75)); // 7DRL 2024: window scaling
*/
}
else if (event.type == sf::Event::KeyPressed || event.type == sf::Event::MouseButtonPressed || event.type == sf::Event::MouseWheelScrolled) actionType = "start";

View File

@ -41,6 +41,7 @@ public:
float getFrameTime() { return frameTime; }
sf::View getView() { return visible; }
void manageTimer(std::string, PyObject*, int);
void setWindowScale(float);
// global textures for scripts to access
std::vector<IndexTexture> textures;

View File

@ -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;
}

View File

@ -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;

View File

@ -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)
{