Ver Fonte

ScriptSystem updated

mikymod há 12 anos atrás
pai
commit
ed07d2fddb

+ 0 - 30
samples/lua/lua.cpp

@@ -1,42 +1,12 @@
 #include <iostream>
 #include "Crown.h"
 #include <unistd.h>
-#include "ScriptSystem.h"
 
 using namespace crown;
 
 int main(int argc, char** argv)
 {
-  lua_State* lua_state;
 
-  Filesystem fs("/home/mikymod/test/res_compiled");
-
-  FileResourceArchive archive(fs);
-
-  MallocAllocator allocator;
-
-  ResourceManager res_manager(archive, allocator);
-
-  ResourceId script = res_manager.load("lua/init.lua");
-
-  res_manager.flush();
-
-  ScriptSystem* sys = scripter();
-
-  while (1)
-  {
-    if (res_manager.is_loaded(script))
-    {
-      assert(res_manager.data(script) != NULL);
-
-      ScriptResource* resource = (ScriptResource*)res_manager.data(script);
-
-      sys->load(resource);
-      sys->execute();
-
-      break;
-    }
-  }
 
   return 0;
 }

+ 0 - 4
samples/terrain/terrain.cpp

@@ -125,15 +125,11 @@ public:
 		red_up    = device()->load("textures/red_up.tga");
 		red_down  = device()->load("textures/red_down.tga");
 		grass     = device()->load("textures/grass.tga");
-		script	  = device()->load("lua/init.lua");
 
 		device()->resource_manager()->flush();
 
 		TextureResource* grass_texture = (TextureResource*)device()->data(grass);
 		grass_id = device()->renderer()->create_texture(grass_texture->width(), grass_texture->height(), grass_texture->format(), grass_texture->data());
-
-		ScriptResource* script_resource = (ScriptResource*)device()->data(script);
-		device()->script_system()->load(script_resource);
 	}
 
 	void on_unload()

+ 0 - 2
src/CMakeLists.txt

