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 = new GxCamera();
    } // create

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

    public void update()
        this.deltaSecs  =; this.deltaSecs );
        //this.controls.update( this.deltaSecs );
        //this.physics.update( this.deltaSecs );
        // 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;

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

public void render () {, 0.21f, 0.2f, 1.0f);;

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

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

    spr.draw( batch );
} // 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