More bindings & scripting API changes scripting
authorHarley Laue <losinggeneration@gmail.com>
Mon, 22 Aug 2011 13:44:15 -0500
branchscripting
changeset 159 a097f09f7e01
parent 158 667a3771d43e
child 160 611dfa42cd91
More bindings & scripting API changes

* Bound hgeColorRGB, hgeColorHSV, and hgeResourceManager (partially)
* Fixed some minor issues with hgeStringTable
* Made the API more Lua-like
* Minor changes to the test script to test StringTable and cleanup the
code a bit
src/core/scripting/lua/CMakeLists.txt
src/core/scripting/lua/binding/hgeBinding.lua
src/core/scripting/lua/binding/hgecolor.lua
src/core/scripting/lua/binding/hgerect.lua
src/core/scripting/lua/binding/hgeresource.lua
src/core/scripting/lua/binding/hgestrings.lua
src/core/scripting/lua/binding/hgevector.lua
src/core/scripting/lua/example/hge_script.lua
--- a/src/core/scripting/lua/CMakeLists.txt	Mon Aug 22 10:56:38 2011 -0500
+++ b/src/core/scripting/lua/CMakeLists.txt	Mon Aug 22 13:44:15 2011 -0500
@@ -27,14 +27,14 @@
 # Generated headers
 # 	cpp/lua_hge.h
 # 	cpp/lua_hgeanim.h
-# 	cpp/lua_hgecolor.h
+	cpp/lua_hgecolor.h
 # 	cpp/lua_hgedistort.h
 # 	cpp/lua_hgefont.h
 # 	cpp/lua_hgegui.h
 # 	cpp/lua_hgeguictrls.h
 # 	cpp/lua_hgeparticle.h
 	cpp/lua_hgerect.h
-# 	cpp/lua_hgeresource.h
+	cpp/lua_hgeresource.h
 # 	cpp/lua_hgesprite.h
 	cpp/lua_hgestrings.h
 	cpp/lua_hgevector.h
@@ -42,14 +42,14 @@
 # Generated source files
 # 	cpp/lua_hge.cpp
 # 	cpp/lua_hgeanim.cpp
-# 	cpp/lua_hgecolor.cpp
+	cpp/lua_hgecolor.cpp
 # 	cpp/lua_hgedistort.cpp
 # 	cpp/lua_hgefont.cpp
 # 	cpp/lua_hgegui.cpp
 # 	cpp/lua_hgeguictrls.cpp
 # 	cpp/lua_hgeparticle.cpp
 	cpp/lua_hgerect.cpp
-# 	cpp/lua_hgeresource.cpp
+	cpp/lua_hgeresource.cpp
 # 	cpp/lua_hgesprite.cpp
 	cpp/lua_hgestrings.cpp
 	cpp/lua_hgevector.cpp
--- a/src/core/scripting/lua/binding/hgeBinding.lua	Mon Aug 22 10:56:38 2011 -0500
+++ b/src/core/scripting/lua/binding/hgeBinding.lua	Mon Aug 22 13:44:15 2011 -0500
@@ -33,9 +33,11 @@
 
 function generate()
 	local files = {
+		'hgeColor',
 		'hgeRect',
 		'hgeStrings',
 		'hgeVector',
+		'hgeResource', -- has members for sprite, animation, font, and others
 	}
 
 	hgeBuildDefinitionDB(files)
--- a/src/core/scripting/lua/binding/hgecolor.lua	Mon Aug 22 10:56:38 2011 -0500
+++ b/src/core/scripting/lua/binding/hgecolor.lua	Mon Aug 22 13:44:15 2011 -0500
@@ -6,67 +6,91 @@
 -- hgeColor*** helper classes
 --
 
