Repository navigation
Expand file tree
/
Copy pathExample_Raylib.cs
More file actions
166 lines (130 loc) · 4.21 KB
/
Copy pathExample_Raylib.cs
File metadata and controls
166 lines (130 loc) · 4.21 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
using System;
using System.Diagnostics;
using ExampleShared;
using System.Numerics;
using NuklearDotNet;
using Raylib_cs;
namespace Example_Raylib {
unsafe class RaylibTexture {
public Texture2D Texture;
public Image Image;
public RaylibTexture(int W, int H, IntPtr Data) {
/*Image = Raylib.GenImageColor(W, H, Color.ORANGE);
for (int y = 0; y < H; y++)
for (int x = 0; x < W; x++) {
NkColor C = ((NkColor*)Data)[y * W + x];
Raylib.ImageDrawPixel(ref Image, x, y, new Color(C.R, C.G, C.B, C.A));
}*/
Image = new Image();
Image.width = W;
Image.height = H;
Image.mipmaps = 1;
Image.format = PixelFormat.PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
Image.data = Data;
Texture = Raylib.LoadTextureFromImage(Image);
Raylib.SetTextureFilter(Texture, TextureFilter.TEXTURE_FILTER_POINT);
Raylib.SetTextureWrap(Texture, TextureWrap.TEXTURE_WRAP_CLAMP);
}
}
unsafe class RaylibDevice : NuklearDeviceTex<RaylibTexture>, IFrameBuffered {
RenderTexture2D RT;
public RaylibDevice() {
CreateRT();
}
void CreateRT() {
int W = Raylib.GetScreenWidth();
int H = Raylib.GetScreenHeight();
RT = Raylib.LoadRenderTexture(W, H);
}
public override RaylibTexture CreateTexture(int W, int H, IntPtr Data) {
return new RaylibTexture(W, H, Data);
}
public void BeginBuffering() {
Raylib.BeginTextureMode(RT);
Raylib.ClearBackground(Color.BLANK);
}
NkVertex[] Verts;
ushort[] Inds;
public override void SetBuffer(NkVertex[] VertexBuffer, ushort[] IndexBuffer) {
Verts = VertexBuffer;
Inds = IndexBuffer;
}
static void rlColor(NkColor Clr) {
Rlgl.rlColor4f(Clr.R / 255.0f, Clr.G / 255.0f, Clr.B / 255.0f, Clr.A / 255.0f);
}
static void DrawVert(NkVertex V) {
rlColor(V.Color);
Rlgl.rlTexCoord2f(V.UV.X, V.UV.Y);
Rlgl.rlVertex2f(V.Position.X, V.Position.Y);
}
static void Draw(NkVertex v1, NkVertex v2, NkVertex v3) {
DrawVert(v1);
DrawVert(v3);
DrawVert(v2);
DrawVert(v2);
}
public override void Render(NkHandle Userdata, RaylibTexture Texture, NkRect ClipRect, uint Offset, uint Count) {
Rlgl.rlDisableBackfaceCulling();
Raylib.BeginScissorMode((int)ClipRect.X, (int)ClipRect.Y, (int)ClipRect.W, (int)ClipRect.H);
{
Rlgl.rlSetTexture(Texture.Texture.id);
Rlgl.rlCheckRenderBatchLimit((int)Count);
Rlgl.rlBegin(Rlgl.RL_QUADS);
for (int i = 0; i < Count; i += 3) {
NkVertex V1 = Verts[Inds[Offset + i]];
NkVertex V2 = Verts[Inds[Offset + i + 1]];
NkVertex V3 = Verts[Inds[Offset + i + 2]];
Draw(V1, V2, V3);
}
Rlgl.rlEnd();
Rlgl.rlSetTexture(0);
}
Raylib.EndScissorMode();
Rlgl.rlEnableBackfaceCulling();
}
public void EndBuffering() {
Raylib.EndTextureMode();
}
public void RenderFinal() {
if (Raylib.IsWindowResized()) {
Raylib.UnloadRenderTexture(RT);
CreateRT();
NuklearAPI.QueueForceUpdate();
}
Raylib.DrawTextureRec(RT.texture, new Rectangle(0, RT.texture.height, RT.texture.width, -RT.texture.height), Vector2.Zero, Color.WHITE);
}
}
class Program {
static void Main(string[] args) {
Stopwatch SWatch = Stopwatch.StartNew();
Raylib.SetConfigFlags(ConfigFlags.FLAG_WINDOW_RESIZABLE);
Raylib.InitWindow(800, 600, "Raylib Window");
Raylib.SetTargetFPS(60);
RaylibDevice Dev = new RaylibDevice();
Shared.Init(Dev);
float Dt = 0.1f;
int LastMouseX = 0;
int LastMouseY = 0;
NuklearAPI.QueueForceUpdate();
while (!Raylib.WindowShouldClose()) {
Vector2 MousePos = Raylib.GetMousePosition();
if (LastMouseX != (int)MousePos.X || LastMouseY != (int)MousePos.Y) {
LastMouseX = (int)MousePos.X;
LastMouseY = (int)MousePos.Y;
Dev.OnMouseMove(LastMouseX, LastMouseY);
}
if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON))
Dev.OnMouseButton(NuklearEvent.MouseButton.Left, LastMouseX, LastMouseY, true);
if (Raylib.IsMouseButtonReleased(MouseButton.MOUSE_LEFT_BUTTON))
Dev.OnMouseButton(NuklearEvent.MouseButton.Left, LastMouseX, LastMouseY, false);
Raylib.BeginDrawing();
Raylib.ClearBackground(Color.BLACK);
Shared.DrawLoop(Dt);
Raylib.EndDrawing();
Dt = SWatch.ElapsedMilliseconds / 1000.0f;
SWatch.Restart();
}
Environment.Exit(0);
}
}
}