Slick Forums

Discuss the Slick 2D Library
It is currently Tue May 21, 2013 2:10 pm

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Mon Aug 13, 2012 11:47 pm 
Offline
Game Developer
User avatar

Joined: Wed Feb 17, 2010 12:24 am
Posts: 594
In regards to this: http://wiki.l33tlabs.org/bin/view/TWL/I ... eBasedGame

Anyone have a "Hello World" example that compiles and runs? I followed the wiki, but keeps crashing when I call super.initGUI();

Code:
Exception in thread "main" java.lang.NullPointerException
   at org.lwjgl.opengl.GL11.glPushAttrib(GL11.java:2594)


Also is there any advantage to using this over TWLInputAdapter? Not sure if I understand the pro/cons of the two ways.


Top
 Profile  
 
PostPosted: Tue Aug 14, 2012 6:07 am 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1171
That looks like you try to call it before the OpenGL context has been created. But as you successfully cut of the stack trace from the exception that's all I can say about it.

The simple TWLInputAdapter version is not state aware - eg you have to handle that yourself.
The TWLStateBasedGame code has it's own root pane for each state which makes it easy to have a main menu in one state, and a game HUD in another state etc.

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
PostPosted: Tue Aug 14, 2012 4:55 pm 
Offline

Joined: Thu Jan 19, 2012 9:13 pm
Posts: 14
I created a Hello World for you, but I already know what was your problem. In the class where you extended TWLStateBasedGame your getThemeURL() method is wrong. It gives back null. Which is because it can't find a theme. Your path is propably wrong there.

these are the libs you need:
slick.jar
twl.jar
lwjgl.jar with natives
xpp3-1.1.4c.jar

resources you need and your project was missing these:
A theme.xml which describe the GUI. In the example I put this into the resources package.

You also need the classes in the TWLSlick.zip

Here are my classes:

Main.java:
Code:
package main;

import java.net.URL;

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

public class Main extends TWLStateBasedGame {

   
   protected Main() {
      super("HelloSlickTWL");
   }

   public static void main(String[] args) {
      try {
         AppGameContainer app = new AppGameContainer(new Main());
         app.setTargetFrameRate(60);
         app.setMaximumLogicUpdateInterval(16);
         app.setMinimumLogicUpdateInterval(16);
         app.setDisplayMode(600,600,false);
         app.start();
      } catch (SlickException e) {
         e.printStackTrace();
      }
   }

   @Override
   protected URL getThemeURL() {
      return Main.class.getResource("/resource/theme.xml");
   }

   @Override
   public void initStatesList(GameContainer arg0) throws SlickException {
      addState(new HelloState());
   }
}


HelloState.java:
Code:
package main;


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

import de.matthiasmann.twl.Button;

public class HelloState extends BasicTWLGameState {

   @Override
   public int getID() {
      return 1;
   }
   
   @Override
   public void enter(GameContainer container, StateBasedGame game)
         throws SlickException {
      super.enter(container, game);
      
      Button btn = new Button("Hello world");
      btn.setSize(100,50);
      btn.setPosition(300, 300);
      btn.addCallback(new Runnable() {
         @Override
         public void run() {
            System.out.println("Hello Slick world! This button works!");
         }
      });
      rootPane.add(btn);
   }
   
   @Override
   public void init(GameContainer container, StateBasedGame game)
         throws SlickException {}   
   @Override
   public void render(GameContainer container, StateBasedGame game, Graphics g)
         throws SlickException {}
   @Override
   public void update(GameContainer container, StateBasedGame game, int delta)
         throws SlickException {}
}


The Main.java have a method called getTheme() where it loads the resource. Your path there is probably wrong.

The wiki on TWL suggest to override the createRootPane() method. That wiki article is wrong, because you can't do that. You can't do it because the return type of createRootPane() is void not RootPane. So don't pay attention to that. Also enterState() automatically calls createRootPane() if you don't override it.


Top
 Profile  
 
PostPosted: Tue Aug 14, 2012 6:00 pm 
Offline
Game Developer
User avatar

Joined: Wed Feb 17, 2010 12:24 am
Posts: 594
Thanks Flekken, your example is working.

Flekken wrote:
The wiki on TWL suggest to override the createRootPane() method. That wiki article is wrong, because you can't do that. You can't do it because the return type of createRootPane() is void not RootPane. So don't pay attention to that. Also enterState() automatically calls createRootPane() if you don't override it.


hrm, so how to set theme? I tried rootPane.setTheme(""); in enter(), but still tries to load theme "state1".


Top
 Profile  
 
PostPosted: Tue Aug 14, 2012 6:26 pm 
Offline

Joined: Thu Jan 19, 2012 9:13 pm
Posts: 14
Edit the BasicTWLGameState. There is a method createRootPane() which has the rootPane.setTheme() line you are searching for.

Mine is like this:
Code:
protected void createRootPane() {
        ActionMap actionMap = new ActionMap();
        actionMap.addMapping(this);

        rootPane = new RootPane(this);
        rootPane.setTheme("");
        rootPane.setActionMap(actionMap);
    }


You can also override this method in the class which extends BasicTWLGameState :
Code:
@Override
   protected void createRootPane() {
      ActionMap actionMap = new ActionMap();
        actionMap.addMapping(this);

        rootPane = new RootPane(this);
        rootPane.setTheme("");
        rootPane.setActionMap(actionMap);
   }


Top
 Profile  
 
PostPosted: Tue Aug 14, 2012 6:33 pm 
Offline
Game Developer
User avatar

Joined: Wed Feb 17, 2010 12:24 am
Posts: 594
Thanks, that is what I did; but don't understand your previous statement on it.

Here is a working project (including simple theme) if anyone else wants it, maybe upload it to wiki?
http://www.powerengine2a.com/twl/TWLHelloWorld.zip


Top
 Profile  
 
PostPosted: Tue Aug 14, 2012 6:53 pm 
Offline

Joined: Thu Jan 19, 2012 9:13 pm
Posts: 14
dime wrote:
Thanks, that is what I did; but don't understand your previous statement on it.

Here is a working project (including simple theme) if anyone else wants it, maybe upload it to wiki?
http://www.powerengine2a.com/twl/TWLHelloWorld.zip


Sorry it seems your TWLSlick.zip is different than mine somehow. Maybe That caused some incorrect statements on my end. I'm going to redownload from the TWL homepage.

So you still have the state1 theme problem or not?

Edit: My TWLSlick.zip is completly different than what is on the TWL site. So I guess what is said is wrong.


Top
 Profile  
 
PostPosted: Wed Aug 15, 2012 5:41 pm 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1171
The TWLSLick.zip on the wiki was an old version - the file linked by the TWL website was up to date. I updated the wiki attachment.

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group