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
|
#define WITH_D3D
#include <rw.h>
#include <skeleton.h>
#include <assert.h>
#include "imgui/imgui.h"
#include "imgui_impl_rw.h"
using namespace rw::RWDEVICE;
static rw::Texture *g_FontTexture;
static Im2DVertex *g_vertbuf;
static int g_vertbufSize;
// TODO: scissor from librw itself
static void
SetClip(const ImVec4 &clip)
{
ImGuiIO &io = ImGui::GetIO();
float x1 = clip.x; float y1 = clip.y;
float x2 = clip.z; float y2 = clip.w;
#ifdef RW_OPENGL
glScissor(x1, io.DisplaySize.y-y2, x2-x1, y2-y1);
glEnable(GL_SCISSOR_TEST);
#endif
#ifdef RW_D3D9
rw::d3d::d3ddevice->SetRenderState(D3DRS_SCISSORTESTENABLE, 1);
RECT r = { (LONG)x1, (LONG)y1, (LONG)x2, (LONG)y2 };
rw::d3d::d3ddevice->SetScissorRect(&r);
#endif
}
static void
DisableClip(void)
{
#ifdef RW_OPENGL
glDisable(GL_SCISSOR_TEST);
#endif
#ifdef RW_D3D9
rw::d3d::d3ddevice->SetRenderState(D3DRS_SCISSORTESTENABLE, 0);
#endif
}
void
ImGui_ImplRW_RenderDrawLists(ImDrawData* draw_data)
{
ImGuiIO &io = ImGui::GetIO();
// minimized
if (io.DisplaySize.x <= 0.0f || io.DisplaySize.y <= 0.0f)
return;
if(g_vertbuf == nil || g_vertbufSize < draw_data->TotalVtxCount){
if(g_vertbuf){
rwFree(g_vertbuf);
g_vertbuf = nil;
}
g_vertbufSize = draw_data->TotalVtxCount + 5000;
g_vertbuf = rwNewT(Im2DVertex, g_vertbufSize, 0);
}
float xoff = 0.0f;
float yoff = 0.0f;
#ifdef RWHALFPIXEL
xoff = -0.5;
yoff = 0.5;
#endif
rw::Camera *cam = (rw::Camera*)rw::engine->currentCamera;
Im2DVertex *vtx_dst = g_vertbuf;
float recipZ = 1.0f/cam->nearPlane;
for(int n = 0; n < draw_data->CmdListsCount; n++){
const ImDrawList *cmd_list = draw_data->CmdLists[n];
const ImDrawVert *vtx_src = cmd_list->VtxBuffer.Data;
for(int i = 0; i < cmd_list->VtxBuffer.Size; i++){
vtx_dst[i].setScreenX(vtx_src[i].pos.x + xoff);
vtx_dst[i].setScreenY(vtx_src[i].pos.y + yoff);
vtx_dst[i].setScreenZ(rw::im2d::GetNearZ());
vtx_dst[i].setCameraZ(cam->nearPlane);
vtx_dst[i].setRecipCameraZ(recipZ);
vtx_dst[i].setColor(vtx_src[i].col&0xFF, vtx_src[i].col>>8 & 0xFF, vtx_src[i].col>>16 & 0xFF, vtx_src[i].col>>24 & 0xFF);
vtx_dst[i].setU(vtx_src[i].uv.x, recipZ);
vtx_dst[i].setV(vtx_src[i].uv.y, recipZ);
}
vtx_dst += cmd_list->VtxBuffer.Size;
}
int vertexAlpha = rw::GetRenderState(rw::VERTEXALPHA);
int srcBlend = rw::GetRenderState(rw::SRCBLEND);
int dstBlend = rw::GetRenderState(rw::DESTBLEND);
int ztest = rw::GetRenderState(rw::ZTESTENABLE);
void *tex = rw::GetRenderStatePtr(rw::TEXTURERASTER);
int addrU = rw::GetRenderState(rw::TEXTUREADDRESSU);
int addrV = rw::GetRenderState(rw::TEXTUREADDRESSV);
int filter = rw::GetRenderState(rw::TEXTUREFILTER);
int cullmode = rw::GetRenderState(rw::CULLMODE);
rw::SetRenderState(rw::VERTEXALPHA, 1);
rw::SetRenderState(rw::SRCBLEND, rw::BLENDSRCALPHA);
rw::SetRenderState(rw::DESTBLEND, rw::BLENDINVSRCALPHA);
rw::SetRenderState(rw::ZTESTENABLE, 0);
rw::SetRenderState(rw::CULLMODE, rw::CULLNONE);
int vtx_offset = 0;
for(int n = 0; n < draw_data->CmdListsCount; n++){
const ImDrawList *cmd_list = draw_data->CmdLists[n];
int idx_offset = 0;
for(int i = 0; i < cmd_list->CmdBuffer.Size; i++){
const ImDrawCmd *pcmd = &cmd_list->CmdBuffer[i];
if(pcmd->UserCallback)
pcmd->UserCallback(cmd_list, pcmd);
else{
rw::Texture *tex = (rw::Texture*)pcmd->GetTexID();
if(tex && tex->raster){
rw::SetRenderStatePtr(rw::TEXTURERASTER, tex->raster);
rw::SetRenderState(rw::TEXTUREADDRESSU, tex->getAddressU());
rw::SetRenderState(rw::TEXTUREADDRESSV, tex->getAddressV());
rw::SetRenderState(rw::TEXTUREFILTER, tex->getFilter());
}else
rw::SetRenderStatePtr(rw::TEXTURERASTER, nil);
SetClip(pcmd->ClipRect);
rw::im2d::RenderIndexedPrimitive(rw::PRIMTYPETRILIST,
g_vertbuf+vtx_offset, cmd_list->VtxBuffer.Size,
cmd_list->IdxBuffer.Data+idx_offset, pcmd->ElemCount);
DisableClip();
}
idx_offset += pcmd->ElemCount;
}
vtx_offset += cmd_list->VtxBuffer.Size;
}
rw::SetRenderState(rw::VERTEXALPHA,vertexAlpha);
rw::SetRenderState(rw::SRCBLEND, srcBlend);
rw::SetRenderState(rw::DESTBLEND, dstBlend);
rw::SetRenderState(rw::ZTESTENABLE, ztest);
rw::SetRenderStatePtr(rw::TEXTURERASTER, tex);
rw::SetRenderState(rw::TEXTUREADDRESSU, addrU);
rw::SetRenderState(rw::TEXTUREADDRESSV, addrV);
rw::SetRenderState(rw::TEXTUREFILTER, filter);
rw::SetRenderState(rw::CULLMODE, cullmode);
}
bool
ImGui_ImplRW_Init(void)
{
using namespace sk;
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
return true;
}
void
ImGui_ImplRW_Shutdown(void)
{
}
static bool
ImGui_ImplRW_CreateFontsTexture()
{
// Build texture atlas
ImGuiIO &io = ImGui::GetIO();
unsigned char *pixels;
int width, height;
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height, nil);
rw::Image *image;
image = rw::Image::create(width, height, 32);
image->allocate();
for(int y = 0; y < height; y++)
memcpy(image->pixels + image->stride*y, pixels + width*4* y, width*4);
g_FontTexture = rw::Texture::create(rw::Raster::createFromImage(image));
g_FontTexture->setFilter(rw::Texture::LINEAR);
image->destroy();
// Store our identifier
io.Fonts->TexID = (void*)g_FontTexture;
return true;
}
bool
ImGui_ImplRW_CreateDeviceObjects()
{
if(!ImGui_ImplRW_CreateFontsTexture())
return false;
return true;
}
void
ImGui_ImplRW_NewFrame(float timeDelta)
{
if(!g_FontTexture)
ImGui_ImplRW_CreateDeviceObjects();
ImGuiIO &io = ImGui::GetIO();
io.DisplaySize = ImVec2(sk::globals.width, sk::globals.height);
io.DeltaTime = timeDelta;
io.KeyCtrl = ImGui::IsKeyPressed(ImGuiKey_LeftCtrl) || ImGui::IsKeyPressed(ImGuiKey_RightCtrl);
io.KeyShift = ImGui::IsKeyPressed(ImGuiKey_LeftShift) || ImGui::IsKeyPressed(ImGuiKey_RightShift);
io.KeyAlt = ImGui::IsKeyPressed(ImGuiKey_LeftAlt) || ImGui::IsKeyPressed(ImGuiKey_RightAlt);
io.KeySuper = false;
if(io.WantSetMousePos)
sk::SetMousePosition(io.MousePos.x, io.MousePos.y);
ImGui::NewFrame();
}
static ImGuiKey SkKeyToImGuiKey(int keycode)
{
switch (keycode) {
case ' ': return ImGuiKey_Space;
case '\'': return ImGuiKey_Apostrophe;
case '-': return ImGuiKey_Minus;
case '.': return ImGuiKey_Period;
case '/': return ImGuiKey_Slash;
case '0': return ImGuiKey_0;
case '1': return ImGuiKey_1;
case '2': return ImGuiKey_2;
case '3': return ImGuiKey_3;
case '4': return ImGuiKey_4;
case '5': return ImGuiKey_5;
case '6': return ImGuiKey_6;
case '7': return ImGuiKey_7;
case '8': return ImGuiKey_8;
case '9': return ImGuiKey_9;
case ';': return ImGuiKey_Semicolon;
case '=': return ImGuiKey_Equal;
case 'A': return ImGuiKey_A;
case 'B': return ImGuiKey_B;
case 'C': return ImGuiKey_C;
case 'D': return ImGuiKey_D;
case 'E': return ImGuiKey_E;
case 'F': return ImGuiKey_F;
case 'G': return ImGuiKey_G;
case 'H': return ImGuiKey_H;
case 'I': return ImGuiKey_I;
case 'J': return ImGuiKey_J;
case 'K': return ImGuiKey_K;
case 'L': return ImGuiKey_L;
case 'M': return ImGuiKey_M;
case 'N': return ImGuiKey_N;
case 'O': return ImGuiKey_O;
case 'P': return ImGuiKey_P;
case 'Q': return ImGuiKey_Q;
case 'R': return ImGuiKey_R;
case 'S': return ImGuiKey_S;
case 'T': return ImGuiKey_T;
case 'U': return ImGuiKey_U;
case 'V': return ImGuiKey_V;
case 'W': return ImGuiKey_W;
case 'X': return ImGuiKey_X;
case 'Y': return ImGuiKey_Y;
case 'Z': return ImGuiKey_Z;
case '[': return ImGuiKey_LeftBracket;
case '\\': return ImGuiKey_Backslash;
case ']': return ImGuiKey_RightBracket;
case '`': return ImGuiKey_GraveAccent;
case sk::KEY_ESC: return ImGuiKey_Escape;
case sk::KEY_ENTER: return ImGuiKey_Enter;
case sk::KEY_TAB: return ImGuiKey_Tab;
case sk::KEY_BACKSP: return ImGuiKey_Backspace;
case sk::KEY_INS: return ImGuiKey_Insert;
case sk::KEY_DEL: return ImGuiKey_Delete;
case sk::KEY_RIGHT: return ImGuiKey_RightArrow;
case sk::KEY_LEFT: return ImGuiKey_LeftArrow;
case sk::KEY_DOWN: return ImGuiKey_DownArrow;
case sk::KEY_UP: return ImGuiKey_UpArrow;
case sk::KEY_PGUP: return ImGuiKey_PageUp;
case sk::KEY_PGDN: return ImGuiKey_PageDown;
case sk::KEY_HOME: return ImGuiKey_Home;
case sk::KEY_END: return ImGuiKey_End;
case sk::KEY_CAPSLK: return ImGuiKey_CapsLock;
case sk::KEY_F1: return ImGuiKey_F1;
case sk::KEY_F2: return ImGuiKey_F2;
case sk::KEY_F3: return ImGuiKey_F3;
case sk::KEY_F4: return ImGuiKey_F4;
case sk::KEY_F5: return ImGuiKey_F5;
case sk::KEY_F6: return ImGuiKey_F6;
case sk::KEY_F7: return ImGuiKey_F7;
case sk::KEY_F8: return ImGuiKey_F8;
case sk::KEY_F9: return ImGuiKey_F9;
case sk::KEY_F10: return ImGuiKey_F10;
case sk::KEY_F11: return ImGuiKey_F11;
case sk::KEY_F12: return ImGuiKey_F12;
case sk::KEY_LSHIFT: return ImGuiKey_LeftShift;
case sk::KEY_LCTRL: return ImGuiKey_LeftCtrl;
case sk::KEY_LALT: return ImGuiKey_LeftAlt;
case sk::KEY_RSHIFT: return ImGuiKey_RightShift;
case sk::KEY_RCTRL: return ImGuiKey_RightCtrl;
case sk::KEY_RALT: return ImGuiKey_RightAlt;
case sk::KEY_NULL: return ImGuiKey_None;
}
return ImGuiKey_None;
}
sk::EventStatus
ImGuiEventHandler(sk::Event e, void *param)
{
using namespace sk;
ImGuiIO &io = ImGui::GetIO();
MouseState *ms;
uint c;
switch(e){
case KEYDOWN:
io.AddKeyEvent(SkKeyToImGuiKey(*(int*)param), true);
return EVENTPROCESSED;
case KEYUP:
io.AddKeyEvent(SkKeyToImGuiKey(*(int*)param), false);
return EVENTPROCESSED;
case CHARINPUT:
c = (uint)(uintptr)param;
io.AddInputCharacter((unsigned short)c);
return EVENTPROCESSED;
case MOUSEMOVE:
ms = (MouseState*)param;
io.MousePos.x = ms->posx;
io.MousePos.y = ms->posy;
return EVENTPROCESSED;
case MOUSEBTN:
ms = (MouseState*)param;
io.MouseDown[0] = !!(ms->buttons & 1);
io.MouseDown[2] = !!(ms->buttons & 2);
io.MouseDown[1] = !!(ms->buttons & 4);
return EVENTPROCESSED;
case MOUSEWHEEL:
ms = (MouseState*)param;
io.MouseWheel += ms->wheelDelta;
return EVENTPROCESSED;
}
return EVENTPROCESSED;
}
|