[win32] re-introduced perf_text, tiger, and ui examples

This commit is contained in:
martinfouilleul 2023-05-17 15:14:14 +02:00
parent 2bec7a633a
commit 9f7cfd985c
4 changed files with 1189 additions and 1182 deletions

View File

@ -1,326 +1,327 @@
#include<stdio.h> #include<stdio.h>
#include<stdlib.h> #include<stdlib.h>
#define LOG_DEFAULT_LEVEL LOG_LEVEL_MESSAGE #define LOG_DEFAULT_LEVEL LOG_LEVEL_MESSAGE
#define LOG_COMPILE_DEBUG #define LOG_COMPILE_DEBUG
#include"milepost.h" #include"milepost.h"
#define LOG_SUBSYSTEM "Main" #define LOG_SUBSYSTEM "Main"
static const char* TEST_STRING = static const char* TEST_STRING =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla quam enim, aliquam in placerat luctus, rutrum in quam. " "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 " "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. " "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" "Etiam malesuada vulputate hendrerit. Cras ultricies diam in lectus finibus, eu laoreet diam rutrum.\n"
"\n" "\n"
"Etiam dictum orci arcu, ac fermentum leo dapibus lacinia. Integer vitae elementum ex. Vestibulum tempor nunc eu hendrerit " "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 " "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 " "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 " "quis, placerat nec enim. Nulla facilisi. Morbi vitae nibh ligula. Suspendisse in molestie magna, eget aliquet mauris. Sed "
"aliquam faucibus magna.\n" "aliquam faucibus magna.\n"
"\n" "\n"
"Sed metus odio, imperdiet et consequat non, faucibus nec risus. Suspendisse facilisis sem neque, id scelerisque dui mattis sit " "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, " "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. " "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. " "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, " "Vestibulum et orci ligula. Sed scelerisque nunc non nisi aliquam, vel eleifend felis suscipit. Integer posuere sapien elit, "
"lacinia ultricies nibh sodales nec.\n" "lacinia ultricies nibh sodales nec.\n"
"\n" "\n"
"Etiam aliquam purus sit amet purus ultricies tristique. Nunc maximus nunc quis magna ornare, vel interdum urna fermentum. " "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 " "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 " "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 " "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, " "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 " "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 " "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" "purus imperdiet nisi, non ultricies libero ipsum eu augue. Mauris at luctus enim.\n"
"\n" "\n"
"Aliquam sed tortor a justo pulvinar dictum consectetur eu felis. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices " "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. " "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. " "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 " "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. " "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" "Etiam aliquam lacinia consectetur. Phasellus nulla ipsum, viverra non nulla in, rhoncus posuere nunc.\n"
"\n" "\n"
"Phasellus efficitur commodo tellus, eget lobortis erat porta quis. Aenean condimentum tortor ut neque dapibus, vitae vulputate quam " "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. " "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 " "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 " "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 " "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" "semper magna mollis luctus. Donec eu dolor ut ante ullamcorper porta. Mauris et est tristique libero pharetra faucibus.\n"
"\n" "\n"
"Duis ut elementum sem. Praesent commodo erat nec sem ultricies sollicitudin. Suspendisse a pellentesque sapien. Nunc ac magna a dui " "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 " "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 " "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 " "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 " "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 " "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."; "sit amet, malesuada enim. Mauris ultricies nibh orci.";
mg_font create_font(const char* path) mg_font create_font(const char* path)
{ {
//NOTE(martin): create font //NOTE(martin): create font
str8 fontPath = mp_app_get_resource_path(mem_scratch(), path); str8 fontPath = mp_app_get_resource_path(mem_scratch(), path);
char* fontPathCString = str8_to_cstring(mem_scratch(), fontPath); char* fontPathCString = str8_to_cstring(mem_scratch(), fontPath);
FILE* fontFile = fopen(fontPathCString, "r"); FILE* fontFile = fopen(fontPathCString, "r");
if(!fontFile) if(!fontFile)
{ {
log_error("Could not load font file '%s'\n", fontPathCString); log_error("Could not load font file '%s'\n", fontPathCString);
return(mg_font_nil()); return(mg_font_nil());
} }
unsigned char* fontData = 0; unsigned char* fontData = 0;
fseek(fontFile, 0, SEEK_END); fseek(fontFile, 0, SEEK_END);
u32 fontDataSize = ftell(fontFile); u32 fontDataSize = ftell(fontFile);
rewind(fontFile); rewind(fontFile);
fontData = (unsigned char*)malloc(fontDataSize); fontData = (unsigned char*)malloc(fontDataSize);
fread(fontData, 1, fontDataSize, fontFile); fread(fontData, 1, fontDataSize, fontFile);
fclose(fontFile); fclose(fontFile);
unicode_range ranges[5] = {UNICODE_RANGE_BASIC_LATIN, unicode_range ranges[5] = {UNICODE_RANGE_BASIC_LATIN,
UNICODE_RANGE_C1_CONTROLS_AND_LATIN_1_SUPPLEMENT, UNICODE_RANGE_C1_CONTROLS_AND_LATIN_1_SUPPLEMENT,
UNICODE_RANGE_LATIN_EXTENDED_A, UNICODE_RANGE_LATIN_EXTENDED_A,
UNICODE_RANGE_LATIN_EXTENDED_B, UNICODE_RANGE_LATIN_EXTENDED_B,
UNICODE_RANGE_SPECIALS}; UNICODE_RANGE_SPECIALS};
mg_font font = mg_font_create_from_memory(fontDataSize, fontData, 5, ranges); mg_font font = mg_font_create_from_memory(fontDataSize, fontData, 5, ranges);
free(fontData); free(fontData);
return(font); return(font);
} }
int main() enum { FONT_COUNT = 3 };
{
mp_init(); int main()
mp_clock_init(); {
mp_init();
mp_rect rect = {.x = 100, .y = 100, .w = 980, .h = 600}; mp_clock_init();
mp_window window = mp_window_create(rect, "test", 0);
mp_rect rect = {.x = 100, .y = 100, .w = 980, .h = 600};
mp_rect contentRect = mp_window_get_content_rect(window); mp_window window = mp_window_create(rect, "test", 0);
//NOTE: create surface, canvas and font mp_rect contentRect = mp_window_get_content_rect(window);
mg_surface surface = mg_surface_create_for_window(window, MG_CANVAS); //NOTE: create surface, canvas and font
mg_surface_swap_interval(surface, 0);
mg_surface surface = mg_surface_create_for_window(window, MG_CANVAS);
mg_canvas canvas = mg_canvas_create(); mg_surface_swap_interval(surface, 0);
const int fontCount = 3; mg_canvas canvas = mg_canvas_create();
int fontIndex = 0;
mg_font fonts[fontCount] = {create_font("../resources/OpenSansLatinSubset.ttf"), int fontIndex = 0;
create_font("../resources/CMUSerif-Roman.ttf"), mg_font fonts[FONT_COUNT] = {create_font("../resources/OpenSansLatinSubset.ttf"),
create_font("../resources/courier.ttf")}; create_font("../resources/CMUSerif-Roman.ttf"),
create_font("../resources/courier.ttf")};
mg_font_extents extents[fontCount];
f32 fontScales[fontCount]; mg_font_extents extents[FONT_COUNT];
f32 lineHeights[fontCount]; f32 fontScales[FONT_COUNT];
f32 lineHeights[FONT_COUNT];
for(int i=0; i<fontCount; i++)
{ for(int i=0; i<FONT_COUNT; i++)
extents[i] = mg_font_get_extents(fonts[i]); {
fontScales[i] = mg_font_get_scale_for_em_pixels(fonts[i], 14); extents[i] = mg_font_get_extents(fonts[i]);
lineHeights[i] = fontScales[i]*(extents[i].ascent + extents[i].descent + extents[i].leading); fontScales[i] = mg_font_get_scale_for_em_pixels(fonts[i], 14);
} lineHeights[i] = fontScales[i]*(extents[i].ascent + extents[i].descent + extents[i].leading);
}
int codePointCount = utf8_codepoint_count_for_string(STR8((char*)TEST_STRING));
u32* codePoints = malloc_array(utf32, codePointCount); int codePointCount = utf8_codepoint_count_for_string(STR8((char*)TEST_STRING));
utf8_to_codepoints(codePointCount, codePoints, STR8((char*)TEST_STRING)); u32* codePoints = malloc_array(utf32, codePointCount);
utf8_to_codepoints(codePointCount, codePoints, STR8((char*)TEST_STRING));
u32 glyphCount = 0;
for(int i=0; i<codePointCount; i++) u32 glyphCount = 0;
{ for(int i=0; i<codePointCount; i++)
if(codePoints[i] != ' ' && codePoints[i] != '\n') {
{ if(codePoints[i] != ' ' && codePoints[i] != '\n')
glyphCount++; {
} glyphCount++;
} }
}
// start app
mp_window_bring_to_front(window); // start app
mp_window_focus(window); mp_window_bring_to_front(window);
mp_window_focus(window);
f64 frameTime = 0;
f64 frameTime = 0;
bool tracked = false;
vec2 trackPoint = {0}; bool tracked = false;
f32 zoom = 1; vec2 trackPoint = {0};
f32 zoom = 1;
f32 startX = 10;
f32 startY = 10 + lineHeights[fontIndex]; f32 startX = 10;
f32 startY = 10 + lineHeights[fontIndex];
mp_input_state inputState = {0};
mp_input_state inputState = {0};
while(!mp_should_quit())
{ while(!mp_should_quit())
f64 startFrameTime = mp_get_time(MP_CLOCK_MONOTONIC); {
f64 startFrameTime = mp_get_time(MP_CLOCK_MONOTONIC);
mp_pump_events(0);
mp_event* event = 0; mp_pump_events(0);
while((event = mp_next_event(mem_scratch())) != 0) mp_event* event = 0;
{ while((event = mp_next_event(mem_scratch())) != 0)
mp_input_process_event(&inputState, event); {
mp_input_process_event(&inputState, event);
switch(event->type)
{ switch(event->type)
case MP_EVENT_WINDOW_CLOSE: {
{ case MP_EVENT_WINDOW_CLOSE:
mp_request_quit(); {
} break; mp_request_quit();
} break;
case MP_EVENT_MOUSE_BUTTON:
{ case MP_EVENT_MOUSE_BUTTON:
if(event->key.code == MP_MOUSE_LEFT) {
{ if(event->key.code == MP_MOUSE_LEFT)
if(event->key.action == MP_KEY_PRESS) {
{ if(event->key.action == MP_KEY_PRESS)
tracked = true; {
vec2 mousePos = mp_mouse_position(&inputState); tracked = true;
trackPoint.x = mousePos.x/zoom - startX; vec2 mousePos = mp_mouse_position(&inputState);
trackPoint.y = mousePos.y/zoom - startY; trackPoint.x = mousePos.x/zoom - startX;
} trackPoint.y = mousePos.y/zoom - startY;
else }
{ else
tracked = false; {
} tracked = false;
} }
} break; }
} break;
case MP_EVENT_MOUSE_WHEEL:
{ case MP_EVENT_MOUSE_WHEEL:
vec2 mousePos = mp_mouse_position(&inputState); {
f32 trackX = mousePos.x/zoom - startX; vec2 mousePos = mp_mouse_position(&inputState);
f32 trackY = mousePos.y/zoom - startY; 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); zoom *= 1 + event->move.deltaY * 0.01;
zoom = Clamp(zoom, 0.2, 10);
startX = mousePos.x/zoom - trackX;
startY = mousePos.y/zoom - trackY; startX = mousePos.x/zoom - trackX;
} break; startY = mousePos.y/zoom - trackY;
} break;
case MP_EVENT_KEYBOARD_KEY:
{ case MP_EVENT_KEYBOARD_KEY:
if(event->key.code == MP_KEY_SPACE && event->key.action == MP_KEY_PRESS) {
{ if(event->key.code == MP_KEY_SPACE && event->key.action == MP_KEY_PRESS)
fontIndex = (fontIndex+1)%fontCount; {
} fontIndex = (fontIndex+1)%FONT_COUNT;
} break; }
} break;
default:
break; default:
} break;
} }
}
if(tracked)
{ if(tracked)
vec2 mousePos = mp_mouse_position(&inputState); {
startX = mousePos.x/zoom - trackPoint.x; vec2 mousePos = mp_mouse_position(&inputState);
startY = mousePos.y/zoom - trackPoint.y; startX = mousePos.x/zoom - trackPoint.x;
} startY = mousePos.y/zoom - trackPoint.y;
}
f32 textX = startX;
f32 textY = startY; f32 textX = startX;
f32 textY = startY;
/*
mg_set_color_rgba(1, 1, 1, 1); /*
mg_clear(); mg_set_color_rgba(1, 1, 1, 1);
mg_set_color_rgba(1, 0, 0, 1); mg_clear();
for(int i=0; i<1000; i++) mg_set_color_rgba(1, 0, 0, 1);
{ for(int i=0; i<1000; i++)
mg_rectangle_fill(0, 0, 100, 100); {
} mg_rectangle_fill(0, 0, 100, 100);
*/ }
*/
mg_matrix_push((mg_mat2x3){zoom, 0, 0,
0, zoom, 0}); mg_matrix_push((mg_mat2x3){zoom, 0, 0,
0, zoom, 0});
mg_set_color_rgba(1, 1, 1, 1);
mg_clear(); mg_set_color_rgba(1, 1, 1, 1);
mg_clear();
mg_set_font(fonts[fontIndex]);
mg_set_font_size(14); mg_set_font(fonts[fontIndex]);
mg_set_color_rgba(0, 0, 0, 1); mg_set_font_size(14);
mg_set_color_rgba(0, 0, 0, 1);
mg_move_to(textX, textY);
mg_move_to(textX, textY);
int startIndex = 0;
while(startIndex < codePointCount) int startIndex = 0;
{ while(startIndex < codePointCount)
bool lineBreak = false; {
int subIndex = 0; bool lineBreak = false;
for(; (startIndex+subIndex) < codePointCount && subIndex < 120; subIndex++) int subIndex = 0;
{ for(; (startIndex+subIndex) < codePointCount && subIndex < 120; subIndex++)
if(codePoints[startIndex + subIndex] == '\n') {
{ if(codePoints[startIndex + subIndex] == '\n')
break; {
} break;
} }
}
u32 glyphs[512];
mg_font_get_glyph_indices(fonts[fontIndex], (str32){subIndex, codePoints+startIndex}, (str32){512, glyphs}); 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(); mg_glyph_outlines((str32){subIndex, glyphs});
mg_fill();
textY += lineHeights[fontIndex];
mg_move_to(textX, textY); textY += lineHeights[fontIndex];
startIndex++; mg_move_to(textX, textY);
startIndex++;
startIndex += subIndex;
} startIndex += subIndex;
}
mg_matrix_pop();
mg_matrix_pop();
mg_set_color_rgba(0, 0, 1, 1);
mg_set_font(fonts[fontIndex]); mg_set_color_rgba(0, 0, 1, 1);
mg_set_font_size(14); mg_set_font(fonts[fontIndex]);
mg_move_to(10, contentRect.h - 10 - lineHeights[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", str8 text = str8_pushf(mem_scratch(),
glyphCount, "Test program: %i glyphs, frame time = %fs, fps = %f",
frameTime, glyphCount,
1./frameTime); frameTime,
mg_text_outlines(text); 1./frameTime);
mg_fill(); mg_text_outlines(text);
mg_fill();
f64 startFlushTime = mp_get_time(MP_CLOCK_MONOTONIC);
f64 startFlushTime = mp_get_time(MP_CLOCK_MONOTONIC);
mg_surface_prepare(surface);
mg_render(surface, canvas); mg_surface_prepare(surface);
mg_render(surface, canvas);
f64 startPresentTime = mp_get_time(MP_CLOCK_MONOTONIC);
mg_surface_present(surface); f64 startPresentTime = mp_get_time(MP_CLOCK_MONOTONIC);
mg_surface_present(surface);
f64 endFrameTime = mp_get_time(MP_CLOCK_MONOTONIC);
f64 endFrameTime = mp_get_time(MP_CLOCK_MONOTONIC);
frameTime = (endFrameTime - startFrameTime);
frameTime = (endFrameTime - startFrameTime);
printf("frame time: %.2fms (%.2fFPS), draw = %f.2ms, flush = %.2fms, present = %.2fms\n",
frameTime*1000, printf("frame time: %.2fms (%.2fFPS), draw = %f.2ms, flush = %.2fms, present = %.2fms\n",
1./frameTime, frameTime*1000,
(startFlushTime - startFrameTime)*1000, 1./frameTime,
(startPresentTime - startFlushTime)*1000, (startFlushTime - startFrameTime)*1000,
(endFrameTime - startPresentTime)*1000); (startPresentTime - startFlushTime)*1000,
(endFrameTime - startPresentTime)*1000);
mp_input_next_frame(&inputState);
mem_arena_clear(mem_scratch()); mp_input_next_frame(&inputState);
} mem_arena_clear(mem_scratch());
}
for(int i=0; i<fontCount; i++)
{ for(int i=0; i<FONT_COUNT; i++)
mg_font_destroy(fonts[i]); {
} mg_font_destroy(fonts[i]);
mg_canvas_destroy(canvas); }
mg_surface_destroy(surface); mg_canvas_destroy(canvas);
mp_window_destroy(window); mg_surface_destroy(surface);
mp_terminate(); mp_window_destroy(window);
mp_terminate();
return(0);
} return(0);
}

