file_browser.c 30 KB

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