Commits

Alex Szpakowski committed 00966c4 Merge

Merged SDL2-cursor into SDL2

Comments (0)

Files changed (15)

platform/macosx/love-framework.xcodeproj/project.pbxproj

 		FA0CDE3D1710F9A50056E8D7 /* FormatHandler.h in Headers */ = {isa = PBXBuildFile; fileRef = FA0CDE3B1710F9A50056E8D7 /* FormatHandler.h */; };
 		FA5454C216F1310000D30303 /* MathModule.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA5454C016F1310000D30303 /* MathModule.cpp */; };
 		FA5454C316F1310000D30303 /* MathModule.h in Headers */ = {isa = PBXBuildFile; fileRef = FA5454C116F1310000D30303 /* MathModule.h */; };
+		FA3D9E0D16E68DE600CA6630 /* Cursor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA3D9E0B16E68DE600CA6630 /* Cursor.cpp */; };
+		FA3D9E0E16E68DE600CA6630 /* Cursor.h in Headers */ = {isa = PBXBuildFile; fileRef = FA3D9E0C16E68DE600CA6630 /* Cursor.h */; };
+		FA3D9E1116E68EAE00CA6630 /* Cursor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA3D9E0F16E68EAE00CA6630 /* Cursor.cpp */; };
+		FA3D9E1216E68EAE00CA6630 /* Cursor.h in Headers */ = {isa = PBXBuildFile; fileRef = FA3D9E1016E68EAE00CA6630 /* Cursor.h */; };
+		FA3D9E1516E6D41100CA6630 /* wrap_Cursor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA3D9E1316E6D41000CA6630 /* wrap_Cursor.cpp */; };
+		FA3D9E1616E6D41100CA6630 /* wrap_Cursor.h in Headers */ = {isa = PBXBuildFile; fileRef = FA3D9E1416E6D41000CA6630 /* wrap_Cursor.h */; };
 		FA577AB016C7507900860150 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA577A7916C71A1700860150 /* Cocoa.framework */; };
 		FA577AC216C7512D00860150 /* FreeType.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA577A6716C719D900860150 /* FreeType.framework */; };
 		FA577AC316C7512F00860150 /* Game_Music_Emu.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA577A6B16C719E400860150 /* Game_Music_Emu.framework */; };
 		FA0CDE3B1710F9A50056E8D7 /* FormatHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FormatHandler.h; sourceTree = "<group>"; };
 		FA5454C016F1310000D30303 /* MathModule.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MathModule.cpp; sourceTree = "<group>"; };
 		FA5454C116F1310000D30303 /* MathModule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MathModule.h; sourceTree = "<group>"; };
+		FA3D9E0B16E68DE600CA6630 /* Cursor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Cursor.cpp; sourceTree = "<group>"; };
+		FA3D9E0C16E68DE600CA6630 /* Cursor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Cursor.h; sourceTree = "<group>"; };
+		FA3D9E0F16E68EAE00CA6630 /* Cursor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Cursor.cpp; sourceTree = "<group>"; };
+		FA3D9E1016E68EAE00CA6630 /* Cursor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Cursor.h; sourceTree = "<group>"; };
+		FA3D9E1316E6D41000CA6630 /* wrap_Cursor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = wrap_Cursor.cpp; sourceTree = "<group>"; };
+		FA3D9E1416E6D41000CA6630 /* wrap_Cursor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = wrap_Cursor.h; sourceTree = "<group>"; };
 		FA577A6716C719D900860150 /* FreeType.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = FreeType.framework; path = /Library/Frameworks/FreeType.framework; sourceTree = "<absolute>"; };
 		FA577A6916C719DE00860150 /* IL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = IL.framework; path = /Library/Frameworks/IL.framework; sourceTree = "<absolute>"; };
 		FA577A6B16C719E400860150 /* Game_Music_Emu.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Game_Music_Emu.framework; path = /Library/Frameworks/Game_Music_Emu.framework; sourceTree = "<absolute>"; };
 		130737AF4BD12D0356A65C87 /* mouse */ = {
 			isa = PBXGroup;
 			children = (
+				FA3D9E0B16E68DE600CA6630 /* Cursor.cpp */,
+				FA3D9E0C16E68DE600CA6630 /* Cursor.h */,
 				31E0110E5797041465FF5F95 /* Mouse.cpp */,
 				63A63DFB339266AC401B545A /* Mouse.h */,
 				1E017A8673D531394B5A3B16 /* sdl */,
+				FA3D9E1316E6D41000CA6630 /* wrap_Cursor.cpp */,
+				FA3D9E1416E6D41000CA6630 /* wrap_Cursor.h */,
 				22EF17981EBD442773FE41B6 /* wrap_Mouse.cpp */,
 				4E972C114A6C25A63B5B6EF2 /* wrap_Mouse.h */,
 			);
 		1E017A8673D531394B5A3B16 /* sdl */ = {
 			isa = PBXGroup;
 			children = (
+				FA3D9E0F16E68EAE00CA6630 /* Cursor.cpp */,
+				FA3D9E1016E68EAE00CA6630 /* Cursor.h */,
 				584E16AE09E12536206C46FE /* Mouse.cpp */,
 				198A44BD71BB61EE517C2A39 /* Mouse.h */,
 			);
 				FAC86E6A1724555D00EED715 /* DrawGable.h in Headers */,
 				FAC86E6C1724555D00EED715 /* Geometry.h in Headers */,
 				FA03546D1731F3A700284828 /* simplexnoise1234.h in Headers */,
+				FA3D9E0E16E68DE600CA6630 /* Cursor.h in Headers */,
+				FA3D9E1216E68EAE00CA6630 /* Cursor.h in Headers */,
+				FA3D9E1616E6D41100CA6630 /* wrap_Cursor.h in Headers */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
 				FAC86E691724555D00EED715 /* DrawGable.cpp in Sources */,
 				FAC86E6B1724555D00EED715 /* Geometry.cpp in Sources */,
 				FA03546C1731F3A700284828 /* simplexnoise1234.cpp in Sources */,
+				FA3D9E0D16E68DE600CA6630 /* Cursor.cpp in Sources */,
+				FA3D9E1116E68EAE00CA6630 /* Cursor.cpp in Sources */,
+				FA3D9E1516E6D41100CA6630 /* wrap_Cursor.cpp in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

src/common/runtime.cpp

 	{"SoundData", SOUND_SOUND_DATA_ID},
 	{"Decoder", SOUND_DECODER_ID},
 
+	// Mouse
+	{"Cursor", MOUSE_CURSOR_ID},
+
 	// Physics
 	{"World", PHYSICS_WORLD_ID},
 	{"Contact", PHYSICS_CONTACT_ID},

src/common/types.h

 	SOUND_SOUND_DATA_ID,
 	SOUND_DECODER_ID,
 
+	// Mouse
+	MOUSE_CURSOR_ID,
+
 	// Physics
 	PHYSICS_WORLD_ID,
 	PHYSICS_CONTACT_ID,
 const bits SOUND_SOUND_DATA_T = (bits(1) << SOUND_SOUND_DATA_ID) | DATA_T;
 const bits SOUND_DECODER_T = bits(1) << SOUND_DECODER_ID;
 
+// Mouse.
+const bits MOUSE_CURSOR_T = (bits(1) << MOUSE_CURSOR_ID) | OBJECT_T;
+
 // Physics.
 const bits PHYSICS_WORLD_T = (bits(1) << PHYSICS_WORLD_ID) | OBJECT_T;
 const bits PHYSICS_CONTACT_T = (bits(1) << PHYSICS_CONTACT_ID) | OBJECT_T;

src/modules/event/sdl/Event.cpp

File contents unchanged.

src/modules/mouse/Cursor.cpp

+/**
+ * Copyright (c) 2006-2013 LOVE Development Team
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty.  In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ *    claim that you wrote the original software. If you use this software
+ *    in a product, an acknowledgment in the product documentation would be
+ *    appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ *    misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ **/
+
+#include "Cursor.h"
+
+namespace love
+{
+namespace mouse
+{
+
+Cursor::~Cursor()
+{
+}
+
+bool Cursor::getConstant(const char *in, SystemCursor &out)
+{
+	return systemCursors.find(in, out);
+}
+
+bool Cursor::getConstant(SystemCursor in, const char *&out)
+{
+	return systemCursors.find(in, out);
+}
+
+bool Cursor::getConstant(const char *in, CursorType &out)
+{
+	return types.find(in, out);
+}
+
+bool Cursor::getConstant(CursorType in, const char *&out)
+{
+	return types.find(in, out);
+}
+
+StringMap<Cursor::SystemCursor, Cursor::CURSOR_MAX_ENUM>::Entry Cursor::systemCursorEntries[] =
+{
+	{"arrow", Cursor::CURSOR_ARROW},
+	{"ibeam", Cursor::CURSOR_IBEAM},
+	{"wait", Cursor::CURSOR_WAIT},
+	{"crosshair", Cursor::CURSOR_CROSSHAIR},
+	{"waitarrow", Cursor::CURSOR_WAITARROW},
+	{"sizenwse", Cursor::CURSOR_SIZENWSE},
+	{"sizenesw", Cursor::CURSOR_SIZENESW},
+	{"sizewe", Cursor::CURSOR_SIZEWE},
+	{"sizens", Cursor::CURSOR_SIZENS},
+	{"sizeall", Cursor::CURSOR_SIZEALL},
+	{"no", Cursor::CURSOR_NO},
+	{"hand", Cursor::CURSOR_HAND},
+};
+
+StringMap<Cursor::SystemCursor, Cursor::CURSOR_MAX_ENUM> Cursor::systemCursors(Cursor::systemCursorEntries, sizeof(Cursor::systemCursorEntries));
+
+StringMap<Cursor::CursorType, Cursor::CURSORTYPE_MAX_ENUM>::Entry Cursor::typeEntries[] =
+{
+	{"system", Cursor::CURSORTYPE_SYSTEM},
+	{"image", Cursor::CURSORTYPE_IMAGE},
+};
+
+StringMap<Cursor::CursorType, Cursor::CURSORTYPE_MAX_ENUM> Cursor::types(Cursor::typeEntries, sizeof(Cursor::typeEntries));
+
+} // mouse
+} // love

src/modules/mouse/Cursor.h

+/**
+ * Copyright (c) 2006-2013 LOVE Development Team
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty.  In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ *    claim that you wrote the original software. If you use this software
+ *    in a product, an acknowledgment in the product documentation would be
+ *    appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ *    misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ **/
+
+#ifndef LOVE_MOUSE_CURSOR_H
+#define LOVE_MOUSE_CURSOR_H
+
+// LOVE
+#include "image/ImageData.h"
+#include "common/Object.h"
+#include "common/StringMap.h"
+
+namespace love
+{
+namespace mouse
+{
+
+class Cursor : public Object
+{
+public:
+
+	// Types of system cursors.
+	enum SystemCursor
+	{
+		CURSOR_ARROW,
+		CURSOR_IBEAM,
+		CURSOR_WAIT,
+		CURSOR_CROSSHAIR,
+		CURSOR_WAITARROW,
+		CURSOR_SIZENWSE,
+		CURSOR_SIZENESW,
+		CURSOR_SIZEWE,
+		CURSOR_SIZENS,
+		CURSOR_SIZEALL,
+		CURSOR_NO,
+		CURSOR_HAND,
+		CURSOR_MAX_ENUM
+	};
+
+	enum CursorType
+	{
+		CURSORTYPE_SYSTEM,
+		CURSORTYPE_IMAGE,
+		CURSORTYPE_MAX_ENUM
+	};
+
+	virtual ~Cursor();
+
+	/**
+	 * 
+	 **/
+	virtual void *getHandle() const = 0;
+
+	/**
+	 * Returns whether this Cursor is system-defined or a custom image.
+	 **/
+	virtual CursorType getType() const = 0;
+
+	static bool getConstant(const char *in, SystemCursor &out);
+	static bool getConstant(SystemCursor in, const char *&out);
+
+	static bool getConstant(const char *in, CursorType &out);
+	static bool getConstant(CursorType in, const char *&out);
+
+private:
+
+	static StringMap<SystemCursor, CURSOR_MAX_ENUM>::Entry systemCursorEntries[];
+	static StringMap<SystemCursor, CURSOR_MAX_ENUM> systemCursors;
+
+	static StringMap<CursorType, CURSORTYPE_MAX_ENUM>::Entry typeEntries[];
+	static StringMap<CursorType, CURSORTYPE_MAX_ENUM> types;
+
+};
+
+} // mouse
+} // love
+
+#endif // LOVE_MOUSE_CURSOR_H

src/modules/mouse/Mouse.h

 #define LOVE_MOUSE_MOUSE_H
 
 // LOVE
+#include "Cursor.h"
 #include "common/Module.h"
 #include "common/StringMap.h"
+#include "image/ImageData.h"
 
 namespace love
 {
 
 	virtual ~Mouse() {};
 
+	virtual Cursor *newCursor(love::image::ImageData *data, int hotx, int hoty) = 0;
+	virtual Cursor *newCursor(Cursor::SystemCursor cursortype) = 0;
+
+	virtual void setCursor(Cursor *cursor) = 0;
+	virtual void setCursor() = 0;
+
+	virtual Cursor *getCursor() const = 0;
+
 	virtual int getX() const = 0;
 	virtual int getY() const = 0;
 	virtual void getPosition(int &x, int &y) const = 0;

src/modules/mouse/sdl/Cursor.cpp

+/**
+ * Copyright (c) 2006-2013 LOVE Development Team
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty.  In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ *    claim that you wrote the original software. If you use this software
+ *    in a product, an acknowledgment in the product documentation would be
+ *    appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ *    misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ **/
+
+// LOVE
+#include "Cursor.h"
+#include "common/config.h"
+
+namespace love
+{
+namespace mouse
+{
+namespace sdl
+{
+
+Cursor::Cursor(image::ImageData *data, int hotx, int hoty)
+	: type(CURSORTYPE_IMAGE)
+{
+	Uint32 rmask, gmask, bmask, amask;
+#ifdef LOVE_BIG_ENDIAN
+	rmask = 0xFF000000;
+	gmask = 0x00FF0000;
+	bmask = 0x0000FF00;
+	amask = 0x000000FF;
+#else
+	rmask = 0x000000FF;
+	gmask = 0x0000FF00;
+	bmask = 0x00FF0000;
+	amask = 0xFF000000;
+#endif
+
+	int w = data->getWidth();
+	int h = data->getHeight();
+	int pitch = w * 4;
+
+	SDL_Surface *surface = SDL_CreateRGBSurfaceFrom(data->getData(), w, h, 32, pitch, rmask, gmask, bmask, amask);
+	if (!surface)
+		throw love::Exception("Cannot create cursor: out of memory!");
+
+	cursor = SDL_CreateColorCursor(surface, hotx, hoty);
+	SDL_FreeSurface(surface);
+
+	if (!cursor)
+		throw love::Exception("Cannot create cursor: %s", SDL_GetError());
+}
+
+Cursor::Cursor(mouse::Cursor::SystemCursor cursortype)
+	: type(CURSORTYPE_SYSTEM)
+{
+	SDL_SystemCursor sdlcursor;
+
+	if (systemCursors.find(cursortype, sdlcursor))
+		cursor = SDL_CreateSystemCursor(sdlcursor);
+	else
+		throw love::Exception("Cannot create system cursor: unknown type!");
+
+	if (!cursor)
+		throw love::Exception("Cannot create system cursor: %s", SDL_GetError());
+}
+
+Cursor::~Cursor()
+{
+	SDL_FreeCursor(cursor);
+}
+
+void *Cursor::getHandle() const
+{
+	return cursor;
+}
+
+mouse::Cursor::CursorType Cursor::getType() const
+{
+	return type;
+}
+
+EnumMap<mouse::Cursor::SystemCursor, SDL_SystemCursor, mouse::Cursor::CURSOR_MAX_ENUM>::Entry Cursor::systemCursorEntries[] =
+{
+	{mouse::Cursor::CURSOR_ARROW, SDL_SYSTEM_CURSOR_ARROW},
+	{mouse::Cursor::CURSOR_IBEAM, SDL_SYSTEM_CURSOR_IBEAM},
+	{mouse::Cursor::CURSOR_WAIT, SDL_SYSTEM_CURSOR_WAIT},
+	{mouse::Cursor::CURSOR_CROSSHAIR, SDL_SYSTEM_CURSOR_CROSSHAIR},
+	{mouse::Cursor::CURSOR_WAITARROW, SDL_SYSTEM_CURSOR_WAITARROW},
+	{mouse::Cursor::CURSOR_SIZENWSE, SDL_SYSTEM_CURSOR_SIZENWSE},
+	{mouse::Cursor::CURSOR_SIZENESW, SDL_SYSTEM_CURSOR_SIZENESW},
+	{mouse::Cursor::CURSOR_SIZEWE, SDL_SYSTEM_CURSOR_SIZEWE},
+	{mouse::Cursor::CURSOR_SIZENS, SDL_SYSTEM_CURSOR_SIZENS},
+	{mouse::Cursor::CURSOR_SIZEALL, SDL_SYSTEM_CURSOR_SIZEALL},
+	{mouse::Cursor::CURSOR_NO, SDL_SYSTEM_CURSOR_NO},
+	{mouse::Cursor::CURSOR_HAND, SDL_SYSTEM_CURSOR_HAND},
+};
+
+EnumMap<mouse::Cursor::SystemCursor, SDL_SystemCursor, mouse::Cursor::CURSOR_MAX_ENUM> Cursor::systemCursors(Cursor::systemCursorEntries, sizeof(Cursor::systemCursorEntries));
+
+} // sdl
+} // mouse
+} // love

src/modules/mouse/sdl/Cursor.h

+/**
+ * Copyright (c) 2006-2013 LOVE Development Team
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty.  In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ *    claim that you wrote the original software. If you use this software
+ *    in a product, an acknowledgment in the product documentation would be
+ *    appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ *    misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ **/
+
+#ifndef LOVE_MOUSE_SDL_CURSOR_H
+#define LOVE_MOUSE_SDL_CURSOR_H
+
+// LOVE
+#include "mouse/Cursor.h"
+#include "common/EnumMap.h"
+
+// SDL
+#include <SDL_mouse.h>
+
+namespace love
+{
+namespace mouse
+{
+namespace sdl
+{
+
+class Cursor : public love::mouse::Cursor
+{
+public:
+
+	Cursor(image::ImageData *imageData, int hotx, int hoty);
+	Cursor(mouse::Cursor::SystemCursor cursortype);
+	~Cursor();
+
+	void *getHandle() const;
+	mouse::Cursor::CursorType getType() const;
+
+private:
+
+	SDL_Cursor *cursor;
+	mouse::Cursor::CursorType type;
+
+	static EnumMap<mouse::Cursor::SystemCursor, SDL_SystemCursor, mouse::Cursor::CURSOR_MAX_ENUM>::Entry systemCursorEntries[];
+	static EnumMap<mouse::Cursor::SystemCursor, SDL_SystemCursor, mouse::Cursor::CURSOR_MAX_ENUM> systemCursors;
+};
+
+} // sdl
+} // mouse
+} // love
+
+#endif // LOVE_MOUSE_SDL_CURSOR_H

src/modules/mouse/sdl/Mouse.cpp

  * 3. This notice may not be removed or altered from any source distribution.
  **/
 
+// LOVE
 #include "Mouse.h"
-
 #include "window/sdl/Window.h"
 
 // SDL
-#include <SDL.h>
+#include <SDL_mouse.h>
 
 namespace love
 {
 	return "love.mouse.sdl";
 }
 
+Mouse::Mouse()
+	: curCursor(0)
+{
+}
+
+Mouse::~Mouse()
+{
+	if (curCursor)
+		setCursor();
+}
+
+love::mouse::Cursor *Mouse::newCursor(love::image::ImageData *data, int hotx, int hoty)
+{
+	return new Cursor(data, hotx, hoty);
+}
+
+love::mouse::Cursor *Mouse::newCursor(love::mouse::Cursor::SystemCursor cursortype)
+{
+	return new Cursor(cursortype);
+}
+
+void Mouse::setCursor(love::mouse::Cursor *cursor)
+{
+	love::mouse::Cursor *oldcursor = curCursor;
+
+	curCursor = cursor;
+	curCursor->retain();
+
+	SDL_SetCursor((SDL_Cursor *) cursor->getHandle());
+
+	if (oldcursor)
+		oldcursor->release();
+}
+
+void Mouse::setCursor()
+{
+	if (curCursor)
+		curCursor->release();
+
+	curCursor = NULL;
+
+	SDL_SetCursor(SDL_GetDefaultCursor());
+}
+
+love::mouse::Cursor *Mouse::getCursor() const
+{
+	return curCursor;
+}
+
 int Mouse::getX() const
 {
 	int x;

src/modules/mouse/sdl/Mouse.h

 
 // LOVE
 #include "mouse/Mouse.h"
+#include "Cursor.h"
 
 namespace love
 {
 	// Implements Module.
 	const char *getName() const;
 
+	Mouse();
+	~Mouse();
+
+	love::mouse::Cursor *newCursor(love::image::ImageData *data, int hotx, int hoty);
+	love::mouse::Cursor *newCursor(love::mouse::Cursor::SystemCursor cursortype);
+
+	void setCursor(love::mouse::Cursor *cursor);
+	void setCursor();
+
+	love::mouse::Cursor *getCursor() const;
+
 	int getX() const;
 	int getY() const;
 	void getPosition(int &x, int &y) const;
 	bool isVisible() const;
 	void setGrab(bool grab);
 	bool isGrabbed() const;
+
+private:
+
+	love::mouse::Cursor *curCursor;
+
 }; // Mouse
 
 } // sdl

src/modules/mouse/wrap_Cursor.cpp

+/**
+ * Copyright (c) 2006-2013 LOVE Development Team
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty.  In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ *    claim that you wrote the original software. If you use this software
+ *    in a product, an acknowledgment in the product documentation would be
+ *    appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ *    misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ **/
+
+// LOVE
+#include "wrap_Cursor.h"
+#include "sdl/Cursor.h"
+
+namespace love
+{
+namespace mouse
+{
+
+int w_getType(lua_State *L)
+{
+	mouse::Cursor *cursor = luax_checktype<mouse::Cursor>(L, 1, "Cursor", MOUSE_CURSOR_T);
+
+	const char *str;
+	if (mouse::Cursor::getConstant(cursor->getType(), str))
+		lua_pushstring(L, str);
+	else
+		return luaL_error(L, "Unknown cursor type.");
+
+	return 1;
+};
+
+static const luaL_Reg functions[] =
+{
+	{ "getType", w_getType },
+	{ 0, 0 },
+};
+
+extern "C" int luaopen_cursor(lua_State *L)
+{
+	return luax_register_type(L, "Cursor", functions);
+}
+
+} // mouse
+} // love

src/modules/mouse/wrap_Cursor.h

+/**
+ * Copyright (c) 2006-2013 LOVE Development Team
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty.  In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ *    claim that you wrote the original software. If you use this software
+ *    in a product, an acknowledgment in the product documentation would be
+ *    appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ *    misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ **/
+
+#ifndef LOVE_MOUSE_WRAP_CURSOR_H
+#define LOVE_MOUSE_WRAP_CURSOR_H
+
+// LOVE
+#include "common/runtime.h"
+
+namespace love
+{
+namespace mouse
+{
+
+int w_getType(lua_State *L);
+extern "C" int luaopen_cursor(lua_State *L);
+
+} // mouse
+} // love
+
+
+#endif // LOVE_MOUSE_WRAP_CURSOR_H

src/modules/mouse/wrap_Mouse.cpp

  * 3. This notice may not be removed or altered from any source distribution.
  **/
 
+// LOVE
+#include "wrap_Mouse.h"
+#include "wrap_Cursor.h"
 #include "common/config.h"
 
 #include "sdl/Mouse.h"
 
-#include "wrap_Mouse.h"
-
 namespace love
 {
 namespace mouse
 
 static Mouse *instance = 0;
 
+int w_newCursor(lua_State *L)
+{
+	Cursor *cursor = 0;
+
+	if (lua_isstring(L, 1))
+	{
+		// System cursor type.
+		const char *str = luaL_checkstring(L, 1);
+		Cursor::SystemCursor systemCursor;
+
+		if (Cursor::getConstant(str, systemCursor))
+		{
+			try
+			{
+				cursor = instance->newCursor(systemCursor);
+			}
+			catch (love::Exception &e)
+			{
+				return luaL_error(L, "%s", e.what());
+			}
+		}
+		else
+			return luaL_error(L, "Invalid cursor type %s", str);
+	}
+	else
+	{
+		// Custom image.
+		love::image::ImageData *data = luax_checktype<love::image::ImageData>(L, 1, "ImageData", IMAGE_IMAGE_DATA_T);
+		int hotx = luaL_optint(L, 2, 0);
+		int hoty = luaL_optint(L, 3, 0);
+
+		try
+		{
+			cursor = instance->newCursor(data, hotx, hoty);
+		}
+		catch (love::Exception &e)
+		{
+			return luaL_error(L, "%s", e.what());
+		}
+	}
+
+	luax_newtype(L, "Cursor", MOUSE_CURSOR_T, (void *) cursor);
+
+	return 1;
+}
+
+int w_setCursor(lua_State *L)
+{
+	// Revert to the default system cursor if no argument is given.
+	if (lua_isnoneornil(L, 1))
+	{
+		instance->setCursor();
+		return 0;
+	}
+
+	Cursor *cursor = luax_checktype<Cursor>(L, 1, "Cursor", MOUSE_CURSOR_T);
+	instance->setCursor(cursor);
+	return 0;
+}
+
+int w_getCursor(lua_State *L)
+{
+	Cursor *cursor = instance->getCursor();
+
+	if (cursor)
+	{
+		cursor->retain();
+		luax_newtype(L, "Cursor", MOUSE_CURSOR_T, (void *) cursor);
+	}
+	else
+		lua_pushnil(L);
+
+	return 1;
+}
+
 int w_getX(lua_State *L)
 {
 	lua_pushnumber(L, instance->getX());
 // List of functions to wrap.
 static const luaL_Reg functions[] =
 {
+	{ "newCursor", w_newCursor },
+	{ "setCursor", w_setCursor },
+	{ "getCursor", w_getCursor },
 	{ "getX", w_getX },
 	{ "getY", w_getY },
 	{ "setX", w_setX },
 	{ 0, 0 }
 };
 
+// Types for this module.
+static const lua_CFunction types[] =
+{
+	luaopen_cursor,
+	0,
+};
+
 extern "C" int luaopen_love_mouse(lua_State *L)
 {
 	if (instance == 0)
 	w.name = "mouse";
 	w.flags = MODULE_T;
 	w.functions = functions;
-	w.types = 0;
+	w.types = types;
 
 	return luax_register_module(L, w);
 }

src/modules/mouse/wrap_Mouse.h

 #define LOVE_MOUSE_WRAP_MOUSE_H
 
 // LOVE
-#include "Mouse.h"
+#include "common/runtime.h"
+#include "common/config.h"
 
 namespace love
 {
 namespace mouse
 {
 
+int w_newCursor(lua_State *L);
+int w_setCursor(lua_State *L);
+int w_getCursor(lua_State *L);
 int w_getX(lua_State *L);
 int w_getY(lua_State *L);
 int w_getPosition(lua_State *L);