GameView.cs :  » Game » SDL » SdlDotNetExamples » SimpleGame » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Game » SDL 
SDL » SdlDotNetExamples » SimpleGame » GameView.cs
#region LICENSE
// Copyright 2005 David Hudson (jendave@yahoo.com)
// This file is part of SimpleGame.
//
// SimpleGame 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.
//
// SimpleGame 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 SimpleGame; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#endregion LICENSE

using System;
using System.IO;
using System.Drawing;
using System.Collections;

using SdlDotNet.Graphics;
using SdlDotNet.Input;
using SdlDotNet.Audio;
using SdlDotNet.Core;

namespace SdlDotNetExamples.SimpleGame{
    /// <summary>
    /// Derived class
    /// </summary>
    public class GameView : IDisposable
    {
        Map map;
        int height;
        int width;
        Surface surf;
        //Names names;
        EntitySprite entitySprite;
        Rectangle[] mapRectangles;
        ArrayList backSprites;
        ArrayList frontSprites;
        Sound sound;
        string dataDirectory = "Data";
        string filePath = Path.Combine("..", "..");
        /// <summary>
        /// constructor
        /// </summary>
        public GameView(EventManager eventManager)
        {
            if (eventManager == null)
            {
                throw new ArgumentNullException("eventManager");
            }
            eventManager.OnMapBuiltEvent += new EventHandler<MapBuiltEventArgs>(Subscribe);
            eventManager.OnEntityPlaceEvent += new EventHandler<EntityPlaceEventArgs>(Subscribe);
            eventManager.OnEntityMoveEvent += new EventHandler<EntityMoveEventArgs>(Subscribe);
            Events.Tick += new EventHandler<TickEventArgs>(this.Tick);
            Events.Quit += new EventHandler<QuitEventArgs>(this.Quit);
            this.width = 424;
            this.height = 440;
            this.backSprites = new ArrayList();
            this.frontSprites = new ArrayList();
            if (File.Exists(Path.Combine(dataDirectory, "boing.wav")))
            {
                filePath = "";
            }
            try
            {
                this.sound = new Sound(Path.Combine(filePath, Path.Combine(dataDirectory, "boing.wav")));
            }
            catch (SdlException)
            {
                // Linux audio problem
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void ShowMap(Map map)
        {
            mapRectangles = new Rectangle[9];
            int x = 10;
            int y = 10;
            int width = 128;
            int height = 128;
            int i = 0;

            if (map == null)
            {
                throw new ArgumentNullException("map");
            }
            foreach (Sector sec in map.GetSectors())
            {
                if (i < 3)
                {
                    mapRectangles[i] = new Rectangle(x, y, width, height);
                    //LogFile.WriteLine(mapRectangles[i].ToString());
                    x += 138;
                }
                else if (i >= 3 && i < 6)
                {
                    if (i == 3)
                    {
                        x = 10;
                    }
                    y = 148;

                    mapRectangles[i] = new Rectangle(x, y, width, height);
                    //LogFile.WriteLine(mapRectangles[i].ToString());
                    x += 138;
                }
                else if (i >= 6)
                {
                    if (i == 6)
                    {
                        x = 10;
                    }
                    y = 286;

                    mapRectangles[i] = new Rectangle(x, y, width, height);
                    //LogFile.WriteLine(mapRectangles[i].ToString());
                    x += 138;
                }
                backSprites.Add(new SectorSprite(Video.Screen, sec, mapRectangles[i]));
                i++;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void CreateView()
        {
            Video.WindowIcon();
            Video.WindowCaption = Names.WindowCaption;
            Video.SetVideoMode(this.width, this.height);
            this.surf = Video.Screen.CreateCompatibleSurface(width, height);
            //fill the surface with black
            this.surf.Fill(new Rectangle(new Point(0, 0), surf.Size), Color.Black);
            Mouse.ShowCursor = false;
        }

        /// <summary>
        /// 
        /// </summary>
        public void UpdateView()
        {
            this.surf.Fill(new Rectangle(new Point(0, 0), surf.Size), Color.Black);
            foreach (SectorSprite i in this.backSprites)
            {
                Video.Screen.Blit(i.Surface, i.Rectangle);
            }
            foreach (EntitySprite j in this.frontSprites)
            {
                Video.Screen.Blit(j.Surface, j.Rectangle);
            }
            Video.Screen.Update();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        public void ShowEntity(Entity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            this.entitySprite = new EntitySprite(surf);
            this.frontSprites.Add(this.entitySprite);
            SectorSprite sectSprite = this.GetSectorSprite(entity.Sector);
            this.entitySprite.Center = sectSprite.Center;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        public void MoveEntity(Entity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            SectorSprite sectSprite = this.GetSectorSprite(entity.Sector);
            this.entitySprite.Center = sectSprite.Center;
            if (this.sound != null)
            {
                this.sound.Play();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="eventManager"></param>
        /// <param name="e"></param>
        void Subscribe(object eventManager, MapBuiltEventArgs e)
        {
            //LogFile.WriteLine("GameView received a MapBuilt event");
            this.map = e.Map;
            ShowMap(this.map);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="eventManager"></param>
        /// <param name="e"></param>
        void Subscribe(object eventManager, EntityPlaceEventArgs e)
        {
            //LogFile.WriteLine("GameView received a EntityPlace event");
            ShowEntity(e.Entity);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="eventManager"></param>
        /// <param name="e"></param>
        void Subscribe(object eventManager, EntityMoveEventArgs e)
        {
            //LogFile.WriteLine("GameView received a EntityMove event");
            MoveEntity(e.Entity);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sector"></param>
        /// <returns></returns>
        public SectorSprite GetSectorSprite(Sector sector)
        {
            SectorSprite sectSprite = null;
            foreach (SectorSprite s in this.backSprites)
            {
                if (s.Sector == sector)
                {
                    sectSprite = s;
                }
            }
            return sectSprite;
        }

        private void Tick(object sender, TickEventArgs e)
        {
            //LogFile.WriteLine("GameView received a Tick event");
            UpdateView();
        }
        #region IDisposable Members

        private bool disposed;

        /// <summary>
        /// Destroy sprite
        /// </summary>
        /// <param name="disposing">If true, remove all unamanged resources</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    if (this.entitySprite != null)
                    {
                        this.entitySprite.Dispose();
                        this.entitySprite = null;
                    }
                    if (this.sound != null)
                    {
                        this.sound.Dispose();
                        this.sound = null;
                    }
                }
                this.disposed = true;
            }
        }
        /// <summary>
        /// Destroy object
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Destroy object
        /// </summary>
        public void Close()
        {
            Dispose();
        }

        /// <summary>
        /// Destroy object
        /// </summary>
        ~GameView()
        {
            Dispose(false);
        }

        #endregion

        private void Quit(object sender, QuitEventArgs e)
        {
            Events.QuitApplication();
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.