public unsafe void Run()
        {
            var timer = new Timer();
            timer.start();

            // for fps info update
            int lastTick = timer.read_ms();

            string infoString = "";

            SDCardManager.Mount();

            _background = new Bitmap("BACK.DAT", 480, 272);
            _sprite = new Bitmap("SPRITE.DAT", 64, 64);
            _font = Font.LoadFromFile("DEJAVU.FNT");

            // create double buffered display
            var canvas = new Canvas();

            var sprites = new List<Sprite>();

            var r = new Random();

            for (int i = 0; i < 20; i++)
            {
                sprites.Add(new Sprite
                {
                    X = r.Next(480 - 64),
                    Y = r.Next(272 - 64),
                    Width = (int)_sprite.Width,
                    Height = (int)_sprite.Height,
                    Dx = r.Next(2) == 0 ? 2 : -2,
                    Dy = r.Next(2) == 0 ? 2 : -2
                });
            }

            while (true)
            {
                canvas.Clear(_background);

                foreach (var sprite in sprites)
                {
                    canvas.DrawBitmap(_sprite, 0, 0, sprite.X, sprite.Y, 64, 64);
                    sprite.Step(Canvas.ScreenWidth, Canvas.ScreenHeight);
                }

                // show info every couple of seconds
                if (timer.read_ms() - lastTick > 2000)
                {
                    // string.format broken?
                    //infoString = String.Format("FPS: {0} MEMAVAIL: {1} MEMALOC: {2}",
                    //    display.Fps,
                    //    Microsoft.Zelig.Runtime.MemoryManager.Instance.AvailableMemory,
                    //    Microsoft.Zelig.Runtime.MemoryManager.Instance.AllocatedMemory);

                    infoString = "FPS: " + canvas.Fps.ToString() + " MEMAVAIL: " + Microsoft.Zelig.Runtime.MemoryManager.Instance.AvailableMemory.ToString();

                    lastTick = timer.read_ms();
                }

                canvas.DrawString(infoString, 0, 0, _font);

                // show the back buffer
                canvas.Flip();
            }
        }
        public void Run(Control root)
        {
            SDCardManager.Mount();

            // load in the system font
            _systemFont = Font.LoadFromFile("DEJAVU.FNT");

            var timer = new Timer();
            timer.start();

            // for fps info update
            float lastDebugTime = 0;
            float lastUpdateTime = 0;

            string infoString = "";

            while (true)
            {
                FrameTime = timer.read();

                // fixed time step - both for update and draw
                if (FrameTime - lastUpdateTime >= DeltaTime)
                {
                    int touches = Touch.GetTouchInfo();

                    for (int i = 0; i < touches; i++)
                    {
                        // near a touch we have seen recently ?
                        var previous = _activeTouches.Find(x => Math.Abs(x.Position.X - Touch.X[i]) < TouchTrackTolerance &&
                                                                Math.Abs(x.Position.Y - Touch.Y[i]) < TouchTrackTolerance);

                        if (previous == null)
                        {
                            _nextTouchId++;

                            // remember this touch
                            _activeTouches.Add(new TouchInfo { Position = new Point((int)Touch.X[i], (int)Touch.Y[i]), LastSeenTime = FrameTime, Id = _nextTouchId });

                            // new touch so send message to root control
                            root.SendMessage(UIMessage.TouchStart, new TouchEventArgs(_nextTouchId, new Point((int)Touch.X[i], (int)Touch.Y[i])));
                        }
                        else
                        {
                            bool hasMoved = (Math.Abs(previous.Position.X - Touch.X[i]) > 0 || Math.Abs(previous.Position.Y - Touch.Y[i]) > 0);

                            // touch seen again - update for tracking
                            previous.Position = new Point((int)Touch.X[i], (int)Touch.Y[i]);
                            previous.LastSeenTime = FrameTime;

                            // moved at all ?
                            if (hasMoved)
                            {
                                root.SendMessage(UIMessage.TouchMove, new TouchEventArgs(previous.Id, previous.Position));
                            }
                        }
                    }

                    // get rid of old touches
                    for (int i = _activeTouches.Count - 1; i >= 0; i--)
                    {
                        if (FrameTime - _activeTouches[i].LastSeenTime > 0.1f)
                        {
                            root.SendMessage(UIMessage.TouchEnd, new TouchEventArgs(_activeTouches[i].Id, _activeTouches[i].Position));

                            _activeTouches.RemoveAt(i);
                        }
                    }

                    lastUpdateTime = FrameTime;

                    Display.Clear(0xFFFFFFFF);

                    root.Update(DeltaTime);
                    root.Draw();

                    if (ShowDebug)
                    {
                        // show debug info every couple of seconds
                        if (timer.read_ms() - lastDebugTime > 2.0f)
                        {
                            // string.format broken?
                            //infoString = String.Format("FPS: {0} MEMAVAIL: {1} MEMALOC: {2}",
                            //    Display.Fps,
                            //    Microsoft.Zelig.Runtime.MemoryManager.Instance.AvailableMemory,
                            //    Microsoft.Zelig.Runtime.MemoryManager.Instance.AllocatedMemory);

                            infoString = "FPS: " + Display.Fps.ToString() + " MEMAVAIL: " + Microsoft.Zelig.Runtime.MemoryManager.Instance.AvailableMemory.ToString();

                            lastDebugTime = timer.read();
                        }

                        //Display.DrawString(infoString, 0, 0);
                    }

                    // show the back buffer
                    Display.Flip();
                }
            }
        }
        /// <summary>
        /// Calculate size of string
        /// </summary>
        /// <param name="text">text to calculate size of</param>
        /// <param name="font">font to use to calculate size</param>
        /// <returns>size of string</returns>
        public Size MeasureString(string text, Font font)
        {
            int maxWidth = 0, maxHeight = 0;

            Font.Character fontCharacter;

            int spacingFromPrevious;

            for (int i = 0; i < text.Length; i++)
            {
                if (i == 0)
                {
                    // first chracter no spacing
                    spacingFromPrevious = 0;
                }
                //else if (font.Kernings.TryGetValue(new Font.Kerning(text[i - 1], text[i]), out spacingFromPrevious))
                else
                {
                    // no kerning found - default to 1 pixel
                    spacingFromPrevious = 1;
                }

                maxWidth += spacingFromPrevious;

                if (font.Characters.TryGetValue(text[i], out fontCharacter))
                {
                    maxWidth += fontCharacter.Rect.Width;
                    maxHeight = Math.Max(maxHeight, fontCharacter.Offset.Y + fontCharacter.Rect.Height);
                }
            }

            return new Size(maxWidth, maxHeight);
        }
        /// <summary>
        /// Draw a text string 
        /// </summary>
        /// <param name="text">text string to draw</param>
        /// <param name="x">x position to draw</param>
        /// <param name="y">y position to draw</param>
        /// <param name="font">font to draw text with</param>
        public void DrawString(string text, int x, int y, Font font)
        {
            Font.Character fontCharacter;
            Font.Page fontPage;
            int spacingFromPrevious;

            for (int i = 0; i < text.Length; i++)
            {
                if (i == 0)
                {
                    // first chracter no spacing
                    spacingFromPrevious = 0;
                }
                //else if (font.Kernings.TryGetValue(new Font.Kerning(text[i - 1], text[i]), out spacingFromPrevious))
                else
                {
                    // no kerning found - default to 1 pixel
                    spacingFromPrevious = 1;
                }

                // adjust the spacing to draw the character
                x += spacingFromPrevious;

                // character found ?
                if (font.Characters.TryGetValue(text[i], out fontCharacter))
                {
                    if (font.Pages.TryGetValue(fontCharacter.Page, out fontPage))
                    {
                        DrawBitmap(fontPage.Texture,
                            fontCharacter.Rect.X, fontCharacter.Rect.Y, x, y + fontCharacter.Offset.Y,
                            fontCharacter.Rect.Width, fontCharacter.Rect.Height);
                    }

                    x += fontCharacter.Rect.Width;
                }
            }
        }
 public void DrawCenteredString(string text, int x, int y, int width, int height, Font font)
 {
     Size size = MeasureString(text, font);
     DrawString(text, x + (width / 2) - (size.Width / 2), y + (height / 2) - (size.Height / 2), font);
 }
