AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Tutorial 2.2: Engine main file

Ein Thema von DP News-Robot · begonnen am 4. Apr 2020
Antwort Antwort
Benutzerbild von DP News-Robot
DP News-Robot

Registriert seit: 4. Jun 2010
15.404 Beiträge
 
#1

Tutorial 2.2: Engine main file

  Alt 4. Apr 2020, 22:10
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.





Weiterlesen...
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 17:10 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz