public Canvas2DGame() { var info = this.Graphics.GetDescription(); this.Window.Title = "Canvas2D Sandbox"; this.canvas = new CanvasRenderer(this.Graphics); this.solidColorBrush = new SolidColorBrush(this.Graphics); this.textureBrush = new TextureBrush(this.Graphics); this.linearGradientBrush = new LinearGradientBrush(this.Graphics); this.lineStripe = Texture2D.LoadFromFile(this.Graphics, "LineStripe.png", new TextureParams() { WrapS = TextureWrap.Repeat, WrapT = TextureWrap.Repeat }); this.textureBrush.Texture = lineStripe; this.samuraiLogo = Texture2D.LoadFromFile(this.Graphics, "SamuraiLogo.png", new TextureParams() { WrapS = TextureWrap.Repeat, WrapT = TextureWrap.Repeat }); this.polygonPositions = new Vector2[8]; this.polygonPositions[0] = new Vector2(400, 400); this.polygonPositions[1] = new Vector2(450, 300); this.polygonPositions[2] = new Vector2(500, 450); this.polygonPositions[3] = new Vector2(550, 250); this.polygonPositions[4] = new Vector2(600, 500); this.polygonPositions[5] = new Vector2(650, 200); this.polygonPositions[6] = new Vector2(700, 550); this.polygonPositions[7] = new Vector2(750, 150); }
public static Texture2D 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"); Texture2D texture = new Texture2D(graphics); Texture.Initialize(texture, graphics, GLContext.Texture2D, bytes, parameters); 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; }
public PlanesSample() : base() { this.Window.Title = "Samurai Planes Sample"; this.spriteRenderer = new SpriteRenderer(this.Graphics); this.shaderProgram = new BasicSpriteShaderProgram(this.Graphics); this.planesTexture = Texture2D.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) )); } }
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 = Texture2D.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); }
public Demo2DGame() : base(new GameOptions() { AutoResizeViewport = true, WindowResizable = true }) { this.Window.Title = "Samurai 2D Demo"; this.Graphics.DepthBufferState = DepthBufferState.LessThanOrEqual; this.Graphics.BlendState = BlendState.AlphaBlend; this.Graphics.RasterizerState = RasterizerState.Default; this.spriteRenderer = new SpriteRenderer(this.Graphics, 1024); this.shaderProgram = new BasicSpriteShaderProgram(this.Graphics); this.planesTexture = Texture2D.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, //ColorKey = Color4.Black }); this.keyboard = new Keyboard(); this.mouse = new Mouse(this.Window); 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), this.Window.Size )); } }
public DemoGame() { this.Window.Title = "Samurai Demo"; this.Graphics.BlendEnabled = true; this.Graphics.SourceBlendFactor = SourceBlendFactor.SourceAlpha; this.Graphics.DestinationBlendFactor = DestinationBlendFactor.OneMinusDestinationAlpha; this.Graphics.DepthBufferEnabled = true; this.Graphics.DepthFunction = DepthFunction.LessThanOrEqual; this.Graphics.FrontFace = FrontFace.Clockwise; this.Graphics.CullMode = CullMode.Back; this.shaderProgram = new ShaderProgram( this.Graphics, VertexShader.Compile(this.Graphics, File.ReadAllText("Shader.vert")), FragmentShader.Compile(this.Graphics, File.ReadAllText("Shader.frag"))); this.vertexBuffer = new StaticVertexBuffer<Vertex>(this.Graphics, this.vertexData); this.indexBuffer = new StaticIndexBuffer<byte>(this.Graphics, this.indexData); this.texture0 = Texture2D.LoadFromFile(this.Graphics, "Texture0.png", new TextureParams() { WrapS = TextureWrap.Repeat, WrapT = TextureWrap.Repeat }); this.texture1 = Texture2D.LoadFromFile(this.Graphics, "Texture1.png", new TextureParams() { WrapS = TextureWrap.Clamp, WrapT = TextureWrap.Clamp }); this.gamePad1 = new GamePad(GamePadIndex.One); this.controlInputHandler = new ControlInputHandler(this.Window); this.controlRenderer = new ControlRenderer(this.Graphics); this.controlRenderer.DefaultFont = TextureFont.Build(this.Graphics, "Segoe UI", 72, new TextureFontParams() { BackgroundColor = Color4.Transparent }); this.panel = new Panel() { Position = new Vector2(100, 100), Size = new Size(100, 100) }; this.label = new Label() { Text = "Hello World!", Font = TextureFont.Build(this.Graphics, "Segoe UI", 24, new TextureFontParams() { BackgroundColor = Color4.Transparent }) }; this.label.CursorEnter += (s, e) => { this.label.ForegroundColor = Color4.Black; }; this.label.CursorLeave += (s, e) => { this.label.ForegroundColor = Color4.White; }; this.panel.Controls.Add(label); }
private void ImportPicture() { OpenFileDialog dialog = new OpenFileDialog() { Filter = "Image Files (*.jpg,*.png)|*.jpg;*.png" }; if (dialog.ShowDialog() == true) { Texture2D picture = null; try { picture = Texture2D.LoadFromFile(this.GraphicsBox.Graphics, dialog.FileName, new TextureParams()); } catch (Exception) { picture = null; } if (picture != null) { this.currentPicture.Dispose(); this.currentPicture = picture; } } }
private void GraphicsBox_GraphicsContextCreated(object sender, GraphicsContextEventArgs e) { e.Graphics.ClearColor = Color4.CornflowerBlue; this.vertexBuffer = new StaticVertexBuffer<Vertex>(e.Graphics, new Vertex[] { new Vertex() { Position = Vector2.Zero, TexCoords = Vector2.Zero }, new Vertex() { Position = Vector2.UnitX, TexCoords = Vector2.UnitX }, new Vertex() { Position = Vector2.UnitY, TexCoords = Vector2.UnitY }, new Vertex() { Position = Vector2.UnitY, TexCoords = Vector2.UnitY }, new Vertex() { Position = Vector2.UnitX, TexCoords = Vector2.UnitX }, new Vertex() { Position = Vector2.One, TexCoords = Vector2.One }, }); this.currentShaderProgram = new ShaderProgram( e.Graphics, VertexShader.Compile(e.Graphics, vertexShaderCode), FragmentShader.Compile(e.Graphics, defaultFragmentShaderCode)); this.currentPicture = Texture2D.LoadFromFile(e.Graphics, "SamuraiLogo.png", new TextureParams()); this.stopwatch.Start(); }