Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BACKENDS: Add SDL3 backend + update imgui #6312

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

scemino
Copy link
Contributor

@scemino scemino commented Dec 14, 2024

This PR modifies the current SDL backend to add SDL3 support.
There are a lot of combinations so don't hesitate to read it carefully and/or test it.
I didn't test the SDL3_net library.
This is the first time I modify the configure file, so have a look and tell me if I need to do some changes (I'm sure you will ;)).
And maybe the way I handled all the #ifdef are not at your taste, so feel free to give some advice.

To do this PR I used these recommendations: https://github.com/libsdl-org/SDL/blob/main/docs/README-migration.md and of course https://wiki.libsdl.org/SDL3/CategoryAPI

@ccawley2011
Copy link
Member

Since there are a lot of repeated ifdefs relates to functions that have simply been renamed or only have minor changes to the parameters, I'd recommend using wrapper functions where possible to make the changes easier to read and less fragile to maintain.

@scemino
Copy link
Contributor Author

scemino commented Dec 15, 2024

Since there are a lot of repeated ifdefs relates to functions that have simply been renamed or only have minor changes to the parameters, I'd recommend using wrapper functions where possible to make the changes easier to read and less fragile to maintain.

Thanks for your advice.
Do you prefer #define, static functions or member functions?

Here is an example with static functions:

Before:

#if SDL_VERSION_ATLEAST(3, 0, 0)
    if (!SDL_AddGamepadMappingsFromFile(file.getPath().toString(Common::Path::kNativeSeparator).c_str()))
#else
    if (SDL_GameControllerAddMappingsFromFile(file.getPath().toString(Common::Path::kNativeSeparator).c_str()) < 0)
#endif
        error("File %s not valid: %s", file.getPath().toString(Common::Path::kNativeSeparator).c_str(), SDL_GetError());

After with static functions:

static bool gameControllerAddMappingsFromFile(const char *file) {
#if SDL_VERSION_ATLEAST(3, 0, 0)
	return SDL_AddGamepadMappingsFromFile(file);
#else
	return SDL_GameControllerAddMappingsFromFile(file) >= 0;
#endif
}

...

if (!gameControllerAddMappingsFromFile(file.getPath().toString(Common::Path::kNativeSeparator).c_str()))
        error("File %s not valid: %s", file.getPath().toString(Common::Path::kNativeSeparator).c_str(), SDL_GetError());
    

@sev-
Copy link
Member

sev- commented Dec 15, 2024

Regarding the imgui update, did you add the files from the matching git commit as rest of the code?

@scemino
Copy link
Contributor Author

scemino commented Dec 16, 2024

Regarding the imgui update, did you add the files from the matching git commit as rest of the code?

Yes I used the commit ocornut/imgui@cb16568 corresponding to the tag https://github.com/ocornut/imgui/releases/tag/v1.91.3

About this, I see that there is a backends/imgui/scummvm.patch file, I should update this file too, no?

@scemino
Copy link
Contributor Author

scemino commented Dec 16, 2024

Since there are a lot of repeated ifdefs relates to functions that have simply been renamed or only have minor changes to the parameters, I'd recommend using wrapper functions where possible to make the changes easier to read and less fragile to maintain.

Tell me if it's OK now or did you think something different?

@scemino scemino force-pushed the master branch 4 times, most recently from 26a395e to 117ab44 Compare December 16, 2024 21:48
@sev-
Copy link
Member

sev- commented Dec 16, 2024

About this, I see that there is a backends/imgui/scummvm.patch file, I should update this file too, no?
Yes. I use it for updating imgui from time to time. Just make a diff against the imgui git commit.

@scemino scemino force-pushed the master branch 2 times, most recently from d0f76eb to fbd5a5a Compare December 17, 2024 20:37
@scemino
Copy link
Contributor Author

scemino commented Dec 17, 2024

About this, I see that there is a backends/imgui/scummvm.patch file, I should update this file too, no?
Yes. I use it for updating imgui from time to time. Just make a diff against the imgui git commit.

I updated the patch file, but I have a lot more changes than the previous one, can you have a look @sev- please?

@bluegr
Copy link
Member

bluegr commented Dec 18, 2024

Since there are a lot of repeated ifdefs relates to functions that have simply been renamed or only have minor changes to the parameters, I'd recommend using wrapper functions where possible to make the changes easier to read and less fragile to maintain.

Tell me if it's OK now or did you think something different?

There is no definite answer here... use whatever makes the code easier to read and maintain.

From the three options you provided:

  • static wrapper functions would be a very straightforward way
  • defines are a bit uglier than function wrappers, IMHO
  • member functions are also a nice idea, we could keep all of these functions neatly placed inside classes, and instantiate a different class per SDL version

So, from the options above, we could either use a C-style implementation (static wrapper functions) or a C++-style implementation (different class per SDL version, inherited from a common parent class, with member functions). I personally like the C++ approach better, but function wrappers would also work just fine :)

@scemino
Copy link
Contributor Author

scemino commented Dec 18, 2024

Since there are a lot of repeated ifdefs relates to functions that have simply been renamed or only have minor changes to the parameters, I'd recommend using wrapper functions where possible to make the changes easier to read and less fragile to maintain.

Tell me if it's OK now or did you think something different?

