2
\$\begingroup\$

i do my first game and already read alot about design strategie, programming style, design .. and so on theoretically it works perfect but in practice i dont know if i do thinks "right"

i have three classes my main which starts the view in this i do all my stuff -currently-

it holds an state variable, it has an onDraw method .. and so on

the third class is the game-thread while my game is running, in here are decisions -depending on my current state- what should be done

@Override
public void run() {
    Canvas canvas = null;
    mStartTime = System.currentTimeMillis();
    while (mRun) {
        canvas = mHolder.lockCanvas();
        if (canvas != null) {

            // State Updating State means to manage state transitions, such as a game over, character select or next level.

            switch( mPanel.getModeState() ){
                case Surface.MODE_LOGO_SPLASH :
                break;

                case Surface.MODE_MENU : 
                    // the menu

                break;
                case Surface.MODE_LOADING : 
                    // loading animation

                break;
                case Surface.MODE_GAME_PLAYING : 
                    // game stuff
                                            // GAME START _ RUNNING _ END

                    // Input
                    // AI
                    // Physics

                    // animation
                    //mPanel.animate(mElapsed);
                break;
            }

            // draw everyThing
            mPanel.doDraw(mElapsed, canvas);
            // sound
            // Sound and Video.             
            mElapsed = System.currentTimeMillis() - mStartTime;
            mHolder.unlockCanvasAndPost(canvas);
        }
        mStartTime = System.currentTimeMillis();
    }
}

my problem is .. the function onDraw draws everything .. including the menu ? this sounds for me like bad code because everytime doDraw is executet, i have to check in this function which state i am currently in .. this cost time, dosent it ?

public void doDraw(long elapsed, Canvas canvas) {
    canvas.drawColor(Color.BLACK);

        // currently just gameLayout
        canvas.drawBitmap(background, 0, 0, mPaint);
    gameLayout.draw(canvas);

}

would it not be better do implement an other thread which draws the menu ? but if so, how can i overlay the menu afer pause the game ?

:\ thanks for help erwin

\$\endgroup\$
2
  • \$\begingroup\$ You should do all the drawing in the same place, as you correctly are doing, for the reasons you correctly guessed. Maybe I misunderstood something you are trying to ask and missed the point. \$\endgroup\$
    – o0'.
    Commented Feb 28, 2012 at 10:42
  • \$\begingroup\$ ok that helped, thanks, my problem is just, the "render class" do the init stuff, the drawing it seems for me, that its a little bit overload, so i though, is better to source the menu-drawing out \$\endgroup\$
    – erwin
    Commented Feb 28, 2012 at 11:20

1 Answer 1

2
\$\begingroup\$

You have here the rudiments of a normal game loop.

You have your "update" phase.

You have your "draw" phase.

This is the heartbeat of pretty much every game ever.

However.

You haven't QUITE gone far enough yet with abstracting the game state.

Here's what I mean.

Make yourself a GameState interface.

The methods supported are update, draw, along with whatever parameters you find to be necessary for each.

Additionally, there is a method called next, which returns the next game state to run.

Important: each game state updates itself. each game state draws itself.

The game states themselves should be represented in an enum, but could be ints or strings or whatever you please.

Next, make yourself a class for each of your game states that implements the GameState interface.

Now make a collection (prolly a HashMap), populated with the game state and objects representing the game states.

Now your loop looks like:

Map<YourEnumType,GameState> stateMachine = new HashMap<YourEnumType,GameState>();
//populate stateMachine here
YourEnumType theGameState = YourEnumType.INITIAL;
while(theGameState!=YourEnumType.FINAL){
    //whatever you do at the start of each loop
    GameState theGameStateObject = stateMachine.get(theGameState);
    theGameStateObject.update();
    theGameStateObject.draw();
    theGameState = theGameStateObject.next();
    //whatever your do at the end of each loop
}

For your states, you will need to have (accoring to your post as well as the seemingly obvious states to include:

INITIAL

LOADING

PLAYING

PAUSED

FINAL

Then, when in PLAYING, if the user does whatever is needed to pause the game, you set a field needed to return the value PAUSED from the call to next.

In the PAUSED state, you draw what is normally drawn in PLAYING, but on top of it you draw the ui you are interested in drawing.

There's a little more to it, for example having some "enter" and "leave" methods for the GameState objects, but the idea should be effectively visible.

Hope this helps.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .