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