SnowDemo.cs :  » Game » SDL » SdlDotNetExamples » SmallDemos » 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 » SmallDemos » SnowDemo.cs
#region LICENSE
/* This file is part of SnowDemo
 * SnowDemo.cs, (c) 2005 David Hudson
 * based on code by (c) 2003 Sijmen Mulder
 *
 * This program 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 program 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 program; 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 SdlDotNet.Core;
using SdlDotNet.Input;
using SdlDotNet.Graphics;
using SdlDotNet.Graphics.Sprites;

namespace SdlDotNetExamples.SmallDemos{
    /// <summary>
    /// 
    /// </summary>
    public class SnowDemo : IDisposable
    {
        static string[] textArray = {
                    "when the cold of winter comes",
                    "starless night", "will cover day",
                    "in the veiling of the sun", 
                    "we will walk",
                    "in bitter rain"
                  };
        SpriteCollection snowflakes = new SpriteCollection();
        SpriteCollection textItems = new SpriteCollection();
        Surface screen;
        Surface background;
        Surface tree;
        Surface treeStretch;
        string dataDirectory = "Data";
        string filePath = Path.Combine("..", "..");
        string fontName = "FreeSans.ttf";

        /// <summary>
        /// 
        /// </summary>
        public SnowDemo()
        {
        }

        void Initialize(int numberOfSnowflakes)
        {
            for (int i = 0; i < numberOfSnowflakes; i++)
            {
                snowflakes.Add(new Snowflake());
            }
            SdlDotNet.Graphics.Font font = new SdlDotNet.Graphics.Font(Path.Combine(filePath, Path.Combine(dataDirectory, fontName)), 24);

            textItems.Add(new TextItem(textArray[0], font, 25, 0));
            for (int i = 1; i < textArray.Length; i++)
            {
                textItems.Add(
                    new TextItem(textArray[i],
                    font, i * 50,
                    i * 2));
            }
            snowflakes.EnableTickEvent();
            textItems.EnableTickEvent();
        }

        [STAThread]
        public static void Run()
        {
            SnowDemo snowDemo = new SnowDemo();
            snowDemo.Go();
        }

        /// <summary>
        /// 
        /// </summary>
        public void Go()
        {
            if (File.Exists(Path.Combine(dataDirectory, "snowbackground.png")))
            {
                filePath = "";
            }
            Video.WindowIcon();
            Video.WindowCaption = "SDL.NET - Snow Demo";
            screen = Video.SetVideoMode(640, 480, 16);
            background = new Surface(Path.Combine(filePath, Path.Combine(dataDirectory, "snowbackground.png")));
            background.Transparent = true;
            background.TransparentColor = Color.Magenta;

            tree = new Surface(Path.Combine(filePath, Path.Combine(dataDirectory, "Tree.bmp")));
            tree.TransparentColor = Color.Magenta;
            tree.Transparent = true;
            treeStretch = new Surface(tree);
            treeStretch = treeStretch.CreateStretchedSurface(new Size(100, 100));
            treeStretch.TransparentColor = Color.Magenta;
            treeStretch.Transparent = true;
            Initialize(250);
            Events.KeyboardDown +=
                new EventHandler<KeyboardEventArgs>(this.KeyboardDown);
            Events.KeyboardDown +=
                new EventHandler<KeyboardEventArgs>(this.KeyboardDown);
            Events.Tick += new EventHandler<TickEventArgs>(this.Tick);
            Events.Quit += new EventHandler<QuitEventArgs>(this.Quit);
            Events.Run();
        }

        private void Tick(object sender, TickEventArgs args)
        {
            screen.Fill(Color.FromArgb(64, 175, 239));
            screen.Blit(snowflakes);
            screen.Blit(background, new Point(0, 280));
            screen.Blit(textItems);
            screen.Blit(tree, new Point(100, 300));
            screen.Blit(tree, new Point(130, 295));
            screen.Blit(tree, new Point(155, 302));
            screen.Blit(tree, new Point(230, 302));
            screen.Blit(treeStretch, new Point(180, 290));
            screen.Update();
        }

        private void KeyboardDown(object sender, KeyboardEventArgs e)
        {
            if (e.Key == Key.Escape ||
                e.Key == Key.Q)
            {
                Events.QuitApplication();
            }
        }

        private void Quit(object sender, QuitEventArgs e)
        {
            Events.QuitApplication();
        }

        /// <summary>
        /// Lesson Title
        /// </summary>
        public static string Title
        {
            get
            {
                return "SnowDemo: Alpha Blending";
            }
        }

        #region IDisposable Members

        private bool disposed;

        /// <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>
        ~SnowDemo()
        {
            Dispose(false);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    if (this.tree != null)
                    {
                        this.tree.Dispose();
                        this.tree = null;
                    }
                    if (this.treeStretch != null)
                    {
                        this.treeStretch.Dispose();
                        this.treeStretch = null;
                    }
                    if (this.background != null)
                    {
                        this.background.Dispose();
                        this.background = null;
                    }
                }
                this.disposed = true;
            }
        }
        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.