Skip to content

Tutorial 1. Simple Application

Timur Gafarov edited this page Oct 31, 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. You can use a release instead of the master branch if you want stability, but that way you won't get all the latest features (remember that Dagon is not production-ready).

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

module main;

import dagon;

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 Windows, you can take the lib directory with its content from Dagon demo application and put it to your project along with the application executable. Otherwise Dagon will use SDL2 and Freetype installed system-wise.

NOTE: Dagon 0.9.0 will automatically copy all necessary files after build, so you won't have to do it manually.

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.

Browse source code for this tutorial