Saturday, April 4, 2020

Tutorial 2.2: Engine main file

Your car is made of many sub-systems. Engine, suspension, airbags, lights, cruise-control, and of course a seat fart absorber. But we all refer it as "a car". You don't bring your exhaust system to the garage, you bring your car. As far as our game is concerned, it talks with "the engine".

So it makes sense to have one main entry point, an Engine class. This class will create, manage (and destroys) its sub-systems, and has some easy functions to access or use these sub-systems. In general, the less functions it offers, the easier it is is to grasp your engine. But typically also the more limited it is. We'll just start simply, and expand whenever we feel it is needed.

public class Engine {
    float   deltaSecs;

    GxCamera camera;

    public Engine()
    {   // Create sub-systems
        this.camera = new GxCamera();
    } // create


    public void shutdown()
    {
        // Dispose sub-systems
    } // shutdown


    public void update()
    {
        this.deltaSecs  = Gdx.graphics.getDeltaTime();

        this.camera.update( this.deltaSecs );
        //this.controls.update( this.deltaSecs );
        //this.physics.update( this.deltaSecs );
        //this.world.update( this.deltaSecs );
        //this.soundManager.update( this.deltaSecs );
    } // update


    public void render( )
    {
        // 
    } // render


    public GxCamera getCamera()
    {
        return camera;
    } // getCamera

} // Engine


Unreal engine has a few more lines of code. The only sub-system we made so far, is a cheap camera. But as the code suggests, it will soon also contain an EntitySystem, soundSystem, well, the parts we have talked about in the previous chapter.

Notice the difference between "Render" and "Update". Both are called every cycle, but I felt like splitting up the visual part (graphics) from the invisible background work. We don't render anything yet here, as the Game code is still in charge of that. Throughout the next chapters, we'll be moving more and more "test snippets" from the Game code into the Engine code. For now, the game code will look as follow:
Engine        engine;
SpriteBatch batch;
Texture     img;
Sprite         spr;

@Override
public void create () {
    engine  = new Engine();
    // TEST CODE
    batch  = new SpriteBatch();
    img    = new Texture("Images/Orb/Orb01.png");

    spr       = new Sprite( img );
    spr.setOrigin( 0,0 );
    spr.setSize( 4,4 );
} // create


@Override
public void render () {
    Gdx.gl.glClearColor(0.05f, 0.21f, 0.2f, 1.0f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    engine.update();
    
    // TEST CODE
    float camMoveSpeed = -2.f *  Gdx.graphics.getDeltaTime();
    engine.getCamera().move( 0.f , camMoveSpeed );

    batch.setProjectionMatrix( engine.getCamera().camera.combined );

    batch.begin();
    spr.draw( batch );
    batch.end();
    
    
    engine.render();
} // render

What this does? Not much. It still draws that purple ball, and the camera is moving to the left so the ball soon dissapears out of view. That's all folks.



No comments:

Post a Comment