Merge branch 'win32' into orca

This commit is contained in:
Martin Fouilleul 2023-05-24 15:11:32 +02:00
commit 43c23627e2
20 changed files with 10099 additions and 8710 deletions

View File

@ -1,11 +1,11 @@
if not exist bin mkdir bin
set glsl_shaders=src\glsl_shaders\common.glsl src\glsl_shaders\blit_vertex.glsl src\glsl_shaders\blit_fragment.glsl src\glsl_shaders\clear_counters.glsl src\glsl_shaders\tile.glsl src\glsl_shaders\sort.glsl src\glsl_shaders\draw.glsl
call python3 scripts\embed_text.py %glsl_shaders% --prefix=glsl_ --output src\glsl_shaders.h
set INCLUDES=/I src /I src/util /I src/platform /I ext /I ext/angle_headers
set LIBS=user32.lib opengl32.lib gdi32.lib shcore.lib delayimp.lib dwmapi.lib /LIBPATH:./bin libEGL.dll.lib libGLESv2.dll.lib /DELAYLOAD:libEGL.dll /DELAYLOAD:libGLESv2.dll
cl /we4013 /Zi /Zc:preprocessor /DMP_BUILD_DLL /std:c11 %INCLUDES% src/milepost.c /Fo:bin/milepost.o /LD /link %LIBS% /OUT:bin/milepost.dll /IMPLIB:bin/milepost.dll.lib
if not exist bin mkdir bin
set glsl_shaders=src\glsl_shaders\common.glsl src\glsl_shaders\blit_vertex.glsl src\glsl_shaders\blit_fragment.glsl src\glsl_shaders\clear_counters.glsl src\glsl_shaders\tile.glsl src\glsl_shaders\sort.glsl src\glsl_shaders\draw.glsl
call python3 scripts\embed_text.py %glsl_shaders% --prefix=glsl_ --output src\glsl_shaders.h
set INCLUDES=/I src /I src/util /I src/platform /I ext /I ext/angle_headers
set LIBS=user32.lib opengl32.lib gdi32.lib shcore.lib delayimp.lib dwmapi.lib comctl32.lib ole32.lib shell32.lib /LIBPATH:./bin libEGL.dll.lib libGLESv2.dll.lib /DELAYLOAD:libEGL.dll /DELAYLOAD:libGLESv2.dll
cl /we4013 /Zi /Zc:preprocessor /DMP_BUILD_DLL /std:c11 %INCLUDES% src/milepost.c /Fo:bin/milepost.o /LD /link /MANIFEST:EMBED /MANIFESTINPUT:src/win32_manifest.xml %LIBS% /OUT:bin/milepost.dll /IMPLIB:bin/milepost.dll.lib

View File

@ -1,209 +1,210 @@
/************************************************************//**
*
* @file: main.cpp
* @author: Martin Fouilleul
* @date: 30/07/2022
* @revision:
*
*****************************************************************/
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#define _USE_MATH_DEFINES //NOTE: necessary for MSVC
#include<math.h>
#include"milepost.h"
#define LOG_SUBSYSTEM "Main"
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);
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);
f32 x = 400, y = 300;
f32 speed = 0;
f32 dx = speed, dy = speed;
f64 frameTime = 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)
{
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.action == MP_KEY_REPEAT)
{
f32 factor = (event->key.mods & MP_KEYMOD_SHIFT) ? 10 : 1;
if(event->key.code == MP_KEY_LEFT)
{
x-=0.3*factor;
}
else if(event->key.code == MP_KEY_RIGHT)
{
x+=0.3*factor;
}
else if(event->key.code == MP_KEY_UP)
{
y-=0.3*factor;
}
else if(event->key.code == MP_KEY_DOWN)
{
y+=0.3*factor;
}
}
} break;
default:
break;
}
}
if(x-200 < 0)
{
x = 200;
dx = speed;
}
if(x+200 > contentRect.w)
{
x = contentRect.w - 200;
dx = -speed;
}
if(y-200 < 0)
{
y = 200;
dy = speed;
}
if(y+200 > contentRect.h)
{
y = contentRect.h - 200;
dy = -speed;
}
x += dx;
y += dy;
// background
mg_set_color_rgba(0, 1, 1, 1);
mg_clear();
// head
mg_set_color_rgba(1, 1, 0, 1);
mg_circle_fill(x, y, 200);
// smile
f32 frown = frameTime > 0.033 ? -100 : 0;
mg_set_color_rgba(0, 0, 0, 1);
mg_set_width(20);
mg_move_to(x-100, y+100);
mg_cubic_to(x-50, y+150+frown, x+50, y+150+frown, x+100, y+100);
mg_stroke();
// eyes
mg_ellipse_fill(x-70, y-50, 30, 50);
mg_ellipse_fill(x+70, y-50, 30, 50);
// 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_surface_prepare(surface);
mg_render(surface, canvas);
mg_surface_present(surface);
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<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<errno.h>
#define _USE_MATH_DEFINES //NOTE: necessary for MSVC
#include<math.h>
#include"milepost.h"
#define LOG_SUBSYSTEM "Main"
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);
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);
f32 x = 400, y = 300;
f32 speed = 0;
f32 dx = speed, dy = speed;
f64 frameTime = 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)
{
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.action == MP_KEY_REPEAT)
{
f32 factor = (event->key.mods & MP_KEYMOD_SHIFT) ? 10 : 1;
if(event->key.code == MP_KEY_LEFT)
{
x-=0.3*factor;
}
else if(event->key.code == MP_KEY_RIGHT)
{
x+=0.3*factor;
}
else if(event->key.code == MP_KEY_UP)
{
y-=0.3*factor;
}
else if(event->key.code == MP_KEY_DOWN)
{
y+=0.3*factor;
}
}
} break;
default:
break;
}
}
if(x-200 < 0)
{
x = 200;
dx = speed;
}
if(x+200 > contentRect.w)
{
x = contentRect.w - 200;
dx = -speed;
}
if(y-200 < 0)
{
y = 200;
dy = speed;
}
if(y+200 > contentRect.h)
{
y = contentRect.h - 200;
dy = -speed;
}
x += dx;
y += dy;
// background
mg_set_color_rgba(0, 1, 1, 1);
mg_clear();
// head
mg_set_color_rgba(1, 1, 0, 1);
mg_circle_fill(x, y, 200);
// smile
f32 frown = frameTime > 0.033 ? -100 : 0;
mg_set_color_rgba(0, 0, 0, 1);
mg_set_width(20);
mg_move_to(x-100, y+100);
mg_cubic_to(x-50, y+150+frown, x+50, y+150+frown, x+100, y+100);
mg_stroke();
// eyes
mg_ellipse_fill(x-70, y-50, 30, 50);
mg_ellipse_fill(x+70, y-50, 30, 50);
// 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_surface_prepare(surface);
mg_render(surface, canvas);
mg_surface_present(surface);
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);
}

View File

@ -1,326 +1,327 @@
#include<stdio.h>
#include<stdlib.h>
#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; i<fontCount; i++)
{
extents[i] = mg_font_get_extents(fonts[i]);
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);
utf8_to_codepoints(codePointCount, codePoints, STR8((char*)TEST_STRING));
u32 glyphCount = 0;
for(int i=0; i<codePointCount; i++)
{
if(codePoints[i] != ' ' && codePoints[i] != '\n')
{
glyphCount++;
}
}
// start app
mp_window_bring_to_front(window);
mp_window_focus(window);
f64 frameTime = 0;
bool tracked = false;
vec2 trackPoint = {0};
f32 zoom = 1;
f32 startX = 10;
f32 startY = 10 + lineHeights[fontIndex];
mp_input_state inputState = {0};
while(!mp_should_quit())
{
f64 startFrameTime = 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_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<fontCount; i++)
{
mg_font_destroy(fonts[i]);
}
mg_canvas_destroy(canvas);
mg_surface_destroy(surface);
mp_window_destroy(window);
mp_terminate();
return(0);
}
#include<stdio.h>
#include<stdlib.h>
#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; i<FONT_COUNT; i++)
{
extents[i] = mg_font_get_extents(fonts[i]);
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);
utf8_to_codepoints(codePointCount, codePoints, STR8((char*)TEST_STRING));
u32 glyphCount = 0;
for(int i=0; i<codePointCount; i++)
{
if(codePoints[i] != ' ' && codePoints[i] != '\n')
{
glyphCount++;
}
}
// start app
mp_window_bring_to_front(window);
mp_window_focus(window);
f64 frameTime = 0;
bool tracked = false;
vec2 trackPoint = {0};
f32 zoom = 1;
f32 startX = 10;
f32 startY = 10 + lineHeights[fontIndex];
mp_input_state inputState = {0};
while(!mp_should_quit())
{
f64 startFrameTime = 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_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<FONT_COUNT; i++)
{
mg_font_destroy(fonts[i]);
}
mg_canvas_destroy(canvas);
mg_surface_destroy(surface);
mp_window_destroy(window);
mp_terminate();
return(0);
}

View File

@ -1,2 +1,2 @@
set INCLUDES=/I ..\..\src /I ..\..\src\util /I ..\..\src\platform /I ../../ext
cl /we4013 /Zi /Zc:preprocessor /std:c11 %INCLUDES% main.c /link /LIBPATH:../../bin milepost.lib user32.lib opengl32.lib gdi32.lib /out:test.exe
set INCLUDES=/I ..\..\src /I ..\..\src\util /I ..\..\src\platform /I ../../ext
cl /we4013 /Zi /Zc:preprocessor /std:c11 %INCLUDES% main.c /link /LIBPATH:../../bin milepost.dll.lib user32.lib opengl32.lib gdi32.lib /out:../../bin/example_window.exe

View File

