/* * GameThread.cs - Controls the main loop which executes opcodes of a Level9 * game. * * Copyright (C) 2004 - 2011 Andreas Scherrer * * This is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this software; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, * USA. */ using System; using System.Threading; namespace Level9 { /// /// Controls the main loop which executes opcodes of a Level 9 game. /// public class GameThread { // -- Attributes ----------------------------------------------------- /// /// The current game thread instance. /// static Thread gameThread = null; /// /// This property indicates whether the game thread is running. /// static bool isRunning = false; /// /// This property indicates whether the game thread is sleeping. /// static bool isSleeping = false; /// /// This property indicates whether the game thread should be stopped. /// static bool stopRequest = false; // -- Methods -------------------------------------------------------- /// /// Starts a new game thread. /// public static void Start() { if (gameThread == null) { gameThread = new Thread(new ThreadStart(ThreadProc)); isRunning = true; isSleeping = false; stopRequest = false; gameThread.Start(); Logger.Log(typeof(GameThread), "Start"); } } /// /// Stops a game thread. /// public static void Stop() { if (gameThread != null) { Logger.Log(typeof(GameThread), "Stop: " + gameThread.ThreadState + " -> Stop"); stopRequest = true; Interpreter.StopGame(); if (isSleeping) { Resume(); } } } /// /// Interrupts a running game thread. /// public static void Sleep() { if (gameThread != null && !isSleeping) { Logger.Log(typeof(GameThread), "Sleep: " + gameThread.ThreadState + " -> Sleep"); try { isSleeping = true; Thread.Sleep(Timeout.Infinite); } catch (ThreadInterruptedException) { isSleeping = false; Logger.Log(typeof(GameThread), "Sleep: Interrupted"); } } } /// /// Resumes a sleeping game thread. /// public static void Resume() { if (gameThread != null && isSleeping) { Logger.Log(typeof(GameThread), "Resume: " + gameThread.ThreadState + " -> Resume"); gameThread.Interrupt(); } } /// /// Joins a game thread. /// public static void Join() { if (gameThread != null) { gameThread.Join(); } } /// /// The thread procedure. /// static void ThreadProc() { while(Interpreter.RunGame()); isRunning = false; gameThread = null; Interpreter.os_flush(); Logger.Log(typeof(GameThread), "ThreadProc: Exit"); } // -- Accessors ------------------------------------------------------ /// /// Gets a value that indicates whether the game thread should be /// stopped. /// public static bool StopRequest { get { return stopRequest; } } /// /// Gets a value that indicates whether the game thread is running. /// public static bool IsRunning { get { return isRunning; } } /// /// Gets a value that indicates whether the game thread is sleeping. /// public static bool IsSleeping { get { return isSleeping; } } } }