コード例 #1
0
ファイル: PlanesSample.cs プロジェクト: HaKDMoDz/Samurai
        public PlanesSample()
            : base()
        {
            this.Window.Title = "Samurai Planes Sample";

            this.spriteRenderer = new SpriteRenderer(this.Graphics);
            this.shaderProgram = new BasicSpriteShaderProgram(this.Graphics);

            this.planesTexture = Texture.LoadFromFile(this.Graphics, "Planes.png", new TextureParams()
                {
                });

            this.planeSpriteSheet = SpriteSheet.Build(this.planesTexture, 64, 64);

            this.font = TextureFont.Build(this.Graphics, "Segoe UI", 72, new TextureFontParams()
                {
                    Color = Color4.White,
                    BackgroundColor = Color4.Transparent,
                });

            Random random = new Random();

            this.planes = new List<Plane>();

            for (int i = 0; i < PlaneCount; i++)
            {
                this.planes.Add(new Plane(
                    random.Next(4) * 3,
                    new Vector2(random.Next(this.Window.Width), random.Next(this.Window.Height)),
                    (float)random.Next(360),
                    new Size(this.Window.Width, this.Window.Height)
                ));
            }
        }
コード例 #2
0
ファイル: WavingFlagSample.cs プロジェクト: HaKDMoDz/Samurai
        public WavingFlagSample()
            : base()
        {
            this.Window.Title = "Samurai Waving Flag Sample";

            this.shader = new ShaderProgram(
                this.Graphics,
                VertexShader.Compile(this.Graphics, File.ReadAllText("WavingFlagSample.vert")),
                FragmentShader.Compile(this.Graphics, File.ReadAllText("WavingFlagSample.frag")));

            this.Graphics.ShaderProgram = this.shader;

            this.texture = Texture.LoadFromFile(this.Graphics, "Flag.png", TextureParams.Default);

            int totalChunks = 100;
            int chunkSize = this.texture.Width / totalChunks;
            Rectangle destination = new Rectangle(
                (this.Window.Width - this.texture.Width) / 2,
                (this.Window.Height - this.texture.Height) / 2,
                chunkSize,
                this.texture.Height);

            Rectangle source = new Rectangle(0, 0, chunkSize, this.texture.Height);

            Vertex[] vertexData = new Vertex[totalChunks * 4];

            for (int i = 0; i < totalChunks * 4; i += 4)
            {
                vertexData[i] = new Vertex() { Position = new Vector2(destination.X, destination.Y), UV = new Vector2(source.X / (float)texture.Width, 0) };
                vertexData[i + 1] = new Vertex() { Position = new Vector2(destination.X + chunkSize, destination.Y), UV = new Vector2((source.X + chunkSize) / (float)texture.Width, 0) };
                vertexData[i + 2] = new Vertex() { Position = new Vector2(destination.X + chunkSize, destination.Y + this.texture.Height), UV = new Vector2((source.X + chunkSize) / (float)texture.Width, 1.0f) };
                vertexData[i + 3] = new Vertex() { Position = new Vector2(destination.X, destination.Y + this.texture.Height), UV = new Vector2(source.X / (float)texture.Width, 1.0f) };

                destination.X += chunkSize;
                source.X += chunkSize;
            }

            this.vertexBuffer = new StaticVertexBuffer<Vertex>(this.Graphics, vertexData);

            ushort[] indexData = new ushort[vertexData.Length * 6];

            for (ushort i = 0, vertex = 0; i < indexData.Length; i += 6, vertex += 4)
            {
                indexData[i] = vertex;
                indexData[i + 1] = (ushort)(vertex + 1);
                indexData[i + 2] = (ushort)(vertex + 2);
                indexData[i + 3] = vertex;
                indexData[i + 4] = (ushort)(vertex + 2);
                indexData[i + 5] = (ushort)(vertex + 3);
            }

            this.indexBuffer = new StaticIndexBuffer<ushort>(this.Graphics, indexData);
        }
コード例 #3
0
ファイル: FractalSample.cs プロジェクト: HaKDMoDz/Samurai
        public FractalSample()
        {
            this.Window.Title = "Fractals";

            this.shaderProgram = new ShaderProgram(
                this.Graphics,
                VertexShader.Compile(this.Graphics, File.ReadAllText("Mandelbrot.vert")),
                FragmentShader.Compile(this.Graphics, File.ReadAllText("Mandelbrot.frag")));

            this.Graphics.ShaderProgram = this.shaderProgram;

            this.vertexBuffer = new StaticVertexBuffer<Vertex>(this.Graphics, this.vertexData);

            this.palette = Texture.LoadFromFile(this.Graphics, "Palette.png", new TextureParams()
                {
                    WrapS = TextureWrap.Repeat,
                    WrapT = TextureWrap.Repeat
                });

            this.keyboard = new Keyboard(this.Window);
        }
コード例 #4
0
ファイル: Texture.cs プロジェクト: HaKDMoDz/Samurai
        public static Texture LoadFromBytes(GraphicsContext graphics, byte[] bytes, int width, int height, TextureParams parameters)
        {
            if (graphics == null)
                throw new ArgumentNullException("graphics");

            if (bytes == null)
                throw new ArgumentNullException("bytes");

            Texture texture = new Texture(graphics);

            graphics.GL.ActiveTexture(GLContext.Texture0 + texture.Index);
            graphics.GL.BindTexture(GLContext.Texture2D, texture.Handle);

            if (parameters.ColorKey != null)
            {
                for (int i = 0; i < bytes.Length; i += 4)
                {
                    uint pixel = GLHelper.MakePixelRGBA(bytes[i], bytes[i + 1], bytes[i + 2], bytes[i + 3]);

                    if (pixel == parameters.ColorKey.Value.ToRgba())
                        GLHelper.DecomposePixelRGBA(parameters.TransparentPixel.ToRgba(), out bytes[i], out bytes[i + 1], out bytes[i + 2], out bytes[i + 3]);
                }
            }

            graphics.GL.TexParameteri(GLContext.Texture2D, GLContext.TextureMagFilter, (int)parameters.MagFilter);
            graphics.GL.TexParameteri(GLContext.Texture2D, GLContext.TextureMinFilter, (int)parameters.MinFilter);
            graphics.GL.TexParameteri(GLContext.Texture2D, GLContext.TextureWrapS, (int)parameters.WrapS);
            graphics.GL.TexParameteri(GLContext.Texture2D, GLContext.TextureWrapT, (int)parameters.WrapT);

            graphics.GL.TexImage2D(
                GLContext.Texture2D,
                0,
                (int)GLContext.Rgba8,
                width,
                height,
                0,
                GLContext.Rgba,
                (int)GLContext.UnsignedByte,
                bytes);

            texture.Width = width;
            texture.Height = height;

            return texture;
        }
コード例 #5
0
ファイル: Button.cs プロジェクト: HaKDMoDz/Samurai
        protected override void InitializeControl()
        {
            IUIContentLoader contentLoader = (IUIContentLoader)this.Services.GetRequiredService<IUIContentLoader>();

            this.Texture = contentLoader.LoadButtonTexture();

            if (this.Texture == null)
                throw new InvalidOperationException("IUIContentLoader failed to load Button texture.");
        }
コード例 #6
0
ファイル: ShaderProgram.cs プロジェクト: HaKDMoDz/Samurai
 public void SetValue(string name, Texture texture)
 {
     int location = this.GetUniformLocation(name);
     this.Graphics.GL.Uniform1i(location, (int)texture.Index);
 }