//来至智机网的coolypf
extern alias Bejeweled;
using System;
using System.Windows;
using System.Threading;
using System.Collections.Generic;
using Microsoft.Phone.Shell;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.GamerServices;
namespace GameCenter
{
public class Loader : Application
{
Game game;
bool firstLaunch = false, firstActivation = false;
bool chosen = false;
bool skip = false;
Timer timer, clock;
bool paused = true;
long clk = 0;
void startgame()
{
if (game == null)
return;
try
{
game.Run();
}
catch (InvalidOperationException ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
paused = false;
clock = new Timer(new TimerCallback(clock_tick));
clock.Change(0, 1);
timer = new Timer(new TimerCallback(tick));
timer.Change(0, 0);
}
void clock_tick(object sender)
{
clk++;
}
void tick(object sender)
{
if (game.IsFixedTimeStep)
timer.Change(-1, -1);
else
throw new NotImplementedException();
try
{
long c1 = clk, c2;
game.RunOneFrame();
c2 = clk - c1;
if (!paused)
{
int tw;
if (c2 >= game.TargetElapsedTime.TotalMilliseconds) tw = 0;
else tw = (int)Math.Ceiling(game.TargetElapsedTime.TotalMilliseconds - c2);
timer.Change(tw, 0);
}
}
finally
{
}
}
public Loader()
{
base.Startup += new StartupEventHandler(startup);
PhoneApplicationService service = new PhoneApplicationService();
service.Launching += new EventHandler<LaunchingEventArgs>(launching);
service.Closing += new EventHandler<Microsoft.Phone.Shell.ClosingEventArgs>(closing);
service.Activated += new EventHandler<ActivatedEventArgs>(activated);
service.Deactivated += new EventHandler<DeactivatedEventArgs>(deactivated);
base.Exit += new EventHandler(exit);
base.ApplicationLifetimeObjects.Add(service);
base.UnhandledException += new EventHandler<ApplicationUnhandledExceptionEventArgs>(except);
}
void startup(object sender, StartupEventArgs e)
{
List<string> games = new List<string>();
List<string> titles = new List<string>();
titles.Add("进入游戏");
titles.Add("换一个");
games.Add("愤怒的小鸟");
games.Add("水果忍者");
games.Add("钻石迷情");
games.Add("手榴弹大师");
games.Add("极品飞车");
int index = 0;
while (!chosen)
{
Guide.BeginShowMessageBox("游戏中心", games[index], titles, 0,
MessageBoxIcon.None, new AsyncCallback(mb_callback), null);
while (Guide.IsVisible)
Thread.Sleep(0x20);
if (chosen)
{
switch (index)
{
case 0:
game = new AngryBirds.GameMain();
break;
case 1:
game = new Mortar.TheGame();
game.TargetElapsedTime = TimeSpan.FromSeconds(0.025);
break;
case 2:
game = new Bejeweled::Sexy.Main();
break;
case 3:
game = new FraggerWP7.FraggerGame();
break;
case 4:
game = new nfshp_wp7.NFSHP();
game.IsFixedTimeStep = true;
game.TargetElapsedTime = TimeSpan.FromSeconds(0.025);
break;
default:
break;
}
if (game != null)
break;
}
else if (!skip)
break;
if (++index >= games.Count)
index = 0;
}
}
void launching(object sender, LaunchingEventArgs e)
{
startgame();
firstLaunch = true;
}
void closing(object sender, Microsoft.Phone.Shell.ClosingEventArgs e)
{
if (game != null)
game.Exit();
}
void activated(object sender, ActivatedEventArgs e)
{
if (!firstLaunch && !firstActivation)
startgame();
firstActivation = true;
paused = false;
if (timer != null) timer.Change(0, 0);
}
void deactivated(object sender, DeactivatedEventArgs e)
{
paused = true;
if (timer != null) timer.Change(-1, -1);
}
void exit(object sender, EventArgs e)
{
if (game != null)
game.Exit();
}
void except(object sender, ApplicationUnhandledExceptionEventArgs e)
{
System.Diagnostics.Debug.WriteLine(e.ExceptionObject.Message);
}
void mb_callback(IAsyncResult result)
{
int? c = Guide.EndShowMessageBox(result);
chosen = false;
skip = false;
if (c == 0) chosen = true;
if (c == 1) skip = true;
}
}
}