public VirtualTexture2D(VirtualTexture2D existingInstance, int x, int y, int width, int height) { Texture = existingInstance.Texture; SourceRectangle = existingInstance.GetRelativeSourceRectangle(x, y, width, height); Width = width; Height = height; }
// A lot of code is written here, but it's mostly not important at all // for the shader relevant parts of this example. This just loads the // textures and the effect file in from disk and setups the tiles to // be rendered for the game screen. protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // First load the light mask texture _lightMask = Content.Load <Texture2D>(@"lightmask"); // Next, load the simple light shader effect _simpleLightShader = Content.Load <Effect>(@"SimpleLightShader"); // Next, create a new VirtualTexture2D for the texture atlas. VirtualTexture2D atlas = new VirtualTexture2D(Content.Load <Texture2D>(@"atlas")); // The first three columns of the top row of the atlas are the left and right wall tile sprites int numWallTextures = 3; VirtualTexture2D[] wallTextures = new VirtualTexture2D[numWallTextures]; for (int i = 0; i < numWallTextures; i++) { wallTextures[i] = new VirtualTexture2D(atlas, i * 32, 0, 32, 32); } // The first three columns of the middle row of the atlas are the grass tiles sprites int numGroundTextures = 4; VirtualTexture2D[] groundTextures = new VirtualTexture2D[numGroundTextures]; for (int i = 0; i < numGroundTextures; i++) { groundTextures[i] = new VirtualTexture2D(atlas, i * 32, 32, 32, 32); } // The first three columns of the third row of the atlas are the top wall tile sprites int numTopWallTextures = 3; VirtualTexture2D[] topWallTextures = new VirtualTexture2D[numTopWallTextures]; for (int i = 0; i < numTopWallTextures; i++) { topWallTextures[i] = new VirtualTexture2D(atlas, i * 32, 64, 32, 32); } // The columns in the bottom row of the atals are the frames of the fire tile sprite int numFireFrames = 4; VirtualTexture2D[] fireFrames = new VirtualTexture2D[numFireFrames]; for (int i = 0; i < numFireFrames; i++) { fireFrames[i] = new VirtualTexture2D(atlas, i * 32, 96, 32, 32); } // Now that we've got the textures sorted for the actual tiles, let's create // the tilemap Random random = new Random(); int columnCount = 640 / 32; int rowCount = 360 / 32; _tiles = new GameObject[columnCount * rowCount]; int tileIndex = 0; for (int row = 0; row < rowCount; row++) { for (int column = 0; column < columnCount; column++) { // Calculate the position of the tile Vector2 tilePos = new Vector2(column * 32, row * 32); // If this is the first column, or the last column, or if this // is the last row, then we use the normal wall tile sprites. if (column == 0 || column == columnCount - 1 || row == rowCount - 1) { _tiles[tileIndex] = new Tile(tilePos, wallTextures[random.Next(0, numWallTextures)]); } else { // if this is the top row, then we use the top row wall tile sprites if (row == 0) { _tiles[tileIndex] = new Tile(tilePos, topWallTextures[random.Next(0, numTopWallTextures)]); } else { // Otherwise, this tile is one of the ones inside the wall, so we // add a grass tile _tiles[tileIndex] = new Tile(tilePos, groundTextures[random.Next(0, numGroundTextures)]); } } // Increase the tileindex tileIndex++; } } // Create the four tourch animated tiles _fireTiles = new GameObject[4]; float delay = 0.15f; _fireTiles[0] = _tiles[42] = new AnimatedTile(fireFrames, delay, new Vector2(64, 64)); _fireTiles[1] = _tiles[57] = new AnimatedTile(fireFrames, delay, new Vector2(544, 64)); _fireTiles[2] = _tiles[162] = new AnimatedTile(fireFrames, delay, new Vector2(64, 256)); _fireTiles[3] = _tiles[177] = new AnimatedTile(fireFrames, delay, new Vector2(544, 256)); // Create our two render targets int width = GraphicsDevice.PresentationParameters.BackBufferWidth; int height = GraphicsDevice.PresentationParameters.BackBufferHeight; _lightTarget = new RenderTarget2D(GraphicsDevice, width, height); _mainTarget = new RenderTarget2D(GraphicsDevice, width, height); }