/// <summary> /// Loads a tile map from a bitmap /// </summary> /// <param name="bitmap">The bitmap to create a tile map from.</param> public static void LoadTileMap(Bitmap image, int tileWidth, int tileHeight) { // Get the columns and row amount for the tile grid int cols = image.Width / tileWidth; int rows = image.Height / tileHeight; // Create a new texture grid ResTexture[,] map = new ResTexture[cols, rows]; // Iterate through columns for (int col = 0; col < cols; col++) { // Iterate through rows for (int row = 0; row < rows; row++) { // Copy image part rectangle Rectangle rect = new Rectangle(col * tileWidth, row * tileHeight, tileWidth, tileHeight); // Create tile texture map[col, row] = new ResTexture(image.Clone(rect, image.PixelFormat)); } } // Add tile map _tileMaps.Add(map); }
/// <summary> /// Loads a texture to the texture list /// </summary> /// <param name="image">The bitmap image used as a texture</param> /// <param name="id">The id of the texture.</param> public static void LoadTexture(PixelMap image, int id) { // If the key already exists if (_sprites.ContainsKey(id)) { // Dispose of old texture _sprites[id].Dispose(); // Create a new texture _sprites[id] = new ResTexture(image); } else // Add a texture to the list { _sprites.Add(id, new ResTexture(image)); } }
/// <summary> /// Constructs a new quad. /// </summary> /// <param name="textureId"></param> /// <param name="vertices"></param> /// <param name="textureCoordinates"></param> /// <param name="color"></param> public Quad(ResTexture texture, PointF position, PointF scale, float angle, Color color) { _textureId = texture.TextureId; _color = color; // Set vertices. _vertices[0] = new PointF(position.X, position.Y); _vertices[1] = new PointF(texture.TextureSize + _vertices[0].X, position.Y); _vertices[2] = new PointF(texture.TextureSize + _vertices[0].X, texture.TextureSize + _vertices[0].Y); _vertices[3] = new PointF(position.X, texture.TextureSize + _vertices[0].Y); // Set texture coordinates. _textureCoordinates[0] = new PointF(0.0f, 0.0f); _textureCoordinates[1] = new PointF(scale.X, 0.0f); _textureCoordinates[2] = new PointF(scale.X, scale.Y); _textureCoordinates[3] = new PointF(0.0f, scale.Y); }
/// <summary> /// Stores a sprite in a cached batch. /// </summary> /// <param name="id">The texture resource id.</param> /// <param name="x">The horizontal coordinate.</param> /// <param name="y">The vertical coordinate.</param> /// <param name="color">The blend color.</param> public static void DrawSpriteCached(int id, int x, int y, Color color) { // If the sprite does not exist, return. if (_sprites.ContainsKey(id) == false) { return; } // Get the resource. ResTexture texture = _sprites[id]; // If the texture is empty return. if (texture == null) { return; } // Add a textured quad. _quads.Add(new Quad(texture, new PointF(x, y), new PointF(1, 1), 0, color)); }
/// <summary> /// Draws a sprite. /// </summary> /// <param name="id">The texture resource id.</param> /// <param name="x">The horizontal coordinate.</param> /// <param name="y">The vertical coordinate.</param> /// <param name="color">The blend color.</param> public static void DrawSprite(int id, int x, int y, Color color) { // If the sprite does not exist, return. if (_sprites.ContainsKey(id) == false) { return; } // Get the resource. ResTexture texture = _sprites[id]; // If the texture is empty return. if (texture == null) { return; } // Add a textured quad. Quad quad = new Quad(texture, new PointF(x, y), new PointF(1, 1), 0, color); OpenGL.glEnable(GLOption.Texture2D); OpenGL.glPolygonMode(GLPolygonFaces.Back, GLPolygonMode.Fill); OpenGL.glBindTexture(GLTexture.Texture2D, quad.TextureId); OpenGL.glBegin(GLPrimative.Quads); OpenGL.glColor4(quad.Color); OpenGL.glTexCoord2f(quad.TextureCoordinates[0].X, quad.TextureCoordinates[0].Y); OpenGL.glVertex2f(quad.Vertices[0].X - _offsetX, quad.Vertices[0].Y - _offsetY); OpenGL.glTexCoord2f(quad.TextureCoordinates[1].X, quad.TextureCoordinates[1].Y); OpenGL.glVertex2f(quad.Vertices[1].X - _offsetX, quad.Vertices[1].Y - _offsetY); OpenGL.glTexCoord2f(quad.TextureCoordinates[2].X, quad.TextureCoordinates[2].Y); OpenGL.glVertex2f(quad.Vertices[2].X - _offsetX, quad.Vertices[2].Y - _offsetY); OpenGL.glTexCoord2f(quad.TextureCoordinates[3].X, quad.TextureCoordinates[3].Y); OpenGL.glVertex2f(quad.Vertices[3].X - _offsetX, quad.Vertices[3].Y - _offsetY); OpenGL.glEnd(); }
private PointF[] _vertices = new PointF[4]; // The vertices. #endregion Fields #region Constructors /// <summary> /// Constructs a new quad. /// </summary> /// <param name="textureId"></param> /// <param name="vertices"></param> /// <param name="textureCoordinates"></param> /// <param name="color"></param> public Quad(ResTexture texture, PointF position, PointF scale, float angle, Color color) { _textureId = texture.TextureId; _color = color; // Set vertices. _vertices[0] = new PointF(position.X, position.Y); _vertices[1] = new PointF(texture.TextureSize + _vertices[0].X, position.Y); _vertices[2] = new PointF(texture.TextureSize + _vertices[0].X, texture.TextureSize + _vertices[0].Y); _vertices[3] = new PointF(position.X, texture.TextureSize + _vertices[0].Y); // Set texture coordinates. _textureCoordinates[0] = new PointF(0.0f, 0.0f); _textureCoordinates[1] = new PointF(scale.X, 0.0f); _textureCoordinates[2] = new PointF(scale.X, scale.Y); _textureCoordinates[3] = new PointF(0.0f, scale.Y); }
/// <summary> /// Draws a part of an image resource with the desired, scale, rotation, and blend colors. /// </summary> /// <param name="x">The x coordinate.</param> /// <param name="y">The y coordinate.</param> public static void DrawTile(ResTexture texture, int x, int y, float scaleX, float scaleY, float rotation, Color color) { // Add a textured quad. _quads.Add(new Quad(texture, new PointF(x, y), new PointF(scaleX, scaleY), rotation, color)); }