#include "ScriptBindings.h" #include "raylib.h" #include #include #include "log/log.h" // Expose log level constants to AngelScript static int AS_LOG_TRACE = 1; static int AS_LOG_DEBUG = 2; static int AS_LOG_INFO = 3; static int AS_LOG_WARNING = 4; static int AS_LOG_ERROR = 5; static int AS_LOG_FATAL = 6; void ScriptBindings::RegisterAll(asIScriptEngine* engine) { // Register Print function with generic calling convention to access context int r = engine->RegisterGlobalFunction("void Print(const string &in)", asFUNCTION(Print), asCALL_GENERIC); assert(r >= 0); // Register log level constants so scripts can refer to LOG_INFO, LOG_ERROR, etc. r = engine->RegisterGlobalProperty("int LOG_TRACE", (void*)&AS_LOG_TRACE); assert(r >= 0); r = engine->RegisterGlobalProperty("int LOG_DEBUG", (void*)&AS_LOG_DEBUG); assert(r >= 0); r = engine->RegisterGlobalProperty("int LOG_INFO", (void*)&AS_LOG_INFO); assert(r >= 0); r = engine->RegisterGlobalProperty("int LOG_WARNING", (void*)&AS_LOG_WARNING); assert(r >= 0); r = engine->RegisterGlobalProperty("int LOG_ERROR", (void*)&AS_LOG_ERROR); assert(r >= 0); r = engine->RegisterGlobalProperty("int LOG_FATAL", (void*)&AS_LOG_FATAL); assert(r >= 0); // Register Log(level, message) to allow scripts to log at different levels r = engine->RegisterGlobalFunction("void Log(int, const string &in)", asFUNCTION(Log), asCALL_GENERIC); assert(r >= 0); // Register DrawText function r = engine->RegisterGlobalFunction("void DrawText(const string &in, int, int, int, uint)", asFUNCTION(AS_DrawText), asCALL_CDECL); assert(r >= 0); } void ScriptBindings::Print(asIScriptGeneric* gen) { // Get the message parameter std::string* msg = static_cast(gen->GetArgObject(0)); // Get the active context to retrieve script file and line number asIScriptContext* ctx = asGetActiveContext(); if (ctx) { const char* section = nullptr; int line = 0; // Get the current line number and script section line = ctx->GetLineNumber(0, nullptr, §ion); if (section && line > 0) { // Log with script file and line number log_log(LOG_INFO, section, line, "%s", msg->c_str()); } else { // Fallback if we can't get the info log_info("%s", msg->c_str()); } } else { // No context available, just log normally log_info("%s", msg->c_str()); } } void ScriptBindings::Log(asIScriptGeneric* gen) { // arg0: int level, arg1: string message int level = gen->GetArgDWord(0); std::string* msg = static_cast(gen->GetArgObject(1)); // Obtain script context to fetch file/line asIScriptContext* ctx = asGetActiveContext(); if (ctx) { const char* section = nullptr; int line = 0; line = ctx->GetLineNumber(0, nullptr, §ion); if (section && line > 0) { // Use the library's log_log to preserve filename/line info log_log(level, section, line, "%s", msg ? msg->c_str() : ""); return; } } // Fallback: log without script location switch (level) { case LOG_TRACE: log_trace("%s", msg ? msg->c_str() : ""); break; case LOG_DEBUG: log_debug("%s", msg ? msg->c_str() : ""); break; case LOG_WARNING: log_warn("%s", msg ? msg->c_str() : ""); break; case LOG_ERROR: log_error("%s", msg ? msg->c_str() : ""); break; case LOG_FATAL: log_fatal("%s", msg ? msg->c_str() : ""); break; case LOG_INFO: default: log_info("%s", msg ? msg->c_str() : ""); break; } } Color ColorFromUInt(unsigned int c) { Color col; col.r = (c >> 24) & 0xFF; col.g = (c >> 16) & 0xFF; col.b = (c >> 8) & 0xFF; col.a = c & 0xFF; return col; } void ScriptBindings::AS_DrawText(const std::string &text, int x, int y, int fontSize, unsigned int color) { DrawText(text.c_str(), x, y, fontSize, ColorFromUInt(color)); }