-
-
Notifications
You must be signed in to change notification settings - Fork 33
Tutorial 1. Simple Application
This tutorial will guide you through creating your first Dagon application.
-
Create a new Dub project and add
"dagon": "~master"
dependency to yourdub.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). -
Import
dagon
module and create a class that inherits fromScene
:
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.
- 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 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.