Browse Source

Use lightuserdata instead of Lua numbers (doubles) to represent touch IDs.

--HG--
branch : minor
Alex Szpakowski 10 years ago
parent
commit
5652475dd2
3 changed files with 17 additions and 6 deletions
  1. 0 2
      src/common/int.h
  2. 6 1
      src/modules/event/sdl/Event.cpp
  3. 11 3
      src/modules/touch/wrap_Touch.cpp

+ 0 - 2
src/common/int.h

@@ -22,8 +22,6 @@
 #define LOVE_INT_H
 #define LOVE_INT_H
 
 
 // C standard sized integer types.
 // C standard sized integer types.
-// This header was added to Visual studio in VS 2012, which is LOVE's current
-// minimum supported VS version (as of this comment's commit date.)
 #include <stdint.h>
 #include <stdint.h>
 
 
 #define LOVE_INT8_MAX   0x7F
 #define LOVE_INT8_MAX   0x7F

+ 6 - 1
src/modules/event/sdl/Event.cpp

@@ -302,7 +302,12 @@ Message *Event::convert(const SDL_Event &e) const
 		if (touchmodule)
 		if (touchmodule)
 			touchmodule->onEvent(e.type, touchinfo);
 			touchmodule->onEvent(e.type, touchinfo);
 
 
-		vargs.push_back(new Variant((double) touchinfo.id));
+		// This is a bit hackish and we lose the higher 32 bits of the id on
+		// 32-bit systems, but SDL only ever gives id's that at most use as many
+		// bits as can fit in a pointer (for now.)
+		// We use lightuserdata instead of a lua_Number (double) because doubles
+		// can't represent all possible id values on 64-bit systems.
+		vargs.push_back(new Variant((void *) (intptr_t) touchinfo.id));
 		vargs.push_back(new Variant(touchinfo.x));
 		vargs.push_back(new Variant(touchinfo.x));
 		vargs.push_back(new Variant(touchinfo.y));
 		vargs.push_back(new Variant(touchinfo.y));
 		vargs.push_back(new Variant(touchinfo.dx));
 		vargs.push_back(new Variant(touchinfo.dx));

+ 11 - 3
src/modules/touch/wrap_Touch.cpp

@@ -19,6 +19,7 @@
  **/
  **/
 
 
 #include "common/config.h"
 #include "common/config.h"
+#include "common/int.h"
 
 
 // LOVE
 // LOVE
 #include "wrap_Touch.h"
 #include "wrap_Touch.h"
@@ -40,8 +41,12 @@ int w_getTouchIDs(lua_State *L)
 
 
 	for (size_t i = 0; i < ids.size(); i++)
 	for (size_t i = 0; i < ids.size(); i++)
 	{
 	{
-		// Lets hope the ID can be accurately represented in a Lua number...
-		lua_pushnumber(L, (lua_Number) ids[i]);
+		// This is a bit hackish and we lose the higher 32 bits of the id on
+		// 32-bit systems, but SDL only ever gives id's that at most use as many
+		// bits as can fit in a pointer (for now.)
+		// We use lightuserdata instead of a lua_Number (double) because doubles
+		// can't represent all possible id values on 64-bit systems.
+		lua_pushlightuserdata(L, (void *) (intptr_t) ids[i]);
 		lua_rawseti(L, -2, i + 1);
 		lua_rawseti(L, -2, i + 1);
 	}
 	}
 
 
@@ -50,7 +55,10 @@ int w_getTouchIDs(lua_State *L)
 
 
 int w_getPosition(lua_State *L)
 int w_getPosition(lua_State *L)
 {
 {
-	int64 id = (int64) luaL_checknumber(L, 1);
+	if (!lua_islightuserdata(L, 1))
+		return luax_typerror(L, 1, "touch id");
+
+	int64 id = (int64) (intptr_t) lua_touserdata(L, 1);
 
 
 	double x = 0;
 	double x = 0;
 	double y = 0;
 	double y = 0;