-
-
Save menduz/5ec397c3495dcfe7911413eb049c32ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <eternity.hpp> | |
ScriptSystem::ScriptSystem() { | |
m_platform = 0; | |
m_isolate = 0; | |
m_scriptableCount = 0; | |
m_scriptablePoolSize = 0; | |
m_scriptables = 0; | |
} | |
ScriptSystem::~ScriptSystem() { | |
if (m_isolate) { | |
m_isolate = 0; | |
} | |
if (m_platform) { | |
delete m_platform; | |
m_platform = 0; | |
} | |
} | |
void ScriptSystem::Initialize() { | |
v8::V8::InitializeICU(); | |
v8::V8::Initialize(); | |
v8::Platform* platform = v8::platform::CreateDefaultPlatform(); | |
v8::V8::InitializePlatform(platform); | |
v8::V8::Initialize(); | |
v8::Isolate::CreateParams create_params; | |
create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator(); | |
m_isolate = v8::Isolate::New(create_params); | |
v8::Isolate::Scope isolate_scope(m_isolate); | |
m_isolate->Enter(); | |
} | |
void ScriptSystem::Shutdown() { | |
m_isolate->Dispose(); | |
v8::V8::Dispose(); | |
v8::V8::ShutdownPlatform(); | |
} | |
void ScriptSystem::Update(float delta) { | |
for(int i = 0; i < m_scriptablePoolSize; i++) { | |
if(m_scriptables[i]) { | |
// Execute script | |
Script* script = m_scriptables[i]->GetScript(); | |
script->Execute(); | |
} | |
} | |
} | |
void ScriptSystem::AddScriptable(Scriptable *scriptable) { | |
bool resize = false; | |
// If this is our first resource (or we're empty), set the pool size | |
if(m_scriptableCount == 0) { | |
m_scriptablePoolSize = SCRIPT_POOL_INCREMENT; | |
resize = true; | |
} | |
// Increment the resource count | |
unsigned int newScriptableCount = m_scriptableCount + 1; | |
// If this is bigger than the pool size, increase the pool size | |
if(newScriptableCount > m_scriptablePoolSize) { | |
m_scriptablePoolSize += SCENE_POOL_INCREMENT; | |
resize = true; | |
} | |
if(resize) { | |
Scriptable** pool = (Scriptable**)malloc(sizeof(Scriptable*) * m_scriptablePoolSize); | |
memset(pool, 0, sizeof(Scriptable*) * m_scriptablePoolSize); | |
memcpy(pool, m_scriptables, sizeof(Scriptable*) * m_scriptableCount); | |
free(m_scriptables); | |
m_scriptables = pool; | |
} | |
m_scriptables[m_scriptableCount] = scriptable; | |
m_scriptableCount = newScriptableCount; | |
} | |
void ScriptSystem::RemoveScriptable(Scriptable *scriptable) { | |
for(unsigned int i = 0; i < m_scriptableCount; i++) { | |
if(m_scriptables[i] == scriptable) { | |
m_scriptableCount--; | |
memcpy(m_scriptables + i, m_scriptables + i + 1, sizeof(Scriptable*) * (m_scriptableCount - i)); | |
m_scriptables[m_scriptableCount] = 0; | |
return; | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Our scripting system which executes JavaScript scripts | |
*/ | |
class ScriptSystem : public System { | |
public: | |
ScriptSystem(); | |
~ScriptSystem(); | |
/** | |
* Initialize the scripting library (V8) | |
*/ | |
void Initialize(); | |
/** | |
* Shutdown the scripting library | |
*/ | |
void Shutdown(); | |
/** | |
* Execute attach scripts | |
*/ | |
void Update(float delta); | |
/** | |
* Add a scriptable component to the system | |
*/ | |
void AddScriptable(Scriptable* scriptable); | |
/** | |
* Remove a scriptable component from the system | |
*/ | |
void RemoveScriptable(Scriptable* scriptable); | |
protected: | |
v8::Platform* m_platform; | |
v8::Isolate* m_isolate; | |
Scriptable** m_scriptables; | |
unsigned int m_scriptableCount; | |
unsigned int m_scriptablePoolSize; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment