-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRaylib.cs
More file actions
360 lines (302 loc) · 12.2 KB
/
Raylib.cs
File metadata and controls
360 lines (302 loc) · 12.2 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
using Veldrid;
using Veldrid.Sdl2;
using Veldrid.StartupUtilities;
using System.Numerics;
namespace VeldridRaylib
{
/// <summary>
/// Main raylib API implementation using Veldrid
/// </summary>
public static class Raylib
{
private static Sdl2Window? _window;
private static GraphicsDevice? _graphicsDevice;
private static Renderer? _renderer;
private static bool _windowShouldClose = false;
private static Dictionary<Veldrid.Key, bool> _keyStates = new();
private static Dictionary<Veldrid.Key, bool> _previousKeyStates = new();
private static Vector2 _mousePosition = Vector2.Zero;
// Window management
public static void InitWindow(int width, int height, string title)
{
try
{
Console.WriteLine($"Creating window: {width}x{height} - {title}");
var windowCI = new WindowCreateInfo(50, 50, width, height, WindowState.Normal, title);
_window = VeldridStartup.CreateWindow(ref windowCI);
Console.WriteLine("Window created successfully");
// Try different graphics backends in order of preference for compatibility
GraphicsBackend[] backends = { GraphicsBackend.OpenGL, GraphicsBackend.Direct3D11, GraphicsBackend.Vulkan };
foreach (var backend in backends)
{
try
{
Console.WriteLine($"Trying graphics backend: {backend}");
var options = new GraphicsDeviceOptions(
debug: false,
swapchainDepthFormat: null,
syncToVerticalBlank: true,
resourceBindingModel: ResourceBindingModel.Default);
_graphicsDevice = VeldridStartup.CreateGraphicsDevice(_window, options, backend);
Console.WriteLine($"Graphics device created successfully: {_graphicsDevice.BackendType}");
break;
}
catch (Exception ex)
{
Console.WriteLine($"Failed to create graphics device with {backend}: {ex.Message}");
continue;
}
}
if (_graphicsDevice == null)
{
throw new Exception("Failed to create graphics device with any backend");
}
Console.WriteLine("Creating renderer...");
_renderer = new Renderer(_graphicsDevice);
Console.WriteLine("Renderer created successfully");
// Set up orthographic projection
var projection = Matrix4x4.CreateOrthographicOffCenter(0, width, height, 0, -1, 1);
_renderer.SetProjection(projection);
// Set up input handling
_window.KeyDown += OnKeyDown;
_window.KeyUp += OnKeyUp;
_window.MouseMove += OnMouseMove;
_window.Closed += () => _windowShouldClose = true;
Console.WriteLine("Window initialization completed successfully");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to initialize window: {ex.Message}");
Console.WriteLine($"Stack trace: {ex.StackTrace}");
throw;
}
}
public static bool WindowShouldClose()
{
return _windowShouldClose;
}
public static void CloseWindow()
{
_windowShouldClose = true;
_renderer?.Dispose();
_graphicsDevice?.Dispose();
_window?.Close();
}
public static void SetTargetFPS(int fps)
{
// Note: This is a simplified implementation
// In a real implementation, you'd want proper frame timing
}
// Drawing functions
public static void BeginDrawing()
{
if (_window == null || _graphicsDevice == null || _renderer == null) return;
var inputSnapshot = _window.PumpEvents();
// Update previous key states
_previousKeyStates.Clear();
foreach (var kvp in _keyStates)
{
_previousKeyStates[kvp.Key] = kvp.Value;
}
_renderer.BeginFrame();
}
public static void EndDrawing()
{
if (_graphicsDevice == null || _renderer == null) return;
var commandList = _renderer.GetCommandList();
commandList.Begin();
commandList.SetFramebuffer(_graphicsDevice.SwapchainFramebuffer);
// Clear the screen first
commandList.ClearColorTarget(0, _renderer.GetClearColor());
// Render all accumulated vertices
_renderer.Flush(commandList);
commandList.End();
_graphicsDevice.SubmitCommands(commandList);
_graphicsDevice.SwapBuffers();
}
public static void ClearBackground(Color color)
{
if (_graphicsDevice == null || _renderer == null) return;
var colorVec = color.ToVector4();
_renderer.SetClearColor(new RgbaFloat(colorVec.X, colorVec.Y, colorVec.Z, colorVec.W));
}
// Shape drawing functions
public static void DrawRectangle(int posX, int posY, int width, int height, Color color)
{
DrawRectangleRec(new Rectangle(posX, posY, width, height), color);
}
public static void DrawRectangleRec(Rectangle rec, Color color)
{
if (_renderer == null) return;
var p1 = new Vector2(rec.X, rec.Y);
var p2 = new Vector2(rec.X + rec.Width, rec.Y);
var p3 = new Vector2(rec.X + rec.Width, rec.Y + rec.Height);
var p4 = new Vector2(rec.X, rec.Y + rec.Height);
_renderer.DrawQuad(p1, p2, p3, p4, color);
}
public static void DrawCircle(int centerX, int centerY, float radius, Color color)
{
DrawCircleV(new Vector2(centerX, centerY), radius, color);
}
public static void DrawCircleV(Vector2 center, float radius, Color color)
{
if (_renderer == null) return;
// Draw circle as multiple triangles
int segments = Math.Max(12, (int)(radius * 0.5f));
float angleStep = (float)(2 * Math.PI / segments);
for (int i = 0; i < segments; i++)
{
float angle1 = i * angleStep;
float angle2 = (i + 1) * angleStep;
var p1 = center;
var p2 = center + new Vector2((float)Math.Cos(angle1) * radius, (float)Math.Sin(angle1) * radius);
var p3 = center + new Vector2((float)Math.Cos(angle2) * radius, (float)Math.Sin(angle2) * radius);
_renderer.DrawTriangle(p1, p2, p3, color);
}
}
public static void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, Color color)
{
DrawLineV(new Vector2(startPosX, startPosY), new Vector2(endPosX, endPosY), color);
}
public static void DrawLineV(Vector2 startPos, Vector2 endPos, Color color)
{
if (_renderer == null) return;
// Draw line as a thin rectangle
var direction = endPos - startPos;
var length = direction.Length();
if (length < 0.001f) return;
var normalized = direction / length;
var perpendicular = new Vector2(-normalized.Y, normalized.X);
float thickness = 1.0f;
var p1 = startPos + perpendicular * thickness * 0.5f;
var p2 = startPos - perpendicular * thickness * 0.5f;
var p3 = endPos - perpendicular * thickness * 0.5f;
var p4 = endPos + perpendicular * thickness * 0.5f;
_renderer.DrawQuad(p1, p2, p3, p4, color);
}
public static void DrawPixel(int posX, int posY, Color color)
{
DrawRectangle(posX, posY, 1, 1, color);
}
// Text functions (simplified - no actual font rendering)
public static void DrawText(string text, int posX, int posY, int fontSize, Color color)
{
// This is a placeholder - real text rendering would require font loading and glyph rendering
// For now, just draw a rectangle to indicate text position
int textWidth = text.Length * fontSize / 2;
DrawRectangle(posX, posY, textWidth, fontSize, color);
}
public static int MeasureText(string text, int fontSize)
{
// Simplified text measurement
return text.Length * fontSize / 2;
}
// Input functions
public static bool IsKeyPressed(KeyCode key)
{
var veldridKey = (Veldrid.Key)(int)key;
return _keyStates.GetValueOrDefault(veldridKey, false) &&
!_previousKeyStates.GetValueOrDefault(veldridKey, false);
}
public static bool IsKeyDown(KeyCode key)
{
var veldridKey = (Veldrid.Key)(int)key;
return _keyStates.GetValueOrDefault(veldridKey, false);
}
public static bool IsKeyReleased(KeyCode key)
{
var veldridKey = (Veldrid.Key)(int)key;
return !_keyStates.GetValueOrDefault(veldridKey, false) &&
_previousKeyStates.GetValueOrDefault(veldridKey, false);
}
public static bool IsKeyUp(KeyCode key)
{
var veldridKey = (Veldrid.Key)(int)key;
return !_keyStates.GetValueOrDefault(veldridKey, false);
}
public static Vector2 GetMousePosition()
{
return _mousePosition;
}
public static bool IsMouseButtonPressed(MouseButton button)
{
// Simplified - would need proper mouse button tracking
return false;
}
// Utility functions
public static int GetScreenWidth()
{
return _window?.Width ?? 0;
}
public static int GetScreenHeight()
{
return _window?.Height ?? 0;
}
public static float GetFrameTime()
{
// Simplified - would need proper frame timing
return 1.0f / 60.0f;
}
public static int GetFPS()
{
// Simplified
return 60;
}
// Event handlers
private static void OnKeyDown(KeyEvent keyEvent)
{
_keyStates[keyEvent.Key] = true;
}
private static void OnKeyUp(KeyEvent keyEvent)
{
_keyStates[keyEvent.Key] = false;
}
private static void OnMouseMove(MouseMoveEventArgs args)
{
_mousePosition = new Vector2(args.MousePosition.X, args.MousePosition.Y);
}
}
// Key codes enum (simplified)
public enum KeyCode
{
Space = (int)Veldrid.Key.Space,
Enter = (int)Veldrid.Key.Enter,
Escape = (int)Veldrid.Key.Escape,
A = (int)Veldrid.Key.A,
B = (int)Veldrid.Key.B,
C = (int)Veldrid.Key.C,
D = (int)Veldrid.Key.D,
E = (int)Veldrid.Key.E,
F = (int)Veldrid.Key.F,
G = (int)Veldrid.Key.G,
H = (int)Veldrid.Key.H,
I = (int)Veldrid.Key.I,
J = (int)Veldrid.Key.J,
K = (int)Veldrid.Key.K,
L = (int)Veldrid.Key.L,
M = (int)Veldrid.Key.M,
N = (int)Veldrid.Key.N,
O = (int)Veldrid.Key.O,
P = (int)Veldrid.Key.P,
Q = (int)Veldrid.Key.Q,
R = (int)Veldrid.Key.R,
S = (int)Veldrid.Key.S,
T = (int)Veldrid.Key.T,
U = (int)Veldrid.Key.U,
V = (int)Veldrid.Key.V,
W = (int)Veldrid.Key.W,
X = (int)Veldrid.Key.X,
Y = (int)Veldrid.Key.Y,
Z = (int)Veldrid.Key.Z,
Up = (int)Veldrid.Key.Up,
Down = (int)Veldrid.Key.Down,
Left = (int)Veldrid.Key.Left,
Right = (int)Veldrid.Key.Right
}
public enum MouseButton
{
Left,
Right,
Middle
}
}