SteelGolem icon

nes-style palettes

SteelGolem | PRO | 02/02/20 03:47:41 PM UTC | 0 ⭐ | 507 👁️ | Never ⏰ | []
C# |

8.31 KB

|

None

|

0 👍

/

0 👎

 
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
 
namespace WindowsGame1
{
    static class Program
    {
        static void Main(string[] args)
        {
            Game1 game = new Game1();
            game.Run();
        }
    }
 
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
 
        Texture2D spriteSheet;
        Texture2D[] spriteSheetParts;
        Texture2D tileSheet;
        Texture2D[] tileSheetParts;
        Dictionary<string, Rectangle> srcRects;
        Dictionary<string, SpriteEffects> srcRectsFlip;
        Dictionary<string, Color[]> palettes;
 
        string mapPal;
 
        Vector2 playerPos;
        string playerDir;
        string playerSrcRect;
        int playerCounter;
        string playerPal;
 
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 720;
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
            Window.Title = "zelda1 tilemap palette testing";
        }
 
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
 
            spriteSheet = Content.Load<Texture2D>("sprite_sheet");
            spriteSheetParts = BuildSheetParts(spriteSheet);
 
            tileSheet = Content.Load<Texture2D>("tile_sheet");
            tileSheetParts = BuildSheetParts(tileSheet);
 
            palettes = new Dictionary<string, Color[]>();
            AddPalette("overworld_green", new UInt32[] { 0x000000, 0x00A800, 0xFCD8A8, 0x2038EC });
            AddPalette("overworld_brown", new UInt32[] { 0x000000, 0xC84C0C, 0xFCD8A8, 0x2038EC });
            AddPalette("overworld_gray", new UInt32[] { 0x000000, 0xFFFFFF, 0x747474, 0x2038EC });
            AddPalette("cave", new UInt32[] { 0x000000, 0x7C0800, 0x000000, 0xC84C0C });
            AddPalette("green_link", new UInt32[] { 0x000000, 0x80D010, 0xFC9838, 0xC84C0C });
            AddPalette("blue_link", new UInt32[] { 0x000000, 0xC4D4FC, 0xFC9838, 0xC84C0C });
            AddPalette("red_link", new UInt32[] { 0x000000, 0xC4D4FC, 0xFC9838, 0xC84C0C });
            AddPalette("red_mob", new UInt32[] { 0x000000, 0xD82800, 0xFC9838, 0xFFFFFF });
            AddPalette("blue_mob", new UInt32[] { 0x000000, 0x0000A8, 0x5C94FC, 0xFFFFFF });
            AddPalette("overworld_mob", new UInt32[] { 0x000000, 0x000000, 0x008088, 0xD82800 });
 
            srcRects = new Dictionary<string, Rectangle>();
            srcRectsFlip = new Dictionary<string, SpriteEffects>();
            AddSrcRect("rock", new Rectangle(51, 17, 16, 16), SpriteEffects.None);
            AddSrcRect("link_walk_n0", new Rectangle(0, 17, 16, 16), SpriteEffects.None);
            AddSrcRect("link_walk_n1", new Rectangle(0, 17, 16, 16), SpriteEffects.FlipHorizontally);
            AddSrcRect("link_walk_s0", new Rectangle(0, 0, 16, 16), SpriteEffects.None);
            AddSrcRect("link_walk_s1", new Rectangle(17, 0, 16, 16), SpriteEffects.None);
            AddSrcRect("link_walk_e0", new Rectangle(0, 34, 16, 16), SpriteEffects.None);
            AddSrcRect("link_walk_e1", new Rectangle(17, 34, 16, 16), SpriteEffects.None);
            AddSrcRect("link_walk_w0", new Rectangle(0, 34, 16, 16), SpriteEffects.FlipHorizontally);
            AddSrcRect("link_walk_w1", new Rectangle(17, 34, 16, 16), SpriteEffects.FlipHorizontally);
 
            mapPal = "overworld_green";
 
            playerPos = new Vector2(192, 64);
            playerDir = "s";
            playerCounter = 0;
            playerPal = "green_link";
        }
 
        Texture2D[] BuildSheetParts(Texture2D sheet)
        {
            Color[] data = new Color[sheet.Width * sheet.Height];
            sheet.GetData<Color>(data);
 
            Color[][] dataParts = new Color[4][];
            for (int s = 0; s < 4; s++)
                dataParts[s] = new Color[sheet.Width * sheet.Height];
 
            Color black = new Color(0x00, 0x00, 0x00);
            Color dark = new Color(0x50, 0x50, 0x50);
            Color light = new Color(0xA0, 0xA0, 0xA0);
            Color white = new Color(0xFF, 0xFF, 0xFF);
            Color mask = new Color(0x00, 0x00, 0x00, 0x00);
            for (int y = 0; y < sheet.Height; y++)
            {
                for (int x = 0; x < sheet.Width; x++)
                {
                    int pos = x + y * sheet.Width;
 
                    for (int p = 0; p < 4; p++)
                        dataParts[p][pos] = mask;
 
                    if (data[pos] == black)
                        dataParts[0][pos] = white;
                    if (data[pos] == dark)
                        dataParts[1][pos] = white;
                    if (data[pos] == light)
                        dataParts[2][pos] = white;
                    if (data[pos] == white)
                        dataParts[3][pos] = white;
                }
            }
 
            Texture2D[] sheetParts = new Texture2D[4];
            for (int s = 0; s < 4; s++)
            {
                sheetParts[s] = new Texture2D(GraphicsDevice, sheet.Width, sheet.Height);
                sheetParts[s].SetData<Color>(dataParts[s]);
            }
 
            return sheetParts;
        }
 
        protected override void Update(GameTime gameTime)
        {
            KeyboardState keyboard = Keyboard.GetState();
 
            if (keyboard.IsKeyDown(Keys.Escape)) Exit();
 
            if (keyboard.IsKeyDown(Keys.D1)) mapPal = "overworld_green";
            if (keyboard.IsKeyDown(Keys.D2)) mapPal = "overworld_brown";
            if (keyboard.IsKeyDown(Keys.D3)) mapPal = "overworld_gray";
            if (keyboard.IsKeyDown(Keys.D4)) mapPal = "cave";
 
            if (keyboard.IsKeyDown(Keys.Up)) playerDir = "n";
            if (keyboard.IsKeyDown(Keys.Down)) playerDir = "s";
            if (keyboard.IsKeyDown(Keys.Left)) playerDir = "w";
            if (keyboard.IsKeyDown(Keys.Right)) playerDir = "e";
 
            playerCounter++; if (playerCounter >= 16) playerCounter = 0;
            playerSrcRect = "link_walk_" + playerDir + (playerCounter / 8);
 
            base.Update(gameTime);
        }
 
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
 
            Matrix drawMatrix = Matrix.CreateScale(4f);
            spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, drawMatrix);
            {
                for (int y = 0; y < 8; y++)
                for (int x = 0; x < 8; x++)
                    DrawTile(srcRects["rock"], new Vector2(x * 16, y * 16), palettes[mapPal], srcRectsFlip["rock"]);
                DrawSprite(srcRects[playerSrcRect], playerPos, palettes[playerPal], srcRectsFlip[playerSrcRect]);
            }
            spriteBatch.End();
 
            base.Draw(gameTime);
        }
 
        void AddPalette(string name, UInt32[] colorData)
        {
            Color[] colors = new Color[4];
 
            for (int c = 0; c < 4; c++)
            {
                UInt32 r = (colorData[c] & 0xFF0000) >> 16;
                UInt32 g = (colorData[c] & 0x00FF00) >> 8;
                UInt32 b = (colorData[c] & 0x0000FF) >> 0;
                colors[c] = new Color((byte)r, (byte)g, (byte)b);
            }
 
            palettes.Add(name, colors);
        }
 
        void AddSrcRect(string name, Rectangle rect, SpriteEffects flip)
        {
            srcRects.Add(name, rect);
            srcRectsFlip.Add(name, flip);
        }
 
        void DrawTile(Rectangle srcRect, Vector2 topLeft, Color[] palette, SpriteEffects flip)
        {
            for (int p = 0; p < 4; p++)
                spriteBatch.Draw(tileSheetParts[p], topLeft, srcRect, palette[p], 0f, Vector2.Zero, 1f, flip, 0f);
        }
 
        void DrawSprite(Rectangle srcRect, Vector2 topLeft, Color[] palette, SpriteEffects flip)
        {
            for (int p = 1; p < 4; p++)
                spriteBatch.Draw(spriteSheetParts[p], topLeft, srcRect, palette[p], 0f, Vector2.Zero, 1f, flip, 0f);
        }
    }
}

Comments