Skip to content

Using Slingworks Basics

JonSnowbd edited this page Sep 27, 2021 · 1 revision

You need to have installed slingworks first

Consider the initial main:

const sling = @import("sling");

pub fn main() void {
    sling.run();
}

This gets a basic editor and blank screen up and running, but is basically useless without content to create.

Slingworks operates on purely your types, it offers no initial content and is up to you to expose the types and how they should work inside the engine.

Before we integrate some types, lets go over static initialization. Slingworks lets you add on functions to be called after GLFW/GL/Ig is initialized

const sling = @import("sling");

pub fn main() anyerror!void {
    // Add them before the run
    sling.addStaticInit(init);
    sling.run();
}

fn init() void {
    // Here you can do any loading
    sling.setWindowTitle("Pong");
}

And now we can get over to our first two types

const Scene = struct {
    // Slingworks doesnt allow empty objects, so for
    // now we will put in some dummy data
    dummy:i32=0,
    // On each of your types this is important if they are to be used
    // inside of a scene, or as a scene. 
    pub fn slingIntegration() void {
        // We dont need the return value of configure yet.
        // Just know that calling this registers this type
        // into sling.
        _ = sling.configure(Scene);
    }
};

and

const Ball = struct {
    position: sling.math.Vec2 = .{},
    velocity: sling.math.Vec2 = .{.x=110,.y=40},

    pub fn update(self: *Ball) void {
        // Arbitrary bounds:
        // This isn't a good way to do this, as it can lock outside, but it doesnt matter here.
        if(self.position.x > 100 or self.position.x < -100) {
            self.velocity.x *= -1;
        }
        if(self.position.y > 100 or self.position.y < -100) {
            self.velocity.y *= -1;
        }

        // You can access delta time and most global state from sling itself.
        self.position = self.position.add(self.velocity.scale(sling.dt));
        sling.debug.dot(self.position, 10.0, .err);
    }

    pub fn editor(self: *Ball) void {
        // In the editor part, you want to avoid any 
        sling.debug.dot(self.position, 10.0, .err);
        sling.debug.dot(self.position.add(self.velocity), 2, .warning);

        // the sling.handles group lets you create editor 'handles'
        // that let you drag and interact to manipulate data.
        sling.handles.positionHandle(&self.position, false, null);
        sling.handles.positionHandle(&self.velocity, true, self.position);
    }

    pub fn slingIntegration() void {
        var config = sling.configure(Ball);
        config.updateMethod(.update, .gameOnly);
        config.updateMethod(.editor, .editorOnly);
    }
};

With these two simple types we have enough to register a scene type inside of slingworks, then it will be possible to create your content through the ingame editor. To register we must go back into our main;

pub fn main() anyerror!void {
    // Remember that slingIntegration method we had on those types?
    // sling.integrate calls that, to keep things organized and typed.
    // This is the preferred way instead of having configures littered in your main
    sling.integrate(Scene);
    sling.integrate(Ball);

    // the first type is the parent type of which there is one, and the
    // second parameter is a tuple list of each child type, which will be many.
    sling.register.scene(Scene, .{Ball});

    sling.addStaticInit(init);
    sling.run();
}

And with that we have a working scene. Boot your game up with the editor argument like zig build run -- editor or ./zig-out/bin/game.exe editor

It will take a bit of time to organize the editor windows as you like them. Be sure to drag them by the title bar to show the docking handles to set up your editor as you prefer.

Once you're settled you can create a new scene with the file menu in the top left, and use the palette window to spawn a ball.

Under the room menu tab you can playtest the scene you're currently editing, and watch the ball bounce within its 200x200 bounds.

Congrats, you have a working sling engine, optimized to allow your devs and fans alike create and modify the games content.

Clone this wiki locally