Skip to content

Tutorial 1. Simple Application

Timur Gafarov edited this page May 24, 2018 · 30 revisions

This tutorial will guide you through creating your first Dagon application.

  1. Create a new Dub project and add "dagon": "~master" dependency to your dub.json.

  2. Import dagon module and create a class that inherits from Scene:

class TestScene: Scene
{
    OBJAsset aOBJSuzanne;

    this(SceneManager smngr)
    {
        super(smngr);
    }

    override void onAssetsRequest()
    {    
        aOBJSuzanne = addOBJAsset("data/suzanne.obj");
    }

    override void onAllocate()
    {
        super.onAllocate();
        
        view = New!Freeview(eventManager, assetManager);
        
        auto mat = createMaterial();
        mat.diffuse = Color4f(1.0, 0.2, 0.2, 1.0);

        auto eSuzanne = createEntity3D();
        eSuzanne.drawable = aOBJSuzanne.mesh;
        eSuzanne.material = mat;
        eSuzanne.position = Vector3f(0, 1, 0);
        
        auto ePlane = createEntity3D();
        ePlane.drawable = New!ShapePlane(10, 10, 1, assetManager);
    }
}

In the example above we load a model from OBJ file (data/suzanne.obj) and attach it to an entity. We also create a plane object in the same manner. Assigning Freeview object to the view property allows the user to navigate the scene with mouse.

  1. Create a class that inherits from SceneApplication and add your scene to it:
class MyApplication: SceneApplication
{
    this(string[] args)
    {
        super(1280, 720, false, "Dagon demo", args);

        TestScene test = New!TestScene(sceneManager);
        sceneManager.addScene(test, "TestScene");
        sceneManager.goToScene("TestScene");
    }
}

The code is obvious: we create an instance of our TestScene and switch to it.

  1. Add a main function, create an instance of MyApplication and call its run method:
void main(string[] args)
{
    MyApplication app = New!MyApplication(args);
    app.run();
    Delete(app);
}
  1. Compile and run. Make sure to have latest SDL2 and Freetype installed. If you're on 64-bit Windows, you can take them from Dagon demo application and put to your project in lib/x64 directory.

You should see something like this:

Use left mouse button to rotate the view, right mouse button to translate, and mouse wheel (or LMB+Ctrl) to zoom.