Sign in to follow this  
  • entries
    90
  • comments
    59
  • views
    42091

NUTS

Sign in to follow this  
diablo_tk

123 views

Well what an adventure today has been.

First of all it was way to hot to do anything so I just stayed in bed till 10 and then spent the rest of the morning reading (The Sicons of Shannara - great book). When it had cooled down a bit I risked turing my computer on and just sat there blankley thinking about what I could do. So eventually I came to the decision of doing more to my engine and doing somthing that I have really wanted to do for a long time - Adding Scripting.

The first question, 'What scripting language should I use?'. Well to tell the truth it took all of 0.2 seconds to think about that and I decided upon 'Squirrel' over 'Lua' because 'Lua' makes me want to die every time I look at how disgusting it is. Anyway after a bit of looking (google) I found the download (sqplus) and started just trying to get it working. Well after much hair pulling I finally figured out how to do stuff with it and have now just been playing around with making the library functions visiable to a script - and it is sooo cool.

As things stand an example script looks like this:

local running = true;

while (running == true)
{

running = UpdateSystem();
UpdateInput();

Cls();

DrawRect(MouseX(), MouseY(), 10, 10);

Flip();

if (KeyDown(27) == true)
running = false;
}


Which is exctally the same as for the hardcoded c++ version, cept you dont have to declare types(bool for 'running').

Basically the above code draws a white box at the mouse position.

The C++ Complimenting code is below (it is just test code - im probaly going to create a full sq version of the engine):

/*

Fallen Smash Bash
-----------------------------

A game written using the Fallen Engine

Basic clone of breakout

*/


/*

Fallen Engine - Test Project

Written by Stefan J Lulham

*/


// System includes
#include
#include
#include
#include

#include
#include
#include
#include

#pragma comment (lib ,"squirrel.lib")
#pragma comment (lib ,"sqdbglib.lib")
#pragma comment (lib ,"sqstdlib.lib")
#pragma comment (lib ,"sqplus.lib")

// Library includes
#include "../Fallen Engine/system.h"
#include "../Fallen Engine/graphics.h"
#include "../Fallen Engine/input.h"
#include "../Fallen Engine/resource_manager.h"

// Game includes
#include "game.h"

// Usee some library's
using namespace Fallen::System;
using namespace Fallen::Graphics;
using namespace Fallen::Input;
using namespace Fallen::ResourceManager;

using namespace std;

using namespace SqPlus;

Key ToFallenKey(SQInteger k)
{
switch(k)
{
case 27:
return KEY_ESCAPE;
}
}

SQInteger s_DrawRect(HSQUIRRELVM v)
{
SQFloat x, y, w, h;
sq_getfloat(v, 2, &x);
sq_getfloat(v, 3, &y);
sq_getfloat(v, 4, &w);
sq_getfloat(v, 5, &h);
CGraphics::DrawRect(x, y, w, h);
return 0;
}

SQInteger s_SetDrawColor(HSQUIRRELVM v)
{
SQFloat r, g, b;
sq_getfloat(v, 2, &r);
sq_getfloat(v, 3, &g);
sq_getfloat(v, 4, &b);
CGraphics::SetDrawColor(r, g, b);
return 0;
}

SQInteger s_MouseX(HSQUIRRELVM v)
{
sq_pushfloat(v, SQFloat(CInput::MouseX()));
return 1;
}

SQInteger s_MouseY(HSQUIRRELVM v)
{
sq_pushfloat(v, SQFloat(CInput::MouseY()));
return 1;
}

SQInteger s_KeyDown(HSQUIRRELVM v)
{
SQInteger k;
sq_getinteger(v, 2, &k);
sq_pushbool(v, (SQBool)CInput::KeyDown(ToFallenKey(k)));
return 1;
}

SQInteger s_Cls(HSQUIRRELVM v)
{
CGraphics::Cls();
return 0;
}

SQInteger s_Flip(HSQUIRRELVM v)
{
CGraphics::Flip();
return 0;
}

SQInteger s_UpdateInput(HSQUIRRELVM v)
{
CInput::Update();
return 0;
}

SQInteger s_UpdateSystem(HSQUIRRELVM v)
{
sq_pushbool(v, (SQBool)CSystem::Update());
return 1;
}

SQRESULT register_functions(HSQUIRRELVM v)
{
SquirrelVM::CreateFunctionGlobal(s_DrawRect, _SC("DrawRect"), _SC("*"));
SquirrelVM::CreateFunctionGlobal(s_SetDrawColor, _SC("SetDrawColor"), _SC("*"));
SquirrelVM::CreateFunctionGlobal(s_MouseX, _SC("MouseX"), _SC(""));
SquirrelVM::CreateFunctionGlobal(s_MouseY, _SC("MouseY"), _SC(""));
SquirrelVM::CreateFunctionGlobal(s_Flip, _SC("Flip"), _SC(""));
SquirrelVM::CreateFunctionGlobal(s_Cls, _SC("Cls"), _SC(""));
SquirrelVM::CreateFunctionGlobal(s_UpdateInput, _SC("UpdateInput"), _SC(""));
SquirrelVM::CreateFunctionGlobal(s_KeyDown, _SC("KeyDown"), _SC("*"));
SquirrelVM::CreateFunctionGlobal(s_UpdateSystem, _SC("UpdateSystem"), _SC(""));
return SQ_OK;
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{/*
Game* game = new Game();

game->Run();

delete game;*/




// Load modules
CSystem::LoadModule();
CGraphics::LoadModule();
CInput::LoadModule();

// Set the interfaces
CSystem::SetInterface(CSystem::Interface_Win32());
CGraphics::SetInterface(CGraphics::Interface_OpenGL());
CInput::SetInterface(CInput::Interface_Win32());

// Setup the graphics context
CGraphics::CreateContext("Fallen Smash Bash", 800, 600, 32, false);
CGraphics::SetMaskColor(255, 0, 255);
CGraphics::SetVSync(true);
CGraphics::SetHandle(0, 0);

bool running = true;

SquirrelVM::Init();
register_functions(SquirrelVM::GetVMPtr());
SquirrelObject test;
test = SquirrelVM::CompileScript("script.nut");

//while (running == true)
//{
//running = CSystem::Update();
//CInput::Update();

//CGraphics::Cls();

SquirrelVM::RunScript(test);

//CGraphics::DrawRect(100, 100, 100, 100);

//CGraphics::Flip();

//if (CInput::KeyPressed(KEY_ESCAPE))
// running = false;
//}

SquirrelVM::Shutdown();

CGraphics::DestroyContext();

CGraphics::UnloadModule();
CSystem::UnloadModule();
CInput::UnloadModule();

CResourceManager::UnloadResources();
}



Oh I also got a really SUPER COOL book the other day that cost about GBP100 -`Character Modeling 2 - /Ballistic/ book number '0145'` It came with a certificate and four prints of some of the works found inside.

Sign in to follow this  


3 Comments


Recommended Comments

But how does Squirrel work for functors? One of the big reasons I'm using Lua is how nice LuaPlus is for functors.

Share this comment


Link to comment
Im not sure about luaplus , but in sqplus you basicaly have something like the following:

SQInteger foo(HSQUIRRELVM v) // SQ Function
{
float blob;
sq_getfloat(v, 2, &blob); // Get one argument (2 - Is the offset so to get next argument use 3, 4, etc
// Call a function or somthing
GraphicsSetAlpha(blob);
sq_pushbool(v, (SQBool)true); // push a return value
return 1; // The function returns a value (0 if it doesnt)
}

// Register the function
// Use createFunctionGlobal to add it to the global table
// arg1: SQFunction (above)
// arg2: Script function name
// arg3: Argument mask (user '*' for any data type)
SquirrelVM::CreateFunctionGlobal(foo, _SC("Foo"), _SC("*"));




Im still lerning my self ;) (I only got it working tonight and it took some figuring out)

Share this comment


Link to comment
Re- functors/closures and script access: SqPlus was derived from LuaPlus and extended for Squirrel. See testSqPlus2.cpp in the SqPlus package: http://wiki.squirrel-lang.org/default.aspx/SquirrelWiki/SqPlus.html. More info here: http://squirrel-lang.org/forums/default.aspx.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now