1
+ // Adapted from https://github.com/nx-python/imgui-switch/blob/master/src/main.cpp
2
+ #include < switch.h>
3
+
4
+ #include " Python.h"
5
+
6
+ #include " imgui/imgui.h"
7
+ #include " imgui/imgui_sw.hpp"
8
+
9
+ #include < vector>
10
+
11
+ u32 * framebuf;
12
+ int width, height;
13
+ std::vector<uint32_t > * pixel_buffer;
14
+ bool initialized = false ;
15
+
16
+ PyDoc_STRVAR (module_doc, " Helper functions for pyimgui on switch\n " );
17
+
18
+
19
+ PyDoc_STRVAR (imguihelper_initialize_doc, " " ); // TODO
20
+
21
+ static PyObject *
22
+ imguihelper_initialize (PyObject *self, PyObject *args)
23
+ {
24
+ gfxSetMode (GfxMode_LinearDouble);
25
+
26
+ if (initialized) Py_RETURN_NONE;
27
+
28
+
29
+ framebuf = (u32*) gfxGetFramebuffer ((u32*)&width, (u32*)&height);
30
+ pixel_buffer = new std::vector<uint32_t >(width * height, 0 );
31
+
32
+ ImGuiIO& io = ImGui::GetIO ();
33
+ io.MouseDrawCursor = true ;
34
+ io.DisplaySize = ImVec2 ((float )width, (float )height);
35
+ io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
36
+ ImGui::GetStyle ().ScaleAllSizes (2 );
37
+ ImGui::GetStyle ().MouseCursorScale = 1 ;
38
+
39
+ imgui_sw::bind_imgui_painting ();
40
+ imgui_sw::make_style_fast ();
41
+ initialized = true ;
42
+
43
+ Py_RETURN_NONE;
44
+ }
45
+
46
+
47
+
48
+ PyDoc_STRVAR (imguihelper_handleinputs_doc, " " ); // TODO
49
+
50
+ static PyObject *
51
+ imguihelper_handleinputs (PyObject *self, PyObject *args)
52
+ {
53
+ ImGuiIO& io = ImGui::GetIO ();
54
+ hidScanInput ();
55
+ u32 kHeld = hidKeysHeld (CONTROLLER_P1_AUTO);
56
+ JoystickPosition pos;
57
+ hidJoystickRead (&pos, CONTROLLER_P1_AUTO, JOYSTICK_LEFT);
58
+
59
+ memset (io.NavInputs , 0 , sizeof (io.NavInputs ));
60
+
61
+ #define MAP_BUTTON (NAV, BUTTON ) { if (kHeld & BUTTON) io.NavInputs [NAV] = 1 .0f ; }
62
+ MAP_BUTTON (ImGuiNavInput_Activate, KEY_A);
63
+ MAP_BUTTON (ImGuiNavInput_Cancel, KEY_B);
64
+ MAP_BUTTON (ImGuiNavInput_Menu, KEY_Y);
65
+ MAP_BUTTON (ImGuiNavInput_Input, KEY_X);
66
+ MAP_BUTTON (ImGuiNavInput_DpadLeft, KEY_DLEFT);
67
+ MAP_BUTTON (ImGuiNavInput_DpadRight, KEY_DRIGHT);
68
+ MAP_BUTTON (ImGuiNavInput_DpadUp, KEY_DUP);
69
+ MAP_BUTTON (ImGuiNavInput_DpadDown, KEY_DDOWN);
70
+ MAP_BUTTON (ImGuiNavInput_FocusPrev, KEY_L);
71
+ MAP_BUTTON (ImGuiNavInput_FocusNext, KEY_R);
72
+ MAP_BUTTON (ImGuiNavInput_TweakSlow, KEY_L);
73
+ MAP_BUTTON (ImGuiNavInput_TweakFast, KEY_R);
74
+ MAP_BUTTON (ImGuiNavInput_LStickLeft, KEY_LSTICK_LEFT);
75
+ MAP_BUTTON (ImGuiNavInput_LStickRight, KEY_LSTICK_RIGHT);
76
+ MAP_BUTTON (ImGuiNavInput_LStickUp, KEY_LSTICK_UP);
77
+ MAP_BUTTON (ImGuiNavInput_LStickDown, KEY_LSTICK_DOWN);
78
+ #undef MAP_BUTTON
79
+
80
+ io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
81
+
82
+
83
+ // io.MousePos = ImVec2(-FLT_MAX,-FLT_MAX);
84
+ io.MouseDown [0 ] = false ;
85
+ unsigned long touch_count = hidTouchCount ();
86
+ if (touch_count > 0 ) {
87
+ touchPosition pos;
88
+ hidTouchRead (&pos, 0 );
89
+ io.MousePos = ImVec2 (pos.px , pos.py );
90
+ io.MouseDown [0 ] = true ;
91
+ }
92
+
93
+ Py_RETURN_NONE;
94
+ }
95
+
96
+
97
+
98
+ PyDoc_STRVAR (imguihelper_render_doc, " " ); // TODO
99
+
100
+ static PyObject *
101
+ imguihelper_render (PyObject *self, PyObject *args)
102
+ {
103
+ // fill gray (this could be any previous rendering)
104
+ std::fill_n (pixel_buffer->data (), width * height, 0x19191919u );
105
+
106
+ // overlay the GUI
107
+ imgui_sw::paint_imgui (pixel_buffer->data (), width, height);
108
+
109
+ for (int i = 0 ; i < height; i++) {
110
+ for (int j = 0 ; j < width; j++) {
111
+ u32 pos = i * width + j;
112
+ framebuf[pos] = pixel_buffer->data ()[pos];
113
+ }
114
+ }
115
+
116
+ gfxFlushBuffers ();
117
+ gfxSwapBuffers ();
118
+ gfxWaitForVsync ();
119
+
120
+ Py_RETURN_NONE;
121
+ }
122
+
123
+
124
+
125
+ PyDoc_STRVAR (imguihelper_shutdown_doc, " " ); // TODO
126
+
127
+ static PyObject *
128
+ imguihelper_shutdown (PyObject *self, PyObject *args)
129
+ {
130
+ imgui_sw::unbind_imgui_painting ();
131
+ delete pixel_buffer;
132
+ initialized = false ;
133
+
134
+ Py_RETURN_NONE;
135
+ }
136
+
137
+
138
+
139
+ static PyMethodDef module_methods[] = {
140
+ {" initialize" , imguihelper_initialize, METH_NOARGS, imguihelper_initialize_doc},
141
+ {" handleinputs" , imguihelper_handleinputs, METH_NOARGS, imguihelper_handleinputs_doc},
142
+ {" render" , imguihelper_render, METH_NOARGS, imguihelper_render_doc},
143
+ {" shutdown" , imguihelper_shutdown, METH_NOARGS, imguihelper_shutdown_doc},
144
+ {NULL , NULL }
145
+ };
146
+
147
+ struct PyModuleDef _imguihelper_Module = {
148
+ PyModuleDef_HEAD_INIT,
149
+ " imguihelper" ,
150
+ module_doc,
151
+ -1 ,
152
+ module_methods,
153
+ NULL ,
154
+ NULL ,
155
+ NULL ,
156
+ NULL ,
157
+ };
158
+
159
+ PyMODINIT_FUNC
160
+ PyInit_imguihelper (void )
161
+ {
162
+ PyObject *m = PyModule_Create (&_imguihelper_Module);
163
+ return m;
164
+ }
0 commit comments