Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- class NES
- {
- public const int WIDTH = 256;
- public const int HEIGHT = 224;
- public static Color clear_color;
- }
- class GAME : Game
- {
- static int draw_scale = 3;
- static Matrix scale_matrix;
- static SpriteBatch sprite_batch;
- static Texture2D mario_png;
- static Texture2D[] font_pngs;
- static Texture2D[] tileset_pngs;
- struct PALETTE
- {
- public Color[] colors;
- public PALETTE(Color color1, Color color2, Color color3)
- {
- // color 0 is NES.clear_color
- colors = new Color[] { color1, color2, color3 };
- }
- }
- static PALETTE[] tilemap_pals = new PALETTE[4];
- class TILEMAP
- {
- public int width, height;
- public int[,] tiles;
- public int[,] colors;
- public void draw(Vector2 pos)
- {
- Vector2 start_pos = pos;
- Rectangle src_rect = new Rectangle(0, 0, 16, 16);
- for (int y = 0; y < height; y++)
- {
- for (int x = 0; x < width; x++)
- {
- src_rect.X = (tiles[y, x] % 8) * 16;
- src_rect.Y = (tiles[y, x] / 8) * 16;
- for (int c = 0; c < 3; c++)
- sprite_batch.Draw(tileset_pngs[c], pos, src_rect,
- tilemap_pals[colors[y, x]].colors[c]);
- pos.X += 16;
- }
- pos.X = start_pos.X;
- pos.Y += 16;
- }
- }
- }
- static TILEMAP tilemap;
- static void Main() { new GAME().Run(); }
- GAME()
- {
- GraphicsDeviceManager gdm = new GraphicsDeviceManager(this);
- gdm.PreferredBackBufferWidth = NES.WIDTH * draw_scale;
- gdm.PreferredBackBufferHeight = NES.HEIGHT * draw_scale;
- gdm.ApplyChanges();
- IsMouseVisible = true;
- }
- protected override void LoadContent()
- {
- scale_matrix = Matrix.CreateScale(draw_scale);
- sprite_batch = new SpriteBatch(GraphicsDevice);
- mario_png = Texture2D.FromFile(GraphicsDevice, "mario.png");
- font_pngs = new Texture2D[3];
- Texture2D font_gs_png = Texture2D.FromFile(GraphicsDevice, "font_gs.png");
- font_pngs[0] = get_png_color(font_gs_png, new Color(255, 255, 255));
- font_pngs[1] = get_png_color(font_gs_png, new Color(128, 128, 128));
- font_pngs[2] = get_png_color(font_gs_png, new Color(0, 0, 0));
- tileset_pngs = new Texture2D[3];
- Texture2D tileset_gs_png = Texture2D.FromFile(GraphicsDevice, "tileset_gs.png");
- tileset_pngs[0] = get_png_color(tileset_gs_png, new Color(255, 255, 255));
- tileset_pngs[1] = get_png_color(tileset_gs_png, new Color(128, 128, 128));
- tileset_pngs[2] = get_png_color(tileset_gs_png, new Color(0, 0, 0));
- NES.clear_color = new Color(92, 148, 252);
- tilemap_pals = new PALETTE[4]
- {
- new PALETTE(new Color(128, 208, 16), new Color(0, 168, 0), new Color(0, 0, 0)),
- new PALETTE(new Color(252, 188, 176), new Color(200, 76, 12), new Color(0, 0, 0)),
- new PALETTE(new Color(252, 252, 252), new Color(60, 188, 252), new Color(0, 0, 0)),
- new PALETTE(new Color(252, 152, 56), new Color(200, 76, 12), new Color(0, 0, 0)),
- };
- tilemap = new TILEMAP();
- tilemap.width = 16;
- tilemap.height = 14;
- tilemap.tiles = new int[14, 16]
- {
- { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, },
- { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, },
- { 00, 00, 00, 00, 00, 00, 00, 00, 08, 09, 10, 00, 00, 00, 00, 00, },
- { 00, 00, 00, 00, 00, 00, 00, 00, 11, 12, 13, 00, 00, 00, 00, 00, },
- { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, },
- { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, },
- { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, },
- { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, },
- { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, },
- { 00, 00, 07, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, },
- { 00, 02, 03, 06, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, },
- { 02, 03, 04, 05, 06, 00, 00, 00, 00, 00, 00, 08, 09, 09, 09, 10, },
- { 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, },
- { 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, },
- };
- tilemap.colors = new int[14, 16]
- {
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
- { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, },
- { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
- { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, },
- { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, },
- };
- base.LoadContent();
- }
- Texture2D get_png_color(Texture2D png, Color color)
- {
- Color[] data = new Color[png.Width * png.Height];
- png.GetData(data);
- for (int d = 0; d < data.Length; d++)
- if (data[d] == color) data[d] = Color.White;
- else data[d] = Color.Transparent;
- Texture2D png_color = new Texture2D(GraphicsDevice, png.Width, png.Height);
- png_color.SetData(data);
- return png_color;
- }
- protected override void Update(GameTime gameTime)
- {
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(NES.clear_color);
- sprite_batch.Begin(SpriteSortMode.Deferred, null,
- SamplerState.PointClamp, null, null, null, scale_matrix);
- tilemap.draw(new Vector2(0, 8));
- draw_string(
- " MARIO WORLD TIME\n" +
- " 000000 x00 1-1 400",
- new Vector2(0, 8), 2);
- draw_string("\u0010", new Vector2(88, 16), 3);
- sprite_batch.Draw(mario_png, new Vector2(40, 185), Color.White);
- sprite_batch.End();
- base.Draw(gameTime);
- }
- void draw_string(string text, Vector2 pos, int pal)
- {
- Vector2 start_pos = pos;
- Rectangle src_rect = new Rectangle(0, 0, 8, 8);
- for (int t = 0; t < text.Length; t++)
- {
- char c = text[t];
- if (c == '\n')
- {
- pos.X = start_pos.X;
- pos.Y += 8;
- continue;
- }
- src_rect.X = (c % 16) * 8;
- src_rect.Y = (c / 16) * 8;
- for (int p = 0; p < 3; p++)
- sprite_batch.Draw(font_pngs[p], pos, src_rect,
- tilemap_pals[pal].colors[p]);
- pos.X += 8;
- }
- }
- }
Advertisement