+array.pushBack(gen_lua.number_types, 'DWORD')
+
+bindhgeColor = {
+	classes = {
+		{
+			name = 'hge.ColorRGB',
+			className = 'hgeColorRGB',
+			ctors = {
+				'()',
+				'(DWORD col)',
+				'(float _r, float _g, float _b, float _a)',
+			},
+			properties = {
+				'float r',
+				'float g',
+				'float b',
+				'float a',
+			},
+			memberFunctions = {[[
+				hgeColorRGB operator-(const hgeColorRGB &c) const;
+				hgeColorRGB operator+(const hgeColorRGB &c) const;
+				hgeColorRGB operator*(const hgeColorRGB &c) const;
+				hgeColorRGB & operator-=(const hgeColorRGB &c);
+				hgeColorRGB & operator+=(const hgeColorRGB &c);
+			]],
+				-- Not implemented in luna-gen
+-- 				bool operator ==(const hgeColorRGB &c) const;
+-- 				bool operator!=(const hgeColorRGB &c) const;
+			[[
+				hgeColorRGB operator/(const float scalar) const;
+				hgeColorRGB operator*(const float scalar) const;
+				hgeColorRGB & operator*=(const float scalar);
+			]],
+
+				{ rename = 'clamp', 'void Clamp();' },
+				{ rename = 'sethwcolor', 'void SetHWColor(DWORD col);' },
+				{ rename = 'gethwcolor', 'DWORD GetHWColor() const;' },
+			},
+		},
+		{
+			name = 'hge.ColorHSV',
+			className = 'hgeColorHSV',
+			ctors = {
+				'()',
+				'(DWORD col)',
+				'(float _h, float _s, float _v, float _a)',
+			},
+			properties = {
+				'float h',
+				'float s',
+				'float v',
+				'float a',
+			},
+			memberFunctions = {[[
+				hgeColorHSV operator-(const hgeColorHSV &c) const;
+				hgeColorHSV operator+(const hgeColorHSV &c) const;
+				hgeColorHSV operator*(const hgeColorHSV &c) const;
+				hgeColorHSV & operator-=(const hgeColorHSV &c);
+				hgeColorHSV & operator+=(const hgeColorHSV &c);
+			]],
+-- 				bool operator==(const hgeColorHSV &c) const;
+-- 				bool operator!=(const hgeColorHSV &c) const;
+			[[
+				hgeColorHSV operator/(const float scalar) const;
+				hgeColorHSV operator*(const float scalar) const;
+				hgeColorHSV & operator*=(const float scalar);
+			]],
+
+				{ rename = 'clamp', 'void Clamp();' },
+				{ rename = 'sethwcolor', 'void SetHWColor(DWORD col);' },
+				{ rename = 'gethwcolor', 'DWORD GetHWColor() const;' },
+			}
+		},
+	},
+	modules = {
+	}
+}
+
 --[[
 
 #define hgeColor hgeColorRGB
 
 inline void ColorClamp(float &x) { if(x<0.0f) x=0.0f; if(x>1.0f) x=1.0f; }
 
-class hgeColorRGB
-{
-public:
-	float		r,g,b,a;
-
-	hgeColorRGB(float _r, float _g, float _b, float _a) { r=_r; g=_g; b=_b; a=_a; }
-	hgeColorRGB(DWORD col) { SetHWColor(col); }
-	hgeColorRGB() { r=g=b=a=0; }
-
-	hgeColorRGB		operator-  (const hgeColorRGB &c) const { return hgeColorRGB(r-c.r, g-c.g, b-c.b, a-c.a); }
-	hgeColorRGB		operator+  (const hgeColorRGB &c) const { return hgeColorRGB(r+c.r, g+c.g, b+c.b, a+c.a); }
-	hgeColorRGB		operator*  (const hgeColorRGB &c) const { return hgeColorRGB(r*c.r, g*c.g, b*c.b, a*c.a); }
-	hgeColorRGB&	operator-= (const hgeColorRGB &c)		{ r-=c.r; g-=c.g; b-=c.b; a-=c.a; return *this;   }
-	hgeColorRGB&	operator+= (const hgeColorRGB &c)		{ r+=c.r; g+=c.g; b+=c.b; a+=c.a; return *this;   }
-	bool			operator== (const hgeColorRGB &c) const { return (r==c.r && g==c.g && b==c.b && a==c.a);  }
-	bool			operator!= (const hgeColorRGB &c) const { return (r!=c.r || g!=c.g || b!=c.b || a!=c.a);  }
-
-	hgeColorRGB		operator/  (const float scalar) const { return hgeColorRGB(r/scalar, g/scalar, b/scalar, a/scalar); }
-	hgeColorRGB		operator*  (const float scalar) const { return hgeColorRGB(r*scalar, g*scalar, b*scalar, a*scalar); }
-	hgeColorRGB&	operator*= (const float scalar)		  { r*=scalar; g*=scalar; b*=scalar; a*=scalar; return *this;   }
-
-	void			Clamp() { ColorClamp(r); ColorClamp(g); ColorClamp(b); ColorClamp(a); }
-	void			SetHWColor(DWORD col) {	a = (col>>24)/255.0f; r = ((col>>16) & 0xFF)/255.0f; g = ((col>>8) & 0xFF)/255.0f; b = (col & 0xFF)/255.0f;	}
-	DWORD			GetHWColor() const { return (DWORD(a*255.0f)<<24) + (DWORD(r*255.0f)<<16) + (DWORD(g*255.0f)<<8) + DWORD(b*255.0f);	}
-};
-
 inline hgeColorRGB operator* (const float sc, const hgeColorRGB &c) { return c*sc; }
-
-
-class hgeColorHSV
-{
-public:
-	float		h,s,v,a;
-
-	hgeColorHSV(float _h, float _s, float _v, float _a) { h=_h; s=_s; v=_v; a=_a; }
-	hgeColorHSV(DWORD col) { SetHWColor(col); }
-	hgeColorHSV() { h=s=v=a=0; }
-
-	hgeColorHSV		operator-  (const hgeColorHSV &c) const { return hgeColorHSV(h-c.h, s-c.s, v-c.v, a-c.a); }
-	hgeColorHSV		operator+  (const hgeColorHSV &c) const { return hgeColorHSV(h+c.h, s+c.s, v+c.v, a+c.a); }
-	hgeColorHSV		operator*  (const hgeColorHSV &c) const { return hgeColorHSV(h*c.h, s*c.s, v*c.v, a*c.a); }
-	hgeColorHSV&	operator-= (const hgeColorHSV &c)		{ h-=c.h; s-=c.s; v-=c.v; a-=c.a; return *this;   }
-	hgeColorHSV&	operator+= (const hgeColorHSV &c)		{ h+=c.h; s+=c.s; v+=c.v; a+=c.a; return *this;   }
-	bool			operator== (const hgeColorHSV &c) const { return (h==c.h && s==c.s && v==c.v && a==c.a);  }
-	bool			operator!= (const hgeColorHSV &c) const { return (h!=c.h || s!=c.s || v!=c.v || a!=c.a);  }
-
-	hgeColorHSV		operator/  (const float scalar) const { return hgeColorHSV(h/scalar, s/scalar, v/scalar, a/scalar); }
-	hgeColorHSV		operator*  (const float scalar) const { return hgeColorHSV(h*scalar, s*scalar, v*scalar, a*scalar); }
-	hgeColorHSV&	operator*= (const float scalar)		  { h*=scalar; s*=scalar; v*=scalar; a*=scalar; return *this;   }
-
-	void			Clamp() { ColorClamp(h); ColorClamp(s); ColorClamp(v); ColorClamp(a); }
-	void			SetHWColor(DWORD col);
-	DWORD			GetHWColor() const;
-};
-
 inline hgeColorHSV operator* (const float sc, const hgeColorHSV &c) { return c*sc; }
 
 --]]
