Skip to content

Commit

Permalink
Use simplesquirrel library for creating Squirrel API binds
Browse files Browse the repository at this point in the history
Incorporates a SuperTux fork of the `simplesquirrel` library. Most important feature of this library in the context of SuperTux is the ability to easily expose functions, classes (which can include properties) and other entries to Squirrel in one or a few lines.

Every exposed class, variable and function has been migrated to incorporate this change. As a result, the `scripting/` directory, which included all scripting functions, classes and variables, as well as the API wrapper, has been removed, since it is no longer needed for the new binds.

Some scripting functions have been deprecated (currently only via a tag in comment, later to be migrated to docmentation) for several reasons:
* Some `get_` and `set_` functions have been deprecated, since a class property has been added and should replace their usage.
* Functions with the names `get_pos_x` or `get_pos_y` have been replaced with `get_x` and `get_y` for consistency, thus are now deprecated.

This way of creating Squirrel binds would allow for way more flexibility in exposing functions, classes and properties.

**TODO:**

* Expand usage of simplesquirrel functions, instead of ones in `SquirrelVM`, to reduce code duplication.
* Update the scripting API documentation/reference generator to incorporate all changes.

Closes SuperTux#2736.
  • Loading branch information
Vankata453 committed Feb 20, 2024
1 parent d11e926 commit 799e7eb
Show file tree
Hide file tree
Showing 187 changed files with 5,682 additions and 27,517 deletions.
5 changes: 1 addition & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@
[submodule "external/fmt"]
path = external/fmt
url = https://github.com/fmtlib/fmt.git
[submodule "tools/miniswig"]
path = tools/miniswig
url = https://github.com/WindstilleTeam/miniswig
[submodule "external/glm"]
path = external/glm
url = https://github.com/g-truc/glm.git
[submodule "external/simplesquirrel"]
path = external/simplesquirrel
url = https://github.com/matusnovak/simplesquirrel
url = https://github.com/SuperTux/simplesquirrel
5 changes: 0 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ include(SuperTux/ProvideOpenGL)
include(SuperTux/BuildVersion)
include(SuperTux/BuildDocumentation)
include(SuperTux/BuildMessagePot)
include(SuperTux/BuildMiniswigWrapper)

## Build list of sources for supertux binary
file(GLOB SUPERTUX_SOURCES_C RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} external/obstack/*.c external/findlocale/findlocale.c)
Expand All @@ -207,10 +206,6 @@ list(SORT SUPERTUX_SOURCES_C)
list(SORT SUPERTUX_SOURCES_CXX)
list(SORT SUPERTUX_RESOURCES)

if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/scripting/wrapper.cpp)
set(SUPERTUX_SOURCES_CXX ${SUPERTUX_SOURCES_CXX} ${CMAKE_CURRENT_SOURCE_DIR}/src/scripting/wrapper.cpp)
endif()

## On Windows, add an icon
if(WIN32)
if(MINGW)
Expand Down
16 changes: 0 additions & 16 deletions data/scripts/default.nut
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,6 @@
* and variables you define here can be used in all threads
*/

//Create Level table
Level <- {
finish=Level_finish,
has_active_sequence=Level_has_active_sequence,
spawn=Level_spawn,
set_start_point=Level_set_start_point,
set_start_pos=Level_set_start_pos,
set_respawn_point=Level_set_respawn_point,
set_respawn_pos=Level_set_respawn_pos,
flip_vertically=Level_flip_vertically,
toggle_pause=Level_toggle_pause,
pause_target_timer=Level_pause_target_timer,
resume_target_timer=Level_resume_target_timer
};


function end_level()
{
play_music("music/misc/leveldone.ogg");
Expand Down
27 changes: 0 additions & 27 deletions mk/cmake/SuperTux/BuildMiniswigWrapper.cmake

This file was deleted.

15 changes: 13 additions & 2 deletions src/badguy/badguy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

#include "badguy/badguy.hpp"

#include <simplesquirrel/class.hpp>
#include <simplesquirrel/vm.hpp>

#include "audio/sound_manager.hpp"
#include "badguy/dispenser.hpp"
#include "editor/editor.hpp"
Expand Down Expand Up @@ -50,7 +53,6 @@ BadGuy::BadGuy(const Vector& pos, const std::string& sprite_name, int layer,
BadGuy::BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer,
const std::string& light_sprite_name, const std::string& ice_sprite_name) :
MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED),
ExposedObject<BadGuy, scripting::BadGuy>(this),
m_physic(),
m_countMe(true),
m_is_initialized(false),
Expand Down Expand Up @@ -93,7 +95,6 @@ BadGuy::BadGuy(const ReaderMapping& reader, const std::string& sprite_name,
Direction default_direction, int layer,
const std::string& light_sprite_name, const std::string& ice_sprite_name) :
MovingSprite(reader, sprite_name, layer, COLGROUP_DISABLED),
ExposedObject<BadGuy, scripting::BadGuy>(this),
m_physic(),
m_countMe(true),
m_is_initialized(false),
Expand Down Expand Up @@ -1126,4 +1127,14 @@ BadGuy::add_wind_velocity(const Vector& velocity, const Vector& end_speed)
m_physic.set_velocity_y(std::max(m_physic.get_velocity_y() + velocity.y, end_speed.y));
}


