예제 #1
0
        public static TextureAtlas FromGrid(Texture2D texture, int rows, int columns)
        {
            int tex_w = texture.Width;
            int tex_h = texture.Height;

            int tile_width  = tex_w / columns;
            int tile_height = tex_h / rows;

            var regions = new RectF[rows * columns];

            int index = 0;

            for (int i = 0; i < rows; ++i)
            {
                for (int j = 0; j < columns; ++j)
                {
                    float x = j * tile_width;
                    float y = i * tile_height;

                    regions[index++] = RectF.FromBox(x, y, tile_width, tile_height);
                }
            }

            return(new TextureAtlas(texture, regions));
        }
예제 #2
0
        public override void Load()
        {
            Engine.Canvas.StretchMode = CanvasStretchMode.LetterBox;

            left_view  = Engine.Canvas.CreateView();
            right_view = Engine.Canvas.CreateView();

            left_view.Viewport  = RectF.FromBox(0f, 0f, 0.495f, 1f);
            right_view.Viewport = RectF.FromBox(0.5f, 0f, 0.5f, 1f);

            left_view.SizeFactor  = new Vec2(0.5f, 1.0f);
            right_view.SizeFactor = new Vec2(0.5f, 1.0f);

            texture = Engine.Content.Get <Texture2D>("party");
            bg_tile = Engine.Content.Get <Texture2D>("purple_tile");

            bg_tile.Tiled = true;

            int canvas_w = Engine.Canvas.Width;
            int canvas_h = Engine.Canvas.Height;

            quad = new Quad(texture);

            quad.V0.X = canvas_w / 2 - texture.Width / 2;
            quad.V0.Y = canvas_h / 2 - texture.Height / 2;
            quad.V1.X = quad.V0.X + texture.Width;
            quad.V1.Y = quad.V0.Y;
            quad.V2.X = quad.V1.X;
            quad.V2.Y = quad.V0.Y + texture.Height;
            quad.V3.X = quad.V0.X;
            quad.V3.Y = quad.V2.Y;

            bg_quad = new Quad();

            bg_quad.V0.X   = 0;
            bg_quad.V0.Y   = 0;
            bg_quad.V0.Col = 0xFFFFFFFF;
            bg_quad.V1.X   = canvas_w;
            bg_quad.V1.Y   = 0;
            bg_quad.V1.Col = 0xFFFFFFFF;
            bg_quad.V2.X   = canvas_w;
            bg_quad.V2.Y   = canvas_h;
            bg_quad.V2.Col = 0xFFFFFFFF;
            bg_quad.V3.X   = 0;
            bg_quad.V3.Y   = canvas_h;
            bg_quad.V3.Col = 0xFFFFFFFF;

            bg_quad.V0.Tx = 0;
            bg_quad.V0.Ty = 0;
            bg_quad.V1.Tx = canvas_w / 64f;
            bg_quad.V1.Ty = 0;
            bg_quad.V2.Tx = canvas_w / 64f;
            bg_quad.V2.Ty = canvas_h / 64f;
            bg_quad.V3.Tx = 0;
            bg_quad.V3.Ty = canvas_h / 64f;
        }
