I am currently trying to combine both slick2d and nifty to work together. I've had some amount of success - I can now actually run my test and it won't crash. As far as I can tell, it will also receive input properly.
First, I'm having doubts I'm doing it right. I extend NiftyStateBasedGame in my outer game and then I extend two NiftyOverlayGameState to create the two states. However, I am getting an error that doesn't crash my application saying:
WARNING: An event service by the name NiftyEventBusalready exists. Perhaps multiple threads tried to create a service about the same time?
org.bushe.swing.event.EventServiceExistsException: An event service by the name NiftyEventBusalready exists. Perhaps multiple threads tried to create a service about the same time?
at org.bushe.swing.event.EventServiceLocator.setEventService(EventServiceLocator.java:123)
I could ignore it, since the application is running well enough (I think?) but I'm concerened that once I figure out how to actually implement some parts of the GUI they might not work.
Which leads me to my next question. I was trying to create a simple button on screen and I could not for the life of me figure out how to go about it. I have my nifty instance... But what do I do with it? Any pointers in the right direction would be greatly appriciated!
My three classes:
The outer class, TheGame (This is the StateBasedGame)
public class TheGame extends NiftyStateBasedGame
{
Menu menu;
Nifty nifty;
GameOver go;
public TheGame(String name)
{
super(name);
menu = new Menu();
go = new GameOver();
}
@Override
public void initStatesList(GameContainer arg0) throws SlickException
{
this.addState(menu);
this.addState(go);
}
public static void main(String[] args) throws Exception
{
AppGameContainer game = new AppGameContainer(new TheGame("OMG, Super test!"));
game.setDisplayMode(800, 600, false);
game.setAlwaysRender(true);
game.start();
}
And the two dummy classes the are the states:
public class Menu extends NiftyOverlayGameState
{
@Override
protected void initGameAndGUI(GameContainer gc, StateBasedGame sbg) throws SlickException
{
initNifty(gc, sbg);
}
@Override
protected void renderGame(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
{
g.drawString("Does it work?", 100, 100);
}
@Override
protected void updateGame(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
{
Input input = gc.getInput();
if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON))
sbg.enterState(1);
}
public int getID()
{
return 0;
}
public class GameOver extends NiftyOverlayGameState
{
protected void initGameAndGUI(GameContainer gc, StateBasedGame sbg) throws SlickException
{
initNifty(gc, sbg);
}
@Override
protected void renderGame(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
{
g.drawString("WORKS, OOOH-YEA!", 100, 100);
}
@Override
protected void updateGame(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
{
Input input = gc.getInput();
if (input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON))
sbg.enterState(0);
}
public int getID()
{
return 1;
}
Again, if compiled, this gets me the two screens I want with the warning/exception I mentioned above. What am I doing wrong? And how would I start actually adding GUI elements to this?