| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#ifndef NK_SDL_GL2_H_ |
| 14 |
#define NK_SDL_GL2_H_ |
| 15 |
|
| 16 |
#include <SDL.h> |
| 17 |
NK_API struct nk_context* nk_sdl_init(SDL_Window *win); |
| 18 |
NK_API void nk_sdl_font_stash_begin(struct nk_font_atlas **atlas); |
| 19 |
NK_API void nk_sdl_font_stash_end(void); |
| 20 |
NK_API int nk_sdl_handle_event(SDL_Event *evt); |
| 21 |
NK_API void nk_sdl_render(enum nk_anti_aliasing); |
| 22 |
NK_API void nk_sdl_shutdown(void); |
| 23 |
|
| 24 |
#endif |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#ifdef NK_SDL_GL2_IMPLEMENTATION |
| 33 |
|
| 34 |
struct nk_sdl_device { |
| 35 |
struct nk_buffer cmds; |
| 36 |
struct nk_draw_null_texture null; |
| 37 |
GLuint font_tex; |
| 38 |
}; |
| 39 |
|
| 40 |
struct nk_sdl_vertex { |
| 41 |
float position[2]; |
| 42 |
float uv[2]; |
| 43 |
nk_byte col[4]; |
| 44 |
}; |
| 45 |
|
| 46 |
static struct nk_sdl { |
| 47 |
SDL_Window *win; |
| 48 |
struct nk_sdl_device ogl; |
| 49 |
struct nk_context ctx; |
| 50 |
struct nk_font_atlas atlas; |
| 51 |
} sdl; |
| 52 |
|
| 53 |
NK_INTERN void |
| 54 |
nk_sdl_device_upload_atlas(const void *image, int width, int height) |
| 55 |
{ |
| 56 |
struct nk_sdl_device *dev = &sdl.ogl; |
| 57 |
glGenTextures(1, &dev->font_tex); |
| 58 |
glBindTexture(GL_TEXTURE_2D, dev->font_tex); |
| 59 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 60 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 61 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0, |
| 62 |
GL_RGBA, GL_UNSIGNED_BYTE, image); |
| 63 |
} |
| 64 |
|
| 65 |
NK_API void |
| 66 |
nk_sdl_render(enum nk_anti_aliasing AA) |
| 67 |
{ |
| 68 |
|
| 69 |
struct nk_sdl_device *dev = &sdl.ogl; |
| 70 |
int width, height; |
| 71 |
int display_width, display_height; |
| 72 |
struct nk_vec2 scale; |
| 73 |
|
| 74 |
SDL_GetWindowSize(sdl.win, &width, &height); |
| 75 |
SDL_GL_GetDrawableSize(sdl.win, &display_width, &display_height); |
| 76 |
scale.x = (float)display_width/(float)width; |
| 77 |
scale.y = (float)display_height/(float)height; |
| 78 |
|
| 79 |
glPushAttrib(GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT|GL_TRANSFORM_BIT); |
| 80 |
glDisable(GL_CULL_FACE); |
| 81 |
glDisable(GL_DEPTH_TEST); |
| 82 |
glEnable(GL_SCISSOR_TEST); |
| 83 |
glEnable(GL_BLEND); |
| 84 |
glEnable(GL_TEXTURE_2D); |
| 85 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 86 |
|
| 87 |
|
| 88 |
glViewport(0,0,(GLsizei)display_width,(GLsizei)display_height); |
| 89 |
glMatrixMode(GL_PROJECTION); |
| 90 |
glPushMatrix(); |
| 91 |
glLoadIdentity(); |
| 92 |
glOrtho(0.0f, width, height, 0.0f, -1.0f, 1.0f); |
| 93 |
glMatrixMode(GL_MODELVIEW); |
| 94 |
glPushMatrix(); |
| 95 |
glLoadIdentity(); |
| 96 |
|
| 97 |
glEnableClientState(GL_VERTEX_ARRAY); |
| 98 |
glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 99 |
glEnableClientState(GL_COLOR_ARRAY); |
| 100 |
{ |
| 101 |
GLsizei vs = sizeof(struct nk_sdl_vertex); |
| 102 |
size_t vp = offsetof(struct nk_sdl_vertex, position); |
| 103 |
size_t vt = offsetof(struct nk_sdl_vertex, uv); |
| 104 |
size_t vc = offsetof(struct nk_sdl_vertex, col); |
| 105 |
|
| 106 |
|
| 107 |
const struct nk_draw_command *cmd; |
| 108 |
const nk_draw_index *offset = NULL; |
| 109 |
struct nk_buffer vbuf, ebuf; |
| 110 |
|
| 111 |
|
| 112 |
struct nk_convert_config config; |
| 113 |
static const struct nk_draw_vertex_layout_element vertex_layout[] = { |
| 114 |
{NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_sdl_vertex, position)}, |
| 115 |
{NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_sdl_vertex, uv)}, |
| 116 |
{NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct nk_sdl_vertex, col)}, |
| 117 |
{NK_VERTEX_LAYOUT_END} |
| 118 |
}; |
| 119 |
NK_MEMSET(&config, 0, sizeof(config)); |
| 120 |
config.vertex_layout = vertex_layout; |
| 121 |
config.vertex_size = sizeof(struct nk_sdl_vertex); |
| 122 |
config.vertex_alignment = NK_ALIGNOF(struct nk_sdl_vertex); |
| 123 |
config.null = dev->null; |
| 124 |
config.circle_segment_count = 22; |
| 125 |
config.curve_segment_count = 22; |
| 126 |
config.arc_segment_count = 22; |
| 127 |
config.global_alpha = 1.0f; |
| 128 |
config.shape_AA = AA; |
| 129 |
config.line_AA = AA; |
| 130 |
|
| 131 |
|
| 132 |
nk_buffer_init_default(&vbuf); |
| 133 |
nk_buffer_init_default(&ebuf); |
| 134 |
nk_convert(&sdl.ctx, &dev->cmds, &vbuf, &ebuf, &config); |
| 135 |
|
| 136 |
|
| 137 |
{const void *vertices = nk_buffer_memory_const(&vbuf); |
| 138 |
glVertexPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vp)); |
| 139 |
glTexCoordPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vt)); |
| 140 |
glColorPointer(4, GL_UNSIGNED_BYTE, vs, (const void*)((const nk_byte*)vertices + vc));} |
| 141 |
|
| 142 |
|
| 143 |
offset = (const nk_draw_index*)nk_buffer_memory_const(&ebuf); |
| 144 |
nk_draw_foreach(cmd, &sdl.ctx, &dev->cmds) |
| 145 |
{ |
| 146 |
if (!cmd->elem_count) continue; |
| 147 |
glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id); |
| 148 |
glScissor( |
| 149 |
(GLint)(cmd->clip_rect.x * scale.x), |
| 150 |
(GLint)((height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h)) * scale.y), |
| 151 |
(GLint)(cmd->clip_rect.w * scale.x), |
| 152 |
(GLint)(cmd->clip_rect.h * scale.y)); |
| 153 |
glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset); |
| 154 |
offset += cmd->elem_count; |
| 155 |
} |
| 156 |
nk_clear(&sdl.ctx); |
| 157 |
nk_buffer_free(&vbuf); |
| 158 |
nk_buffer_free(&ebuf); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
glDisableClientState(GL_VERTEX_ARRAY); |
| 163 |
glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 164 |
glDisableClientState(GL_COLOR_ARRAY); |
| 165 |
|
| 166 |
glDisable(GL_CULL_FACE); |
| 167 |
glDisable(GL_DEPTH_TEST); |
| 168 |
glDisable(GL_SCISSOR_TEST); |
| 169 |
glDisable(GL_BLEND); |
| 170 |
glDisable(GL_TEXTURE_2D); |
| 171 |
|
| 172 |
glBindTexture(GL_TEXTURE_2D, 0); |
| 173 |
glMatrixMode(GL_MODELVIEW); |
| 174 |
glPopMatrix(); |
| 175 |
glMatrixMode(GL_PROJECTION); |
| 176 |
glPopMatrix(); |
| 177 |
glPopAttrib(); |
| 178 |
} |
| 179 |
|
| 180 |
static void |
| 181 |
nk_sdl_clipboard_paste(nk_handle usr, struct nk_text_edit *edit) |
| 182 |
{ |
| 183 |
const char *text = SDL_GetClipboardText(); |
| 184 |
if (text) nk_textedit_paste(edit, text, nk_strlen(text)); |
| 185 |
(void)usr; |
| 186 |
} |
| 187 |
|
| 188 |
static void |
| 189 |
nk_sdl_clipboard_copy(nk_handle usr, const char *text, int len) |
| 190 |
{ |
| 191 |
char *str = 0; |
| 192 |
(void)usr; |
| 193 |
if (!len) return; |
| 194 |
str = (char*)malloc((size_t)len+1); |
| 195 |
if (!str) return; |
| 196 |
memcpy(str, text, (size_t)len); |
| 197 |
str[len] = '\0'; |
| 198 |
SDL_SetClipboardText(str); |
| 199 |
free(str); |
| 200 |
} |
| 201 |
|
| 202 |
NK_API struct nk_context* |
| 203 |
nk_sdl_init(SDL_Window *win) |
| 204 |
{ |
| 205 |
sdl.win = win; |
| 206 |
nk_init_default(&sdl.ctx, 0); |
| 207 |
sdl.ctx.clip.copy = nk_sdl_clipboard_copy; |
| 208 |
sdl.ctx.clip.paste = nk_sdl_clipboard_paste; |
| 209 |
sdl.ctx.clip.userdata = nk_handle_ptr(0); |
| 210 |
nk_buffer_init_default(&sdl.ogl.cmds); |
| 211 |
return &sdl.ctx; |
| 212 |
} |
| 213 |
|
| 214 |
NK_API void |
| 215 |
nk_sdl_font_stash_begin(struct nk_font_atlas **atlas) |
| 216 |
{ |
| 217 |
nk_font_atlas_init_default(&sdl.atlas); |
| 218 |
nk_font_atlas_begin(&sdl.atlas); |
| 219 |
*atlas = &sdl.atlas; |
| 220 |
} |
| 221 |
|
| 222 |
NK_API void |
| 223 |
nk_sdl_font_stash_end(void) |
| 224 |
{ |
| 225 |
const void *image; int w, h; |
| 226 |
image = nk_font_atlas_bake(&sdl.atlas, &w, &h, NK_FONT_ATLAS_RGBA32); |
| 227 |
nk_sdl_device_upload_atlas(image, w, h); |
| 228 |
nk_font_atlas_end(&sdl.atlas, nk_handle_id((int)sdl.ogl.font_tex), &sdl.ogl.null); |
| 229 |
if (sdl.atlas.default_font) |
| 230 |
nk_style_set_font(&sdl.ctx, &sdl.atlas.default_font->handle); |
| 231 |
} |
| 232 |
|
| 233 |
NK_API int |
| 234 |
nk_sdl_handle_event(SDL_Event *evt) |
| 235 |
{ |
| 236 |
struct nk_context *ctx = &sdl.ctx; |
| 237 |
const Uint32 windowId = SDL_GetWindowID(sdl.win); |
| 238 |
|
| 239 |
|
| 240 |
if (ctx->input.mouse.grab) { |
| 241 |
SDL_SetRelativeMouseMode(SDL_TRUE); |
| 242 |
ctx->input.mouse.grab = 0; |
| 243 |
} else if (ctx->input.mouse.ungrab) { |
| 244 |
int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y; |
| 245 |
SDL_SetRelativeMouseMode(SDL_FALSE); |
| 246 |
SDL_WarpMouseInWindow(sdl.win, x, y); |
| 247 |
ctx->input.mouse.ungrab = 0; |
| 248 |
} |
| 249 |
if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) { |
| 250 |
|
| 251 |
int down = evt->type == SDL_KEYDOWN; |
| 252 |
const Uint8* state = SDL_GetKeyboardState(0); |
| 253 |
SDL_Keycode sym = evt->key.keysym.sym; |
| 254 |
if (sym == SDLK_RSHIFT || sym == SDLK_LSHIFT) |
| 255 |
nk_input_key(ctx, NK_KEY_SHIFT, down); |
| 256 |
else if (sym == SDLK_DELETE) |
| 257 |
nk_input_key(ctx, NK_KEY_DEL, down); |
| 258 |
else if (sym == SDLK_RETURN) |
| 259 |
nk_input_key(ctx, NK_KEY_ENTER, down); |
| 260 |
else if (sym == SDLK_TAB) |
| 261 |
nk_input_key(ctx, NK_KEY_TAB, down); |
| 262 |
else if (sym == SDLK_BACKSPACE) |
| 263 |
nk_input_key(ctx, NK_KEY_BACKSPACE, down); |
| 264 |
else if (sym == SDLK_HOME) { |
| 265 |
nk_input_key(ctx, NK_KEY_TEXT_START, down); |
| 266 |
nk_input_key(ctx, NK_KEY_SCROLL_START, down); |
| 267 |
} else if (sym == SDLK_END) { |
| 268 |
nk_input_key(ctx, NK_KEY_TEXT_END, down); |
| 269 |
nk_input_key(ctx, NK_KEY_SCROLL_END, down); |
| 270 |
} else if (sym == SDLK_PAGEDOWN) { |
| 271 |
nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down); |
| 272 |
} else if (sym == SDLK_PAGEUP) { |
| 273 |
nk_input_key(ctx, NK_KEY_SCROLL_UP, down); |
| 274 |
} else if (sym == SDLK_z) |
| 275 |
nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && state[SDL_SCANCODE_LCTRL]); |
| 276 |
else if (sym == SDLK_r) |
| 277 |
nk_input_key(ctx, NK_KEY_TEXT_REDO, down && state[SDL_SCANCODE_LCTRL]); |
| 278 |
else if (sym == SDLK_c) |
| 279 |
nk_input_key(ctx, NK_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]); |
| 280 |
else if (sym == SDLK_v) |
| 281 |
nk_input_key(ctx, NK_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]); |
| 282 |
else if (sym == SDLK_x) |
| 283 |
nk_input_key(ctx, NK_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]); |
| 284 |
else if (sym == SDLK_b) |
| 285 |
nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && state[SDL_SCANCODE_LCTRL]); |
| 286 |
else if (sym == SDLK_e) |
| 287 |
nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && state[SDL_SCANCODE_LCTRL]); |
| 288 |
else if (sym == SDLK_UP) |
| 289 |
nk_input_key(ctx, NK_KEY_UP, down); |
| 290 |
else if (sym == SDLK_DOWN) |
| 291 |
nk_input_key(ctx, NK_KEY_DOWN, down); |
| 292 |
else if (sym == SDLK_LEFT) { |
| 293 |
if (state[SDL_SCANCODE_LCTRL]) |
| 294 |
nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down); |
| 295 |
else nk_input_key(ctx, NK_KEY_LEFT, down); |
| 296 |
} else if (sym == SDLK_RIGHT) { |
| 297 |
if (state[SDL_SCANCODE_LCTRL]) |
| 298 |
nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down); |
| 299 |
else nk_input_key(ctx, NK_KEY_RIGHT, down); |
| 300 |
} else return 0; |
| 301 |
return 1; |
| 302 |
} else if (evt->type == SDL_MOUSEBUTTONDOWN || evt->type == SDL_MOUSEBUTTONUP) { |
| 303 |
|
| 304 |
int down = evt->type == SDL_MOUSEBUTTONDOWN; |
| 305 |
const int x = evt->button.x, y = evt->button.y; |
| 306 |
if (evt->button.button == SDL_BUTTON_LEFT) { |
| 307 |
if (evt->button.clicks > 1) |
| 308 |
nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, down); |
| 309 |
nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down); |
| 310 |
} else if (evt->button.button == SDL_BUTTON_MIDDLE) |
| 311 |
nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down); |
| 312 |
else if (evt->button.button == SDL_BUTTON_RIGHT) |
| 313 |
nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down); |
| 314 |
return 1; |
| 315 |
} else if (evt->type == SDL_MOUSEMOTION) { |
| 316 |
|
| 317 |
if (windowId == evt->motion.windowID) { |
| 318 |
if (ctx->input.mouse.grabbed) { |
| 319 |
int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y; |
| 320 |
nk_input_motion(ctx, x + evt->motion.xrel, y + evt->motion.yrel); |
| 321 |
} else nk_input_motion(ctx, evt->motion.x, evt->motion.y); |
| 322 |
} |
| 323 |
return 1; |
| 324 |
} else if (evt->type == SDL_TEXTINPUT) { |
| 325 |
|
| 326 |
nk_glyph glyph; |
| 327 |
memcpy(glyph, evt->text.text, NK_UTF_SIZE); |
| 328 |
nk_input_glyph(ctx, glyph); |
| 329 |
return 1; |
| 330 |
} else if (evt->type == SDL_MOUSEWHEEL) { |
| 331 |
|
| 332 |
nk_input_scroll(ctx,nk_vec2((float)evt->wheel.x,(float)evt->wheel.y)); |
| 333 |
return 1; |
| 334 |
} |
| 335 |
return 0; |
| 336 |
} |
| 337 |
|
| 338 |
NK_API |
| 339 |
void nk_sdl_shutdown(void) |
| 340 |
{ |
| 341 |
struct nk_sdl_device *dev = &sdl.ogl; |
| 342 |
nk_font_atlas_clear(&sdl.atlas); |
| 343 |
nk_free(&sdl.ctx); |
| 344 |
glDeleteTextures(1, &dev->font_tex); |
| 345 |
nk_buffer_free(&dev->cmds); |
| 346 |
memset(&sdl, 0, sizeof(sdl)); |
| 347 |
} |
| 348 |
|
| 349 |
#endif |