73 lines
2.0 KiB
C++
73 lines
2.0 KiB
C++
#include "raylib.h"
|
|
#include <angelscript.h>
|
|
#include <scriptstdstring.h>
|
|
#include <cassert>
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
// -------------------------
|
|
// Functions exposed to AngelScript
|
|
// -------------------------
|
|
void Print(const std::string &msg) {
|
|
std::cout << "[Script] " << msg << std::endl;
|
|
}
|
|
|
|
// -------------------------
|
|
// AngelScript message callback
|
|
// -------------------------
|
|
void AngelScriptMessageCallback(const asSMessageInfo* msg, void* param) {
|
|
std::cout << msg->section << " (" << msg->row << "): " << msg->message << std::endl;
|
|
}
|
|
|
|
// -------------------------
|
|
// Main
|
|
// -------------------------
|
|
int main() {
|
|
// Initialize Raylib
|
|
InitWindow(800, 600, "Raylib + AngelScript");
|
|
SetTargetFPS(60);
|
|
|
|
// Initialize AngelScript
|
|
asIScriptEngine* engine = asCreateScriptEngine();
|
|
assert(engine);
|
|
|
|
RegisterStdString(engine);
|
|
|
|
// Register the message callback
|
|
int r = engine->SetMessageCallback(asFUNCTION(AngelScriptMessageCallback), nullptr, asCALL_CDECL);
|
|
assert(r >= 0);
|
|
|
|
// Register the Print function for scripts
|
|
r = engine->RegisterGlobalFunction("void Print(const string &in)", asFUNCTION(Print), asCALL_CDECL);
|
|
assert(r >= 0);
|
|
|
|
// Create a module and add a simple script
|
|
asIScriptModule* mod = engine->GetModule("Game", asGM_ALWAYS_CREATE);
|
|
const char* scriptCode =
|
|
"void main() { "
|
|
" Print('Hello from AngelScript!'); "
|
|
"}";
|
|
mod->AddScriptSection("test", scriptCode);
|
|
r = mod->Build();
|
|
if (r < 0) std::cerr << "Failed to build script.\n";
|
|
|
|
// Prepare and execute the script
|
|
asIScriptFunction* func = mod->GetFunctionByDecl("void main()");
|
|
asIScriptContext* ctx = engine->CreateContext();
|
|
ctx->Prepare(func);
|
|
ctx->Execute();
|
|
ctx->Release();
|
|
|
|
// Main loop
|
|
while (!WindowShouldClose()) {
|
|
BeginDrawing();
|
|
ClearBackground(RAYWHITE);
|
|
DrawText("Raylib + AngelScript running!", 180, 280, 20, DARKGRAY);
|
|
EndDrawing();
|
|
}
|
|
|
|
// Clean up
|
|
CloseWindow();
|
|
engine->Release();
|
|
}
|