main.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  1. /*================================================================
  2. * Copyright: 2020 John Jackson
  3. * serialization
  4. Custom Byte Buffer / Binary Serialization example
  5. Shown in video: https://www.youtube.com/watch?v=GXdT8twQxxI
  6. The purpose of this example is to demonstrate how to serialize arbitrary data to disk using a custom byte buffer implementation.
  7. A simple pixel art editor is thrown in to demonstrate some of these ideas in practice.
  8. Libraries used :
  9. {
  10. * Nuklear UI: https://github.com/Immediate-Mode-UI/Nuklear
  11. * TinyFileDialog: https://github.com/native-toolkit/tinyfiledialogs
  12. }
  13. Press `esc` to exit the application.
  14. =================================================================*/
  15. #include <gs.h>
  16. #include "font.h"
  17. #include "tinyfiledialogs.h"
  18. #include "font/font_data.c"
  19. #define NK_INCLUDE_FIXED_TYPES
  20. #define NK_INCLUDE_STANDARD_IO
  21. #define NK_INCLUDE_STANDARD_VARARGS
  22. #define NK_INCLUDE_DEFAULT_ALLOCATOR
  23. #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  24. #define NK_INCLUDE_FONT_BAKING
  25. #define NK_INCLUDE_DEFAULT_FONT
  26. #define NK_IMPLEMENTATION
  27. #define NK_GLFW_GL3_IMPLEMENTATION
  28. #define NK_KEYSTATE_BASED_INPUT
  29. #include "nuklear.h"
  30. #include "nuklear_glfw_gl3.h"
  31. #define MAX_VERTEX_BUFFER 512 * 1024
  32. #define MAX_ELEMENT_BUFFER 128 * 1024
  33. //================================================================
  34. void print_bin(uint8_t byte)
  35. {
  36. int i = CHAR_BIT; /* however many bits are in a byte on your platform */
  37. while(i--) {
  38. putchar('0' + ((byte >> i) & 1)); /* loop through and print the bits */
  39. }
  40. }
  41. // Forward Decls
  42. void pan_camera();
  43. /*==============
  44. // Byte Buffer
  45. ==============*/
  46. typedef struct byte_buffer_t
  47. {
  48. uint8_t* data;
  49. uint32_t position;
  50. uint32_t capacity;
  51. uint32_t size;
  52. } byte_buffer_t;
  53. #define byte_buffer_default_capacity 1024
  54. byte_buffer_t byte_buffer_new()
  55. {
  56. byte_buffer_t buffer = {0};
  57. buffer.data = malloc(byte_buffer_default_capacity);
  58. memset(buffer.data, 0, byte_buffer_default_capacity);
  59. buffer.capacity = byte_buffer_default_capacity;
  60. buffer.size = 0;
  61. buffer.position = 0;
  62. return buffer;
  63. }
  64. void byte_buffer_resize(byte_buffer_t* buffer, size_t sz);
  65. void __byte_buffer_write_impl(byte_buffer_t* buffer, void* data, size_t sz)
  66. {
  67. size_t total_write_size = buffer->position + sz;
  68. if (total_write_size >= buffer->capacity)
  69. {
  70. size_t capacity = buffer->capacity ? buffer->capacity * 2 : byte_buffer_default_capacity;
  71. while (capacity < total_write_size)
  72. {
  73. capacity *= 2;
  74. }
  75. byte_buffer_resize(buffer, capacity);
  76. }
  77. memcpy(buffer->data + buffer->position, data, sz);
  78. buffer->position += sz;
  79. buffer->size += sz;
  80. }
  81. // Generic write function
  82. #define byte_buffer_write(_buffer, T, _val)\
  83. do {\
  84. byte_buffer_t* _bb = (_buffer);\
  85. size_t _sz = sizeof(T);\
  86. T _v = _val;\
  87. __byte_buffer_write_impl(_bb, (void*)&(_v), _sz);\
  88. } while (0)
  89. // Generic read function
  90. #define byte_buffer_read(_buffer, T, _val_p)\
  91. do {\
  92. T* _v = (T*)(_val_p);\
  93. byte_buffer_t* _bb = (_buffer);\
  94. *(_v) = *(T*)(_bb->data + _bb->position);\
  95. _bb->position += sizeof(T);\
  96. } while (0)
  97. // Utiltiy functions
  98. void byte_buffer_clear(byte_buffer_t* buffer)
  99. {
  100. buffer->size = 0;
  101. buffer->position = 0;
  102. }
  103. void byte_buffer_free(byte_buffer_t* buffer)
  104. {
  105. byte_buffer_clear(buffer);
  106. free(buffer->data);
  107. buffer->data = NULL;
  108. buffer->capacity = 0;
  109. }
  110. void byte_buffer_resize(byte_buffer_t* buffer, size_t sz)
  111. {
  112. uint8_t* data = realloc(buffer->data, sz);
  113. if (data == NULL) {
  114. return;
  115. }
  116. buffer->data = data;
  117. buffer->capacity = sz;
  118. }
  119. void byte_buffer_seek_to_beg(byte_buffer_t* buffer)
  120. {
  121. buffer->position = 0;
  122. }
  123. void byte_buffer_seek_to_end(byte_buffer_t* buffer)
  124. {
  125. buffer->position = buffer->size;
  126. }
  127. // Writing
  128. gs_result
  129. byte_buffer_write_to_file(byte_buffer_t* buffer, const char* path)
  130. {
  131. gs_platform_i* platform = gs_engine_instance()->ctx.platform;
  132. gs_result res = platform->write_file_contents(path, "wb", buffer->data, sizeof(uint8_t), buffer->size);
  133. return res;
  134. }
  135. // Reading
  136. gs_result
  137. byte_buffer_read_from_file(byte_buffer_t* buffer, const char* path)
  138. {
  139. // Clear previous data if any
  140. if (buffer->data)
  141. {
  142. byte_buffer_free(buffer);
  143. }
  144. gs_platform_i* platform = gs_engine_instance()->ctx.platform;
  145. buffer->size = platform->file_size_in_bytes(path);
  146. buffer->data = platform->read_file_contents(path, "rb", NULL);
  147. if (!buffer->data)
  148. {
  149. gs_assert(false);
  150. return gs_result_failure;
  151. }
  152. buffer->position = 0;
  153. buffer->capacity = buffer->size;
  154. return gs_result_success;
  155. }
  156. //================================================================
  157. /*=========
  158. // NK - UI
  159. =========*/
  160. struct pixel_frame_t;
  161. gs_global struct nk_glfw g_nk_glfw = {0};
  162. gs_global struct nk_context* g_nk_ctx;
  163. gs_global struct nk_colorf g_nk_color;
  164. void nk_init_ui();
  165. void nk_do_ui(struct pixel_frame_t* pf);
  166. //================================================================
  167. /*=======================================
  168. // Texture Buffer For Simple Pixel Editor
  169. =======================================*/
  170. b32 color_equal(gs_color_t c0, gs_color_t c1)
  171. {
  172. return (c0.r == c1.r) &&
  173. (c0.g == c1.g) &&
  174. (c0.b == c1.b) &&
  175. (c0.a == c1.a);
  176. }
  177. #define default_pixel_frame_width 128
  178. #define default_pixel_frame_height 128
  179. #define ui_frame_width 128
  180. #define ui_frame_height 128
  181. const char* pixel_frame_v_src = "\n"
  182. "#version 330 core\n"
  183. "layout(location = 0) in vec2 a_pos;\n"
  184. "layout(location = 1) in vec2 a_uv;\n"
  185. "uniform mat4 u_proj;\n"
  186. "uniform mat4 u_view;\n"
  187. "out vec2 uv;\n"
  188. "void main()\n"
  189. "{\n"
  190. " gl_Position = u_proj * u_view * vec4(a_pos, 0.0, 1.0);\n"
  191. " uv = a_uv;\n"
  192. "}";
  193. const char* pixel_frame_f_src = "\n"
  194. "#version 330 core\n"
  195. "uniform sampler2D u_tex;"
  196. "in vec2 uv;\n"
  197. "out vec4 frag_color;\n"
  198. "void main()\n"
  199. "{\n"
  200. " frag_color = texture(u_tex, uv);\n"
  201. "}";
  202. /*
  203. These will be all the required op codes for undo/redo in our pixel editor
  204. We'll treat our undo/redo buffer as a simple VM
  205. */
  206. typedef enum pixel_frame_action_op_code_type
  207. {
  208. pixel_frame_action_op_write_pixels = 0x00,
  209. pixel_frame_action_op_clear_pixels = 0x01
  210. } pixel_frame_action_op_code_type;
  211. typedef struct pixel_frame_delta_color_t
  212. {
  213. u8 r, g, b, a;
  214. u8 sign_bits;
  215. } pixel_frame_delta_color_t;
  216. typedef struct pixel_frame_action_write_t
  217. {
  218. uint16_t idx;
  219. u8 r, g, b, a;
  220. u8 sign_bits;
  221. } pixel_frame_action_write_t;
  222. typedef struct pixel_frame_t
  223. {
  224. gs_color_t* pixel_data;
  225. gs_color_t* ui_data;
  226. uint32_t pixel_frame_width;
  227. uint32_t pixel_frame_height;
  228. gs_texture_t bg_texture;
  229. gs_texture_t texture;
  230. gs_texture_t ui_texture;
  231. gs_shader_t shader;
  232. gs_uniform_t u_tex;
  233. gs_uniform_t u_proj;
  234. gs_uniform_t u_view;
  235. gs_vertex_buffer_t vbo;
  236. gs_index_buffer_t ibo;
  237. uint32_t paint_radius;
  238. b32 mouse_down;
  239. byte_buffer_t undo_buffer;
  240. byte_buffer_t redo_buffer;
  241. uint32_t current_write_size;
  242. } pixel_frame_t;
  243. typedef void (*put_pixel_func)(void*, gs_color_t, int32_t, int32_t);
  244. // Bresenham Line -> from https://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm
  245. void drawLine(void* data, gs_color_t color, int32_t x0, int32_t x1, int32_t y0, int32_t y1, put_pixel_func func)
  246. {
  247. int32_t dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
  248. int32_t dy = abs(y1-y0), sy = y0<y1 ? 1 : -1;
  249. int32_t err = (dx>dy ? dx : -dy)/2, e2;
  250. for(;;)
  251. {
  252. func(data, color, x0, y0);
  253. if (x0==x1 && y0==y1) break;
  254. e2 = err;
  255. if (e2 >-dx) { err -= dy; x0 += sx; }
  256. if (e2 < dy) { err += dx; y0 += sy; }
  257. }
  258. }
  259. // Function to put pixels
  260. // at subsequence points
  261. void drawCircle(void* data, gs_color_t color, b32 filled, int32_t xc, int32_t yc, int32_t x, int32_t y, put_pixel_func func)
  262. {
  263. if (filled)
  264. {
  265. drawLine(data, color, xc - x, xc + x, yc + y, yc + y, func);
  266. drawLine(data, color, xc - x, xc + x, yc - y, yc - y, func);
  267. drawLine(data, color, xc - y, xc + y, yc + x, yc + x, func);
  268. drawLine(data, color, xc - y, xc + y, yc - x, yc - x, func);
  269. }
  270. else
  271. {
  272. func(data, color, xc+x, yc+y);
  273. func(data, color, xc-x, yc+y);
  274. func(data, color, xc+x, yc-y);
  275. func(data, color, xc-x, yc-y);
  276. func(data, color, xc+y, yc+x);
  277. func(data, color, xc-y, yc+x);
  278. func(data, color, xc+y, yc-x);
  279. func(data, color, xc-y, yc-x);
  280. }
  281. }
  282. // Function for circle-generation
  283. // using Bresenham's algorithm
  284. void circleBres(void* data, gs_color_t color, b32 filled, int32_t xc, int32_t yc, int32_t r, put_pixel_func func)
  285. {
  286. int x = 0, y = r;
  287. int d = 3 - 2 * r;
  288. drawCircle(data, color, filled, xc, yc, x, y, func);
  289. while (y >= x)
  290. {
  291. // For each pixel we will
  292. // draw all eight pixels
  293. x++;
  294. // Check for decision parameter
  295. // and correspondingly
  296. // update d, x, y
  297. if (d > 0)
  298. {
  299. y--;
  300. d = d + 4 * (x - y) + 10;
  301. }
  302. else
  303. d = d + 4 * x + 6;
  304. drawCircle(data, color, filled, xc, yc, x, y, func);
  305. }
  306. }
  307. gs_texture_parameter_desc pixel_frame_texture_parameter_desc()
  308. {
  309. gs_texture_parameter_desc desc = gs_texture_parameter_desc_default();
  310. desc.mag_filter = gs_nearest;
  311. desc.min_filter = gs_nearest;
  312. desc.generate_mips = false;
  313. desc.width = default_pixel_frame_width;
  314. desc.height = default_pixel_frame_height;
  315. desc.num_comps = 4;
  316. return desc;
  317. }
  318. pixel_frame_t pixel_frame_new()
  319. {
  320. gs_graphics_i* gfx = gs_engine_instance()->ctx.graphics;
  321. pixel_frame_t pf = {0};
  322. gs_texture_parameter_desc desc = pixel_frame_texture_parameter_desc();
  323. const gs_color_t checker_blue = (gs_color_t){ 50, 80, 180, 100 };
  324. const gs_color_t checker_white = (gs_color_t){ 255, 255, 255, 100 };
  325. const uint32_t bg_width = 5;
  326. const uint32_t bg_height = 5;
  327. gs_color_t* bg_data = gs_malloc(bg_width * bg_height * sizeof(gs_color_t));
  328. // Background image
  329. for (uint32_t h = 0; h < bg_width; ++h)
  330. {
  331. for (uint32_t w = 0; w < bg_height; ++w)
  332. {
  333. const uint32_t idx = h * bg_width + w;
  334. if (h % 2 == 0) {
  335. bg_data[ idx ] = w % 2 == 0 ? checker_white : checker_blue;
  336. }
  337. else {
  338. bg_data[ idx ] = w % 2 == 0 ? checker_blue : checker_white;
  339. }
  340. }
  341. }
  342. desc.width = bg_width;
  343. desc.height = bg_height;
  344. desc.data = bg_data;
  345. pf.bg_texture = gfx->construct_texture(desc);
  346. gs_free(bg_data);
  347. pf.pixel_frame_width = default_pixel_frame_width;
  348. pf.pixel_frame_height = default_pixel_frame_height;
  349. pf.pixel_data = gs_malloc(default_pixel_frame_width * default_pixel_frame_height * sizeof(gs_color_t));
  350. memset(pf.pixel_data, 0, default_pixel_frame_width * default_pixel_frame_height * sizeof(gs_color_t));
  351. desc = pixel_frame_texture_parameter_desc();
  352. desc.data = pf.pixel_data;
  353. pf.texture = gfx->construct_texture(desc);
  354. // UI data
  355. desc.width = ui_frame_width;
  356. desc.height = ui_frame_height;
  357. pf.ui_data = gs_malloc(desc.width * desc.height * sizeof(gs_color_t));
  358. desc.data = pf.ui_data;
  359. memset(pf.ui_data, 0, desc.width * desc.height * sizeof(gs_color_t));
  360. pf.ui_texture = gfx->construct_texture(desc);
  361. pf.paint_radius = 1;
  362. pf.shader = gfx->construct_shader(pixel_frame_v_src, pixel_frame_f_src);
  363. pf.u_tex = gfx->construct_uniform(pf.shader, "u_tex", gs_uniform_type_sampler2d);
  364. pf.u_proj = gfx->construct_uniform(pf.shader, "u_proj", gs_uniform_type_mat4);
  365. pf.u_view = gfx->construct_uniform(pf.shader, "u_view", gs_uniform_type_mat4);
  366. // Vertex data layout for our mesh
  367. gs_vertex_attribute_type layout[] = {
  368. gs_vertex_attribute_float2, // Position
  369. gs_vertex_attribute_float2 // UV
  370. };
  371. // Vertex data for editor screen quad
  372. f32 v_data[] =
  373. {
  374. // Positions UVs
  375. -1.0f, -1.0f, 0.0f, 0.0f, // Top Left
  376. 1.0f, -1.0f, 1.0f, 0.0f, // Top Right
  377. -1.0f, 1.0f, 0.0f, 1.0f, // Bottom Left
  378. 1.0f, 1.0f, 1.0f, 1.0f // Bottom Right
  379. };
  380. u32 i_data[] = {
  381. 0, 3, 2, // First Triangle
  382. 0, 1, 3 // Second Triangle
  383. };
  384. // Construct vertex and index buffers
  385. pf.vbo = gfx->construct_vertex_buffer(layout, sizeof(layout), v_data, sizeof(v_data));
  386. pf.ibo = gfx->construct_index_buffer(i_data, sizeof(i_data));
  387. pf.mouse_down = false;
  388. pf.undo_buffer = byte_buffer_new();
  389. pf.redo_buffer = byte_buffer_new();
  390. pf.current_write_size = 0;
  391. return pf;
  392. }
  393. b32 pixel_frame_action_is_recording(pixel_frame_t* pf)
  394. {
  395. return (pf->current_write_size != 0);
  396. }
  397. // Undo and Redo ops
  398. void pixel_frame_action_start_record(pixel_frame_t* pf, pixel_frame_action_op_code_type op)
  399. {
  400. // Cannot start another operation until the previous is done recording
  401. if (pixel_frame_action_is_recording(pf)) {
  402. return;
  403. }
  404. gs_println("Starting action...");
  405. // Write op code into buffer
  406. byte_buffer_write(&pf->undo_buffer, uint32_t, (uint32_t)op);
  407. pf->current_write_size += sizeof(pixel_frame_action_op_code_type);
  408. // Clear out redo buffer from previous actions
  409. byte_buffer_clear(&pf->redo_buffer);
  410. }
  411. void pixel_frame_action_end_record(pixel_frame_t* pf)
  412. {
  413. if (!pixel_frame_action_is_recording(pf)) {
  414. return;
  415. }
  416. if (pf->current_write_size > sizeof(pixel_frame_action_op_code_type)) {
  417. // Write current write size into buffer then reset write size
  418. byte_buffer_write(&pf->undo_buffer, uint32_t, pf->current_write_size);
  419. }
  420. else {
  421. pf->undo_buffer.position -= sizeof(pixel_frame_action_op_code_type);
  422. pf->undo_buffer.size -= sizeof(pixel_frame_action_op_code_type);
  423. }
  424. gs_println("Ending action: sz: %zu, undo_size: %zu, undo_cap: %zu, redo_cap: %zu", pf->current_write_size,
  425. pf->undo_buffer.size, pf->undo_buffer.capacity, pf->redo_buffer.capacity);
  426. pf->current_write_size = 0;
  427. }
  428. // Writing actions pushing ops into undo buffer
  429. void pixel_frame_write_action_op(pixel_frame_t* pf, pixel_frame_action_write_t data)
  430. {
  431. if (!pixel_frame_action_is_recording(pf)) {
  432. return;
  433. }
  434. // Write pixels...okie dokie, what does that data look like?
  435. // Do I need to keep track of previous pixel data? Not really, right? Because we know the starting state for all pixel data. It's memset to nothing.
  436. // Push data into byte buffer
  437. byte_buffer_write(&pf->undo_buffer, pixel_frame_action_write_t, data);
  438. pf->current_write_size += sizeof(data);
  439. }
  440. void pixel_frame_undo_action(pixel_frame_t* pf)
  441. {
  442. if (pixel_frame_action_is_recording(pf)) {
  443. return;
  444. }
  445. if (pf->undo_buffer.position == 0)
  446. {
  447. return;
  448. }
  449. // To undo, we'll transfer an action from the undo buffer into the redo buffer, assuming there is data to be read
  450. // Move position back to read size from buffer
  451. pf->undo_buffer.position -= sizeof(uint32_t);
  452. uint32_t sz;
  453. byte_buffer_read(&pf->undo_buffer, uint32_t, &sz);
  454. // Move back in buffer entire write size now, including size variable
  455. pf->undo_buffer.position -= (sz + sizeof(uint32_t));
  456. // Save current position. We'll use this reset ourselves after applying undo.
  457. uint32_t start_position = pf->undo_buffer.position;
  458. pixel_frame_action_op_code_type op;
  459. byte_buffer_read(&pf->undo_buffer, uint32_t, &op);
  460. switch (op)
  461. {
  462. case pixel_frame_action_op_write_pixels:
  463. {
  464. const uint32_t num_pixels = (sz - sizeof(uint32_t)) / sizeof(pixel_frame_action_write_t);
  465. // Write op into redo buffer
  466. byte_buffer_write(&pf->redo_buffer, uint32_t, pixel_frame_action_op_write_pixels);
  467. // Write data into redo buffer and reset pixels before action
  468. for (u32 i = 0; i < num_pixels; ++i)
  469. {
  470. pixel_frame_action_write_t wd;
  471. byte_buffer_read(&pf->undo_buffer, pixel_frame_action_write_t, &wd);
  472. // Compute new delta based on this change
  473. gs_color_t c = pf->pixel_data[ wd.idx ];
  474. u8 r = wd.r;
  475. u8 g = wd.g;
  476. u8 b = wd.b;
  477. u8 a = wd.a;
  478. gs_color_t p;
  479. p.r = (wd.sign_bits & 0x01) ? c.r + r : c.r - r;
  480. p.g = (wd.sign_bits & 0x02) ? c.g + g : c.g - g;
  481. p.b = (wd.sign_bits & 0x04) ? c.b + b : c.b - b;
  482. p.a = (wd.sign_bits & 0x08) ? c.a + a : c.a - a;
  483. pf->pixel_data[ wd.idx ] = p;
  484. // Calculate new delta for redo
  485. wd.sign_bits = 0x0;
  486. wd.sign_bits |= c.r > p.r ? 0x01 : 0x0;
  487. wd.sign_bits |= c.g > p.g ? 0x02 : 0x0;
  488. wd.sign_bits |= c.b > p.b ? 0x04 : 0x0;
  489. wd.sign_bits |= c.a > p.a ? 0x08 : 0x0;
  490. byte_buffer_write(&pf->redo_buffer, pixel_frame_action_write_t, wd);
  491. }
  492. // Write total size into redo buffer
  493. byte_buffer_write(&pf->redo_buffer, uint32_t, sz);
  494. // Set back to position before this action was recorded
  495. pf->undo_buffer.position = start_position;
  496. } break;
  497. default:
  498. {
  499. gs_assert(false);
  500. } break;
  501. }
  502. }
  503. typedef struct texture_t {
  504. uint32_t width;
  505. uint32_t height;
  506. uint32_t num_comps;
  507. void* pixels;
  508. } texture_t;
  509. void pixel_frame_redo_action(pixel_frame_t* pf)
  510. {
  511. if (pixel_frame_action_is_recording(pf)) {
  512. return;
  513. }
  514. if (pf->redo_buffer.position == 0)
  515. {
  516. return;
  517. }
  518. // Same as undo
  519. pf->redo_buffer.position -= sizeof(uint32_t);
  520. uint32_t sz;
  521. byte_buffer_read(&pf->redo_buffer, uint32_t, &sz);
  522. // Move back in buffer entire write size now, including size variable
  523. pf->redo_buffer.position -= (sz + sizeof(uint32_t));
  524. // Save current position. We'll use this reset ourselves after applying undo.
  525. uint32_t start_position = pf->redo_buffer.position;
  526. pixel_frame_action_op_code_type op;
  527. byte_buffer_read(&pf->redo_buffer, uint32_t, &op);
  528. switch (op)
  529. {
  530. case pixel_frame_action_op_write_pixels:
  531. {
  532. const uint32_t num_pixels = (sz - sizeof(uint32_t)) / sizeof(pixel_frame_action_write_t);
  533. // Write op into undo buffer
  534. byte_buffer_write(&pf->undo_buffer, uint32_t, pixel_frame_action_op_write_pixels);
  535. // Write data into redo buffer and reset pixels before action
  536. for (u32 i = 0; i < num_pixels; ++i)
  537. {
  538. pixel_frame_action_write_t wd;
  539. byte_buffer_read(&pf->redo_buffer, pixel_frame_action_write_t, &wd);
  540. // Compute new delta based on this change
  541. gs_color_t c = pf->pixel_data[ wd.idx ];
  542. u8 r = wd.r;
  543. u8 g = wd.g;
  544. u8 b = wd.b;
  545. u8 a = wd.a;
  546. gs_color_t p;
  547. p.r = (wd.sign_bits & 0x01) ? c.r + r : c.r - r;
  548. p.g = (wd.sign_bits & 0x02) ? c.g + g : c.g - g;
  549. p.b = (wd.sign_bits & 0x04) ? c.b + b : c.b - b;
  550. p.a = (wd.sign_bits & 0x08) ? c.a + a : c.a - a;
  551. pf->pixel_data[ wd.idx ] = p;
  552. // Calculate new delta for redo
  553. wd.sign_bits = 0x0;
  554. wd.sign_bits |= c.r > p.r ? 0x01 : 0x0;
  555. wd.sign_bits |= c.g > p.g ? 0x02 : 0x0;
  556. wd.sign_bits |= c.b > p.b ? 0x04 : 0x0;
  557. wd.sign_bits |= c.a > p.a ? 0x08 : 0x0;
  558. byte_buffer_write(&pf->undo_buffer, pixel_frame_action_write_t, wd);
  559. }
  560. // Write total size into redo buffer
  561. byte_buffer_write(&pf->undo_buffer, uint32_t, sz);
  562. // Set back to position before this action was recorded
  563. pf->redo_buffer.position = start_position;
  564. } break;
  565. default:
  566. {
  567. gs_assert(false);
  568. } break;
  569. }
  570. }
  571. gs_vec2 pixel_frame_calculate_mouse_position(pixel_frame_t* pf, gs_camera_t* camera)
  572. {
  573. gs_platform_i* platform = gs_engine_instance()->ctx.platform;
  574. gs_vec2 ws = platform->window_size(platform->main_window());
  575. gs_vec2 pmp = platform->mouse_position();
  576. gs_vec3 _tpmp = gs_camera_unproject(camera, (gs_vec3){pmp.x, pmp.y, 0.f}, ws.x, ws.y);
  577. gs_vec2 tpmp = (gs_vec2){_tpmp.x, _tpmp.y};
  578. tpmp.x = tpmp.x * 0.5f + 0.5f;
  579. tpmp.y = tpmp.y * 0.5f + 0.5f;
  580. tpmp.y = gs_map_range(1.f, 0.f, 0.f, 1.f, tpmp.y);
  581. // Need to place mouse into frame
  582. f32 x_scale = tpmp.x;
  583. f32 y_scale = tpmp.y;
  584. return (gs_vec2){ x_scale * (f32)pf->pixel_frame_width, y_scale * (f32)pf->pixel_frame_height };
  585. }
  586. int32_t pixel_frame_compute_idx_from_position(pixel_frame_t* pf, int32_t x, int32_t y)
  587. {
  588. if (x >= pf->pixel_frame_width || y >= pf->pixel_frame_height || x < 0 || y < 0 ) {
  589. return -1;
  590. }
  591. return (y * pf->pixel_frame_width + x);
  592. }
  593. void pixel_frame_draw_pixel(void* data, gs_color_t color, int32_t x, int32_t y)
  594. {
  595. pixel_frame_t* pf = (pixel_frame_t*)data;
  596. if (x < 0 || x >= pf->pixel_frame_width || y < 0 || y >= pf->pixel_frame_height)
  597. return;
  598. int32_t idx = pixel_frame_compute_idx_from_position(pf, x, y);
  599. if (idx != -1) {
  600. // Get converted color from color picker
  601. gs_color_t c = color;
  602. gs_color_t p = pf->pixel_data[ idx ];
  603. // Compute delta and signs
  604. uint8_t sign_bits = 0x0;
  605. uint8_t r = (uint8_t)abs((int32_t)c.r - (int32_t)p.r);
  606. uint8_t g = (uint8_t)abs((int32_t)c.g - (int32_t)p.g);
  607. uint8_t b = (uint8_t)abs((int32_t)c.b - (int32_t)p.b);
  608. uint8_t a = (uint8_t)abs((int32_t)c.a - (int32_t)p.a);
  609. sign_bits |= c.r < p.r ? 0x01 : 0x0;
  610. sign_bits |= c.g < p.g ? 0x02 : 0x0;
  611. sign_bits |= c.b < p.b ? 0x04 : 0x0;
  612. sign_bits |= c.a < p.a ? 0x08 : 0x0;
  613. pixel_frame_action_write_t wd =
  614. {
  615. (uint16_t)idx,
  616. r, g, b, a,
  617. sign_bits
  618. };
  619. // Only write if there's an actual change
  620. if (!color_equal(c, p)) {
  621. pixel_frame_write_action_op(pf, wd);
  622. pf->pixel_data[ idx ] = color;
  623. }
  624. }
  625. }
  626. void pixel_frame_draw_ui_pixel(void* data, gs_color_t color, int32_t x, int32_t y)
  627. {
  628. pixel_frame_t* pf = (pixel_frame_t*)data;
  629. if (x >= ui_frame_width || x < 0 || y >= ui_frame_height || y < 0)
  630. return;
  631. uint32_t idx = y * ui_frame_width + x;
  632. // Get converted color from color picker
  633. gs_color_t c = (gs_color_t){g_nk_color.r * 255, g_nk_color.g * 255, g_nk_color.b * 255, g_nk_color.a * 100};
  634. pf->ui_data[ idx ] = color;
  635. }
  636. void pixel_frame_clear_data(pixel_frame_t* pf)
  637. {
  638. if (!pixel_frame_action_is_recording(pf))
  639. {
  640. // Start record
  641. pixel_frame_action_start_record(pf, pixel_frame_action_op_write_pixels);
  642. const gs_color_t clear_color = (gs_color_t){ 0, 0, 0, 0 };
  643. // Clear all data
  644. for (uint32_t h = 0; h < pf->pixel_frame_height; ++h) {
  645. for (uint32_t w = 0; w < pf->pixel_frame_width; ++w) {
  646. pixel_frame_draw_pixel(pf, clear_color, w, h);
  647. }
  648. }
  649. // End record
  650. pixel_frame_action_end_record(pf);
  651. }
  652. // memset(pf->pixel_data, 0, pf->pixel_frame_width * pf->pixel_frame_height * sizeof(gs_color_t));
  653. }
  654. void pixel_frame_save_image_to_disk(pixel_frame_t* pf)
  655. {
  656. if (pixel_frame_action_is_recording(pf)) {
  657. return;
  658. }
  659. // We'll just write it to 'test.bin'
  660. byte_buffer_t buffer = byte_buffer_new();
  661. // Serialize all pixel frame data to disk
  662. for (uint32_t h = 0; h < pf->pixel_frame_height; ++h)
  663. {
  664. for (uint32_t w = 0; w < pf->pixel_frame_width; ++w)
  665. {
  666. uint32_t idx = h * pf->pixel_frame_width + w;
  667. byte_buffer_write(&buffer, uint8_t, pf->pixel_data[ idx ].r);
  668. byte_buffer_write(&buffer, uint8_t, pf->pixel_data[ idx ].g);
  669. byte_buffer_write(&buffer, uint8_t, pf->pixel_data[ idx ].b);
  670. byte_buffer_write(&buffer, uint8_t, pf->pixel_data[ idx ].a);
  671. }
  672. }
  673. const char* filter_patterns[] = {
  674. "*.pix"
  675. };
  676. const char* file_name = tinyfd_saveFileDialog(
  677. "Save Image",
  678. "./image.pix",
  679. sizeof(filter_patterns) / sizeof(char*),
  680. filter_patterns,
  681. NULL
  682. );
  683. // Write to file if valid
  684. if (file_name) {
  685. byte_buffer_write_to_file(&buffer, file_name);
  686. }
  687. byte_buffer_free(&buffer);
  688. }
  689. void pixel_frame_load_image_from_disk(pixel_frame_t* pf)
  690. {
  691. if (pixel_frame_action_is_recording(pf)) {
  692. return;
  693. }
  694. const char* filter_patterns[] = {
  695. "*.pix", "*.png"
  696. };
  697. const char* file_name = tinyfd_openFileDialog(
  698. "Open Image",
  699. "./",
  700. sizeof(filter_patterns) / sizeof(char*),
  701. filter_patterns,
  702. NULL,
  703. false
  704. );
  705. if (file_name == NULL) {
  706. return;
  707. }
  708. gs_platform_i* platform = gs_engine_instance()->ctx.platform;
  709. if (!platform->file_exists(file_name)) {
  710. return;
  711. }
  712. char file_ext[256];
  713. gs_util_get_file_extension(file_ext, sizeof(file_ext), file_name);
  714. if (gs_string_compare_equal(file_ext, "pix"))
  715. {
  716. byte_buffer_t buffer = byte_buffer_new();
  717. byte_buffer_read_from_file(&buffer, file_name);
  718. // Serialize all pixel frame data to disk
  719. for (uint32_t h = 0; h < pf->pixel_frame_height; ++h)
  720. {
  721. for (uint32_t w = 0; w < pf->pixel_frame_width; ++w)
  722. {
  723. uint32_t idx = h * pf->pixel_frame_width + w;
  724. byte_buffer_read(&buffer, uint8_t, &pf->pixel_data[ idx ].r);
  725. byte_buffer_read(&buffer, uint8_t, &pf->pixel_data[ idx ].g);
  726. byte_buffer_read(&buffer, uint8_t, &pf->pixel_data[ idx ].b);
  727. byte_buffer_read(&buffer, uint8_t, &pf->pixel_data[ idx ].a);
  728. }
  729. }
  730. // Clear out undo/redo buffers
  731. byte_buffer_clear(&pf->undo_buffer);
  732. byte_buffer_clear(&pf->redo_buffer);
  733. byte_buffer_free(&buffer);
  734. }
  735. else if (gs_string_compare_equal(file_ext, "png"))
  736. {
  737. // Need to load in .png and then change size of texture...
  738. }
  739. }
  740. b32 pixel_frame_hovering_ui_window()
  741. {
  742. gs_platform_i* platform = gs_engine_instance()->ctx.platform;
  743. gs_vec2 mp = platform->mouse_position();
  744. gs_vec2 ws = platform->window_size(platform->main_window());
  745. uint32_t sw = 250.f;
  746. return mp.x < sw;
  747. }
  748. // Drag/drop texture into frame?
  749. // Save/Load images
  750. // Change colors (might just have a set number of colors - not sure how I'd represent 32 bit colors without some
  751. // complicated gui mechanism)
  752. // Radius for painting
  753. void pixel_frame_update(pixel_frame_t* pf, gs_camera_t* camera)
  754. {
  755. gs_platform_i* platform = gs_engine_instance()->ctx.platform;
  756. b32 need_update = false;
  757. static gs_vec2 original_mouse_position;
  758. b32 alt_down = platform->key_down(gs_keycode_lalt);
  759. b32 space_down = platform->key_down(gs_keycode_space);
  760. gs_vec2 mp = pixel_frame_calculate_mouse_position(pf, camera);
  761. const uint32_t mp_x = (uint32_t)mp.x;
  762. const uint32_t mp_y = pf->pixel_frame_height - (uint32_t)mp.y - 1;
  763. const int32_t idx = pixel_frame_compute_idx_from_position(pf, mp_x, mp_y);
  764. // Painting operation
  765. if (platform->mouse_down(gs_mouse_lbutton) && !pixel_frame_hovering_ui_window())
  766. {
  767. if (!alt_down && !space_down)
  768. {
  769. if (!pf->mouse_down) {
  770. pf->mouse_down = true;
  771. // Start recording
  772. pixel_frame_action_start_record(pf, pixel_frame_action_op_write_pixels);
  773. }
  774. if (idx != -1) {
  775. // Get converted color from color picker
  776. gs_color_t col = (gs_color_t){g_nk_color.r * 255, g_nk_color.g * 255, g_nk_color.b * 255, g_nk_color.a * 255};
  777. if (pf->paint_radius > 1) {
  778. circleBres(pf, col, true, mp.x, pf->pixel_frame_height - mp.y, pf->paint_radius, &pixel_frame_draw_pixel);
  779. } else {
  780. pixel_frame_draw_pixel(pf, col, mp_x, mp_y);
  781. }
  782. }
  783. }
  784. // Color picking
  785. else if (alt_down)
  786. {
  787. if (idx != -1) {
  788. gs_color_t c = pf->pixel_data[ idx ];
  789. g_nk_color.r = (f32)c.r / 255.f;
  790. g_nk_color.g = (f32)c.g / 255.f;
  791. g_nk_color.b = (f32)c.b / 255.f;
  792. g_nk_color.a = (f32)c.a / 255.f;
  793. }
  794. }
  795. // Camera pan
  796. else if (space_down)
  797. {
  798. pan_camera();
  799. }
  800. }
  801. // Need to record actions here
  802. if (platform->mouse_released(gs_mouse_lbutton))
  803. {
  804. // Start recording
  805. pixel_frame_action_end_record(pf);
  806. pf->mouse_down = false;
  807. }
  808. // Paint radius
  809. if (platform->key_pressed(gs_keycode_lbracket))
  810. {
  811. pf->paint_radius = gs_max((int32_t)1, (int32_t)pf->paint_radius - 1);
  812. }
  813. if (platform->key_pressed(gs_keycode_rbracket))
  814. {
  815. pf->paint_radius = gs_min(20, pf->paint_radius + 1);
  816. }
  817. if (platform->key_down(gs_keycode_lctrl) && platform->key_pressed(gs_keycode_z))
  818. {
  819. pixel_frame_undo_action(pf);
  820. }
  821. if (platform->key_down(gs_keycode_lctrl) && platform->key_pressed(gs_keycode_y))
  822. {
  823. pixel_frame_redo_action(pf);
  824. }
  825. if (platform->key_down(gs_keycode_lctrl) && platform->key_pressed(gs_keycode_s))
  826. {
  827. pixel_frame_save_image_to_disk(pf);
  828. }
  829. if (platform->key_down(gs_keycode_lctrl) && platform->key_pressed(gs_keycode_o))
  830. {
  831. pixel_frame_load_image_from_disk(pf);
  832. }
  833. if (platform->key_pressed(gs_keycode_c))
  834. {
  835. pixel_frame_clear_data(pf);
  836. }
  837. // Update GPU texture data
  838. gs_graphics_i* gfx = gs_engine_instance()->ctx.graphics;
  839. gs_texture_parameter_desc desc = pixel_frame_texture_parameter_desc();
  840. desc.width = pf->pixel_frame_width;
  841. desc.height = pf->pixel_frame_height;
  842. desc.data = pf->pixel_data;
  843. gfx->update_texture_data(&pf->texture, desc);
  844. // Draw ui into buffer
  845. memset(pf->ui_data, 0, ui_frame_width * ui_frame_height * sizeof(gs_color_t));
  846. const gs_color_t col = (gs_color_t){ (u8)g_nk_color.r * 255, (u8)g_nk_color.g * 255, (u8)g_nk_color.b * 255, 50 };
  847. gs_vec2 ws = platform->window_size(platform->main_window());
  848. gs_vec2 m_pos = platform->mouse_position();
  849. f32 scl_x = m_pos.x / ws.x;
  850. f32 scl_y = m_pos.y / ws.y;
  851. if (pf->paint_radius > 0)
  852. {
  853. circleBres(pf, col, false, mp.x, ui_frame_height - mp.y, pf->paint_radius, &pixel_frame_draw_ui_pixel);
  854. }
  855. desc = pixel_frame_texture_parameter_desc();
  856. desc.width = ui_frame_width;
  857. desc.height = ui_frame_height;
  858. desc.data = pf->ui_data;
  859. gfx->update_texture_data(&pf->ui_texture, desc);
  860. }
  861. void pixel_frame_render(pixel_frame_t* pf, gs_command_buffer_t* cb, gs_camera_t* camera)
  862. {
  863. gs_graphics_i* gfx = gs_engine_instance()->ctx.graphics;
  864. gs_platform_i* platform = gs_engine_instance()->ctx.platform;
  865. gs_vec2 ws = platform->window_size(platform->main_window());
  866. gs_mat4 proj_mtx = gs_camera_get_projection(camera, ws.x, ws.y);
  867. gs_mat4 view_mtx = gs_camera_get_view(camera);
  868. f32 clear_color[4] = { 0.5f, 0.5f, 0.5f, 1.f };
  869. gfx->set_view_clear(cb, clear_color);
  870. // Bind shader
  871. gfx->bind_shader(cb, pf->shader);
  872. gfx->bind_uniform(cb, pf->u_proj, &proj_mtx);
  873. gfx->bind_uniform(cb, pf->u_view, &view_mtx);
  874. // Render background texture
  875. gfx->bind_texture(cb, pf->u_tex, pf->bg_texture, 0);
  876. gfx->bind_vertex_buffer(cb, pf->vbo);
  877. gfx->bind_index_buffer(cb, pf->ibo);
  878. gfx->draw_indexed(cb, 6, 0);
  879. // Render paint texture
  880. gfx->bind_texture(cb, pf->u_tex, pf->texture, 0);
  881. gfx->bind_vertex_buffer(cb, pf->vbo);
  882. gfx->bind_index_buffer(cb, pf->ibo);
  883. gfx->draw_indexed(cb, 6, 0);
  884. // Render ui texture
  885. gfx->bind_texture(cb, pf->u_tex, pf->ui_texture, 0);
  886. gfx->bind_vertex_buffer(cb, pf->vbo);
  887. gfx->bind_index_buffer(cb, pf->ibo);
  888. gfx->draw_indexed(cb, 6, 0);
  889. }
  890. //================================================================
  891. // Globals
  892. gs_global gs_command_buffer_t g_cb = {0};
  893. gs_global pixel_frame_t g_pixel_frame = {0};
  894. gs_global gs_camera_t g_camera = {0};
  895. // Forward Decls.
  896. gs_result app_init(); // Use to init your application
  897. gs_result app_update(); // Use to update your application
  898. gs_result app_shutdown(); // Use to shutdown your appliaction
  899. typedef struct test_t
  900. {
  901. u32 unsigned_val;
  902. s32 signed_val;
  903. f32 float_val;
  904. f64 double_val;
  905. } test_t;
  906. void print_test(test_t* t)
  907. {
  908. gs_println("test: { %zu, %d, %.2f, %.5f }",
  909. t->unsigned_val, t->signed_val, t->float_val, t->double_val);
  910. }
  911. int main(int argc, char** argv)
  912. {
  913. gs_application_desc_t app = {0};
  914. app.window_title = "Serialization Example";
  915. app.window_width = 1200;
  916. app.window_height = 600;
  917. app.init = &app_init;
  918. app.update = &app_update;
  919. app.shutdown = &app_shutdown;
  920. // Construct internal instance of our engine
  921. gs_engine_t* engine = gs_engine_construct(app);
  922. // Run the internal engine loop until completion
  923. gs_result res = engine->run();
  924. // Check result of engine after exiting loop
  925. if (res != gs_result_success)
  926. {
  927. gs_println("Error: Engine did not successfully finish running.");
  928. return -1;
  929. }
  930. gs_println("Gunslinger exited successfully.");
  931. return 0;
  932. }
  933. void byte_buffer_test()
  934. {
  935. test_t t0 = {0};
  936. t0.unsigned_val = 5;
  937. t0.signed_val = -20;
  938. t0.float_val = -3.145f;
  939. t0.double_val = 2.37854;
  940. byte_buffer_t buffer = byte_buffer_new();
  941. // Serialize our test data structure into our buffer
  942. byte_buffer_write(&buffer, uint32_t, t0.unsigned_val);
  943. byte_buffer_write(&buffer, int32_t, t0.signed_val);
  944. byte_buffer_write(&buffer, float, t0.float_val);
  945. byte_buffer_write(&buffer, double, t0.double_val);
  946. // Seek back to beginning
  947. byte_buffer_seek_to_beg(&buffer);
  948. // Read values into our new test data
  949. test_t t1 = {0};
  950. byte_buffer_read(&buffer, uint32_t, &t1.unsigned_val);
  951. byte_buffer_read(&buffer, int32_t, &t1.signed_val);
  952. byte_buffer_read(&buffer, float, &t1.float_val);
  953. byte_buffer_read(&buffer, double, &t1.double_val);
  954. print_test(&t1);
  955. // Write to file
  956. byte_buffer_write_to_file(&buffer, "./test.bin");
  957. // Read back from file
  958. byte_buffer_read_from_file(&buffer, "./test.bin");
  959. // Read back into new object
  960. test_t t2 = {0};
  961. // Read back entire object from memory
  962. byte_buffer_read(&buffer, uint32_t, &t2.unsigned_val);
  963. byte_buffer_read(&buffer, int32_t, &t2.signed_val);
  964. byte_buffer_read(&buffer, float, &t2.float_val);
  965. byte_buffer_read(&buffer, double, &t2.double_val);
  966. print_test(&t2);
  967. // Clean up byte buffer memory
  968. byte_buffer_free(&buffer);
  969. }
  970. // Here, we'll initialize all of our application data, which in this case is our graphics resources
  971. gs_result app_init()
  972. {
  973. // Cache instance of graphics/platform apis from engine
  974. gs_graphics_i* gfx = gs_engine_instance()->ctx.graphics;
  975. gs_platform_i* platform = gs_engine_instance()->ctx.platform;
  976. byte_buffer_test();
  977. g_pixel_frame = pixel_frame_new();
  978. // This is a descriptor for our texture. It includes various metrics, such as the width, height, texture format,
  979. // and holds the actual uncompressed texture data for the texture. After using it for loading a raw texture
  980. // from file, it's the responsibility of the user to free the data.
  981. gs_texture_parameter_desc desc = gs_texture_parameter_desc_default();
  982. // Construct command buffer (the command buffer is used to allow for immediate drawing at any point in our program)
  983. g_cb = gs_command_buffer_new();
  984. // Construct camera parameters
  985. g_camera.transform = gs_vqs_default();
  986. g_camera.transform.position = (gs_vec3){-1.f, 0.f, -1.f};
  987. g_camera.fov = 60.f;
  988. g_camera.near_plane = 0.1f;
  989. g_camera.far_plane = 1000.f;
  990. g_camera.ortho_scale = 2.f;
  991. g_camera.proj_type = gs_projection_type_orthographic;
  992. nk_init_ui();
  993. return gs_result_success;
  994. }
  995. gs_result app_update()
  996. {
  997. // Grab global instance of engine
  998. gs_engine_t* engine = gs_engine_instance();
  999. // Platform api
  1000. gs_platform_i* platform = engine->ctx.platform;
  1001. // Main window size
  1002. gs_vec2 ws = platform->window_size(platform->main_window());
  1003. gs_vec2 fbs = platform->frame_buffer_size(platform->main_window());
  1004. // If we press the escape key, exit the application
  1005. if (platform->key_pressed(gs_keycode_esc))
  1006. {
  1007. return gs_result_success;
  1008. }
  1009. const f32 dt = platform->time.delta;
  1010. const f32 t = platform->elapsed_time();
  1011. /*=================
  1012. // Camera controls
  1013. ==================*/
  1014. if (platform->key_down(gs_keycode_q)) {
  1015. g_camera.ortho_scale += 0.1f;
  1016. }
  1017. if (platform->key_down(gs_keycode_e)) {
  1018. g_camera.ortho_scale -= 0.1f;
  1019. }
  1020. if (platform->key_down(gs_keycode_a)) {
  1021. g_camera.transform.position.x -= 0.1f;
  1022. }
  1023. if (platform->key_down(gs_keycode_d)) {
  1024. g_camera.transform.position.x += 0.1f;
  1025. }
  1026. if (platform->key_down(gs_keycode_w)) {
  1027. g_camera.transform.position.y += 0.1f;
  1028. }
  1029. if (platform->key_down(gs_keycode_s)) {
  1030. g_camera.transform.position.y -= 0.1f;
  1031. }
  1032. if (platform->mouse_down(gs_mouse_mbutton))
  1033. {
  1034. // Need to transform the camera's position by the transformation of the mouse in world space only...I think
  1035. gs_vec2 md = platform->mouse_delta();
  1036. gs_vec2 mc = platform->mouse_position();
  1037. gs_vec3 wc = (gs_vec3){mc.x, mc.y, 0.f};
  1038. gs_vec3 pmp = gs_camera_unproject(&g_camera, wc, ws.x, ws.y);
  1039. gs_vec3 pmp_d = gs_camera_unproject(&g_camera, (gs_vec3){mc.x + md.x, mc.y + md.y, 0.f}, ws.x, ws.y);
  1040. gs_vec3 delta = gs_vec3_sub(pmp, pmp_d);
  1041. g_camera.transform.position.x += delta.x;
  1042. g_camera.transform.position.y += delta.y;
  1043. }
  1044. f32 mwx, mwy;
  1045. platform->mouse_wheel(&mwx, &mwy);
  1046. g_camera.ortho_scale = gs_clamp(g_camera.ortho_scale - mwy * 0.2f, 0.2f, 10.f);
  1047. /*===============
  1048. // Update scene
  1049. ================*/
  1050. pixel_frame_update(&g_pixel_frame, &g_camera);
  1051. /*===============
  1052. // Render scene
  1053. ================*/
  1054. // Graphics api instance
  1055. gs_graphics_i* gfx = engine->ctx.graphics;
  1056. gs_command_buffer_t* cb = &g_cb;
  1057. // Set clear color and clear screen
  1058. f32 clear_color[4] = { 0.1f, 0.1f, 0.1f, 1.f };
  1059. gfx->set_view_clear(cb, clear_color);
  1060. gfx->set_viewport(cb, 0.f, 0.f, fbs.x, fbs.y);
  1061. gfx->set_depth_enabled(cb, false);
  1062. gfx->set_blend_mode(cb, gs_blend_mode_src_alpha, gs_blend_mode_one_minus_src_alpha);
  1063. pixel_frame_render(&g_pixel_frame, cb, &g_camera);
  1064. // Submit command buffer for rendering
  1065. gfx->submit_command_buffer(cb);
  1066. // Handle ui
  1067. nk_do_ui(&g_pixel_frame);
  1068. return gs_result_in_progress;
  1069. }
  1070. gs_result app_shutdown()
  1071. {
  1072. gs_println("Goodbye, Gunslinger.");
  1073. return gs_result_success;
  1074. }
  1075. void pan_camera()
  1076. {
  1077. gs_platform_i* platform = gs_engine_instance()->ctx.platform;
  1078. gs_vec2 ws = platform->window_size(platform->main_window());
  1079. gs_vec2 md = platform->mouse_delta();
  1080. gs_vec2 mc = platform->mouse_position();
  1081. gs_vec3 wc = (gs_vec3){mc.x, mc.y, 0.f};
  1082. gs_vec3 pmp = gs_camera_unproject(&g_camera, wc, ws.x, ws.y);
  1083. gs_vec3 pmp_d = gs_camera_unproject(&g_camera, (gs_vec3){mc.x + md.x, mc.y + md.y, 0.f}, ws.x, ws.y);
  1084. gs_vec3 delta = gs_vec3_sub(pmp, pmp_d);
  1085. g_camera.transform.position.x += delta.x;
  1086. g_camera.transform.position.y += delta.y;
  1087. }
  1088. void nk_init_ui()
  1089. {
  1090. g_nk_color = (struct nk_colorf){0.f, 0.f, 0.f, 1.f};
  1091. gs_platform_i* platform = gs_engine_instance()->ctx.platform;
  1092. GLFWwindow* win = platform->raw_window_handle(platform->main_window());
  1093. g_nk_ctx = nk_glfw3_init(&g_nk_glfw, win, 0);
  1094. struct nk_font_atlas* atlas;
  1095. nk_glfw3_font_stash_begin(&g_nk_glfw, &atlas);
  1096. nk_glfw3_font_stash_end(&g_nk_glfw);
  1097. }
  1098. void nk_do_ui(struct pixel_frame_t* pf)
  1099. {
  1100. gs_platform_i* platform = gs_engine_instance()->ctx.platform;
  1101. gs_vec2 ws = platform->window_size(platform->main_window());
  1102. // Do the ui stuff
  1103. nk_glfw3_new_frame(&g_nk_glfw);
  1104. /* GUI */
  1105. if (nk_begin(g_nk_ctx, "Demo", nk_rect(0, 0, 250, ws.y), NK_WINDOW_BORDER))
  1106. {
  1107. nk_layout_row_dynamic(g_nk_ctx, 20, 1);
  1108. nk_label(g_nk_ctx, "Color", NK_TEXT_LEFT);
  1109. {
  1110. nk_layout_row_dynamic(g_nk_ctx, 120, 1);
  1111. g_nk_color = nk_color_picker(g_nk_ctx, g_nk_color, NK_RGBA);
  1112. nk_layout_row_dynamic(g_nk_ctx, 25, 1);
  1113. g_nk_color.r = nk_propertyf(g_nk_ctx, "#R:", 0, g_nk_color.r, 1.0f, 0.01f,0.005f);
  1114. g_nk_color.g = nk_propertyf(g_nk_ctx, "#G:", 0, g_nk_color.g, 1.0f, 0.01f,0.005f);
  1115. g_nk_color.b = nk_propertyf(g_nk_ctx, "#B:", 0, g_nk_color.b, 1.0f, 0.01f,0.005f);
  1116. g_nk_color.a = nk_propertyf(g_nk_ctx, "#A:", 0, g_nk_color.a, 1.0f, 0.01f,0.005f);
  1117. }
  1118. // nk_layout_row_begin(g_nk_ctx, NK_STATIC, 2, 3);
  1119. {
  1120. g_pixel_frame.paint_radius = nk_propertyi(g_nk_ctx, "Paint Radius", 0, g_pixel_frame.paint_radius, 20, 1, 0.8f);
  1121. // nk_layout_row_static(g_nk_ctx, 30, 80, 1);
  1122. if (nk_button_label(g_nk_ctx, "Save"))
  1123. {
  1124. pixel_frame_save_image_to_disk(pf);
  1125. }
  1126. // nk_layout_row_static(g_nk_ctx, 30, 80, 1);
  1127. if (nk_button_label(g_nk_ctx, "Load"))
  1128. {
  1129. pixel_frame_load_image_from_disk(pf);
  1130. }
  1131. }
  1132. // nk_layout_row_end(g_nk_ctx);
  1133. }
  1134. nk_end(g_nk_ctx);
  1135. nk_glfw3_render(&g_nk_glfw, NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
  1136. }