@ -1,113 +1,111 @@
/************************************************************//**
*
* @file: main.cpp
* @author: Martin Fouilleul
* @date: 30/07/2022
* @revision:
*
*****************************************************************/
#include<stdlib.h>
#include<string.h>
#include"milepost.h"
#define LOG_SUBSYSTEM "Main"
int main()
{
LogLevel(LOG_LEVEL_DEBUG);
mp_init();
mp_rect rect = {.x = 100, .y = 100, .w = 800, .h = 600};
mp_window window = mp_window_create(rect, "test", 0);
mp_window_bring_to_front(window);
mp_window_focus(window);
while(!mp_should_quit())
{
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_WINDOW_RESIZE:
{
printf("resized, rect = {%f, %f, %f, %f}\n",
event.frame.rect.x,
event.frame.rect.y,
event.frame.rect.w,
event.frame.rect.h);
} break;
case MP_EVENT_WINDOW_MOVE:
{
printf("moved, rect = {%f, %f, %f, %f}\n",
event.frame.rect.x,
event.frame.rect.y,
event.frame.rect.w,
event.frame.rect.h);
} break;
case MP_EVENT_MOUSE_MOVE:
{
printf("mouse moved, pos = {%f, %f}, delta = {%f, %f}\n",
event.move.x,
event.move.y,
event.move.deltaX,
event.move.deltaY);
} break;
case MP_EVENT_MOUSE_WHEEL:
{
printf("mouse wheel, delta = {%f, %f}\n",
event.move.deltaX,
event.move.deltaY);
} break;
case MP_EVENT_MOUSE_ENTER:
{
printf("mouse enter\n");
} break;
case MP_EVENT_MOUSE_LEAVE:
{
printf("mouse leave\n");
} break;
case MP_EVENT_MOUSE_BUTTON:
{
printf("mouse button %i: %i\n",
event.key.code,
event.key.action == MP_KEY_PRESS ? 1 : 0);
} break;
case MP_EVENT_KEYBOARD_KEY:
{
printf("key %i: %s\n",
event.key.code,
event.key.action == MP_KEY_PRESS ? "press" : (event.key.action == MP_KEY_RELEASE ? "release" : "repeat"));
} break;
case MP_EVENT_KEYBOARD_CHAR:
{
printf("entered char %s\n", event.character.sequence);
} break;
default:
break;
}
}
}
mp_terminate();
return(0);
}
/************************************************************//**
*
* @file: main.cpp
* @author: Martin Fouilleul
* @date: 30/07/2022
* @revision:
*
*****************************************************************/
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include"milepost.h"
int main()
{
mp_init();
mp_rect rect = {.x = 100, .y = 100, .w = 800, .h = 600};
mp_window window = mp_window_create(rect, "test", 0);
mp_window_bring_to_front(window);
mp_window_focus(window);
while(!mp_should_quit())
{
mp_pump_events(0);
mp_event *event = 0;
while((event = mp_next_event(mem_scratch())) != 0)
{
switch(event->type)
{
case MP_EVENT_WINDOW_CLOSE:
{
mp_request_quit();
} break;
case MP_EVENT_WINDOW_RESIZE:
{
printf("resized, rect = {%f, %f, %f, %f}\n",
event->frame.rect.x,
event->frame.rect.y,
event->frame.rect.w,
event->frame.rect.h);
} break;
case MP_EVENT_WINDOW_MOVE:
{
printf("moved, rect = {%f, %f, %f, %f}\n",
event->frame.rect.x,
event->frame.rect.y,
event->frame.rect.w,
event->frame.rect.h);
} break;
case MP_EVENT_MOUSE_MOVE:
{
printf("mouse moved, pos = {%f, %f}, delta = {%f, %f}\n",
event->move.x,
event->move.y,
event->move.deltaX,
event->move.deltaY);
} break;
case MP_EVENT_MOUSE_WHEEL:
{
printf("mouse wheel, delta = {%f, %f}\n",
event->move.deltaX,
event->move.deltaY);
} break;
case MP_EVENT_MOUSE_ENTER:
{
printf("mouse enter\n");
} break;
case MP_EVENT_MOUSE_LEAVE:
{
printf("mouse leave\n");
} break;
case MP_EVENT_MOUSE_BUTTON:
{
printf("mouse button %i: %i\n",
event->key.code,
event->key.action == MP_KEY_PRESS ? 1 : 0);
} break;
case MP_EVENT_KEYBOARD_KEY:
{
printf("key %i: %s\n",
event->key.code,
event->key.action == MP_KEY_PRESS ? "press" : (event->key.action == MP_KEY_RELEASE ? "release" : "repeat"));
} break;
case MP_EVENT_KEYBOARD_CHAR:
{
printf("entered char %s\n", event->character.sequence);
} break;
default:
break;
}
}
mem_arena_clear(mem_scratch());
}
mp_terminate();
return(0);
}

View File

@ -1,4 +1,4 @@
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
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_tiger.exe

View File

@ -1,247 +1,248 @@
/************************************************************//**
*
* @file: main.cpp
* @author: Martin Fouilleul
* @date: 30/07/2022
* @revision:
*
*****************************************************************/
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#define _USE_MATH_DEFINES //NOTE: necessary for MSVC
#include<math.h>
#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<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<errno.h>
#define _USE_MATH_DEFINES //NOTE: necessary for MSVC
#include<math.h>
#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);
}

View File

@ -1,165 +1,163 @@
/************************************************************//**
*
* @file: main.cpp
* @author: Martin Fouilleul
* @date: 30/07/2022
* @revision:
*
*****************************************************************/
#include<stdlib.h>
#include<string.h>
#define _USE_MATH_DEFINES //NOTE: necessary for MSVC
#include<math.h>
#define MG_INCLUDE_GL_API
#include"milepost.h"
#define LOG_SUBSYSTEM "Main"
unsigned int program;
const char* vshaderSource =
"#version 430\n"
"attribute vec4 vPosition;\n"
"uniform mat4 transform;\n"
"void main()\n"
"{\n"
" gl_Position = transform*vPosition;\n"
"}\n";
const char* fshaderSource =
"#version 430\n"
"precision mediump float;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";
void compile_shader(GLuint shader, const char* source)
{
glShaderSource(shader, 1, &source, 0);
glCompileShader(shader);
int err = glGetError();
if(err)
{
printf("gl error: %i\n", err);
}
int status = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if(!status)
{
char buffer[256];
int size = 0;
glGetShaderInfoLog(shader, 256, &size, buffer);
printf("shader error: %.*s\n", size, buffer);
}
}
int main()
{
LogLevel(LOG_LEVEL_DEBUG);
mp_init();
mp_rect rect = {.x = 100, .y = 100, .w = 800, .h = 600};
mp_window window = mp_window_create(rect, "test", 0);
//NOTE: create surface
mg_surface surface = mg_surface_create_for_window(window, MG_BACKEND_GL);
//NOTE: init shader and gl state
mg_surface_prepare(surface);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
GLfloat vertices[] = {
-0.866/2, -0.5/2, 0, 0.866/2, -0.5/2, 0, 0, 0.5, 0};
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
unsigned int vshader = glCreateShader(GL_VERTEX_SHADER);
unsigned int fshader = glCreateShader(GL_FRAGMENT_SHADER);
program = glCreateProgram();
compile_shader(vshader, vshaderSource);
compile_shader(fshader, fshaderSource);
glAttachShader(program, vshader);
glAttachShader(program, fshader);
glLinkProgram(program);
int status = 0;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if(!status)
{
char buffer[256];
int size = 0;
glGetProgramInfoLog(program, 256, &size, buffer);
printf("link error: %.*s\n", size, buffer);
}
glUseProgram(program);
mp_window_bring_to_front(window);
// mp_window_focus(window);
while(!mp_should_quit())
{
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;
default:
break;
}
}
mg_surface_prepare(surface);
glClearColor(0.3, 0.3, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
static float alpha = 0;
//f32 aspect = frameSize.x/frameSize.y;
f32 aspect = 800/(f32)600;
GLfloat matrix[] = {cosf(alpha)/aspect, sinf(alpha), 0, 0,
-sinf(alpha)/aspect, cosf(alpha), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1};
alpha += 2*M_PI/120;
glUniformMatrix4fv(0, 1, false, matrix);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3);
mg_surface_present(surface);
}
mp_terminate();
return(0);
}
/************************************************************//**
*
* @file: main.cpp
* @author: Martin Fouilleul
* @date: 30/07/2022
* @revision:
*
*****************************************************************/
#include<stdio.h>
#include<string.h>
#define _USE_MATH_DEFINES //NOTE: necessary for MSVC
#include<math.h>
#define MG_INCLUDE_GL_API
#include"milepost.h"
unsigned int program;
const char* vshaderSource =
"#version 430\n"
"attribute vec4 vPosition;\n"
"uniform mat4 transform;\n"
"void main()\n"
"{\n"
" gl_Position = transform*vPosition;\n"
"}\n";
const char* fshaderSource =
"#version 430\n"
"precision mediump float;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";
void compile_shader(GLuint shader, const char* source)
{
glShaderSource(shader, 1, &source, 0);
glCompileShader(shader);
int err = glGetError();
if(err)
{
printf("gl error: %i\n", err);
}
int status = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if(!status)
{
char buffer[256];
int size = 0;
glGetShaderInfoLog(shader, 256, &size, buffer);
printf("shader error: %.*s\n", size, buffer);
}
}
int main()
{
mp_init();
mp_rect rect = {.x = 100, .y = 100, .w = 800, .h = 600};
mp_window window = mp_window_create(rect, "test", 0);
//NOTE: create surface
mg_surface surface = mg_surface_create_for_window(window, MG_GL);
//NOTE: init shader and gl state
mg_surface_prepare(surface);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
GLfloat vertices[] = {
-0.866/2, -0.5/2, 0, 0.866/2, -0.5/2, 0, 0, 0.5, 0};
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
unsigned int vshader = glCreateShader(GL_VERTEX_SHADER);
unsigned int fshader = glCreateShader(GL_FRAGMENT_SHADER);
program = glCreateProgram();
compile_shader(vshader, vshaderSource);
compile_shader(fshader, fshaderSource);
glAttachShader(program, vshader);
glAttachShader(program, fshader);
glLinkProgram(program);
int status = 0;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if(!status)
{
char buffer[256];
int size = 0;
glGetProgramInfoLog(program, 256, &size, buffer);
printf("link error: %.*s\n", size, buffer);
}
glUseProgram(program);
mp_window_bring_to_front(window);
// mp_window_focus(window);
while(!mp_should_quit())
{
mp_pump_events(0);
mp_event* event = 0;
while((event = mp_next_event(mem_scratch())) != 0)
{
switch(event->type)
{
case MP_EVENT_WINDOW_CLOSE:
{
mp_request_quit();
} break;
default:
break;
}
}
mg_surface_prepare(surface);
glClearColor(0.3, 0.3, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
static float alpha = 0;
//f32 aspect = frameSize.x/frameSize.y;
f32 aspect = 800/(f32)600;
GLfloat matrix[] = {cosf(alpha)/aspect, sinf(alpha), 0, 0,
-sinf(alpha)/aspect, cosf(alpha), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1};
alpha += 2*M_PI/120;
glUniformMatrix4fv(0, 1, false, matrix);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3);
mg_surface_present(surface);
mem_arena_clear(mem_scratch());
}
mp_terminate();
return(0);
}

