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
Fixed SDL(3)_SetRenderScale handling #7178
base: master
Are you sure you want to change the base?
Conversation
Hello Bogdan! |
@ocornut Hi, thanks for your quick reply. I don't see how it will work, because usually we call Here is the test that I was using to test my patch: #include <SDL3/SDL.h>
#include <backends/imgui_impl_sdl3.h>
#include <backends/imgui_impl_sdlrenderer3.h>
int main(int argc, char *argv[]) {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
SDL_Log("SDL_Init failed (%s)", SDL_GetError());
return 1;
}
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
constexpr int wflags = SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_RESIZABLE;
if (SDL_CreateWindowAndRenderer(640, 480, wflags, &window, &renderer) < 0) {
SDL_Log("SDL_CreateWindowAndRenderer failed (%s)", SDL_GetError());
SDL_Quit();
return 1;
}
SDL_SetWindowTitle(window, "X-Mas");
const auto scale = SDL_GetWindowDisplayScale(window);
SDL_SetRenderScale(renderer, scale, scale);
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io{ImGui::GetIO()};
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableSetMousePos;
ImGui_ImplSDL3_InitForSDLRenderer(window, renderer);
ImGui_ImplSDLRenderer3_Init(renderer);
while (1) {
int finished = 0;
SDL_Event event;
while (SDL_PollEvent(&event)) {
ImGui_ImplSDL3_ProcessEvent(&event);
if (event.type == SDL_EVENT_QUIT) {
finished = 1;
break;
}
}
if (finished) {
break;
}
ImGui_ImplSDLRenderer3_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
ImGui::Begin("X-Mas", nullptr);
ImGui::Text("Hello there !");
ImGui::End();
ImGui::Render();
SDL_SetRenderDrawColor(renderer, 80, 80, 80, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData());
SDL_RenderPresent(renderer);
}
ImGui_ImplSDLRenderer3_Shutdown();
ImGui_ImplSDL3_Shutdown();
ImGui::DestroyContext();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
} CMakeLists.txt: cmake_minimum_required(VERSION 3.5)
project(SDL3_imgui LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(FetchContent)
find_package(SDL3 QUIET)
if(NOT SDL3_FOUND)
set(SDL_SHARED TRUE CACHE BOOL "Build a SDL shared library (if available)")
set(SDL_STATIC TRUE CACHE BOOL "Build a SDL static library (if available)")
FetchContent_Declare(
SDL
GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
GIT_TAG main # Replace this with a particular git tag or git hash
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
)
message(STATUS "Using SDL3 via FetchContent")
FetchContent_MakeAvailable(SDL)
set_property(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/_deps/sdl-src" PROPERTY EXCLUDE_FROM_ALL TRUE)
endif()
FetchContent_Declare(
imgui
GIT_REPOSITORY "https://github.com/bog-dan-ro/imgui.git"
GIT_TAG master
)
FetchContent_GetProperties(imgui)
if (NOT imgui_POPULATED)
FetchContent_Populate(imgui)
endif ()
add_library(imgui
${imgui_SOURCE_DIR}/imgui.cpp
${imgui_SOURCE_DIR}/imgui.h
${imgui_SOURCE_DIR}/imconfig.h
${imgui_SOURCE_DIR}/imgui_demo.cpp
${imgui_SOURCE_DIR}/imgui_draw.cpp
${imgui_SOURCE_DIR}/imgui_internal.h
${imgui_SOURCE_DIR}/imgui_tables.cpp
${imgui_SOURCE_DIR}/imgui_widgets.cpp
${imgui_SOURCE_DIR}/imstb_rectpack.h
${imgui_SOURCE_DIR}/imstb_textedit.h
${imgui_SOURCE_DIR}/imstb_truetype.h
${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.cpp
${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.h
${imgui_SOURCE_DIR}/backends/imgui_impl_sdl3.h
${imgui_SOURCE_DIR}/backends/imgui_impl_sdl3.cpp
${imgui_SOURCE_DIR}/backends/imgui_impl_sdlrenderer3.h
${imgui_SOURCE_DIR}/backends/imgui_impl_sdlrenderer3.cpp)
target_include_directories(imgui PUBLIC ${imgui_SOURCE_DIR})
target_link_libraries(imgui PUBLIC SDL3::SDL3)
FetchContent_MakeAvailable(imgui)
add_executable(${PROJECT_NAME} main.cpp)
target_compile_definitions(${PROJECT_NAME}
PRIVATE
GL_GLEXT_PROTOTYPES
)
target_link_libraries(${PROJECT_NAME}
PRIVATE
SDL3::SDL3
imgui
)
include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
) |
@@ -71,7 +71,7 @@ struct ImGui_ImplSDL3_Data | |||
int PendingMouseLeaveFrame; | |||
char* ClipboardTextData; | |||
bool MouseCanUseGlobalState; | |||
|
|||
ImVec2 RenderScale() const { ImVec2 res; SDL_GetRenderScale(Renderer, &res.x, &res.y); return res;} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renderer
is able to be null in cases where SDL_Renderer isn't being used (IE: OpenGL, etc), so this needs to be able to handle that.
@@ -267,6 +267,7 @@ struct ImVec2 | |||
#ifdef IM_VEC2_CLASS_EXTRA | |||
IM_VEC2_CLASS_EXTRA // Define additional constructors and implicit cast operators in imconfig.h to convert back and forth between your math types and ImVec2. | |||
#endif | |||
inline ImVec2 & operator /=(ImVec2 other) {x /= other.x; y /= other.y; return *this; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are already math operators defined for ImVec2
, they're just disabled by default, and none of the backends enable them. Not sure if there's a reason for that or if it was just out of habit.
It might be easier to just match the existing backends and do the math manually per-component for consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are disabled by default as I think it caused issues for users of IM_VEC2_CLASS_EXTRA
with own math class + own math operators, though honestly I am not sure of specifics off-hand that that might need a separate investigation.
But yeah at the moment it'd be good if backends didn't rely on those.
This change only applies to I do wonder if Dear ImGui should even support As an aside, we already use I can't imagine that's just always been broken, so someone should double-check that this PR didn't cause problems with it. |
Fixed mouse handling when using SDL_SetRenderScale for HiDPI.
Fixed #6065