feat: re-organise files
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
#include "ScriptEngine.h"
|
||||
#include "ScriptBindings.h"
|
||||
#include "scriptstdstring.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <assert.h>
|
||||
|
||||
ScriptEngine::ScriptEngine() : engine(nullptr), updateFunc(nullptr), drawFunc(nullptr) {
|
||||
}
|
||||
|
||||
ScriptEngine::~ScriptEngine() {
|
||||
Shutdown();
|
||||
}
|
||||
|
||||
bool ScriptEngine::Initialize() {
|
||||
engine = asCreateScriptEngine();
|
||||
if (!engine) {
|
||||
std::cerr << "Failed to create AngelScript engine\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Register std::string
|
||||
RegisterStdString(engine);
|
||||
|
||||
// Register script bindings
|
||||
ScriptBindings::RegisterAll(engine);
|
||||
|
||||
// Set message callback
|
||||
int r = engine->SetMessageCallback(asFUNCTION(MessageCallback), nullptr, asCALL_CDECL);
|
||||
if (r < 0) {
|
||||
std::cerr << "Failed to set message callback\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ScriptEngine::Shutdown() {
|
||||
if (engine) {
|
||||
engine->ShutDownAndRelease();
|
||||
engine = nullptr;
|
||||
}
|
||||
updateFunc = nullptr;
|
||||
drawFunc = nullptr;
|
||||
}
|
||||
|
||||
bool ScriptEngine::CompileScript(const std::string& filename) {
|
||||
if (!engine) return false;
|
||||
|
||||
engine->GarbageCollect();
|
||||
asIScriptModule* mod = engine->GetModule("main", asGM_ALWAYS_CREATE);
|
||||
|
||||
std::string code = ReadFile(filename);
|
||||
if (code.empty()) {
|
||||
std::cerr << "Failed to read script file: " << filename << "\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
int r = mod->AddScriptSection(filename.c_str(), code.c_str());
|
||||
if (r < 0) return false;
|
||||
|
||||
r = mod->Build();
|
||||
if (r < 0) return false;
|
||||
|
||||
// Cache Update(float dt) and Draw() functions if they exist
|
||||
updateFunc = mod->GetFunctionByName("Update");
|
||||
drawFunc = mod->GetFunctionByName("Draw");
|
||||
|
||||
std::cout << "Script compiled and cached: " << filename << "\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
void ScriptEngine::CallScriptFunction(asIScriptFunction* func, float dt) {
|
||||
if (!func || !engine) return;
|
||||
|
||||
asIScriptContext* ctx = engine->CreateContext();
|
||||
ctx->Prepare(func);
|
||||
|
||||
if (func->GetParamCount() == 1) {
|
||||
ctx->SetArgFloat(0, dt);
|
||||
}
|
||||
|
||||
ctx->Execute();
|
||||
ctx->Release();
|
||||
}
|
||||
|
||||
void ScriptEngine::GarbageCollect() {
|
||||
if (engine) {
|
||||
engine->GarbageCollect();
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptEngine::MessageCallback(const asSMessageInfo* msg, void*) {
|
||||
std::cout << msg->section << " (" << msg->row << "): " << msg->message << std::endl;
|
||||
}
|
||||
|
||||
std::string ScriptEngine::ReadFile(const std::string& filename) {
|
||||
std::ifstream file(filename);
|
||||
if (!file) return "";
|
||||
std::stringstream ss;
|
||||
ss << file.rdbuf();
|
||||
return ss.str();
|
||||
}
|
||||
Reference in New Issue
Block a user