Esempio n. 6
0
        /// <summary>
        /// Load in font template file
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns>loaded font</returns>
        public static Font LoadFromFile(string fileName)
        {
            Font font = new Font();

            Dictionary<int, Page> pages = new Dictionary<int, Page>();
            Dictionary<char, Character> characters = new Dictionary<char, Character>();
            //Dictionary<Kerning, int> kernings = new Dictionary<Kerning, int>();

            byte[] data = SDCardManager.ReadAllBytes(fileName);

            int i = 0;
            int k;
            int l = 0;

            char[] lineChars = new char[255];

            string line;

            int commonWidth = 0, commonHeight = 0;

            while (i < data.Length)
            {
                k = i;

                l = 0;

                while (k < data.Length && data[k] != 10)
                {
                    if (data[k] != 10 && data[k] != 13)
                    {
                        lineChars[l] = (char)data[k];
                        l++;
                    }
                    k++;
                }

                line = new string(lineChars, 0, l);

                i = k + 1;

                string[] items = line.Split(' ');

                if (items.Length != 0)
                {
                    switch (items[0])
                    {
                        case "common":
                            commonWidth = GetInt(items, "scaleW");
                            commonHeight = GetInt(items, "scaleH");
                            break;
                        case "page":
                            int id = GetInt(items, "id");
                            string file = GetString(items, "file").Trim('"');
                            Bitmap texture = new Bitmap(file, commonWidth, commonHeight);
                            pages.Add(id, new Page(id, file, texture));
                            break;
                        case "char":
                            var character = new Character
                            {
                                Id = (char)GetInt(items, "id"),
                                Rect = new Rect(GetInt(items, "x"), GetInt(items, "y"), GetInt(items, "width"), GetInt(items, "height")),
                                Offset = new Point(GetInt(items, "xoffset"), GetInt(items, "yoffset")),
                                XAdvance = GetInt(items, "xadvance"),
                                Page = GetInt(items, "page"),
                                Channel = GetInt(items, "chnl")
                            };
                            characters.Add(character.Id, character);
                            break;
                        /*case "kerning":
                            var kerning = new Kerning
                            {
                                First = (char)GetInt(items, "first"),
                                Second = (char)GetInt(items, "second")
                            };
                            kernings.Add(kerning, GetInt(items, "amount"));
                            break;*/

                    }
                }
            }

            font.Pages = pages;
            font.Characters = characters;
            //font.Kernings = kernings;

            return font;
        }