Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified build/Spacepunk_Debug.exe
Binary file not shown.
Binary file modified build/Spacepunk_Hybrid.exe
Binary file not shown.
Binary file modified build/Spacepunk_Release.exe
Binary file not shown.
85 changes: 35 additions & 50 deletions src/ArrayList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
#pragma once

#include "Main.hpp"

#include <luajit-2.0/lua.hpp>
#include <LuaBridge/LuaBridge.h>
#include "sol.hpp"

// templated ArrayList (similar to std::vector)
// adding or removing elements can unsort the list.
Expand Down Expand Up @@ -363,56 +361,43 @@ class ArrayList {

// exposes this list type to a script
// @param lua The script engine to expose to
// @param name The type name in lua
static void exposeToScript(lua_State* lua, const char* name) {
typedef T* (ArrayList<T>::*ArrayFn)();
ArrayFn getArray = static_cast<ArrayFn>(&ArrayList<T>::getArray);

typedef const T* (ArrayList<T>::*ArrayConstFn)() const;
ArrayConstFn getArrayConst = static_cast<ArrayConstFn>(&ArrayList<T>::getArray);

typedef ArrayList<T>& (ArrayList<T>::*CopyFn)(const ArrayList<T>&);
CopyFn copy = static_cast<CopyFn>(&ArrayList<T>::copy);

typedef T& (ArrayList<T>::*PeekFn)();
PeekFn peek = static_cast<PeekFn>(&ArrayList<T>::peek);

typedef const T& (ArrayList<T>::*PeekConstFn)() const;
PeekConstFn peekConst = static_cast<PeekConstFn>(&ArrayList<T>::peek);

typedef T& (ArrayList<T>::*GetFn)(size_t);
GetFn get = static_cast<GetFn>(&ArrayList<T>::get);

typedef const T& (ArrayList<T>::*GetConstFn)(size_t) const;
GetConstFn getConst = static_cast<GetConstFn>(&ArrayList<T>::get);

luabridge::getGlobalNamespace(lua)
.beginClass<ArrayList<T>>(name)
.addConstructor<void (*)()>()
.addFunction("getArray", getArray)
.addFunction("getArrayConst", getArrayConst)
.addFunction("getSize", &ArrayList<T>::getSize)
.addFunction("getMaxSize", &ArrayList<T>::getMaxSize)
.addFunction("empty", &ArrayList<T>::empty)
.addFunction("alloc", &ArrayList<T>::alloc)
.addFunction("resize", &ArrayList<T>::resize)
.addFunction("clear", &ArrayList<T>::clear)
.addFunction("copy", copy)
.addFunction("push", &ArrayList<T>::push)
.addFunction("insert", &ArrayList<T>::insert)
.addFunction("pop", &ArrayList<T>::pop)
.addFunction("peek", peek)
.addFunction("peekConst", peekConst)
.addFunction("remove", &ArrayList<T>::remove)
.addFunction("removeAndRearrange", &ArrayList<T>::removeAndRearrange)
.addFunction("get", get)
.addFunction("getConst", getConst)
.endClass()
;
// @param luaTypeName The type name in lua
static void exposeToScript(sol::state& lua, const char* luaTypeName)
{
//First identify the constructors.
sol::constructors<ArrayList<T>()> constructors;

//Then do the thing.
sol::usertype<ArrayList<T>> usertype(constructors,
//"getArray", sol::resolve<T*()>(&ArrayList<T>::getArray), //This is not exposed because bad juju. GCC will kill you. (When passing in World* type, it hits strange errors for some reason...is it because it's an abstract base class?)
//"getArrayConst", sol::resolve<const T*() const>(&ArrayList<T>::getArray),
"getSize", &ArrayList<T>::getSize,
"getMaxSize", &ArrayList<T>::getMaxSize,
"empty", &ArrayList<T>::empty,
"alloc", &ArrayList<T>::alloc,
"resize", &ArrayList<T>::resize,
"clear", &ArrayList<T>::clear,
"copy", sol::resolve<ArrayList<T>&(const ArrayList<T>&)>(&ArrayList<T>::copy),
"push", &ArrayList<T>::push,
"insert", &ArrayList<T>::insert,
"pop", &ArrayList<T>::pop,
"peek", sol::resolve<T&()>(&ArrayList<T>::peek),
"peekConst", sol::resolve<const T&() const>(&ArrayList<T>::peek),
"remove", &ArrayList<T>::remove,
"removeAndRearrange", &ArrayList<T>::removeAndRearrange,
"get", sol::resolve<T&(size_t)>(&ArrayList<T>::get),
"getConst", sol::resolve<const T&(size_t) const>(&ArrayList<T>::get)
);

//Then do the thing.
//sol::usertype<ArrayList<T>> usertype(constructors);

//Finally register the thing.
lua.set_usertype(luaTypeName, usertype);
}

private:
T* arr = nullptr; // array data
size_t size = 0; // current array capacity
size_t maxSize = 0; // maximum array capacity
};
};
30 changes: 30 additions & 0 deletions src/BBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,3 +647,33 @@ void BBox::serialize(FileInterface* file) {
file->property("mass", mass);
}
}

