private void DrawBatchString(float tx, float ty, string text, LColor c, int startIndex, int endIndex) { if (isClose) { return; } if (displays.Size() > DEFAULT_MAX_CHAR) { displays.Clear(); } lazyHashCode = 1; if (c != null) { lazyHashCode = LSystem.Unite(lazyHashCode, c.r); lazyHashCode = LSystem.Unite(lazyHashCode, c.g); lazyHashCode = LSystem.Unite(lazyHashCode, c.b); lazyHashCode = LSystem.Unite(lazyHashCode, c.a); } string key = text + lazyHashCode; Display display = (Display)displays.Get(key); if (display == null) { int x = 0, y = 0; displayList.GLBegin(); displayList.SetBatchPos(tx, ty); if (c != null) { displayList.SetImageColor(c); } CharDef lastCharDef = null; char[] data = text.ToCharArray(); for (int i = 0; i < data.Length; i++) { int id = data[i]; if (id == '\n') { x = 0; y += GetLineHeight(); continue; } if (id >= chars.Length) { continue; } CharDef charDef = chars[id]; if (charDef == null) { continue; } if (lastCharDef != null) { x += lastCharDef.GetKerning(id); } lastCharDef = charDef; if ((i >= startIndex) && (i <= endIndex)) { charDef.Draw(x, y); } x += charDef.advance; } if (c != null) { displayList.SetImageColor(LColor.white); } displayList.GLEnd(); display = new Display(); display.cache = displayList.NewBatchCache(); display.text = text; display.width = 0; display.height = 0; displays.Put(key, display); } else if (display.cache != null) { display.cache.x = tx; display.cache.y = ty; LTextureBatch.Commit(displayList, display.cache); } }
/// <summary> /// 渲染当前层画面到LGraphics之上 /// </summary> /// /// <param name="g"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="sx"></param> /// <param name="sy"></param> /// <param name="width"></param> /// <param name="ty"></param> /// <param name="isLine"></param> /// <param name="mapTileWidth"></param> /// <param name="mapTileHeight"></param> public void Draw(GLEx g, int x, int y, int sx, int sy, int width, int height, bool isLine, int mapTileWidth, int mapTileHeight) { if (width == 0 || height == 0) { return; } if (lightingOn) { GLUtils.SetShadeModelSmooth(GLEx.GL); } this.tmxTileSet = null; this.mapTileSet = null; for (int tileset = 0; tileset < tmx.GetTileSetCount(); tileset++) { keyHashCode = 1; keyHashCode = LSystem.Unite(keyHashCode, tileset); keyHashCode = LSystem.Unite(keyHashCode, sx); keyHashCode = LSystem.Unite(keyHashCode, sy); keyHashCode = LSystem.Unite(keyHashCode, width); keyHashCode = LSystem.Unite(keyHashCode, height); keyHashCode = LSystem.Unite(keyHashCode, mapTileWidth); keyHashCode = LSystem.Unite(keyHashCode, mapTileHeight); keyHashCode = LSystem.Unite(keyHashCode, lightingOn); mapTileSet = (MapTileSet)CollectionUtils.Get(lazyMaps, keyHashCode); if (!isLightDirty && mapTileSet != null) { mapTileSet.cache.x = x; mapTileSet.cache.y = y; LTextureBatch.Commit(mapTileSet.texture, mapTileSet.cache); if (isLine) { tmx.Draw(g, x, y, sx, sy, width, height, index); } if (lightingOn) { GLUtils.SetShadeModelSmooth(GLEx.GL); } return; } for (int ty = 0; ty < height; ty++) { for (int tx = 0; tx < width; tx++) { if ((sx + tx < 0) || (sy + ty < 0)) { continue; } if ((sx + tx >= this.width) || (sy + ty >= this.height)) { continue; } if (data[sx + tx, sy + ty, 0] == tileset) { if (tmxTileSet == null) { tmxTileSet = tmx.GetTileSet(tileset); tmxTileSet.tiles.GLBegin(); } int sheetX = tmxTileSet .GetTileX(data[sx + tx, sy + ty, 1]); int sheetY = tmxTileSet .GetTileY(data[sx + tx, sy + ty, 1]); int tileOffsetY = tmxTileSet.tileHeight - mapTileHeight; cx = tx * mapTileWidth; cy = ty * mapTileHeight - tileOffsetY; if (lightingOn) { SetLightColor(cx / mapTileWidth, cy / mapTileHeight); } tmxTileSet.tiles .Draw(g, cx, cy, sheetX, sheetY, colors); } } } if (tmxTileSet != null) { tmxTileSet.tiles.GLEnd(); if (mapTileSet == null) { mapTileSet = new TMXLayer.MapTileSet(); } else { mapTileSet.texture = null; mapTileSet.cache.Dispose(); mapTileSet.cache = null; } mapTileSet.texture = tmxTileSet.tiles.GetTarget(); mapTileSet.cache = tmxTileSet.tiles.NewCache(); mapTileSet.cache.x = x; mapTileSet.cache.y = y; CollectionUtils.Put(lazyMaps, keyHashCode, mapTileSet); if (lightingOn) { GLUtils.SetShadeModelFlat(GLEx.GL); } if (isLine) { tmx.Draw(g, x, y, sx, sy, width, height, index); } isLightDirty = false; tmxTileSet = null; } } }
public virtual void PaintObjects(GLEx g, int minX, int minY, int maxX, int maxY) { lock (objects) { IIterator it = objects.Iterator(); for (; it.HasNext();) { thing = (Actor)it.Next(); if (!thing.visible) { continue; } isListener = (thing.actorListener != null); if (isVSync) { if (isListener) { thing.actorListener.Update(elapsedTime); } thing.Update(elapsedTime); } RectBox rect = thing.GetRectBox(); actorX = minX + thing.GetX(); actorY = minY + thing.GetY(); actorWidth = rect.width; actorHeight = rect.height; if (actorX + actorWidth < minX || actorX > maxX || actorY + actorHeight < minY || actorY > maxY) { continue; } LTexture actorImage = thing.GetImage(); if (actorImage != null) { width = (actorImage.GetWidth() * thing.scaleX); height = (actorImage.GetHeight() * thing.scaleY); isBitmapFilter = (thing.filterColor != null); thing.SetLastPaintSeqNum(paintSeq++); angle = thing.GetRotation(); colorAlpha = thing.alpha; if (thing.isAnimation) { if (isBitmapFilter) { g.DrawTexture(actorImage, actorX, actorY, width, height, angle, thing.filterColor); } else { if (colorAlpha != 1f) { g.SetAlpha(colorAlpha); } g.DrawTexture(actorImage, actorX, actorY, width, height, angle); if (colorAlpha != 1f) { g.SetAlpha(1f); } } } else { int texId = actorImage.GetTextureID(); LTextureBatch batch = (LTextureBatch)textures .GetValue(texId); if (batch == null) { LTextureBatch pBatch = LTextureBatch .BindBatchCache(this, texId, actorImage); batch = pBatch; batch.GLBegin(); textures.Put(texId, batch); } batch.SetTexture(actorImage); if (isBitmapFilter) { batch.Draw(actorX, actorY, width, height, angle, thing.filterColor); } else { if (colorAlpha != 1f) { alphaColor.a = colorAlpha; } batch.Draw(actorX, actorY, width, height, angle, alphaColor); if (colorAlpha != 1f) { alphaColor.a = 1; } } } } if (thing.isConsumerDrawing) { if (actorX == 0 && actorY == 0) { thing.Draw(g); if (isListener) { thing.actorListener.Draw(g); } } else { g.Translate(actorX, actorY); thing.Draw(g); if (isListener) { thing.actorListener.Draw(g); } g.Translate(-actorX, -actorY); } } } int size = textures.Size(); if (size > 0) { for (int i = 0; i < size; i++) { LTextureBatch batch = (LTextureBatch)textures.Get(i); batch.GLEnd(); } textures.Clear(); } } }