Browse Source

Moved SDL renderer access to ScreenHandler class

Ivan Savenko 2 years ago
parent
commit
5143ca266d

+ 0 - 3
client/CMT.h

@@ -10,13 +10,10 @@
 #pragma once
 
 struct SDL_Texture;
-struct SDL_Window;
 struct SDL_Renderer;
 struct SDL_Surface;
 
 extern SDL_Texture * screenTexture;
-
-extern SDL_Window * mainWindow;
 extern SDL_Renderer * mainRenderer;
 
 extern SDL_Surface *screen;      // main screen surface

+ 6 - 20
client/eventsSDL/InputSourceText.cpp

@@ -14,11 +14,12 @@
 #include "../CMT.h"
 #include "../gui/CGuiHandler.h"
 #include "../gui/EventDispatcher.h"
+#include "../render/IScreenHandler.h"
+#include "../renderSDL/SDL_Extensions.h"
 
 #include "../../lib/Rect.h"
 
 #include <SDL_events.h>
-#include <SDL_render.h>
 
 #ifdef VCMI_APPLE
 #	include <dispatch/dispatch.h>
@@ -40,30 +41,15 @@ void InputSourceText::handleEventTextEditing(const SDL_TextEditingEvent & text)
 
 void InputSourceText::startTextInput(const Rect & whereInput)
 {
+
 #ifdef VCMI_APPLE
 	dispatch_async(dispatch_get_main_queue(), ^{
 #endif
 
-	// TODO ios: looks like SDL bug actually, try fixing there
-	auto renderer = SDL_GetRenderer(mainWindow);
-	float scaleX, scaleY;
-	SDL_Rect viewport;
-	SDL_RenderGetScale(renderer, &scaleX, &scaleY);
-	SDL_RenderGetViewport(renderer, &viewport);
-
-#ifdef VCMI_IOS
-	const auto nativeScale = iOS_utils::screenScale();
-	scaleX /= nativeScale;
-	scaleY /= nativeScale;
-#endif
-
-	SDL_Rect rectInScreenCoordinates;
-	rectInScreenCoordinates.x = (viewport.x + whereInput.x) * scaleX;
-	rectInScreenCoordinates.y = (viewport.y + whereInput.y) * scaleY;
-	rectInScreenCoordinates.w = whereInput.w * scaleX;
-	rectInScreenCoordinates.h = whereInput.h * scaleY;
+	Rect rectInScreenCoordinates = GH.screenHandler().convertLogicalPointsToWindow(whereInput);
+	SDL_Rect textInputRect = CSDL_Ext::toSDL(rectInScreenCoordinates);
 
-	SDL_SetTextInputRect(&rectInScreenCoordinates);
+	SDL_SetTextInputRect(&textInputRect);
 
 	if (SDL_IsTextInputActive() == SDL_FALSE)
 	{

+ 4 - 0
client/render/IScreenHandler.h

@@ -12,6 +12,7 @@
 
 VCMI_LIB_NAMESPACE_BEGIN
 class Point;
+class Rect;
 VCMI_LIB_NAMESPACE_END
 
 class IScreenHandler
@@ -33,4 +34,7 @@ public:
 
 	/// Returns <min, max> range of possible values for screen scaling percentage
 	virtual std::tuple<int, int> getSupportedScalingRange() const = 0;
+
+	/// Converts provided rect from logical coordinates into coordinates within window, accounting for scaling and viewport
+	virtual Rect convertLogicalPointsToWindow(const Rect & input) const = 0;
 };

+ 26 - 0
client/renderSDL/ScreenHandler.cpp

@@ -50,6 +50,32 @@ std::tuple<int, int> ScreenHandler::getSupportedScalingRange() const
 	return { minimalScaling, maximalScaling };
 }
 
+Rect ScreenHandler::convertLogicalPointsToWindow(const Rect & input) const
+{
+	Rect result;
+
+	// FIXME: use SDL_RenderLogicalToWindow instead? Needs to be tested on ios
+
+	float scaleX, scaleY;
+	SDL_Rect viewport;
+	SDL_RenderGetScale(mainRenderer, &scaleX, &scaleY);
+	SDL_RenderGetViewport(mainRenderer, &viewport);
+
+#ifdef VCMI_IOS
+	// TODO ios: looks like SDL bug actually, try fixing there
+	const auto nativeScale = iOS_utils::screenScale();
+	scaleX /= nativeScale;
+	scaleY /= nativeScale;
+#endif
+
+	result.x = (viewport.x + input.x) * scaleX;
+	result.y = (viewport.y + input.y) * scaleY;
+	result.w = input.w * scaleX;
+	result.h = input.h * scaleY;
+
+	return result;
+}
+
 Point ScreenHandler::getPreferredLogicalResolution() const
 {
 	Point renderResolution = getPreferredRenderingResolution();

+ 1 - 0
client/renderSDL/ScreenHandler.h

@@ -86,4 +86,5 @@ public:
 	std::vector<Point> getSupportedResolutions() const final;
 	std::vector<Point> getSupportedResolutions(int displayIndex) const;
 	std::tuple<int, int> getSupportedScalingRange() const final;
+	Rect convertLogicalPointsToWindow(const Rect & input) const final;
 };