예제 #3
0
        private TextureAtlas(Texture2D texture, RectF[] regions)
        {
            _texture = texture;

            this._quads = new Quad[regions.Length];

            for (int i = 0; i < regions.Length; ++i)
            {
                ref var region = ref regions[i];

                _quads[i] = new Quad(texture, RectF.FromBox(region.X1, region.Y1, region.Width, region.Height));
            }
예제 #4
0
        public override void Load()
        {
            m_render_target = RenderTarget.Create(320, 240, false);

            m_render_target_view = Engine.Canvas.CreateView(Color.Black);
            m_render_target_view.AbsoluteViewport = RectF.FromBox(0, 0, 320, 240);
            m_render_target_view.RenderTarget     = m_render_target;

            m_sprite = new Sprite(Engine.Content.Get <Texture2D>("party"));
            m_sprite.SetPosition(Engine.Canvas.Width / 2, Engine.Canvas.Height / 2);

            m_sprite_target = new Sprite(m_render_target.Texture);

            m_sprite_target.SetPosition(Engine.Canvas.Width / 2, Engine.Canvas.Height / 2);
        }
예제 #5
0
        public override void Load()
        {
            quad = new Quad(RectF.FromBox(
                                100f,
                                100f,
                                Engine.Canvas.Width - 200f,
                                Engine.Canvas.Height - 200f
                                ));

            quad.SetColors(
                color_top_left: Color.Red,
                color_top_right: Color.Orange,
                color_bottom_left: Color.PeachPuff,
                color_bottom_right: Color.GreenYellow
                );
        }
예제 #6
0
        public Quad(Texture2D texture, RectF src_rect = default, RectF dest_rect = default)
        {
            this.V0 = new Vertex2D();
            this.V1 = new Vertex2D();
            this.V2 = new Vertex2D();
            this.V3 = new Vertex2D();

            this.Blend = BlendMode.AlphaBlend;

            if (texture == null)
            {
                return;
            }

            float ax, ay, bx, by;

            float dest_x1, dest_y1, dest_x2, dest_y2;

            if (src_rect.IsEmpty)
            {
                src_rect = RectF.FromBox(0, 0, texture.Width, texture.Height);

                ax = 0;
                ay = 0;
                bx = 1;
                by = 1;
            }
            else
            {
                float inv_tex_w = 1.0f / texture.Width;
                float inv_tex_h = 1.0f / texture.Height;

                ax = src_rect.X1 * inv_tex_w;
                ay = src_rect.Y1 * inv_tex_h;
                bx = src_rect.X2 * inv_tex_w;
                by = src_rect.Y2 * inv_tex_h;
            }

            if (dest_rect.IsEmpty)
            {
                dest_x1 = 0;
                dest_y1 = 0;
                dest_x2 = src_rect.Width;
                dest_y2 = src_rect.Height;
            }
            else
            {
                dest_x1 = dest_rect.X1;
                dest_y1 = dest_rect.Y1;
                dest_x2 = dest_rect.X2;
                dest_y2 = dest_rect.Y2;
            }

            this.V0.X   = dest_x1;
            this.V0.Y   = dest_y1;
            this.V0.Tx  = ax;
            this.V0.Ty  = ay;
            this.V0.Col = 0xFFFFFFFF;

            this.V1.X   = dest_x2;
            this.V1.Y   = dest_y1;
            this.V1.Tx  = bx;
            this.V1.Ty  = ay;
            this.V1.Col = 0xFFFFFFFF;

            this.V2.X   = dest_x2;
            this.V2.Y   = dest_y2;
            this.V2.Tx  = bx;
            this.V2.Ty  = by;
            this.V2.Col = 0xFFFFFFFF;

            this.V3.X   = dest_x1;
            this.V3.Y   = dest_y2;
            this.V3.Tx  = ax;
            this.V3.Ty  = by;
            this.V3.Col = 0xFFFFFFFF;
        }
예제 #7
0
        public FontData LoadFontData(string descr_path, string image_path)
        {
            var sheet_data = LoadPixmapData(image_path);

            var glyphs        = new RectF[255];
            var pre_spacings  = new float[255];
            var post_spacings = new float[255];

            using (var descr_stream = File.OpenRead(descr_path))
            {
                using (var reader = new StreamReader(descr_stream, Encoding.UTF8))
                {
                    string line;
                    var    idx = 0;

                    while ((line = reader.ReadLine()) != null)
                    {
                        if (line.Length == 0)
                        {
                            continue;
                        }

                        if (idx == 0 && !line.Equals(FNT_HEADER_TAG))
                        {
                            throw new Exception("Invalid Font Description File.");
                        }

                        if (line.StartsWith(FNT_CHAR_TAG))
                        {
                            string char_def_str = line.Split('=')[1];

                            string[] char_def_attrs = char_def_str.Split(',');

                            if (char_def_attrs.Length != 7)
                            {
                                throw new Exception(
                                          $"Invalid Font Description File: Invalid Char Definition at line: {line + 1}");
                            }

                            int ch_idx = int.Parse(char_def_attrs[0]);

                            if (ch_idx < 0 || ch_idx > 255)
                            {
                                throw new Exception("Invalid Font Description File: Character Id out of range");
                            }

                            int letter_reg_x     = int.Parse(char_def_attrs[1]);
                            int letter_reg_y     = int.Parse(char_def_attrs[2]);
                            int letter_reg_w     = int.Parse(char_def_attrs[3]);
                            int letter_reg_h     = int.Parse(char_def_attrs[4]);
                            int letter_pre_spac  = int.Parse(char_def_attrs[5]);
                            int letter_post_spac = int.Parse(char_def_attrs[6]);

                            glyphs[ch_idx] = RectF.FromBox(letter_reg_x, letter_reg_y, letter_reg_w,
                                                           letter_reg_h);

                            pre_spacings[ch_idx]  = letter_pre_spac;
                            post_spacings[ch_idx] = letter_post_spac;
                        }

                        idx++;
                    }
                }
            }

            var id = Path.GetFileNameWithoutExtension(descr_path);

            var font_data = new FontData()
            {
                FontSheet    = sheet_data,
                GlyphRects   = glyphs,
                Id           = id,
                PreSpacings  = pre_spacings,
                PostSpacings = post_spacings
            };

            return(font_data);
        }