@@ -8,7 +8,6 @@ set (SRC
 
 	TextureResource.cpp
 	TextResource.cpp
-	ScriptResource.cpp
 	FontResource.cpp
 	ArchiveResourceArchive.cpp
 	FileResourceArchive.cpp
@@ -32,7 +31,6 @@ set (HEADERS
 
 	TextureResource.h
 	TextResource.h
-	ScriptResource.h
 	FontResource.h
 	ArchiveResourceArchive.h
 	FileResourceArchive.h

+ 3 - 1
src/Crown.h

@@ -106,7 +106,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "TextResource.h"
 #include "TextureResource.h"
-#include "ScriptResource.h"
 #include "MaterialResource.h"
 #include "FontResource.h"
 
@@ -131,3 +130,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "PixelFormat.h"
 #include "VertexFormat.h"
 
+// Engine/Script
+#include "ScriptSystem.h"
+

+ 1 - 1
src/Device.cpp

@@ -475,7 +475,7 @@ void Device::create_debug_renderer()
 //-----------------------------------------------------------------------------
 void Device::create_script_system()
 {
-	m_script_system = scripter();
+	m_script_system = new ScriptSystem();
 }
 
 //-----------------------------------------------------------------------------

+ 1 - 13
src/ResourceManager.cpp

@@ -38,7 +38,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "FileStream.h"
 #include "TextResource.h"
 #include "TextureResource.h"
-#include "ScriptResource.h"
 
 namespace crown
 {
@@ -343,10 +342,6 @@ void* ResourceManager::load_by_type(ResourceId name) const
 	{
 		return TextResource::load(m_resource_allocator, m_resource_archive, name);
 	}
-	else if (name.type == SCRIPT_TYPE)
-	{
-		return ScriptResource::load(m_resource_allocator, m_resource_archive, name);
-	}
 
 	return NULL;
 }
@@ -362,10 +357,6 @@ void ResourceManager::unload_by_type(ResourceId name, void* resource) const
 	{
 		TextResource::unload(m_resource_allocator, (TextResource*)resource);
 	}
-	else if (name.type == SCRIPT_TYPE)
-	{
-		ScriptResource::unload(m_resource_allocator, (ScriptResource*)resource);
-	}
 
 	return;
 }
@@ -381,10 +372,7 @@ void ResourceManager::online(ResourceId name, void* resource)
 	{
 		TextResource::unload(m_resource_allocator, (TextResource*)resource);
 	}
-	else if (name.type == SCRIPT_TYPE)
-	{
-		ScriptResource::online((ScriptResource*)resource);
-	}
+
 
 	m_resources_mutex.lock();
 

+ 0 - 58
src/ScriptResource.cpp

@@ -1,58 +0,0 @@
-#include <cassert>
-
-#include "ScriptResource.h"
-#include "ResourceArchive.h"
-#include "Log.h"
-#include "FileStream.h"
-#include "Allocator.h"
-
-namespace crown
-{
-
-//-----------------------------------------------------------------------------
-void* ScriptResource::load(Allocator& allocator, ResourceArchive& archive, ResourceId id)
-{
-	FileStream* stream = archive.open(id);
-
-	if (stream != NULL)
-	{
-		ScriptResource* resource = (ScriptResource*)allocator.allocate(sizeof(ScriptResource));
-
-		stream->read(&resource->m_length, sizeof(uint32_t));
-		
-		size_t size = resource->m_length;
-
-		resource->m_data = (uint8_t*)allocator.allocate(sizeof(uint8_t) * size);
-
-		stream->read(resource->m_data, size);
-
-		archive.close(stream);
-
-		return resource;
-	}
-
-	return NULL;
-}
-
-//-----------------------------------------------------------------------------
-void ScriptResource::online(void* resource)
-{
-	(void) resource;
-}
-
-//-----------------------------------------------------------------------------
-void ScriptResource::unload(Allocator& allocator, void* resource)
-{
-	assert(resource != NULL);
-
-	allocator.deallocate(((ScriptResource*)resource)->m_data);
-	allocator.deallocate(resource);
-}
-
-//-----------------------------------------------------------------------------
-void ScriptResource::offline()
-{
-
-}
-
-} // namespace crown

+ 0 - 58
src/ScriptResource.h

@@ -1,58 +0,0 @@
-/*
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#pragma once
-
-#include "Types.h"
-#include "Resource.h"
-
-namespace crown
-{
-
-class ResourceArchive;
-class Allocator;
-
-class ScriptResource
-{
-public:
-
-	static void*		load(Allocator& allocator, ResourceArchive& archive, ResourceId id);
-	static void			online(void* script);
-	static void			unload(Allocator& allocator, void* resource);
-	static void			offline();
-
-public:
-
-	const uint32_t		length() const { return m_length; }
-	const uint8_t*		data() const { return m_data; }
-
-	void				set_length(uint32_t len) { m_length = len; }
-
-private:
-	uint32_t 			m_length;
-	uint8_t*			m_data;
-};
-
-} // namespace crown

+ 15 - 18
src/script/ScriptSystem.cpp

@@ -1,10 +1,11 @@
 #include "ScriptSystem.h"
+#include "Filesystem.h"
 #include <cassert>
 
 namespace crown
 {
 
-const char* BOOT_SCRIPT = "lua/init.lua";
+const char* BOOT_SCRIPT = "lua/game.lua";
 
 //-----------------------------------------------------------
 // ScriptSystem
@@ -13,7 +14,6 @@ const char* BOOT_SCRIPT = "lua/init.lua";
 //-----------------------------------------------------------
 ScriptSystem::ScriptSystem() :
 m_state(),
-m_scripts_count(0),
 m_vec2_count(0),
 m_vec3_count(0),
 m_mat4_count(0),
@@ -22,32 +22,21 @@ m_quat_count(0)
 }
 
 //-----------------------------------------------------------
-void ScriptSystem::load(ScriptResource* script)
+void ScriptSystem::load(const char* script)
 {
-	m_scripts[m_scripts_count] = script;
-
-	m_scripts_count++;
+	assert(m_state.load_file(script) == 0);
 }
 
 //-----------------------------------------------------------
 void ScriptSystem::execute()
 {
-	if (m_scripts_count != 0)
-	{
-		for (int i = 0; i < m_scripts_count; i++)
-		{
-			assert(m_state.load_buffer((char*)m_scripts[i]->data(), m_scripts[i]->length()) == 0);
-		}
-
-		assert(m_state.execute() == 0);	
-	}
+	assert(m_state.execute() == 0);	
 }
 
 //-----------------------------------------------------------
-void ScriptSystem::unload(ScriptResource* resource)
+void ScriptSystem::unload(const char* script)
 {
-	(void*) resource;
-	// FIXME
+	(void*) script;
 }
 
 //-----------------------------------------------------------
@@ -182,6 +171,14 @@ int32_t LuaState::load_string(const char* str)
 	return s;
 }
 
+//-----------------------------------------------------------
+int32_t LuaState::load_file(const char* file)
+{
+	int32_t s = luaL_loadfile(m_state, file);
+
+	return s;
+}
+
 //-----------------------------------------------------------
 int32_t LuaState::execute()
 {

+ 9 - 20
src/script/ScriptSystem.h

@@ -21,15 +21,17 @@ namespace crown
 class LuaState
 {
 public:
-								/// Constructor, private for singleton
+	/// Constructor, private for singleton
 								LuaState();
-								/// Destructor
+	/// Destructor
 								~LuaState();
-								/// Load lua chunk as buffer
+	/// Load lua chunk as buffer
 	int32_t 					load_buffer(const char* buf, size_t len);
-								/// Load lua chunk as string
+	/// Load lua chunk as string
 	int32_t						load_string(const char* str);
-								/// Executes lua chunk loaded in stack
+	/// Load lua chunk as file
+	int32_t 					load_file(const char* file);
+	/// Executes lua chunk loaded in stack
 	int32_t 					execute();
 
 private:
@@ -47,11 +49,11 @@ public:
 								ScriptSystem();
 
 								/// Loads script resource
-	void						load(ScriptResource* script);
+	void						load(const char* script);
 								/// Executes
 	void						execute();
 								/// Unloads script resource
-	void						unload(ScriptResource* script);
+	void						unload(const char* script);
 								/// Returns the first free Vec2
 	Vec2&						next_vec2(float nx, float ny);
 								/// Returns the first free Vec3
@@ -71,22 +73,12 @@ public:
 
 								/// First file loaded by ScriptSystem
 	static const char*			BOOT_SCRIPT;
-								/// Number of Scripts which can be loaded by ScriptSystem
-	static const uint32_t		MAX_SCRIPTS 		= 256;
 								/// Max number of temporary objects allowed
 	static const uint32_t		MAX_TEMP_OBJECTS 	= 1024;
 
 
 private:
-								// Disable coping
-								ScriptSystem(const ScriptSystem&);
-								ScriptSystem& operator=(const ScriptSystem&);
-
 	LuaState 					m_state;							
-								/// Scripts loaded in ScriptSystem
-	ScriptResource*				m_scripts[MAX_SCRIPTS];
-								/// number of scripts loaded
-	uint32_t					m_scripts_count;
 								/// Vec2 used by lua environment
 	Vec2 						m_vec2_list[MAX_TEMP_OBJECTS];
 								/// Counter which points to the next free Vec2
@@ -106,9 +98,6 @@ private:
 };
 
 
-
-ScriptSystem* scripter();
-
 // This block provides fews utilities for lua environment
 extern "C"
 {

+ 4 - 3
src/script/binds/Mat4Binds.cpp

@@ -1,3 +1,4 @@
+#include "Device.h"
 #include "ScriptSystem.h"
 #include "OS.h"
 
@@ -65,7 +66,7 @@ extern "C"
 
 Mat4& mat4(float r1c1, float r2c1, float r3c1, float r1c2, float r2c2, float r3c2, float r1c3, float r2c3, float r3c3)
 {
-	return scripter()->next_mat4(r1c1, r2c1, r3c1, r1c2, r2c2, r3c2, r1c3, r2c3, r3c3);
+	return device()->script_system()->next_mat4(r1c1, r2c1, r3c1, r1c2, r2c2, r3c2, r1c3, r2c3, r3c3);
 }
 					
 Mat4& mat4_add(Mat4& self, Mat4& m)
@@ -196,7 +197,7 @@ Vec3& mat4_get_translation(Mat4& self)
 {	
 	Vec3 tmp = self.get_translation();
 
-	return scripter()->next_vec3(tmp.x, tmp.y, tmp.z);
+	return device()->script_system()->next_vec3(tmp.x, tmp.y, tmp.z);
 }
 
 void mat4_set_translation(Mat4& self, const Vec3& trans)
@@ -207,7 +208,7 @@ void mat4_set_translation(Mat4& self, const Vec3& trans)
 Vec3& mat4_get_scale(Mat4& self)
 {
 	Vec3 tmp = self.get_scale();
-	return scripter()->next_vec3(tmp.x, tmp.y, tmp.z);	
+	return device()->script_system()->next_vec3(tmp.x, tmp.y, tmp.z);	
 }
 
 void mat4_set_scale(Mat4& self, const Vec3& scale)

+ 2 - 2
src/script/binds/MouseBinds.cpp

@@ -81,7 +81,7 @@ Vec2& mouse_cursor_xy()
 
 	Point2 tmp = device()->mouse()->cursor_xy();
 
-	return scripter()->next_vec2(tmp.x, tmp.y);
+	return device()->script_system()->next_vec2(tmp.x, tmp.y);
 }
 
 void mouse_set_cursor_xy(const Vec2& position)
@@ -97,7 +97,7 @@ Vec2& mouse_cursor_relative_xy()
 {
 	Vec2 tmp = device()->mouse()->cursor_relative_xy();
 
-	return scripter()->next_vec2(tmp.x, tmp.y);
+	return device()->script_system()->next_vec2(tmp.x, tmp.y);
 }
 
 void mouse_set_cursor_relative_xy(const Vec2& position)

+ 2 - 1
src/script/binds/QuatBinds.cpp

@@ -1,3 +1,4 @@
+#include "Device.h"
 #include "ScriptSystem.h"
 
 namespace crown
@@ -28,7 +29,7 @@ extern "C"
 
 Quat& quat(float angle, const Vec3& v)
 {
-	return scripter()->next_quat(angle, v);
+	return device()->script_system()->next_quat(angle, v);
 }
 
 void quat_negate(Quat& self)

+ 3 - 1
src/script/binds/Vec2Binds.cpp

@@ -1,5 +1,7 @@
+#include "Device.h"
 #include "ScriptSystem.h"
 
+
 namespace crown
 {
 
@@ -43,7 +45,7 @@ extern "C"
 
 Vec2& vec2(float nx, float ny)
 {
-	return scripter()->next_vec2(nx, ny);
+	return device()->script_system()->next_vec2(nx, ny);
 }
 
 Vec2& vec2_add(Vec2& self, const Vec2& a)

+ 2 - 1
src/script/binds/Vec3Binds.cpp

@@ -1,3 +1,4 @@
+#include "Device.h"
 #include "ScriptSystem.h"
 
 namespace crown
@@ -48,7 +49,7 @@ extern "C"
 //------------------------------------------------------------
 Vec3& vec3(float nx, float ny, float nz)
 {
-	return scripter()->next_vec3(nx, ny, nz);
+	return device()->script_system()->next_vec3(nx, ny, nz);
 }
 
 //------------------------------------------------------------