Beispiel #1
0
        private void GamePanel_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            long start = DateTime.Now.Ticks;

            g.DrawImageUnscaled(SpriteCache["board"], 0, 0, 620, 620);

            foreach (CBack.Pieces.Piece pcs in Game.Table)
            {
                if (pcs == null)
                {
                    continue;
                }

                string key = (int)pcs.Color + "_" + (int)pcs.Type;
                if (!SpriteCache.ContainsKey(key))
                {
                    //Console.WriteLine($"Sprite not found for piece({pcs}). Expected filename \"70px_{key}.png\"");
                    continue;
                }
                if (pcs.Equals(Game.SelectedPiece))
                {
                    if (dragging)
                    {
                        // TODO: draw the piece at the cursor;
                        g.DrawImage(SpriteCache[key], new Rectangle(cursorOffs.X, cursorOffs.Y, pcsSize, pcsSize));


                        continue;
                    }
                }

                // (image, destRect, srcRect);
                g.DrawImage(SpriteCache[key],
                            new Rectangle(boardEdge + 5 + CalcXFromCol(pcs.Column), boardEdge + 5 + CalcYFromRow(pcs.Row), pcsSize, pcsSize));
            }

            for (int i = 0; i < Game.TableStatus.Length; ++i)
            {
                //Console.Write(" " + Game.TableStatus[i]);
                //if (i % 8 == 7)
                //Console.WriteLine();

                if (Game.IsFlagSetAtCell(i, CBack.CellStatus.LastMove))
                {
                    // TODO: draw yellow curved rect;
                    g.DrawRectangle(yellowPen, boardEdge + CalcXFromIndex(i) + offs, boardEdge + CalcYFromIndex(i) + offs, 70 - offs - offs, 70 - offs - offs);
                }
                if (Game.IsFlagSetAtCell(i, CBack.CellStatus.Targetable))
                {
                    // TODO: draw red curved rect;
                    g.DrawRectangle(darkRedPen, boardEdge + CalcXFromIndex(i) + offs, boardEdge + CalcYFromIndex(i) + offs, 70 - offs - offs, 70 - offs - offs);
                }
                if (Game.IsFlagSetAtCell(i, CBack.CellStatus.Movable))
                {
                    // TODO: draw cyan curved rect;
                    g.DrawRectangle(cyanPen, boardEdge + CalcXFromIndex(i) + offs, boardEdge + CalcYFromIndex(i) + offs, 70 - offs - offs, 70 - offs - offs);
                }
                if (Game.IsFlagSetAtCell(i, CBack.CellStatus.Checking))
                {
                    g.DrawRectangle(redPen, boardEdge + CalcXFromIndex(i) + offs, boardEdge + CalcYFromIndex(i) + offs, 70 - offs - offs, 70 - offs - offs);
                }
                if (Game.IsFlagSetAtCell(i, CBack.CellStatus.Selecting))
                {
                    g.FillRectangle(overlayBrush, boardEdge + CalcXFromIndex(i) + offs, boardEdge + CalcYFromIndex(i) + offs, 70 - offs - offs, 70 - offs - offs);
                }
            }

            int r = CalcRowFromY(cursorOffs.Y);
            int c = CalcColFromX(cursorOffs.X);

            //Console.WriteLine("r = " + r + " c = " + c);
            if (r >= 0 && r < CBack.Game.ColumnSize && c >= 0 && c < CBack.Game.RowSize)
            {
                g.FillRectangle(overlayBrush, boardEdge + CalcXFromCol(c), boardEdge + CalcYFromRow(r), 70, 70);
            }

            long     end = DateTime.Now.Ticks;
            TimeSpan ts  = new TimeSpan(end - start);

            if (ts.TotalMilliseconds < minFrametime)
            {
                minFrametime = (float)ts.TotalMilliseconds;
            }
            if (ts.TotalMilliseconds > maxFrametime)
            {
                maxFrametime = (float)ts.TotalMilliseconds;
            }

            //Console.WriteLine($"Last Frametime: {ts.TotalMilliseconds:F}ms (min {minFrametime:F}ms, max {maxFrametime:F}ms).");
            Form1 f = Parent as Form1;

            f.lblLog.Text = $"Last Frametime: {ts.TotalMilliseconds:F}ms (min {minFrametime:F}ms, max {maxFrametime:F}ms).";
        }
Beispiel #2
0
        /**
            layout, materialize時に画像を読み込む。
            キャッシュヒット処理込み。
         */
        public IEnumerator<Sprite> LoadImageAsync(string uriSource)
        {
            while (spriteDownloadingUris.Contains(uriSource))
            {
                yield return null;
            }

            if (spriteCache.ContainsKey(uriSource))
            {
                yield return spriteCache[uriSource];
            }
            else
            {
                // start downloading.
                spriteDownloadingUris.Add(uriSource);
                {
                    /*
                        supported schemes are,
                            
                            ^http://		http scheme => load asset from web.
                            ^https://		https scheme => load asset from web.
                            ^./             relative path => load asset from web.
                            ^assetbundle://	assetbundle scheme => load asset from assetBundle.
                            ^resources://   resources scheme => (Resources/)somewhere/resource path.
                    */
                    var schemeAndPath = uriSource.Split(new char[] { '/' }, 2);
                    var scheme = schemeAndPath[0];

                    IEnumerator<Sprite> cor = null;
                    switch (scheme)
                    {
                        case "assetbundle:":
                            {
                                cor = LoadImageFromAssetBundle(uriSource);
                                break;
                            }
                        case "https:":
                        case "http:":
                            {
                                cor = LoadImageFromWeb(uriSource);
                                break;
                            }
                        case ".":
                            {
                                if (uriSource[1] != '/')
                                {
                                    throw new Exception("unsupported scheme:" + scheme);
                                }

                                switch (basePath)
                                {
                                    case "":
                                        {
                                            var modifiedUriSource = uriSource.Substring(2);
                                            cor = LoadImageFromResources(modifiedUriSource);
                                            break;
                                        }
                                    default:
                                        {
                                            if (string.IsNullOrEmpty(basePath))
                                            {
                                                throw new Exception("unknown error, basePath is empty.");
                                            }

                                            var modifiedUriSource = basePath + "/" + uriSource.Substring(2);
                                            cor = LoadImageFromWeb(modifiedUriSource);
                                            break;
                                        }
                                }
                                break;
                            }
                        case "resources:":
                            {
                                var resourcePath = uriSource.Substring("resources:".Length + 2);
                                cor = LoadImageFromResources(resourcePath);
                                break;
                            }
                        default:
                            {// other.
                                throw new Exception("unsupported scheme:" + scheme);
                            }
                    }

                    while (cor.MoveNext())
                    {
                        if (cor.Current != null)
                        {
                            // set cache.
                            spriteCache[uriSource] = cor.Current;
                            break;
                        }
                        yield return null;
                    }

                    if (cor.Current == null)
                    {
                        // failed to get image.
                        spriteDownloadingUris.Remove(uriSource);
                        yield break;
                    }

                    spriteDownloadingUris.Remove(uriSource);
                    yield return spriteCache[uriSource];
                }
            }
        }