-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
316 lines (277 loc) · 11.4 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using System.Net;
using SFML.Graphics;
using SFML.System;
using SFML.Window;
/*
* This code was AI generated using ChatGPT 4.0 classic
* from a chat by Andreas Toth.
*
* NuGet Package Requirements:
* - SFML.Net (https://www.nuget.org/packages/SFML.Net/)
*/
namespace AmigaStyleDemo;
class AmigaStyleDemo
{
private static RenderWindow window;
private static string text = "Hello, this is a classic Amiga style sine text scroller with copper bars!";
private static string instructions = " Keys/click: Left/Right for scroller, Up/Down for copper. ";
private static Font font;
private static Text sfmlText;
private static Text sfmlInstructions;
private static float time;
private static float scrollerSpeed = 100.0f;
private static float copperSpeed = 2.0f;
private static float instructionAmplitude;
private static float instructionFrequency = 0.5f; // Increased frequency for faster motion
private static int windowWidth = 800;
private static int windowHeight = 600;
private static string fontUrl = "https://github.com/google/fonts/raw/main/ofl/pressstart2p/PressStart2P-Regular.ttf";
private static string fontPath = "PressStart2P-Regular.ttf";
private static float flashOpacity = 0;
private static Directions activeDirection = Directions.NONE;
private enum Directions
{
NONE,
TOP,
BOTTOM,
LEFT,
RIGHT
}
static void Main()
{
// Download the font if it does not exist
if (!File.Exists(fontPath))
{
using (WebClient client = new WebClient())
{
client.DownloadFile(fontUrl, fontPath);
}
}
window = new RenderWindow(new VideoMode((uint)windowWidth, (uint)windowHeight), "Amiga Style Demo");
window.SetFramerateLimit(60);
window.Closed += (sender, e) => window.Close();
window.KeyPressed += OnKeyPressed;
window.MouseButtonPressed += OnMousePressed;
font = new Font(fontPath); // Ensure you have a valid font file path here
sfmlText = new Text(text, font, 32)
{
FillColor = Color.White
};
sfmlInstructions = new Text(instructions, font, 16)
{
FillColor = Color.White
};
// Calculate the amplitude to ensure the text can move fully from left to right
instructionAmplitude = (windowWidth - sfmlInstructions.GetLocalBounds().Width) / 2;
while (window.IsOpen)
{
window.DispatchEvents();
Update();
Render();
}
}
private static void OnKeyPressed(object sender, KeyEventArgs e)
{
switch (e.Code)
{
case Keyboard.Key.Left:
HandleInteraction(Directions.LEFT);
break;
case Keyboard.Key.Right:
HandleInteraction(Directions.RIGHT);
break;
case Keyboard.Key.Up:
HandleInteraction(Directions.TOP);
break;
case Keyboard.Key.Down:
HandleInteraction(Directions.BOTTOM);
break;
}
}
private static void OnMousePressed(object sender, MouseButtonEventArgs e)
{
float x = e.X * (windowWidth / (float)window.Size.X);
float y = e.Y * (windowHeight / (float)window.Size.Y);
HandleMouseInteraction(x, y);
}
private static void HandleMouseInteraction(float x, float y)
{
float cx = windowWidth / 2;
float cy = windowHeight / 2;
if (IsPointInTriangle(x, y, cx, cy, 0, 0, windowWidth, 0))
{
HandleInteraction(Directions.TOP);
}
else if (IsPointInTriangle(x, y, cx, cy, 0, windowHeight, windowWidth, windowHeight))
{
HandleInteraction(Directions.BOTTOM);
}
else if (IsPointInTriangle(x, y, cx, cy, 0, 0, 0, windowHeight))
{
HandleInteraction(Directions.LEFT);
}
else if (IsPointInTriangle(x, y, cx, cy, windowWidth, 0, windowWidth, windowHeight))
{
HandleInteraction(Directions.RIGHT);
}
}
private static void HandleInteraction(Directions direction)
{
activeDirection = direction;
flashOpacity = 0.5f; // Reset flash opacity for visibility
switch (direction)
{
case Directions.TOP:
copperSpeed += 0.1f;
break;
case Directions.BOTTOM:
copperSpeed = Math.Max(0.1f, copperSpeed - 0.1f);
break;
case Directions.LEFT:
scrollerSpeed = Math.Max(10f, scrollerSpeed + 10f);
break;
case Directions.RIGHT:
scrollerSpeed = Math.Max(10f, scrollerSpeed - 10f);
break;
default:
break;
}
}
private static void Update()
{
time += 0.05f;
if (flashOpacity > 0)
{
flashOpacity -= 0.01f; // Gradually reduce the flash opacity
}
else
{
activeDirection = Directions.NONE; // Reset direction when opacity fades out
}
}
private static void Render()
{
window.Clear(Color.Black);
DrawCopperBars();
DrawScrollingText();
DrawInstructions();
DrawActiveTriangle();
window.Display();
}
private static void DrawScrollingText()
{
float frequency = 0.05f;
float amplitude = 50.0f;
for (int i = 0; i < text.Length; i++)
{
float x = windowWidth - ((time * scrollerSpeed) % (text.Length * 32 + windowWidth)) + i * 32;
float y = (windowHeight / 2) + (float)(Math.Sin(x * frequency + time) * amplitude);
sfmlText.DisplayedString = text[i].ToString();
sfmlText.Position = new Vector2f(x, y);
window.Draw(sfmlText);
}
}
private static void DrawInstructions()
{
float instructionX = (windowWidth / 2) + (float)(Math.Sin(time * instructionFrequency) * instructionAmplitude);
sfmlInstructions.Position = new Vector2f(instructionX - sfmlInstructions.GetLocalBounds().Width / 2, windowHeight - 30);
window.Draw(sfmlInstructions);
}
private static void DrawCopperBars()
{
float barHeight = 20.0f;
int numBars = 20;
float barFrequency = 0.2f;
float barAmplitude = 40.0f;
float centerY = windowHeight / 2;
for (int i = 0; i < numBars; i++)
{
float phase = time + i * 0.2f;
float yOffset = (float)(Math.Sin(phase * barFrequency + time * copperSpeed) * barAmplitude);
float yPosition = centerY + yOffset + (i - numBars / 2) * barHeight;
Color barColor = ColorFromHue((time * 10 + i * 5) % 360);
DrawGradientRectangle(0, yPosition, windowWidth, barHeight, barColor);
}
}
private static void DrawGradientRectangle(float x, float y, float width, float height, Color color)
{
Vertex[] gradientRectangle = new Vertex[4];
gradientRectangle[0] = new Vertex(new Vector2f(x, y), color);
gradientRectangle[1] = new Vertex(new Vector2f(x + width, y), color);
gradientRectangle[2] = new Vertex(new Vector2f(x + width, y + height), new Color(0, 0, 0, 0));
gradientRectangle[3] = new Vertex(new Vector2f(x, y + height), new Color(0, 0, 0, 0));
window.Draw(gradientRectangle, PrimitiveType.Quads);
}
private static void DrawActiveTriangle()
{
if (flashOpacity > 0 && activeDirection != Directions.NONE)
{
float cx = windowWidth / 2;
float cy = windowHeight / 2;
VertexArray triangle = new VertexArray(PrimitiveType.Triangles, 3);
switch (activeDirection)
{
case Directions.TOP:
triangle[0] = new Vertex(new Vector2f(cx, cy), new Color(255, 255, 255, (byte)(flashOpacity * 255)));
triangle[1] = new Vertex(new Vector2f(0, 0), new Color(255, 255, 255, (byte)(flashOpacity * 255)));
triangle[2] = new Vertex(new Vector2f(windowWidth, 0), new Color(255, 255, 255, (byte)(flashOpacity * 255)));
break;
case Directions.BOTTOM:
triangle[0] = new Vertex(new Vector2f(cx, cy), new Color(255, 255, 255, (byte)(flashOpacity * 255)));
triangle[1] = new Vertex(new Vector2f(0, windowHeight), new Color(255, 255, 255, (byte)(flashOpacity * 255)));
triangle[2] = new Vertex(new Vector2f(windowWidth, windowHeight), new Color(255, 255, 255, (byte)(flashOpacity * 255)));
break;
case Directions.LEFT:
triangle[0] = new Vertex(new Vector2f(cx, cy), new Color(255, 255, 255, (byte)(flashOpacity * 255)));
triangle[1] = new Vertex(new Vector2f(0, 0), new Color(255, 255, 255, (byte)(flashOpacity * 255)));
triangle[2] = new Vertex(new Vector2f(0, windowHeight), new Color(255, 255, 255, (byte)(flashOpacity * 255)));
break;
case Directions.RIGHT:
triangle[0] = new Vertex(new Vector2f(cx, cy), new Color(255, 255, 255, (byte)(flashOpacity * 255)));
triangle[1] = new Vertex(new Vector2f(windowWidth, 0), new Color(255, 255, 255, (byte)(flashOpacity * 255)));
triangle[2] = new Vertex(new Vector2f(windowWidth, windowHeight), new Color(255, 255, 255, (byte)(flashOpacity * 255)));
break;
default:
break;
}
window.Draw(triangle);
}
}
private static bool IsPointInTriangle(float px, float py, float ax, float ay, float bx, float by, float cx, float cy)
{
bool b1 = Sign(px, py, ax, ay, bx, by) < 0.0f;
bool b2 = Sign(px, py, bx, by, cx, cy) < 0.0f;
bool b3 = Sign(px, py, cx, cy, ax, ay) < 0.0f;
return ((b1 == b2) && (b2 == b3));
}
private static float Sign(float px, float py, float ax, float ay, float bx, float by)
{
return (px - bx) * (ay - by) - (ax - bx) * (py - by);
}
private static Color ColorFromHue(float hue)
{
int h = (int)(hue / 60) % 6;
float f = hue / 60 - h;
float v = 1;
float p = 0;
float q = 1 - f;
float t = f;
switch (h)
{
case 0:
return new Color((byte)(v * 255), (byte)(t * 255), (byte)(p * 255));
case 1:
return new Color((byte)(q * 255), (byte)(v * 255), (byte)(p * 255));
case 2:
return new Color((byte)(p * 255), (byte)(v * 255), (byte)(t * 255));
case 3:
return new Color((byte)(p * 255), (byte)(q * 255), (byte)(v * 255));
case 4:
return new Color((byte)(t * 255), (byte)(p * 255), (byte)(v * 255));
case 5:
return new Color((byte)(v * 255), (byte)(p * 255), (byte)(q * 255));
default:
return new Color(255, 255, 255);
}
}
}