file_browser.c 30 KB

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