Click here to Skip to main content
15,886,077 members
Articles / Game Development

Learning XNA 2D Engine IceCream With 1945 Demo Project

Rate me:
Please Sign up or sign in to vote.
5.00/5 (13 votes)
8 Aug 2012CPOL16 min read 66.5K   2.3K   51  
IceCream1945 is a demonstration of XNA and the IceCream 2D library in a 2D top-down scrolling shooter similar to 1942 for the NES.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using IceCream;
using IceCream.Drawing;
using Milkshake.GraphicsDeviceControls;
using Rectangle = Microsoft.Xna.Framework.Rectangle;
using Point = Microsoft.Xna.Framework.Point;
using IceCream.SceneItems;

namespace Milkshake.Editors
{
    public class ZoomBox
    {
        #region Fields

        private ToolStripButton _toolStripButtonZoomIn;
        private ToolStripButton _toolStripButtonZoomOut;
        private ToolStripButton _toolStripButtonZoomNormal;

        private Camera _camera = new Camera();
        internal Camera Camera
        {
            get { return _camera; }
            set { _camera = value; }
        }
        private float _zoomDelta = 0.06f;
        private float _zoomFactor = 1f;
        internal float ZoomFactor
        {
            get { return _zoomFactor; }
            set
            {
                if (value < 0.05f)
                {
                    _zoomFactor = 0.05f;
                    if (_toolStripButtonZoomOut != null)
                    {
                        _toolStripButtonZoomOut.Enabled = false;
                    }
                }
                else
                {
                    _zoomFactor = value;
                    if (_toolStripButtonZoomOut != null)
                    {
                        _toolStripButtonZoomOut.Enabled = true;
                    }
                }
                if (_zoomFactor == 1.0f)
                {
                    _toolStripButtonZoomNormal.Enabled = false;
                }
                else
                {
                    _toolStripButtonZoomNormal.Enabled = true;
                }
                _camera.Zoom = new Vector2(_zoomFactor);
                _camera.Update(1 / 60f);
            }
        }        

        #endregion

        #region Constructor

        public ZoomBox()
        {
            _camera = new Camera();            
            _camera.Pivot = Vector2.Zero;
        }

        #endregion

        #region Methods

        public void ZoomIn()
        {
            ZoomFactor += _zoomDelta;
        }

        public void ZoomOut()
        {
            ZoomFactor -= _zoomDelta;
        }

        public void ZoomNormal()
        {
            ZoomFactor = 1f;
        }

        public void SetToolStripButtomZoomIn(ToolStripButton toolStripButton)
        {
            _toolStripButtonZoomIn = toolStripButton;
            _toolStripButtonZoomIn.Click += buttonZoomIn_Click;
        }

        public void SetToolStripButtomZoomOut(ToolStripButton toolStripButton)
        {
            _toolStripButtonZoomOut = toolStripButton;
            _toolStripButtonZoomOut.Click += buttonZoomOut_Click;
        }

        public void SetToolStripButtomZoomNormal(ToolStripButton toolStripButton)
        {
            _toolStripButtonZoomNormal = toolStripButton;
            _toolStripButtonZoomNormal.Click += buttonZoomNormal_Click;
            _toolStripButtonZoomNormal.Enabled = false;
        }

        #endregion

        #region Events

        private void buttonZoomIn_Click(object sender, EventArgs e)
        {
            ZoomIn();
        }

        private void buttonZoomNormal_Click(object sender, EventArgs e)
        {
            ZoomNormal();
        }

        private void buttonZoomOut_Click(object sender, EventArgs e)
        {
            ZoomOut();
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions