this repo has no description
1// Copyright (c) 2021 ezequias2d <ezequiasmoises@gmail.com> and the Peridot contributors
2// This code is licensed under MIT license (see LICENSE for details)
3using System.Drawing;
4using Veldrid;
5using Veldrid.StartupUtilities;
6using System.Numerics;
7using Peridot.Demo;
8
9using StbImageSharp;
10using System.Diagnostics;
11using Rectangle = System.Drawing.Rectangle;
12using Peridot;
13using Peridot.Text;
14
15var title = "Peridot.Demo";
16var wci = new WindowCreateInfo(100, 100, 640, 480, WindowState.Normal, title);
17
18var window = VeldridStartup.CreateWindow(wci);
19var gd = VeldridStartup.CreateVulkanGraphicsDevice(
20 new(true,
21 Veldrid.PixelFormat.D32_Float_S8_UInt,
22 true,
23 ResourceBindingModel.Default,
24 true,
25 true),
26 window);
27
28window.Resized += () =>
29{
30 gd.MainSwapchain.Resize((uint)window.Width, (uint)window.Height);
31};
32
33var peridot = new VPeridot(gd);
34
35var shaders = peridot.LoadDefaultShaders();
36var sbd = new SpriteBatchDescriptor(gd.MainSwapchain.Framebuffer.OutputDescription, shaders);
37var sb = peridot.CreateSpriteBatch(sbd);
38
39var tr = new TextRenderer(peridot, sb);
40var cl = gd.ResourceFactory.CreateCommandList();
41var fence = gd.ResourceFactory.CreateFence(false);
42var texture = LoadTexture(Resource._4_2_07);
43var font = LoadFont(Resource.romulus);
44var count = 0;
45
46var time = DateTime.Now;
47var neg = false;
48var ps = 0;
49while (window.Exists)
50{
51 count++;
52
53 var now = DateTime.Now;
54 var delta = now - time;
55 var deltaTime = delta.TotalSeconds;
56 time = now;
57
58 if((count % 1000) == 0)
59 {
60 window.Title = title + " " + (1f / deltaTime);
61 }
62
63 window.PumpEvents();
64
65 sb.Begin();
66 sb.ViewMatrix = Matrix4x4.CreateOrthographic(window.Width, window.Height, 0.01f, -100f);
67
68 var size = new Vector2(texture.Width, texture.Height);
69 var pos = size * -0.5f;
70 var source = new System.Drawing.Rectangle((int)texture.Width / 2, (int)texture.Height / 2, (int)texture.Width / 2, (int)texture.Height / 2);
71
72 ps += neg ? -1 : 1;
73 if (ps == 0 || ps == 100)
74 neg = !neg;
75
76 var options = ps >= 0 && ps < 25 ? SpriteOptions.None :
77 ps >= 25 && ps < 50 ? SpriteOptions.FlipVertically :
78 ps >= 50 && ps < 75 ? SpriteOptions.FlipVertically | SpriteOptions.FlipHorizontally :
79 SpriteOptions.FlipHorizontally;
80
81 sb.Draw(texture, default, source, Color.White, 0, new(-size.X / 6, 0), Vector2.One, options, 1f);
82
83 var s = font.MeasureString("Hello World!", 32);
84 var strScissor = new Rectangle(0, 0, (int)s.X, (int)s.Y * ps / 100);
85 tr.DrawString(font, 32, "Hello World!", new Vector2(1, 1), Color.White, 0, new Vector2(0, 0), new Vector2(1), 2f, strScissor);
86 sb.End();
87
88 cl.Begin();
89 cl.SetFramebuffer(gd.SwapchainFramebuffer);
90 cl.ClearColorTarget(0, RgbaFloat.CornflowerBlue);
91 cl.ClearDepthStencil(0f);
92 cl.DrawBatch(sb);
93 cl.End();
94
95 fence.Reset();
96 gd.SubmitCommands(cl, fence);
97 gd.WaitForFence(fence);
98 gd.SwapBuffers();
99}
100
101Texture LoadTexture(byte[] data)
102{
103 ImageResult image;
104 {
105 image = ImageResult.FromMemory(data, ColorComponents.Default);
106 Debug.Assert(image != null);
107 }
108
109 var td = new TextureDescription(
110 (uint)image.Width, (uint)image.Height, 1,
111 1, 1,
112 Veldrid.PixelFormat.R8_G8_B8_A8_UNorm,
113 TextureUsage.Sampled,
114 TextureType.Texture2D);
115
116 var texture = gd.ResourceFactory.CreateTexture(td);
117 gd.UpdateTexture(texture, image.Data, 0, 0, 0, td.Width, td.Height, td.Depth, 0, 0);
118
119 return texture;
120}
121
122Font LoadFont(byte[] data)
123{
124 using var stream = new MemoryStream();
125 stream.Write(data);
126 stream.Position = 0;
127 var font = new Font();
128 font.AddFont(data);
129 return font;
130}