//These functions are not in the script.cpp file since they drastically increase compile time and memory usage due to heavy template usage.
void Script::exposeBBox() {
Copy link
Collaborator

@SheridanR SheridanR Jan 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine for a first pass but if these expose functions could be members of their respective classes (ie BBox::exposeToScript() instead of Script::exposeBBox()) it would be way better.

{
//First identify the constructors.
sol::constructors<BBox(Entity&, Component*)> constructors;

//Then do the thing.
sol::usertype<BBox> usertype(constructors,
sol::base_classes, sol::bases<Component>(),
"nearestCeiling", &BBox::nearestCeiling,
//"nearestFloor", &BBox::nearestFloor,
"distToCeiling", &BBox::distToCeiling,
"distToFloor", &BBox::distToFloor,
"getShape", &BBox::getShape,
"getMass", &BBox::getMass,
"isEnabled", &BBox::isEnabled,
"setEnabled", &BBox::setEnabled,
"setShape", &BBox::setShape,
"setMass", &BBox::setMass,
"findAllOverlappingEntities", &BBox::findAllOverlappingEntities
);

//Finally register the thing.
lua.set_usertype("BBox", usertype);
}

LinkedList<BBox*>::exposeToScript(lua, "LinkedListBBoxPtr", "NodeBBoxPtr");
ArrayList<BBox*>::exposeToScript(lua, "ArrayListBBoxPtr");
}
60 changes: 41 additions & 19 deletions src/Button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#pragma once

#include <assert.h>

#define GLM_FORCE_RADIANS
#include <glm/vec4.hpp>

Expand Down Expand Up @@ -58,7 +60,7 @@ class Button {
const bool isHighlighted() const { return highlighted; }
const bool isDisabled() const { return disabled; }
const style_t getStyle() const { return style; }
Script::Args& getParams() { return params; }
const Script::Args& getParams() const { return params; }

void setBorder(const int _border) { border = _border; }
void setPos(const int x, const int y) { size.x = x; size.y = y; }
Expand All @@ -73,23 +75,43 @@ class Button {
void setStyle(const style_t _style) { style = _style; }
void setPressed(const bool _pressed) { reallyPressed = _pressed; }

template<typename T>
void addParam(T param)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is way better, kudos.

{
if (nullptr == parent)
{
assert(0);
return;
}

Script* scripter = parent->getScriptEngine();
if (nullptr == scripter)
{
assert(0);
return;
}

scripter->addParam(param, params);
}

private:
Frame* parent = nullptr; // parent frame

String name; // internal button name
String text; // button text, if any
String icon; // icon, if any (supersedes text content)
String tooltip; // if empty, button has no tooltip; otherwise, it does
Script::Args params; // optional function parameters to use when the button function is called
int border = 3; // size of the button border in pixels
Rect<int> size; // size and position of the button within its parent frame
bool pressed = false; // button pressed state
bool reallyPressed = false; // the "actual" button state, pre-mouse process
bool highlighted = true; // true if mouse is hovering over button; false otherwise
glm::vec4 color; // the button's color
glm::vec4 textColor; // text color
style_t style = STYLE_NORMAL; // button style
bool disabled=false; // if true, the button is invisible and unusable
Uint32 highlightTime = 0; // records the time since the button was highlighted
Image* iconImg = nullptr; // the icon image
Frame* parent = nullptr; // parent frame

String name; // internal button name
String text; // button text, if any
String icon; // icon, if any (supersedes text content)
String tooltip; // if empty, button has no tooltip; otherwise, it does
//std::vector<sol::object> params; // optional function parameters to use when the button function is called.
Script::Args params; // optional function parameters to use when the button function is called.
int border = 3; // size of the button border in pixels
Rect<int> size; // size and position of the button within its parent frame
bool pressed = false; // button pressed state
bool reallyPressed = false; // the "actual" button state, pre-mouse process
bool highlighted = true; // true if mouse is hovering over button; false otherwise
glm::vec4 color; // the button's color
glm::vec4 textColor; // text color
style_t style = STYLE_NORMAL; // button style
bool disabled=false; // if true, the button is invisible and unusable
Uint32 highlightTime = 0; // records the time since the button was highlighted
Image* iconImg = nullptr; // the icon image
};
34 changes: 33 additions & 1 deletion src/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,36 @@ void Camera::drawDebug() {

void Camera::onFrameDrawn() {
++framesDrawn;
}
}

//These functions are not in the script.cpp file since they drastically increase compile time and memory usage due to heavy template usage.
void Script::exposeCamera() {
{
//First identify the constructors.
sol::constructors<Camera(Entity&, Component*)> constructors;

//Then do the thing.
sol::usertype<Camera> usertype(constructors,
sol::base_classes, sol::bases<Component>(),
"setupProjection", &Camera::setupProjection,
"worldPosToScreenPos", &Camera::worldPosToScreenPos,
"screenPosToWorldRay", &Camera::screenPosToWorldRay,
"getClipNear", &Camera::getClipNear,
"getClipFar", &Camera::getClipFar,
"getWin", &Camera::getWin,
"getFov", &Camera::getFov,
"isOrtho", &Camera::isOrtho,
"setClipNear", &Camera::setClipNear,
"setClipFar", &Camera::setClipFar,
"setWin", &Camera::setWin,
"setFov", &Camera::setFov,
"setOrtho", &Camera::setOrtho
);

//Finally register the thing.
lua.set_usertype("Camera", usertype);
}

LinkedList<Camera*>::exposeToScript(lua, "LinkedListCameraPtr", "NodeCameraPtr");
ArrayList<Camera*>::exposeToScript(lua, "ArrayListCameraPtr");
}
53 changes: 52 additions & 1 deletion src/Character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,55 @@ void Character::serialize(FileInterface * file) {
file->property("perception", perception);
file->property("charisma", charisma);
file->property("luck", luck);
}
}

//These functions are not in the script.cpp file since they drastically increase compile time and memory usage due to heavy template usage.
void Script::exposeCharacter() {
{
auto characterType = lua.create_simple_usertype<Character>(sol::constructors<Character(Entity&, Component*)>(),
sol::base_classes, sol::bases<Component>()
);

//Bind all of the editor's member functions.
characterType.set("getHp", &Character::getHp);
characterType.set("getMp", &Character::getMp);
characterType.set("getSex", &Character::getSex);
characterType.set("getLevel", &Character::getLevel);
characterType.set("getXp", &Character::getXp);
characterType.set("getHunger", &Character::getHunger);
characterType.set("getNanoMatter", &Character::getNanoMatter);
characterType.set("getBioMatter", &Character::getBioMatter);
characterType.set("getNeuroThread", &Character::getNeuroThread);
characterType.set("getGold", &Character::getGold);
characterType.set("getStrength", &Character::getStrength);
characterType.set("getDexterity", &Character::getDexterity);
characterType.set("getIntelligence", &Character::getIntelligence);
characterType.set("getConstitution", &Character::getConstitution);
characterType.set("getPerception", &Character::getPerception);
characterType.set("getCharisma", &Character::getCharisma);
characterType.set("getLuck", &Character::getLuck);
characterType.set("setHp", &Character::setHp);
characterType.set("setMp", &Character::setMp);
characterType.set("setSex", &Character::setSex);
characterType.set("setLevel", &Character::setLevel);
characterType.set("setXp", &Character::setXp);
characterType.set("setHunger", &Character::setHunger);
characterType.set("setNanoMatter", &Character::setNanoMatter);
characterType.set("setBioMatter", &Character::setBioMatter);
characterType.set("setNeuroThread", &Character::setNeuroThread);
characterType.set("setGold", &Character::setGold);
characterType.set("setStrength", &Character::setStrength);
characterType.set("setDexterity", &Character::setDexterity);
characterType.set("setIntelligence", &Character::setIntelligence);
characterType.set("setConstitution", &Character::setConstitution);
characterType.set("setPerception", &Character::setPerception);
characterType.set("setCharisma", &Character::setCharisma);
characterType.set("setLuck", &Character::setLuck);

//Finally register the thing.
lua.set_usertype("Character", characterType);
}

LinkedList<Character*>::exposeToScript(lua, "LinkedListCharacterPtr", "NodeCharacterPtr");
ArrayList<Character*>::exposeToScript(lua, "ArrayListCharacterPtr");
}
10 changes: 5 additions & 5 deletions src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Client::Client() {

Client::~Client() {
if( script ) {
script->dispatch("term");
script->dispatchFunction("term");
delete script;
}
if( gui ) {
Expand Down Expand Up @@ -67,7 +67,7 @@ void Client::init() {
}

script->load("scripts/client/main.lua");
script->dispatch("init");
script->dispatchFunction("init");
}

void Client::handleNetMessages() {
Expand Down Expand Up @@ -681,7 +681,7 @@ void Client::preProcess() {
}
if( framesToRun ) {
renderer->clearBuffers();
script->dispatch("preprocess");
script->dispatchFunction("preprocess");

if( editor ) {
editor->preProcess();
Expand All @@ -706,7 +706,7 @@ void Client::process() {
Game::process();

for( Uint32 frame=0; frame<framesToRun; ++frame ) {
script->dispatch("process");
script->dispatchFunction("process");

// drop down console
if( consoleAllowed ) {
Expand Down Expand Up @@ -877,7 +877,7 @@ void Client::postProcess() {
renderer->swapWindow();

// run script
script->dispatch("postprocess");
script->dispatchFunction("postprocess");

if( editor ) {
editor->postProcess();
Expand Down
Loading