Rellac
Rellac

Reputation: 63

What is wrong with my Game Thread?

I have been trying for a while to implement a Game Thread to utilise a loop to implement logic. I posted a question here not long ago, I hope no one minds the follow up.

I have managed to scrape together this code from my research:

public class GameView extends SurfaceView implements SurfaceHolder.Callback 
{
    class GameThread extends Thread 
    {
        //states
        public static final int STATE_LOSE = 1;

        public static final int STATE_PAUSE = 2;

        public static final int STATE_READY = 3;

        public static final int STATE_RUNNING = 4;

        private Paint m_paint;

        //canvas dimensions
        private int m_canvasWidth;

        private int m_canvasHeight;

        private long m_lastTime;

        private boolean m_run = false;

        private int m_mode;

        public ImageView ship;
        RelativeLayout.LayoutParams shipParams;

        // Handle to the surface manager
        private SurfaceHolder m_surfaceHolder;

        public GameThread(SurfaceHolder surfaceHolder, Context context, Handler handler)
        {
            m_surfaceHolder = surfaceHolder;
        }

        //Initialise the game
        public void doStart() 
        {

            synchronized (m_surfaceHolder)
            {
                resetGame();
                m_lastTime = System.currentTimeMillis() + 100;
                setState(STATE_RUNNING);
                ship = (ImageView) findViewById(R.id.imageView1);
                shipParams = (RelativeLayout.LayoutParams)ship.getLayoutParams();
            }
        }

        public void pause() 
        {
            synchronized (m_surfaceHolder) 
            {
                if (m_mode == STATE_RUNNING)
                setState(STATE_PAUSE);
            }
        }

        @Override
        public void run() 
        {
            while (m_run) 
            {
                Canvas c = null;
                try 
                {
                c = m_surfaceHolder.lockCanvas(null);
                synchronized (m_surfaceHolder) 
                    {
                        if (m_mode == STATE_RUNNING)
                        {
                            updateGame();
                        }
                        doDraw(c);
                    }
                }
                catch(Exception e){}
                finally 
                {

                    if (c != null) 
                    {
                        m_surfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }

        public void setRunning(boolean b) 
        {
            m_run = b;
        }


        public void setState(int mode) 
        {
            synchronized (m_surfaceHolder) 
            {
                setState(mode, null);
            }
        }


        public void setState(int mode, CharSequence message) 
        {
            synchronized (m_surfaceHolder) 
            {
                m_mode = mode;
            }
        }

        public void setPlayers(boolean onePlayer)
        {

        }


        public void setSurfaceSize(int width, int height) 
        {
            synchronized (m_surfaceHolder) 
            {
                m_canvasWidth = width;
                m_canvasHeight = height;
            }
        }


        public void unpause() 
        {
            synchronized (m_surfaceHolder) 
            {
                m_lastTime = System.currentTimeMillis() + 100;
            }
            setState(STATE_RUNNING);
        }


        private void doDraw(Canvas canvas)
        {
            canvas.drawARGB(255, 0, 0, 0);
        }

        private void updateGame() 
        {
            long now = System.currentTimeMillis();
            if (m_lastTime > now)
                return;
                double elapsed = (now - m_lastTime) / 1000.0;
                m_lastTime = now;
            System.out.print("HELLO WORLD");
            shipParams.topMargin++;
            ship.setLayoutParams(shipParams);
        }

        private boolean collided(Rect rectangle)
        {
            return false;
        }

        public boolean foundWinner()
        {
            return false;
        }

        public void resetGame()
        {

        }

        public void handleInput(MotionEvent event)
        {

        }
    }

    private Context m_context;

    private GameThread m_thread;

    private Handler m_handler;

    public GameView(Context context, AttributeSet attrs) 
    {
        super(context, attrs);


        SurfaceHolder holder = getHolder();
        holder.addCallback(this);

        m_handler = new Handler() {
        @Override
        public void handleMessage(Message m) {

        Bundle b = m.getData();
        MotionEvent e = b.getParcelable("event");
        m_thread.handleInput(e);
        }
        };

        m_thread = new GameThread(holder, context, m_handler);
        setFocusable(true); 
    };


    public GameThread getThread() 
    {
        return m_thread;
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus)
    {
        if (!hasWindowFocus)
        m_thread.pause();
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
    {
        m_thread.setSurfaceSize(width, height);
    }


    public void surfaceCreated(SurfaceHolder holder)
    {
        if(m_thread.getState() == State.TERMINATED)
        {
            m_thread = new GameThread(getHolder(), m_context, m_handler);
            m_thread.setRunning(true);
            m_thread.start();
            m_thread.doStart();
        }
        else
        {
            m_thread.setRunning(true);
            m_thread.start();
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder)
    {
        boolean retry = true;
        m_thread.setRunning(false);
        while (retry) 
        {
            try 
            {
                m_thread.join();
                retry = false;
            } 
            catch (InterruptedException e) 
            {
            }
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) 
    {
        return true;
    }
}

I am fairly certain that my issue lies here and it is merely a logical one. Everything does seem fine to me, however and I am in need of assistance.

I have attempted to draw an image at line 47 and defined a movement to take place in the update method at line 153. I also have placed a print line for extra debug, but the line doesn't show.

I am stumped.

Any help would be great, thanks.

Here are my other codes, if neccessary:

edit: I should note that I'm not getting any kind of errors within the code, it merely doesn't respond

Upvotes: 0

Views: 103

Answers (2)

Mariano Schmands
Mariano Schmands

Reputation: 806

set m_run to true in your doStart() procedure

Upvotes: 1

Ricky
Ricky

Reputation: 258

You are initializing m_run as false,then in the while cycle in the run() method you must have set to true. Change it to true and the thread will work normally.

Upvotes: 1

Related Questions