Just getting properly started with game development in Java with Slick, after a few small games with C++ and working with Java a bit in college. It’s going fine so far but a few small things are confusing me…
- It seems a class deriving from BasicGameState has to implement certain methods such as:
public void render( GameContainer arg0, StateBasedGame arg1, Graphics arg2 )throws SlickException
That’s fine, but what if you have something like:
public class PlatformGame extends StateBasedGame
{
public static final int SCREEN_WIDTH = 320;
}
And you’d like to be able to access that variable in the game’s various states?
You have to implement render etc. with StateBasedGame, so then how might I access something particular to my derived class from within the methods of a class deriving from BasicGameState?
I know I COULD cast a StateBasedGame into a PlatformGame, but that seems pretty sloppy, then I dunno.
Maybe I’m missing something obvious here. It’s been a while since I coded properly at all in fact.
- The main reason I need to ask question 1 is because I want to make a game in fairly low resolution but then scale it up so that things are actually discernible on the screen.
Right now I’m messing around with 320x200 and scaling it up by 2.
What I want to know is: what is a proper way to do that? Is there a fairly standard way of setting that up in Slick? I have it working in a basic way now but I’m afraid that maybe it may require more careful attention than is necessary.
Right now my PlatformGame class (extending from StateBasedGame) has the following fields:
public static final int WIN_RES_WIDTH = 320;
public static final int WIN_RES_HEIGHT = 200;
public static final float WIN_RES_SCALE = 2.0f;
public static final int WIN_WIDTH = ( int ) ( WIN_RES_WIDTH * WIN_RES_SCALE );
public static final int WIN_HEIGHT = ( int ) ( WIN_RES_HEIGHT * WIN_RES_SCALE );
My main then has:
AppGameContainer app = new AppGameContainer( new PlatformGame() );
app.setDisplayMode( WIN_WIDTH, WIN_HEIGHT, false );
So that sets up a window that’s twice the resolution I want.
Then to actually draw something, I access the resolution scaling value and alter the coordinates appropriately such as in this code I’m using to draw map tiles:
draw( x / TILE_SIZE * TILE_SIZE * PlatformGame.WIN_RES_SCALE,
y / TILE_SIZE * TILE_SIZE * PlatformGame.WIN_RES_SCALE,
x / TILE_SIZE * TILE_SIZE * PlatformGame.WIN_RES_SCALE + TILE_SIZE * PlatformGame.WIN_RES_SCALE,
y / TILE_SIZE * TILE_SIZE * PlatformGame.WIN_RES_SCALE + TILE_SIZE * PlatformGame.WIN_RES_SCALE,
spriteSheetX * TILE_SIZE,
spriteSheetY * TILE_SIZE, spriteSheetX * TILE_SIZE + TILE_SIZE,
spriteSheetY * TILE_SIZE + TILE_SIZE );
The last 3 arguments are what part of the sprite sheet to draw and of course irrelevant here, but you can see the first 4 arguments are a bit of a convoluted mess.
That’s going to cause serious confusion and bugginess down the line if I leave it like that.
So, how should I scale my graphics up? There must be a relatively painless way that keeps the scaling code all in one or very few places. There must be a fairly standard solution I imagine.
- Also, I see you can draw images using the Image class’ own draw() method, but also there appears to be a list of identical functions in the Graphics class called drawImage(). What’s the difference between those? Are there situations where you’d need to use one way over the other? Does Image.draw() just use the methods of the AppGameContainer’s Graphics object implicitly?