Esempio n. 1
0
        /// <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;
                }
            }
        }
Esempio n. 2
0
        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);
            }
        }