Browse Source

Implemented hardware cursor support

Ivan Savenko 2 years ago
parent
commit
26a1c5801b
2 changed files with 54 additions and 4 deletions
  1. 40 1
      client/gui/CursorHandler.cpp
  2. 14 3
      client/gui/CursorHandler.h

+ 40 - 1
client/gui/CursorHandler.cpp

@@ -20,7 +20,7 @@
 #include "../CMT.h"
 
 CursorHandler::CursorHandler()
-	: cursorSW(new CursorSoftware())
+	: cursorSW(new CursorHardware())
 	, frameTime(0.f)
 	, showing(false)
 	, pos(0,0)
@@ -52,6 +52,9 @@ void CursorHandler::changeGraphic(Cursor::Type type, size_t index)
 {
 	assert(dndObject == nullptr);
 
+	if (type == this->type && index == this->frame)
+		return;
+
 	this->type = type;
 	this->frame = index;
 
@@ -346,3 +349,39 @@ CursorSoftware::~CursorSoftware()
 		SDL_FreeSurface(cursorSurface);
 
 }
+
+CursorHardware::CursorHardware():
+	cursor(nullptr)
+{
+}
+
+CursorHardware::~CursorHardware()
+{
+	if(cursor)
+		SDL_FreeCursor(cursor);
+}
+
+void CursorHardware::setImage(std::shared_ptr<IImage> image, const Point & pivotOffset)
+{
+	auto cursorSurface = CSDL_Ext::newSurface(image->dimensions().x, image->dimensions().y);
+
+	image->draw(cursorSurface);
+
+	cursor = SDL_CreateColorCursor(cursorSurface, pivotOffset.x, pivotOffset.y);
+
+	if (!cursor)
+		logGlobal->error("Failed to set cursor! SDL says %s", SDL_GetError());
+
+	SDL_FreeSurface(cursorSurface);
+	SDL_SetCursor(cursor);
+}
+
+void CursorHardware::setCursorPosition( const Point & newPos )
+{
+	//no-op
+}
+
+void CursorHardware::render()
+{
+	//no-op
+}

+ 14 - 3
client/gui/CursorHandler.h

@@ -13,6 +13,7 @@ class CAnimation;
 class IImage;
 struct SDL_Surface;
 struct SDL_Texture;
+struct SDL_Cursor;
 
 #include "Geometries.h"
 
@@ -113,7 +114,18 @@ namespace Cursor
 
 class CursorHardware
 {
-	//TODO
+	std::shared_ptr<IImage> cursorImage;
+
+	SDL_Cursor * cursor;
+
+public:
+	CursorHardware();
+	~CursorHardware();
+
+	void setImage(std::shared_ptr<IImage> image, const Point & pivotOffset);
+	void setCursorPosition( const Point & newPos );
+
+	void render();
 };
 
 class CursorSoftware
@@ -137,7 +149,6 @@ public:
 	void setCursorPosition( const Point & newPos );
 
 	void render();
-	void setVisible(bool on);
 };
 
 /// handles mouse cursor
@@ -167,7 +178,7 @@ class CursorHandler final
 
 	std::shared_ptr<IImage> getCurrentImage();
 
-	std::unique_ptr<CursorSoftware> cursorSW;
+	std::unique_ptr<CursorHardware> cursorSW;
 public:
 	CursorHandler();
 	~CursorHandler();