Hello World - Gornova/MarteEngine GitHub Wiki

Hello World is the first program that anyone must develop to understand a new language or a new platform: in this tutorial it will be explained how to write the most simple Hello World example with MarteEngine.

Our Hello World example shows you how to write a single (and boring) string on screen "Hello World", but it also explains some core concepts of MarteEngine.

Main class

In our case the Main class is the one that launches the application. Imagine that every game in MarteEngine has a number of different worlds: loading resources, title screen, options, introduction, game, pause, etc...

Now what you need is a central class that initiates all of those worlds and starts the work!

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

/**
 * Launch Hello World example
**/
public class HelloWorldTest extends StateBasedGame {

	public HelloWorldTest(String title) {
		super(title);
	}

	@Override
	public void initStatesList(GameContainer container) throws SlickException {
                // add HelloWorld to current game with id 1
		addState(new HelloWorld(1, container));
	}
	
	public static void main(String[] argv) {
		try {
			AppGameContainer container = new AppGameContainer(new HelloWorldTest(
					"Hello World Marte Engine"));
			container.setDisplayMode(800, 600, false);
			container.setTargetFrameRate(60);
			container.start();
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}

}

You can notice the main method that Java calls when the game will start and another overriden method: initStatesList. In this particular method MarteEngine will initialize all states/worlds of your game. In our simple test case there is only one state HelloWorld.

Hello World

You can see HelloWorld source code here:

package test;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import it.randomtower.engine.World;

public class HelloWorld extends World {

	public HelloWorld(int id, GameContainer container) {
		super(id, container);
	}
	
	@Override
	public void render(GameContainer container, StateBasedGame stateBasedGame, Graphics g)
			throws SlickException {
		super.render(container,stateBasedGame,g);
		g.drawString("Hello World", 300, 200);
	}

}

You can see that our HelloWorld class extends World (we call states of your game World because that's basically what they are!) and another overriden method, render. Using render MarteEngine draws something on screen, in our case, the string Hello World at position x=300 and y=200.

To see the example running, just run Main class!

If you feel comfortable with the code you can continue with the Entity and World tutorial!


MarteEngine version 0.3