There is no definite answer here... use whatever makes the code easier to read and maintain.

From the three options you provided:

  • static wrapper functions would be a very straightforward way
  • defines are a bit uglier than function wrappers, IMHO
  • member functions are also a nice idea, we could keep all of these functions neatly placed inside classes, and instantiate a different class per SDL version

So, from the options above, we could either use a C-style implementation (static wrapper functions) or a C++-style implementation (different class per SDL version, inherited from a common parent class, with member functions). I personally like the C++ approach better, but function wrappers would also work just fine :)

I made my choice already, sorry I'm not a huge fan of inheritance and I don't see the benefit of creating 1 class by version.

@bluegr
Copy link
Member

bluegr commented Dec 19, 2024

I made my choice already, sorry I'm not a huge fan of inheritance and I don't see the benefit of creating 1 class by version.

That's fine :) Nice implementation!

@lotharsm
Copy link
Member

Thank you for looking into this!

I wanted to give it a try on Mingw64, and currently it seems that Win32 isn't fully supported yet:

In file included from backends/platform/sdl/win32/win32-window.cpp:27:
./backends/platform/sdl/win32/win32-window.h:32:9: error: 'HWND' does not name a type
   32 |         HWND getHwnd();
      |         ^~~~
backends/platform/sdl/win32/win32-main.cpp: In function 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)':
backends/platform/sdl/win32/win32-main.cpp:54:16: error: 'main' was not declared in this scope
   54 |         return main(__argc, __argv);
      |                ^~~~
make: *** [Makefile.common:176: backends/platform/sdl/win32/win32-main.o] Error 1
make: *** Waiting for unfinished jobs....
    C++      engines/scumm/boxes.o
    C++      engines/scumm/camera.o
backends/platform/sdl/win32/win32-window.cpp: In member function 'virtual void SdlWindow_Win32::setupIcon()':
backends/platform/sdl/win32/win32-window.cpp:36:29: error: 'getHwnd' was not declared in this scope; did you mean 'getwc'?
   36 |                 HWND hwnd = getHwnd();
      |                             ^~~~~~~
      |                             getwc
backends/platform/sdl/win32/win32-window.cpp: At global scope:
backends/platform/sdl/win32/win32-window.cpp:53:6: error: no declaration matches 'HWND__* SdlWindow_Win32::getHwnd()'
   53 | HWND SdlWindow_Win32::getHwnd() {
      |      ^~~~~~~~~~~~~~~
backends/platform/sdl/win32/win32-window.cpp:53:6: note: no functions named 'HWND__* SdlWindow_Win32::getHwnd()'
./backends/platform/sdl/win32/win32-window.h:29:7: note: 'class SdlWindow_Win32' defined here
   29 | class SdlWindow_Win32 final : public SdlWindow {
      |       ^~~~~~~~~~~~~~~
make: *** [Makefile.common:176: backends/platform/sdl/win32/win32-window.o] Error 1

I quickly went through the release notes and indeed it looks like backends/platform/sdl/win32/win32-window.cpp is missing something - I'll check if I can adapt it from the other changes.

@scemino
Copy link
Contributor Author

scemino commented Dec 26, 2024

I quickly went through the release notes and indeed it looks like backends/platform/sdl/win32/win32-window.cpp is missing something - I'll check if I can adapt it from the other changes.

That's nice of you, thank you.

Copy link
Member

@sev- sev- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Colossal work, thank you!

Have number of comments, mainly on formatting and suggestion to add more wrappers.

I am concerned that the code now becomes much harder to maintain with this amount of ifdefs, so maybe splitting files/adding more wrappers could make it easier to read and less fragile?

backends/graphics/openglsdl/openglsdl-graphics.cpp Outdated Show resolved Hide resolved
@@ -46,13 +50,133 @@ static uint32 convUTF8ToUTF32(const char *src) {
Common::U32String u32(src);
return u32[0];
}
#endif

#if SDL_VERSION_ATLEAST(3, 0, 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing the number of ifdefs, wouldn't it be easier to split this file into three, e.g.

sdl#-events.cpp:

#if SDL_VERSION(ATLEAST(3,0,0)
... all the contents

then sdl2-events.cpp and finally sdl-common-events.cpp?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this what you had in mind?

backends/graphics/sdl/sdl-graphics.cpp Outdated Show resolved Hide resolved
backends/graphics/sdl/sdl-graphics.cpp Outdated Show resolved Hide resolved
backends/graphics/sdl/sdl-graphics.cpp Outdated Show resolved Hide resolved
backends/platform/sdl/sdl-window.cpp Outdated Show resolved Hide resolved
backends/platform/sdl/sdl-window.cpp Outdated Show resolved Hide resolved
backends/platform/sdl/sdl.cpp Outdated Show resolved Hide resolved
configure Show resolved Hide resolved
configure Outdated Show resolved Hide resolved
@scemino
Copy link
Contributor Author

scemino commented Dec 30, 2024

Colossal work, thank you!

Have number of comments, mainly on formatting and suggestion to add more wrappers.

I am concerned that the code now becomes much harder to maintain with this amount of ifdefs, so maybe splitting files/adding more wrappers could make it easier to read and less fragile?

Sorry I've been sick for a few days, as soon as I feel better I'll take care of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants