Click here to Skip to main content
15,896,912 members
Articles / Game Development

XNA Alpha Textures

Rate me:
Please Sign up or sign in to vote.
4.71/5 (5 votes)
5 Jan 2011CPOL2 min read 31.3K   1.1K   16  
This shows how to create and combine transparent sprites in C# XNA
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Graphics;
using LibBadTactics;

namespace MapEditor {
    public partial class frmMain : Form {
        public frmMain() {
            InitializeComponent();
            //texture1 = Texture2D.FromFile(scoper2DRenderer1.GraphicsDevice, "Terrain/rock.bmp");
            ////texture2 = Texture2D.FromFile(scoper2DRenderer1.GraphicsDevice, "Terrain/sand.bmp");
            //texture2 = Texture2D.FromFile(scoper2DRenderer1.GraphicsDevice, "Terrain/snow.bmp");

            //texture3 = TextureBuilder.FromFileWithAlpha(scoper2DRenderer1.GraphicsDevice, "Terrain/rock.bmp", "Terrain/test.bmp");
            ////incercam sa modificam alpha la texturi
            ////Texture2D tex = new Texture2D(scoper2DRenderer1.GraphicsDevice, 512, 512,64,TextureUsage.Linear,SurfaceFormat.Rgba32);
            map = new Scoper2DMap(this.scoperMain, "Terrain/test.bmp", 6120, 6120);
            mapPreview = new Scoper2DMap(this.scoperPreview, "Terrain/test.bmp", 6120, 6120);
        }
        Scoper2DMap map;
        Scoper2DMap mapPreview;
        int x;
        int y;
        private void scoper2DRenderer1_Draw(int TickCount) {
            scoperMain.GraphicsDevice.Clear(Microsoft.Xna.Framework.Graphics.Color.White);
            map.Render(x, y);
            x += dx;
            y += dy;
            if (x < 0) x = 0;
            if (y < 0) y = 0;
            if (x > 4500) x = 4500;
            if (y > 4500) y = 4500;
        }
        int dx;
        int dy;
        private void Form1_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyCode == Keys.Up) {
                dy = -10;
            }
            if (e.KeyCode == Keys.Down) {
                dy = 10;
            }
            if (e.KeyCode == Keys.Left) {
                dx = -10;
            }
            if (e.KeyCode == Keys.Right) {
                dx = 10;
            }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e) {
            if (e.KeyCode == Keys.Up) {
                dy = -0;
            }
            if (e.KeyCode == Keys.Down) {
                dy = 0;
            }
            if (e.KeyCode == Keys.Left) {
                dx = 0;
            }
            if (e.KeyCode == Keys.Right) {
                dx = 0;
            }

        }

        private void Form1_MouseDown(object sender, MouseEventArgs e) {
            if (e.Button == MouseButtons.Left) {
                if (e.X < this.scoperMain.Width / 4) {
                    dx = -25;
                } else if (e.X > this.scoperMain.Width * 3 / 4) {
                    dx = 25;
                } else {
                    dx = 0;
                }
                if (e.Y < this.Height / 4) {
                    dy = -25;
                } else if (e.Y > this.scoperMain.Height * 3 / 4) {
                    dy = 25;
                } else {
                    dy = 0;
                }
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e) {
            dx = 0; dy = 0;
        }

        private void scoper2DRenderer1_MouseMove(object sender, MouseEventArgs e) {
            this.Form1_MouseDown(sender, e);
        }

        private void scoperPreview_Draw(int TickCount) {
            scoperPreview.GraphicsDevice.Clear(Microsoft.Xna.Framework.Graphics.Color.White);
            mapPreview.RenderAll();
        }

    }
}

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
Research Institute For Artificial Intelligence, Ro
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions