SteelGolem

drawing a screenshot

Jul 10th, 2025
1,070
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.45 KB | None | 0 0
  1.  
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4.  
  5. class NES
  6. {
  7.     public const int WIDTH = 256;
  8.     public const int HEIGHT = 224;
  9.     public static Color clear_color;
  10. }
  11.  
  12. class GAME : Game
  13. {
  14.     static int draw_scale = 3;
  15.     static Matrix scale_matrix;
  16.     static SpriteBatch sprite_batch;
  17.     static Texture2D mario_png;
  18.     static Texture2D[] font_pngs;
  19.     static Texture2D[] tileset_pngs;
  20.     struct PALETTE
  21.     {
  22.         public Color[] colors;
  23.         public PALETTE(Color color1, Color color2, Color color3)
  24.         {
  25.             // color 0 is NES.clear_color
  26.             colors = new Color[] { color1, color2, color3 };
  27.         }
  28.     }
  29.     static PALETTE[] tilemap_pals = new PALETTE[4];
  30.     class TILEMAP
  31.     {
  32.         public int width, height;
  33.         public int[,] tiles;
  34.         public int[,] colors;
  35.         public void draw(Vector2 pos)
  36.         {
  37.             Vector2 start_pos = pos;
  38.             Rectangle src_rect = new Rectangle(0, 0, 16, 16);
  39.             for (int y = 0; y < height; y++)
  40.             {
  41.                 for (int x = 0; x < width; x++)
  42.                 {
  43.                     src_rect.X = (tiles[y, x] % 8) * 16;
  44.                     src_rect.Y = (tiles[y, x] / 8) * 16;
  45.                     for (int c = 0; c < 3; c++)
  46.                         sprite_batch.Draw(tileset_pngs[c], pos, src_rect,
  47.                             tilemap_pals[colors[y, x]].colors[c]);
  48.                     pos.X += 16;
  49.                 }
  50.                 pos.X = start_pos.X;
  51.                 pos.Y += 16;
  52.             }
  53.         }
  54.     }
  55.     static TILEMAP tilemap;
  56.     static void Main() { new GAME().Run(); }
  57.     GAME()
  58.     {
  59.         GraphicsDeviceManager gdm = new GraphicsDeviceManager(this);
  60.         gdm.PreferredBackBufferWidth = NES.WIDTH * draw_scale;
  61.         gdm.PreferredBackBufferHeight = NES.HEIGHT * draw_scale;
  62.         gdm.ApplyChanges();
  63.         IsMouseVisible = true;
  64.     }
  65.     protected override void LoadContent()
  66.     {
  67.         scale_matrix = Matrix.CreateScale(draw_scale);
  68.         sprite_batch = new SpriteBatch(GraphicsDevice);
  69.         mario_png = Texture2D.FromFile(GraphicsDevice, "mario.png");
  70.         font_pngs = new Texture2D[3];
  71.         Texture2D font_gs_png = Texture2D.FromFile(GraphicsDevice, "font_gs.png");
  72.         font_pngs[0] = get_png_color(font_gs_png, new Color(255, 255, 255));
  73.         font_pngs[1] = get_png_color(font_gs_png, new Color(128, 128, 128));
  74.         font_pngs[2] = get_png_color(font_gs_png, new Color(0, 0, 0));
  75.         tileset_pngs = new Texture2D[3];
  76.         Texture2D tileset_gs_png = Texture2D.FromFile(GraphicsDevice, "tileset_gs.png");
  77.         tileset_pngs[0] = get_png_color(tileset_gs_png, new Color(255, 255, 255));
  78.         tileset_pngs[1] = get_png_color(tileset_gs_png, new Color(128, 128, 128));
  79.         tileset_pngs[2] = get_png_color(tileset_gs_png, new Color(0, 0, 0));
  80.         NES.clear_color = new Color(92, 148, 252);
  81.         tilemap_pals = new PALETTE[4]
  82.         {
  83.             new PALETTE(new Color(128, 208, 16), new Color(0, 168, 0), new Color(0, 0, 0)),
  84.             new PALETTE(new Color(252, 188, 176), new Color(200, 76, 12), new Color(0, 0, 0)),
  85.             new PALETTE(new Color(252, 252, 252), new Color(60, 188, 252), new Color(0, 0, 0)),
  86.             new PALETTE(new Color(252, 152, 56), new Color(200, 76, 12), new Color(0, 0, 0)),
  87.         };
  88.         tilemap = new TILEMAP();
  89.         tilemap.width = 16;
  90.         tilemap.height = 14;
  91.         tilemap.tiles = new int[14, 16]
  92.         {
  93.             { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, },
  94.             { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, },
  95.             { 00, 00, 00, 00, 00, 00, 00, 00, 08, 09, 10, 00, 00, 00, 00, 00, },
  96.             { 00, 00, 00, 00, 00, 00, 00, 00, 11, 12, 13, 00, 00, 00, 00, 00, },
  97.             { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, },
  98.             { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, },
  99.             { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, },
  100.             { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, },
  101.             { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, },
  102.             { 00, 00, 07, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, },
  103.             { 00, 02, 03, 06, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, },
  104.             { 02, 03, 04, 05, 06, 00, 00, 00, 00, 00, 00, 08, 09, 09, 09, 10, },
  105.             { 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, },
  106.             { 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, },
  107.         };
  108.         tilemap.colors = new int[14, 16]
  109.         {
  110.             { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
  111.             { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
  112.             { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, },
  113.             { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, },
  114.             { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
  115.             { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
  116.             { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
  117.             { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
  118.             { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
  119.             { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
  120.             { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
  121.             { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
  122.             { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, },
  123.             { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, },
  124.         };
  125.         base.LoadContent();
  126.     }
  127.     Texture2D get_png_color(Texture2D png, Color color)
  128.     {
  129.         Color[] data = new Color[png.Width * png.Height];
  130.         png.GetData(data);
  131.         for (int d = 0; d < data.Length; d++)
  132.             if (data[d] == color) data[d] = Color.White;
  133.             else data[d] = Color.Transparent;
  134.         Texture2D png_color = new Texture2D(GraphicsDevice, png.Width, png.Height);
  135.         png_color.SetData(data);
  136.         return png_color;
  137.     }
  138.     protected override void Update(GameTime gameTime)
  139.     {
  140.         base.Update(gameTime);
  141.     }
  142.     protected override void Draw(GameTime gameTime)
  143.     {
  144.         GraphicsDevice.Clear(NES.clear_color);
  145.         sprite_batch.Begin(SpriteSortMode.Deferred, null,
  146.             SamplerState.PointClamp, null, null, null, scale_matrix);
  147.         tilemap.draw(new Vector2(0, 8));
  148.         draw_string(
  149.             "   MARIO          WORLD  TIME\n" +
  150.             "   000000   x00    1-1    400",
  151.             new Vector2(0, 8), 2);
  152.         draw_string("\u0010", new Vector2(88, 16), 3);
  153.         sprite_batch.Draw(mario_png, new Vector2(40, 185), Color.White);
  154.         sprite_batch.End();
  155.         base.Draw(gameTime);
  156.     }
  157.     void draw_string(string text, Vector2 pos, int pal)
  158.     {
  159.         Vector2 start_pos = pos;
  160.         Rectangle src_rect = new Rectangle(0, 0, 8, 8);
  161.         for (int t = 0; t < text.Length; t++)
  162.         {
  163.             char c = text[t];
  164.             if (c == '\n')
  165.             {
  166.                 pos.X = start_pos.X;
  167.                 pos.Y += 8;
  168.                 continue;
  169.             }
  170.             src_rect.X = (c % 16) * 8;
  171.             src_rect.Y = (c / 16) * 8;
  172.             for (int p = 0; p < 3; p++)
  173.                 sprite_batch.Draw(font_pngs[p], pos, src_rect,
  174.                     tilemap_pals[pal].colors[p]);
  175.             pos.X += 8;
  176.         }
  177.     }
  178. }
  179.  
Advertisement