Просмотр исходного кода

Merge pull request #368 from christopherreed/linux-SDLCore-setCursor

[Linux] Implemented setCursor in PolySDLCore.
Ivan Safrin 12 лет назад
Родитель
Сommit
a438979e68
1 измененных файлов с 84 добавлено и 2 удалено
  1. 84 2
      Core/Contents/Source/PolySDLCore.cpp

+ 84 - 2
Core/Contents/Source/PolySDLCore.cpp

@@ -53,6 +53,16 @@
 	void put_scrap(int type, int srclen, const char *src);
 	void put_scrap(int type, int srclen, const char *src);
 	void get_scrap(int type, int *dstlen, char **dst);
 	void get_scrap(int type, int *dstlen, char **dst);
 	// end SDL scrap
 	// end SDL scrap
+
+// X11 cursor
+#include <X11/cursorfont.h>
+
+namespace {
+	void set_cursor(int cursorType);
+	void free_cursors();
+} // namespace
+// end X11 cursor
+
 #endif
 #endif
 
 
 using namespace Polycode;
 using namespace Polycode;
@@ -111,11 +121,13 @@ SDLCore::SDLCore(PolycodeView *view, int _xRes, int _yRes, bool fullScreen, bool
 		SDL_JoystickOpen(i);
 		SDL_JoystickOpen(i);
 		input->addJoystick(i);
 		input->addJoystick(i);
 	}
 	}
-	
+
+#ifdef USE_X11
 	// Start listening to clipboard events.
 	// Start listening to clipboard events.
 	// (Yes on X11 you need to actively listen to
 	// (Yes on X11 you need to actively listen to
 	//  clipboard events and respond to them)
 	//  clipboard events and respond to them)
 	init_scrap();
 	init_scrap();
+#endif // USE_X11
 
 
 	((OpenGLRenderer*)renderer)->Init();
 	((OpenGLRenderer*)renderer)->Init();
 	CoreServices::getInstance()->installModule(new GLSLShaderModule());	
 	CoreServices::getInstance()->installModule(new GLSLShaderModule());	
@@ -184,6 +196,9 @@ vector<Polycode::Rectangle> SDLCore::getVideoModes() {
 }
 }
 
 
 SDLCore::~SDLCore() {
 SDLCore::~SDLCore() {
+#ifdef USE_X11
+	free_cursors();
+#endif // USE_X11
 	SDL_Quit();
 	SDL_Quit();
 }
 }
 
 
@@ -396,7 +411,9 @@ bool SDLCore::Update() {
 }
 }
 
 
 void SDLCore::setCursor(int cursorType) {
 void SDLCore::setCursor(int cursorType) {
-
+#ifdef USE_X11
+	set_cursor(cursorType);
+#endif // USE_X11
 }
 }
 
 
 void SDLCore::warpCursor(int x, int y) {
 void SDLCore::warpCursor(int x, int y) {
@@ -860,4 +877,69 @@ PRIVATE int clipboard_filter(const SDL_Event *event)
   return(1);
   return(1);
 }
 }
 
 
+// X11 cursor
+
+namespace {
+
+// WARNING: These functions rely on the SDL_Display and SDL_Window previously initialized by init_scrap
+
+const int CURSOR_COUNT = 7;
+Cursor defined_cursors[CURSOR_COUNT] = {0};
+
+void set_cursor(int cursorType) {
+	Cursor cursor = 0;
+	
+	if(cursorType >= 0 && cursorType < CURSOR_COUNT) {
+		cursor = defined_cursors[cursorType];
+		if(!cursor) {
+			switch(cursorType) {
+				case Polycode::Core::CURSOR_TEXT:
+					cursor = XCreateFontCursor (SDL_Display, XC_xterm);
+				break;
+				case Polycode::Core::CURSOR_POINTER:
+					cursor = XCreateFontCursor (SDL_Display, XC_hand1);
+				break;
+				case Polycode::Core::CURSOR_CROSSHAIR:
+					cursor = XCreateFontCursor (SDL_Display, XC_crosshair);
+				break;
+				case Polycode::Core::CURSOR_RESIZE_LEFT_RIGHT:
+					cursor = XCreateFontCursor (SDL_Display, XC_sb_h_double_arrow);
+				break;
+				case Polycode::Core::CURSOR_RESIZE_UP_DOWN:
+					cursor = XCreateFontCursor (SDL_Display, XC_sb_v_double_arrow);
+				break;
+				case Polycode::Core::CURSOR_OPEN_HAND:
+					cursor = XCreateFontCursor (SDL_Display, XC_fleur);
+				break;
+				case Polycode::Core::CURSOR_ARROW:
+					cursor = 0;
+				break;
+			}
+
+			defined_cursors[cursorType] = cursor;
+		}
+	}
+
+	if(!cursor) {
+		// Restore the normal cursor.
+		XUndefineCursor(SDL_Display, SDL_Window);
+	} else {
+		XDefineCursor(SDL_Display, SDL_Window, cursor);
+	}
+	
+	XFlush(SDL_Display);
+}
+
+void free_cursors() {
+	XUndefineCursor(SDL_Display, SDL_Window);
+	for(int i = 0; i < CURSOR_COUNT; i++) {
+		if(defined_cursors[i]) {
+			XFreeCursor(SDL_Display, defined_cursors[i]);
+			defined_cursors[i] = 0;
+		}
+	}
+}
+} // namespace
+// end X11 cursor
+
 #endif // USE_X11
 #endif // USE_X11