コード例 #1
0
        private void btnLoadSprite_Click(object sender, EventArgs e)
        {
            if (openFile.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            Sprite spTest   = null;
            string filename = openFile.FileName;

            try
            {
                spTest = new Sprite(filename,
                                    int.Parse(txtWidth.Text), int.Parse(txtHeight.Text));
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.GetType().Name);
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }

            if (spTest != null)
            {
                SetSprite(spTest);
            }
            else
            {
                // since loading the sprite from the file failed, try it as a resource file.
                AgateResourceCollection resources = AgateResourceLoader.LoadResources(filename);

                AgateFileProvider.Images.Clear();
                AgateFileProvider.Images.AddPath(System.IO.Path.GetDirectoryName(filename));

                if (resources.Sprites.ToArray().Length == 1)
                {
                    var sprites = resources.Sprites.ToArray();

                    Sprite sp = new Sprite(resources, sprites[0].Name);

                    SetSprite(sp);
                }
                else
                {
                    frmChooseSprite frm = new frmChooseSprite();

                    if (frm.ShowDialog(this, resources) == DialogResult.OK)
                    {
                        Sprite sp = new Sprite(resources, frm.SelectedSprite);

                        SetSprite(sp);
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Constructs a sprite from a resource.
        /// </summary>
        /// <param name="resources"></param>
        /// <param name="name"></param>
        public Sprite(AgateResourceCollection resources, string name)
        {
            AgateResource  generic_res = resources[name];
            SpriteResource sprite_res  = generic_res as SpriteResource;

            if (sprite_res == null)
            {
                throw new AgateResourceException("Resource " + generic_res.Name + " is not a sprite.");
            }

            BuildSpriteFromResource(resources, resources.RootDirectory, sprite_res);
        }
コード例 #3
0
        /// <summary>
        /// Returns true if the file was saved and an existing operation should
        /// continue.
        /// </summary>
        private bool SaveAs(AgateResourceCollection resources)
        {
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                mFilename = saveFileDialog.FileName;

                return(Save(resources));
            }
            else
            {
                return(false);
            }
        }
コード例 #4
0
        public DialogResult ShowDialog(IWin32Window owner, AgateResourceCollection resources)
        {
            this.resources = resources;

            lstSprites.Items.Clear();

            foreach (var sprite in resources.Sprites)
            {
                lstSprites.Items.Add(sprite.Name);
            }

            return(ShowDialog(owner));
        }
コード例 #5
0
        /// <summary>
        /// Returns true if the file was saved and an existing operation should
        /// continue.
        /// </summary>
        /// <returns></returns>
        private bool Save(AgateResourceCollection resources)
        {
            try
            {
                AgateResourceLoader.SaveResources(resources, mFilename);

                return(true);
            }
            catch (IOException e)
            {
                MessageBox.Show("An error has occurred:" + Environment.NewLine +
                                e.Message);

                return(false);
            }
        }
コード例 #6
0
        /// <summary>
        /// Constructs a FontSurface object from a resource.
        /// </summary>
        /// <param name="resources"></param>
        /// <param name="resourceName"></param>
        public FontSurface(AgateResourceCollection resources, string resourceName)
        {
            AgateResource      res     = resources[resourceName];
            BitmapFontResource bmpFont = res as BitmapFontResource;

            if (res is BitmapFontResource)
            {
                Surface surf = new Surface(bmpFont.Image);

                impl = new BitmapFontImpl(surf, bmpFont.FontMetrics);
            }
            else
            {
                throw new AgateResourceException(string.Format(
                                                     "The resource {0} is of type {1} which cannot be used to construct a font.",
                                                     resourceName, res.GetType().Name));
            }
        }
コード例 #7
0
        private void New()
        {
            if (CloseFile() == false)
            {
                return;
            }

            AgateResourceCollection newRes = new AgateResourceCollection();

            if (SaveAs(newRes) == false)
            {
                return;
            }

            Resources = newRes;

            UpdateControls();
        }
コード例 #8
0
        private void AddFramesFromGrid(AgateResourceCollection resources, AgateLib.ImplementationBase.SurfaceImpl thisImpl, SpriteResource.SpriteImageResource.Grid grid)
        {
            Point location = grid.Location;

            for (int y = 0; y < grid.Array.Height; y++)
            {
                for (int x = 0; x < grid.Array.Width; x++)
                {
                    var surfImpl = thisImpl.CarveSubSurface(new Rectangle(location, grid.Size));
                    AddFrame(new Surface(surfImpl), false, new Rectangle(0, 0, grid.Size.Width, grid.Size.Height), Point.Empty);

                    location.X += grid.Size.Width;
                }

                location.Y += grid.Size.Height;
                location.X  = grid.Location.X;
            }
        }
コード例 #9
0
        internal void SaveFont(string resourceFile, string fontName, string imageFile)
        {
            AgateResourceCollection resources;

            if (File.Exists(resourceFile))
            {
                resources = AgateResourceLoader.LoadResources(resourceFile);
            }
            else
            {
                resources = new AgateResourceCollection();
            }

            if (Path.IsPathRooted(resourceFile) == false)
            {
                resourceFile = Path.Combine(Directory.GetCurrentDirectory(), resourceFile);
            }

            string localImagePath;
            string dir = Path.GetDirectoryName(resourceFile);

            if (Path.IsPathRooted(imageFile) == false)
            {
                localImagePath = imageFile;
                imageFile      = Path.Combine(Path.GetDirectoryName(resourceFile), imageFile);
            }
            else
            {
                localImagePath = GetRelativePath(dir, imageFile);
            }

            SaveImage(imageFile);

            localImagePath = localImagePath.Replace(Path.DirectorySeparatorChar.ToString(), "/");

            BitmapFontResource res = new BitmapFontResource(fontName);

            res.Image       = localImagePath;
            res.FontMetrics = ((BitmapFontImpl)Font.Impl).FontMetrics.Clone();

            resources.Add(res);

            AgateResourceLoader.SaveResources(resources, resourceFile);
        }
コード例 #10
0
        public void Main(string[] args)
        {
            using (AgateSetup setup = new AgateSetup("Resource Tester", args))
            {
                setup.InitializeAll();
                if (setup.WasCanceled)
                {
                    return;
                }

                AgateFileProvider.Resources.Add(new AgateLib.Utility.FileSystemProvider("Data"));

                AgateResourceCollection resources = new AgateResourceCollection("TestResourceFile.xml");

                DisplayWindow wind   = new DisplayWindow(resources, "main_window");
                Surface       surf   = new Surface(resources, "sample_surf");
                ISprite       sprite = new Sprite(resources, "sample_sprite");
                FontSurface   font   = new FontSurface(resources, "sample_font");

                sprite.StartAnimation();

                while (wind.IsClosed == false)
                {
                    Display.BeginFrame();
                    Display.Clear(Color.Red);

                    font.DrawText(0, 0, "FPS: " + Display.FramesPerSecond.ToString());

                    surf.Draw(20, 20);

                    sprite.Update();
                    sprite.Draw(100, 100);

                    Display.EndFrame();
                    Core.KeepAlive();
                }
            }
        }
コード例 #11
0
        private void BuildSpriteFromResource(AgateResourceCollection resources,
                                             string root, SpriteResource resource)
        {
            Surface defaultSurface = null;

            if (string.IsNullOrEmpty(resource.Filename) == false)
            {
                defaultSurface = new Surface(resources.LoadSurfaceImpl(resource.Filename));
                mOwnedSurfaces.Add(defaultSurface);
            }

            if (resource.HasSize)
            {
                mSpriteSize = resource.Size;
            }
            else if (defaultSurface != null)
            {
                mSpriteSize = defaultSurface.SurfaceSize;
            }

            for (int i = 0; i < resource.ChildElements.Count; i++)
            {
                SpriteResource.SpriteSubResource child = resource.ChildElements[i];
                Surface thisSurface = defaultSurface;

                if (child is SpriteResource.SpriteFrameResource)
                {
                    SpriteResource.SpriteFrameResource frame = (SpriteResource.SpriteFrameResource)child;
                    if (string.IsNullOrEmpty(frame.Filename) == false)
                    {
                        thisSurface = new Surface(resources.LoadSurfaceImpl(frame.Filename));
                        mOwnedSurfaces.Add(thisSurface);

                        if (i == 0 && defaultSurface == null && resource.HasSize == false)
                        {
                            mSpriteSize = thisSurface.SurfaceSize;
                        }
                    }
                    if (thisSurface == null)
                    {
                        throw new AgateException(string.Format(
                                                     "The surface to create the sprite from in resource {0} was not specified.", resource.Name));
                    }

                    // we pass false to ownSurface here because the surface has already been added to the
                    // owned surfaces list.
                    AddFrame(thisSurface, false, frame.Bounds, frame.Offset);
                }
                else
                {
                    var image = (SpriteResource.SpriteImageResource)child;

                    ImplementationBase.SurfaceImpl thisImpl = resources.LoadSurfaceImpl(image.Filename);
                    if (i == 0 && defaultSurface == null && resource.HasSize == false)
                    {
                        mSpriteSize = thisImpl.SurfaceSize;
                    }

                    if (image.Grids.Count == 0)
                    {
                        AddFrame(new Surface(thisImpl), false,
                                 new Rectangle(0, 0, mSpriteSize.Width, mSpriteSize.Height),
                                 Point.Empty);
                    }
                    else
                    {
                        for (int j = 0; j < image.Grids.Count; j++)
                        {
                            AddFramesFromGrid(resources, thisImpl, image.Grids[j]);
                        }
                    }
                }
            }
        }
コード例 #12
0
        public DialogResult ShowDialog(IWin32Window parent, AgateResourceCollection resources)
        {
            this.resources = resources;

            return(ShowDialog(parent));
        }