Advertisement
SteelGolem

nes-style palettes

Feb 2nd, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.31 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Content;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Microsoft.Xna.Framework.Input;
  8.  
  9. namespace WindowsGame1
  10. {
  11.     static class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             Game1 game = new Game1();
  16.             game.Run();
  17.         }
  18.     }
  19.  
  20.     public class Game1 : Game
  21.     {
  22.         GraphicsDeviceManager graphics;
  23.         SpriteBatch spriteBatch;
  24.  
  25.         Texture2D spriteSheet;
  26.         Texture2D[] spriteSheetParts;
  27.         Texture2D tileSheet;
  28.         Texture2D[] tileSheetParts;
  29.         Dictionary<string, Rectangle> srcRects;
  30.         Dictionary<string, SpriteEffects> srcRectsFlip;
  31.         Dictionary<string, Color[]> palettes;
  32.  
  33.         string mapPal;
  34.  
  35.         Vector2 playerPos;
  36.         string playerDir;
  37.         string playerSrcRect;
  38.         int playerCounter;
  39.         string playerPal;
  40.  
  41.         public Game1()
  42.         {
  43.             graphics = new GraphicsDeviceManager(this);
  44.             graphics.PreferredBackBufferWidth = 1280;
  45.             graphics.PreferredBackBufferHeight = 720;
  46.             Content.RootDirectory = "Content";
  47.             IsMouseVisible = true;
  48.             Window.Title = "zelda1 tilemap palette testing";
  49.         }
  50.  
  51.         protected override void LoadContent()
  52.         {
  53.             spriteBatch = new SpriteBatch(GraphicsDevice);
  54.  
  55.             spriteSheet = Content.Load<Texture2D>("sprite_sheet");
  56.             spriteSheetParts = BuildSheetParts(spriteSheet);
  57.  
  58.             tileSheet = Content.Load<Texture2D>("tile_sheet");
  59.             tileSheetParts = BuildSheetParts(tileSheet);
  60.  
  61.             palettes = new Dictionary<string, Color[]>();
  62.             AddPalette("overworld_green", new UInt32[] { 0x000000, 0x00A800, 0xFCD8A8, 0x2038EC });
  63.             AddPalette("overworld_brown", new UInt32[] { 0x000000, 0xC84C0C, 0xFCD8A8, 0x2038EC });
  64.             AddPalette("overworld_gray", new UInt32[] { 0x000000, 0xFFFFFF, 0x747474, 0x2038EC });
  65.             AddPalette("cave", new UInt32[] { 0x000000, 0x7C0800, 0x000000, 0xC84C0C });
  66.             AddPalette("green_link", new UInt32[] { 0x000000, 0x80D010, 0xFC9838, 0xC84C0C });
  67.             AddPalette("blue_link", new UInt32[] { 0x000000, 0xC4D4FC, 0xFC9838, 0xC84C0C });
  68.             AddPalette("red_link", new UInt32[] { 0x000000, 0xC4D4FC, 0xFC9838, 0xC84C0C });
  69.             AddPalette("red_mob", new UInt32[] { 0x000000, 0xD82800, 0xFC9838, 0xFFFFFF });
  70.             AddPalette("blue_mob", new UInt32[] { 0x000000, 0x0000A8, 0x5C94FC, 0xFFFFFF });
  71.             AddPalette("overworld_mob", new UInt32[] { 0x000000, 0x000000, 0x008088, 0xD82800 });
  72.  
  73.             srcRects = new Dictionary<string, Rectangle>();
  74.             srcRectsFlip = new Dictionary<string, SpriteEffects>();
  75.             AddSrcRect("rock", new Rectangle(51, 17, 16, 16), SpriteEffects.None);
  76.             AddSrcRect("link_walk_n0", new Rectangle(0, 17, 16, 16), SpriteEffects.None);
  77.             AddSrcRect("link_walk_n1", new Rectangle(0, 17, 16, 16), SpriteEffects.FlipHorizontally);
  78.             AddSrcRect("link_walk_s0", new Rectangle(0, 0, 16, 16), SpriteEffects.None);
  79.             AddSrcRect("link_walk_s1", new Rectangle(17, 0, 16, 16), SpriteEffects.None);
  80.             AddSrcRect("link_walk_e0", new Rectangle(0, 34, 16, 16), SpriteEffects.None);
  81.             AddSrcRect("link_walk_e1", new Rectangle(17, 34, 16, 16), SpriteEffects.None);
  82.             AddSrcRect("link_walk_w0", new Rectangle(0, 34, 16, 16), SpriteEffects.FlipHorizontally);
  83.             AddSrcRect("link_walk_w1", new Rectangle(17, 34, 16, 16), SpriteEffects.FlipHorizontally);
  84.  
  85.             mapPal = "overworld_green";
  86.  
  87.             playerPos = new Vector2(192, 64);
  88.             playerDir = "s";
  89.             playerCounter = 0;
  90.             playerPal = "green_link";
  91.         }
  92.  
  93.         Texture2D[] BuildSheetParts(Texture2D sheet)
  94.         {
  95.             Color[] data = new Color[sheet.Width * sheet.Height];
  96.             sheet.GetData<Color>(data);
  97.  
  98.             Color[][] dataParts = new Color[4][];
  99.             for (int s = 0; s < 4; s++)
  100.                 dataParts[s] = new Color[sheet.Width * sheet.Height];
  101.  
  102.             Color black = new Color(0x00, 0x00, 0x00);
  103.             Color dark = new Color(0x50, 0x50, 0x50);
  104.             Color light = new Color(0xA0, 0xA0, 0xA0);
  105.             Color white = new Color(0xFF, 0xFF, 0xFF);
  106.             Color mask = new Color(0x00, 0x00, 0x00, 0x00);
  107.             for (int y = 0; y < sheet.Height; y++)
  108.             {
  109.                 for (int x = 0; x < sheet.Width; x++)
  110.                 {
  111.                     int pos = x + y * sheet.Width;
  112.  
  113.                     for (int p = 0; p < 4; p++)
  114.                         dataParts[p][pos] = mask;
  115.  
  116.                     if (data[pos] == black)
  117.                         dataParts[0][pos] = white;
  118.                     if (data[pos] == dark)
  119.                         dataParts[1][pos] = white;
  120.                     if (data[pos] == light)
  121.                         dataParts[2][pos] = white;
  122.                     if (data[pos] == white)
  123.                         dataParts[3][pos] = white;
  124.                 }
  125.             }
  126.  
  127.             Texture2D[] sheetParts = new Texture2D[4];
  128.             for (int s = 0; s < 4; s++)
  129.             {
  130.                 sheetParts[s] = new Texture2D(GraphicsDevice, sheet.Width, sheet.Height);
  131.                 sheetParts[s].SetData<Color>(dataParts[s]);
  132.             }
  133.  
  134.             return sheetParts;
  135.         }
  136.  
  137.         protected override void Update(GameTime gameTime)
  138.         {
  139.             KeyboardState keyboard = Keyboard.GetState();
  140.  
  141.             if (keyboard.IsKeyDown(Keys.Escape)) Exit();
  142.  
  143.             if (keyboard.IsKeyDown(Keys.D1)) mapPal = "overworld_green";
  144.             if (keyboard.IsKeyDown(Keys.D2)) mapPal = "overworld_brown";
  145.             if (keyboard.IsKeyDown(Keys.D3)) mapPal = "overworld_gray";
  146.             if (keyboard.IsKeyDown(Keys.D4)) mapPal = "cave";
  147.  
  148.             if (keyboard.IsKeyDown(Keys.Up)) playerDir = "n";
  149.             if (keyboard.IsKeyDown(Keys.Down)) playerDir = "s";
  150.             if (keyboard.IsKeyDown(Keys.Left)) playerDir = "w";
  151.             if (keyboard.IsKeyDown(Keys.Right)) playerDir = "e";
  152.  
  153.             playerCounter++; if (playerCounter >= 16) playerCounter = 0;
  154.             playerSrcRect = "link_walk_" + playerDir + (playerCounter / 8);
  155.  
  156.             base.Update(gameTime);
  157.         }
  158.  
  159.         protected override void Draw(GameTime gameTime)
  160.         {
  161.             GraphicsDevice.Clear(Color.CornflowerBlue);
  162.  
  163.             Matrix drawMatrix = Matrix.CreateScale(4f);
  164.             spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, drawMatrix);
  165.             {
  166.                 for (int y = 0; y < 8; y++)
  167.                 for (int x = 0; x < 8; x++)
  168.                     DrawTile(srcRects["rock"], new Vector2(x * 16, y * 16), palettes[mapPal], srcRectsFlip["rock"]);
  169.                 DrawSprite(srcRects[playerSrcRect], playerPos, palettes[playerPal], srcRectsFlip[playerSrcRect]);
  170.             }
  171.             spriteBatch.End();
  172.  
  173.             base.Draw(gameTime);
  174.         }
  175.  
  176.         void AddPalette(string name, UInt32[] colorData)
  177.         {
  178.             Color[] colors = new Color[4];
  179.  
  180.             for (int c = 0; c < 4; c++)
  181.             {
  182.                 UInt32 r = (colorData[c] & 0xFF0000) >> 16;
  183.                 UInt32 g = (colorData[c] & 0x00FF00) >> 8;
  184.                 UInt32 b = (colorData[c] & 0x0000FF) >> 0;
  185.                 colors[c] = new Color((byte)r, (byte)g, (byte)b);
  186.             }
  187.  
  188.             palettes.Add(name, colors);
  189.         }
  190.  
  191.         void AddSrcRect(string name, Rectangle rect, SpriteEffects flip)
  192.         {
  193.             srcRects.Add(name, rect);
  194.             srcRectsFlip.Add(name, flip);
  195.         }
  196.  
  197.         void DrawTile(Rectangle srcRect, Vector2 topLeft, Color[] palette, SpriteEffects flip)
  198.         {
  199.             for (int p = 0; p < 4; p++)
  200.                 spriteBatch.Draw(tileSheetParts[p], topLeft, srcRect, palette[p], 0f, Vector2.Zero, 1f, flip, 0f);
  201.         }
  202.  
  203.         void DrawSprite(Rectangle srcRect, Vector2 topLeft, Color[] palette, SpriteEffects flip)
  204.         {
  205.             for (int p = 1; p < 4; p++)
  206.                 spriteBatch.Draw(spriteSheetParts[p], topLeft, srcRect, palette[p], 0f, Vector2.Zero, 1f, flip, 0f);
  207.         }
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement