Sfoglia il codice sorgente

as suggested by Niklas

mikymod 12 anni fa
parent
commit
ea693b1782
11 ha cambiato i file con 1 aggiunte e 645 eliminazioni
  1. 1 5
      game/Game.cpp
  2. 0 90
      game/lua/camera.lua
  3. 0 11
      game/lua/game.lua
  4. 0 142
      game/lua/init.lua
  5. 0 111
      game/lua/mat4.lua
  6. 0 66
      game/lua/math_utils.lua
  7. 0 25
      game/lua/mouse.lua
  8. 0 43
      game/lua/quat.lua
  9. 0 16
      game/lua/script.lua
  10. 0 65
      game/lua/vec2.lua
  11. 0 71
      game/lua/vec3.lua

+ 1 - 5
game/Game.cpp

@@ -7,17 +7,13 @@ namespace crown
 {
 
 lua_State* state;
-int z;
 
 void init()
 {
 	state = luaL_newstate();
 	luaL_openlibs(state);
 
-	if (luaL_loadfile(state, "lua/lua/game.lua.script") || lua_pcall(state, 0, 0, 0))
-	{
-		os::printf("error: %s", lua_tostring(state, -1));
-	}
+	luaL_loadfile(state, "lua/lua/game.lua.script");
 
 	lua_getglobal(state, "init");
 

+ 0 - 90
game/lua/camera.lua

@@ -1,90 +0,0 @@
-local ffi = require("ffi")
-
-ffi.cdef
-[[
-	typedef struct 
-	{
-		Vec3			m_position;
-		Vec3			m_look_at;
-		Vec3			m_up;
-
-		float			m_angle_x;
-		float			m_angle_y;
-
-		Mat4			m_view;
-		Mat4			m_projection;
-
-		float			m_FOV;
-		float			m_aspect;
-
-		float			m_near;
-		float			m_far;
-	} Camera;
-
-	Camera*				camera(const Vec3& position, float fov, float aspect);
-
-	const Vec3&			camera_position(Camera* self);
-
-	void				camera_set_position(Camera* self, const Vec3& position);
-
-	const Vec3&			camera_look_at(Camera* self);
-
-	void				camera_set_look_at(Camera* self, const Vec3& lookat);
-
-	void				camera_set_rotation(Camera* self, const float x, const float y);
-
-	const Vec3&			camera_up(Camera* self);
-
-	float				camera_fov(Camera* self);
-
-	void				camera_set_fov(Camera* self, float fov);
-
-	float				camera_aspect(Camera* self);
-
-	void				camera_set_aspect(Camera* self, float aspect);
-
-	float				camera_near_clip_distance(Camera* self);
-
-	void				camera_set_near_clip_distance(Camera* self, float near);
-
-	float				camera_far_clip_distance(Camera* self);
-
-	void				camera_set_far_clip_distance(Camera* self, float far);
-
-	const Mat4&			camera_projection_matrix(Camera* self);
-
-	const Mat4&			camera_view_matrix(Camera* self);
-
-	void				camera_move_forward(Camera* self, float meters);
-
-	void				camera_move_backward(Camera* self, float meters);
-
-	void				camera_strafe_left(Camera* self, float meters);
-
-	void				camera_strafe_right(Camera* self, float meters);
-
-]]
-
-Camera = {}
-
-Camera.camera 					= lib.camera
-Camera.position					= lib.camera_position
-Camera.set_position				= lib.camera_set_position
-Camera.look_at					= lib.camera_look_at
-Camera.set_look_at				= lib.camera_set_look_at
-Camera.set_rotation				= lib.camera_set_rotation
-Camera.up						= lib.camera_up
-Camera.fov						= lib.camera_fov
-Camera.set_fov					= lib.camera_set_fov
-Camera.aspect					= lib.camera_aspect
-Camera.set_aspect				= lib.camera_set_aspect
-Camera.near_clip_distance		= lib.camera_near_clip_distance
-Camera.set_near_clip_distance	= lib.camera_set_near_clip_distance
-Camera.far_clip_distance		= lib.camera_far_clip_distance
-Camera.set_far_clip_distance	= lib.camera_set_far_clip_distance
-Camera.projection_matrix		= lib.camera_projection_matrix
-Camera.view_matrix				= lib.camera_view_matrix
-Camera.move_forward				= lib.camera_move_forward
-Camera.move_backward			= lib.camera_move_backward
-Camera.strafe_left				= lib.camera_strafe_left
-Camera.strafe_right				= lib.camera_strafe_right

+ 0 - 11
game/lua/game.lua

@@ -1,11 +0,0 @@
-function init()
-	print("Lua Init called.")
-end
-
-function shutdown()
-	print("Lua Shutdown called.")
-end
-
-function frame(dt)
-	print("Lua Frame called.")
-end

+ 0 - 142
game/lua/init.lua

@@ -1,142 +0,0 @@
-local ffi = require("ffi")
-
-local lib_path = os.getenv("LD_LIBRARY_PATH")
-
-lib = ffi.load(lib_path .. "/libcrown.so", true)
-
-require("vec2")
-require("vec3")
-require("mat4")
-require("quat")
-require("math_utils")
-require("camera")
-require("script")
-
---------------------------------------------------------------
---------------------------------------------------------------
---------------------------------------------------------------
-print("-- Testing Vec2 --\n")
-local v2 = Vec2.vec2(1.0, 1.0)
-
-print(v2.x)
-print(v2.y)
-
-
---------------------------------------------------------------
---------------------------------------------------------------
---------------------------------------------------------------
-print("-- Testing Vec3 --\n")
-local pos = Vec3.vec3(1.0, 1.0, 1.0)
-
-pos = Vec3.add(pos, Vec3.vec3(1.0, 2.0, 3.0)
-)
-
-print(pos.x)
-print(pos.y)
-print(pos.z)
-
-print("-- Testing Vec3.negate --\n")
-
-Vec3.negate(pos)
-
-print(pos.x)
-print(pos.y)
-print(pos.z)
-
-
---------------------------------------------------------------
---------------------------------------------------------------
---------------------------------------------------------------
-
-print("-- Testing Mat4 --\n")
-local m = Mat4.mat4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)
-local t = Mat4.mat4(9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0)
-local trans = Vec3.vec3(1.0, 1.0, 0.0)
-local scale = Vec3.vec3(10.0, 10.0, 10.0)
-
-Mat4.print(m)
-print("\n")
-
-print("-- Mat4.add --\n")
-
-m = Mat4.add(m, t)
-Mat4.print(m)
-print("\n")
-
-print("-- Mat4.subtract --\n")
-
-m = Mat4.subtract(m, t)
-Mat4.print(m)
-print("\n")
-
-print(".. Mat4.set_translation-- \n")
-Mat4.set_translation(m, trans)
-Mat4.print(m)
-print("\n")
-
-print(".. Mat4.get_translation-- \n")
-local tr = Mat4.get_translation(m)
-print(tr.x)
-print(tr.y)
-print(tr.z)
-
-print(".. Mat4.set_scale-- \n")
-Mat4.set_scale(m, scale)
-Mat4.print(m)
-print("\n")
-
-print(".. Mat4.get_scale-- \n")
-local sc = Mat4.get_scale(m)
-print(sc.x)
-print(sc.y)
-print(sc.z)
-
---------------------------------------------------------------
---------------------------------------------------------------
---------------------------------------------------------------
-print("-- Testing MathUtils --\n")
-print("sin of 0 is " .. Math.sin(0.0))
-
--- --------------------------------------------------------------
--- --------------------------------------------------------------
--- --------------------------------------------------------------
--- print("-- Testing Camera --\n")
-
--- local cam = Camera.camera(pos, 90.0, 1.6)
-
--- print("@move forward by 1 meter")
--- print("x:" .. Camera.position(cam).x)
--- print("y:" .. Camera.position(cam).y)
--- print("z:" .. Camera.position(cam).z)
-
--- for i=1,10 do	
--- 	Camera.move_forward(cam, 1.0);
--- 	print("@move forward by 1 meter\n")
--- 	print("x:" .. Camera.position(cam).x)
--- 	print("y:" .. Camera.position(cam).y)
--- 	print("z:" .. Camera.position(cam).z)
--- end
-
--- local vm = Camera.view_matrix(cam)
--- local pm = Camera.projection_matrix(cam)
-
--- print("@printing view matrix\n")
--- print(Mat4.print(vm))
--- print("@printing projection matrix\n")
--- print(Mat4.print(pm))
-
---------------------------------------------------------------
---------------------------------------------------------------
---------------------------------------------------------------
-print("-- Testing Script --\n")
-
-print(Script.vec3_used())
-print(Script.mat4_used())
-print(Script.quat_used())
-
-
-
-
-
-
-

+ 0 - 111
game/lua/mat4.lua

@@ -1,111 +0,0 @@
-local ffi = require("ffi")
-
-ffi.cdef
-[[
-	typedef struct
-	{
-		float r1c1;
-		float r2c1;
-		float r3c1;
-		float r4c1;
-		float r1c2;
-		float r2c2;
-		float r3c2;
-		float r4c2;
-		float r1c3;
-		float r2c3;
-		float r3c3;
-		float r4c3;
-		float r1c4;
-		float r2c4;
-		float r3c4;
-		float r4c4;
-	} Mat4;
-
-	Mat4&				mat4(float r1c1, float r2c1, float r3c1, float r1c2, float r2c2, float r3c2, float r1c3, float r2c3, float r3c3);
-						
-	Mat4&				mat4_add(Mat4& self, Mat4& m);
-
-	Mat4&				mat4_subtract(Mat4& self, Mat4& m);
-
-	Mat4&				mat4_multiply(Mat4& self, Mat4& m);
-
-	Mat4&				mat4_multiply_by_scalar(Mat4& self, float k);
-
-	Mat4&				mat4_divide_by_scalar(Mat4& self, float k);
-
-	void				mat4_build_rotation_x(Mat4& self, float radians);
-
-	void				mat4_build_rotation_y(Mat4& self, float radians);	
-
-	void				mat4_build_rotation_z(Mat4& self, float radians);
-
-	void				mat4_build_rotation(Mat4& self, const Vec3& n, float radians);
-
-	void				mat4_build_projection_perspective_rh(Mat4& self, float fovy, float aspect, float near, float far);
-
-	void				mat4_build_projection_perspective_lh(Mat4& self, float fovy, float aspect, float near, float far);
-
-	void				mat4_build_projection_ortho_rh(Mat4& self, float width, float height, float near, float far);
-
-	void				mat4_build_projection_ortho_lh(Mat4& self, float width, float height, float near, float far);
-
-	void				mat4_build_projection_ortho_2d_rh(Mat4& self, float width, float height, float near, float far);
-
-	void				mat4_build_look_at_rh(Mat4& self, const Vec3& pos, const Vec3& target, const Vec3& up);
-
-	void				mat4_build_look_at_lh(Mat4& self, const Vec3& pos, const Vec3& target, const Vec3& up);
-
-	void				mat4_build_viewpoint_billboard(Mat4& self, const Vec3& pos, const Vec3& target, const Vec3& up);
-
-	void				mat4_build_axis_billboard(Mat4& self, const Vec3& pos, const Vec3& target, const Vec3& axis);
-
-	Mat4&				mat4_transpose(Mat4& self);
-
-	float				mat4_determinant(Mat4& self);
-
-	Mat4&				mat4_invert(Mat4& self);
-
-	void				mat4_load_identity(Mat4& self);
-
-	Vec3&				mat4_get_translation(Mat4& self);
-
-	void				mat4_set_translation(Mat4& self, const Vec3& trans);
-
-	Vec3&				mat4_get_scale(Mat4& self);
-
-	void				mat4_set_scale(Mat4& self, const Vec3& scale);
-
-	void 				mat4_print(Mat4& self);
-]]
-
-Mat4 = {}
-
-Mat4.mat4 								= lib.mat4
-Mat4.add 								= lib.mat4_add
-Mat4.subtract 							= lib.mat4_subtract
-Mat4.multiply 							= lib.mat4_multiply
-Mat4.multiply_by_scalar 				= lib.mat4_multiply_by_scalar
-Mat4.divide_by_scalar 					= lib.mat4_divide_by_scalar
-Mat4.build_rotation_x 					= lib.mat4_build_rotation_x
-Mat4.build_rotation_y 					= lib.mat4_build_rotation_y
-Mat4.build_rotation_z 					= lib.mat4_build_rotation_z
-Mat4.build_rotation 					= lib.mat4_build_rotation
-Mat4.build_projection_perspective_rh 	= lib.mat4_build_projection_perspective_rh
-Mat4.build_projection_perspective_lh 	= lib.mat4_build_projection_perspective_lh
-Mat4.build_projection_ortho_rh 			= lib.mat4_build_projection_ortho_rh
-Mat4.build_projection_ortho_lh 			= lib.mat4_build_projection_ortho_lh
-Mat4.build_projection_ortho_2d_rh 		= lib.mat4_build_projection_ortho_2d_rh
-Mat4.build_look_at_rh 					= lib.mat4_build_look_at_rh
-Mat4.build_look_at_lh 					= lib.mat4_build_look_at_lh
-Mat4.build_viewpoint_billboard 			= lib.mat4_build_viewpoint_billboard
-Mat4.build_axis_billboard 				= lib.mat4_build_axis_billboard
-Mat4.transpose 							= lib.mat4_transpose
-Mat4.determinant 						= lib.mat4_determinant
-Mat4.invert 							= lib.mat4_invert
-Mat4.load_identity 						= lib.mat4_load_identity
-Mat4.get_translation 					= lib.mat4_get_translation
-Mat4.set_translation 					= lib.mat4_set_translation
-Mat4.get_scale 							= lib.mat4_get_scale
-Mat4.set_scale 							= lib.mat4_set_scale
-Mat4.print 								= lib.mat4_print

+ 0 - 66
game/lua/math_utils.lua

@@ -1,66 +0,0 @@
-local ffi = require("ffi")
-
-ffi.cdef
-[[	
-	bool						math_equals(float a, float b);
-
-	bool						math_test_bitmask(int32_t value, int32_t bitmask);
-
-	int32_t						math_set_bitmask(int32_t value, int32_t bitmask);
-
-	int32_t						math_unset_bitmask(int32_t value, int32_t bitmask);					
-
-	float						math_deg_to_rad(float deg);
-
-	float						math_rad_to_deg(float rad);
-
-	uint32_t					math_next_pow_2(uint32_t x);
-
-	bool						math_is_pow_2(uint32_t x);	
-
-	float						math_ceil(float x);		
-
-	float						math_floor(float x);	
-
-	float						math_sqrt(float x);	
-
-	float						math_inv_sqrt(float x);
-
-	float						math_sin(float x);	
-
-	float						math_cos(float x);
-
-	float						math_asin(float x);	
-
-	float						math_acos(float x);	
-
-	float						math_tan(float x);		
-
-	float						math_atan2(float y, float x);	
-
-	float						math_abs(float x);			
-
-	float						math_fmod(float n, float d);
-]]
-
-Math = {}
-
-Math.equals 		= lib.math_equals
-Math.test_bitmask 	= lib.math_test_bitmask
-Math.set_bitmask 	= lib.math_set_bitmask
-Math.unset_bitmask 	= lib.math_unset_bitmask	
-Math.deg_to_rad 	= lib.math_deg_to_rad
-Math.rad_to_deg 	= lib.math_rad_to_deg
-Math.next_pow_2 	= lib.math_next_pow_2
-Math.is_pow_2 		= lib.math_is_pow_2
-Math.ceil 			= lib.math_ceil
-Math.floor 			= lib.math_floor
-Math.sqrt 			= lib.math_sqrt
-Math.sin 			= lib.math_sin
-Math.cos 			= lib.math_cos
-Math.asin 			= lib.math_asin
-Math.acos 			= lib.math_acos
-Math.tan 			= lib.math_tan	
-Math.atan2 			= lib.math_atan2
-Math.abs 			= lib.math_abs
-Math.fmod 			= lib.math_fmod

+ 0 - 25
game/lua/mouse.lua

@@ -1,25 +0,0 @@
-local ffi = require("ffi")
-
-ffi.cdef
-[[
-	bool	mouse_button_pressed(uint32_t button);
-
-	bool	mouse_button_released(uint32_t button);
-
-	Vec2&	mouse_cursor_xy();
-
-	void	mouse_set_cursor_xy(const Vec2& position);
-
-	Vec2&	mouse_cursor_relative_xy();
-
-	void	mouse_set_cursor_relative_xy(const Vec2& position);
-]]
-
-Mouse = {}
-
-Mouse.button_pressed			= lib.mouse_button_pressed
-Mouse.button_released			= lib.mouse_button_released
-Mouse.cursor_xy					= lib.mouse_cursor_xy
-Mouse.set_cursor_xy				= lib.mouse_set_cursor_xy
-Mouse.cursor_relative_xy		= lib.mouse_cursor_relative_xy
-Mouse.set_cursor_relative_xy	= lib.mouse_set_cursor_relative_xy

+ 0 - 43
game/lua/quat.lua

@@ -1,43 +0,0 @@
-require("vec3")
-
-local ffi = require("ffi")
-
-ffi.cdef
-[[
-	typedef struct
-	{
-		Vec3  v;
-		float w;
-
-	} Quat;
-
-	Quat&		quat(float angle, const Vec3& v);
-
-	void		quat_negate(Quat& self);
-
-	void		quat_load_identity(Quat& self);
-
-	float		quat_length(Quat& self);	
-
-	void		quat_conjugate(Quat& self);
-
-	Quat&		quat_inverse(Quat& self);			
-
-	Quat&		quat_cross(Quat& self, const Quat& b);
-
-	Quat&		quat_multiply(Quat& self, const float& k);
-
-	Quat&		quat_power(Quat& self, float exp);
-]]
-
-Quat = {}
-
-Quat.quat 			= lib.quat
-Quat.negate  		= lib.quat_negate
-Quat.load_identity  = lib.quat_load_identity
-Quat.length  		= lib.quat_length	
-Quat.conjugate 		= lib.quat_conjugate
-Quat.inverse  		= lib.quat_inverse			
-Quat.cross  		= lib.quat_cross
-Quat.multiply 		= lib.quat_multiply
-Quat.power  		= lib.quat_power

+ 0 - 16
game/lua/script.lua

@@ -1,16 +0,0 @@
-local ffi = require("ffi")
-
-ffi.cdef
-[[
-	uint32_t script_system_vec3_used();
-
-	uint32_t script_system_mat4_used();
-
-	uint32_t script_system_quat_used();
-]]
-
-Script = {}
-
-Script.vec3_used = lib.script_system_vec3_used
-Script.mat4_used = lib.script_system_mat4_used
-Script.quat_used = lib.script_system_quat_used

+ 0 - 65
game/lua/vec2.lua

@@ -1,65 +0,0 @@
-local ffi = require("ffi")
-
-ffi.cdef
-[[
-	typedef struct 
-	{
-		float x;
-		float y;
-	}
-	Vec2;
-
-	Vec2&				vec2(float nx, float ny);					
-
-	Vec2&				vec2_add(Vec2& self, const Vec2& a);			
-
-	Vec2& 				vec2_subtract(Vec2& self, const Vec2& a);			
-
-	Vec2&				vec2_multiply(Vec2& self, float k);			
-
-	Vec2&				vec2_divide(Vec2& self, float k);
-
-	float				vec2_dot(Vec2& self, const Vec2& a);				
-
-	bool				vec2_equals(Vec2& self, const Vec2& other);	
-
-	bool				vec2_lower(Vec2& self, const Vec2& other);		
-
-	bool				vec2_greater(Vec2& self, const Vec2& other);
-
-	float				vec2_length(Vec2& self);
-
-	float				vec2_squared_length(Vec2& self);
-
-	void				vec2_set_length(Vec2& self, float len);					
-
-	Vec2&				vec2_normalize(Vec2& self);
-
-	Vec2&				vec2_negate(Vec2& self);
-
-	float				vec2_get_distance_to(Vec2& self, const Vec2& a);
-
-	float				vec2_get_angle_between(Vec2& self, const Vec2& a);
-
-	void				vec2_zero(Vec2& self);
-]]
-
-Vec2 = {}
-
-Vec2.vec2  				= lib.vec2
-Vec2.add				= lib.vec2_add			
-Vec2.subtract			= lib.vec2_subtract			
-Vec2.multiply			= lib.vec2_multiply
-Vec2.divide				= lib.vec2_divide
-Vec2.dot				= lib.vec2_dot				
-Vec2.equals				= lib.vec2_equals	
-Vec2.lower				= lib.vec2_lower		
-Vec2.greater			= lib.vec2_greater
-Vec2.length				= lib.vec2_length
-Vec2.squared_length		= lib.vec2_squared_length
-Vec2.set_length			= lib.vec2_set_length					
-Vec2.normalize			= lib.vec2_normalize
-Vec2.negate				= lib.vec2_negate
-Vec2.get_distance_to	= lib.vec2_get_distance_to
-Vec2.get_angle_between	= lib.vec2_get_angle_between
-Vec2.zero				= lib.vec2_zero

+ 0 - 71
game/lua/vec3.lua

@@ -1,71 +0,0 @@
-local ffi = require("ffi")
-
-ffi.cdef
-[[
-	typedef struct
-	{
-		float x;
-		float y;
-		float z;
-	}
-	Vec3;
-
-	Vec3&				vec3(float nx, float ny, float nz);
-
-	Vec3&				vec3_add(Vec3& self, const Vec3& v);
-
-	Vec3&				vec3_subtract(Vec3& self, const Vec3& v);
-
-	Vec3&				vec3_multiply(Vec3& self, const float s);
-
-	Vec3&				vec3_divide(Vec3& self, const float s);
-
-	float				vec3_dot(Vec3& self, const Vec3& v);
-
-	Vec3&				vec3_cross(Vec3& self, const Vec3& v);
-
-	bool				vec3_equal(Vec3& self, const Vec3& other);	
-	
-	bool				vec3_lower(Vec3& self, const Vec3& other);
-
-	bool				vec3_greater(Vec3& self, const Vec3& other);		
-
-	float				vec3_length(Vec3& self);	
-
-	float				vec3_squared_length(Vec3& self);
-
-	void				vec3_set_length(Vec3& self, float len);
-
-	Vec3&				vec3_normalize(Vec3& self);
-
-	Vec3&				vec3_negate(Vec3& self);					
-
-	float				vec3_get_distance_to(Vec3& self, const Vec3& a);	
-
-	float				vec3_get_angle_between(Vec3& self, const Vec3& a);	
-
-	void				vec3_zero(Vec3& self);						
-]]
-
--- Encapsulate method in Vec3 table
-
-Vec3 = {}
-
-Vec3.vec3 				= lib.vec3;
-Vec3.add 				= lib.vec3_add
-Vec3.subtract 			= lib.vec3_subtract
-Vec3.multiply 			= lib.vec3_multiply
-Vec3.divide 			= lib.vec3_divide
-Vec3.dot 				= lib.vec3_dot
-Vec3.cross 				= lib.vec3_cross
-Vec3.equal 				= lib.vec3_equal
-Vec3.lower 				= lib.vec3_lower
-Vec3.greater 			= lib.vec3_greater
-Vec3.lenght 			= lib.vec3_length
-Vec3.squared_length 	= lib.vec3_squared_length
-Vec3.set_length 		= lib.vec3_set_length
-Vec3.normalize 			= lib.vec3_normalize
-Vec3.negate 			= lib.vec3_negate
-Vec3.get_distance_to 	= lib.vec3_get_distance_to
-Vec3.get_angle_between 	= lib.vec3_get_angle_between
-Vec3.zero 				= lib.vec3_zero