mirror of
https://github.com/Leonmmcoset/CMLeonOS.git
synced 2026-03-03 15:30:27 +00:00
GUI桌面环境
This commit is contained in:
91
Gui/Apps/Demos/Mandelbrot.cs
Normal file
91
Gui/Apps/Demos/Mandelbrot.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
using CMLeonOS;
|
||||
using CMLeonOS.Gui.UILib;
|
||||
using System;
|
||||
using System.Drawing;
|
||||
|
||||
namespace CMLeonOS.Gui.Apps.Demos
|
||||
{
|
||||
internal class Mandelbrot : Process
|
||||
{
|
||||
internal Mandelbrot() : base("Mandelbrot", ProcessType.Application) { }
|
||||
|
||||
AppWindow window;
|
||||
|
||||
WindowManager wm = ProcessManager.GetProcess<WindowManager>();
|
||||
|
||||
private Color GetColor(double v)
|
||||
{
|
||||
int red = Math.Clamp((int)(255 * v), 0, 255);
|
||||
int green = 0;
|
||||
int blue = Math.Clamp((int)(255 * (1 - v)), 0, 255);
|
||||
|
||||
return Color.FromArgb(red, green, blue);
|
||||
}
|
||||
|
||||
private void RenderMandelbrot()
|
||||
{
|
||||
window.Clear(Color.Black);
|
||||
wm.Update(window);
|
||||
|
||||
int width = window.Width;
|
||||
int height = window.Height;
|
||||
|
||||
const int max = 20;
|
||||
const double bail = 2.0;
|
||||
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
double zx = 0;
|
||||
double zy = 0;
|
||||
double cx = (x - width / 2.0) / (width / 4.0);
|
||||
double cy = (y - height / 2.0) / (height / 4.0);
|
||||
|
||||
int iteration = 0;
|
||||
|
||||
while (zx * zx + zy * zy < bail && iteration < max)
|
||||
{
|
||||
double zxNew = zx * zx - zy * zy + cx;
|
||||
zy = 2 * zx * zy + cy;
|
||||
zx = zxNew;
|
||||
iteration++;
|
||||
}
|
||||
|
||||
double smooth = iteration + 1 - Math.Log(Math.Log(Math.Sqrt(zx * zx + zy * zy)) / Math.Log(bail)) / Math.Log(2);
|
||||
window.DrawPoint(x, y, GetColor(smooth / max));
|
||||
|
||||
if (x % 32 == 0)
|
||||
{
|
||||
ProcessManager.Yield();
|
||||
}
|
||||
}
|
||||
|
||||
if (y % 8 == 0)
|
||||
{
|
||||
wm.Update(window);
|
||||
}
|
||||
}
|
||||
|
||||
wm.Update(window);
|
||||
}
|
||||
|
||||
public override void Start()
|
||||
{
|
||||
base.Start();
|
||||
window = new AppWindow(this, 256, 256, 256, 256);
|
||||
wm.AddWindow(window);
|
||||
window.Title = "Mandelbrot";
|
||||
window.CanResize = true;
|
||||
window.Closing = TryStop;
|
||||
window.UserResized = RenderMandelbrot;
|
||||
|
||||
RenderMandelbrot();
|
||||
}
|
||||
|
||||
public override void Run()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
105
Gui/Apps/Demos/Starfield.cs
Normal file
105
Gui/Apps/Demos/Starfield.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using CMLeonOS;
|
||||
using CMLeonOS.Gui.UILib;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
|
||||
namespace CMLeonOS.Gui.Apps.Demos
|
||||
{
|
||||
internal class Starfield : Process
|
||||
{
|
||||
internal Starfield() : base("Starfield", ProcessType.Application) { }
|
||||
|
||||
AppWindow window;
|
||||
|
||||
WindowManager wm = ProcessManager.GetProcess<WindowManager>();
|
||||
|
||||
private readonly Random random = new Random();
|
||||
|
||||
private readonly List<Star> stars = new List<Star>();
|
||||
|
||||
private int timerId;
|
||||
|
||||
private class Star
|
||||
{
|
||||
internal double X { get; set; }
|
||||
internal double Y { get; set; }
|
||||
internal double Z { get; set; }
|
||||
internal double Velocity { get; set; }
|
||||
|
||||
internal Star(double x, double y, double z, double velocity)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
Z = z;
|
||||
Velocity = velocity;
|
||||
}
|
||||
}
|
||||
|
||||
internal (double, double) TransformCoordinates(double x, double y, double z, double fov)
|
||||
{
|
||||
double screenX = x / (z * Math.Tan(fov / 2)) + 0.5;
|
||||
double screenY = y / (z * Math.Tan(fov / 2)) + 0.5;
|
||||
|
||||
return (screenX, screenY);
|
||||
}
|
||||
|
||||
public override void Start()
|
||||
{
|
||||
base.Start();
|
||||
window = new AppWindow(this, 256, 256, 256, 256);
|
||||
wm.AddWindow(window);
|
||||
window.Title = "Starfield";
|
||||
window.CanResize = true;
|
||||
window.Closing = TryStop;
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
stars.Add(new Star(
|
||||
x: random.NextDouble() * 2 - 1.5,
|
||||
y: random.NextDouble() * 2 - 1.5,
|
||||
z: 3,
|
||||
velocity: random.NextDouble() * 0.05 + 0.05));
|
||||
}
|
||||
|
||||
timerId = Cosmos.HAL.Global.PIT.RegisterTimer(new Cosmos.HAL.PIT.PITTimer(() =>
|
||||
{
|
||||
foreach (var star in stars)
|
||||
{
|
||||
star.Z -= star.Velocity;
|
||||
|
||||
if (star.Z < 0)
|
||||
{
|
||||
star.X = random.NextDouble() * 2 - 1.5;
|
||||
star.Y = random.NextDouble() * 2 - 1.5;
|
||||
star.Z = 3;
|
||||
}
|
||||
}
|
||||
}, (ulong)((1000 /* ms */ / 30) * 1e+6 /* ms -> ns */ ), true));
|
||||
}
|
||||
|
||||
public override void Run()
|
||||
{
|
||||
window.Clear(Color.Black);
|
||||
|
||||
foreach (var star in stars)
|
||||
{
|
||||
(double X, double Y) pos = TransformCoordinates(star.X, star.Y, star.Z, Math.PI / 2);
|
||||
|
||||
int screenX = (int)((pos.X + 1) * (window.Width / 2));
|
||||
int screenY = (int)((pos.Y + 1) * (window.Height / 2));
|
||||
|
||||
window.DrawPoint(screenX, screenY, Color.White);
|
||||
}
|
||||
|
||||
wm.Update(window);
|
||||
}
|
||||
|
||||
public override void Stop()
|
||||
{
|
||||
base.Stop();
|
||||
|
||||
Cosmos.HAL.Global.PIT.UnregisterTimer(timerId);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user