File diff suppressed because it is too large Load Diff

View File

@ -1,77 +1,84 @@
## Angle install on macOS
* Get ninja if needed: `brew install ninja`
* Get the `depot_tools`repo: `git clone https://chromium.googlesource.com/* chromium/tools/depot_tools.git`
* Set path: `export PATH=/path/to/depot_tools:$PATH`
* Maybe necessary to fiddle with certificates here, otherwise `fetch angle` fails in the subsequent steps.
```
cd /Applications/Python\ 3.6
sudo ./Install\ Certificates.command
```
* Fetch angle:
```
mkdir angle
cd angle
fetch angle
```
* Generate build config: `gn gen out/Debug`
* To see available arguments: `gn args out/Debug --list`
* To change arguments: `gn args out/Debug`
For example, to generate dwarf dsyms files, set:
```
enable_dsyms=true
use_debug_fission=true
symbol_level=2
```
We also need to set `is_component_build=false` in order to have self-contained librarries.
Then, build with `autoninja -C out/Debug`and wait until you pass out.
## Angle install on windows
* need Python3 (can install through win app store)
* need Windows SDK
* clone `depot_tools`: `git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git`
or download and unzip bundle at [https://storage.googleapis.com/chrome-infra/depot_tools.zip](https://storage.googleapis.com/chrome-infra/depot_tools.zip)
* set `depot_tools` in path env variable through control panel>System and security>system>advanced system settings
* run `gclient` in a cmd shell
* set `DEPOT_TOOLS_WIN_TOOLCHAIN=0`
* `mkdir angle`
* `cd angle`
* `fetch angle`
* wait a million years
* if it fails when running `python3 third_party/depot_tools/download_from_google_storage.py ...`
-> open `DEPS` and change `third_party/depot_tools` to `../depot/tools`
* run `gclient sync` to complete previous step
* `gn gen out/Debug`
* `gn args out/Debug` and edit arguments:
* `angle_enable_vulkan = false`
* `angle_build_tests = false`
* `is_component_build = false`
* link with `libEGL.dll.lib` and `libGLESv2.dll.lib`
* put `libEGL.dll` and `libGLESv2.dll` in same directory as executable
## To get debugging kinda working with renderdoc:
Run `gn args out/Debug` and set
* `angle_enable_trace = true`
* `angle_enable_annotator_run_time_checks = true`
* `autoninja -C out/Debug`
* wait a while
In renderdoc, set env variables
`RENDERDOC_HOOK_EGL 0` (if you want to trace underlying native API)
`RENDERDOC_HOOK_EGL 1` (if you want to trace EGL calls. You also need to put `libEGL` in the renderdoc folder so it's found when capturing stuff. Unfortunately though, that seems to provoke crashes...)
`ANGLE_ENABLE_DEBUG_MARKERS 1` (to turn on debug markers)
## Angle install on macOS
* Get ninja if needed: `brew install ninja`
* Get the `depot_tools`repo: `git clone https://chromium.googlesource.com/* chromium/tools/depot_tools.git`
* Set path: `export PATH=/path/to/depot_tools:$PATH`
* Maybe necessary to fiddle with certificates here, otherwise `fetch angle` fails in the subsequent steps.
```
cd /Applications/Python\ 3.6
sudo ./Install\ Certificates.command
```
* Fetch angle:
```
mkdir angle
cd angle
fetch angle
```
* Generate build config: `gn gen out/Debug`
* To see available arguments: `gn args out/Debug --list`
* To change arguments: `gn args out/Debug`
For example, to generate dwarf dsyms files, set:
```
enable_dsyms=true
use_debug_fission=true
symbol_level=2
```
We also need to set `is_component_build=false` in order to have self-contained librarries.
Then, build with `autoninja -C out/Debug`and wait until you pass out.
## Angle install on windows
* need Python3 (can install through win app store)
* need Windows SDK
* clone `depot_tools`: `git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git`
or download and unzip bundle at [https://storage.googleapis.com/chrome-infra/depot_tools.zip](https://storage.googleapis.com/chrome-infra/depot_tools.zip)
* set `depot_tools` in path env variable through control panel>System and security>system>advanced system settings
* run `gclient` in a cmd shell
* set `DEPOT_TOOLS_WIN_TOOLCHAIN=0`
* `mkdir angle`
* `cd angle`
* `fetch angle`
* wait a million years
* if it fails when running `python3 third_party/depot_tools/download_from_google_storage.py ...`
-> open `DEPS` and change `third_party/depot_tools` to `../depot_tools`
* run `gclient sync` to complete previous step
* `gn gen out/Debug`
* `gn args out/Debug` and edit arguments:
```
is_component_build = false
angle_build_tests = false
angle_enable_metal = false
angle_enable_d3d9 = false
angle_enable_gl = false
angle_enable_vulkan = false
```
* `ninja -C out/Debug`
* link with `libEGL.dll.lib` and `libGLESv2.dll.lib`
* put `libEGL.dll` and `libGLESv2.dll` in same directory as executable
## To get debugging kinda working with renderdoc:
Run `gn args out/Debug` and set
* `angle_enable_trace = true`
* `angle_enable_annotator_run_time_checks = true`
* `autoninja -C out/Debug`
* wait a while
In renderdoc, set env variables
`RENDERDOC_HOOK_EGL 0` (if you want to trace underlying native API)
`RENDERDOC_HOOK_EGL 1` (if you want to trace EGL calls. You also need to put `libEGL` in the renderdoc folder so it's found when capturing stuff. Unfortunately though, that seems to provoke crashes...)
`ANGLE_ENABLE_DEBUG_MARKERS 1` (to turn on debug markers)

File diff suppressed because it is too large Load Diff

View File

@ -1,469 +0,0 @@
/*********************************************************************
*
* file: glsl_shaders.h
* note: string literals auto-generated by embed_text.py
* date: 09/032023
*
**********************************************************************/
#ifndef __GLSL_SHADERS_H__
#define __GLSL_SHADERS_H__
//NOTE: string imported from src\glsl_shaders\common.glsl
const char* glsl_common =
"\n"
"layout(std430) buffer;\n"
"\n"
"struct vertex {\n"
" vec4 cubic;\n"
" vec2 pos;\n"
" int shapeIndex;\n"
"};\n"
"\n"
"struct shape {\n"
" vec4 color;\n"
" vec4 clip;\n"
" float uvTransform[6];\n"
"};\n";
//NOTE: string imported from src\glsl_shaders\blit_vertex.glsl
const char* glsl_blit_vertex =
"\n"
"precision mediump float;\n"
"\n"
"out vec2 uv;\n"
"\n"
"void main()\n"
"{\n"
" /* generate (0, 0) (1, 0) (1, 1) (1, 1) (0, 1) (0, 0)*/\n"
"\n"
" float x = float(((uint(gl_VertexID) + 2u) / 3u)%2u);\n"
" float y = float(((uint(gl_VertexID) + 1u) / 3u)%2u);\n"
"\n"
" gl_Position = vec4(-1.0f + x*2.0f, -1.0f+y*2.0f, 0.0f, 1.0f);\n"
" uv = vec2(x, 1-y);\n"
"}\n";
//NOTE: string imported from src\glsl_shaders\blit_fragment.glsl
const char* glsl_blit_fragment =
"\n"
"precision mediump float;\n"
"\n"
"in vec2 uv;\n"
"out vec4 fragColor;\n"
"\n"
"layout(location=0) uniform sampler2D tex;\n"
"\n"
"void main()\n"
"{\n"
" fragColor = texture(tex, uv);\n"
"}\n";
//NOTE: string imported from src\glsl_shaders\clear_counters.glsl
const char* glsl_clear_counters =
"\n"
"layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;\n"
"\n"
"precision mediump float;\n"
"layout(std430) buffer;\n"
"\n"
"layout(binding = 0) coherent restrict writeonly buffer tileCounterBufferSSBO {\n"
" uint elements[];\n"
"} tileCounterBuffer ;\n"
"\n"
"void main()\n"
"{\n"
" uint tileIndex = gl_WorkGroupID.x;\n"
" tileCounterBuffer.elements[tileIndex] = 0u;\n"
"}\n";
//NOTE: string imported from src\glsl_shaders\tile.glsl
const char* glsl_tile =
"\n"
"layout(local_size_x = 512, local_size_y = 1, local_size_z = 1) in;\n"
"\n"
"precision mediump float;\n"
"\n"
"layout(binding = 0) restrict readonly buffer vertexBufferSSBO {\n"
" vertex elements[];\n"
"} vertexBuffer ;\n"
"\n"
"layout(binding = 1) restrict readonly buffer shapeBufferSSBO {\n"
" shape elements[];\n"
"} shapeBuffer ;\n"
"\n"
"layout(binding = 2) restrict readonly buffer indexBufferSSBO {\n"
" uint elements[];\n"
"} indexBuffer ;\n"
"\n"
"layout(binding = 3) coherent restrict buffer tileCounterBufferSSBO {\n"
" uint elements[];\n"
"} tileCounterBuffer ;\n"
"\n"
"layout(binding = 4) coherent restrict writeonly buffer tileArrayBufferSSBO {\n"
" uint elements[];\n"
"} tileArrayBuffer ;\n"
"\n"
"layout(location = 0) uniform uint indexCount;\n"
"layout(location = 1) uniform uvec2 tileCount;\n"
"layout(location = 2) uniform uint tileSize;\n"
"layout(location = 3) uniform uint tileArraySize;\n"
"layout(location = 4) uniform vec2 scaling;\n"
"\n"
"void main()\n"
"{\n"
" uint triangleIndex = (gl_WorkGroupID.x*gl_WorkGroupSize.x + gl_LocalInvocationIndex) * 3u;\n"
" if(triangleIndex >= indexCount)\n"
" {\n"
" return;\n"
" }\n"
"\n"
" uint i0 = indexBuffer.elements[triangleIndex];\n"
" uint i1 = indexBuffer.elements[triangleIndex+1u];\n"
" uint i2 = indexBuffer.elements[triangleIndex+2u];\n"
"\n"
" vec2 p0 = vertexBuffer.elements[i0].pos * scaling;\n"
" vec2 p1 = vertexBuffer.elements[i1].pos * scaling;\n"
" vec2 p2 = vertexBuffer.elements[i2].pos * scaling;\n"
"\n"
" int shapeIndex = vertexBuffer.elements[i0].shapeIndex;\n"
" vec4 clip = shapeBuffer.elements[shapeIndex].clip * vec4(scaling, scaling);\n"
"\n"
" vec4 fbox = vec4(max(min(min(p0.x, p1.x), p2.x), clip.x),\n"
" max(min(min(p0.y, p1.y), p2.y), clip.y),\n"
" min(max(max(p0.x, p1.x), p2.x), clip.z),\n"
" min(max(max(p0.y, p1.y), p2.y), clip.w));\n"
"\n"
" ivec4 box = ivec4(floor(fbox))/int(tileSize);\n"
"\n"
" //NOTE(martin): it's importat to do the computation with signed int, so that we can have negative xMax/yMax\n"
" // otherwise all triangles on the left or below the x/y axis are attributed to tiles on row/column 0.\n"
" int xMin = max(0, box.x);\n"
" int yMin = max(0, box.y);\n"
" int xMax = min(box.z, int(tileCount.x) - 1);\n"
" int yMax = min(box.w, int(tileCount.y) - 1);\n"
"\n"
" for(int y = yMin; y <= yMax; y++)\n"
" {\n"
" for(int x = xMin ; x <= xMax; x++)\n"
" {\n"
" uint tileIndex = uint(y)*tileCount.x + uint(x);\n"
" uint tileCounter = atomicAdd(tileCounterBuffer.elements[tileIndex], 1u);\n"
" if(tileCounter < tileArraySize)\n"
" {\n"
" tileArrayBuffer.elements[tileArraySize*tileIndex + tileCounter] = triangleIndex;\n"
" }\n"
" }\n"
" }\n"
"}\n";
//NOTE: string imported from src\glsl_shaders\sort.glsl
const char* glsl_sort =
"\n"
"layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;\n"
"\n"
"precision mediump float;\n"
"\n"
"layout(binding = 0) restrict readonly buffer vertexBufferSSBO {\n"
" vertex elements[];\n"
"} vertexBuffer ;\n"
"\n"
"layout(binding = 1) restrict readonly buffer shapeBufferSSBO {\n"
" shape elements[];\n"
"} shapeBuffer ;\n"
"\n"
"layout(binding = 2) restrict readonly buffer indexBufferSSBO {\n"
" uint elements[];\n"
"} indexBuffer ;\n"
"\n"
"layout(binding = 3) coherent readonly restrict buffer tileCounterBufferSSBO {\n"
" uint elements[];\n"
"} tileCounterBuffer ;\n"
"\n"
"layout(binding = 4) coherent restrict buffer tileArrayBufferSSBO {\n"
" uint elements[];\n"
"} tileArrayBuffer ;\n"
"\n"
"layout(location = 0) uniform uint indexCount;\n"
"layout(location = 1) uniform uvec2 tileCount;\n"
"layout(location = 2) uniform uint tileSize;\n"
"layout(location = 3) uniform uint tileArraySize;\n"
"\n"
"int get_shape_index(uint tileArrayOffset, uint tileArrayIndex)\n"
"{\n"
" uint triangleIndex = tileArrayBuffer.elements[tileArrayOffset + tileArrayIndex];\n"
" uint i0 = indexBuffer.elements[triangleIndex];\n"
" int shapeIndex = vertexBuffer.elements[i0].shapeIndex;\n"
" return(shapeIndex);\n"
"}\n"
"\n"
"void main()\n"
"{\n"
" uint tileIndex = gl_WorkGroupID.x;\n"
" uint tileArrayOffset = tileArraySize * tileIndex;\n"
" uint tileArrayCount = min(tileCounterBuffer.elements[tileIndex], tileArraySize);\n"
"\n"
" for(uint tileArrayIndex=1u; tileArrayIndex < tileArrayCount; tileArrayIndex++)\n"
" {\n"
" for(uint sortIndex = tileArrayIndex; sortIndex > 0u; sortIndex--)\n"
" {\n"
" int shapeIndex = get_shape_index(tileArrayOffset, sortIndex);\n"
" int prevShapeIndex = get_shape_index(tileArrayOffset, sortIndex-1u);\n"
"\n"
" if(shapeIndex >= prevShapeIndex)\n"
" {\n"
" break;\n"
" }\n"
" uint tmp = tileArrayBuffer.elements[tileArrayOffset + sortIndex];\n"
" tileArrayBuffer.elements[tileArrayOffset + sortIndex] = tileArrayBuffer.elements[tileArrayOffset + sortIndex - 1u];\n"
" tileArrayBuffer.elements[tileArrayOffset + sortIndex - 1u] = tmp;\n"
" }\n"
" }\n"
"}\n";
//NOTE: string imported from src\glsl_shaders\draw.glsl
const char* glsl_draw =
"\n"
"#extension GL_ARB_gpu_shader_int64 : require\n"
"layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;\n"
"\n"
"precision mediump float;\n"
"//precision mediump image2D;\n"
"\n"
"layout(binding = 0) restrict readonly buffer vertexBufferSSBO {\n"
" vertex elements[];\n"
"} vertexBuffer ;\n"
"\n"
"layout(binding = 1) restrict readonly buffer shapeBufferSSBO {\n"
" shape elements[];\n"
"} shapeBuffer ;\n"
"\n"
"layout(binding = 2) restrict readonly buffer indexBufferSSBO {\n"
" uint elements[];\n"
"} indexBuffer ;\n"
"\n"
"layout(binding = 3) restrict readonly buffer tileCounterBufferSSBO {\n"
" uint elements[];\n"
"} tileCounterBuffer ;\n"
"\n"
"layout(binding = 4) restrict readonly buffer tileArrayBufferSSBO {\n"
" uint elements[];\n"
"} tileArrayBuffer ;\n"
"\n"
"layout(location = 0) uniform uint indexCount;\n"
"layout(location = 1) uniform uvec2 tileCount;\n"
"layout(location = 2) uniform uint tileSize;\n"
"layout(location = 3) uniform uint tileArraySize;\n"
"layout(location = 4) uniform vec2 scaling;\n"
"layout(location = 5) uniform uint useTexture;\n"
"\n"
"layout(rgba8, binding = 0) uniform restrict writeonly image2D outTexture;\n"
"\n"
"layout(binding = 1) uniform sampler2D srcTexture;\n"
"\n"
"\n"
"bool is_top_left(ivec2 a, ivec2 b)\n"
"{\n"
" return( (a.y == b.y && b.x < a.x)\n"
" ||(b.y < a.y));\n"
"}\n"
"\n"
"//////////////////////////////////////////////////////////////////////////////\n"
"//TODO: we should do these computations on 64bits, because otherwise\n"
"// we might overflow for values > 2048.\n"
"// Unfortunately this is costly.\n"
"// Another way is to precompute triangle edges (b - a) in full precision\n"
"// once to avoid doing it all the time...\n"
"//////////////////////////////////////////////////////////////////////////////\n"
"int orient2d(ivec2 a, ivec2 b, ivec2 p)\n"
"{\n"
" return((b.x-a.x)*(p.y-a.y) - (b.y-a.y)*(p.x-a.x));\n"
"}\n"
"\n"
"int is_clockwise(ivec2 p0, ivec2 p1, ivec2 p2)\n"
"{\n"
" return((p1 - p0).x*(p2 - p0).y - (p1 - p0).y*(p2 - p0).x);\n"
"}\n"
"\n"
"void main()\n"
"{\n"
" ivec2 pixelCoord = ivec2(gl_WorkGroupID.xy*uvec2(16, 16) + gl_LocalInvocationID.xy);\n"
" uvec2 tileCoord = uvec2(pixelCoord) / tileSize;\n"
" uint tileIndex = tileCoord.y * tileCount.x + tileCoord.x;\n"
" uint tileCounter = min(tileCounterBuffer.elements[tileIndex], tileArraySize);\n"
"\n"
" const float subPixelFactor = 16.;\n"
" ivec2 centerPoint = ivec2((vec2(pixelCoord) + vec2(0.5, 0.5)) * subPixelFactor);\n"
"\n"
"//*\n"
" const int sampleCount = 8;\n"
" ivec2 samplePoints[sampleCount] = ivec2[sampleCount](centerPoint + ivec2(1, 3),\n"
" centerPoint + ivec2(-1, -3),\n"
" centerPoint + ivec2(5, -1),\n"
" centerPoint + ivec2(-3, 5),\n"
" centerPoint + ivec2(-5, -5),\n"
" centerPoint + ivec2(-7, 1),\n"
" centerPoint + ivec2(3, -7),\n"
" centerPoint + ivec2(7, 7));\n"
"/*/\n"
" const int sampleCount = 4;\n"
" ivec2 samplePoints[sampleCount] = ivec2[sampleCount](centerPoint + ivec2(-2, 6),\n"
" centerPoint + ivec2(6, 2),\n"
" centerPoint + ivec2(-6, -2),\n"
" centerPoint + ivec2(2, -6));\n"
"//*/\n"
" //DEBUG\n"
"/*\n"
" {\n"
" vec4 fragColor = vec4(0);\n"
"\n"
" if( pixelCoord.x % 16 == 0\n"
" ||pixelCoord.y % 16 == 0)\n"
" {\n"
" fragColor = vec4(0, 0, 0, 1);\n"
" }\n"
" else if(tileCounterBuffer.elements[tileIndex] == 0xffffu)\n"
" {\n"
" fragColor = vec4(1, 0, 1, 1);\n"
" }\n"
" else if(tileCounter != 0u)\n"
" {\n"
" fragColor = vec4(0, 1, 0, 1);\n"
" }\n"
" else\n"
" {\n"
" fragColor = vec4(1, 0, 0, 1);\n"
" }\n"
" imageStore(outTexture, pixelCoord, fragColor);\n"
" return;\n"
" }\n"
"//*/\n"
" //----\n"
"\n"
" vec4 sampleColor[sampleCount];\n"
" vec4 currentColor[sampleCount];\n"
" int currentShapeIndex[sampleCount];\n"
" int flipCount[sampleCount];\n"
"\n"
" for(int i=0; i<sampleCount; i++)\n"
" {\n"
" currentShapeIndex[i] = -1;\n"
" flipCount[i] = 0;\n"
" sampleColor[i] = vec4(0, 0, 0, 0);\n"
" currentColor[i] = vec4(0, 0, 0, 0);\n"
" }\n"
"\n"
" for(uint tileArrayIndex=0u; tileArrayIndex < tileCounter; tileArrayIndex++)\n"
" {\n"
" uint triangleIndex = tileArrayBuffer.elements[tileArraySize * tileIndex + tileArrayIndex];\n"
"\n"
" uint i0 = indexBuffer.elements[triangleIndex];\n"
" uint i1 = indexBuffer.elements[triangleIndex+1u];\n"
" uint i2 = indexBuffer.elements[triangleIndex+2u];\n"
"\n"
" ivec2 p0 = ivec2((vertexBuffer.elements[i0].pos * scaling) * subPixelFactor);\n"
" ivec2 p1 = ivec2((vertexBuffer.elements[i1].pos * scaling) * subPixelFactor);\n"
" ivec2 p2 = ivec2((vertexBuffer.elements[i2].pos * scaling) * subPixelFactor);\n"
"\n"
" int shapeIndex = vertexBuffer.elements[i0].shapeIndex;\n"
" vec4 color = shapeBuffer.elements[shapeIndex].color;\n"
" color.rgb *= color.a;\n"
"\n"
" ivec4 clip = ivec4(round((shapeBuffer.elements[shapeIndex].clip * vec4(scaling, scaling) + vec4(0.5, 0.5, 0.5, 0.5)) * subPixelFactor));\n"
"\n"
" mat3 uvTransform = mat3(shapeBuffer.elements[shapeIndex].uvTransform[0],\n"
" shapeBuffer.elements[shapeIndex].uvTransform[3],\n"
" 0.,\n"
" shapeBuffer.elements[shapeIndex].uvTransform[1],\n"
" shapeBuffer.elements[shapeIndex].uvTransform[4],\n"
" 0.,\n"
" shapeBuffer.elements[shapeIndex].uvTransform[2],\n"
" shapeBuffer.elements[shapeIndex].uvTransform[5],\n"
" 1.);\n"
"\n"
" //NOTE(martin): reorder triangle counter-clockwise and compute bias for each edge\n"
" int cw = is_clockwise(p0, p1, p2);\n"
" if(cw < 0)\n"
" {\n"
" uint tmpIndex = i1;\n"
" i1 = i2;\n"
" i2 = tmpIndex;\n"
"\n"
" ivec2 tmpPoint = p1;\n"
" p1 = p2;\n"
" p2 = tmpPoint;\n"
" }\n"
"\n"
" vec4 cubic0 = vertexBuffer.elements[i0].cubic;\n"
" vec4 cubic1 = vertexBuffer.elements[i1].cubic;\n"
" vec4 cubic2 = vertexBuffer.elements[i2].cubic;\n"
"\n"
" int bias0 = is_top_left(p1, p2) ? 0 : -1;\n"
" int bias1 = is_top_left(p2, p0) ? 0 : -1;\n"
" int bias2 = is_top_left(p0, p1) ? 0 : -1;\n"
"\n"
" for(int sampleIndex = 0; sampleIndex < sampleCount; sampleIndex++)\n"
" {\n"
" ivec2 samplePoint = samplePoints[sampleIndex];\n"
"\n"
" if( samplePoint.x < clip.x\n"
" || samplePoint.x > clip.z\n"
" || samplePoint.y < clip.y\n"
" || samplePoint.y > clip.w)\n"
" {\n"
" continue;\n"
" }\n"
"\n"
" int w0 = orient2d(p1, p2, samplePoint);\n"
" int w1 = orient2d(p2, p0, samplePoint);\n"
" int w2 = orient2d(p0, p1, samplePoint);\n"
"\n"
" if((w0+bias0) >= 0 && (w1+bias1) >= 0 && (w2+bias2) >= 0)\n"
" {\n"
" vec4 cubic = (cubic0*float(w0) + cubic1*float(w1) + cubic2*float(w2))/(float(w0)+float(w1)+float(w2));\n"
"\n"
" float eps = 0.0001;\n"
" if(cubic.w*(cubic.x*cubic.x*cubic.x - cubic.y*cubic.z) <= eps)\n"
" {\n"
" if(shapeIndex == currentShapeIndex[sampleIndex])\n"
" {\n"
" flipCount[sampleIndex]++;\n"
" }\n"
" else\n"
" {\n"
" if((flipCount[sampleIndex] & 0x01) != 0)\n"
" {\n"
" sampleColor[sampleIndex] = currentColor[sampleIndex];\n"
" }\n"
"\n"
" vec4 nextColor = color;\n"
" if(useTexture)\n"
" {\n"
" vec3 sampleFP = vec3(vec2(samplePoint).xy/(subPixelFactor*2.), 1);\n"
" vec2 uv = (uvTransform * sampleFP).xy;\n"
" vec4 texColor = texture(srcTexture, uv);\n"
" texColor.rgb *= texColor.a;\n"
" nextColor *= texColor;\n"
" }\n"
" currentColor[sampleIndex] = sampleColor[sampleIndex]*(1.-nextColor.a) + nextColor;\n"
" currentShapeIndex[sampleIndex] = shapeIndex;\n"
" flipCount[sampleIndex] = 1;\n"
" }\n"
" }\n"
" }\n"
" }\n"
" }\n"
" vec4 pixelColor = vec4(0);\n"
" for(int sampleIndex = 0; sampleIndex < sampleCount; sampleIndex++)\n"
" {\n"
" if((flipCount[sampleIndex] & 0x01) != 0)\n"
" {\n"
" sampleColor[sampleIndex] = currentColor[sampleIndex];\n"
" }\n"
" pixelColor += sampleColor[sampleIndex];\n"
" }\n"
"\n"
" imageStore(outTexture, pixelCoord, pixelColor/float(sampleCount));\n"
"}\n";
#endif // __GLSL_SHADERS_H__

View File

@ -1,407 +1,407 @@
/************************************************************//**
*
* @file: graphics_surface.c
* @author: Martin Fouilleul
* @date: 25/04/2023
*
*****************************************************************/
#include"graphics_surface.h"
//---------------------------------------------------------------
// typed handles functions
//---------------------------------------------------------------
mg_surface mg_surface_handle_alloc(mg_surface_data* surface)
{
mg_surface handle = {.h = mg_handle_alloc(MG_HANDLE_SURFACE, (void*)surface) };
return(handle);
}
mg_surface_data* mg_surface_data_from_handle(mg_surface handle)
{
mg_surface_data* data = mg_data_from_handle(MG_HANDLE_SURFACE, handle.h);
return(data);
}
mg_image mg_image_handle_alloc(mg_image_data* image)
{
mg_image handle = {.h = mg_handle_alloc(MG_HANDLE_IMAGE, (void*)image) };
return(handle);
}
mg_image_data* mg_image_data_from_handle(mg_image handle)
{
mg_image_data* data = mg_data_from_handle(MG_HANDLE_IMAGE, handle.h);
return(data);
}
//---------------------------------------------------------------
// surface API
//---------------------------------------------------------------
#if MG_COMPILE_GL
#if PLATFORM_WINDOWS
#include"wgl_surface.h"
#define gl_surface_create_for_window mg_wgl_surface_create_for_window
#endif
#endif
#if MG_COMPILE_GLES
#include"egl_surface.h"
#endif
#if MG_COMPILE_METAL
#include"mtl_surface.h"
#endif
#if MG_COMPILE_CANVAS
#if PLATFORM_MACOS
mg_surface_data* mtl_canvas_surface_create_for_window(mp_window window);
#elif PLATFORM_WINDOWS
//TODO
#endif
#endif
bool mg_is_surface_backend_available(mg_surface_api api)
{
bool result = false;
switch(api)
{
#if MG_COMPILE_METAL
case MG_METAL:
#endif
#if MG_COMPILE_GL
case MG_GL:
#endif
#if MG_COMPILE_GLES
case MG_GLES:
#endif
#if MG_COMPILE_CANVAS
case MG_CANVAS:
#endif
result = true;
break;
default:
break;
}
return(result);
}
mg_surface mg_surface_nil() { return((mg_surface){.h = 0}); }
bool mg_surface_is_nil(mg_surface surface) { return(surface.h == 0); }
mg_surface mg_surface_create_for_window(mp_window window, mg_surface_api api)
{
if(__mgData.init)
{
mg_init();
}
mg_surface surfaceHandle = mg_surface_nil();
mg_surface_data* surface = 0;
switch(api)
{
#if MG_COMPILE_GL
case MG_GL:
surface = gl_surface_create_for_window(window);
break;
#endif
#if MG_COMPILE_GLES
case MG_GLES:
surface = mg_egl_surface_create_for_window(window);
break;
#endif
#if MG_COMPILE_METAL
case MG_METAL:
surface = mg_mtl_surface_create_for_window(window);
break;
#endif
#if MG_COMPILE_CANVAS
case MG_CANVAS:
#if PLATFORM_MACOS
surface = mtl_canvas_surface_create_for_window(window);
#elif PLATFORM_WINDOWS
surface = gl_canvas_surface_create_for_window(window);
#endif
break;
#endif
default:
break;
}
if(surface)
{
surfaceHandle = mg_surface_handle_alloc(surface);
}
return(surfaceHandle);
}
mg_surface mg_surface_create_remote(u32 width, u32 height, mg_surface_api api)
{
if(__mgData.init)
{
mg_init();
}
mg_surface surfaceHandle = mg_surface_nil();
mg_surface_data* surface = 0;
switch(api)
{
#if MG_COMPILE_GLES
case MG_GLES:
surface = mg_egl_surface_create_remote(width, height);
break;
#endif
default:
break;
}
if(surface)
{
surfaceHandle = mg_surface_handle_alloc(surface);
}
return(surfaceHandle);
}
mg_surface mg_surface_create_host(mp_window window)
{
if(__mgData.init)
{
mg_init();
}
mg_surface handle = mg_surface_nil();
mg_surface_data* surface = 0;
#if PLATFORM_MACOS
surface = mg_osx_surface_create_host(window);
#elif PLATFORM_WINDOWS
surface = mg_win32_surface_create_host(window);
#endif
if(surface)
{
handle = mg_surface_handle_alloc(surface);
}
return(handle);
}
void mg_surface_destroy(mg_surface handle)
{
DEBUG_ASSERT(__mgData.init);
mg_surface_data* surface = mg_surface_data_from_handle(handle);
if(surface)
{
if(surface->backend && surface->backend->destroy)
{
surface->backend->destroy(surface->backend);
}
surface->destroy(surface);
mg_handle_recycle(handle.h);
}
}
void mg_surface_prepare(mg_surface surface)
{
DEBUG_ASSERT(__mgData.init);
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->prepare)
{
surfaceData->prepare(surfaceData);
}
}
void mg_surface_present(mg_surface surface)
{
DEBUG_ASSERT(__mgData.init);
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->present)
{
surfaceData->present(surfaceData);
}
}
void mg_surface_swap_interval(mg_surface surface, int swap)
{
DEBUG_ASSERT(__mgData.init);
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->swapInterval)
{
surfaceData->swapInterval(surfaceData, swap);
}
}
vec2 mg_surface_contents_scaling(mg_surface surface)
{
DEBUG_ASSERT(__mgData.init);
vec2 scaling = {1, 1};
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->contentsScaling)
{
scaling = surfaceData->contentsScaling(surfaceData);
}
return(scaling);
}
void mg_surface_set_frame(mg_surface surface, mp_rect frame)
{
DEBUG_ASSERT(__mgData.init);
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->setFrame)
{
surfaceData->setFrame(surfaceData, frame);
}
}
mp_rect mg_surface_get_frame(mg_surface surface)
{
DEBUG_ASSERT(__mgData.init);
mp_rect res = {0};
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->getFrame)
{
res = surfaceData->getFrame(surfaceData);
}
return(res);
}
void mg_surface_set_hidden(mg_surface surface, bool hidden)
{
DEBUG_ASSERT(__mgData.init);
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->setHidden)
{
surfaceData->setHidden(surfaceData, hidden);
}
}
bool mg_surface_get_hidden(mg_surface surface)
{
DEBUG_ASSERT(__mgData.init);
bool res = false;
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->getHidden)
{
res = surfaceData->getHidden(surfaceData);
}
return(res);
}
void* mg_surface_native_layer(mg_surface surface)
{
void* res = 0;
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->nativeLayer)
{
res = surfaceData->nativeLayer(surfaceData);
}
return(res);
}
mg_surface_id mg_surface_remote_id(mg_surface handle)
{
mg_surface_id remoteId = 0;
mg_surface_data* surface = mg_surface_data_from_handle(handle);
if(surface && surface->remoteID)
{
remoteId = surface->remoteID(surface);
}
return(remoteId);
}
void mg_surface_host_connect(mg_surface handle, mg_surface_id remoteID)
{
mg_surface_data* surface = mg_surface_data_from_handle(handle);
if(surface && surface->hostConnect)
{
surface->hostConnect(surface, remoteID);
}
}
void mg_surface_render_commands(mg_surface surface,
mg_color clearColor,
u32 primitiveCount,
mg_primitive* primitives,
u32 eltCount,
mg_path_elt* elements)
{
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->backend)
{
surfaceData->backend->render(surfaceData->backend,
clearColor,
primitiveCount,
primitives,
eltCount,
elements);
}
}
//------------------------------------------------------------------------------------------
//NOTE(martin): images
//------------------------------------------------------------------------------------------
vec2 mg_image_size(mg_image image)
{
vec2 res = {0};
mg_image_data* imageData = mg_image_data_from_handle(image);
if(imageData)
{
res = imageData->size;
}
return(res);
}
mg_image mg_image_create(mg_surface surface, u32 width, u32 height)
{
mg_image image = mg_image_nil();
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->backend)
{
DEBUG_ASSERT(surfaceData->api == MG_CANVAS);
mg_image_data* imageData = surfaceData->backend->imageCreate(surfaceData->backend, (vec2){width, height});
if(imageData)
{
imageData->surface = surface;
image = mg_image_handle_alloc(imageData);
}
}
return(image);
}
void mg_image_destroy(mg_image image)
{
mg_image_data* imageData = mg_image_data_from_handle(image);
if(imageData)
{
mg_surface_data* surface = mg_surface_data_from_handle(imageData->surface);
if(surface && surface->backend)
{
surface->backend->imageDestroy(surface->backend, imageData);
mg_handle_recycle(image.h);
}
}
}
void mg_image_upload_region_rgba8(mg_image image, mp_rect region, u8* pixels)
{
mg_image_data* imageData = mg_image_data_from_handle(image);
if(imageData)
{
mg_surface_data* surfaceData = mg_surface_data_from_handle(imageData->surface);
if(surfaceData)
{
DEBUG_ASSERT(surfaceData->backend);
surfaceData->backend->imageUploadRegion(surfaceData->backend, imageData, region, pixels);
}
}
}
/************************************************************//**
*
* @file: graphics_surface.c
* @author: Martin Fouilleul
* @date: 25/04/2023
*
*****************************************************************/
#include"graphics_surface.h"
//---------------------------------------------------------------
// typed handles functions
//---------------------------------------------------------------
mg_surface mg_surface_handle_alloc(mg_surface_data* surface)
{
mg_surface handle = {.h = mg_handle_alloc(MG_HANDLE_SURFACE, (void*)surface) };
return(handle);
}
mg_surface_data* mg_surface_data_from_handle(mg_surface handle)
{
mg_surface_data* data = mg_data_from_handle(MG_HANDLE_SURFACE, handle.h);
return(data);
}
mg_image mg_image_handle_alloc(mg_image_data* image)
{
mg_image handle = {.h = mg_handle_alloc(MG_HANDLE_IMAGE, (void*)image) };
return(handle);
}
mg_image_data* mg_image_data_from_handle(mg_image handle)
{
mg_image_data* data = mg_data_from_handle(MG_HANDLE_IMAGE, handle.h);
return(data);
}
//---------------------------------------------------------------
// surface API
//---------------------------------------------------------------
#if MG_COMPILE_GL
#if PLATFORM_WINDOWS
#include"wgl_surface.h"
#define gl_surface_create_for_window mg_wgl_surface_create_for_window
#endif
#endif
#if MG_COMPILE_GLES
#include"egl_surface.h"
#endif
#if MG_COMPILE_METAL
#include"mtl_surface.h"
#endif
#if MG_COMPILE_CANVAS
#if PLATFORM_MACOS
mg_surface_data* mtl_canvas_surface_create_for_window(mp_window window);
#elif PLATFORM_WINDOWS
mg_surface_data* gl_canvas_surface_create_for_window(mp_window window);
#endif
#endif
bool mg_is_surface_backend_available(mg_surface_api api)
{
bool result = false;
switch(api)
{
#if MG_COMPILE_METAL
case MG_METAL:
#endif
#if MG_COMPILE_GL
case MG_GL:
#endif
#if MG_COMPILE_GLES
case MG_GLES:
#endif
#if MG_COMPILE_CANVAS
case MG_CANVAS:
#endif
result = true;
break;
default:
break;
}
return(result);
}
mg_surface mg_surface_nil() { return((mg_surface){.h = 0}); }
bool mg_surface_is_nil(mg_surface surface) { return(surface.h == 0); }
mg_surface mg_surface_create_for_window(mp_window window, mg_surface_api api)
{
if(__mgData.init)
{
mg_init();
}
mg_surface surfaceHandle = mg_surface_nil();
mg_surface_data* surface = 0;
switch(api)
{
#if MG_COMPILE_GL
case MG_GL:
surface = gl_surface_create_for_window(window);
break;
#endif
#if MG_COMPILE_GLES
case MG_GLES:
surface = mg_egl_surface_create_for_window(window);
break;
#endif
#if MG_COMPILE_METAL
case MG_METAL:
surface = mg_mtl_surface_create_for_window(window);
break;
#endif
#if MG_COMPILE_CANVAS
case MG_CANVAS:
#if PLATFORM_MACOS
surface = mtl_canvas_surface_create_for_window(window);
#elif PLATFORM_WINDOWS
surface = gl_canvas_surface_create_for_window(window);
#endif
break;
#endif
default:
break;
}
if(surface)
{
surfaceHandle = mg_surface_handle_alloc(surface);
}
return(surfaceHandle);
}
mg_surface mg_surface_create_remote(u32 width, u32 height, mg_surface_api api)
{
if(__mgData.init)
{
mg_init();
}
mg_surface surfaceHandle = mg_surface_nil();
mg_surface_data* surface = 0;
switch(api)
{
#if MG_COMPILE_GLES
case MG_GLES:
surface = mg_egl_surface_create_remote(width, height);
break;
#endif
default:
break;
}
if(surface)
{
surfaceHandle = mg_surface_handle_alloc(surface);
}
return(surfaceHandle);
}
mg_surface mg_surface_create_host(mp_window window)
{
if(__mgData.init)
{
mg_init();
}
mg_surface handle = mg_surface_nil();
mg_surface_data* surface = 0;
#if PLATFORM_MACOS
surface = mg_osx_surface_create_host(window);
#elif PLATFORM_WINDOWS
surface = mg_win32_surface_create_host(window);
#endif
if(surface)
{
handle = mg_surface_handle_alloc(surface);
}
return(handle);
}
void mg_surface_destroy(mg_surface handle)
{
DEBUG_ASSERT(__mgData.init);
mg_surface_data* surface = mg_surface_data_from_handle(handle);
if(surface)
{
if(surface->backend && surface->backend->destroy)
{
surface->backend->destroy(surface->backend);
}
surface->destroy(surface);
mg_handle_recycle(handle.h);
}
}
void mg_surface_prepare(mg_surface surface)
{
DEBUG_ASSERT(__mgData.init);
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->prepare)
{
surfaceData->prepare(surfaceData);
}
}
void mg_surface_present(mg_surface surface)
{
DEBUG_ASSERT(__mgData.init);
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->present)
{
surfaceData->present(surfaceData);
}
}
void mg_surface_swap_interval(mg_surface surface, int swap)
{
DEBUG_ASSERT(__mgData.init);
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->swapInterval)
{
surfaceData->swapInterval(surfaceData, swap);
}
}
vec2 mg_surface_contents_scaling(mg_surface surface)
{
DEBUG_ASSERT(__mgData.init);
vec2 scaling = {1, 1};
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->contentsScaling)
{
scaling = surfaceData->contentsScaling(surfaceData);
}
return(scaling);
}
void mg_surface_set_frame(mg_surface surface, mp_rect frame)
{
DEBUG_ASSERT(__mgData.init);
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->setFrame)
{
surfaceData->setFrame(surfaceData, frame);
}
}
mp_rect mg_surface_get_frame(mg_surface surface)
{
DEBUG_ASSERT(__mgData.init);
mp_rect res = {0};
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->getFrame)
{
res = surfaceData->getFrame(surfaceData);
}
return(res);
}
void mg_surface_set_hidden(mg_surface surface, bool hidden)
{
DEBUG_ASSERT(__mgData.init);
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->setHidden)
{
surfaceData->setHidden(surfaceData, hidden);
}
}
bool mg_surface_get_hidden(mg_surface surface)
{
DEBUG_ASSERT(__mgData.init);
bool res = false;
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->getHidden)
{
res = surfaceData->getHidden(surfaceData);
}
return(res);
}
void* mg_surface_native_layer(mg_surface surface)
{
void* res = 0;
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->nativeLayer)
{
res = surfaceData->nativeLayer(surfaceData);
}
return(res);
}
mg_surface_id mg_surface_remote_id(mg_surface handle)
{
mg_surface_id remoteId = 0;
mg_surface_data* surface = mg_surface_data_from_handle(handle);
if(surface && surface->remoteID)
{
remoteId = surface->remoteID(surface);
}
return(remoteId);
}
void mg_surface_host_connect(mg_surface handle, mg_surface_id remoteID)
{
mg_surface_data* surface = mg_surface_data_from_handle(handle);
if(surface && surface->hostConnect)
{
surface->hostConnect(surface, remoteID);
}
}
void mg_surface_render_commands(mg_surface surface,
mg_color clearColor,
u32 primitiveCount,
mg_primitive* primitives,
u32 eltCount,
mg_path_elt* elements)
{
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->backend)
{
surfaceData->backend->render(surfaceData->backend,
clearColor,
primitiveCount,
primitives,
eltCount,
elements);
}
}
//------------------------------------------------------------------------------------------
//NOTE(martin): images
//------------------------------------------------------------------------------------------
vec2 mg_image_size(mg_image image)
{
vec2 res = {0};
mg_image_data* imageData = mg_image_data_from_handle(image);
if(imageData)
{
res = imageData->size;
}
return(res);
}
mg_image mg_image_create(mg_surface surface, u32 width, u32 height)
{
mg_image image = mg_image_nil();
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->backend)
{
DEBUG_ASSERT(surfaceData->api == MG_CANVAS);
mg_image_data* imageData = surfaceData->backend->imageCreate(surfaceData->backend, (vec2){width, height});
if(imageData)
{
imageData->surface = surface;
image = mg_image_handle_alloc(imageData);
}
}
return(image);
}
void mg_image_destroy(mg_image image)
{
mg_image_data* imageData = mg_image_data_from_handle(image);
if(imageData)
{
mg_surface_data* surface = mg_surface_data_from_handle(imageData->surface);
if(surface && surface->backend)
{
surface->backend->imageDestroy(surface->backend, imageData);
mg_handle_recycle(image.h);
}
}
}
void mg_image_upload_region_rgba8(mg_image image, mp_rect region, u8* pixels)
{
mg_image_data* imageData = mg_image_data_from_handle(image);
if(imageData)
{
mg_surface_data* surfaceData = mg_surface_data_from_handle(imageData->surface);
if(surfaceData)
{
DEBUG_ASSERT(surfaceData->backend);
surfaceData->backend->imageUploadRegion(surfaceData->backend, imageData, region, pixels);
}
}
}