View File

@ -1,4 +1,4 @@
set INCLUDES=/I ..\..\src /I ..\..\src\util /I ..\..\src\platform /I ../../ext /I ../../ext/angle_headers set INCLUDES=/I ..\..\src /I ..\..\src\util /I ..\..\src\platform /I ../../ext /I ../../ext/angle_headers
cl /we4013 /Zi /Zc:preprocessor /std:c11 %INCLUDES% main.c /link /LIBPATH:../../bin milepost.dll.lib /out:../../bin/example_canvas.exe cl /we4013 /Zi /Zc:preprocessor /std:c11 %INCLUDES% main.c /link /LIBPATH:../../bin milepost.dll.lib /out:../../bin/example_tiger.exe

View File

@ -1,247 +1,248 @@
/************************************************************//** /************************************************************//**
* *
* @file: main.cpp * @file: main.cpp
* @author: Martin Fouilleul * @author: Martin Fouilleul
* @date: 30/07/2022 * @date: 30/07/2022
* @revision: * @revision:
* *
*****************************************************************/ *****************************************************************/
#include<stdlib.h> #include<stdlib.h>
#include<string.h> #include<string.h>
#include<errno.h> #include<stdio.h>
#include<errno.h>
#define _USE_MATH_DEFINES //NOTE: necessary for MSVC
#include<math.h> #define _USE_MATH_DEFINES //NOTE: necessary for MSVC
#include<math.h>
#include"milepost.h"
#include"milepost.h"
#include"tiger.c"
#include"tiger.c"
mg_font create_font()
{ mg_font create_font()
//NOTE(martin): create font {
str8 fontPath = mp_app_get_resource_path(mem_scratch(), "../resources/OpenSansLatinSubset.ttf"); //NOTE(martin): create font
char* fontPathCString = str8_to_cstring(mem_scratch(), fontPath); 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) FILE* fontFile = fopen(fontPathCString, "r");
{ if(!fontFile)
log_error("Could not load font file '%s': %s\n", fontPathCString, strerror(errno)); {
return(mg_font_nil()); 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); unsigned char* fontData = 0;
u32 fontDataSize = ftell(fontFile); fseek(fontFile, 0, SEEK_END);
rewind(fontFile); u32 fontDataSize = ftell(fontFile);
fontData = (unsigned char*)malloc(fontDataSize); rewind(fontFile);
fread(fontData, 1, fontDataSize, fontFile); fontData = (unsigned char*)malloc(fontDataSize);
fclose(fontFile); 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 ranges[5] = {UNICODE_RANGE_BASIC_LATIN,
UNICODE_RANGE_LATIN_EXTENDED_A, UNICODE_RANGE_C1_CONTROLS_AND_LATIN_1_SUPPLEMENT,
UNICODE_RANGE_LATIN_EXTENDED_B, UNICODE_RANGE_LATIN_EXTENDED_A,
UNICODE_RANGE_SPECIALS}; UNICODE_RANGE_LATIN_EXTENDED_B,
UNICODE_RANGE_SPECIALS};
mg_font font = mg_font_create_from_memory(fontDataSize, fontData, 5, ranges);
free(fontData); mg_font font = mg_font_create_from_memory(fontDataSize, fontData, 5, ranges);
free(fontData);
return(font);
} return(font);
}
int main()
{ int main()
mp_init(); {
mp_clock_init(); //TODO put that in mp_init()? 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 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);
mp_rect contentRect = mp_window_get_content_rect(window);
//NOTE: create surface
mg_surface surface = mg_surface_create_for_window(window, MG_CANVAS); //NOTE: create surface
mg_surface_swap_interval(surface, 0); 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(); //TODO: create canvas
mg_canvas canvas = mg_canvas_create();
if(mg_canvas_is_nil(canvas))
{ if(mg_canvas_is_nil(canvas))
printf("Error: couldn't create canvas\n"); {
return(-1); printf("Error: couldn't create canvas\n");
} return(-1);
}
mg_font font = create_font();
mg_font font = create_font();
// start app
mp_window_bring_to_front(window); // start app
mp_window_focus(window); mp_window_bring_to_front(window);
mp_window_focus(window);
bool tracked = false;
vec2 trackPoint = {0}; bool tracked = false;
vec2 trackPoint = {0};
f32 zoom = 1;
f32 startX = 300, startY = 200; f32 zoom = 1;
bool singlePath = false; f32 startX = 300, startY = 200;
int singlePathIndex = 0; bool singlePath = false;
int singlePathIndex = 0;
f64 frameTime = 0;
f64 frameTime = 0;
mp_input_state inputState = {0};
mp_input_state inputState = {0};
while(!mp_should_quit())
{ while(!mp_should_quit())
f64 startTime = mp_get_time(MP_CLOCK_MONOTONIC); {
f64 startTime = mp_get_time(MP_CLOCK_MONOTONIC);
mp_pump_events(0);
mp_event* event = 0; mp_pump_events(0);
while((event = mp_next_event(mem_scratch())) != 0) mp_event* event = 0;
{ while((event = mp_next_event(mem_scratch())) != 0)
mp_input_process_event(&inputState, event); {
mp_input_process_event(&inputState, event);
switch(event->type)
{ switch(event->type)
case MP_EVENT_WINDOW_CLOSE: {
{ case MP_EVENT_WINDOW_CLOSE:
mp_request_quit(); {
} break; mp_request_quit();
} break;
case MP_EVENT_WINDOW_RESIZE:
{ case MP_EVENT_WINDOW_RESIZE:
mp_rect frame = {0, 0, event->frame.rect.w, event->frame.rect.h}; {
mg_surface_set_frame(surface, frame); mp_rect frame = {0, 0, event->frame.rect.w, event->frame.rect.h};
} break; mg_surface_set_frame(surface, frame);
} break;
case MP_EVENT_MOUSE_BUTTON:
{ case MP_EVENT_MOUSE_BUTTON:
if(event->key.code == MP_MOUSE_LEFT) {
{ if(event->key.code == MP_MOUSE_LEFT)
if(event->key.action == MP_KEY_PRESS) {
{ if(event->key.action == MP_KEY_PRESS)
tracked = true; {
vec2 mousePos = mp_mouse_position(&inputState); tracked = true;
trackPoint.x = (mousePos.x - startX)/zoom; vec2 mousePos = mp_mouse_position(&inputState);
trackPoint.y = (mousePos.y - startY)/zoom; trackPoint.x = (mousePos.x - startX)/zoom;
} trackPoint.y = (mousePos.y - startY)/zoom;
else }
{ else
tracked = false; {
} tracked = false;
} }
} break; }
} break;
case MP_EVENT_MOUSE_WHEEL:
{ case MP_EVENT_MOUSE_WHEEL:
vec2 mousePos = mp_mouse_position(&inputState); {
f32 pinX = (mousePos.x - startX)/zoom; vec2 mousePos = mp_mouse_position(&inputState);
f32 pinY = (mousePos.y - startY)/zoom; 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); zoom *= 1 + event->move.deltaY * 0.01;
zoom = Clamp(zoom, 0.5, 5);
startX = mousePos.x - pinX*zoom;
startY = mousePos.y - pinY*zoom; startX = mousePos.x - pinX*zoom;
} break; startY = mousePos.y - pinY*zoom;
} break;
case MP_EVENT_KEYBOARD_KEY:
{ case MP_EVENT_KEYBOARD_KEY:
if(event->key.action == MP_KEY_PRESS || event->key.action == MP_KEY_REPEAT) {
{ if(event->key.action == MP_KEY_PRESS || event->key.action == MP_KEY_REPEAT)
switch(event->key.code) {
{ switch(event->key.code)
case MP_KEY_SPACE: {
singlePath = !singlePath; case MP_KEY_SPACE:
break; singlePath = !singlePath;
break;
case MP_KEY_UP:
{ case MP_KEY_UP:
if(event->key.mods & MP_KEYMOD_SHIFT) {
{ if(event->key.mods & MP_KEYMOD_SHIFT)
singlePathIndex++; {
} singlePathIndex++;
else }
{ else
zoom += 0.001; {
} zoom += 0.001;
} break; }
} break;
case MP_KEY_DOWN:
{ case MP_KEY_DOWN:
if(event->key.mods & MP_KEYMOD_SHIFT) {
{ if(event->key.mods & MP_KEYMOD_SHIFT)
singlePathIndex--; {
} singlePathIndex--;
else }
{ else
zoom -= 0.001; {
} zoom -= 0.001;
} break; }
} } break;
} }
} break; }
} break;
default:
break; default:
} break;
} }
}
if(tracked)
{ if(tracked)
vec2 mousePos = mp_mouse_position(&inputState); {
startX = mousePos.x - trackPoint.x*zoom; vec2 mousePos = mp_mouse_position(&inputState);
startY = mousePos.y - trackPoint.y*zoom; startX = mousePos.x - trackPoint.x*zoom;
} startY = mousePos.y - trackPoint.y*zoom;
}
mg_surface_prepare(surface);
mg_surface_prepare(surface);
mg_set_color_rgba(1, 0, 1, 1);
mg_clear(); mg_set_color_rgba(1, 0, 1, 1);
mg_clear();
mg_matrix_push((mg_mat2x3){zoom, 0, startX,
0, zoom, startY}); mg_matrix_push((mg_mat2x3){zoom, 0, startX,
0, zoom, startY});
draw_tiger(singlePath, singlePathIndex);
draw_tiger(singlePath, singlePathIndex);
if(singlePath)
{ if(singlePath)
printf("display single path %i\n", singlePathIndex); {
printf("viewpos = (%f, %f), zoom = %f\n", startX, startY, zoom); printf("display single path %i\n", singlePathIndex);
} printf("viewpos = (%f, %f), zoom = %f\n", startX, startY, zoom);
}
mg_matrix_pop();
mg_matrix_pop();
// text
mg_set_color_rgba(0, 0, 1, 1); // text
mg_set_font(font); mg_set_color_rgba(0, 0, 1, 1);
mg_set_font_size(12); mg_set_font(font);
mg_move_to(50, 600-50); 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)...", str8 text = str8_pushf(mem_scratch(),
frameTime, "Milepost vector graphics test program (frame time = %fs, fps = %f)...",
1./frameTime); frameTime,
mg_text_outlines(text); 1./frameTime);
mg_fill(); mg_text_outlines(text);
mg_fill();
printf("Milepost vector graphics test program (frame time = %fs, fps = %f)...\n",
frameTime, printf("Milepost vector graphics test program (frame time = %fs, fps = %f)...\n",
1./frameTime); frameTime,
1./frameTime);
mg_render(surface, canvas);
mg_surface_present(surface); mg_render(surface, canvas);
mg_surface_present(surface);
mp_input_next_frame(&inputState);
mem_arena_clear(mem_scratch()); mp_input_next_frame(&inputState);
frameTime = mp_get_time(MP_CLOCK_MONOTONIC) - startTime; mem_arena_clear(mem_scratch());
} frameTime = mp_get_time(MP_CLOCK_MONOTONIC) - startTime;
}
mg_font_destroy(font);
mg_canvas_destroy(canvas); mg_font_destroy(font);
mg_surface_destroy(surface); mg_canvas_destroy(canvas);
mp_window_destroy(window); mg_surface_destroy(surface);
mp_window_destroy(window);
mp_terminate();
mp_terminate();
return(0);
} return(0);
}

File diff suppressed because it is too large Load Diff