Click here to Skip to main content
15,881,248 members
Articles / Game Development

Journey of Windows 8 Game (SkyWar) for Intel App Up Using MonoGame Framework

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
8 Jan 2013CPOL15 min read 23.1K   226   12  
Journey of Windows 8 game SkyWar for Intel App Up competition
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SkyWar
{
    public class Background
    {
        Texture2D t2dBackground, t2dParallax;
        GameData _gameData = null;
        int iViewportWidth = 800;
        int iViewportHeight = 600;

        int iBackgroundWidth = 1600;
        int iBackgroundHeight = 1080;

        int iParallaxWidth = 1600;
        int iParallaxHeight = 1080;

        int iBackgroundOffset;
        int iParallaxOffset;
        public int BackgroundOffset
        {
            get { return iBackgroundOffset; }
            set
            {
                iBackgroundOffset = value;
                if (iBackgroundOffset < 0)
                {
                    iBackgroundOffset += iBackgroundHeight;
                }
                if (iBackgroundOffset > iBackgroundHeight)
                {
                    iBackgroundOffset -= iBackgroundHeight;
                }
            }
        }

        public int ParallaxOffset
        {
            get { return iParallaxOffset; }
            set
            {
                iParallaxOffset = value;
                if (iParallaxOffset < 0)
                {
                    iParallaxOffset += iParallaxHeight;
                }
                if (iParallaxOffset > iParallaxHeight)
                {
                    iParallaxOffset -= iParallaxHeight;
                }
            }
        }

        bool drawParallax = true;

        public bool DrawParallax
        {
            get { return drawParallax; }
            set { drawParallax = value; }
        }

        public Background(ContentManager content, string sBackground, string sParallax)
        {
            _gameData = GameData.GetGameDataInstance();
            iViewportHeight = _gameData.GameScreenHeight;
            iViewportWidth = _gameData.GameScreenWidth;
            t2dBackground = content.Load<Texture2D>(sBackground);
            _gameData.BackgroundWidth = iBackgroundWidth = t2dBackground.Width;
            _gameData.BackgroundHeight = iBackgroundHeight = t2dBackground.Height;
            t2dParallax = content.Load<Texture2D>(sParallax);
            iParallaxWidth = t2dParallax.Width;
            iParallaxHeight = t2dParallax.Height;
        }
        public Background(ContentManager content, string sBackground)
        {
            _gameData = GameData.GetGameDataInstance();
            iViewportHeight = _gameData.GameScreenHeight;
            iViewportWidth = _gameData.GameScreenWidth;
            t2dBackground = content.Load<Texture2D>(sBackground);
            _gameData.BackgroundWidth = iBackgroundWidth = t2dBackground.Width;
            _gameData.BackgroundHeight = iBackgroundHeight = t2dBackground.Height;
            t2dParallax = t2dBackground;
            iParallaxWidth = t2dParallax.Width;
            iParallaxHeight = t2dParallax.Height;
            drawParallax = false;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            // Draw the background panel, offset by the player's location
            spriteBatch.Draw(t2dBackground, new Rectangle(0, -1 * iBackgroundOffset, iViewportWidth, iBackgroundHeight), Color.White);

            // If the right edge of the background panel will end 
            // within the bounds of the display, draw a second copy 
            // of the background at that location.
            if (iBackgroundOffset > iBackgroundHeight - iViewportHeight)
            {
                spriteBatch.Draw(t2dBackground, new Rectangle(0, (-1 * iBackgroundOffset) + iBackgroundHeight, iViewportWidth, iBackgroundHeight), Color.White);
            }

            if (drawParallax)
            {
                // Draw the parallax star field
                spriteBatch.Draw(t2dParallax, new Rectangle(0, -1 * iParallaxOffset, iViewportWidth, iParallaxHeight), Color.SlateGray);
                // if the player is past the point where the star 
                // field will end on the active screen we need 
                // to draw a second copy of it to cover the 
                // remaining screen area.
                if (iParallaxOffset > iParallaxHeight - iViewportHeight)
                {
                    spriteBatch.Draw(t2dParallax, new Rectangle(0, (-1 * iParallaxOffset) + iParallaxHeight, iViewportWidth, iParallaxHeight), Color.SlateGray);
                }
            }
        }
    }
}

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 eMids Technologies Pvt Ltd
India India
2+ year of experience in Information Technology with the extensive exposure of Treasury & Capital, Health Care and ERP domain.

Area of interest - Multithreading, Files, WPF, WCF, Jquery, Mvc, Sql Server, Perceptual programming

Heavily Worked on desktop application ,Web application and WCF Services.

Also worked on small games application for learning purpose.

Latest interest - Windows 8 , Perceptual Programming, Windows Phone, WCF etc

Comments and Discussions