View File

@ -1,158 +1,154 @@
/************************************************************//**
*
* @file: graphics_surface.h
* @author: Martin Fouilleul
* @date: 26/04/2023
*
*****************************************************************/
#ifndef __GRAPHICS_SURFACE_H_
#define __GRAPHICS_SURFACE_H_
#include"graphics_common.h"
#include"mp_app_internal.h"
#ifdef __cplusplus
extern "C" {
#endif
//---------------------------------------------------------------
// surface interface
//---------------------------------------------------------------
typedef struct mg_surface_data mg_surface_data;
typedef struct mg_canvas_backend mg_canvas_backend;
typedef void (*mg_surface_destroy_proc)(mg_surface_data* surface);
typedef void (*mg_surface_prepare_proc)(mg_surface_data* surface);
typedef void (*mg_surface_present_proc)(mg_surface_data* surface);
typedef void (*mg_surface_swap_interval_proc)(mg_surface_data* surface, int swap);
typedef vec2 (*mg_surface_contents_scaling_proc)(mg_surface_data* surface);
typedef mp_rect (*mg_surface_get_frame_proc)(mg_surface_data* surface);
typedef void (*mg_surface_set_frame_proc)(mg_surface_data* surface, mp_rect frame);
typedef bool (*mg_surface_get_hidden_proc)(mg_surface_data* surface);
typedef void (*mg_surface_set_hidden_proc)(mg_surface_data* surface, bool hidden);
typedef void* (*mg_surface_native_layer_proc)(mg_surface_data* surface);
typedef mg_surface_id (*mg_surface_remote_id_proc)(mg_surface_data* surface);
typedef void (*mg_surface_host_connect_proc)(mg_surface_data* surface, mg_surface_id remoteId);
typedef struct mg_surface_data
{
mg_surface_api api;
mp_layer layer;
mg_surface_destroy_proc destroy;
mg_surface_prepare_proc prepare;
mg_surface_present_proc present;
mg_surface_swap_interval_proc swapInterval;
mg_surface_contents_scaling_proc contentsScaling;
mg_surface_get_frame_proc getFrame;
mg_surface_set_frame_proc setFrame;
mg_surface_get_hidden_proc getHidden;
mg_surface_set_hidden_proc setHidden;
mg_surface_native_layer_proc nativeLayer;
mg_surface_remote_id_proc remoteID;
mg_surface_host_connect_proc hostConnect;
mg_canvas_backend* backend;
} mg_surface_data;
mg_surface mg_surface_alloc_handle(mg_surface_data* surface);
mg_surface_data* mg_surface_data_from_handle(mg_surface handle);
void mg_surface_init_for_window(mg_surface_data* surface, mp_window_data* window);
void mg_surface_init_remote(mg_surface_data* surface, u32 width, u32 height);
void mg_surface_init_host(mg_surface_data* surface, mp_window_data* window);
void mg_surface_cleanup(mg_surface_data* surface);
void* mg_surface_native_layer(mg_surface surface);
//---------------------------------------------------------------
// canvas backend interface
//---------------------------------------------------------------
typedef struct mg_image_data
{
list_elt listElt;
u32 generation;
mg_surface surface;
vec2 size;
} mg_image_data;
typedef struct mg_vertex_layout
{
u32 maxVertexCount;
u32 maxIndexCount;
char* posBuffer;
u32 posStride;
char* cubicBuffer;
u32 cubicStride;
char* uvTransformBuffer;
u32 uvTransformStride;
char* colorBuffer;
u32 colorStride;
char* texturedBuffer;
u32 texturedStride;
char* shapeIndexBuffer;
u32 shapeIndexStride;
char* clipBuffer;
u32 clipStride;
char* indexBuffer;
u32 indexStride;
} mg_vertex_layout;
typedef void (*mg_canvas_backend_destroy_proc)(mg_canvas_backend* backend);
typedef void (*mg_canvas_backend_begin_proc)(mg_canvas_backend* backend, mg_color clearColor);
typedef void (*mg_canvas_backend_end_proc)(mg_canvas_backend* backend);
typedef void (*mg_canvas_backend_draw_batch_proc)(mg_canvas_backend* backend,
mg_image_data* imageData,
u32 vertexCount,
u32 shapeCount,
u32 indexCount);
typedef mg_image_data* (*mg_canvas_backend_image_create_proc)(mg_canvas_backend* backend, vec2 size);
typedef void (*mg_canvas_backend_image_destroy_proc)(mg_canvas_backend* backend, mg_image_data* image);
typedef void (*mg_canvas_backend_image_upload_region_proc)(mg_canvas_backend* backend,
mg_image_data* image,
mp_rect region,
u8* pixels);
typedef void (*mg_canvas_backend_render_proc)(mg_canvas_backend* backend,
mg_color clearColor,
u32 primitiveCount,
mg_primitive* primitives,
u32 eltCount,
mg_path_elt* pathElements);
typedef struct mg_canvas_backend
{
mg_vertex_layout vertexLayout;
mg_canvas_backend_destroy_proc destroy;
mg_canvas_backend_begin_proc begin;
mg_canvas_backend_end_proc end;
mg_canvas_backend_draw_batch_proc drawBatch;
mg_canvas_backend_image_create_proc imageCreate;
mg_canvas_backend_image_destroy_proc imageDestroy;
mg_canvas_backend_image_upload_region_proc imageUploadRegion;
mg_canvas_backend_render_proc render;
} mg_canvas_backend;
#ifdef __cplusplus
} // extern "C"
#endif
#endif //__GRAPHICS_SURFACE_H_
/************************************************************//**
*
* @file: graphics_surface.h
* @author: Martin Fouilleul
* @date: 26/04/2023
*
*****************************************************************/
#ifndef __GRAPHICS_SURFACE_H_
#define __GRAPHICS_SURFACE_H_
#include"graphics_common.h"
#include"mp_app_internal.h"
#ifdef __cplusplus
extern "C" {
#endif
//---------------------------------------------------------------
// surface interface
//---------------------------------------------------------------
typedef struct mg_surface_data mg_surface_data;
typedef struct mg_canvas_backend mg_canvas_backend;
typedef void (*mg_surface_destroy_proc)(mg_surface_data* surface);
typedef void (*mg_surface_prepare_proc)(mg_surface_data* surface);
typedef void (*mg_surface_present_proc)(mg_surface_data* surface);
typedef void (*mg_surface_swap_interval_proc)(mg_surface_data* surface, int swap);
typedef vec2 (*mg_surface_contents_scaling_proc)(mg_surface_data* surface);
typedef mp_rect (*mg_surface_get_frame_proc)(mg_surface_data* surface);
typedef void (*mg_surface_set_frame_proc)(mg_surface_data* surface, mp_rect frame);
typedef bool (*mg_surface_get_hidden_proc)(mg_surface_data* surface);
typedef void (*mg_surface_set_hidden_proc)(mg_surface_data* surface, bool hidden);
typedef void* (*mg_surface_native_layer_proc)(mg_surface_data* surface);
typedef mg_surface_id (*mg_surface_remote_id_proc)(mg_surface_data* surface);
typedef void (*mg_surface_host_connect_proc)(mg_surface_data* surface, mg_surface_id remoteId);
typedef struct mg_surface_data
{
mg_surface_api api;
mp_layer layer;
mg_surface_destroy_proc destroy;
mg_surface_prepare_proc prepare;
mg_surface_present_proc present;
mg_surface_swap_interval_proc swapInterval;
mg_surface_contents_scaling_proc contentsScaling;
mg_surface_get_frame_proc getFrame;
mg_surface_set_frame_proc setFrame;
mg_surface_get_hidden_proc getHidden;
mg_surface_set_hidden_proc setHidden;
mg_surface_native_layer_proc nativeLayer;
mg_surface_remote_id_proc remoteID;
mg_surface_host_connect_proc hostConnect;
mg_canvas_backend* backend;
} mg_surface_data;
mg_surface mg_surface_alloc_handle(mg_surface_data* surface);
mg_surface_data* mg_surface_data_from_handle(mg_surface handle);
void mg_surface_init_for_window(mg_surface_data* surface, mp_window_data* window);
void mg_surface_init_remote(mg_surface_data* surface, u32 width, u32 height);
void mg_surface_init_host(mg_surface_data* surface, mp_window_data* window);
void mg_surface_cleanup(mg_surface_data* surface);
void* mg_surface_native_layer(mg_surface surface);
//---------------------------------------------------------------
// canvas backend interface
//---------------------------------------------------------------
typedef struct mg_image_data
{
list_elt listElt;
u32 generation;
mg_surface surface;
vec2 size;
} mg_image_data;
typedef struct mg_vertex_layout
{
u32 maxVertexCount;
u32 maxIndexCount;
char* posBuffer;
u32 posStride;
char* cubicBuffer;
u32 cubicStride;
char* shapeIndexBuffer;
u32 shapeIndexStride;
char* colorBuffer;
u32 colorStride;
char* clipBuffer;
u32 clipStride;
char* uvTransformBuffer;
u32 uvTransformStride;
char* indexBuffer;
u32 indexStride;
} mg_vertex_layout;
typedef void (*mg_canvas_backend_destroy_proc)(mg_canvas_backend* backend);
typedef void (*mg_canvas_backend_begin_proc)(mg_canvas_backend* backend, mg_color clearColor);
typedef void (*mg_canvas_backend_end_proc)(mg_canvas_backend* backend);
typedef void (*mg_canvas_backend_draw_batch_proc)(mg_canvas_backend* backend,
mg_image_data* imageData,
u32 vertexCount,
u32 shapeCount,
u32 indexCount);
typedef mg_image_data* (*mg_canvas_backend_image_create_proc)(mg_canvas_backend* backend, vec2 size);
typedef void (*mg_canvas_backend_image_destroy_proc)(mg_canvas_backend* backend, mg_image_data* image);
typedef void (*mg_canvas_backend_image_upload_region_proc)(mg_canvas_backend* backend,
mg_image_data* image,
mp_rect region,
u8* pixels);
typedef void (*mg_canvas_backend_render_proc)(mg_canvas_backend* backend,
mg_color clearColor,
u32 primitiveCount,
mg_primitive* primitives,
u32 eltCount,
mg_path_elt* pathElements);
typedef struct mg_canvas_backend
{
// mg_vertex_layout vertexLayout;
mg_canvas_backend_destroy_proc destroy;
mg_canvas_backend_begin_proc begin;
mg_canvas_backend_end_proc end;
mg_canvas_backend_draw_batch_proc drawBatch;
mg_canvas_backend_image_create_proc imageCreate;
mg_canvas_backend_image_destroy_proc imageDestroy;
mg_canvas_backend_image_upload_region_proc imageUploadRegion;
mg_canvas_backend_render_proc render;
} mg_canvas_backend;
#ifdef __cplusplus
} // extern "C"
#endif
#endif //__GRAPHICS_SURFACE_H_

File diff suppressed because it is too large Load Diff

View File

@ -1,26 +1,27 @@
/************************************************************//**
*
* @file: platform_math.h
* @author: Martin Fouilleul
* @date: 26/04/2023
*
*****************************************************************/
#ifndef __PLATFORM_MATH_H_
#define __PLATFORM_MATH_H_
#include"platform.h"
#if !PLATFORM_ORCA
#include<math.h>
#else
#define M_PI 3.14159265358979323846
double fabs(double x);
double sqrt(double sqrt);
double cos(double x);
double sin(double x);
#endif
#endif //__PLATFORM_MATH_H_
/************************************************************//**
*
* @file: platform_math.h
* @author: Martin Fouilleul
* @date: 26/04/2023
*
*****************************************************************/
#ifndef __PLATFORM_MATH_H_
#define __PLATFORM_MATH_H_
#include"platform.h"
#if !PLATFORM_ORCA
#define _USE_MATH_DEFINES //NOTE: necessary for MSVC
#include<math.h>
#else
#define M_PI 3.14159265358979323846
double fabs(double x);
double sqrt(double sqrt);
double cos(double x);
double sin(double x);
#endif
#endif //__PLATFORM_MATH_H_

5632
src/ui.c

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -35,6 +35,7 @@ typedef struct win32_app_data
int mouseCaptureMask;
bool mouseTracked;
vec2 lastMousePos;
} win32_app_data;

22
src/win32_manifest.xml Normal file
View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="0.0.0.1"
processorArchitecture="*"
name="forkingpaths.orca.runtime"
type="win32"
/>
<description>Orca Runtime</description>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>