Mercurial > repos > blastem
annotate nuklear_ui/nuklear_sdl_gles2.h @ 2167:fe68a9ad509f
Merge
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 23 Jul 2022 10:55:44 -0700 |
parents | 0757da8ee702 |
children | 1838b0b8f4ff |
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 |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 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
|
29 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
|
30 NK_API void nk_sdl_font_stash_end(void); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 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
|
32 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
|
33 NK_API void nk_sdl_shutdown(void); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 NK_API void nk_sdl_device_destroy(void); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 NK_API void nk_sdl_device_create(void); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 #endif |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 |
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 * ============================================================== |
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 * IMPLEMENTATION |
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 */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 #ifdef NK_SDL_GLES2_IMPLEMENTATION |
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 #include <string.h> |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 |
1825
56a1171e29b9
Allow Nuklear UI to be used when OpenGL is disabled
Michael Pavone <pavone@retrodev.com>
parents:
1658
diff
changeset
|
50 #ifndef DISABLE_OPENGL |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
51 struct nk_sdl_device { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 struct nk_buffer cmds; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
53 struct nk_draw_null_texture null; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 GLuint vbo, ebo; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 GLuint prog; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
56 GLuint vert_shdr; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 GLuint frag_shdr; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
58 GLint attrib_pos; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 GLint attrib_uv; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
60 GLint attrib_col; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 GLint uniform_tex; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
62 GLint uniform_proj; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 GLuint font_tex; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 GLsizei vs; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 size_t vp, vt, vc; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 }; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
67 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
68 struct nk_sdl_vertex { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 GLfloat position[2]; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 GLfloat uv[2]; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 nk_byte col[4]; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 }; |
1825
56a1171e29b9
Allow Nuklear UI to be used when OpenGL is disabled
Michael Pavone <pavone@retrodev.com>
parents:
1658
diff
changeset
|
73 #endif |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
75 static struct nk_sdl { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
76 SDL_Window *win; |
1825
56a1171e29b9
Allow Nuklear UI to be used when OpenGL is disabled
Michael Pavone <pavone@retrodev.com>
parents:
1658
diff
changeset
|
77 #ifndef DISABLE_OPENGL |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
78 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
|
79 #endif |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
80 struct nk_context ctx; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
81 struct nk_font_atlas atlas; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
82 } sdl; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
83 |
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
|
84 #ifdef USE_GLES |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
85 #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
|
86 #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
|
87 #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
|
88 #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
|
89 #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
|
90 #endif |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
91 |
1825
56a1171e29b9
Allow Nuklear UI to be used when OpenGL is disabled
Michael Pavone <pavone@retrodev.com>
parents:
1658
diff
changeset
|
92 #ifndef DISABLE_OPENGL |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
93 NK_API void |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
94 nk_sdl_device_create(void) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
95 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
96 GLint status; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
97 static const GLchar *vertex_shader = |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
98 NK_SHADER_VERSION |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
99 "uniform mat4 ProjMtx;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
100 "attribute vec2 Position;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
101 "attribute vec2 TexCoord;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
102 "attribute vec4 Color;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
103 "varying vec2 Frag_UV;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
104 "varying vec4 Frag_Color;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
105 "void main() {\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
106 " Frag_UV = TexCoord;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
107 " Frag_Color = Color;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
108 " gl_Position = ProjMtx * vec4(Position.xy, 0, 1);\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
109 "}\n"; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
110 static const GLchar *fragment_shader = |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
111 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
|
112 DECLARE_PRECISION |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
113 "uniform sampler2D Texture;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
114 "varying vec2 Frag_UV;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
115 "varying vec4 Frag_Color;\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
116 "void main(){\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
117 " gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV);\n" |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
118 "}\n"; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
119 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
120 struct nk_sdl_device *dev = &sdl.ogl; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
121 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
122 nk_buffer_init_default(&dev->cmds); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
123 dev->prog = glCreateProgram(); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
124 dev->vert_shdr = glCreateShader(GL_VERTEX_SHADER); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
125 dev->frag_shdr = glCreateShader(GL_FRAGMENT_SHADER); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
126 glShaderSource(dev->vert_shdr, 1, &vertex_shader, 0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
127 glShaderSource(dev->frag_shdr, 1, &fragment_shader, 0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
128 glCompileShader(dev->vert_shdr); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
129 glCompileShader(dev->frag_shdr); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
130 glGetShaderiv(dev->vert_shdr, GL_COMPILE_STATUS, &status); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
131 assert(status == GL_TRUE); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
132 glGetShaderiv(dev->frag_shdr, GL_COMPILE_STATUS, &status); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
133 assert(status == GL_TRUE); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
134 glAttachShader(dev->prog, dev->vert_shdr); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
135 glAttachShader(dev->prog, dev->frag_shdr); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
136 glLinkProgram(dev->prog); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
137 glGetProgramiv(dev->prog, GL_LINK_STATUS, &status); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
138 assert(status == GL_TRUE); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
139 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
140 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
141 dev->uniform_tex = glGetUniformLocation(dev->prog, "Texture"); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
142 dev->uniform_proj = glGetUniformLocation(dev->prog, "ProjMtx"); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
143 dev->attrib_pos = glGetAttribLocation(dev->prog, "Position"); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
144 dev->attrib_uv = glGetAttribLocation(dev->prog, "TexCoord"); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
145 dev->attrib_col = glGetAttribLocation(dev->prog, "Color"); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
146 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
147 dev->vs = sizeof(struct nk_sdl_vertex); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
148 dev->vp = offsetof(struct nk_sdl_vertex, position); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
149 dev->vt = offsetof(struct nk_sdl_vertex, uv); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
150 dev->vc = offsetof(struct nk_sdl_vertex, col); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
151 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
152 /* Allocate buffers */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
153 glGenBuffers(1, &dev->vbo); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
154 glGenBuffers(1, &dev->ebo); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
155 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
156 glBindTexture(GL_TEXTURE_2D, 0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
157 glBindBuffer(GL_ARRAY_BUFFER, 0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
158 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
159 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
160 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
161 NK_INTERN void |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
162 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
|
163 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
164 struct nk_sdl_device *dev = &sdl.ogl; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
165 glGenTextures(1, &dev->font_tex); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
166 glBindTexture(GL_TEXTURE_2D, dev->font_tex); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
167 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
|
168 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
|
169 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
|
170 GL_RGBA, GL_UNSIGNED_BYTE, image); |
2011
579fe3f6fe76
Improved texture atlas size estimation in Nuklear
Mike Pavone <pavone@retrodev.com>
parents:
1862
diff
changeset
|
171 GLenum err = glGetError(); |
579fe3f6fe76
Improved texture atlas size estimation in Nuklear
Mike Pavone <pavone@retrodev.com>
parents:
1862
diff
changeset
|
172 if (err != GL_NO_ERROR) { |
579fe3f6fe76
Improved texture atlas size estimation in Nuklear
Mike Pavone <pavone@retrodev.com>
parents:
1862
diff
changeset
|
173 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
|
174 } |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
175 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
176 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
177 NK_API void |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
178 nk_sdl_device_destroy(void) |
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 struct nk_sdl_device *dev = &sdl.ogl; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
181 glDetachShader(dev->prog, dev->vert_shdr); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
182 glDetachShader(dev->prog, dev->frag_shdr); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
183 glDeleteShader(dev->vert_shdr); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
184 glDeleteShader(dev->frag_shdr); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
185 glDeleteProgram(dev->prog); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
186 glDeleteTextures(1, &dev->font_tex); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
187 glDeleteBuffers(1, &dev->vbo); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
188 glDeleteBuffers(1, &dev->ebo); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
189 nk_buffer_free(&dev->cmds); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
190 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
191 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
192 NK_API void |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
193 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
|
194 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
195 struct nk_sdl_device *dev = &sdl.ogl; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
196 int width, height; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
197 int display_width, display_height; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
198 struct nk_vec2 scale; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
199 GLfloat ortho[4][4] = { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
200 {2.0f, 0.0f, 0.0f, 0.0f}, |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
201 {0.0f,-2.0f, 0.0f, 0.0f}, |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
202 {0.0f, 0.0f,-1.0f, 0.0f}, |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
203 {-1.0f,1.0f, 0.0f, 1.0f}, |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
204 }; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
205 SDL_GetWindowSize(sdl.win, &width, &height); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
206 SDL_GL_GetDrawableSize(sdl.win, &display_width, &display_height); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
207 ortho[0][0] /= (GLfloat)width; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
208 ortho[1][1] /= (GLfloat)height; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
209 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
210 scale.x = (float)display_width/(float)width; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
211 scale.y = (float)display_height/(float)height; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
212 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
213 /* setup global state */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
214 glViewport(0,0,display_width,display_height); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
215 glEnable(GL_BLEND); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
216 glBlendEquation(GL_FUNC_ADD); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
217 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
218 glDisable(GL_CULL_FACE); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
219 glDisable(GL_DEPTH_TEST); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
220 glEnable(GL_SCISSOR_TEST); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
221 glActiveTexture(GL_TEXTURE0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
222 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
223 /* setup program */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
224 glUseProgram(dev->prog); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
225 glUniform1i(dev->uniform_tex, 0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
226 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
|
227 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
228 /* 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
|
229 const struct nk_draw_command *cmd; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
230 void *vertices, *elements; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
231 const nk_draw_index *offset = NULL; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
232 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
233 /* Bind buffers */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
234 glBindBuffer(GL_ARRAY_BUFFER, dev->vbo); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
235 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dev->ebo); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
236 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
237 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
238 /* buffer setup */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
239 glEnableVertexAttribArray((GLuint)dev->attrib_pos); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
240 glEnableVertexAttribArray((GLuint)dev->attrib_uv); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
241 glEnableVertexAttribArray((GLuint)dev->attrib_col); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
242 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
243 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
|
244 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
|
245 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
|
246 } |
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 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
|
249 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
|
250 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
251 /* load vertices/elements directly into vertex/element buffer */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
252 vertices = malloc((size_t)max_vertex_buffer); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
253 elements = malloc((size_t)max_element_buffer); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
254 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
255 /* fill convert configuration */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
256 struct nk_convert_config config; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
257 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
|
258 {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
|
259 {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
|
260 {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
|
261 {NK_VERTEX_LAYOUT_END} |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
262 }; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
263 NK_MEMSET(&config, 0, sizeof(config)); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
264 config.vertex_layout = vertex_layout; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
265 config.vertex_size = sizeof(struct nk_sdl_vertex); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
266 config.vertex_alignment = NK_ALIGNOF(struct nk_sdl_vertex); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
267 config.null = dev->null; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
268 config.circle_segment_count = 22; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
269 config.curve_segment_count = 22; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
270 config.arc_segment_count = 22; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
271 config.global_alpha = 1.0f; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
272 config.shape_AA = AA; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
273 config.line_AA = AA; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
274 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
275 /* setup buffers to load vertices and elements */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
276 {struct nk_buffer vbuf, ebuf; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
277 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
|
278 nk_buffer_init_fixed(&ebuf, elements, (nk_size)max_element_buffer); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
279 nk_convert(&sdl.ctx, &dev->cmds, &vbuf, &ebuf, &config);} |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
280 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
281 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
|
282 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
|
283 free(vertices); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
284 free(elements); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
285 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
286 /* iterate over and execute each draw command */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
287 nk_draw_foreach(cmd, &sdl.ctx, &dev->cmds) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
288 if (!cmd->elem_count) continue; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
289 glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
290 glScissor((GLint)(cmd->clip_rect.x * scale.x), |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
291 (GLint)((height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h)) * scale.y), |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
292 (GLint)(cmd->clip_rect.w * scale.x), |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
293 (GLint)(cmd->clip_rect.h * scale.y)); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
294 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
|
295 offset += cmd->elem_count; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
296 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
297 nk_clear(&sdl.ctx); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
298 } |
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 glUseProgram(0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
301 glBindBuffer(GL_ARRAY_BUFFER, 0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
302 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
303 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
304 glDisable(GL_BLEND); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
305 glDisable(GL_SCISSOR_TEST); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
306 } |
1825
56a1171e29b9
Allow Nuklear UI to be used when OpenGL is disabled
Michael Pavone <pavone@retrodev.com>
parents:
1658
diff
changeset
|
307 #endif |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
308 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
309 static void |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
310 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
|
311 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
312 const char *text = SDL_GetClipboardText(); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
313 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
|
314 (void)usr; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
315 } |
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 static void |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
318 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
|
319 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
320 char *str = 0; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
321 (void)usr; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
322 if (!len) return; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
323 str = (char*)malloc((size_t)len+1); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
324 if (!str) return; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
325 memcpy(str, text, (size_t)len); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
326 str[len] = '\0'; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
327 SDL_SetClipboardText(str); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
328 free(str); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
329 } |
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 NK_API struct nk_context* |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
332 nk_sdl_init(SDL_Window *win) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
333 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
334 sdl.win = win; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
335 nk_init_default(&sdl.ctx, 0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
336 sdl.ctx.clip.copy = nk_sdl_clipbard_copy; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
337 sdl.ctx.clip.paste = nk_sdl_clipbard_paste; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
338 sdl.ctx.clip.userdata = nk_handle_ptr(0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
339 return &sdl.ctx; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
340 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
341 |
1825
56a1171e29b9
Allow Nuklear UI to be used when OpenGL is disabled
Michael Pavone <pavone@retrodev.com>
parents:
1658
diff
changeset
|
342 #ifndef DISABLE_OPENGL |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
343 NK_API void |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
344 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
|
345 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
346 nk_font_atlas_init_default(&sdl.atlas); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
347 nk_font_atlas_begin(&sdl.atlas); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
348 *atlas = &sdl.atlas; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
349 } |
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 NK_API void |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
352 nk_sdl_font_stash_end(void) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
353 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
354 const void *image; int w, h; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
355 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
|
356 nk_sdl_device_upload_atlas(image, w, h); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
357 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
|
358 if (sdl.atlas.default_font) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
359 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
|
360 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
361 } |
1825
56a1171e29b9
Allow Nuklear UI to be used when OpenGL is disabled
Michael Pavone <pavone@retrodev.com>
parents:
1658
diff
changeset
|
362 #endif |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
363 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
364 NK_API int |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
365 nk_sdl_handle_event(SDL_Event *evt) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
366 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
367 struct nk_context *ctx = &sdl.ctx; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
368 if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
369 /* key events */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
370 int down = evt->type == SDL_KEYDOWN; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
371 const Uint8* state = SDL_GetKeyboardState(0); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
372 SDL_Keycode sym = evt->key.keysym.sym; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
373 if (sym == SDLK_RSHIFT || sym == SDLK_LSHIFT) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
374 nk_input_key(ctx, NK_KEY_SHIFT, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
375 else if (sym == SDLK_DELETE) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
376 nk_input_key(ctx, NK_KEY_DEL, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
377 else if (sym == SDLK_RETURN) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
378 nk_input_key(ctx, NK_KEY_ENTER, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
379 else if (sym == SDLK_TAB) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
380 nk_input_key(ctx, NK_KEY_TAB, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
381 else if (sym == SDLK_BACKSPACE) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
382 nk_input_key(ctx, NK_KEY_BACKSPACE, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
383 else if (sym == SDLK_HOME) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
384 nk_input_key(ctx, NK_KEY_TEXT_START, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
385 nk_input_key(ctx, NK_KEY_SCROLL_START, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
386 } else if (sym == SDLK_END) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
387 nk_input_key(ctx, NK_KEY_TEXT_END, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
388 nk_input_key(ctx, NK_KEY_SCROLL_END, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
389 } else if (sym == SDLK_PAGEDOWN) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
390 nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
391 } else if (sym == SDLK_PAGEUP) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
392 nk_input_key(ctx, NK_KEY_SCROLL_UP, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
393 } else if (sym == SDLK_z) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
394 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
|
395 else if (sym == SDLK_r) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
396 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
|
397 else if (sym == SDLK_c) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
398 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
|
399 else if (sym == SDLK_v) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
400 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
|
401 else if (sym == SDLK_x) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
402 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
|
403 else if (sym == SDLK_b) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
404 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
|
405 else if (sym == SDLK_e) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
406 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
|
407 else if (sym == SDLK_UP) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
408 nk_input_key(ctx, NK_KEY_UP, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
409 else if (sym == SDLK_DOWN) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
410 nk_input_key(ctx, NK_KEY_DOWN, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
411 else if (sym == SDLK_LEFT) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
412 if (state[SDL_SCANCODE_LCTRL]) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
413 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
|
414 else nk_input_key(ctx, NK_KEY_LEFT, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
415 } else if (sym == SDLK_RIGHT) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
416 if (state[SDL_SCANCODE_LCTRL]) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
417 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
|
418 else nk_input_key(ctx, NK_KEY_RIGHT, down); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
419 } else return 0; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
420 return 1; |
1862
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
421 } 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
|
422 int down = evt->type == SDL_CONTROLLERBUTTONDOWN; |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
423 if (evt->cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP) { |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
424 nk_input_key(ctx, NK_KEY_UP, down); |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
425 } 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
|
426 nk_input_key(ctx, NK_KEY_DOWN, down); |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
427 } 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
|
428 nk_input_key(ctx, NK_KEY_ENTER, down); |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
429 } |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
430 } else if (evt->type == SDL_CONTROLLERAXISMOTION) { |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
431 if (evt->caxis.axis == SDL_CONTROLLER_AXIS_LEFTY || evt->caxis.axis == SDL_CONTROLLER_AXIS_RIGHTY) { |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
432 int down = abs(evt->caxis.value) > 2000; |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
433 if (evt->caxis.value >= 0) { |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
434 if (ctx->input.keyboard.keys[NK_KEY_UP].down) { |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
435 nk_input_key(ctx, NK_KEY_UP, 0); |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
436 } |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
437 nk_input_key(ctx, NK_KEY_DOWN, down); |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
438 } else { |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
439 if (ctx->input.keyboard.keys[NK_KEY_DOWN].down) { |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
440 nk_input_key(ctx, NK_KEY_DOWN, 0); |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
441 } |
e07fc3d473b2
Basic UI navigation with controller
Michael Pavone <pavone@retrodev.com>
parents:
1825
diff
changeset
|
442 nk_input_key(ctx, NK_KEY_UP, down); |
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 } |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
445 } 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
|
446 /* mouse button */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
447 int down = evt->type == SDL_MOUSEBUTTONDOWN; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
448 const int x = evt->button.x, y = evt->button.y; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
449 if (evt->button.button == SDL_BUTTON_LEFT) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
450 if (evt->button.clicks > 1) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
451 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
|
452 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
|
453 } else if (evt->button.button == SDL_BUTTON_MIDDLE) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
454 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
|
455 else if (evt->button.button == SDL_BUTTON_RIGHT) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
456 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
|
457 return 1; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
458 } else if (evt->type == SDL_MOUSEMOTION) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
459 /* mouse motion */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
460 if (ctx->input.mouse.grabbed) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
461 int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
462 nk_input_motion(ctx, x + evt->motion.xrel, y + evt->motion.yrel); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
463 } else nk_input_motion(ctx, evt->motion.x, evt->motion.y); |
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_TEXTINPUT) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
466 /* text input */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
467 nk_glyph glyph; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
468 memcpy(glyph, evt->text.text, NK_UTF_SIZE); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
469 nk_input_glyph(ctx, glyph); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
470 return 1; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
471 } else if (evt->type == SDL_MOUSEWHEEL) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
472 /* mouse wheel */ |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
473 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
|
474 return 1; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
475 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
476 return 0; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
477 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
478 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
479 NK_API |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
480 void nk_sdl_shutdown(void) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
481 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
482 nk_font_atlas_clear(&sdl.atlas); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
483 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
|
484 #ifndef DISABLE_OPENGL |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
485 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
|
486 #endif |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
487 memset(&sdl, 0, sizeof(sdl)); |
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 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
490 #endif |