private IVisualization2d GenerateHiResSprite(ReadOnlyDictionary <string, object> parms) { int offset = Util.GetFromObjDict(parms, P_OFFSET, 0); byte color = (byte)Util.GetFromObjDict(parms, P_COLOR, 0); bool isDoubleWide = Util.GetFromObjDict(parms, P_DOUBLE_WIDE, false); bool isDoubleHigh = Util.GetFromObjDict(parms, P_DOUBLE_HIGH, false); if (offset < 0 || offset >= mFileData.Length || color < 0 || color > MAX_COLOR) { // the UI should flag these based on range (and ideally wouldn't have called us) mAppRef.ReportError("Invalid parameter"); return(null); } int lastOffset = offset + SPRITE_SIZE - 1; if (lastOffset >= mFileData.Length) { mAppRef.ReportError("Sprite runs off end of file (last offset +" + lastOffset.ToString("x6") + ")"); return(null); } int xwide = isDoubleWide ? 2 : 1; int xhigh = isDoubleHigh ? 2 : 1; VisBitmap8 vb = new VisBitmap8(BYTE_WIDTH * 8 * xwide, HEIGHT * xhigh); SetPalette(vb); // Clear all pixels to transparent, then just draw the non-transparent ones. vb.SetAllPixelIndices(TRANSPARENT); for (int row = 0; row < HEIGHT; row++) { for (int col = 0; col < BYTE_WIDTH; col++) { byte val = mFileData[offset + row * BYTE_WIDTH + col]; for (int bit = 0; bit < 8; bit++) { if ((val & 0x80) != 0) { int xc = (col * 8 + bit) * xwide; int yc = row * xhigh; vb.SetPixelIndex(xc, yc, color); if (isDoubleWide || isDoubleHigh) { // Draw doubled pixels. If we're only doubled in one dimension // this will draw pixels twice. vb.SetPixelIndex(xc + xwide - 1, yc, color); vb.SetPixelIndex(xc, yc + xhigh - 1, color); vb.SetPixelIndex(xc + xwide - 1, yc + xhigh - 1, color); } } val <<= 1; } } } return(vb); }
private IVisualization2d GenerateSprite(ReadOnlyDictionary <string, object> parms, bool isMultiColor) { int offset = Util.GetFromObjDict(parms, P_OFFSET, 0); byte color = (byte)Util.GetFromObjDict(parms, P_COLOR, 0); bool isDoubleWide = Util.GetFromObjDict(parms, P_DOUBLE_WIDE, false); bool isDoubleHigh = Util.GetFromObjDict(parms, P_DOUBLE_HIGH, false); byte color01 = 0; byte color11 = 0; if (isMultiColor) { color01 = (byte)Util.GetFromObjDict(parms, P_COLOR_01, 0); color11 = (byte)Util.GetFromObjDict(parms, P_COLOR_11, 0); } if (offset < 0 || offset >= mFileData.Length || color < 0 || color > MAX_COLOR || color01 < 0 || color01 > MAX_COLOR || color11 < 0 || color11 > MAX_COLOR) { // the UI should flag these based on range (and ideally wouldn't have called us) mAppRef.ReportError("Invalid parameter"); return(null); } int lastOffset = offset + SPRITE_SIZE - 1; if (lastOffset >= mFileData.Length) { mAppRef.ReportError("Sprite runs off end of file (last offset +" + lastOffset.ToString("x6") + ")"); return(null); } int xwide = isDoubleWide ? 2 : 1; int xhigh = isDoubleHigh ? 2 : 1; VisBitmap8 vb = new VisBitmap8(SPRITE_BYTE_WIDTH * 8 * xwide, SPRITE_HEIGHT * xhigh); SetPalette(vb); vb.SetAllPixelIndices(TRANSPARENT); if (isMultiColor) { RenderMultiColorBitmap(offset, SPRITE_BYTE_WIDTH, SPRITE_HEIGHT, isDoubleWide, isDoubleHigh, color, color01, color11, vb, 0, 0); } else { RenderHiResBitmap(offset, SPRITE_BYTE_WIDTH, SPRITE_HEIGHT, isDoubleWide, isDoubleHigh, color, vb, 0, 0); } return(vb); }
private IVisualization2d GenerateFont(ReadOnlyDictionary <string, object> parms, bool isMultiColor) { int offset = Util.GetFromObjDict(parms, P_OFFSET, 0); int count = Util.GetFromObjDict(parms, P_COUNT, 96); if (offset < 0 || offset >= mFileData.Length) { mAppRef.ReportError("Invalid parameter"); return(null); } int lastOffset = offset + count - 1; if (lastOffset >= mFileData.Length) { mAppRef.ReportError("Font runs off end of file (last offset +" + lastOffset.ToString("x6") + ")"); return(null); } // Set the number of horizontal cells. For small counts we try to make it square, // for larger counts we use a reasonable power of 2. int hcells; if (count > 128) { hcells = 32; } else if (count > 64) { hcells = 16; } else if (count >= 32) { hcells = 8; } else { hcells = (int)Math.Sqrt(count + 1); } int vcells = (count + hcells - 1) / hcells; const int FONT_BYTE_WIDTH = 1; const int FONT_HEIGHT = 8; const int CELL_STRIDE = FONT_BYTE_WIDTH * FONT_HEIGHT; // Create a bitmap with room for each cell, plus a 1-pixel boundary // between them and around the edges. VisBitmap8 vb = new VisBitmap8(1 + hcells * FONT_BYTE_WIDTH * 8 + hcells, 1 + vcells * FONT_HEIGHT + vcells); SetPalette(vb); vb.SetAllPixelIndices(BORDER_COLOR); int cellX = 1; int cellY = 1; byte color = 0; /* black */ byte color01 = 11; /* dark grey */ byte color11 = 15; /* light grey */ for (int idx = 0; idx < count; idx++) { if (isMultiColor) { RenderMultiColorBitmap(offset + idx * CELL_STRIDE, FONT_BYTE_WIDTH, FONT_HEIGHT, false, false, color, color01, color11, vb, cellX, cellY); } else { RenderHiResBitmap(offset + idx * CELL_STRIDE, FONT_BYTE_WIDTH, FONT_HEIGHT, false, false, color, vb, cellX, cellY); } cellX += FONT_BYTE_WIDTH * 8 + 1; if (cellX == vb.Width) { cellX = 1; cellY += FONT_HEIGHT + 1; } } return(vb); }
private IVisualization2d GenerateSpriteGrid(ReadOnlyDictionary <string, object> parms, bool isMultiColor) { int offset = Util.GetFromObjDict(parms, P_OFFSET, 0); int count = Util.GetFromObjDict(parms, P_COUNT, 16); byte color = (byte)Util.GetFromObjDict(parms, P_COLOR, 0); bool isDoubleWide = Util.GetFromObjDict(parms, P_DOUBLE_WIDE, false); bool isDoubleHigh = Util.GetFromObjDict(parms, P_DOUBLE_HIGH, false); byte color01 = 0; byte color11 = 0; if (isMultiColor) { color01 = (byte)Util.GetFromObjDict(parms, P_COLOR_01, 0); color11 = (byte)Util.GetFromObjDict(parms, P_COLOR_11, 0); } if (offset < 0 || offset >= mFileData.Length || color < 0 || color > MAX_COLOR || color01 < 0 || color01 > MAX_COLOR || color11 < 0 || color11 > MAX_COLOR) { // the UI should flag these based on range (and ideally wouldn't have called us) mAppRef.ReportError("Invalid parameter"); return(null); } int lastOffset = offset + SPRITE_STRIDE * count - 1; if (lastOffset >= mFileData.Length) { mAppRef.ReportError("Sprite set runs off end of file (last offset +" + lastOffset.ToString("x6") + ")"); return(null); } int xwide = isDoubleWide ? 2 : 1; int xhigh = isDoubleHigh ? 2 : 1; // Try to make it square, unless there's a large number of them. Limit the width // to 16 sprites (384 pixels + padding). int hcells; if (count * xwide > 64) { hcells = 16 / xwide; } else if (count * xwide >= 32) { hcells = 8 / xwide; } else { hcells = (int)Math.Sqrt(count * xwide + 1); } int vcells = (count + hcells - 1) / hcells; VisBitmap8 vb = new VisBitmap8(1 + hcells * SPRITE_BYTE_WIDTH * 8 * xwide + hcells, 1 + vcells * SPRITE_HEIGHT * xhigh + vcells); SetPalette(vb); vb.SetAllPixelIndices(BORDER_COLOR); int cellX = 1; int cellY = 1; for (int idx = 0; idx < count; idx++) { if (isMultiColor) { RenderMultiColorBitmap(offset + idx * SPRITE_STRIDE, SPRITE_BYTE_WIDTH, SPRITE_HEIGHT, isDoubleWide, isDoubleHigh, color, color01, color11, vb, cellX, cellY); } else { RenderHiResBitmap(offset + idx * SPRITE_STRIDE, SPRITE_BYTE_WIDTH, SPRITE_HEIGHT, isDoubleWide, isDoubleHigh, color, vb, cellX, cellY); } cellX += SPRITE_BYTE_WIDTH * 8 * xwide + 1; if (cellX == vb.Width) { cellX = 1; cellY += SPRITE_HEIGHT * xhigh + 1; } } return(vb); }