file_browser.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. /* nuklear - v1.05 - public domain */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <stdarg.h>
  6. #include <string.h>
  7. #include <math.h>
  8. #include <assert.h>
  9. #include <time.h>
  10. #include <limits.h>
  11. #include <unistd.h>
  12. #include <dirent.h>
  13. #include <GL/glew.h>
  14. #include <GLFW/glfw3.h>
  15. #define NK_INCLUDE_FIXED_TYPES
  16. #define NK_INCLUDE_STANDARD_IO
  17. #define NK_INCLUDE_DEFAULT_ALLOCATOR
  18. #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  19. #define NK_INCLUDE_FONT_BAKING
  20. #define NK_INCLUDE_DEFAULT_FONT
  21. #define NK_IMPLEMENTATION
  22. #include "../nuklear.h"
  23. #define STB_IMAGE_IMPLEMENTATION
  24. #include "stb_image.h"
  25. /* macros */
  26. #define WINDOW_WIDTH 1200
  27. #define WINDOW_HEIGHT 800
  28. #define MAX_VERTEX_MEMORY 512 * 1024
  29. #define MAX_ELEMENT_MEMORY 128 * 1024
  30. #define UNUSED(a) (void)a
  31. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  32. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  33. #define LEN(a) (sizeof(a)/sizeof(a)[0])
  34. #ifdef __APPLE__
  35. #define NK_SHADER_VERSION "#version 150\n"
  36. #else
  37. #define NK_SHADER_VERSION "#version 300 es\n"
  38. #endif
  39. /* ===============================================================
  40. *
  41. * GUI
  42. *
  43. * ===============================================================*/
  44. struct icons {
  45. struct nk_image desktop;
  46. struct nk_image home;
  47. struct nk_image computer;
  48. struct nk_image directory;
  49. struct nk_image default_file;
  50. struct nk_image text_file;
  51. struct nk_image music_file;
  52. struct nk_image font_file;
  53. struct nk_image img_file;
  54. struct nk_image movie_file;
  55. };
  56. enum file_groups {
  57. FILE_GROUP_DEFAULT,
  58. FILE_GROUP_TEXT,
  59. FILE_GROUP_MUSIC,
  60. FILE_GROUP_FONT,
  61. FILE_GROUP_IMAGE,
  62. FILE_GROUP_MOVIE,
  63. FILE_GROUP_MAX
  64. };
  65. enum file_types {
  66. FILE_DEFAULT,
  67. FILE_TEXT,
  68. FILE_C_SOURCE,
  69. FILE_CPP_SOURCE,
  70. FILE_HEADER,
  71. FILE_CPP_HEADER,
  72. FILE_MP3,
  73. FILE_WAV,
  74. FILE_OGG,
  75. FILE_TTF,
  76. FILE_BMP,
  77. FILE_PNG,
  78. FILE_JPEG,
  79. FILE_PCX,
  80. FILE_TGA,
  81. FILE_GIF,
  82. FILE_MAX
  83. };
  84. struct file_group {
  85. enum file_groups group;
  86. const char *name;
  87. struct nk_image *icon;
  88. };
  89. struct file {
  90. enum file_types type;
  91. const char *suffix;
  92. enum file_groups group;
  93. };
  94. struct media {
  95. int font;
  96. int icon_sheet;
  97. struct icons icons;
  98. struct file_group group[FILE_GROUP_MAX];
  99. struct file files[FILE_MAX];
  100. };
  101. #define MAX_PATH_LEN 512
  102. struct file_browser {
  103. /* path */
  104. char file[MAX_PATH_LEN];
  105. char home[MAX_PATH_LEN];
  106. char desktop[MAX_PATH_LEN];
  107. char directory[MAX_PATH_LEN];
  108. /* directory content */
  109. char **files;
  110. char **directories;
  111. size_t file_count;
  112. size_t dir_count;
  113. struct media *media;
  114. };
  115. #ifdef __unix__
  116. #include <dirent.h>
  117. #include <unistd.h>
  118. #endif
  119. #ifndef _WIN32
  120. # include <pwd.h>
  121. #endif
  122. static void
  123. die(const char *fmt, ...)
  124. {
  125. va_list ap;
  126. va_start(ap, fmt);
  127. vfprintf(stderr, fmt, ap);
  128. va_end(ap);
  129. fputs("\n", stderr);
  130. exit(EXIT_FAILURE);
  131. }
  132. #if 0
  133. static char*
  134. file_load(const char* path, size_t* siz)
  135. {
  136. char *buf;
  137. FILE *fd = fopen(path, "rb");
  138. if (!fd) die("Failed to open file: %s\n", path);
  139. fseek(fd, 0, SEEK_END);
  140. *siz = (size_t)ftell(fd);
  141. fseek(fd, 0, SEEK_SET);
  142. buf = (char*)calloc(*siz, 1);
  143. fread(buf, *siz, 1, fd);
  144. fclose(fd);
  145. return buf;
  146. }
  147. #endif
  148. static char*
  149. str_duplicate(const char *src)
  150. {
  151. char *ret;
  152. size_t len = strlen(src);
  153. if (!len) return 0;
  154. ret = (char*)malloc(len+1);
  155. if (!ret) return 0;
  156. memcpy(ret, src, len);
  157. ret[len] = '\0';
  158. return ret;
  159. }
  160. static void
  161. dir_free_list(char **list, size_t size)
  162. {
  163. size_t i;
  164. for (i = 0; i < size; ++i)
  165. free(list[i]);
  166. free(list);
  167. }
  168. static char**
  169. dir_list(const char *dir, int return_subdirs, size_t *count)
  170. {
  171. size_t n = 0;
  172. char buffer[MAX_PATH_LEN];
  173. char **results = NULL;
  174. const DIR *none = NULL;
  175. size_t capacity = 32;
  176. size_t size;
  177. DIR *z;
  178. assert(dir);
  179. assert(count);
  180. strncpy(buffer, dir, MAX_PATH_LEN);
  181. buffer[MAX_PATH_LEN - 1] = 0;
  182. n = strlen(buffer);
  183. if (n > 0 && (buffer[n-1] != '/'))
  184. buffer[n++] = '/';
  185. size = 0;
  186. z = opendir(dir);
  187. if (z != none) {
  188. int nonempty = 1;
  189. struct dirent *data = readdir(z);
  190. nonempty = (data != NULL);
  191. if (!nonempty) return NULL;
  192. do {
  193. DIR *y;
  194. char *p;
  195. int is_subdir;
  196. if (data->d_name[0] == '.')
  197. continue;
  198. strncpy(buffer + n, data->d_name, MAX_PATH_LEN-n);
  199. y = opendir(buffer);
  200. is_subdir = (y != NULL);
  201. if (y != NULL) closedir(y);
  202. if ((return_subdirs && is_subdir) || (!is_subdir && !return_subdirs)){
  203. if (!size) {
  204. results = (char**)calloc(sizeof(char*), capacity);
  205. } else if (size >= capacity) {
  206. void *old = results;
  207. capacity = capacity * 2;
  208. results = (char**)realloc(results, capacity * sizeof(char*));
  209. assert(results);
  210. if (!results) free(old);
  211. }
  212. p = str_duplicate(data->d_name);
  213. results[size++] = p;
  214. }
  215. } while ((data = readdir(z)) != NULL);
  216. }
  217. if (z) closedir(z);
  218. *count = size;
  219. return results;
  220. }
  221. static struct file_group
  222. FILE_GROUP(enum file_groups group, const char *name, struct nk_image *icon)
  223. {
  224. struct file_group fg;
  225. fg.group = group;
  226. fg.name = name;
  227. fg.icon = icon;
  228. return fg;
  229. }
  230. static struct file
  231. FILE_DEF(enum file_types type, const char *suffix, enum file_groups group)
  232. {
  233. struct file fd;
  234. fd.type = type;
  235. fd.suffix = suffix;
  236. fd.group = group;
  237. return fd;
  238. }
  239. static struct nk_image*
  240. media_icon_for_file(struct media *media, const char *file)
  241. {
  242. int i = 0;
  243. const char *s = file;
  244. char suffix[4];
  245. int found = 0;
  246. memset(suffix, 0, sizeof(suffix));
  247. /* extract suffix .xxx from file */
  248. while (*s++ != '\0') {
  249. if (found && i < 3)
  250. suffix[i++] = *s;
  251. if (*s == '.') {
  252. if (found){
  253. found = 0;
  254. break;
  255. }
  256. found = 1;
  257. }
  258. }
  259. /* check for all file definition of all groups for fitting suffix*/
  260. for (i = 0; i < FILE_MAX && found; ++i) {
  261. struct file *d = &media->files[i];
  262. {
  263. const char *f = d->suffix;
  264. s = suffix;
  265. while (f && *f && *s && *s == *f) {
  266. s++; f++;
  267. }
  268. /* found correct file definition so */
  269. if (f && *s == '\0' && *f == '\0')
  270. return media->group[d->group].icon;
  271. }
  272. }
  273. return &media->icons.default_file;
  274. }
  275. static void
  276. media_init(struct media *media)
  277. {
  278. /* file groups */
  279. struct icons *icons = &media->icons;
  280. media->group[FILE_GROUP_DEFAULT] = FILE_GROUP(FILE_GROUP_DEFAULT,"default",&icons->default_file);
  281. media->group[FILE_GROUP_TEXT] = FILE_GROUP(FILE_GROUP_TEXT, "textual", &icons->text_file);
  282. media->group[FILE_GROUP_MUSIC] = FILE_GROUP(FILE_GROUP_MUSIC, "music", &icons->music_file);
  283. media->group[FILE_GROUP_FONT] = FILE_GROUP(FILE_GROUP_FONT, "font", &icons->font_file);
  284. media->group[FILE_GROUP_IMAGE] = FILE_GROUP(FILE_GROUP_IMAGE, "image", &icons->img_file);
  285. media->group[FILE_GROUP_MOVIE] = FILE_GROUP(FILE_GROUP_MOVIE, "movie", &icons->movie_file);
  286. /* files */
  287. media->files[FILE_DEFAULT] = FILE_DEF(FILE_DEFAULT, NULL, FILE_GROUP_DEFAULT);
  288. media->files[FILE_TEXT] = FILE_DEF(FILE_TEXT, "txt", FILE_GROUP_TEXT);
  289. media->files[FILE_C_SOURCE] = FILE_DEF(FILE_C_SOURCE, "c", FILE_GROUP_TEXT);
  290. media->files[FILE_CPP_SOURCE] = FILE_DEF(FILE_CPP_SOURCE, "cpp", FILE_GROUP_TEXT);
  291. media->files[FILE_HEADER] = FILE_DEF(FILE_HEADER, "h", FILE_GROUP_TEXT);
  292. media->files[FILE_CPP_HEADER] = FILE_DEF(FILE_HEADER, "hpp", FILE_GROUP_TEXT);
  293. media->files[FILE_MP3] = FILE_DEF(FILE_MP3, "mp3", FILE_GROUP_MUSIC);
  294. media->files[FILE_WAV] = FILE_DEF(FILE_WAV, "wav", FILE_GROUP_MUSIC);
  295. media->files[FILE_OGG] = FILE_DEF(FILE_OGG, "ogg", FILE_GROUP_MUSIC);
  296. media->files[FILE_TTF] = FILE_DEF(FILE_TTF, "ttf", FILE_GROUP_FONT);
  297. media->files[FILE_BMP] = FILE_DEF(FILE_BMP, "bmp", FILE_GROUP_IMAGE);
  298. media->files[FILE_PNG] = FILE_DEF(FILE_PNG, "png", FILE_GROUP_IMAGE);
  299. media->files[FILE_JPEG] = FILE_DEF(FILE_JPEG, "jpg", FILE_GROUP_IMAGE);
  300. media->files[FILE_PCX] = FILE_DEF(FILE_PCX, "pcx", FILE_GROUP_IMAGE);
  301. media->files[FILE_TGA] = FILE_DEF(FILE_TGA, "tga", FILE_GROUP_IMAGE);
  302. media->files[FILE_GIF] = FILE_DEF(FILE_GIF, "gif", FILE_GROUP_IMAGE);
  303. }
  304. static void
  305. file_browser_reload_directory_content(struct file_browser *browser, const char *path)
  306. {
  307. const size_t path_len = nk_strlen(path) + 1;
  308. NK_MEMCPY(browser->directory, path, MIN(path_len, MAX_PATH_LEN));
  309. browser->directory[MAX_PATH_LEN - 1] = 0;
  310. dir_free_list(browser->files, browser->file_count);
  311. dir_free_list(browser->directories, browser->dir_count);
  312. browser->files = dir_list(path, 0, &browser->file_count);
  313. browser->directories = dir_list(path, 1, &browser->dir_count);
  314. }
  315. static void
  316. file_browser_init(struct file_browser *browser, struct media *media)
  317. {
  318. memset(browser, 0, sizeof(*browser));
  319. browser->media = media;
  320. {
  321. /* load files and sub-directory list */
  322. const char *home = getenv("HOME");
  323. #ifdef _WIN32
  324. if (!home) home = getenv("USERPROFILE");
  325. #else
  326. if (!home) home = getpwuid(getuid())->pw_dir;
  327. {
  328. size_t l;
  329. strncpy(browser->home, home, MAX_PATH_LEN);
  330. browser->home[MAX_PATH_LEN - 1] = 0;
  331. l = strlen(browser->home);
  332. strcpy(browser->home + l, "/");
  333. strcpy(browser->directory, browser->home);
  334. }
  335. #endif
  336. {
  337. size_t l;
  338. strcpy(browser->desktop, browser->home);
  339. l = strlen(browser->desktop);
  340. strcpy(browser->desktop + l, "desktop/");
  341. }
  342. browser->files = dir_list(browser->directory, 0, &browser->file_count);
  343. browser->directories = dir_list(browser->directory, 1, &browser->dir_count);
  344. }
  345. }
  346. static void
  347. file_browser_free(struct file_browser *browser)
  348. {
  349. if (browser->files)
  350. dir_free_list(browser->files, browser->file_count);
  351. if (browser->directories)
  352. dir_free_list(browser->directories, browser->dir_count);
  353. browser->files = NULL;
  354. browser->directories = NULL;
  355. memset(browser, 0, sizeof(*browser));
  356. }
  357. static int
  358. file_browser_run(struct file_browser *browser, struct nk_context *ctx)
  359. {
  360. int ret = 0;
  361. struct media *media = browser->media;
  362. struct nk_rect total_space;
  363. if (nk_begin(ctx, "File Browser", nk_rect(50, 50, 800, 600),
  364. NK_WINDOW_BORDER|NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_MOVABLE))
  365. {
  366. static float ratio[] = {0.25f, NK_UNDEFINED};
  367. float spacing_x = ctx->style.window.spacing.x;
  368. /* output path directory selector in the menubar */
  369. ctx->style.window.spacing.x = 0;
  370. nk_menubar_begin(ctx);
  371. {
  372. char *d = browser->directory;
  373. char *begin = d + 1;
  374. nk_layout_row_dynamic(ctx, 25, 6);
  375. while (*d++) {
  376. if (*d == '/') {
  377. *d = '\0';
  378. if (nk_button_label(ctx, begin)) {
  379. *d++ = '/'; *d = '\0';
  380. file_browser_reload_directory_content(browser, browser->directory);
  381. break;
  382. }
  383. *d = '/';
  384. begin = d + 1;
  385. }
  386. }
  387. }
  388. nk_menubar_end(ctx);
  389. ctx->style.window.spacing.x = spacing_x;
  390. /* window layout */
  391. total_space = nk_window_get_content_region(ctx);
  392. nk_layout_row(ctx, NK_DYNAMIC, total_space.h, 2, ratio);
  393. nk_group_begin(ctx, "Special", NK_WINDOW_NO_SCROLLBAR);
  394. {
  395. struct nk_image home = media->icons.home;
  396. struct nk_image desktop = media->icons.desktop;
  397. struct nk_image computer = media->icons.computer;
  398. nk_layout_row_dynamic(ctx, 40, 1);
  399. if (nk_button_image_label(ctx, home, "home", NK_TEXT_CENTERED))
  400. file_browser_reload_directory_content(browser, browser->home);
  401. if (nk_button_image_label(ctx,desktop,"desktop",NK_TEXT_CENTERED))
  402. file_browser_reload_directory_content(browser, browser->desktop);
  403. if (nk_button_image_label(ctx,computer,"computer",NK_TEXT_CENTERED))
  404. file_browser_reload_directory_content(browser, "/");
  405. nk_group_end(ctx);
  406. }
  407. /* output directory content window */
  408. nk_group_begin(ctx, "Content", 0);
  409. {
  410. int index = -1;
  411. size_t i = 0, j = 0, k = 0;
  412. size_t rows = 0, cols = 0;
  413. size_t count = browser->dir_count + browser->file_count;
  414. cols = 4;
  415. rows = count / cols;
  416. for (i = 0; i <= rows; i += 1) {
  417. {size_t n = j + cols;
  418. nk_layout_row_dynamic(ctx, 135, (int)cols);
  419. for (; j < count && j < n; ++j) {
  420. /* draw one row of icons */
  421. if (j < browser->dir_count) {
  422. /* draw and execute directory buttons */
  423. if (nk_button_image(ctx,media->icons.directory))
  424. index = (int)j;
  425. } else {
  426. /* draw and execute files buttons */
  427. struct nk_image *icon;
  428. size_t fileIndex = ((size_t)j - browser->dir_count);
  429. icon = media_icon_for_file(media,browser->files[fileIndex]);
  430. if (nk_button_image(ctx, *icon)) {
  431. strncpy(browser->file, browser->directory, MAX_PATH_LEN);
  432. n = strlen(browser->file);
  433. strncpy(browser->file + n, browser->files[fileIndex], MAX_PATH_LEN - n);
  434. ret = 1;
  435. }
  436. }
  437. }}
  438. {size_t n = k + cols;
  439. nk_layout_row_dynamic(ctx, 20, (int)cols);
  440. for (; k < count && k < n; k++) {
  441. /* draw one row of labels */
  442. if (k < browser->dir_count) {
  443. nk_label(ctx, browser->directories[k], NK_TEXT_CENTERED);
  444. } else {
  445. size_t t = k-browser->dir_count;
  446. nk_label(ctx,browser->files[t],NK_TEXT_CENTERED);
  447. }
  448. }}
  449. }
  450. if (index != -1) {
  451. size_t n = strlen(browser->directory);
  452. strncpy(browser->directory + n, browser->directories[index], MAX_PATH_LEN - n);
  453. n = strlen(browser->directory);
  454. if (n < MAX_PATH_LEN - 1) {
  455. browser->directory[n] = '/';
  456. browser->directory[n+1] = '\0';
  457. }
  458. file_browser_reload_directory_content(browser, browser->directory);
  459. }
  460. nk_group_end(ctx);
  461. }
  462. }
  463. nk_end(ctx);
  464. return ret;
  465. }
  466. /* ===============================================================
  467. *
  468. * DEVICE
  469. *
  470. * ===============================================================*/
  471. struct nk_glfw_vertex {
  472. float position[2];
  473. float uv[2];
  474. nk_byte col[4];
  475. };
  476. struct device {
  477. struct nk_buffer cmds;
  478. struct nk_draw_null_texture tex_null;
  479. GLuint vbo, vao, ebo;
  480. GLuint prog;
  481. GLuint vert_shdr;
  482. GLuint frag_shdr;
  483. GLint attrib_pos;
  484. GLint attrib_uv;
  485. GLint attrib_col;
  486. GLint uniform_tex;
  487. GLint uniform_proj;
  488. GLuint font_tex;
  489. };
  490. static struct nk_image
  491. icon_load(const char *filename)
  492. {
  493. int x,y,n;
  494. GLuint tex;
  495. unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
  496. if (!data) die("[SDL]: failed to load image: %s", filename);
  497. glGenTextures(1, &tex);
  498. glBindTexture(GL_TEXTURE_2D, tex);
  499. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  500. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  501. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  502. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  503. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  504. glGenerateMipmap(GL_TEXTURE_2D);
  505. stbi_image_free(data);
  506. return nk_image_id((int)tex);
  507. }
  508. static void
  509. device_init(struct device *dev)
  510. {
  511. GLint status;
  512. static const GLchar *vertex_shader =
  513. NK_SHADER_VERSION
  514. "uniform mat4 ProjMtx;\n"
  515. "in vec2 Position;\n"
  516. "in vec2 TexCoord;\n"
  517. "in vec4 Color;\n"
  518. "out vec2 Frag_UV;\n"
  519. "out vec4 Frag_Color;\n"
  520. "void main() {\n"
  521. " Frag_UV = TexCoord;\n"
  522. " Frag_Color = Color;\n"
  523. " gl_Position = ProjMtx * vec4(Position.xy, 0, 1);\n"
  524. "}\n";
  525. static const GLchar *fragment_shader =
  526. NK_SHADER_VERSION
  527. "precision mediump float;\n"
  528. "uniform sampler2D Texture;\n"
  529. "in vec2 Frag_UV;\n"
  530. "in vec4 Frag_Color;\n"
  531. "out vec4 Out_Color;\n"
  532. "void main(){\n"
  533. " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
  534. "}\n";
  535. nk_buffer_init_default(&dev->cmds);
  536. dev->prog = glCreateProgram();
  537. dev->vert_shdr = glCreateShader(GL_VERTEX_SHADER);
  538. dev->frag_shdr = glCreateShader(GL_FRAGMENT_SHADER);
  539. glShaderSource(dev->vert_shdr, 1, &vertex_shader, 0);
  540. glShaderSource(dev->frag_shdr, 1, &fragment_shader, 0);
  541. glCompileShader(dev->vert_shdr);
  542. glCompileShader(dev->frag_shdr);
  543. glGetShaderiv(dev->vert_shdr, GL_COMPILE_STATUS, &status);
  544. assert(status == GL_TRUE);
  545. glGetShaderiv(dev->frag_shdr, GL_COMPILE_STATUS, &status);
  546. assert(status == GL_TRUE);
  547. glAttachShader(dev->prog, dev->vert_shdr);
  548. glAttachShader(dev->prog, dev->frag_shdr);
  549. glLinkProgram(dev->prog);
  550. glGetProgramiv(dev->prog, GL_LINK_STATUS, &status);
  551. assert(status == GL_TRUE);
  552. dev->uniform_tex = glGetUniformLocation(dev->prog, "Texture");
  553. dev->uniform_proj = glGetUniformLocation(dev->prog, "ProjMtx");
  554. dev->attrib_pos = glGetAttribLocation(dev->prog, "Position");
  555. dev->attrib_uv = glGetAttribLocation(dev->prog, "TexCoord");
  556. dev->attrib_col = glGetAttribLocation(dev->prog, "Color");
  557. {
  558. /* buffer setup */
  559. GLsizei vs = sizeof(struct nk_glfw_vertex);
  560. size_t vp = offsetof(struct nk_glfw_vertex, position);
  561. size_t vt = offsetof(struct nk_glfw_vertex, uv);
  562. size_t vc = offsetof(struct nk_glfw_vertex, col);
  563. glGenBuffers(1, &dev->vbo);
  564. glGenBuffers(1, &dev->ebo);
  565. glGenVertexArrays(1, &dev->vao);
  566. glBindVertexArray(dev->vao);
  567. glBindBuffer(GL_ARRAY_BUFFER, dev->vbo);
  568. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dev->ebo);
  569. glEnableVertexAttribArray((GLuint)dev->attrib_pos);
  570. glEnableVertexAttribArray((GLuint)dev->attrib_uv);
  571. glEnableVertexAttribArray((GLuint)dev->attrib_col);
  572. glVertexAttribPointer((GLuint)dev->attrib_pos, 2, GL_FLOAT, GL_FALSE, vs, (void*)vp);
  573. glVertexAttribPointer((GLuint)dev->attrib_uv, 2, GL_FLOAT, GL_FALSE, vs, (void*)vt);
  574. glVertexAttribPointer((GLuint)dev->attrib_col, 4, GL_UNSIGNED_BYTE, GL_TRUE, vs, (void*)vc);
  575. }
  576. glBindTexture(GL_TEXTURE_2D, 0);
  577. glBindBuffer(GL_ARRAY_BUFFER, 0);
  578. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  579. glBindVertexArray(0);
  580. }
  581. static void
  582. device_upload_atlas(struct device *dev, const void *image, int width, int height)
  583. {
  584. glGenTextures(1, &dev->font_tex);
  585. glBindTexture(GL_TEXTURE_2D, dev->font_tex);
  586. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  587. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  588. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0,
  589. GL_RGBA, GL_UNSIGNED_BYTE, image);
  590. }
  591. static void
  592. device_shutdown(struct device *dev)
  593. {
  594. glDetachShader(dev->prog, dev->vert_shdr);
  595. glDetachShader(dev->prog, dev->frag_shdr);
  596. glDeleteShader(dev->vert_shdr);
  597. glDeleteShader(dev->frag_shdr);
  598. glDeleteProgram(dev->prog);
  599. glDeleteTextures(1, &dev->font_tex);
  600. glDeleteBuffers(1, &dev->vbo);
  601. glDeleteBuffers(1, &dev->ebo);
  602. nk_buffer_free(&dev->cmds);
  603. }
  604. static void
  605. device_draw(struct device *dev, struct nk_context *ctx, int width, int height,
  606. struct nk_vec2 scale, enum nk_anti_aliasing AA)
  607. {
  608. GLfloat ortho[4][4] = {
  609. {2.0f, 0.0f, 0.0f, 0.0f},
  610. {0.0f,-2.0f, 0.0f, 0.0f},
  611. {0.0f, 0.0f,-1.0f, 0.0f},
  612. {-1.0f,1.0f, 0.0f, 1.0f},
  613. };
  614. ortho[0][0] /= (GLfloat)width;
  615. ortho[1][1] /= (GLfloat)height;
  616. /* setup global state */
  617. glEnable(GL_BLEND);
  618. glBlendEquation(GL_FUNC_ADD);
  619. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  620. glDisable(GL_CULL_FACE);
  621. glDisable(GL_DEPTH_TEST);
  622. glEnable(GL_SCISSOR_TEST);
  623. glActiveTexture(GL_TEXTURE0);
  624. /* setup program */
  625. glUseProgram(dev->prog);
  626. glUniform1i(dev->uniform_tex, 0);
  627. glUniformMatrix4fv(dev->uniform_proj, 1, GL_FALSE, &ortho[0][0]);
  628. {
  629. /* convert from command queue into draw list and draw to screen */
  630. const struct nk_draw_command *cmd;
  631. void *vertices, *elements;
  632. const nk_draw_index *offset = NULL;
  633. /* allocate vertex and element buffer */
  634. glBindVertexArray(dev->vao);
  635. glBindBuffer(GL_ARRAY_BUFFER, dev->vbo);
  636. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dev->ebo);
  637. glBufferData(GL_ARRAY_BUFFER, MAX_VERTEX_MEMORY, NULL, GL_STREAM_DRAW);
  638. glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_ELEMENT_MEMORY, NULL, GL_STREAM_DRAW);
  639. /* load draw vertices & elements directly into vertex + element buffer */
  640. vertices = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
  641. elements = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
  642. {
  643. /* fill convert configuration */
  644. struct nk_convert_config config;
  645. static const struct nk_draw_vertex_layout_element vertex_layout[] = {
  646. {NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_glfw_vertex, position)},
  647. {NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_glfw_vertex, uv)},
  648. {NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct nk_glfw_vertex, col)},
  649. {NK_VERTEX_LAYOUT_END}
  650. };
  651. NK_MEMSET(&config, 0, sizeof(config));
  652. config.vertex_layout = vertex_layout;
  653. config.vertex_size = sizeof(struct nk_glfw_vertex);
  654. config.vertex_alignment = NK_ALIGNOF(struct nk_glfw_vertex);
  655. config.tex_null = dev->tex_null;
  656. config.circle_segment_count = 22;
  657. config.curve_segment_count = 22;
  658. config.arc_segment_count = 22;
  659. config.global_alpha = 1.0f;
  660. config.shape_AA = AA;
  661. config.line_AA = AA;
  662. /* setup buffers to load vertices and elements */
  663. {struct nk_buffer vbuf, ebuf;
  664. nk_buffer_init_fixed(&vbuf, vertices, MAX_VERTEX_MEMORY);
  665. nk_buffer_init_fixed(&ebuf, elements, MAX_ELEMENT_MEMORY);
  666. nk_convert(ctx, &dev->cmds, &vbuf, &ebuf, &config);}
  667. }
  668. glUnmapBuffer(GL_ARRAY_BUFFER);
  669. glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
  670. /* iterate over and execute each draw command */
  671. nk_draw_foreach(cmd, ctx, &dev->cmds) {
  672. if (!cmd->elem_count) continue;
  673. glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
  674. glScissor(
  675. (GLint)(cmd->clip_rect.x * scale.x),
  676. (GLint)((height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h)) * scale.y),
  677. (GLint)(cmd->clip_rect.w * scale.x),
  678. (GLint)(cmd->clip_rect.h * scale.y));
  679. glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset);
  680. offset += cmd->elem_count;
  681. }
  682. nk_clear(ctx);
  683. nk_buffer_clear(&dev->cmds);
  684. }
  685. /* default OpenGL state */
  686. glUseProgram(0);
  687. glBindBuffer(GL_ARRAY_BUFFER, 0);
  688. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  689. glBindVertexArray(0);
  690. glDisable(GL_BLEND);
  691. glDisable(GL_SCISSOR_TEST);
  692. }
  693. /* glfw callbacks (I don't know if there is a easier way to access text and scroll )*/
  694. static void error_callback(int e, const char *d){printf("Error %d: %s\n", e, d);}
  695. static void text_input(GLFWwindow *win, unsigned int codepoint)
  696. {nk_input_unicode((struct nk_context*)glfwGetWindowUserPointer(win), codepoint);}
  697. static void scroll_input(GLFWwindow *win, double _, double yoff)
  698. {UNUSED(_);nk_input_scroll((struct nk_context*)glfwGetWindowUserPointer(win), nk_vec2(0, (float)yoff));}
  699. int main(int argc, char *argv[])
  700. {
  701. /* Platform */
  702. static GLFWwindow *win;
  703. int width = 0, height = 0;
  704. int display_width = 0, display_height = 0;
  705. /* GUI */
  706. struct device device;
  707. struct nk_context ctx;
  708. struct nk_font *font;
  709. struct nk_font_atlas atlas;
  710. struct file_browser browser;
  711. struct media media;
  712. /* GLFW */
  713. glfwSetErrorCallback(error_callback);
  714. if (!glfwInit()) {
  715. fprintf(stdout, "[GFLW] failed to init!\n");
  716. exit(1);
  717. }
  718. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  719. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  720. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  721. #ifdef __APPLE__
  722. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  723. #endif
  724. win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Demo", NULL, NULL);
  725. glfwMakeContextCurrent(win);
  726. glfwSetWindowUserPointer(win, &ctx);
  727. glfwSetCharCallback(win, text_input);
  728. glfwSetScrollCallback(win, scroll_input);
  729. /* OpenGL */
  730. glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
  731. glewExperimental = 1;
  732. if (glewInit() != GLEW_OK) {
  733. fprintf(stderr, "Failed to setup GLEW\n");
  734. exit(1);
  735. }
  736. {/* GUI */
  737. device_init(&device);
  738. {const void *image; int w, h;
  739. const char *font_path = (argc > 1) ? argv[1]: 0;
  740. nk_font_atlas_init_default(&atlas);
  741. nk_font_atlas_begin(&atlas);
  742. if (font_path) font = nk_font_atlas_add_from_file(&atlas, font_path, 13.0f, NULL);
  743. else font = nk_font_atlas_add_default(&atlas, 13.0f, NULL);
  744. image = nk_font_atlas_bake(&atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
  745. device_upload_atlas(&device, image, w, h);
  746. nk_font_atlas_end(&atlas, nk_handle_id((int)device.font_tex), &device.tex_null);}
  747. nk_init_default(&ctx, &font->handle);}
  748. /* icons */
  749. glEnable(GL_TEXTURE_2D);
  750. media.icons.home = icon_load("../icon/home.png");
  751. media.icons.directory = icon_load("../icon/directory.png");
  752. media.icons.computer = icon_load("../icon/computer.png");
  753. media.icons.desktop = icon_load("../icon/desktop.png");
  754. media.icons.default_file = icon_load("../icon/default.png");
  755. media.icons.text_file = icon_load("../icon/text.png");
  756. media.icons.music_file = icon_load("../icon/music.png");
  757. media.icons.font_file = icon_load("../icon/font.png");
  758. media.icons.img_file = icon_load("../icon/img.png");
  759. media.icons.movie_file = icon_load("../icon/movie.png");
  760. media_init(&media);
  761. file_browser_init(&browser, &media);
  762. while (!glfwWindowShouldClose(win))
  763. {
  764. /* High DPI displays */
  765. struct nk_vec2 scale;
  766. glfwGetWindowSize(win, &width, &height);
  767. glfwGetFramebufferSize(win, &display_width, &display_height);
  768. scale.x = (float)display_width/(float)width;
  769. scale.y = (float)display_height/(float)height;
  770. /* Input */
  771. {double x, y;
  772. nk_input_begin(&ctx);
  773. glfwPollEvents();
  774. nk_input_key(&ctx, NK_KEY_DEL, glfwGetKey(win, GLFW_KEY_DELETE) == GLFW_PRESS);
  775. nk_input_key(&ctx, NK_KEY_ENTER, glfwGetKey(win, GLFW_KEY_ENTER) == GLFW_PRESS);
  776. nk_input_key(&ctx, NK_KEY_TAB, glfwGetKey(win, GLFW_KEY_TAB) == GLFW_PRESS);
  777. nk_input_key(&ctx, NK_KEY_BACKSPACE, glfwGetKey(win, GLFW_KEY_BACKSPACE) == GLFW_PRESS);
  778. nk_input_key(&ctx, NK_KEY_LEFT, glfwGetKey(win, GLFW_KEY_LEFT) == GLFW_PRESS);
  779. nk_input_key(&ctx, NK_KEY_RIGHT, glfwGetKey(win, GLFW_KEY_RIGHT) == GLFW_PRESS);
  780. nk_input_key(&ctx, NK_KEY_UP, glfwGetKey(win, GLFW_KEY_UP) == GLFW_PRESS);
  781. nk_input_key(&ctx, NK_KEY_DOWN, glfwGetKey(win, GLFW_KEY_DOWN) == GLFW_PRESS);
  782. if (glfwGetKey(win, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS ||
  783. glfwGetKey(win, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS) {
  784. nk_input_key(&ctx, NK_KEY_COPY, glfwGetKey(win, GLFW_KEY_C) == GLFW_PRESS);
  785. nk_input_key(&ctx, NK_KEY_PASTE, glfwGetKey(win, GLFW_KEY_P) == GLFW_PRESS);
  786. nk_input_key(&ctx, NK_KEY_CUT, glfwGetKey(win, GLFW_KEY_X) == GLFW_PRESS);
  787. nk_input_key(&ctx, NK_KEY_CUT, glfwGetKey(win, GLFW_KEY_E) == GLFW_PRESS);
  788. nk_input_key(&ctx, NK_KEY_SHIFT, 1);
  789. } else {
  790. nk_input_key(&ctx, NK_KEY_COPY, 0);
  791. nk_input_key(&ctx, NK_KEY_PASTE, 0);
  792. nk_input_key(&ctx, NK_KEY_CUT, 0);
  793. nk_input_key(&ctx, NK_KEY_SHIFT, 0);
  794. }
  795. glfwGetCursorPos(win, &x, &y);
  796. nk_input_motion(&ctx, (int)x, (int)y);
  797. nk_input_button(&ctx, NK_BUTTON_LEFT, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS);
  798. nk_input_button(&ctx, NK_BUTTON_MIDDLE, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS);
  799. nk_input_button(&ctx, NK_BUTTON_RIGHT, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS);
  800. nk_input_end(&ctx);}
  801. /* GUI */
  802. file_browser_run(&browser, &ctx);
  803. /* Draw */
  804. glViewport(0, 0, display_width, display_height);
  805. glClear(GL_COLOR_BUFFER_BIT);
  806. glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
  807. device_draw(&device, &ctx, width, height, scale, NK_ANTI_ALIASING_ON);
  808. glfwSwapBuffers(win);
  809. }
  810. glDeleteTextures(1,(const GLuint*)&media.icons.home.handle.id);
  811. glDeleteTextures(1,(const GLuint*)&media.icons.directory.handle.id);
  812. glDeleteTextures(1,(const GLuint*)&media.icons.computer.handle.id);
  813. glDeleteTextures(1,(const GLuint*)&media.icons.desktop.handle.id);
  814. glDeleteTextures(1,(const GLuint*)&media.icons.default_file.handle.id);
  815. glDeleteTextures(1,(const GLuint*)&media.icons.text_file.handle.id);
  816. glDeleteTextures(1,(const GLuint*)&media.icons.music_file.handle.id);
  817. glDeleteTextures(1,(const GLuint*)&media.icons.font_file.handle.id);
  818. glDeleteTextures(1,(const GLuint*)&media.icons.img_file.handle.id);
  819. glDeleteTextures(1,(const GLuint*)&media.icons.movie_file.handle.id);
  820. file_browser_free(&browser);
  821. nk_font_atlas_clear(&atlas);
  822. nk_free(&ctx);
  823. device_shutdown(&device);
  824. glfwTerminate();
  825. return 0;
  826. }