Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Way to use Window without GL (ALREADY DONE, woop), and maybe user data ptr in it  (Read 5510 times)

0 Members and 1 Guest are viewing this topic.

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Basically, SFML Window module is nice for input handling, and if someone knows how to use it they have two choices when they want to move to DirectX, Vulkan or use software: totally ignore the GL context or use something else.

So my idea is: why not add some flag to turn off/stop SFML from using GL at all, no creating context, no nothing.

This would allow someone to use DirectX, Vulkan or WinAPI themselves. It (making Window work without GL) could also be a stepping stone towards Vulkan coming into SFML in the future.


Woops, this was already done, I don't follow GH so I didn't know such a major thing got into there without going through here.

And a side idea is to add some user data pointer (just set/get void *). WinAPI has a user data pointer for the window handle, but SFML tucks its own Window pointer there. This came up when I did example of handling drag and drop with WinAPI and SFML. Getting list of filenames out of that message handler would require using a global variable or something like that, but if SFML Window had a user data ptr in it then I could get that pointer from window handle and from that pointer get my userdata.
« Last Edit: February 13, 2019, 09:59:13 am by FRex »
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Way to use Window without GL, and maybe user data ptr in it
« Reply #1 on: February 13, 2019, 09:45:05 am »
This has already been implemented a few weeks ago, see the related PR: https://github.com/SFML/SFML/pull/1484 :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Way to use Window without GL, and maybe user data ptr in it
« Reply #2 on: February 13, 2019, 09:58:24 am »
Well that's funny. I subscribe to RSS on the forum but not to GitHub issues so I didn't know.

And what about that user data part?
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
If there's a cross-platform API design that if possible isn't a void* then I think that would be great. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Since this is for user data the only good way to do it that I can think of is void ptr that user then handles.

This is for situation like that one (this was example for how to do it yourself on Windows when someone requested drag and drop support again): https://gist.github.com/FRex/3f7b8d1ad1289a2117553ff3702f04af

I can replace the GWLP_WNDPROC to add handling of WM_DROPFILES but I can't use GWLP_USERDATA to store those paths I get somewhere since SFML uses that for sf::Window pointer. But if sf::Window itself had a user data field for people using SFML to use then I could retrieve that sf::Window pointer and do getUserData on it and cast that to my class and use it to stuff the filenames in there.

It's super niche I guess but I thought of it in addition to this first feature since both deal with sf::Window and maybe for something such a field in sf::Window (or rather sf::WindowBase) could be useful.

I'm not sure how it is outside of Windows, googling 'X windows user data' the only good result for me was: https://bugs.freedesktop.org/show_bug.cgi?id=12871
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
I know the use-case with drag and drop and would love to give the SFML users more possibilities, but I also don't want to make SFML's API asymmetric by introducing functionality for one specific OS. And if we do "corrupt" the SFML API with a void* for advanced usages, then I want to make sure, that it has use cases for all the platforms. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
With https://github.com/binary1248/SFML/commit/ef593102ba730c300895109125860e5ab59d3c63 you can do what you want:
#include <SFML/Window.hpp>
#include <iostream>
#include <Windows.h>

LONG_PTR originalsfmlcallback = 0x0;

LRESULT CALLBACK mycallback(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)
{
    if (message == WM_DROPFILES)
    {
        sf::Window& app = *reinterpret_cast<sf::Window*>(GetWindowLongPtrW(handle, GWLP_USERDATA));
        std::cout << "Window position: " << app.getPosition().x << ", " << app.getPosition().y << "\n";

        HDROP hdrop = reinterpret_cast<HDROP>(wParam);
        POINT p;
        p.x = 0;
        p.y = 0;
        if (DragQueryPoint(hdrop, &p))
            std::printf("Point is %d, %d\n", p.x, p.y);
        else
            std::cout << "Failed to get point" << std::endl;

        const UINT filescount = DragQueryFile(hdrop, 0xFFFFFFFF, NULL, 0);
        for (UINT i = 0; i < filescount; ++i)
        {
            const UINT bufsize = DragQueryFile(hdrop, i, NULL, 0);
            std::wstring str;
            str.resize(bufsize + 1);
            if (DragQueryFileW(hdrop, i, &str[0], bufsize + 1))
            {
                std::string stdstr;
                sf::Utf8::fromWide(str.begin(), str.end(), std::back_inserter(stdstr));
                std::cout << stdstr << std::endl;
            }
        }
        DragFinish(hdrop);
        std::cout << "-------------" << std::endl;
    }//if WM_DROPFILES
    return CallWindowProcW(reinterpret_cast<WNDPROC>(originalsfmlcallback), handle, message, wParam, lParam);
}

int main(int argc, char ** argv)
{
    sf::Window app(sf::VideoMode(640u, 480u), "Drag and Drop from USERLAND");
    HWND handle = app.getSystemHandle();

    DragAcceptFiles(handle, TRUE);
    SetWindowLongPtrW(handle, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(&app));
    originalsfmlcallback = SetWindowLongPtrW(handle, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(mycallback));

    while (app.isOpen())
    {
        sf::Event eve;
        while (app.pollEvent(eve))
        {
            if (eve.type == sf::Event::Closed)
                app.close();

        }//while app poll event eve
        app.display();
    }//while app is open
}
 

The question is... should I open a pull request in the main repository for this... ::)
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
¯\_(ツ)_/¯

It's not a big deal, it was just this chain of:
  • SFML doesn't support something, e.g. drag and drop
  • On Windows (arguably most common platform for games) you can do it yourself
  • But you need a global (or make own subclass of one/two/three window classes) since SFML hogs the userdata pointer in window handle...

It's like two bads in one: not supporting a thing (which is fair) and getting in the way of someone ducktaping the feature on themselves (that's less nice).
« Last Edit: February 15, 2019, 02:34:07 am by FRex »
Back to C++ gamedev with SFML in May 2023