/** * Copies the bits of the given sprite to the chosen location on the canvas */ public void BitBltExt(MySprite sprite, int x, int y, bool invertColors, bool transparentBackground) { // Don't start drawing outside the screen - too complicated for late night programming if (x < 0 || y < 0) { return; } // Move the screen cursor to the initial position int screenPos = resX * y + x; // Compute the length of the sprite int spriteLength = sprite.height * sprite.width; // Move the sprite's horizontal cursor to the first column int spritePosX = 0; // Loop through the sprite's pixels and copy them to the screen buffer for (int spritePos = 0; spritePos < spriteLength; spritePos++) { try { // Copy the value of the current pixel, after transforming it according to the given rules Buffer[screenPos] = TransformSourcePixelValue(sprite.data[spritePos], Buffer[screenPos], invertColors, transparentBackground); // Move the screen cursor to the next pixel on the screens screenPos++; } catch (Exception exc) { // If it's outside the screen, it will overflow and it will throw an exception which needs to be caught } // Don't draw content outside the screen if (screenPos >= length - 1) { return; } // Move the sprite's horizontal cursor to the next column spritePosX++; // If the sprite's horizontal cursor has reached the last column if (spritePosX == sprite.width) { spritePosX = 0; // Reset the sprite's horizontal cursor screenPos += resX - sprite.width; // Move the screen cursor to the next row } } }
public void BitBlt( MySprite Sprite, int cropX1, int cropY1, int cropX2, int cropY2, int x, int y ) { int trgX = x; for (int srcX = cropX1; srcX <= cropX2; srcX++) { int trgY = y; for (int srcY = cropY1; srcY <= cropY2; srcY++) { SetPixel(trgX, trgY, Sprite.data[srcY * Sprite.width + srcX]); trgY++; } trgX++; } }
/** * Draws text on the canvas using the default font. * Multi-font support may be added at a later time. */ public void DrawColorText(int x, int y, String text, bool invertColors, bool transparentBackground) { // Don't crash if there's no font selected or if there's no text provided if (DefaultFont == null || text == null) { return; } // Get the chars array from the given text char[] textChars = text.ToCharArray(); // Initialize cursors int screenPosX = x; int prevSpacing = 7; // This will ensure that, if the font is missing a sprite, there can still be an empty space in its place // For each character in the given text foreach (char chr in textChars) { // Identify the sprite related to to the char MySprite CharSprite = DefaultFont[chr]; // If the bitmap font has a sprite defined for the char, // then put it on the screen and set the value of prevSpacing to the width of that last sprite // NOTE: some bitmap fonts might have sprites with different widths (they may not all be mono-spaced) if (CharSprite != null) { BitBltExt(CharSprite, screenPosX, y, invertColors, transparentBackground); prevSpacing = CharSprite.width; } // Regardless of whether or not there's a sprite defined for the character, // the screen cursor needs to advance to the next character position screenPosX += prevSpacing; // However, if the screen cursor has advanced so far that it's outside the drawing area, // then there's no sense in processing any more text if (screenPosX >= resX) { return; } } }
public static MySprite ResizeSpriteCanvas(MySprite Sprite, int newWidth, int newHeight, int horizontalAlignment, int verticalAlignment) { // Garbage in, garbage out if (Sprite == null || newWidth < 1 || newHeight < 1) { return(null); } // Create a new canvas to paint the original sprite to MyCanvas NewCanvas = new MyCanvas(newWidth, newHeight); // Compute the coordinates int posX = ComputePos(Sprite.width, newWidth, horizontalAlignment); int posY = ComputePos(Sprite.height, newHeight, verticalAlignment); // Draw the sprite onto the canvas NewCanvas.BitBlt(Sprite, posX, posY); // Return a new sprite with the data of a new canvas return(new MySprite(newWidth, newHeight, NewCanvas.GetBuffer())); }
public static MySprite ResizeSpriteCanvas(MySprite Sprite, int newWidth, int newHeight) { return(ResizeSpriteCanvas(Sprite, newWidth, newHeight, DrawingFrameworkConstants.HORIZONTAL_ALIGN_CENTER, DrawingFrameworkConstants.VERTICAL_ALIGN_MIDDLE)); }
/** * Copies the bits of the given sprite to the chosen location on the canvas */ public void BitBlt(MySprite sprite, int x, int y) { BitBltExt(sprite, x, y, false, false); }