Mercurial > repos > blastem
annotate nuklear_ui/nuklear_sdl_gles2.h @ 2456:72d0eac49507
Implement movep in new 68K core
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 21 Feb 2024 20:09:11 -0800 |
parents | 1c7329ac7f3f |
children |
rev | line source |
---|---|
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 /* |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 * Nuklear - 1.40.8 - public domain |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 * no warrenty implied; use at your own risk. |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 * authored from 2015-2017 by Micha Mettke |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 * emscripten from 2016 by Chris Willcocks |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 * OpenGL ES 2.0 from 2017 by Dmitry Hrabrov a.k.a. DeXPeriX |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 /* |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 * ============================================================== |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 * |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 * API |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 * |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 * =============================================================== |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 #ifndef NK_SDL_GLES2_H_ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 #define NK_SDL_GLES2_H_ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 |
1496
b82a44729d7c
Fix GL include issue for 32-bit builds
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
18 #include <SDL.h> |
b82a44729d7c
Fix GL include issue for 32-bit builds
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
19 #ifndef DISABLE_OPENGL |
1658
fa9ae059e4d3
Added support for GLES in addition to desktop GL
Mike Pavone <pavone@retrodev.com>
parents:
1538
diff
changeset
|
20 #ifdef USE_GLES |
fa9ae059e4d3
Added support for GLES in addition to desktop GL
Mike Pavone <pavone@retrodev.com>
parents:
1538
diff
changeset
|
21 #include <SDL_opengles2.h> |
fa9ae059e4d3
Added support for GLES in addition to desktop GL
Mike Pavone <pavone@retrodev.com>
parents:
1538
diff
changeset
|
22 #else |
1496
b82a44729d7c
Fix GL include issue for 32-bit builds
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
23 #include <GL/glew.h> |
b82a44729d7c
Fix GL include issue for 32-bit builds
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
24 #endif |
1658
fa9ae059e4d3
Added support for GLES in addition to desktop GL
Mike Pavone <pavone@retrodev.com>
parents:
1538
diff
changeset
|
25 #endif |
2318
1c7329ac7f3f
Make UI respect stick deadzone
Michael Pavone <pavone@retrodev.com>
parents:
2316
diff
changeset
|
26 #include "../render_sdl.h" |
2308
b7768c58f0da
Initial stab at DPI scaling support
Michael Pavone <pavone@retrodev.com>
parents:
2249
diff
changeset
|
27 #include "../render.h" |
2318
1c7329ac7f3f
Make UI respect stick deadzone
Michael Pavone <pavone@retrodev.com>
parents:
2316
diff
changeset
|
28 #include "../controller_info.h" |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
30 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 NK_API struct nk_context* nk_sdl_init(SDL_Window *win); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 NK_API void nk_sdl_font_stash_begin(struct nk_font_atlas **atlas); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 NK_API void nk_sdl_font_stash_end(void); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 NK_API int nk_sdl_handle_event(SDL_Event *evt); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 NK_API void nk_sdl_render(enum nk_anti_aliasing , int max_vertex_buffer, int max_element_buffer); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 NK_API void nk_sdl_shutdown(void); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 NK_API void nk_sdl_device_destroy(void); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 NK_API void nk_sdl_device_create(void); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 #endif |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
42 /* |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 * ============================================================== |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
44 * |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 * IMPLEMENTATION |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 * |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 * =============================================================== |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 #ifdef NK_SDL_GLES2_IMPLEMENTATION |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
51 #include <string.h> |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 |
1825
56a1171e29b9
Allow Nuklear UI to be used when OpenGL is disabled
Michael Pavone <pavone@retrodev.com>
parents:
1658
diff
changeset
|
53 #ifndef DISABLE_OPENGL |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 struct nk_sdl_device { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 struct nk_buffer cmds; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
56 struct nk_draw_null_texture null; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 GLuint vbo, ebo; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
58 GLuint prog; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 GLuint vert_shdr; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
60 GLuint frag_shdr; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 GLint attrib_pos; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
62 GLint attrib_uv; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 GLint attrib_col; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 GLint uniform_tex; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 GLint uniform_proj; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 GLuint font_tex; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
67 GLsizei vs; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
68 size_t vp, vt, vc; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 }; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 struct nk_sdl_vertex { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 GLfloat position[2]; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
73 GLfloat uv[2]; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 nk_byte col[4]; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
75 }; |
1825
56a1171e29b9
Allow Nuklear UI to be used when OpenGL is disabled
Michael Pavone <pavone@retrodev.com>
parents:
1658
diff
changeset
|
76 #endif |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
77 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
78 static struct nk_sdl { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
79 SDL_Window *win; |
1825
56a1171e29b9
Allow Nuklear UI to be used when OpenGL is disabled
Michael Pavone <pavone@retrodev.com>
parents:
1658
diff
changeset
|
80 #ifndef DISABLE_OPENGL |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
81 struct nk_sdl_device ogl; |
1825
56a1171e29b9
Allow Nuklear UI to be used when OpenGL is disabled
Michael Pavone <pavone@retrodev.com>
parents:
1658
diff
changeset
|
82 #endif |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
83 struct nk_context ctx; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
84 struct nk_font_atlas atlas; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
85 } sdl; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
86 |
1538
b581663c42ec
Don't try to use GLES specific stuff unless we're actualy on a GLES platform
Michael Pavone <pavone@retrodev.com>
parents:
1496
diff
changeset
|
87 #ifdef USE_GLES |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
88 #define NK_SHADER_VERSION "#version 100\n" |
1538
b581663c42ec
Don't try to use GLES specific stuff unless we're actualy on a GLES platform
Michael Pavone <pavone@retrodev.com>
parents:
1496
diff
changeset
|
89 #define DECLARE_PRECISION "precision mediump float;\n" |
b581663c42ec
Don't try to use GLES specific stuff unless we're actualy on a GLES platform
Michael Pavone <pavone@retrodev.com>
parents:
1496
diff
changeset
|
90 #else |
b581663c42ec
Don't try to use GLES specific stuff unless we're actualy on a GLES platform
Michael Pavone <pavone@retrodev.com>
parents:
1496
diff
changeset
|
91 #define NK_SHADER_VERSION "#version 110\n" |
b581663c42ec
Don't try to use GLES specific stuff unless we're actualy on a GLES platform
Michael Pavone <pavone@retrodev.com>
parents:
1496
diff
changeset
|
92 #define DECLARE_PRECISION |
b581663c42ec
Don't try to use GLES specific stuff unless we're actualy on a GLES platform
Michael Pavone <pavone@retrodev.com>
parents:
1496
diff
changeset
|
93 #endif |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
94 |
1825
56a1171e29b9
Allow Nuklear UI to be used when OpenGL is disabled
Michael Pavone <pavone@retrodev.com>
parents:
1658
diff
changeset
|
95 #ifndef DISABLE_OPENGL |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
96 NK_API void |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
97 nk_sdl_device_create(void) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
98 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
99 GLint status; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
100 static const GLchar *vertex_shader = |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
101 NK_SHADER_VERSION |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
102 "uniform mat4 ProjMtx;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
103 "attribute vec2 Position;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
104 "attribute vec2 TexCoord;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
105 "attribute vec4 Color;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
106 "varying vec2 Frag_UV;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
107 "varying vec4 Frag_Color;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
108 "void main() {\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
109 " Frag_UV = TexCoord;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
110 " Frag_Color = Color;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
111 " gl_Position = ProjMtx * vec4(Position.xy, 0, 1);\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
112 "}\n"; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
113 static const GLchar *fragment_shader = |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
114 NK_SHADER_VERSION |
1538
b581663c42ec
Don't try to use GLES specific stuff unless we're actualy on a GLES platform
Michael Pavone <pavone@retrodev.com>
parents:
1496
diff
changeset
|
115 DECLARE_PRECISION |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
116 "uniform sampler2D Texture;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
117 "varying vec2 Frag_UV;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
118 "varying vec4 Frag_Color;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
119 "void main(){\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
120 " gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV);\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
121 "}\n"; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
122 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
123 struct nk_sdl_device *dev = &sdl.ogl; |
2308
b7768c58f0da
Initial stab at DPI scaling support
Michael Pavone <pavone@retrodev.com>
parents:
2249
diff
changeset
|
124 |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
125 nk_buffer_init_default(&dev->cmds); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
126 dev->prog = glCreateProgram(); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
127 dev->vert_shdr = glCreateShader(GL_VERTEX_SHADER); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
128 dev->frag_shdr = glCreateShader(GL_FRAGMENT_SHADER); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
129 glShaderSource(dev->vert_shdr, 1, &vertex_shader, 0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
130 glShaderSource(dev->frag_shdr, 1, &fragment_shader, 0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
131 glCompileShader(dev->vert_shdr); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
132 glCompileShader(dev->frag_shdr); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
133 glGetShaderiv(dev->vert_shdr, GL_COMPILE_STATUS, &status); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
134 assert(status == GL_TRUE); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
135 glGetShaderiv(dev->frag_shdr, GL_COMPILE_STATUS, &status); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
136 assert(status == GL_TRUE); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
137 glAttachShader(dev->prog, dev->vert_shdr); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
138 glAttachShader(dev->prog, dev->frag_shdr); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
139 glLinkProgram(dev->prog); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
140 glGetProgramiv(dev->prog, GL_LINK_STATUS, &status); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
141 assert(status == GL_TRUE); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
142 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
143 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
144 dev->uniform_tex = glGetUniformLocation(dev->prog, "Texture"); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
145 dev->uniform_proj = glGetUniformLocation(dev->prog, "ProjMtx"); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
146 dev->attrib_pos = glGetAttribLocation(dev->prog, "Position"); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
147 dev->attrib_uv = glGetAttribLocation(dev->prog, "TexCoord"); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
148 dev->attrib_col = glGetAttribLocation(dev->prog, "Color"); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
149 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
150 dev->vs = sizeof(struct nk_sdl_vertex); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
151 dev->vp = offsetof(struct nk_sdl_vertex, position); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
152 dev->vt = offsetof(struct nk_sdl_vertex, uv); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
153 dev->vc = offsetof(struct nk_sdl_vertex, col); |
2308
b7768c58f0da
Initial stab at DPI scaling support
Michael Pavone <pavone@retrodev.com>
parents:
2249
diff
changeset
|
154 |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
155 /* Allocate buffers */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
156 glGenBuffers(1, &dev->vbo); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
157 glGenBuffers(1, &dev->ebo); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
158 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
159 glBindTexture(GL_TEXTURE_2D, 0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
160 glBindBuffer(GL_ARRAY_BUFFER, 0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
161 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
162 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
163 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
164 NK_INTERN void |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
165 nk_sdl_device_upload_atlas(const void *image, int width, int height) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
166 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
167 struct nk_sdl_device *dev = &sdl.ogl; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
168 glGenTextures(1, &dev->font_tex); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
169 glBindTexture(GL_TEXTURE_2D, dev->font_tex); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
170 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
171 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
172 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0, |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
173 GL_RGBA, GL_UNSIGNED_BYTE, image); |
2011
579fe3f6fe76
Improved texture atlas size estimation in Nuklear
Mike Pavone <pavone@retrodev.com>
parents:
1862
diff
changeset
|
174 GLenum err = glGetError(); |
579fe3f6fe76
Improved texture atlas size estimation in Nuklear
Mike Pavone <pavone@retrodev.com>
parents:
1862
diff
changeset
|
175 if (err != GL_NO_ERROR) { |
579fe3f6fe76
Improved texture atlas size estimation in Nuklear
Mike Pavone <pavone@retrodev.com>
parents:
1862
diff
changeset
|
176 printf("glTexImage2D failed with error %d\n", err); |
579fe3f6fe76
Improved texture atlas size estimation in Nuklear
Mike Pavone <pavone@retrodev.com>
parents:
1862
diff
changeset
|
177 } |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
178 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
179 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
180 NK_API void |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
181 nk_sdl_device_destroy(void) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
182 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
183 struct nk_sdl_device *dev = &sdl.ogl; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
184 glDetachShader(dev->prog, dev->vert_shdr); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
185 glDetachShader(dev->prog, dev->frag_shdr); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
186 glDeleteShader(dev->vert_shdr); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
187 glDeleteShader(dev->frag_shdr); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
188 glDeleteProgram(dev->prog); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
189 glDeleteTextures(1, &dev->font_tex); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
190 glDeleteBuffers(1, &dev->vbo); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
191 glDeleteBuffers(1, &dev->ebo); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
192 nk_buffer_free(&dev->cmds); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
193 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
194 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
195 NK_API void |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
196 nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
197 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
198 struct nk_sdl_device *dev = &sdl.ogl; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
199 int display_width, display_height; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
200 struct nk_vec2 scale; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
201 GLfloat ortho[4][4] = { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
202 {2.0f, 0.0f, 0.0f, 0.0f}, |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
203 {0.0f,-2.0f, 0.0f, 0.0f}, |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
204 {0.0f, 0.0f,-1.0f, 0.0f}, |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
205 {-1.0f,1.0f, 0.0f, 1.0f}, |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
206 }; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
207 SDL_GL_GetDrawableSize(sdl.win, &display_width, &display_height); |
2309
5e34369ed6be
Fix DPI scaling when OpenGL is enabled
Michael Pavone <pavone@retrodev.com>
parents:
2308
diff
changeset
|
208 ortho[0][0] /= (GLfloat)display_width; |
5e34369ed6be
Fix DPI scaling when OpenGL is enabled
Michael Pavone <pavone@retrodev.com>
parents:
2308
diff
changeset
|
209 ortho[1][1] /= (GLfloat)display_height; |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
210 |
2309
5e34369ed6be
Fix DPI scaling when OpenGL is enabled
Michael Pavone <pavone@retrodev.com>
parents:
2308
diff
changeset
|
211 scale.x = 1.0f; |
5e34369ed6be
Fix DPI scaling when OpenGL is enabled
Michael Pavone <pavone@retrodev.com>
parents:
2308
diff
changeset
|
212 scale.y = 1.0f; |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
213 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
214 /* setup global state */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
215 glViewport(0,0,display_width,display_height); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
216 glEnable(GL_BLEND); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
217 glBlendEquation(GL_FUNC_ADD); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
218 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
219 glDisable(GL_CULL_FACE); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
220 glDisable(GL_DEPTH_TEST); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
221 glEnable(GL_SCISSOR_TEST); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
222 glActiveTexture(GL_TEXTURE0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
223 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
224 /* setup program */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
225 glUseProgram(dev->prog); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
226 glUniform1i(dev->uniform_tex, 0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
227 glUniformMatrix4fv(dev->uniform_proj, 1, GL_FALSE, &ortho[0][0]); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
228 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
229 /* convert from command queue into draw list and draw to screen */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
230 const struct nk_draw_command *cmd; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
231 void *vertices, *elements; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
232 const nk_draw_index *offset = NULL; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
233 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
234 /* Bind buffers */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
235 glBindBuffer(GL_ARRAY_BUFFER, dev->vbo); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
236 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dev->ebo); |
2308
b7768c58f0da
Initial stab at DPI scaling support
Michael Pavone <pavone@retrodev.com>
parents:
2249
diff
changeset
|
237 |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
238 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
239 /* buffer setup */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
240 glEnableVertexAttribArray((GLuint)dev->attrib_pos); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
241 glEnableVertexAttribArray((GLuint)dev->attrib_uv); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
242 glEnableVertexAttribArray((GLuint)dev->attrib_col); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
243 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
244 glVertexAttribPointer((GLuint)dev->attrib_pos, 2, GL_FLOAT, GL_FALSE, dev->vs, (void*)dev->vp); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
245 glVertexAttribPointer((GLuint)dev->attrib_uv, 2, GL_FLOAT, GL_FALSE, dev->vs, (void*)dev->vt); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
246 glVertexAttribPointer((GLuint)dev->attrib_col, 4, GL_UNSIGNED_BYTE, GL_TRUE, dev->vs, (void*)dev->vc); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
247 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
248 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
249 glBufferData(GL_ARRAY_BUFFER, max_vertex_buffer, NULL, GL_STREAM_DRAW); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
250 glBufferData(GL_ELEMENT_ARRAY_BUFFER, max_element_buffer, NULL, GL_STREAM_DRAW); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
251 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
252 /* load vertices/elements directly into vertex/element buffer */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
253 vertices = malloc((size_t)max_vertex_buffer); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
254 elements = malloc((size_t)max_element_buffer); |
2249
1838b0b8f4ff
Fix use of variable after it left scope bug identified by asan
Michael Pavone <pavone@retrodev.com>
parents:
2031
diff
changeset
|
255 struct nk_buffer vbuf, ebuf; |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
256 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
257 /* fill convert configuration */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
258 struct nk_convert_config config; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
259 static const struct nk_draw_vertex_layout_element vertex_layout[] = { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
260 {NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_sdl_vertex, position)}, |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
261 {NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_sdl_vertex, uv)}, |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
262 {NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct nk_sdl_vertex, col)}, |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
263 {NK_VERTEX_LAYOUT_END} |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
264 }; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
265 NK_MEMSET(&config, 0, sizeof(config)); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
266 config.vertex_layout = vertex_layout; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
267 config.vertex_size = sizeof(struct nk_sdl_vertex); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
268 config.vertex_alignment = NK_ALIGNOF(struct nk_sdl_vertex); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
269 config.null = dev->null; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
270 config.circle_segment_count = 22; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
271 config.curve_segment_count = 22; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
272 config.arc_segment_count = 22; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
273 config.global_alpha = 1.0f; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
274 config.shape_AA = AA; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
275 config.line_AA = AA; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
276 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
277 /* setup buffers to load vertices and elements */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
278 nk_buffer_init_fixed(&vbuf, vertices, (nk_size)max_vertex_buffer); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
279 nk_buffer_init_fixed(&ebuf, elements, (nk_size)max_element_buffer); |
2249
1838b0b8f4ff
Fix use of variable after it left scope bug identified by asan
Michael Pavone <pavone@retrodev.com>
parents:
2031
diff
changeset
|
280 nk_convert(&sdl.ctx, &dev->cmds, &vbuf, &ebuf, &config); |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
281 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
282 glBufferSubData(GL_ARRAY_BUFFER, 0, (size_t)max_vertex_buffer, vertices); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
283 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, (size_t)max_element_buffer, elements); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
284 free(vertices); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
285 free(elements); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
286 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
287 /* iterate over and execute each draw command */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
288 nk_draw_foreach(cmd, &sdl.ctx, &dev->cmds) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
289 if (!cmd->elem_count) continue; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
290 glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
291 glScissor((GLint)(cmd->clip_rect.x * scale.x), |
2309
5e34369ed6be
Fix DPI scaling when OpenGL is enabled
Michael Pavone <pavone@retrodev.com>
parents:
2308
diff
changeset
|
292 (GLint)((display_height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h)) * scale.y), |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
293 (GLint)(cmd->clip_rect.w * scale.x), |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
294 (GLint)(cmd->clip_rect.h * scale.y)); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
295 glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
296 offset += cmd->elem_count; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
297 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
298 nk_clear(&sdl.ctx); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
299 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
300 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
301 glUseProgram(0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
302 glBindBuffer(GL_ARRAY_BUFFER, 0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
303 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
304 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
305 glDisable(GL_BLEND); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
306 glDisable(GL_SCISSOR_TEST); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
307 } |
1825
56a1171e29b9
Allow Nuklear UI to be used when OpenGL is disabled
Michael Pavone <pavone@retrodev.com>
parents:
1658
diff
changeset
|
308 #endif |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
309 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
310 static void |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
311 nk_sdl_clipbard_paste(nk_handle usr, struct nk_text_edit *edit) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
312 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
313 const char *text = SDL_GetClipboardText(); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
314 if (text) nk_textedit_paste(edit, text, nk_strlen(text)); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
315 (void)usr; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
316 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
317 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
318 static void |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
319 nk_sdl_clipbard_copy(nk_handle usr, const char *text, int len) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
320 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
321 char *str = 0; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
322 (void)usr; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
323 if (!len) return; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
324 str = (char*)malloc((size_t)len+1); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
325 if (!str) return; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
326 memcpy(str, text, (size_t)len); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
327 str[len] = '\0'; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
328 SDL_SetClipboardText(str); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
329 free(str); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
330 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
331 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
332 NK_API struct nk_context* |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
333 nk_sdl_init(SDL_Window *win) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
334 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
335 sdl.win = win; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
336 nk_init_default(&sdl.ctx, 0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
337 sdl.ctx.clip.copy = nk_sdl_clipbard_copy; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
338 sdl.ctx.clip.paste = nk_sdl_clipbard_paste; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
339 sdl.ctx.clip.userdata = nk_handle_ptr(0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
340 return &sdl.ctx; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
341 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
342 |
1825
56a1171e29b9
Allow Nuklear UI to be used when OpenGL is disabled
Michael Pavone <pavone@retrodev.com>
parents:
1658
diff
changeset
|
343 #ifndef DISABLE_OPENGL |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
344 NK_API void |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
345 nk_sdl_font_stash_begin(struct nk_font_atlas **atlas) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
346 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
347 nk_font_atlas_init_default(&sdl.atlas); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
348 nk_font_atlas_begin(&sdl.atlas); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
349 *atlas = &sdl.atlas; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
350 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
351 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
352 NK_API void |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
353 nk_sdl_font_stash_end(void) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
354 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
355 const void *image; int w, h; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
356 image = nk_font_atlas_bake(&sdl.atlas, &w, &h, NK_FONT_ATLAS_RGBA32); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
357 nk_sdl_device_upload_atlas(image, w, h); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
358 nk_font_atlas_end(&sdl.atlas, nk_handle_id((int)sdl.ogl.font_tex), &sdl.ogl.null); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
359 if (sdl.atlas.default_font) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
360 nk_style_set_font(&sdl.ctx, &sdl.atlas.default_font->handle); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
361 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
362 } |
1825
56a1171e29b9
Allow Nuklear UI to be used when OpenGL is disabled
Michael Pavone <pavone@retrodev.com>
parents:
1658
diff
changeset
|
363 #endif |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
364 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
365 NK_API int |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
366 nk_sdl_handle_event(SDL_Event *evt) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
367 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
368 struct nk_context *ctx = &sdl.ctx; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
369 if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
370 /* key events */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
371 int down = evt->type == SDL_KEYDOWN; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
372 const Uint8* state = SDL_GetKeyboardState(0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
373 SDL_Keycode sym = evt->key.keysym.sym; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
374 if (sym == SDLK_RSHIFT || sym == SDLK_LSHIFT) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
375 nk_input_key(ctx, NK_KEY_SHIFT, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
376 else if (sym == SDLK_DELETE) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
377 nk_input_key(ctx, NK_KEY_DEL, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
378 else if (sym == SDLK_RETURN) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
379 nk_input_key(ctx, NK_KEY_ENTER, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
380 else if (sym == SDLK_TAB) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
381 nk_input_key(ctx, NK_KEY_TAB, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
382 else if (sym == SDLK_BACKSPACE) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
383 nk_input_key(ctx, NK_KEY_BACKSPACE, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
384 else if (sym == SDLK_HOME) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
385 nk_input_key(ctx, NK_KEY_TEXT_START, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
386 nk_input_key(ctx, NK_KEY_SCROLL_START, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
387 } else if (sym == SDLK_END) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
388 nk_input_key(ctx, NK_KEY_TEXT_END, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
389 nk_input_key(ctx, NK_KEY_SCROLL_END, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
390 } else if (sym == SDLK_PAGEDOWN) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
391 nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
392 } else if (sym == SDLK_PAGEUP) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
393 nk_input_key(ctx, NK_KEY_SCROLL_UP, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
394 } else if (sym == SDLK_z) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
395 nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && state[SDL_SCANCODE_LCTRL]); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
396 else if (sym == SDLK_r) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
397 nk_input_key(ctx, NK_KEY_TEXT_REDO, down && state[SDL_SCANCODE_LCTRL]); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
398 else if (sym == SDLK_c) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
399 nk_input_key(ctx, NK_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
400 else if (sym == SDLK_v) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
401 nk_input_key(ctx, NK_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
402 else if (sym == SDLK_x) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
403 nk_input_key(ctx, NK_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
404 else if (sym == SDLK_b) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
405 nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && state[SDL_SCANCODE_LCTRL]); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
406 else if (sym == SDLK_e) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
407 nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && state[SDL_SCANCODE_LCTRL]); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
408 else if (sym == SDLK_UP) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
409 nk_input_key(ctx, NK_KEY_UP, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
410 else if (sym == SDLK_DOWN) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
411 nk_input_key(ctx, NK_KEY_DOWN, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
412 else if (sym == SDLK_LEFT) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
413 if (state[SDL_SCANCODE_LCTRL]) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
414 nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
415 else nk_input_key(ctx, NK_KEY_LEFT, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
416 } else if (sym == SDLK_RIGHT) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
417 if (state[SDL_SCANCODE_LCTRL]) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
418 nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
419 else nk_input_key(ctx, NK_KEY_RIGHT, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
420 } else return 0; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
421 return 1; |
1862
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
422 } else if (evt->type == SDL_CONTROLLERBUTTONDOWN || evt->type == SDL_CONTROLLERBUTTONUP) { |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
423 int down = evt->type == SDL_CONTROLLERBUTTONDOWN; |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
424 if (evt->cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP) { |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
425 nk_input_key(ctx, NK_KEY_UP, down); |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
426 } else if (evt->cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN) { |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
427 nk_input_key(ctx, NK_KEY_DOWN, down); |
2316
523ab225815b
Allow dpad to increment/decrement property fields
Michael Pavone <pavone@retrodev.com>
parents:
2309
diff
changeset
|
428 } else if (evt->cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_LEFT) { |
523ab225815b
Allow dpad to increment/decrement property fields
Michael Pavone <pavone@retrodev.com>
parents:
2309
diff
changeset
|
429 nk_input_key(ctx, NK_KEY_LEFT, down); |
523ab225815b
Allow dpad to increment/decrement property fields
Michael Pavone <pavone@retrodev.com>
parents:
2309
diff
changeset
|
430 } else if (evt->cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_RIGHT) { |
523ab225815b
Allow dpad to increment/decrement property fields
Michael Pavone <pavone@retrodev.com>
parents:
2309
diff
changeset
|
431 nk_input_key(ctx, NK_KEY_RIGHT, down); |
1862
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
432 } else if (evt->cbutton.button == SDL_CONTROLLER_BUTTON_A || evt->cbutton.button == SDL_CONTROLLER_BUTTON_START) { |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
433 nk_input_key(ctx, NK_KEY_ENTER, down); |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
434 } |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
435 } else if (evt->type == SDL_CONTROLLERAXISMOTION) { |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
436 if (evt->caxis.axis == SDL_CONTROLLER_AXIS_LEFTY || evt->caxis.axis == SDL_CONTROLLER_AXIS_RIGHTY) { |
2318
1c7329ac7f3f
Make UI respect stick deadzone
Michael Pavone <pavone@retrodev.com>
parents:
2316
diff
changeset
|
437 int joystick = render_find_joystick_index(evt->caxis.which); |
1c7329ac7f3f
Make UI respect stick deadzone
Michael Pavone <pavone@retrodev.com>
parents:
2316
diff
changeset
|
438 controller_info info = get_controller_info(joystick); |
1c7329ac7f3f
Make UI respect stick deadzone
Michael Pavone <pavone@retrodev.com>
parents:
2316
diff
changeset
|
439 int down = abs(evt->caxis.value) > info.stick_deadzone; |
1862
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
440 if (evt->caxis.value >= 0) { |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
441 if (ctx->input.keyboard.keys[NK_KEY_UP].down) { |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
442 nk_input_key(ctx, NK_KEY_UP, 0); |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
443 } |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
444 nk_input_key(ctx, NK_KEY_DOWN, down); |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
445 } else { |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
446 if (ctx->input.keyboard.keys[NK_KEY_DOWN].down) { |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
447 nk_input_key(ctx, NK_KEY_DOWN, 0); |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
448 } |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
449 nk_input_key(ctx, NK_KEY_UP, down); |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
450 } |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
451 } |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
452 } else if (evt->type == SDL_MOUSEBUTTONDOWN || evt->type == SDL_MOUSEBUTTONUP) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
453 /* mouse button */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
454 int down = evt->type == SDL_MOUSEBUTTONDOWN; |
2308
b7768c58f0da
Initial stab at DPI scaling support
Michael Pavone <pavone@retrodev.com>
parents:
2249
diff
changeset
|
455 const int x = render_ui_to_pixels_x(evt->button.x), y = render_ui_to_pixels_y(evt->button.y); |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
456 if (evt->button.button == SDL_BUTTON_LEFT) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
457 if (evt->button.clicks > 1) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
458 nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
459 nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
460 } else if (evt->button.button == SDL_BUTTON_MIDDLE) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
461 nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
462 else if (evt->button.button == SDL_BUTTON_RIGHT) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
463 nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
464 return 1; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
465 } else if (evt->type == SDL_MOUSEMOTION) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
466 /* mouse motion */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
467 if (ctx->input.mouse.grabbed) { |
2308
b7768c58f0da
Initial stab at DPI scaling support
Michael Pavone <pavone@retrodev.com>
parents:
2249
diff
changeset
|
468 int x = render_ui_to_pixels_x((int)ctx->input.mouse.prev.x), y = render_ui_to_pixels_y((int)ctx->input.mouse.prev.y); |
b7768c58f0da
Initial stab at DPI scaling support
Michael Pavone <pavone@retrodev.com>
parents:
2249
diff
changeset
|
469 nk_input_motion(ctx, x + render_ui_to_pixels_x(evt->motion.xrel), y + render_ui_to_pixels_y(evt->motion.yrel)); |
b7768c58f0da
Initial stab at DPI scaling support
Michael Pavone <pavone@retrodev.com>
parents:
2249
diff
changeset
|
470 } else nk_input_motion(ctx, render_ui_to_pixels_x(evt->motion.x), render_ui_to_pixels_y(evt->motion.y)); |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
471 return 1; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
472 } else if (evt->type == SDL_TEXTINPUT) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
473 /* text input */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
474 nk_glyph glyph; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
475 memcpy(glyph, evt->text.text, NK_UTF_SIZE); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
476 nk_input_glyph(ctx, glyph); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
477 return 1; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
478 } else if (evt->type == SDL_MOUSEWHEEL) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
479 /* mouse wheel */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
480 nk_input_scroll(ctx,nk_vec2((float)evt->wheel.x,(float)evt->wheel.y)); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
481 return 1; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
482 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
483 return 0; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
484 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
485 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
486 NK_API |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
487 void nk_sdl_shutdown(void) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
488 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
489 nk_font_atlas_clear(&sdl.atlas); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
490 nk_free(&sdl.ctx); |
1825
56a1171e29b9
Allow Nuklear UI to be used when OpenGL is disabled
Michael Pavone <pavone@retrodev.com>
parents:
1658
diff
changeset
|
491 #ifndef DISABLE_OPENGL |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
492 nk_sdl_device_destroy(); |
1825
56a1171e29b9
Allow Nuklear UI to be used when OpenGL is disabled
Michael Pavone <pavone@retrodev.com>
parents:
1658
diff
changeset
|
493 #endif |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
494 memset(&sdl, 0, sizeof(sdl)); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
495 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
496 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
497 #endif |