diff --git a/src/Backend.cs b/src/Backend.cs index 67b1330..b523ea0 100644 --- a/src/Backend.cs +++ b/src/Backend.cs @@ -17,6 +17,8 @@ namespace WorldOfPeacecraft private Buffer SenderBuffer; private IGui Gui; private int SelfId; + private AutoResetEvent InitializedEvent = new AutoResetEvent(false); + private bool initialized = false; public Backend (IGui gui) { @@ -30,9 +32,9 @@ namespace WorldOfPeacecraft Parse = new Parser (this, receiverBuffer); Rec = new Receiver (Client, receiverBuffer); Send = new Sender (Client, SenderBuffer); - SenderBuffer.AddLine ("get:map"); SenderBuffer.AddLine ("get:me"); SenderBuffer.AddLine ("get:ents"); + SenderBuffer.AddLine ("get:map"); } public void SetSelfId (int id) @@ -98,6 +100,7 @@ namespace WorldOfPeacecraft public void SetMap (Map map) { this.Map = map; + InitializedEvent.Set(); } public ITile[,] GetMap () @@ -166,12 +169,12 @@ namespace WorldOfPeacecraft } } - public void RefreshGui() + public void RefreshGui () { - Gui.PerformRefresh(); + Gui.PerformRefresh (); } - public void StartThreads() { + private void StartThreads() { Parse.Start (); Rec.Start (); Send.Start (); @@ -183,8 +186,11 @@ namespace WorldOfPeacecraft Client.Close (); } - public void Connect(){ + public void Initialize(){ Client.Connect ("localhost", 9999); + StartThreads (); + InitializedEvent.WaitOne(); + initialized = true; } } } diff --git a/src/Gui/Gui.cs b/src/Gui/Gui.cs index d9b94b9..69cde60 100644 --- a/src/Gui/Gui.cs +++ b/src/Gui/Gui.cs @@ -16,6 +16,7 @@ namespace WorldOfPeacecraft private MapPanel MapPanel; private ChatPanel ChatPanel; private OnlinePlayerList OnlinePlayerList; + private bool loaded = false; //private Music m = new Music(); @@ -44,7 +45,6 @@ namespace WorldOfPeacecraft { base.OnLoad (e); InitializeComponents (); - Backend.StartThreads (); } protected override void OnPaint (PaintEventArgs e) @@ -62,11 +62,7 @@ namespace WorldOfPeacecraft this.SuspendLayout(); this.Size = new Size(OnlinePlayerWidth + 400 + ChatWidth, 400); OnlinePlayerList.Location = new Point(0,0); - OnlinePlayerList.Size = new Size(OnlinePlayerWidth, 400); MapPanel.Location = new Point(OnlinePlayerWidth,0); - MapPanel.Size = new Size(400, 400); - ChatPanel.Location = new Point (OnlinePlayerWidth + 400, 0); - ChatPanel.Size = new Size (300, 400); this.DoubleBuffered = true; this.MaximizeBox = false; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; @@ -78,6 +74,8 @@ namespace WorldOfPeacecraft this.Controls.Add (ChatPanel); this.ResumeLayout(); + loaded = true; + this.PerformRefresh(); //m.Playmusic ("overworld"); } @@ -91,19 +89,19 @@ namespace WorldOfPeacecraft public void PerformRefresh () { - this.BeginInvoke(new MethodInvoker(delegate - { - MapPanel.PerformLayout (); - this.SuspendLayout(); - this.SetClientSizeCore(OnlinePlayerWidth + MapPanel.Width + ChatWidth, MapPanel.Height); - OnlinePlayerList.Size = new Size(OnlinePlayerWidth, MapPanel.Height); - MapPanel.Location = new Point(OnlinePlayerWidth, 0); - ChatPanel.Location = new Point (OnlinePlayerWidth + MapPanel.Width, 0); - ChatPanel.Size = new Size (ChatWidth, MapPanel.Height); - this.ResumeLayout(); - ChatPanel.UpdateData (); - this.Refresh(); - })); + if (loaded) { + this.BeginInvoke (new MethodInvoker (delegate { + MapPanel.PerformLayout (); + this.SuspendLayout(); + this.SetClientSizeCore(OnlinePlayerWidth + MapPanel.Width + ChatWidth, MapPanel.Height); + OnlinePlayerList.Size = new Size(OnlinePlayerWidth, MapPanel.Height); + ChatPanel.Location = new Point (OnlinePlayerWidth + MapPanel.Width, 0); + ChatPanel.Size = new Size (ChatWidth, MapPanel.Height); + this.ResumeLayout(); + ChatPanel.UpdateData (); + this.Refresh(); + })); + } } public void LoadResources(){ diff --git a/src/Gui/IBackend.cs b/src/Gui/IBackend.cs index 806f0c6..5097de4 100644 --- a/src/Gui/IBackend.cs +++ b/src/Gui/IBackend.cs @@ -22,10 +22,8 @@ namespace WorldOfPeacecraft void moveRight(); void MoveTo(int x, int y); - void StartThreads(); - void Stop(); - void Connect(); + void Initialize(); } } diff --git a/src/Gui/Splashscreen.cs b/src/Gui/Splashscreen.cs index c40e2b1..92ac941 100644 --- a/src/Gui/Splashscreen.cs +++ b/src/Gui/Splashscreen.cs @@ -37,9 +37,9 @@ namespace WorldOfPeacecraft } private void ThreadEntry(){ + Thread.Sleep(250); gui.LoadResources (); - backend.Connect (); - Thread.Sleep (1000); + backend.Initialize (); if (GuiHasLoaded != null) GuiHasLoaded (this,gui); }