void
BadGuy::register_class(ssq::VM& vm)
{
ssq::Class cls = vm.addAbstractClass<BadGuy>("BadGuy");

cls.addFunc("kill", &BadGuy::kill_fall);
cls.addFunc("ignite", &BadGuy::ignite);
}

/* EOF */
7 changes: 4 additions & 3 deletions src/badguy/badguy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
#include "editor/object_option.hpp"
#include "object/moving_sprite.hpp"
#include "object/portable.hpp"
#include "scripting/badguy.hpp"
#include "squirrel/exposed_object.hpp"
#include "supertux/physic.hpp"
#include "supertux/timer.hpp"

Expand All @@ -31,9 +29,11 @@ class Bullet;

/** Base class for moving sprites that can hurt the Player. */
class BadGuy : public MovingSprite,
public ExposedObject<BadGuy, scripting::BadGuy>,
public Portable
{
public:
static void register_class(ssq::VM& vm);

public:
BadGuy(const Vector& pos, const std::string& sprite_name, int layer = LAYER_OBJECTS,
const std::string& light_sprite_name = "images/objects/lightmap_light/lightmap_light-medium.sprite",
Expand All @@ -58,6 +58,7 @@ class BadGuy : public MovingSprite,

static std::string class_name() { return "badguy"; }
virtual std::string get_class_name() const override { return class_name(); }
virtual std::string get_exposed_class_name() const override { return "BadGuy"; }
static std::string display_name() { return _("Badguy"); }
virtual std::string get_display_name() const override { return display_name(); }

Expand Down
14 changes: 13 additions & 1 deletion src/badguy/dispenser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

#include "badguy/dispenser.hpp"

#include <simplesquirrel/class.hpp>
#include <simplesquirrel/vm.hpp>

#include "audio/sound_manager.hpp"
#include "editor/editor.hpp"
#include "math/random.hpp"
Expand All @@ -30,7 +33,6 @@

Dispenser::Dispenser(const ReaderMapping& reader) :
BadGuy(reader, "images/creatures/dispenser/dropper.sprite", LAYER_OBJECTS + 5),
ExposedObject<Dispenser, scripting::Dispenser>(this),
m_cycle(),
m_objects(),
m_next_object(0),
Expand Down Expand Up @@ -433,4 +435,14 @@ Dispenser::on_flip(float height)
FlipLevelTransformer::transform_flip(m_flip);
}


void
Dispenser::register_class(ssq::VM& vm)
{
ssq::Class cls = vm.addAbstractClass<Dispenser>("Dispenser", vm.findClass("BadGuy"));

cls.addFunc("activate", &Dispenser::activate);
cls.addFunc("deactivate", &Dispenser::deactivate);
}

/* EOF */
19 changes: 5 additions & 14 deletions src/badguy/dispenser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
#define HEADER_SUPERTUX_BADGUY_DISPENSER_HPP

#include "badguy/badguy.hpp"
#include "scripting/dispenser.hpp"
#include "squirrel/exposed_object.hpp"

class GameObject;

class Dispenser final : public BadGuy,
public ExposedObject<Dispenser, scripting::Dispenser>
class Dispenser final : public BadGuy
{
public:
static void register_class(ssq::VM& vm);

private:
enum DispenserType {
DROPPER, CANNON, POINT, GRANITO
Expand All @@ -48,6 +48,7 @@ class Dispenser final : public BadGuy,

static std::string class_name() { return "dispenser"; }
virtual std::string get_class_name() const override { return class_name(); }
virtual std::string get_exposed_class_name() const override { return "Dispenser"; }
static std::string display_name() { return _("Dispenser"); }
virtual std::string get_display_name() const override { return display_name(); }

Expand All @@ -57,16 +58,6 @@ class Dispenser final : public BadGuy,

virtual void on_flip(float height) override;

virtual void expose(HSQUIRRELVM vm, SQInteger table_idx) override
{
ExposedObject<Dispenser, scripting::Dispenser>::expose(vm, table_idx);
}

virtual void unexpose(HSQUIRRELVM vm, SQInteger table_idx) override
{
ExposedObject<Dispenser, scripting::Dispenser>::unexpose(vm, table_idx);
}

void notify_dead() {
if (m_limit_dispensed_badguys) {
m_current_badguys--;
Expand Down
58 changes: 31 additions & 27 deletions src/badguy/willowisp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ static const std::string SOUNDFILE = "sounds/willowisp.wav";
WillOWisp::WillOWisp(const ReaderMapping& reader) :
BadGuy(reader, "images/creatures/willowisp/willowisp.sprite", LAYER_FLOATINGOBJECTS,
"images/objects/lightmap_light/lightmap_light-small.sprite"),
ExposedObject<WillOWisp, scripting::WillOWisp>(this),
PathObject(),
m_mystate(STATE_IDLE),
m_target_sector(),
Expand Down Expand Up @@ -262,44 +261,37 @@ WillOWisp::collision_player(Player& player, const CollisionHit& ) {
void
WillOWisp::goto_node(int node_no)
{
get_walker()->goto_node(node_no);
if (m_mystate != STATE_PATHMOVING && m_mystate != STATE_PATHMOVING_TRACK) {
m_mystate = STATE_PATHMOVING;
}
}

void
WillOWisp::start_moving()
{
get_walker()->start_moving();
}
PathObject::goto_node(node_no);

void
WillOWisp::stop_moving()
{
get_walker()->stop_moving();
if (m_mystate != STATE_PATHMOVING && m_mystate != STATE_PATHMOVING_TRACK)
m_mystate = STATE_PATHMOVING;
}

void
WillOWisp::set_state(const std::string& new_state)
{
if (new_state == "stopped") {
if (new_state == "stopped")
m_mystate = STATE_STOPPED;
} else if (new_state == "idle") {
else if (new_state == "idle")
m_mystate = STATE_IDLE;
} else if (new_state == "move_path") {
else if (new_state == "move_path")
{
m_mystate = STATE_PATHMOVING;
get_walker()->start_moving();
} else if (new_state == "move_path_track") {
if (get_walker())
get_walker()->start_moving();
}
else if (new_state == "move_path_track")
{
m_mystate = STATE_PATHMOVING_TRACK;
get_walker()->start_moving();
} else if (new_state == "normal") {
if (get_walker())
get_walker()->start_moving();
}
else if (new_state == "normal")
m_mystate = STATE_IDLE;
} else if (new_state == "vanish") {
else if (new_state == "vanish")
vanish();
} else {
log_warning << "Can't set unknown willowisp state '" << new_state << std::endl;
}
else
log_warning << "Cannot set unknown Will-O-Wisp state: '" << new_state << "'." << std::endl;
}

ObjectSettings
Expand Down Expand Up @@ -364,4 +356,16 @@ WillOWisp::on_flip(float height)
PathObject::on_flip();
}


void
WillOWisp::register_class(ssq::VM& vm)
{
ssq::Class cls = vm.addAbstractClass<WillOWisp>("WillOWisp", vm.findClass("BadGuy"));

PathObject::register_members(cls);

cls.addFunc("goto_node", &WillOWisp::goto_node);
cls.addFunc("set_state", &WillOWisp::set_state);
}

/* EOF */
35 changes: 15 additions & 20 deletions src/badguy/willowisp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@

#include "badguy/badguy.hpp"
#include "object/path_object.hpp"
#include "squirrel/exposed_object.hpp"
#include "scripting/willowisp.hpp"

class SoundSource;

class WillOWisp final :
public BadGuy,
public ExposedObject<WillOWisp, scripting::WillOWisp>,
public PathObject
class WillOWisp final : public BadGuy,
public PathObject
{
public:
static void register_class(ssq::VM& vm);

public:
WillOWisp(const ReaderMapping& reader);

Expand All @@ -44,16 +43,22 @@ class WillOWisp final :
virtual bool is_hurtable() const override { return false; }
virtual void kill_fall() override { vanish(); }

virtual void goto_node(int node_no);
virtual void set_state(const std::string& state);
virtual void start_moving();
virtual void stop_moving();
void goto_node(int node_no);

/**
* Sets the state of the WillOWisp.
* @param string $state One of the following: "stopped", "move_path" (moves along a path),
"move_path_track" (moves along a path but catches Tux when he is near), "normal" (starts tracking Tux when he is near enough),
"vanish".
*/
void set_state(const std::string& state);

virtual void stop_looping_sounds() override;
virtual void play_looping_sounds() override;

static std::string class_name() { return "willowisp"; }
virtual std::string get_class_name() const override { return class_name(); }
virtual std::string get_exposed_class_name() const override { return "WillOWisp"; }
static std::string display_name() { return _("Will o' Wisp"); }
virtual std::string get_display_name() const override { return display_name(); }

Expand All @@ -62,16 +67,6 @@ class WillOWisp final :

virtual void on_flip(float height) override;

virtual void expose(HSQUIRRELVM vm, SQInteger table_idx) override
{
ExposedObject<WillOWisp, scripting::WillOWisp>::expose(vm, table_idx);
}

virtual void unexpose(HSQUIRRELVM vm, SQInteger table_idx) override
{
ExposedObject<WillOWisp, scripting::WillOWisp>::unexpose(vm, table_idx);
}

/** make WillOWisp vanish */
void vanish();

Expand Down
2 changes: 2 additions & 0 deletions src/interface/control_enum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include "interface/control.hpp"

#include "util/log.hpp"

template<class T>
class ControlEnum : public InterfaceControl
{
Expand Down
Loading

0 comments on commit 799e7eb

Please sign in to comment.