diff --git a/examples/perf_text/main.c b/examples/perf_text/main.c index f5e76ba..7e69264 100644 --- a/examples/perf_text/main.c +++ b/examples/perf_text/main.c @@ -1,326 +1,327 @@ - -#include -#include - -#define LOG_DEFAULT_LEVEL LOG_LEVEL_MESSAGE -#define LOG_COMPILE_DEBUG - -#include"milepost.h" - -#define LOG_SUBSYSTEM "Main" - -static const char* TEST_STRING = -"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla quam enim, aliquam in placerat luctus, rutrum in quam. " -"Cras urna elit, pellentesque ac ipsum at, lobortis scelerisque eros. Aenean et turpis nibh. Maecenas lectus augue, eleifend " -"nec efficitur eu, faucibus eget turpis. Suspendisse vel nulla mi. Duis imperdiet neque orci, ac ultrices orci molestie a. " -"Etiam malesuada vulputate hendrerit. Cras ultricies diam in lectus finibus, eu laoreet diam rutrum.\n" -"\n" -"Etiam dictum orci arcu, ac fermentum leo dapibus lacinia. Integer vitae elementum ex. Vestibulum tempor nunc eu hendrerit " -"ornare. Nunc pretium ligula sit amet massa pulvinar, vitae imperdiet justo bibendum. Maecenas consectetur elementum mi, sed " -"vehicula neque pulvinar sit amet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tortor erat, accumsan in laoreet " -"quis, placerat nec enim. Nulla facilisi. Morbi vitae nibh ligula. Suspendisse in molestie magna, eget aliquet mauris. Sed " -"aliquam faucibus magna.\n" -"\n" -"Sed metus odio, imperdiet et consequat non, faucibus nec risus. Suspendisse facilisis sem neque, id scelerisque dui mattis sit " -"amet. Nullam tincidunt nisl nec dui dignissim mattis. Proin fermentum ornare ipsum. Proin eleifend, mi vitae porttitor placerat, " -"neque magna elementum turpis, eu aliquet mi urna et leo. Pellentesque interdum est mauris, sed pellentesque risus blandit in. " -"Phasellus dignissim consequat eros, at aliquam elit finibus posuere. Proin suscipit tortor leo, id vulputate odio lobortis in. " -"Vestibulum et orci ligula. Sed scelerisque nunc non nisi aliquam, vel eleifend felis suscipit. Integer posuere sapien elit, " -"lacinia ultricies nibh sodales nec.\n" -"\n" -"Etiam aliquam purus sit amet purus ultricies tristique. Nunc maximus nunc quis magna ornare, vel interdum urna fermentum. " -"Vestibulum cursus nisl ut nulla egestas, quis mattis elit venenatis. Praesent malesuada mi non magna aliquam fringilla eget eu " -"turpis. Integer suscipit elit vel consectetur vulputate. Integer euismod, erat eget elementum tempus, magna metus consectetur " -"elit, sed feugiat urna sapien sodales sapien. Sed sit amet varius nunc. Curabitur sodales nunc justo, ac scelerisque ipsum semper " -"eget. Integer ornare, velit ut hendrerit dapibus, erat mauris commodo justo, vel semper urna justo non mauris. Proin blandit, " -"enim ut posuere placerat, leo nibh tristique eros, ut pulvinar sapien elit eget enim. Pellentesque et mauris lectus. Curabitur " -"quis lobortis leo, sit amet egestas dui. Nullam ut sapien eu justo lacinia ultrices. Ut tincidunt, sem non luctus tempus, felis " -"purus imperdiet nisi, non ultricies libero ipsum eu augue. Mauris at luctus enim.\n" -"\n" -"Aliquam sed tortor a justo pulvinar dictum consectetur eu felis. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices " -"posuere cubilia curae; Etiam vehicula porttitor volutpat. Morbi fringilla tortor nec accumsan aliquet. Aliquam in commodo neque. " -"Sed laoreet tellus in consectetur aliquet. Nullam nibh eros, feugiat sit amet aliquam non, malesuada vel urna. Ut vel egestas nunc. " -"Pellentesque vitae ante quis ante pharetra pretium. Nam quis eros commodo, mattis enim sed, finibus ante. Quisque lacinia tortor ut " -"odio laoreet, vel viverra libero porttitor. Vestibulum vitae dapibus ex. Phasellus varius lorem sed justo sollicitudin faucibus. " -"Etiam aliquam lacinia consectetur. Phasellus nulla ipsum, viverra non nulla in, rhoncus posuere nunc.\n" -"\n" -"Phasellus efficitur commodo tellus, eget lobortis erat porta quis. Aenean condimentum tortor ut neque dapibus, vitae vulputate quam " -"condimentum. Aliquam elementum vitae nulla vitae tristique. Suspendisse feugiat turpis ac magna dapibus, ut blandit diam tincidunt. " -"Integer id dui id enim ullamcorper dictum. Maecenas malesuada vitae ex pharetra iaculis. Curabitur eu dolor consectetur, tempus augue " -"sed, finibus est. Nulla facilisi. Vivamus sed lacinia turpis, in gravida dolor. Aenean interdum consectetur enim a malesuada. Sed turpis " -"nisi, lacinia et fermentum nec, pharetra id dui. Vivamus neque ligula, iaculis sed tempor eget, vehicula blandit quam. Morbi rhoncus quam " -"semper magna mollis luctus. Donec eu dolor ut ante ullamcorper porta. Mauris et est tristique libero pharetra faucibus.\n" -"\n" -"Duis ut elementum sem. Praesent commodo erat nec sem ultricies sollicitudin. Suspendisse a pellentesque sapien. Nunc ac magna a dui " -"elementum luctus non a mi. Cras elementum nunc sed nunc gravida, sit amet accumsan tortor pulvinar. Etiam elit arcu, pellentesque non ex " -"id, vestibulum pellentesque velit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque habitant morbi tristique senectus " -"et netus et malesuada fames ac turpis egestas. Proin sit amet velit eget tellus vulputate sagittis eget non massa. Cras accumsan tempor " -"tortor, quis rutrum neque placerat id. Nullam a egestas eros, eu porta nisi. Aenean rutrum, sapien quis fermentum tempus, dolor orci " -"faucibus eros, vel luctus justo leo vitae ante. Curabitur aliquam condimentum ipsum sit amet ultrices. Nullam ac velit semper, dapibus urna " -"sit amet, malesuada enim. Mauris ultricies nibh orci."; - - -mg_font create_font(const char* path) -{ - //NOTE(martin): create font - str8 fontPath = mp_app_get_resource_path(mem_scratch(), path); - char* fontPathCString = str8_to_cstring(mem_scratch(), fontPath); - - FILE* fontFile = fopen(fontPathCString, "r"); - if(!fontFile) - { - log_error("Could not load font file '%s'\n", fontPathCString); - return(mg_font_nil()); - } - unsigned char* fontData = 0; - fseek(fontFile, 0, SEEK_END); - u32 fontDataSize = ftell(fontFile); - rewind(fontFile); - fontData = (unsigned char*)malloc(fontDataSize); - fread(fontData, 1, fontDataSize, fontFile); - fclose(fontFile); - - unicode_range ranges[5] = {UNICODE_RANGE_BASIC_LATIN, - UNICODE_RANGE_C1_CONTROLS_AND_LATIN_1_SUPPLEMENT, - UNICODE_RANGE_LATIN_EXTENDED_A, - UNICODE_RANGE_LATIN_EXTENDED_B, - UNICODE_RANGE_SPECIALS}; - - mg_font font = mg_font_create_from_memory(fontDataSize, fontData, 5, ranges); - free(fontData); - - return(font); -} - -int main() -{ - mp_init(); - mp_clock_init(); - - mp_rect rect = {.x = 100, .y = 100, .w = 980, .h = 600}; - mp_window window = mp_window_create(rect, "test", 0); - - mp_rect contentRect = mp_window_get_content_rect(window); - - //NOTE: create surface, canvas and font - - mg_surface surface = mg_surface_create_for_window(window, MG_CANVAS); - mg_surface_swap_interval(surface, 0); - - mg_canvas canvas = mg_canvas_create(); - - const int fontCount = 3; - int fontIndex = 0; - mg_font fonts[fontCount] = {create_font("../resources/OpenSansLatinSubset.ttf"), - create_font("../resources/CMUSerif-Roman.ttf"), - create_font("../resources/courier.ttf")}; - - mg_font_extents extents[fontCount]; - f32 fontScales[fontCount]; - f32 lineHeights[fontCount]; - - for(int i=0; itype) - { - case MP_EVENT_WINDOW_CLOSE: - { - mp_request_quit(); - } break; - - case MP_EVENT_MOUSE_BUTTON: - { - if(event->key.code == MP_MOUSE_LEFT) - { - if(event->key.action == MP_KEY_PRESS) - { - tracked = true; - vec2 mousePos = mp_mouse_position(&inputState); - trackPoint.x = mousePos.x/zoom - startX; - trackPoint.y = mousePos.y/zoom - startY; - } - else - { - tracked = false; - } - } - } break; - - case MP_EVENT_MOUSE_WHEEL: - { - vec2 mousePos = mp_mouse_position(&inputState); - f32 trackX = mousePos.x/zoom - startX; - f32 trackY = mousePos.y/zoom - startY; - - zoom *= 1 + event->move.deltaY * 0.01; - zoom = Clamp(zoom, 0.2, 10); - - startX = mousePos.x/zoom - trackX; - startY = mousePos.y/zoom - trackY; - } break; - - case MP_EVENT_KEYBOARD_KEY: - { - if(event->key.code == MP_KEY_SPACE && event->key.action == MP_KEY_PRESS) - { - fontIndex = (fontIndex+1)%fontCount; - } - } break; - - default: - break; - } - } - - if(tracked) - { - vec2 mousePos = mp_mouse_position(&inputState); - startX = mousePos.x/zoom - trackPoint.x; - startY = mousePos.y/zoom - trackPoint.y; - } - - f32 textX = startX; - f32 textY = startY; - -/* - mg_set_color_rgba(1, 1, 1, 1); - mg_clear(); - mg_set_color_rgba(1, 0, 0, 1); - for(int i=0; i<1000; i++) - { - mg_rectangle_fill(0, 0, 100, 100); - } -*/ - - mg_matrix_push((mg_mat2x3){zoom, 0, 0, - 0, zoom, 0}); - - mg_set_color_rgba(1, 1, 1, 1); - mg_clear(); - - mg_set_font(fonts[fontIndex]); - mg_set_font_size(14); - mg_set_color_rgba(0, 0, 0, 1); - - mg_move_to(textX, textY); - - int startIndex = 0; - while(startIndex < codePointCount) - { - bool lineBreak = false; - int subIndex = 0; - for(; (startIndex+subIndex) < codePointCount && subIndex < 120; subIndex++) - { - if(codePoints[startIndex + subIndex] == '\n') - { - break; - } - } - - u32 glyphs[512]; - mg_font_get_glyph_indices(fonts[fontIndex], (str32){subIndex, codePoints+startIndex}, (str32){512, glyphs}); - - mg_glyph_outlines((str32){subIndex, glyphs}); - mg_fill(); - - textY += lineHeights[fontIndex]; - mg_move_to(textX, textY); - startIndex++; - - startIndex += subIndex; - } - - mg_matrix_pop(); - - mg_set_color_rgba(0, 0, 1, 1); - mg_set_font(fonts[fontIndex]); - mg_set_font_size(14); - mg_move_to(10, contentRect.h - 10 - lineHeights[fontIndex]); - - str8 text = str8_pushf(mem_scratch(), - "Test program: %i glyphs, frame time = %fs, fps = %f", - glyphCount, - frameTime, - 1./frameTime); - mg_text_outlines(text); - mg_fill(); - - - f64 startFlushTime = mp_get_time(MP_CLOCK_MONOTONIC); - - mg_surface_prepare(surface); - mg_render(surface, canvas); - - f64 startPresentTime = mp_get_time(MP_CLOCK_MONOTONIC); - mg_surface_present(surface); - - f64 endFrameTime = mp_get_time(MP_CLOCK_MONOTONIC); - - frameTime = (endFrameTime - startFrameTime); - - printf("frame time: %.2fms (%.2fFPS), draw = %f.2ms, flush = %.2fms, present = %.2fms\n", - frameTime*1000, - 1./frameTime, - (startFlushTime - startFrameTime)*1000, - (startPresentTime - startFlushTime)*1000, - (endFrameTime - startPresentTime)*1000); - - mp_input_next_frame(&inputState); - mem_arena_clear(mem_scratch()); - } - - - for(int i=0; i +#include + +#define LOG_DEFAULT_LEVEL LOG_LEVEL_MESSAGE +#define LOG_COMPILE_DEBUG + +#include"milepost.h" + +#define LOG_SUBSYSTEM "Main" + +static const char* TEST_STRING = +"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla quam enim, aliquam in placerat luctus, rutrum in quam. " +"Cras urna elit, pellentesque ac ipsum at, lobortis scelerisque eros. Aenean et turpis nibh. Maecenas lectus augue, eleifend " +"nec efficitur eu, faucibus eget turpis. Suspendisse vel nulla mi. Duis imperdiet neque orci, ac ultrices orci molestie a. " +"Etiam malesuada vulputate hendrerit. Cras ultricies diam in lectus finibus, eu laoreet diam rutrum.\n" +"\n" +"Etiam dictum orci arcu, ac fermentum leo dapibus lacinia. Integer vitae elementum ex. Vestibulum tempor nunc eu hendrerit " +"ornare. Nunc pretium ligula sit amet massa pulvinar, vitae imperdiet justo bibendum. Maecenas consectetur elementum mi, sed " +"vehicula neque pulvinar sit amet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tortor erat, accumsan in laoreet " +"quis, placerat nec enim. Nulla facilisi. Morbi vitae nibh ligula. Suspendisse in molestie magna, eget aliquet mauris. Sed " +"aliquam faucibus magna.\n" +"\n" +"Sed metus odio, imperdiet et consequat non, faucibus nec risus. Suspendisse facilisis sem neque, id scelerisque dui mattis sit " +"amet. Nullam tincidunt nisl nec dui dignissim mattis. Proin fermentum ornare ipsum. Proin eleifend, mi vitae porttitor placerat, " +"neque magna elementum turpis, eu aliquet mi urna et leo. Pellentesque interdum est mauris, sed pellentesque risus blandit in. " +"Phasellus dignissim consequat eros, at aliquam elit finibus posuere. Proin suscipit tortor leo, id vulputate odio lobortis in. " +"Vestibulum et orci ligula. Sed scelerisque nunc non nisi aliquam, vel eleifend felis suscipit. Integer posuere sapien elit, " +"lacinia ultricies nibh sodales nec.\n" +"\n" +"Etiam aliquam purus sit amet purus ultricies tristique. Nunc maximus nunc quis magna ornare, vel interdum urna fermentum. " +"Vestibulum cursus nisl ut nulla egestas, quis mattis elit venenatis. Praesent malesuada mi non magna aliquam fringilla eget eu " +"turpis. Integer suscipit elit vel consectetur vulputate. Integer euismod, erat eget elementum tempus, magna metus consectetur " +"elit, sed feugiat urna sapien sodales sapien. Sed sit amet varius nunc. Curabitur sodales nunc justo, ac scelerisque ipsum semper " +"eget. Integer ornare, velit ut hendrerit dapibus, erat mauris commodo justo, vel semper urna justo non mauris. Proin blandit, " +"enim ut posuere placerat, leo nibh tristique eros, ut pulvinar sapien elit eget enim. Pellentesque et mauris lectus. Curabitur " +"quis lobortis leo, sit amet egestas dui. Nullam ut sapien eu justo lacinia ultrices. Ut tincidunt, sem non luctus tempus, felis " +"purus imperdiet nisi, non ultricies libero ipsum eu augue. Mauris at luctus enim.\n" +"\n" +"Aliquam sed tortor a justo pulvinar dictum consectetur eu felis. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices " +"posuere cubilia curae; Etiam vehicula porttitor volutpat. Morbi fringilla tortor nec accumsan aliquet. Aliquam in commodo neque. " +"Sed laoreet tellus in consectetur aliquet. Nullam nibh eros, feugiat sit amet aliquam non, malesuada vel urna. Ut vel egestas nunc. " +"Pellentesque vitae ante quis ante pharetra pretium. Nam quis eros commodo, mattis enim sed, finibus ante. Quisque lacinia tortor ut " +"odio laoreet, vel viverra libero porttitor. Vestibulum vitae dapibus ex. Phasellus varius lorem sed justo sollicitudin faucibus. " +"Etiam aliquam lacinia consectetur. Phasellus nulla ipsum, viverra non nulla in, rhoncus posuere nunc.\n" +"\n" +"Phasellus efficitur commodo tellus, eget lobortis erat porta quis. Aenean condimentum tortor ut neque dapibus, vitae vulputate quam " +"condimentum. Aliquam elementum vitae nulla vitae tristique. Suspendisse feugiat turpis ac magna dapibus, ut blandit diam tincidunt. " +"Integer id dui id enim ullamcorper dictum. Maecenas malesuada vitae ex pharetra iaculis. Curabitur eu dolor consectetur, tempus augue " +"sed, finibus est. Nulla facilisi. Vivamus sed lacinia turpis, in gravida dolor. Aenean interdum consectetur enim a malesuada. Sed turpis " +"nisi, lacinia et fermentum nec, pharetra id dui. Vivamus neque ligula, iaculis sed tempor eget, vehicula blandit quam. Morbi rhoncus quam " +"semper magna mollis luctus. Donec eu dolor ut ante ullamcorper porta. Mauris et est tristique libero pharetra faucibus.\n" +"\n" +"Duis ut elementum sem. Praesent commodo erat nec sem ultricies sollicitudin. Suspendisse a pellentesque sapien. Nunc ac magna a dui " +"elementum luctus non a mi. Cras elementum nunc sed nunc gravida, sit amet accumsan tortor pulvinar. Etiam elit arcu, pellentesque non ex " +"id, vestibulum pellentesque velit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque habitant morbi tristique senectus " +"et netus et malesuada fames ac turpis egestas. Proin sit amet velit eget tellus vulputate sagittis eget non massa. Cras accumsan tempor " +"tortor, quis rutrum neque placerat id. Nullam a egestas eros, eu porta nisi. Aenean rutrum, sapien quis fermentum tempus, dolor orci " +"faucibus eros, vel luctus justo leo vitae ante. Curabitur aliquam condimentum ipsum sit amet ultrices. Nullam ac velit semper, dapibus urna " +"sit amet, malesuada enim. Mauris ultricies nibh orci."; + + +mg_font create_font(const char* path) +{ + //NOTE(martin): create font + str8 fontPath = mp_app_get_resource_path(mem_scratch(), path); + char* fontPathCString = str8_to_cstring(mem_scratch(), fontPath); + + FILE* fontFile = fopen(fontPathCString, "r"); + if(!fontFile) + { + log_error("Could not load font file '%s'\n", fontPathCString); + return(mg_font_nil()); + } + unsigned char* fontData = 0; + fseek(fontFile, 0, SEEK_END); + u32 fontDataSize = ftell(fontFile); + rewind(fontFile); + fontData = (unsigned char*)malloc(fontDataSize); + fread(fontData, 1, fontDataSize, fontFile); + fclose(fontFile); + + unicode_range ranges[5] = {UNICODE_RANGE_BASIC_LATIN, + UNICODE_RANGE_C1_CONTROLS_AND_LATIN_1_SUPPLEMENT, + UNICODE_RANGE_LATIN_EXTENDED_A, + UNICODE_RANGE_LATIN_EXTENDED_B, + UNICODE_RANGE_SPECIALS}; + + mg_font font = mg_font_create_from_memory(fontDataSize, fontData, 5, ranges); + free(fontData); + + return(font); +} + +enum { FONT_COUNT = 3 }; + +int main() +{ + mp_init(); + mp_clock_init(); + + mp_rect rect = {.x = 100, .y = 100, .w = 980, .h = 600}; + mp_window window = mp_window_create(rect, "test", 0); + + mp_rect contentRect = mp_window_get_content_rect(window); + + //NOTE: create surface, canvas and font + + mg_surface surface = mg_surface_create_for_window(window, MG_CANVAS); + mg_surface_swap_interval(surface, 0); + + mg_canvas canvas = mg_canvas_create(); + + int fontIndex = 0; + mg_font fonts[FONT_COUNT] = {create_font("../resources/OpenSansLatinSubset.ttf"), + create_font("../resources/CMUSerif-Roman.ttf"), + create_font("../resources/courier.ttf")}; + + mg_font_extents extents[FONT_COUNT]; + f32 fontScales[FONT_COUNT]; + f32 lineHeights[FONT_COUNT]; + + for(int i=0; itype) + { + case MP_EVENT_WINDOW_CLOSE: + { + mp_request_quit(); + } break; + + case MP_EVENT_MOUSE_BUTTON: + { + if(event->key.code == MP_MOUSE_LEFT) + { + if(event->key.action == MP_KEY_PRESS) + { + tracked = true; + vec2 mousePos = mp_mouse_position(&inputState); + trackPoint.x = mousePos.x/zoom - startX; + trackPoint.y = mousePos.y/zoom - startY; + } + else + { + tracked = false; + } + } + } break; + + case MP_EVENT_MOUSE_WHEEL: + { + vec2 mousePos = mp_mouse_position(&inputState); + f32 trackX = mousePos.x/zoom - startX; + f32 trackY = mousePos.y/zoom - startY; + + zoom *= 1 + event->move.deltaY * 0.01; + zoom = Clamp(zoom, 0.2, 10); + + startX = mousePos.x/zoom - trackX; + startY = mousePos.y/zoom - trackY; + } break; + + case MP_EVENT_KEYBOARD_KEY: + { + if(event->key.code == MP_KEY_SPACE && event->key.action == MP_KEY_PRESS) + { + fontIndex = (fontIndex+1)%FONT_COUNT; + } + } break; + + default: + break; + } + } + + if(tracked) + { + vec2 mousePos = mp_mouse_position(&inputState); + startX = mousePos.x/zoom - trackPoint.x; + startY = mousePos.y/zoom - trackPoint.y; + } + + f32 textX = startX; + f32 textY = startY; + +/* + mg_set_color_rgba(1, 1, 1, 1); + mg_clear(); + mg_set_color_rgba(1, 0, 0, 1); + for(int i=0; i<1000; i++) + { + mg_rectangle_fill(0, 0, 100, 100); + } +*/ + + mg_matrix_push((mg_mat2x3){zoom, 0, 0, + 0, zoom, 0}); + + mg_set_color_rgba(1, 1, 1, 1); + mg_clear(); + + mg_set_font(fonts[fontIndex]); + mg_set_font_size(14); + mg_set_color_rgba(0, 0, 0, 1); + + mg_move_to(textX, textY); + + int startIndex = 0; + while(startIndex < codePointCount) + { + bool lineBreak = false; + int subIndex = 0; + for(; (startIndex+subIndex) < codePointCount && subIndex < 120; subIndex++) + { + if(codePoints[startIndex + subIndex] == '\n') + { + break; + } + } + + u32 glyphs[512]; + mg_font_get_glyph_indices(fonts[fontIndex], (str32){subIndex, codePoints+startIndex}, (str32){512, glyphs}); + + mg_glyph_outlines((str32){subIndex, glyphs}); + mg_fill(); + + textY += lineHeights[fontIndex]; + mg_move_to(textX, textY); + startIndex++; + + startIndex += subIndex; + } + + mg_matrix_pop(); + + mg_set_color_rgba(0, 0, 1, 1); + mg_set_font(fonts[fontIndex]); + mg_set_font_size(14); + mg_move_to(10, contentRect.h - 10 - lineHeights[fontIndex]); + + str8 text = str8_pushf(mem_scratch(), + "Test program: %i glyphs, frame time = %fs, fps = %f", + glyphCount, + frameTime, + 1./frameTime); + mg_text_outlines(text); + mg_fill(); + + + f64 startFlushTime = mp_get_time(MP_CLOCK_MONOTONIC); + + mg_surface_prepare(surface); + mg_render(surface, canvas); + + f64 startPresentTime = mp_get_time(MP_CLOCK_MONOTONIC); + mg_surface_present(surface); + + f64 endFrameTime = mp_get_time(MP_CLOCK_MONOTONIC); + + frameTime = (endFrameTime - startFrameTime); + + printf("frame time: %.2fms (%.2fFPS), draw = %f.2ms, flush = %.2fms, present = %.2fms\n", + frameTime*1000, + 1./frameTime, + (startFlushTime - startFrameTime)*1000, + (startPresentTime - startFlushTime)*1000, + (endFrameTime - startPresentTime)*1000); + + mp_input_next_frame(&inputState); + mem_arena_clear(mem_scratch()); + } + + + for(int i=0; i -#include -#include - -#define _USE_MATH_DEFINES //NOTE: necessary for MSVC -#include - -#include"milepost.h" - -#include"tiger.c" - -mg_font create_font() -{ - //NOTE(martin): create font - str8 fontPath = mp_app_get_resource_path(mem_scratch(), "../resources/OpenSansLatinSubset.ttf"); - char* fontPathCString = str8_to_cstring(mem_scratch(), fontPath); - - FILE* fontFile = fopen(fontPathCString, "r"); - if(!fontFile) - { - log_error("Could not load font file '%s': %s\n", fontPathCString, strerror(errno)); - return(mg_font_nil()); - } - unsigned char* fontData = 0; - fseek(fontFile, 0, SEEK_END); - u32 fontDataSize = ftell(fontFile); - rewind(fontFile); - fontData = (unsigned char*)malloc(fontDataSize); - fread(fontData, 1, fontDataSize, fontFile); - fclose(fontFile); - - unicode_range ranges[5] = {UNICODE_RANGE_BASIC_LATIN, - UNICODE_RANGE_C1_CONTROLS_AND_LATIN_1_SUPPLEMENT, - UNICODE_RANGE_LATIN_EXTENDED_A, - UNICODE_RANGE_LATIN_EXTENDED_B, - UNICODE_RANGE_SPECIALS}; - - mg_font font = mg_font_create_from_memory(fontDataSize, fontData, 5, ranges); - free(fontData); - - return(font); -} - -int main() -{ - mp_init(); - mp_clock_init(); //TODO put that in mp_init()? - - mp_rect windowRect = {.x = 100, .y = 100, .w = 810, .h = 610}; - mp_window window = mp_window_create(windowRect, "test", 0); - - mp_rect contentRect = mp_window_get_content_rect(window); - - //NOTE: create surface - mg_surface surface = mg_surface_create_for_window(window, MG_CANVAS); - mg_surface_swap_interval(surface, 0); - - //TODO: create canvas - mg_canvas canvas = mg_canvas_create(); - - if(mg_canvas_is_nil(canvas)) - { - printf("Error: couldn't create canvas\n"); - return(-1); - } - - mg_font font = create_font(); - - // start app - mp_window_bring_to_front(window); - mp_window_focus(window); - - bool tracked = false; - vec2 trackPoint = {0}; - - f32 zoom = 1; - f32 startX = 300, startY = 200; - bool singlePath = false; - int singlePathIndex = 0; - - f64 frameTime = 0; - - mp_input_state inputState = {0}; - - while(!mp_should_quit()) - { - f64 startTime = mp_get_time(MP_CLOCK_MONOTONIC); - - mp_pump_events(0); - mp_event* event = 0; - while((event = mp_next_event(mem_scratch())) != 0) - { - mp_input_process_event(&inputState, event); - - switch(event->type) - { - case MP_EVENT_WINDOW_CLOSE: - { - mp_request_quit(); - } break; - - case MP_EVENT_WINDOW_RESIZE: - { - mp_rect frame = {0, 0, event->frame.rect.w, event->frame.rect.h}; - mg_surface_set_frame(surface, frame); - } break; - - case MP_EVENT_MOUSE_BUTTON: - { - if(event->key.code == MP_MOUSE_LEFT) - { - if(event->key.action == MP_KEY_PRESS) - { - tracked = true; - vec2 mousePos = mp_mouse_position(&inputState); - trackPoint.x = (mousePos.x - startX)/zoom; - trackPoint.y = (mousePos.y - startY)/zoom; - } - else - { - tracked = false; - } - } - } break; - - case MP_EVENT_MOUSE_WHEEL: - { - vec2 mousePos = mp_mouse_position(&inputState); - f32 pinX = (mousePos.x - startX)/zoom; - f32 pinY = (mousePos.y - startY)/zoom; - - zoom *= 1 + event->move.deltaY * 0.01; - zoom = Clamp(zoom, 0.5, 5); - - startX = mousePos.x - pinX*zoom; - startY = mousePos.y - pinY*zoom; - } break; - - case MP_EVENT_KEYBOARD_KEY: - { - if(event->key.action == MP_KEY_PRESS || event->key.action == MP_KEY_REPEAT) - { - switch(event->key.code) - { - case MP_KEY_SPACE: - singlePath = !singlePath; - break; - - case MP_KEY_UP: - { - if(event->key.mods & MP_KEYMOD_SHIFT) - { - singlePathIndex++; - } - else - { - zoom += 0.001; - } - } break; - - case MP_KEY_DOWN: - { - if(event->key.mods & MP_KEYMOD_SHIFT) - { - singlePathIndex--; - } - else - { - zoom -= 0.001; - } - } break; - } - } - } break; - - default: - break; - } - } - - if(tracked) - { - vec2 mousePos = mp_mouse_position(&inputState); - startX = mousePos.x - trackPoint.x*zoom; - startY = mousePos.y - trackPoint.y*zoom; - } - - mg_surface_prepare(surface); - - mg_set_color_rgba(1, 0, 1, 1); - mg_clear(); - - mg_matrix_push((mg_mat2x3){zoom, 0, startX, - 0, zoom, startY}); - - draw_tiger(singlePath, singlePathIndex); - - if(singlePath) - { - printf("display single path %i\n", singlePathIndex); - printf("viewpos = (%f, %f), zoom = %f\n", startX, startY, zoom); - } - - mg_matrix_pop(); - - // text - mg_set_color_rgba(0, 0, 1, 1); - mg_set_font(font); - mg_set_font_size(12); - mg_move_to(50, 600-50); - - str8 text = str8_pushf(mem_scratch(), - "Milepost vector graphics test program (frame time = %fs, fps = %f)...", - frameTime, - 1./frameTime); - mg_text_outlines(text); - mg_fill(); - - printf("Milepost vector graphics test program (frame time = %fs, fps = %f)...\n", - frameTime, - 1./frameTime); - - mg_render(surface, canvas); - mg_surface_present(surface); - - mp_input_next_frame(&inputState); - mem_arena_clear(mem_scratch()); - frameTime = mp_get_time(MP_CLOCK_MONOTONIC) - startTime; - } - - mg_font_destroy(font); - mg_canvas_destroy(canvas); - mg_surface_destroy(surface); - mp_window_destroy(window); - - mp_terminate(); - - return(0); -} +/************************************************************//** +* +* @file: main.cpp +* @author: Martin Fouilleul +* @date: 30/07/2022 +* @revision: +* +*****************************************************************/ +#include +#include +#include +#include + +#define _USE_MATH_DEFINES //NOTE: necessary for MSVC +#include + +#include"milepost.h" + +#include"tiger.c" + +mg_font create_font() +{ + //NOTE(martin): create font + str8 fontPath = mp_app_get_resource_path(mem_scratch(), "../resources/OpenSansLatinSubset.ttf"); + char* fontPathCString = str8_to_cstring(mem_scratch(), fontPath); + + FILE* fontFile = fopen(fontPathCString, "r"); + if(!fontFile) + { + log_error("Could not load font file '%s': %s\n", fontPathCString, strerror(errno)); + return(mg_font_nil()); + } + unsigned char* fontData = 0; + fseek(fontFile, 0, SEEK_END); + u32 fontDataSize = ftell(fontFile); + rewind(fontFile); + fontData = (unsigned char*)malloc(fontDataSize); + fread(fontData, 1, fontDataSize, fontFile); + fclose(fontFile); + + unicode_range ranges[5] = {UNICODE_RANGE_BASIC_LATIN, + UNICODE_RANGE_C1_CONTROLS_AND_LATIN_1_SUPPLEMENT, + UNICODE_RANGE_LATIN_EXTENDED_A, + UNICODE_RANGE_LATIN_EXTENDED_B, + UNICODE_RANGE_SPECIALS}; + + mg_font font = mg_font_create_from_memory(fontDataSize, fontData, 5, ranges); + free(fontData); + + return(font); +} + +int main() +{ + mp_init(); + mp_clock_init(); //TODO put that in mp_init()? + + mp_rect windowRect = {.x = 100, .y = 100, .w = 810, .h = 610}; + mp_window window = mp_window_create(windowRect, "test", 0); + + mp_rect contentRect = mp_window_get_content_rect(window); + + //NOTE: create surface + mg_surface surface = mg_surface_create_for_window(window, MG_CANVAS); + mg_surface_swap_interval(surface, 0); + + //TODO: create canvas + mg_canvas canvas = mg_canvas_create(); + + if(mg_canvas_is_nil(canvas)) + { + printf("Error: couldn't create canvas\n"); + return(-1); + } + + mg_font font = create_font(); + + // start app + mp_window_bring_to_front(window); + mp_window_focus(window); + + bool tracked = false; + vec2 trackPoint = {0}; + + f32 zoom = 1; + f32 startX = 300, startY = 200; + bool singlePath = false; + int singlePathIndex = 0; + + f64 frameTime = 0; + + mp_input_state inputState = {0}; + + while(!mp_should_quit()) + { + f64 startTime = mp_get_time(MP_CLOCK_MONOTONIC); + + mp_pump_events(0); + mp_event* event = 0; + while((event = mp_next_event(mem_scratch())) != 0) + { + mp_input_process_event(&inputState, event); + + switch(event->type) + { + case MP_EVENT_WINDOW_CLOSE: + { + mp_request_quit(); + } break; + + case MP_EVENT_WINDOW_RESIZE: + { + mp_rect frame = {0, 0, event->frame.rect.w, event->frame.rect.h}; + mg_surface_set_frame(surface, frame); + } break; + + case MP_EVENT_MOUSE_BUTTON: + { + if(event->key.code == MP_MOUSE_LEFT) + { + if(event->key.action == MP_KEY_PRESS) + { + tracked = true; + vec2 mousePos = mp_mouse_position(&inputState); + trackPoint.x = (mousePos.x - startX)/zoom; + trackPoint.y = (mousePos.y - startY)/zoom; + } + else + { + tracked = false; + } + } + } break; + + case MP_EVENT_MOUSE_WHEEL: + { + vec2 mousePos = mp_mouse_position(&inputState); + f32 pinX = (mousePos.x - startX)/zoom; + f32 pinY = (mousePos.y - startY)/zoom; + + zoom *= 1 + event->move.deltaY * 0.01; + zoom = Clamp(zoom, 0.5, 5); + + startX = mousePos.x - pinX*zoom; + startY = mousePos.y - pinY*zoom; + } break; + + case MP_EVENT_KEYBOARD_KEY: + { + if(event->key.action == MP_KEY_PRESS || event->key.action == MP_KEY_REPEAT) + { + switch(event->key.code) + { + case MP_KEY_SPACE: + singlePath = !singlePath; + break; + + case MP_KEY_UP: + { + if(event->key.mods & MP_KEYMOD_SHIFT) + { + singlePathIndex++; + } + else + { + zoom += 0.001; + } + } break; + + case MP_KEY_DOWN: + { + if(event->key.mods & MP_KEYMOD_SHIFT) + { + singlePathIndex--; + } + else + { + zoom -= 0.001; + } + } break; + } + } + } break; + + default: + break; + } + } + + if(tracked) + { + vec2 mousePos = mp_mouse_position(&inputState); + startX = mousePos.x - trackPoint.x*zoom; + startY = mousePos.y - trackPoint.y*zoom; + } + + mg_surface_prepare(surface); + + mg_set_color_rgba(1, 0, 1, 1); + mg_clear(); + + mg_matrix_push((mg_mat2x3){zoom, 0, startX, + 0, zoom, startY}); + + draw_tiger(singlePath, singlePathIndex); + + if(singlePath) + { + printf("display single path %i\n", singlePathIndex); + printf("viewpos = (%f, %f), zoom = %f\n", startX, startY, zoom); + } + + mg_matrix_pop(); + + // text + mg_set_color_rgba(0, 0, 1, 1); + mg_set_font(font); + mg_set_font_size(12); + mg_move_to(50, 600-50); + + str8 text = str8_pushf(mem_scratch(), + "Milepost vector graphics test program (frame time = %fs, fps = %f)...", + frameTime, + 1./frameTime); + mg_text_outlines(text); + mg_fill(); + + printf("Milepost vector graphics test program (frame time = %fs, fps = %f)...\n", + frameTime, + 1./frameTime); + + mg_render(surface, canvas); + mg_surface_present(surface); + + mp_input_next_frame(&inputState); + mem_arena_clear(mem_scratch()); + frameTime = mp_get_time(MP_CLOCK_MONOTONIC) - startTime; + } + + mg_font_destroy(font); + mg_canvas_destroy(canvas); + mg_surface_destroy(surface); + mp_window_destroy(window); + + mp_terminate(); + + return(0); +} diff --git a/examples/ui/main.c b/examples/ui/main.c index 161af0a..9b6b457 100644 --- a/examples/ui/main.c +++ b/examples/ui/main.c @@ -1,605 +1,610 @@ -/************************************************************//** -* -* @file: main.cpp -* @author: Martin Fouilleul -* @date: 30/07/2022 -* @revision: -* -*****************************************************************/ -#include -#include -#include - -#define _USE_MATH_DEFINES //NOTE: necessary for MSVC -#include - -#include"milepost.h" - -#define LOG_SUBSYSTEM "Main" - -void debug_print_indent(int indent) -{ - for(int i=0; ipattern.l, selector, ui_selector, listElt) - { - switch(selector->kind) - { - case UI_SEL_ANY: - printf("any: "); - break; - - case UI_SEL_OWNER: - printf("owner: "); - break; - - case UI_SEL_TEXT: - printf("text='%.*s': ", (int)selector->text.len, selector->text.ptr); - break; - - case UI_SEL_TAG: - printf("tag=0x%llx: ", selector->tag.hash); - break; - - case UI_SEL_STATUS: - { - if(selector->status & UI_HOVER) - { - printf("hover: "); - } - if(selector->status & UI_ACTIVE) - { - printf("active: "); - } - if(selector->status & UI_DRAGGING) - { - printf("dragging: "); - } - } break; - - case UI_SEL_KEY: - printf("key=0x%llx: ", selector->key.hash); - break; - - default: - printf("unknown: "); - break; - } - } - printf("=> font size = %f\n", rule->style->fontSize); -} -void debug_print_size(ui_box* box, ui_axis axis, int indent) -{ - debug_print_indent(indent); - printf("size %s: ", axis == UI_AXIS_X ? "x" : "y"); - f32 value = box->targetStyle->size.c[axis].value; - switch(box->targetStyle->size.c[axis].kind) - { - case UI_SIZE_TEXT: - printf("text\n"); - break; - - case UI_SIZE_CHILDREN: - printf("children\n"); - break; - - case UI_SIZE_PARENT: - printf("parent: %f\n", value); - break; - - case UI_SIZE_PARENT_MINUS_PIXELS: - printf("parent minus pixels: %f\n", value); - break; - - case UI_SIZE_PIXELS: - printf("pixels: %f\n", value); - break; - } - -} - -void debug_print_styles(ui_box* box, int indent) -{ - debug_print_indent(indent); - printf("### box '%.*s'\n", (int)box->string.len, box->string.ptr); - indent++; - - debug_print_indent(indent); - printf("font size: %f\n", box->targetStyle->fontSize); - - debug_print_size(box, UI_AXIS_X, indent); - debug_print_size(box, UI_AXIS_Y, indent); - - if(!list_empty(&box->beforeRules)) - { - debug_print_indent(indent); - printf("before rules:\n"); - for_list(&box->beforeRules, rule, ui_style_rule, boxElt) - { - debug_print_indent(indent+1); - debug_print_rule(rule); - } - } - - if(!list_empty(&box->afterRules)) - { - debug_print_indent(indent); - printf("after rules:\n"); - for_list(&box->afterRules, rule, ui_style_rule, boxElt) - { - debug_print_indent(indent+1); - debug_print_rule(rule); - } - } - - if(!list_empty(&box->children)) - { - debug_print_indent(indent); - printf("children:\n"); - indent++; - for_list(&box->children, child, ui_box, listElt) - { - debug_print_styles(child, indent); - } - } -} - -mg_font create_font() -{ - //NOTE(martin): create font - str8 fontPath = mp_app_get_resource_path(mem_scratch(), "../resources/OpenSansLatinSubset.ttf"); - char* fontPathCString = str8_to_cstring(mem_scratch(), fontPath); - - FILE* fontFile = fopen(fontPathCString, "r"); - if(!fontFile) - { - log_error("Could not load font file '%s': %s\n", fontPathCString, strerror(errno)); - return(mg_font_nil()); - } - unsigned char* fontData = 0; - fseek(fontFile, 0, SEEK_END); - u32 fontDataSize = ftell(fontFile); - rewind(fontFile); - fontData = (unsigned char*)malloc(fontDataSize); - fread(fontData, 1, fontDataSize, fontFile); - fclose(fontFile); - - unicode_range ranges[5] = {UNICODE_RANGE_BASIC_LATIN, - UNICODE_RANGE_C1_CONTROLS_AND_LATIN_1_SUPPLEMENT, - UNICODE_RANGE_LATIN_EXTENDED_A, - UNICODE_RANGE_LATIN_EXTENDED_B, - UNICODE_RANGE_SPECIALS}; - - mg_font font = mg_font_create_from_memory(fontDataSize, fontData, 5, ranges); - free(fontData); - - return(font); -} - -void widget_begin_view(char* str) -{ - ui_style_next(&(ui_style){.layout.axis = UI_AXIS_Y, - .layout.spacing = 10, - .layout.margin.x = 10, - .layout.margin.y = 10, - .layout.align.x = UI_ALIGN_CENTER, - .layout.align.y = UI_ALIGN_START}, - UI_STYLE_LAYOUT); - - ui_box_begin(str, UI_FLAG_DRAW_BORDER); - ui_label(str); - -} - -void widget_end_view(void) -{ - ui_box_end(); -} - -#define widget_view(s) defer_loop(widget_begin_view(s), widget_end_view()) - -int main() -{ - LogLevel(LOG_LEVEL_WARNING); - - mp_init(); - mp_clock_init(); //TODO put that in mp_init()? - - ui_init(); - - mp_rect windowRect = {.x = 100, .y = 100, .w = 810, .h = 610}; - mp_window window = mp_window_create(windowRect, "test", 0); - - mp_rect contentRect = mp_window_get_content_rect(window); - - //NOTE: create surface - mg_surface surface = mg_surface_create_for_window(window, MG_BACKEND_DEFAULT); - mg_surface_swap_interval(surface, 0); - - //TODO: create canvas - mg_canvas canvas = mg_canvas_create(surface); - - if(mg_canvas_is_nil(canvas)) - { - printf("Error: couldn't create canvas\n"); - return(-1); - } - - mg_font font = create_font(); - - mem_arena textArena = {0}; - mem_arena_init(&textArena); - - // start app - mp_window_bring_to_front(window); - mp_window_focus(window); - - while(!mp_should_quit()) - { - bool printDebugStyle = false; - - f64 startTime = mp_get_time(MP_CLOCK_MONOTONIC); - - mp_pump_events(0); - mp_event event = {0}; - while(mp_next_event(&event)) - { - switch(event.type) - { - case MP_EVENT_WINDOW_CLOSE: - { - mp_request_quit(); - } break; - - - case MP_EVENT_KEYBOARD_KEY: - { - if(event.key.action == MP_KEY_PRESS && event.key.code == MP_KEY_P) - { - printDebugStyle = true; - } - } break; - - default: - break; - } - } - - //TEST UI - ui_style defaultStyle = {.bgColor = {0}, - .color = {1, 1, 1, 1}, - .font = font, - .fontSize = 16, - .borderColor = {1, 0, 0, 1}, - .borderSize = 2}; - - ui_style_mask defaultMask = UI_STYLE_BG_COLOR - | UI_STYLE_COLOR - | UI_STYLE_BORDER_COLOR - | UI_STYLE_BORDER_SIZE - | UI_STYLE_FONT - | UI_STYLE_FONT_SIZE; - - ui_flags debugFlags = UI_FLAG_DRAW_BORDER; - - ui_box* root = 0; - ui_frame(&defaultStyle, defaultMask) - { - root = ui_box_top(); - ui_style_match_before(ui_pattern_all(), &defaultStyle, defaultMask); - - ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 1}, - .size.height = {UI_SIZE_PARENT, 1}, - .layout.axis = UI_AXIS_Y, - .layout.align.x = UI_ALIGN_CENTER, - .layout.align.y = UI_ALIGN_START, - .layout.spacing = 10, - .layout.margin.x = 10, - .layout.margin.y = 10, - .bgColor = {0.11, 0.11, 0.11, 1}}, - UI_STYLE_SIZE - | UI_STYLE_LAYOUT - | UI_STYLE_BG_COLOR); - - ui_container("background", UI_FLAG_DRAW_BACKGROUND) - { - ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 1}, - .size.height = {UI_SIZE_CHILDREN}, - .layout.align.x = UI_ALIGN_CENTER}, - UI_STYLE_SIZE - |UI_STYLE_LAYOUT_ALIGN_X); - ui_container("title", debugFlags) - { - ui_style_next(&(ui_style){.fontSize = 26}, UI_STYLE_FONT_SIZE); - ui_label("Milepost UI Demo"); - - if(ui_box_sig(ui_box_top()).hovering) - { - ui_tooltip("tooltip") - { - ui_style_next(&(ui_style){.bgColor = {1, 0.99, 0.82, 1}}, - UI_STYLE_BG_COLOR); - - ui_container("background", UI_FLAG_DRAW_BACKGROUND) - { - ui_style_next(&(ui_style){.color = {0, 0, 0, 1}}, - UI_STYLE_COLOR); - - ui_label("That is a tooltip!"); - } - } - } - } - - ui_menu_bar("Menu bar") - { - ui_menu("Menu 1") - { - if(ui_menu_button("Option 1.1").pressed) - { - printf("Pressed option 1.1\n"); - } - ui_menu_button("Option 1.2"); - ui_menu_button("Option 1.3"); - ui_menu_button("Option 1.4"); - } - - ui_menu("Menu 2") - { - ui_menu_button("Option 2.1"); - ui_menu_button("Option 2.2"); - ui_menu_button("Option 2.3"); - ui_menu_button("Option 2.4"); - } - - ui_menu("Menu 3") - { - ui_menu_button("Option 3.1"); - ui_menu_button("Option 3.2"); - ui_menu_button("Option 3.3"); - ui_menu_button("Option 3.4"); - } - } - - ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 1}, - .size.height = {UI_SIZE_PARENT, 1, 1}}, - UI_STYLE_SIZE); - - ui_style_next(&(ui_style){.layout.axis = UI_AXIS_X}, UI_STYLE_LAYOUT_AXIS); - ui_container("contents", debugFlags) - { - ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 0.5}, - .size.height = {UI_SIZE_PARENT, 1}, - .borderColor = {0, 0, 1, 1}}, - UI_STYLE_SIZE - |UI_STYLE_BORDER_COLOR); - - ui_container("left", debugFlags) - { - ui_style_next(&(ui_style){.layout.axis = UI_AXIS_X, - .layout.spacing = 10, - .layout.margin.x = 10, - .layout.margin.y = 10, - .size.width = {UI_SIZE_PARENT, 1}, - .size.height = {UI_SIZE_PARENT, 0.5}}, - UI_STYLE_LAYOUT_AXIS - |UI_STYLE_LAYOUT_SPACING - |UI_STYLE_LAYOUT_MARGIN_X - |UI_STYLE_LAYOUT_MARGIN_Y - |UI_STYLE_SIZE); - - ui_container("up", debugFlags) - { - ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 0.5}, - .size.height = {UI_SIZE_PARENT, 1}}, - UI_STYLE_SIZE); - widget_view("Buttons") - { - ui_button("Button 1"); - ui_button("Button 2"); - ui_button("Button 3"); - } - - ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 0.5}, - .size.height = {UI_SIZE_PARENT, 1}}, - UI_STYLE_SIZE); - - - ui_pattern pattern = {0}; - ui_pattern_push(mem_scratch(), &pattern, (ui_selector){.kind = UI_SEL_TAG, .tag = ui_tag_make("checkbox")}); - ui_style_match_after(pattern, - &(ui_style){.bgColor = {0, 1, 0, 1}, - .color = {1, 1, 1, 1}}, - UI_STYLE_COLOR | UI_STYLE_BG_COLOR); - - widget_view("checkboxes") - { - static bool check1 = true; - static bool check2 = false; - static bool check3 = false; - - ui_checkbox("check1", &check1); - ui_checkbox("check2", &check2); - ui_checkbox("check3", &check3); - } - } - - ui_style_next(&(ui_style){.layout.axis = UI_AXIS_X, - .size.width = {UI_SIZE_PARENT, 1}, - .size.height = {UI_SIZE_PARENT, 0.5}}, - UI_STYLE_LAYOUT_AXIS - |UI_STYLE_SIZE); - - ui_container("down", debugFlags) - { - widget_view("Vertical Sliders") - { - ui_style_next(&(ui_style){.layout.axis = UI_AXIS_X, - .layout.spacing = 10}, - UI_STYLE_LAYOUT_AXIS - |UI_STYLE_LAYOUT_SPACING); - ui_container("contents", 0) - { - ui_style_next(&(ui_style){.size.width = {UI_SIZE_PIXELS, 20}, - .size.height = {UI_SIZE_PIXELS, 200}}, - UI_STYLE_SIZE); - static f32 slider1 = 0; - ui_slider("slider1", 0.2, &slider1); - - ui_style_next(&(ui_style){.size.width = {UI_SIZE_PIXELS, 20}, - .size.height = {UI_SIZE_PIXELS, 200}}, - UI_STYLE_SIZE); - static f32 slider2 = 0; - ui_slider("slider2", 0.2, &slider2); - - ui_style_next(&(ui_style){.size.width = {UI_SIZE_PIXELS, 20}, - .size.height = {UI_SIZE_PIXELS, 200}}, - UI_STYLE_SIZE); - static f32 slider3 = 0; - ui_slider("slider3", 0.2, &slider3); - } - } - - widget_view("Horizontal Sliders") - { - ui_style_next(&(ui_style){.size.width = {UI_SIZE_PIXELS, 200}, - .size.height = {UI_SIZE_PIXELS, 20}}, - UI_STYLE_SIZE); - static f32 slider1 = 0; - ui_slider("slider1", 0.2, &slider1); - - ui_style_next(&(ui_style){.size.width = {UI_SIZE_PIXELS, 200}, - .size.height = {UI_SIZE_PIXELS, 20}}, - UI_STYLE_SIZE); - static f32 slider2 = 0; - ui_slider("slider2", 0.2, &slider2); - - ui_style_next(&(ui_style){.size.width = {UI_SIZE_PIXELS, 200}, - .size.height = {UI_SIZE_PIXELS, 20}}, - UI_STYLE_SIZE); - static f32 slider3 = 0; - ui_slider("slider3", 0.2, &slider3); - } - } - } - - ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 0.5}, - .size.height = {UI_SIZE_PARENT, 1}}, - UI_STYLE_SIZE); - - ui_container("right", debugFlags) - { - - ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 1}, - .size.height = {UI_SIZE_PARENT, 0.33}}, - UI_STYLE_SIZE); - widget_view("Text box") - { - ui_style_next(&(ui_style){.size.width = {UI_SIZE_PIXELS, 300}, - .size.height = {UI_SIZE_TEXT}}, - UI_STYLE_SIZE); - static str8 text = {0}; - ui_text_box_result res = ui_text_box("textbox", mem_scratch(), text); - if(res.changed) - { - mem_arena_clear(&textArena); - text = str8_push_copy(&textArena, res.text); - } - } - - ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 1}, - .size.height = {UI_SIZE_PARENT, 0.33}}, - UI_STYLE_SIZE); - widget_view("Test") - { - ui_pattern pattern = {}; - ui_pattern_push(mem_scratch(), &pattern, (ui_selector){.kind = UI_SEL_TEXT, .text = STR8("panel")}); - ui_style_match_after(pattern, &(ui_style){.bgColor = {0.3, 0.3, 1, 1}}, UI_STYLE_BG_COLOR); - - static int selected = 0; - str8 options[] = {STR8("option 1"), - STR8("option 2"), - STR8("long option 3"), - STR8("option 4"), - STR8("option 5")}; - ui_select_popup_info info = {.selectedIndex = selected, - .optionCount = 5, - .options = options}; - - ui_select_popup_info result = ui_select_popup("popup", &info); - selected = result.selectedIndex; - } - - ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 1}, - .size.height = {UI_SIZE_PARENT, 0.33}}, - UI_STYLE_SIZE); - widget_view("Color") - { - ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 1}, - .size.height = {UI_SIZE_PARENT, 0.7}, - .layout.axis = UI_AXIS_X}, - UI_STYLE_SIZE - |UI_STYLE_LAYOUT_AXIS); - - ui_panel("Panel", UI_FLAG_DRAW_BORDER) - { - ui_style_next(&(ui_style){.layout.axis = UI_AXIS_X}, - UI_STYLE_LAYOUT_AXIS); - ui_container("contents", 0) - { - ui_style_next(&(ui_style){.layout.spacing = 20}, - UI_STYLE_LAYOUT_SPACING); - ui_container("buttons", 0) - { - ui_button("Button A"); - ui_button("Button B"); - ui_button("Button C"); - ui_button("Button D"); - } - - ui_style_next(&(ui_style){.layout.axis = UI_AXIS_X, - .layout.spacing = 20}, - UI_STYLE_LAYOUT_SPACING - |UI_STYLE_LAYOUT_AXIS); - - ui_container("buttons2", 0) - { - ui_button("Button A"); - ui_button("Button B"); - ui_button("Button C"); - ui_button("Button D"); - } - } - } - } - - } - } - } - } - if(printDebugStyle) - { - debug_print_styles(root, 0); - } - - mg_surface_prepare(surface); - - ui_draw(); - - mg_flush(); - mg_surface_present(surface); - - mem_arena_clear(mem_scratch()); - } - - mg_surface_destroy(surface); - mp_terminate(); - - return(0); -} +/************************************************************//** +* +* @file: main.cpp +* @author: Martin Fouilleul +* @date: 30/07/2022 +* @revision: +* +*****************************************************************/ +#include +#include +#include +#include + +#define _USE_MATH_DEFINES //NOTE: necessary for MSVC +#include + +#include"milepost.h" + +void debug_print_indent(int indent) +{ + for(int i=0; ipattern.l, selector, ui_selector, listElt) + { + switch(selector->kind) + { + case UI_SEL_ANY: + printf("any: "); + break; + + case UI_SEL_OWNER: + printf("owner: "); + break; + + case UI_SEL_TEXT: + printf("text='%.*s': ", (int)selector->text.len, selector->text.ptr); + break; + + case UI_SEL_TAG: + printf("tag=0x%llx: ", selector->tag.hash); + break; + + case UI_SEL_STATUS: + { + if(selector->status & UI_HOVER) + { + printf("hover: "); + } + if(selector->status & UI_ACTIVE) + { + printf("active: "); + } + if(selector->status & UI_DRAGGING) + { + printf("dragging: "); + } + } break; + + case UI_SEL_KEY: + printf("key=0x%llx: ", selector->key.hash); + break; + + default: + printf("unknown: "); + break; + } + } + printf("=> font size = %f\n", rule->style->fontSize); +} +void debug_print_size(ui_box* box, ui_axis axis, int indent) +{ + debug_print_indent(indent); + printf("size %s: ", axis == UI_AXIS_X ? "x" : "y"); + f32 value = box->targetStyle->size.c[axis].value; + switch(box->targetStyle->size.c[axis].kind) + { + case UI_SIZE_TEXT: + printf("text\n"); + break; + + case UI_SIZE_CHILDREN: + printf("children\n"); + break; + + case UI_SIZE_PARENT: + printf("parent: %f\n", value); + break; + + case UI_SIZE_PARENT_MINUS_PIXELS: + printf("parent minus pixels: %f\n", value); + break; + + case UI_SIZE_PIXELS: + printf("pixels: %f\n", value); + break; + } + +} + +void debug_print_styles(ui_box* box, int indent) +{ + debug_print_indent(indent); + printf("### box '%.*s'\n", (int)box->string.len, box->string.ptr); + indent++; + + debug_print_indent(indent); + printf("font size: %f\n", box->targetStyle->fontSize); + + debug_print_size(box, UI_AXIS_X, indent); + debug_print_size(box, UI_AXIS_Y, indent); + + if(!list_empty(&box->beforeRules)) + { + debug_print_indent(indent); + printf("before rules:\n"); + for_list(&box->beforeRules, rule, ui_style_rule, boxElt) + { + debug_print_indent(indent+1); + debug_print_rule(rule); + } + } + + if(!list_empty(&box->afterRules)) + { + debug_print_indent(indent); + printf("after rules:\n"); + for_list(&box->afterRules, rule, ui_style_rule, boxElt) + { + debug_print_indent(indent+1); + debug_print_rule(rule); + } + } + + if(!list_empty(&box->children)) + { + debug_print_indent(indent); + printf("children:\n"); + indent++; + for_list(&box->children, child, ui_box, listElt) + { + debug_print_styles(child, indent); + } + } +} + +mg_font create_font() +{ + //NOTE(martin): create font + str8 fontPath = mp_app_get_resource_path(mem_scratch(), "../resources/OpenSansLatinSubset.ttf"); + char* fontPathCString = str8_to_cstring(mem_scratch(), fontPath); + + FILE* fontFile = fopen(fontPathCString, "r"); + if(!fontFile) + { + log_error("Could not load font file '%s': %s\n", fontPathCString, strerror(errno)); + return(mg_font_nil()); + } + unsigned char* fontData = 0; + fseek(fontFile, 0, SEEK_END); + u32 fontDataSize = ftell(fontFile); + rewind(fontFile); + fontData = (unsigned char*)malloc(fontDataSize); + fread(fontData, 1, fontDataSize, fontFile); + fclose(fontFile); + + unicode_range ranges[5] = {UNICODE_RANGE_BASIC_LATIN, + UNICODE_RANGE_C1_CONTROLS_AND_LATIN_1_SUPPLEMENT, + UNICODE_RANGE_LATIN_EXTENDED_A, + UNICODE_RANGE_LATIN_EXTENDED_B, + UNICODE_RANGE_SPECIALS}; + + mg_font font = mg_font_create_from_memory(fontDataSize, fontData, 5, ranges); + free(fontData); + + return(font); +} + +void widget_begin_view(char* str) +{ + ui_style_next(&(ui_style){.layout.axis = UI_AXIS_Y, + .layout.spacing = 10, + .layout.margin.x = 10, + .layout.margin.y = 10, + .layout.align.x = UI_ALIGN_CENTER, + .layout.align.y = UI_ALIGN_START}, + UI_STYLE_LAYOUT); + + ui_box_begin(str, UI_FLAG_DRAW_BORDER); + ui_label(str); + +} + +void widget_end_view(void) +{ + ui_box_end(); +} + +#define widget_view(s) defer_loop(widget_begin_view(s), widget_end_view()) + +int main() +{ + mp_init(); + mp_clock_init(); //TODO put that in mp_init()? + + ui_context context; + ui_init(&context); + ui_set_context(&context); + + mp_rect windowRect = {.x = 100, .y = 100, .w = 810, .h = 610}; + mp_window window = mp_window_create(windowRect, "test", 0); + + mp_rect contentRect = mp_window_get_content_rect(window); + + //NOTE: create surface + mg_surface surface = mg_surface_create_for_window(window, MG_CANVAS); + mg_surface_swap_interval(surface, 0); + + //TODO: create canvas + mg_canvas canvas = mg_canvas_create(); + + if(mg_canvas_is_nil(canvas)) + { + printf("Error: couldn't create canvas\n"); + return(-1); + } + + mg_font font = create_font(); + + mem_arena textArena = {0}; + mem_arena_init(&textArena); + + // start app + mp_window_bring_to_front(window); + mp_window_focus(window); + + while(!mp_should_quit()) + { + bool printDebugStyle = false; + + f64 startTime = mp_get_time(MP_CLOCK_MONOTONIC); + + mp_pump_events(0); + mp_event* event = 0; + while((event = mp_next_event(mem_scratch())) != 0) + { + ui_process_event(event); + + switch(event->type) + { + case MP_EVENT_WINDOW_CLOSE: + { + mp_request_quit(); + } break; + + + case MP_EVENT_KEYBOARD_KEY: + { + if(event->key.action == MP_KEY_PRESS && event->key.code == MP_KEY_P) + { + printDebugStyle = true; + } + } break; + + default: + break; + } + } + + //TEST UI + ui_style defaultStyle = {.bgColor = {0}, + .color = {1, 1, 1, 1}, + .font = font, + .fontSize = 16, + .borderColor = {1, 0, 0, 1}, + .borderSize = 2}; + + ui_style_mask defaultMask = UI_STYLE_BG_COLOR + | UI_STYLE_COLOR + | UI_STYLE_BORDER_COLOR + | UI_STYLE_BORDER_SIZE + | UI_STYLE_FONT + | UI_STYLE_FONT_SIZE; + + ui_flags debugFlags = UI_FLAG_DRAW_BORDER; + + ui_box* root = 0; + + mp_rect frameRect = mg_surface_get_frame(surface); + vec2 frameSize = {frameRect.w, frameRect.h}; + + ui_frame(frameSize, &defaultStyle, defaultMask) + { + root = ui_box_top(); + ui_style_match_before(ui_pattern_all(), &defaultStyle, defaultMask); + + ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 1}, + .size.height = {UI_SIZE_PARENT, 1}, + .layout.axis = UI_AXIS_Y, + .layout.align.x = UI_ALIGN_CENTER, + .layout.align.y = UI_ALIGN_START, + .layout.spacing = 10, + .layout.margin.x = 10, + .layout.margin.y = 10, + .bgColor = {0.11, 0.11, 0.11, 1}}, + UI_STYLE_SIZE + | UI_STYLE_LAYOUT + | UI_STYLE_BG_COLOR); + + ui_container("background", UI_FLAG_DRAW_BACKGROUND) + { + ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 1}, + .size.height = {UI_SIZE_CHILDREN}, + .layout.align.x = UI_ALIGN_CENTER}, + UI_STYLE_SIZE + |UI_STYLE_LAYOUT_ALIGN_X); + ui_container("title", debugFlags) + { + ui_style_next(&(ui_style){.fontSize = 26}, UI_STYLE_FONT_SIZE); + ui_label("Milepost UI Demo"); + + if(ui_box_sig(ui_box_top()).hovering) + { + ui_tooltip("tooltip") + { + ui_style_next(&(ui_style){.bgColor = {1, 0.99, 0.82, 1}}, + UI_STYLE_BG_COLOR); + + ui_container("background", UI_FLAG_DRAW_BACKGROUND) + { + ui_style_next(&(ui_style){.color = {0, 0, 0, 1}}, + UI_STYLE_COLOR); + + ui_label("That is a tooltip!"); + } + } + } + } + + ui_menu_bar("Menu bar") + { + ui_menu("Menu 1") + { + if(ui_menu_button("Option 1.1").pressed) + { + printf("Pressed option 1.1\n"); + } + ui_menu_button("Option 1.2"); + ui_menu_button("Option 1.3"); + ui_menu_button("Option 1.4"); + } + + ui_menu("Menu 2") + { + ui_menu_button("Option 2.1"); + ui_menu_button("Option 2.2"); + ui_menu_button("Option 2.3"); + ui_menu_button("Option 2.4"); + } + + ui_menu("Menu 3") + { + ui_menu_button("Option 3.1"); + ui_menu_button("Option 3.2"); + ui_menu_button("Option 3.3"); + ui_menu_button("Option 3.4"); + } + } + + ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 1}, + .size.height = {UI_SIZE_PARENT, 1, 1}}, + UI_STYLE_SIZE); + + ui_style_next(&(ui_style){.layout.axis = UI_AXIS_X}, UI_STYLE_LAYOUT_AXIS); + ui_container("contents", debugFlags) + { + ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 0.5}, + .size.height = {UI_SIZE_PARENT, 1}, + .borderColor = {0, 0, 1, 1}}, + UI_STYLE_SIZE + |UI_STYLE_BORDER_COLOR); + + ui_container("left", debugFlags) + { + ui_style_next(&(ui_style){.layout.axis = UI_AXIS_X, + .layout.spacing = 10, + .layout.margin.x = 10, + .layout.margin.y = 10, + .size.width = {UI_SIZE_PARENT, 1}, + .size.height = {UI_SIZE_PARENT, 0.5}}, + UI_STYLE_LAYOUT_AXIS + |UI_STYLE_LAYOUT_SPACING + |UI_STYLE_LAYOUT_MARGIN_X + |UI_STYLE_LAYOUT_MARGIN_Y + |UI_STYLE_SIZE); + + ui_container("up", debugFlags) + { + ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 0.5}, + .size.height = {UI_SIZE_PARENT, 1}}, + UI_STYLE_SIZE); + widget_view("Buttons") + { + ui_button("Button 1"); + ui_button("Button 2"); + ui_button("Button 3"); + } + + ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 0.5}, + .size.height = {UI_SIZE_PARENT, 1}}, + UI_STYLE_SIZE); + + + ui_pattern pattern = {0}; + ui_pattern_push(mem_scratch(), &pattern, (ui_selector){.kind = UI_SEL_TAG, .tag = ui_tag_make("checkbox")}); + ui_style_match_after(pattern, + &(ui_style){.bgColor = {0, 1, 0, 1}, + .color = {1, 1, 1, 1}}, + UI_STYLE_COLOR | UI_STYLE_BG_COLOR); + + widget_view("checkboxes") + { + static bool check1 = true; + static bool check2 = false; + static bool check3 = false; + + ui_checkbox("check1", &check1); + ui_checkbox("check2", &check2); + ui_checkbox("check3", &check3); + } + } + + ui_style_next(&(ui_style){.layout.axis = UI_AXIS_X, + .size.width = {UI_SIZE_PARENT, 1}, + .size.height = {UI_SIZE_PARENT, 0.5}}, + UI_STYLE_LAYOUT_AXIS + |UI_STYLE_SIZE); + + ui_container("down", debugFlags) + { + widget_view("Vertical Sliders") + { + ui_style_next(&(ui_style){.layout.axis = UI_AXIS_X, + .layout.spacing = 10}, + UI_STYLE_LAYOUT_AXIS + |UI_STYLE_LAYOUT_SPACING); + ui_container("contents", 0) + { + ui_style_next(&(ui_style){.size.width = {UI_SIZE_PIXELS, 20}, + .size.height = {UI_SIZE_PIXELS, 200}}, + UI_STYLE_SIZE); + static f32 slider1 = 0; + ui_slider("slider1", 0.2, &slider1); + + ui_style_next(&(ui_style){.size.width = {UI_SIZE_PIXELS, 20}, + .size.height = {UI_SIZE_PIXELS, 200}}, + UI_STYLE_SIZE); + static f32 slider2 = 0; + ui_slider("slider2", 0.2, &slider2); + + ui_style_next(&(ui_style){.size.width = {UI_SIZE_PIXELS, 20}, + .size.height = {UI_SIZE_PIXELS, 200}}, + UI_STYLE_SIZE); + static f32 slider3 = 0; + ui_slider("slider3", 0.2, &slider3); + } + } + + widget_view("Horizontal Sliders") + { + ui_style_next(&(ui_style){.size.width = {UI_SIZE_PIXELS, 200}, + .size.height = {UI_SIZE_PIXELS, 20}}, + UI_STYLE_SIZE); + static f32 slider1 = 0; + ui_slider("slider1", 0.2, &slider1); + + ui_style_next(&(ui_style){.size.width = {UI_SIZE_PIXELS, 200}, + .size.height = {UI_SIZE_PIXELS, 20}}, + UI_STYLE_SIZE); + static f32 slider2 = 0; + ui_slider("slider2", 0.2, &slider2); + + ui_style_next(&(ui_style){.size.width = {UI_SIZE_PIXELS, 200}, + .size.height = {UI_SIZE_PIXELS, 20}}, + UI_STYLE_SIZE); + static f32 slider3 = 0; + ui_slider("slider3", 0.2, &slider3); + } + } + } + + ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 0.5}, + .size.height = {UI_SIZE_PARENT, 1}}, + UI_STYLE_SIZE); + + ui_container("right", debugFlags) + { + + ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 1}, + .size.height = {UI_SIZE_PARENT, 0.33}}, + UI_STYLE_SIZE); + widget_view("Text box") + { + ui_style_next(&(ui_style){.size.width = {UI_SIZE_PIXELS, 300}, + .size.height = {UI_SIZE_TEXT}}, + UI_STYLE_SIZE); + static str8 text = {0}; + ui_text_box_result res = ui_text_box("textbox", mem_scratch(), text); + if(res.changed) + { + mem_arena_clear(&textArena); + text = str8_push_copy(&textArena, res.text); + } + } + + ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 1}, + .size.height = {UI_SIZE_PARENT, 0.33}}, + UI_STYLE_SIZE); + widget_view("Test") + { + ui_pattern pattern = {0}; + ui_pattern_push(mem_scratch(), &pattern, (ui_selector){.kind = UI_SEL_TEXT, .text = STR8("panel")}); + ui_style_match_after(pattern, &(ui_style){.bgColor = {0.3, 0.3, 1, 1}}, UI_STYLE_BG_COLOR); + + static int selected = 0; + str8 options[] = {STR8("option 1"), + STR8("option 2"), + STR8("long option 3"), + STR8("option 4"), + STR8("option 5")}; + ui_select_popup_info info = {.selectedIndex = selected, + .optionCount = 5, + .options = options}; + + ui_select_popup_info result = ui_select_popup("popup", &info); + selected = result.selectedIndex; + } + + ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 1}, + .size.height = {UI_SIZE_PARENT, 0.33}}, + UI_STYLE_SIZE); + widget_view("Color") + { + ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 1}, + .size.height = {UI_SIZE_PARENT, 0.7}, + .layout.axis = UI_AXIS_X}, + UI_STYLE_SIZE + |UI_STYLE_LAYOUT_AXIS); + + ui_panel("Panel", UI_FLAG_DRAW_BORDER) + { + ui_style_next(&(ui_style){.layout.axis = UI_AXIS_X}, + UI_STYLE_LAYOUT_AXIS); + ui_container("contents", 0) + { + ui_style_next(&(ui_style){.layout.spacing = 20}, + UI_STYLE_LAYOUT_SPACING); + ui_container("buttons", 0) + { + ui_button("Button A"); + ui_button("Button B"); + ui_button("Button C"); + ui_button("Button D"); + } + + ui_style_next(&(ui_style){.layout.axis = UI_AXIS_X, + .layout.spacing = 20}, + UI_STYLE_LAYOUT_SPACING + |UI_STYLE_LAYOUT_AXIS); + + ui_container("buttons2", 0) + { + ui_button("Button A"); + ui_button("Button B"); + ui_button("Button C"); + ui_button("Button D"); + } + } + } + } + + } + } + } + } + if(printDebugStyle) + { + debug_print_styles(root, 0); + } + + mg_surface_prepare(surface); + + ui_draw(); + + mg_render(surface, canvas); + mg_surface_present(surface); + + mem_arena_clear(mem_scratch()); + } + + mg_surface_destroy(surface); + mp_terminate(); + + return(0); +}