-
-
Notifications
You must be signed in to change notification settings - Fork 33
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.
-
Create a new Dub project and add
"dagon": "~master"
dependency to yourdub.json
. -
Import
dagon
module and create a class that inherits fromScene
:
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.
- 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.
- Add a
main
function, create an instance ofMyApplication
and call itsrun
method:
void main(string[] args)
{
MyApplication app = New!MyApplication(args);
app.run();
Delete(app);
}
- 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.