--- a/src/core/scripting/lua/binding/hgerect.lua	Mon Aug 22 10:56:38 2011 -0500
+++ b/src/core/scripting/lua/binding/hgerect.lua	Mon Aug 22 13:44:15 2011 -0500
@@ -21,15 +21,15 @@
 				'float x2',
 				'float y2',
 			},
-			memberFunctions = {[[
-				void    Clear();
-				bool    IsClean() const;
-				void	Set(float _x1, float _y1, float _x2, float _y2);
-				void	SetRadius(float x, float y, float r);
-				void	Encapsulate(float x, float y);
-				bool	TestPoint(float x, float y) const;
-				bool	Intersect(const hgeRect *rect) const;
-			]]},
+			memberFunctions = {
+				{ rename = 'clear', 'void Clear();' },
+				{ rename ='isclean', 'bool IsClean() const;' },
+				{ rename ='set', 'void Set(float _x1, float _y1, float _x2, float _y2);' },
+				{ rename ='setradius', 'void SetRadius(float x, float y, float r);' },
+				{ rename ='encapsulate', 'void Encapsulate(float x, float y);' },
+				{ rename ='testpoint', 'bool TestPoint(float x, float y) const;' },
+				{ rename ='intersect', 'bool Intersect(const hgeRect *rect) const;' },
+			},
 		},
 	},
 	modules = {
--- a/src/core/scripting/lua/binding/hgeresource.lua	Mon Aug 22 10:56:38 2011 -0500
+++ b/src/core/scripting/lua/binding/hgeresource.lua	Mon Aug 22 13:44:15 2011 -0500
@@ -6,6 +6,62 @@
 -- hgeResourceManager helper class header
 --
 
+array.pushBack(gen_lua.number_types, 'size_t')
+
+bindhgeResource = {
+	classes = {
+-- Not needed?
+-- 		{
+-- 			name = 'hge.ResDesc',
+-- 			className = 'ResDesc',
+-- 			ctors = {
+-- 				'()',
+-- 			},
+-- 			properties = {
+-- -- 				'char * name',
+-- 				'int resgroup',
+-- 				'size_t handle',
+-- 				'ResDesc * next',
+-- 			},
+-- 			memberFunctions = {
+-- 				{rename = 'get', 'DWORD Get(hgeResourceManager *rm);'},
+-- 				{rename = 'free', 'void  Free();'},
+-- 			},
+-- 		},
+		{
+			name = 'hge.ResourceManager',
+			className = 'hgeResourceManager',
+			ctors = {
+				'(const char *scriptname)',
+			},
+-- 			properties = {
+-- 				'ResDesc* res[RESTYPES]',
+-- 			},
+			memberFunctions = {
+				{ rename = 'changescript', 'void ChangeScript(const char *scriptname);'},
+				{ rename = 'precache', 'bool Precache(int groupid)' },
+				{ rename = 'purge', 'void Purge(int groupid)' },
+
+-- 				{ rename = 'getresource', 'void* GetResource(const char *name, int resgroup)' },
+-- 				{ rename = 'gettexture', 'HTEXTURE GetTexture(const char *name, int resgroup)' },
+-- 				{ rename = 'geteffect', 'HEFFECT GetEffect(const char *name, int resgroup)' },
+-- 				{ rename = 'getmusic', 'HMUSIC GetMusic(const char *name, int resgroup)' },
+-- 				{ rename = 'getstream', 'HSTREAM GetStream(const char *name, int resgroup)' },
+-- 				{ rename = 'gettarget', 'HTARGET GetTarget(const char *name)' },
+
+-- 				{ rename = 'getsprite', 'hgeSprite* GetSprite(const char *name)' },
+-- 				{ rename = 'getanimation', 'hgeAnimation* GetAnimation(const char *name)' },
+-- 				{ rename = 'getfont', 'hgeFont* GetFont(const char *name)' },
+-- 				{ rename = 'getparticlesystem', 'hgeParticleSystem* GetParticleSystem(const char *name)' },
+-- 				{ rename = 'getdistortionmesh', 'hgeDistortionMesh* GetDistortionMesh(const char *name)' },
+				{ rename = 'getstringtable', 'hgeStringTable* GetStringTable(const char *name, int resgroup)' },
+			},
+		},
+	},
+	modules = {
+	},
+}
+
 --[[
 
 #include "hge.h"
--- a/src/core/scripting/lua/binding/hgestrings.lua	Mon Aug 22 10:56:38 2011 -0500
+++ b/src/core/scripting/lua/binding/hgestrings.lua	Mon Aug 22 13:44:15 2011 -0500
@@ -23,9 +23,9 @@
 			ctors = {
 				'(const char *filename)',
 			},
-			memberFunctions = {[[
-				const char* GetString(const char* name);
-			]]},
+			memberFunctions = {
+				{ rename = 'getstring', 'const char* GetString(const char* name);' },
+			},
 		}
 	},
 	modules = {
--- a/src/core/scripting/lua/binding/hgevector.lua	Mon Aug 22 10:56:38 2011 -0500
+++ b/src/core/scripting/lua/binding/hgevector.lua	Mon Aug 22 13:44:15 2011 -0500
@@ -9,8 +9,8 @@
 bindhgeVector = {
 	classes = {
 		{
-			name = "hge.Vector",
-			className = "hgeVector",
+			name = 'hge.Vector',
+			className = 'hgeVector',
 			ctors = {
 				'()',
 				'(float x, float y)',
@@ -29,15 +29,15 @@
 				hgeVector	operator/  (const float scalar)	const;
 				hgeVector	operator*  (const float scalar) const;
 				hgeVector&	operator*= (const float scalar);
+			]],
+				{ rename = 'dot', 'float Dot(const hgeVector *v) const;' },
+				{ rename = 'length', 'float Length() const;' },
+				{ rename = 'angle', 'float Angle(const hgeVector *v) const;' },
 
-				float		Dot(const hgeVector *v) const;
-				float		Length() const;
-				float		Angle(const hgeVector *v) const;
-
-				void		Clamp(const float max);
-				hgeVector*	Normalize();
-				hgeVector*	Rotate(float a);
-			]]},
+				{ rename = 'clamp', 'void Clamp(const float max);' },
+				{ rename = 'normalize', 'hgeVector* Normalize();' },
+				{ rename = 'rotate', 'hgeVector* Rotate(float a);' },
+			},
 		}
 	},
 	modules = {
--- a/src/core/scripting/lua/example/hge_script.lua	Mon Aug 22 10:56:38 2011 -0500
+++ b/src/core/scripting/lua/example/hge_script.lua	Mon Aug 22 13:44:15 2011 -0500
@@ -1,25 +1,44 @@
 print("Hello HGE")
+
+function test_bound(expected, real_value)
+	print("should be "..tostring(expected).."::"..tostring(real_value))
+end
+
 function print_vector(v)
 	print(v.x,v.y)
 end
+
+function print_separator()
+	print("-------------------------------------")
+end
+
 v = hge.Vector()
 v1 = hge.Vector(2.2, 3.3)
 print_vector(v)
 print_vector(v1)
-v3 = v:Normalize()
+v3 = v:normalize()
 print_vector(v3)
-v4 = v1:Rotate(0.5)
+v4 = v1:rotate(0.5)
 print_vector(v4)
 v5 = v4 * 5
 print_vector(v5)
-print(v5:Dot(v4))
-print(v5:Length())
+print(v5:dot(v4))
+print(v5:length())
 
-s = hge.StringTable("hge_script.lua")
--- Seems to not work
---print(s.GetString(""))
+print_separator()
+local f = io.open('test_strings', 'w');
+f:write('[HGESTRINGTABLE]\n')
+f:write('string1="this is a test string"\n')
+f:write('string2="this is another test"\n')
+f:close()
+
+s = hge.StringTable('test_strings')
+print('lua: hgeStringTable string1 = '..s:getstring('string1'))
+print('lua: hgeStringTable string2 = '..s:getstring('string2'))
 -- nothing else really useful to be done with hgestrings yet
 
+print_separator()
+
 function print_rect(r)
 	print(r.x1,r.x2,r.y1,r.y2)
 end
@@ -29,12 +48,14 @@
 
 r1 = hge.Rect(1,2,3,4)
 print_rect(r1)
-r1:Clear()
+r1:clear()
 print_rect(r1)
-print(r1:TestPoint(1.5,3.5))
-print(r1:TestPoint(5,5))
+test_bound(true, r1:testpoint(1.5,3.5))
+test_bound(false, r1:testpoint(5,5))
 r2 = hge.Rect(8,9,10,11)
-print(r1:Intersect(r2))
+test_bound(false, r1:intersect(r2))
 r3 = hge.Rect(1,2,5,6)
-print(r3:Intersect(r1))
-print(r2:Intersect(r3))
+test_bound(true, r3:intersect(r1))
+test_bound(false, r2:intersect(r3))
+
+print_separator()