reorganizing milepost directory tree into app, graphics, platfrom, ui, util

This commit is contained in:
Martin Fouilleul 2023-08-09 17:30:35 +02:00
parent 11dde9fea3
commit bfadcad7f3
51 changed files with 11159 additions and 11155 deletions

View File

@ -10,7 +10,7 @@
#define __OSX_APP_H_
#include"mp_app.h"
#include"graphics.h"
#include"graphics/graphics.h"
#ifdef __OBJC__
#import<Cocoa/Cocoa.h>

View File

@ -18,7 +18,7 @@
#include"macro_helpers.h"
#include"platform_log.h"
#include"platform_clock.h"
#include"graphics_surface.h"
#include"graphics/graphics_surface.h"
#include"mp_app.c"

File diff suppressed because it is too large Load Diff

View File

@ -1,28 +1,28 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 10 GUID -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
<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>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 10 GUID -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
<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>

View File

@ -10,7 +10,7 @@
#define EGL_EGLEXT_PROTOTYPES
#include<EGL/egl.h>
#include<EGL/eglext.h>
#include"mp_app_internal.h"
#include"app/mp_app_internal.h"
#include"graphics_surface.h"
#include"gl_loader.h"

View File

@ -10,7 +10,7 @@
#define __EGL_SURFACE_H_
#include"graphics_surface.h"
#include"mp_app.h"
#include"app/mp_app.h"
mg_surface_data* mg_egl_surface_create_for_window(mp_window window);
mg_surface_data* mg_egl_surface_create_remote(u32 width, u32 height);

File diff suppressed because it is too large Load Diff

View File

@ -1,330 +1,330 @@
/************************************************************//**
*
* @file: graphics.h
* @author: Martin Fouilleul
* @date: 23/01/2023
* @revision:
*
*****************************************************************/
#ifndef __GRAPHICS_H_
#define __GRAPHICS_H_
#include"util/typedefs.h"
#include"platform/platform.h"
#include"mp_app.h"
//------------------------------------------------------------------------------------------
//NOTE(martin): backends selection
//------------------------------------------------------------------------------------------
typedef enum {
MG_NONE,
MG_METAL,
MG_GL,
MG_GLES,
MG_CANVAS,
MG_HOST } mg_surface_api;
//NOTE: these macros are used to select which backend to include when building milepost
// they can be overridden by passing them to the compiler command line
#if PLATFORM_MACOS
#ifndef MG_COMPILE_METAL
#define MG_COMPILE_METAL 1
#endif
#ifndef MG_COMPILE_GLES
#define MG_COMPILE_GLES 1
#endif
#ifndef MG_COMPILE_CANVAS
#if !MG_COMPILE_METAL
#error "Canvas surface requires a Metal backend on macOS. Make sure you define MG_COMPILE_METAL to 1."
#endif
#define MG_COMPILE_CANVAS 1
#endif
#define MG_COMPILE_GL 0
#elif PLATFORM_WINDOWS
#ifndef MG_COMPILE_GL
#define MG_COMPILE_GL 1
#endif
#ifndef MG_COMPILE_GLES
#define MG_COMPILE_GLES 1
#endif
#ifndef MG_COMPILE_CANVAS
#if !MG_COMPILE_GL
#error "Canvas surface requires an OpenGL backend on Windows. Make sure you define MG_COMPILE_GL to 1."
#endif
#define MG_COMPILE_CANVAS 1
#endif
#elif PLATFORM_LINUX
#ifndef MG_COMPILE_GL
#define MG_COMPILE_GL 1
#endif
#ifndef MG_COMPILE_CANVAS
#if !MG_COMPILE_GL
#error "Canvas surface requires an OpenGL backend on Linux. Make sure you define MG_COMPILE_GL to 1."
#endif
#define MG_COMPILE_CANVAS 1
#endif
#endif
//NOTE: these macros are used to select backend-specific APIs to include when using milepost
#ifdef MG_EXPOSE_SURFACE_METAL
#include"mtl_surface.h"
#endif
#ifdef MG_EXPOSE_SURFACE_WGL
#include"wgl_surface.h"
#endif
//TODO: expose nsgl surface when supported, expose egl surface, etc...
//TODO: add MG_INCLUDE_OPENGL/GLES/etc, once we know how we make different gl versions co-exist
MP_API bool mg_is_surface_api_available(mg_surface_api api);
//------------------------------------------------------------------------------------------
//NOTE(martin): graphics surface
//------------------------------------------------------------------------------------------
typedef struct mg_surface { u64 h; } mg_surface;
MP_API mg_surface mg_surface_nil(void);
MP_API bool mg_surface_is_nil(mg_surface surface);
MP_API mg_surface mg_surface_create_for_window(mp_window window, mg_surface_api api);
MP_API void mg_surface_destroy(mg_surface surface);
MP_API void mg_surface_prepare(mg_surface surface);
MP_API void mg_surface_present(mg_surface surface);
MP_API void mg_surface_deselect(void);
MP_API void mg_surface_swap_interval(mg_surface surface, int swap);
MP_API vec2 mg_surface_get_size(mg_surface surface);
MP_API vec2 mg_surface_contents_scaling(mg_surface surface);
MP_API bool mg_surface_get_hidden(mg_surface surface);
MP_API void mg_surface_set_hidden(mg_surface surface, bool hidden);
//NOTE(martin): surface sharing
typedef u64 mg_surface_id;
MP_API mg_surface mg_surface_create_remote(u32 width, u32 height, mg_surface_api api);
MP_API mg_surface mg_surface_create_host(mp_window window);
MP_API mg_surface_id mg_surface_remote_id(mg_surface surface);
MP_API void mg_surface_host_connect(mg_surface surface, mg_surface_id remoteId);
//------------------------------------------------------------------------------------------
//NOTE(martin): graphics canvas structs
//------------------------------------------------------------------------------------------
typedef struct mg_canvas { u64 h; } mg_canvas;
typedef struct mg_font { u64 h; } mg_font;
typedef struct mg_image { u64 h; } mg_image;
typedef struct mg_mat2x3
{
f32 m[6];
} mg_mat2x3;
typedef struct mg_color
{
union
{
struct
{
f32 r;
f32 g;
f32 b;
f32 a;
};
f32 c[4];
};
} mg_color;
typedef enum {MG_JOINT_MITER = 0,
MG_JOINT_BEVEL,
MG_JOINT_NONE } mg_joint_type;
typedef enum {MG_CAP_NONE = 0,
MG_CAP_SQUARE } mg_cap_type;
typedef struct mg_font_extents
{
f32 ascent; // the extent above the baseline (by convention a positive value extends above the baseline)
f32 descent; // the extent below the baseline (by convention, positive value extends below the baseline)
f32 leading; // spacing between one row's descent and the next row's ascent
f32 xHeight; // height of the lower case letter 'x'
f32 capHeight; // height of the upper case letter 'M'
f32 width; // maximum width of the font
} mg_font_extents;
typedef struct mg_text_extents
{
f32 xBearing;
f32 yBearing;
f32 width;
f32 height;
f32 xAdvance;
f32 yAdvance;
} mg_text_extents;
//------------------------------------------------------------------------------------------
//NOTE(martin): graphics canvas
//------------------------------------------------------------------------------------------
MP_API mg_canvas mg_canvas_nil(void);
MP_API bool mg_canvas_is_nil(mg_canvas canvas);
MP_API mg_canvas mg_canvas_create(void);
MP_API void mg_canvas_destroy(mg_canvas canvas);
MP_API mg_canvas mg_canvas_set_current(mg_canvas canvas);
MP_API void mg_render(mg_surface surface, mg_canvas canvas);
//------------------------------------------------------------------------------------------
//NOTE(martin): fonts
//------------------------------------------------------------------------------------------
MP_API mg_font mg_font_nil(void);
MP_API mg_font mg_font_create_from_memory(u32 size, byte* buffer, u32 rangeCount, unicode_range* ranges);
MP_API void mg_font_destroy(mg_font font);
//NOTE(martin): the following int valued functions return -1 if font is invalid or codepoint is not present in font//
//TODO(martin): add enum error codes
MP_API mg_font_extents mg_font_get_extents(mg_font font);
MP_API mg_font_extents mg_font_get_scaled_extents(mg_font font, f32 emSize);
MP_API f32 mg_font_get_scale_for_em_pixels(mg_font font, f32 emSize);
//NOTE(martin): if you need to process more than one codepoint, first convert your codepoints to glyph indices, then use the
// glyph index versions of the functions, which can take an array of glyph indices.
MP_API str32 mg_font_get_glyph_indices(mg_font font, str32 codePoints, str32 backing);
MP_API str32 mg_font_push_glyph_indices(mg_font font, mem_arena* arena, str32 codePoints);
MP_API u32 mg_font_get_glyph_index(mg_font font, utf32 codePoint);
MP_API int mg_font_get_codepoint_extents(mg_font font, utf32 codePoint, mg_text_extents* outExtents);
MP_API int mg_font_get_glyph_extents(mg_font font, str32 glyphIndices, mg_text_extents* outExtents);
MP_API mp_rect mg_text_bounding_box_utf32(mg_font font, f32 fontSize, str32 text);
MP_API mp_rect mg_text_bounding_box(mg_font font, f32 fontSize, str8 text);
//------------------------------------------------------------------------------------------
//NOTE(martin): images
//------------------------------------------------------------------------------------------
MP_API mg_image mg_image_nil(void);
MP_API bool mg_image_is_nil(mg_image a);
MP_API mg_image mg_image_create(mg_surface surface, u32 width, u32 height);
MP_API mg_image mg_image_create_from_rgba8(mg_surface surface, u32 width, u32 height, u8* pixels);
MP_API mg_image mg_image_create_from_data(mg_surface surface, str8 data, bool flip);
MP_API mg_image mg_image_create_from_file(mg_surface surface, str8 path, bool flip);
MP_API void mg_image_destroy(mg_image image);
MP_API void mg_image_upload_region_rgba8(mg_image image, mp_rect region, u8* pixels);
MP_API vec2 mg_image_size(mg_image image);
//------------------------------------------------------------------------------------------
//NOTE(martin): atlasing
//------------------------------------------------------------------------------------------
//NOTE: rectangle allocator
typedef struct mg_rect_atlas mg_rect_atlas;
MP_API mg_rect_atlas* mg_rect_atlas_create(mem_arena* arena, i32 width, i32 height);
MP_API mp_rect mg_rect_atlas_alloc(mg_rect_atlas* atlas, i32 width, i32 height);
MP_API void mg_rect_atlas_recycle(mg_rect_atlas* atlas, mp_rect rect);
//NOTE: image atlas helpers
typedef struct mg_image_region
{
mg_image image;
mp_rect rect;
} mg_image_region;
MP_API mg_image_region mg_image_atlas_alloc_from_rgba8(mg_rect_atlas* atlas, mg_image backingImage, u32 width, u32 height, u8* pixels);
MP_API mg_image_region mg_image_atlas_alloc_from_data(mg_rect_atlas* atlas, mg_image backingImage, str8 data, bool flip);
MP_API mg_image_region mg_image_atlas_alloc_from_file(mg_rect_atlas* atlas, mg_image backingImage, str8 path, bool flip);
MP_API void mg_image_atlas_recycle(mg_rect_atlas* atlas, mg_image_region imageRgn);
//------------------------------------------------------------------------------------------
//NOTE(martin): transform, viewport and clipping
//------------------------------------------------------------------------------------------
MP_API void mg_viewport(mp_rect viewPort);
MP_API void mg_matrix_push(mg_mat2x3 matrix);
MP_API void mg_matrix_pop(void);
MP_API void mg_clip_push(f32 x, f32 y, f32 w, f32 h);
MP_API void mg_clip_pop(void);
//------------------------------------------------------------------------------------------
//NOTE(martin): graphics attributes setting/getting
//------------------------------------------------------------------------------------------
MP_API void mg_set_color(mg_color color);
MP_API void mg_set_color_rgba(f32 r, f32 g, f32 b, f32 a);
MP_API void mg_set_width(f32 width);
MP_API void mg_set_tolerance(f32 tolerance);
MP_API void mg_set_joint(mg_joint_type joint);
MP_API void mg_set_max_joint_excursion(f32 maxJointExcursion);
MP_API void mg_set_cap(mg_cap_type cap);
MP_API void mg_set_font(mg_font font);
MP_API void mg_set_font_size(f32 size);
MP_API void mg_set_text_flip(bool flip);
MP_API void mg_set_image(mg_image image);
MP_API void mg_set_image_source_region(mp_rect region);
MP_API mg_color mg_get_color(void);
MP_API f32 mg_get_width(void);
MP_API f32 mg_get_tolerance(void);
MP_API mg_joint_type mg_get_joint(void);
MP_API f32 mg_get_max_joint_excursion(void);
MP_API mg_cap_type mg_get_cap(void);
MP_API mg_font mg_get_font(void);
MP_API f32 mg_get_font_size(void);
MP_API bool mg_get_text_flip(void);
//------------------------------------------------------------------------------------------
//NOTE(martin): path construction
//------------------------------------------------------------------------------------------
MP_API vec2 mg_get_position(void);
MP_API void mg_move_to(f32 x, f32 y);
MP_API void mg_line_to(f32 x, f32 y);
MP_API void mg_quadratic_to(f32 x1, f32 y1, f32 x2, f32 y2);
MP_API void mg_cubic_to(f32 x1, f32 y1, f32 x2, f32 y2, f32 x3, f32 y3);
MP_API void mg_close_path(void);
MP_API mp_rect mg_glyph_outlines(str32 glyphIndices);
MP_API void mg_codepoints_outlines(str32 string);
MP_API void mg_text_outlines(str8 string);
//------------------------------------------------------------------------------------------
//NOTE(martin): clear/fill/stroke
//------------------------------------------------------------------------------------------
MP_API void mg_clear(void);
MP_API void mg_fill(void);
MP_API void mg_stroke(void);
//------------------------------------------------------------------------------------------
//NOTE(martin): 'fast' shapes primitives
//------------------------------------------------------------------------------------------
MP_API void mg_rectangle_fill(f32 x, f32 y, f32 w, f32 h);
MP_API void mg_rectangle_stroke(f32 x, f32 y, f32 w, f32 h);
MP_API void mg_rounded_rectangle_fill(f32 x, f32 y, f32 w, f32 h, f32 r);
MP_API void mg_rounded_rectangle_stroke(f32 x, f32 y, f32 w, f32 h, f32 r);
MP_API void mg_ellipse_fill(f32 x, f32 y, f32 rx, f32 ry);
MP_API void mg_ellipse_stroke(f32 x, f32 y, f32 rx, f32 ry);
MP_API void mg_circle_fill(f32 x, f32 y, f32 r);
MP_API void mg_circle_stroke(f32 x, f32 y, f32 r);
MP_API void mg_arc(f32 x, f32 y, f32 r, f32 arcAngle, f32 startAngle);
//NOTE: image helpers
MP_API void mg_image_draw(mg_image image, mp_rect rect);
MP_API void mg_image_draw_region(mg_image image, mp_rect srcRegion, mp_rect dstRegion);
#endif //__GRAPHICS_H_
/************************************************************//**
*
* @file: graphics.h
* @author: Martin Fouilleul
* @date: 23/01/2023
* @revision:
*
*****************************************************************/
#ifndef __GRAPHICS_H_
#define __GRAPHICS_H_
#include"util/typedefs.h"
#include"platform/platform.h"
#include"app/mp_app.h"
//------------------------------------------------------------------------------------------
//NOTE(martin): backends selection
//------------------------------------------------------------------------------------------
typedef enum {
MG_NONE,
MG_METAL,
MG_GL,
MG_GLES,
MG_CANVAS,
MG_HOST } mg_surface_api;
//NOTE: these macros are used to select which backend to include when building milepost
// they can be overridden by passing them to the compiler command line
#if PLATFORM_MACOS
#ifndef MG_COMPILE_METAL
#define MG_COMPILE_METAL 1
#endif
#ifndef MG_COMPILE_GLES
#define MG_COMPILE_GLES 1
#endif
#ifndef MG_COMPILE_CANVAS
#if !MG_COMPILE_METAL
#error "Canvas surface requires a Metal backend on macOS. Make sure you define MG_COMPILE_METAL to 1."
#endif
#define MG_COMPILE_CANVAS 1
#endif
#define MG_COMPILE_GL 0
#elif PLATFORM_WINDOWS
#ifndef MG_COMPILE_GL
#define MG_COMPILE_GL 1
#endif
#ifndef MG_COMPILE_GLES
#define MG_COMPILE_GLES 1
#endif
#ifndef MG_COMPILE_CANVAS
#if !MG_COMPILE_GL
#error "Canvas surface requires an OpenGL backend on Windows. Make sure you define MG_COMPILE_GL to 1."
#endif
#define MG_COMPILE_CANVAS 1
#endif
#elif PLATFORM_LINUX
#ifndef MG_COMPILE_GL
#define MG_COMPILE_GL 1
#endif
#ifndef MG_COMPILE_CANVAS
#if !MG_COMPILE_GL
#error "Canvas surface requires an OpenGL backend on Linux. Make sure you define MG_COMPILE_GL to 1."
#endif
#define MG_COMPILE_CANVAS 1
#endif
#endif
//NOTE: these macros are used to select backend-specific APIs to include when using milepost
#ifdef MG_EXPOSE_SURFACE_METAL
#include"mtl_surface.h"
#endif
#ifdef MG_EXPOSE_SURFACE_WGL
#include"wgl_surface.h"
#endif
//TODO: expose nsgl surface when supported, expose egl surface, etc...
//TODO: add MG_INCLUDE_OPENGL/GLES/etc, once we know how we make different gl versions co-exist
MP_API bool mg_is_surface_api_available(mg_surface_api api);
//------------------------------------------------------------------------------------------
//NOTE(martin): graphics surface
//------------------------------------------------------------------------------------------
typedef struct mg_surface { u64 h; } mg_surface;
MP_API mg_surface mg_surface_nil(void);
MP_API bool mg_surface_is_nil(mg_surface surface);
MP_API mg_surface mg_surface_create_for_window(mp_window window, mg_surface_api api);
MP_API void mg_surface_destroy(mg_surface surface);
MP_API void mg_surface_prepare(mg_surface surface);
MP_API void mg_surface_present(mg_surface surface);
MP_API void mg_surface_deselect(void);
MP_API void mg_surface_swap_interval(mg_surface surface, int swap);
MP_API vec2 mg_surface_get_size(mg_surface surface);
MP_API vec2 mg_surface_contents_scaling(mg_surface surface);
MP_API bool mg_surface_get_hidden(mg_surface surface);
MP_API void mg_surface_set_hidden(mg_surface surface, bool hidden);
//NOTE(martin): surface sharing
typedef u64 mg_surface_id;
MP_API mg_surface mg_surface_create_remote(u32 width, u32 height, mg_surface_api api);
MP_API mg_surface mg_surface_create_host(mp_window window);
MP_API mg_surface_id mg_surface_remote_id(mg_surface surface);
MP_API void mg_surface_host_connect(mg_surface surface, mg_surface_id remoteId);
//------------------------------------------------------------------------------------------
//NOTE(martin): graphics canvas structs
//------------------------------------------------------------------------------------------
typedef struct mg_canvas { u64 h; } mg_canvas;
typedef struct mg_font { u64 h; } mg_font;
typedef struct mg_image { u64 h; } mg_image;
typedef struct mg_mat2x3
{
f32 m[6];
} mg_mat2x3;
typedef struct mg_color
{
union
{
struct
{
f32 r;
f32 g;
f32 b;
f32 a;
};
f32 c[4];
};
} mg_color;
typedef enum {MG_JOINT_MITER = 0,
MG_JOINT_BEVEL,
MG_JOINT_NONE } mg_joint_type;
typedef enum {MG_CAP_NONE = 0,
MG_CAP_SQUARE } mg_cap_type;
typedef struct mg_font_extents
{
f32 ascent; // the extent above the baseline (by convention a positive value extends above the baseline)
f32 descent; // the extent below the baseline (by convention, positive value extends below the baseline)
f32 leading; // spacing between one row's descent and the next row's ascent
f32 xHeight; // height of the lower case letter 'x'
f32 capHeight; // height of the upper case letter 'M'
f32 width; // maximum width of the font
} mg_font_extents;
typedef struct mg_text_extents
{
f32 xBearing;
f32 yBearing;
f32 width;
f32 height;
f32 xAdvance;
f32 yAdvance;
} mg_text_extents;
//------------------------------------------------------------------------------------------
//NOTE(martin): graphics canvas
//------------------------------------------------------------------------------------------
MP_API mg_canvas mg_canvas_nil(void);
MP_API bool mg_canvas_is_nil(mg_canvas canvas);
MP_API mg_canvas mg_canvas_create(void);
MP_API void mg_canvas_destroy(mg_canvas canvas);
MP_API mg_canvas mg_canvas_set_current(mg_canvas canvas);
MP_API void mg_render(mg_surface surface, mg_canvas canvas);
//------------------------------------------------------------------------------------------
//NOTE(martin): fonts
//------------------------------------------------------------------------------------------
MP_API mg_font mg_font_nil(void);
MP_API mg_font mg_font_create_from_memory(u32 size, byte* buffer, u32 rangeCount, unicode_range* ranges);
MP_API void mg_font_destroy(mg_font font);
//NOTE(martin): the following int valued functions return -1 if font is invalid or codepoint is not present in font//
//TODO(martin): add enum error codes
MP_API mg_font_extents mg_font_get_extents(mg_font font);
MP_API mg_font_extents mg_font_get_scaled_extents(mg_font font, f32 emSize);
MP_API f32 mg_font_get_scale_for_em_pixels(mg_font font, f32 emSize);
//NOTE(martin): if you need to process more than one codepoint, first convert your codepoints to glyph indices, then use the
// glyph index versions of the functions, which can take an array of glyph indices.
MP_API str32 mg_font_get_glyph_indices(mg_font font, str32 codePoints, str32 backing);
MP_API str32 mg_font_push_glyph_indices(mg_font font, mem_arena* arena, str32 codePoints);
MP_API u32 mg_font_get_glyph_index(mg_font font, utf32 codePoint);
MP_API int mg_font_get_codepoint_extents(mg_font font, utf32 codePoint, mg_text_extents* outExtents);
MP_API int mg_font_get_glyph_extents(mg_font font, str32 glyphIndices, mg_text_extents* outExtents);
MP_API mp_rect mg_text_bounding_box_utf32(mg_font font, f32 fontSize, str32 text);
MP_API mp_rect mg_text_bounding_box(mg_font font, f32 fontSize, str8 text);
//------------------------------------------------------------------------------------------
//NOTE(martin): images
//------------------------------------------------------------------------------------------
MP_API mg_image mg_image_nil(void);
MP_API bool mg_image_is_nil(mg_image a);
MP_API mg_image mg_image_create(mg_surface surface, u32 width, u32 height);
MP_API mg_image mg_image_create_from_rgba8(mg_surface surface, u32 width, u32 height, u8* pixels);
MP_API mg_image mg_image_create_from_data(mg_surface surface, str8 data, bool flip);
MP_API mg_image mg_image_create_from_file(mg_surface surface, str8 path, bool flip);
MP_API void mg_image_destroy(mg_image image);
MP_API void mg_image_upload_region_rgba8(mg_image image, mp_rect region, u8* pixels);
MP_API vec2 mg_image_size(mg_image image);
//------------------------------------------------------------------------------------------
//NOTE(martin): atlasing
//------------------------------------------------------------------------------------------
//NOTE: rectangle allocator
typedef struct mg_rect_atlas mg_rect_atlas;
MP_API mg_rect_atlas* mg_rect_atlas_create(mem_arena* arena, i32 width, i32 height);
MP_API mp_rect mg_rect_atlas_alloc(mg_rect_atlas* atlas, i32 width, i32 height);
MP_API void mg_rect_atlas_recycle(mg_rect_atlas* atlas, mp_rect rect);
//NOTE: image atlas helpers
typedef struct mg_image_region
{
mg_image image;
mp_rect rect;
} mg_image_region;
MP_API mg_image_region mg_image_atlas_alloc_from_rgba8(mg_rect_atlas* atlas, mg_image backingImage, u32 width, u32 height, u8* pixels);
MP_API mg_image_region mg_image_atlas_alloc_from_data(mg_rect_atlas* atlas, mg_image backingImage, str8 data, bool flip);
MP_API mg_image_region mg_image_atlas_alloc_from_file(mg_rect_atlas* atlas, mg_image backingImage, str8 path, bool flip);
MP_API void mg_image_atlas_recycle(mg_rect_atlas* atlas, mg_image_region imageRgn);
//------------------------------------------------------------------------------------------
//NOTE(martin): transform, viewport and clipping
//------------------------------------------------------------------------------------------
MP_API void mg_viewport(mp_rect viewPort);
MP_API void mg_matrix_push(mg_mat2x3 matrix);
MP_API void mg_matrix_pop(void);
MP_API void mg_clip_push(f32 x, f32 y, f32 w, f32 h);
MP_API void mg_clip_pop(void);
//------------------------------------------------------------------------------------------
//NOTE(martin): graphics attributes setting/getting
//------------------------------------------------------------------------------------------
MP_API void mg_set_color(mg_color color);
MP_API void mg_set_color_rgba(f32 r, f32 g, f32 b, f32 a);
MP_API void mg_set_width(f32 width);
MP_API void mg_set_tolerance(f32 tolerance);
MP_API void mg_set_joint(mg_joint_type joint);
MP_API void mg_set_max_joint_excursion(f32 maxJointExcursion);
MP_API void mg_set_cap(mg_cap_type cap);
MP_API void mg_set_font(mg_font font);
MP_API void mg_set_font_size(f32 size);
MP_API void mg_set_text_flip(bool flip);
MP_API void mg_set_image(mg_image image);
MP_API void mg_set_image_source_region(mp_rect region);
MP_API mg_color mg_get_color(void);
MP_API f32 mg_get_width(void);
MP_API f32 mg_get_tolerance(void);
MP_API mg_joint_type mg_get_joint(void);
MP_API f32 mg_get_max_joint_excursion(void);
MP_API mg_cap_type mg_get_cap(void);
MP_API mg_font mg_get_font(void);
MP_API f32 mg_get_font_size(void);
MP_API bool mg_get_text_flip(void);
//------------------------------------------------------------------------------------------
//NOTE(martin): path construction
//------------------------------------------------------------------------------------------
MP_API vec2 mg_get_position(void);
MP_API void mg_move_to(f32 x, f32 y);
MP_API void mg_line_to(f32 x, f32 y);
MP_API void mg_quadratic_to(f32 x1, f32 y1, f32 x2, f32 y2);
MP_API void mg_cubic_to(f32 x1, f32 y1, f32 x2, f32 y2, f32 x3, f32 y3);
MP_API void mg_close_path(void);
MP_API mp_rect mg_glyph_outlines(str32 glyphIndices);
MP_API void mg_codepoints_outlines(str32 string);
MP_API void mg_text_outlines(str8 string);
//------------------------------------------------------------------------------------------
//NOTE(martin): clear/fill/stroke
//------------------------------------------------------------------------------------------
MP_API void mg_clear(void);
MP_API void mg_fill(void);
MP_API void mg_stroke(void);
//------------------------------------------------------------------------------------------
//NOTE(martin): 'fast' shapes primitives
//------------------------------------------------------------------------------------------
MP_API void mg_rectangle_fill(f32 x, f32 y, f32 w, f32 h);
MP_API void mg_rectangle_stroke(f32 x, f32 y, f32 w, f32 h);
MP_API void mg_rounded_rectangle_fill(f32 x, f32 y, f32 w, f32 h, f32 r);
MP_API void mg_rounded_rectangle_stroke(f32 x, f32 y, f32 w, f32 h, f32 r);
MP_API void mg_ellipse_fill(f32 x, f32 y, f32 rx, f32 ry);
MP_API void mg_ellipse_stroke(f32 x, f32 y, f32 rx, f32 ry);
MP_API void mg_circle_fill(f32 x, f32 y, f32 r);
MP_API void mg_circle_stroke(f32 x, f32 y, f32 r);
MP_API void mg_arc(f32 x, f32 y, f32 r, f32 arcAngle, f32 startAngle);
//NOTE: image helpers
MP_API void mg_image_draw(mg_image image, mp_rect rect);
MP_API void mg_image_draw_region(mg_image image, mp_rect srcRegion, mp_rect dstRegion);
#endif //__GRAPHICS_H_

View File

@ -1,82 +1,82 @@
/************************************************************//**
*
* @file: graphics_common.h
* @author: Martin Fouilleul
* @date: 26/04/2023
*
*****************************************************************/
#ifndef __GRAPHICS_COMMON_H_
#define __GRAPHICS_COMMON_H_
#include"graphics.h"
//------------------------------------------------------------------------
// canvas structs
//------------------------------------------------------------------------
typedef enum { MG_PATH_MOVE,
MG_PATH_LINE,
MG_PATH_QUADRATIC,
MG_PATH_CUBIC } mg_path_elt_type;
typedef struct mg_path_elt
{
mg_path_elt_type type;
vec2 p[3];
} mg_path_elt;
typedef struct mg_path_descriptor
{
u32 startIndex;
u32 count;
vec2 startPoint;
} mg_path_descriptor;
typedef struct mg_attributes
{
f32 width;
f32 tolerance;
mg_color color;
mg_joint_type joint;
f32 maxJointExcursion;
mg_cap_type cap;
mg_font font;
f32 fontSize;
mg_image image;
mp_rect srcRegion;
mg_mat2x3 transform;
mp_rect clip;
} mg_attributes;
typedef enum { MG_CMD_FILL,
MG_CMD_STROKE,
MG_CMD_JUMP
} mg_primitive_cmd;
typedef struct mg_primitive
{
mg_primitive_cmd cmd;
mg_attributes attributes;
union
{
mg_path_descriptor path;
mp_rect rect;
u32 jump;
};
} mg_primitive;
MP_API void mg_surface_render_commands(mg_surface surface,
mg_color clearColor,
u32 primitiveCount,
mg_primitive* primitives,
u32 eltCount,
mg_path_elt* elements);
#endif //__GRAPHICS_COMMON_H_
/************************************************************//**
*
* @file: graphics_common.h
* @author: Martin Fouilleul
* @date: 26/04/2023
*
*****************************************************************/
#ifndef __GRAPHICS_COMMON_H_
#define __GRAPHICS_COMMON_H_
#include"graphics.h"
//------------------------------------------------------------------------
// canvas structs
//------------------------------------------------------------------------
typedef enum { MG_PATH_MOVE,
MG_PATH_LINE,
MG_PATH_QUADRATIC,
MG_PATH_CUBIC } mg_path_elt_type;
typedef struct mg_path_elt
{
mg_path_elt_type type;
vec2 p[3];
} mg_path_elt;
typedef struct mg_path_descriptor
{
u32 startIndex;
u32 count;
vec2 startPoint;
} mg_path_descriptor;
typedef struct mg_attributes
{
f32 width;
f32 tolerance;
mg_color color;
mg_joint_type joint;
f32 maxJointExcursion;
mg_cap_type cap;
mg_font font;
f32 fontSize;
mg_image image;
mp_rect srcRegion;
mg_mat2x3 transform;
mp_rect clip;
} mg_attributes;
typedef enum { MG_CMD_FILL,
MG_CMD_STROKE,
MG_CMD_JUMP
} mg_primitive_cmd;
typedef struct mg_primitive
{
mg_primitive_cmd cmd;
mg_attributes attributes;
union
{
mg_path_descriptor path;
mp_rect rect;
u32 jump;
};
} mg_primitive;
MP_API void mg_surface_render_commands(mg_surface surface,
mg_color clearColor,
u32 primitiveCount,
mg_primitive* primitives,
u32 eltCount,
mg_path_elt* elements);
#endif //__GRAPHICS_COMMON_H_

View File

@ -1,454 +1,454 @@
/************************************************************//**
*
* @file: graphics_surface.c
* @author: Martin Fouilleul
* @date: 25/04/2023
*
*****************************************************************/
#include"graphics_surface.h"
//---------------------------------------------------------------
// per-thread selected surface
//---------------------------------------------------------------
mp_thread_local mg_surface __mgSelectedSurface = {0};
//---------------------------------------------------------------
// 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);
mg_surface_prepare(surfaceHandle);
}
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);
mg_surface_prepare(surfaceHandle);
}
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(__mgSelectedSurface.h == handle.h)
{
mg_surface_deselect();
}
if(surface->backend && surface->backend->destroy)
{
surface->backend->destroy(surface->backend);
}
surface->destroy(surface);
mg_handle_recycle(handle.h);
}
}
void mg_surface_deselect()
{
DEBUG_ASSERT(__mgData.init);
mg_surface_data* prevSurface = mg_surface_data_from_handle(__mgSelectedSurface);
if(prevSurface && prevSurface->deselect)
{
prevSurface->deselect(prevSurface);
}
__mgSelectedSurface = mg_surface_nil();
}
void mg_surface_prepare(mg_surface surface)
{
DEBUG_ASSERT(__mgData.init);
if(surface.h != __mgSelectedSurface.h)
{
mg_surface_deselect();
}
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->prepare)
{
surfaceData->prepare(surfaceData);
__mgSelectedSurface = surface;
}
}
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_get_size(mg_surface surface)
{
DEBUG_ASSERT(__mgData.init);
vec2 size = {0};
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->getSize)
{
size = surfaceData->getSize(surfaceData);
}
return(size);
}
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_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(surface.h != __mgSelectedSurface.h)
{
log_error("surface is not selected. Make sure to call mg_surface_prepare() before drawing onto a surface.\n");
}
else 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(surface.h != __mgSelectedSurface.h)
{
log_error("surface is not selected. Make sure to call mg_surface_prepare() before modifying graphics resources.\n");
}
else 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)
{
if(imageData->surface.h != __mgSelectedSurface.h)
{
log_error("surface is not selected. Make sure to call mg_surface_prepare() before modifying graphics resources.\n");
}
else
{
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)
{
if(imageData->surface.h != __mgSelectedSurface.h)
{
log_error("surface is not selected. Make sure to call mg_surface_prepare() before modifying graphics resources.\n");
}
else
{
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"
//---------------------------------------------------------------
// per-thread selected surface
//---------------------------------------------------------------
mp_thread_local mg_surface __mgSelectedSurface = {0};
//---------------------------------------------------------------
// 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);
mg_surface_prepare(surfaceHandle);
}
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);
mg_surface_prepare(surfaceHandle);
}
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(__mgSelectedSurface.h == handle.h)
{
mg_surface_deselect();
}
if(surface->backend && surface->backend->destroy)
{
surface->backend->destroy(surface->backend);
}
surface->destroy(surface);
mg_handle_recycle(handle.h);
}
}
void mg_surface_deselect()
{
DEBUG_ASSERT(__mgData.init);
mg_surface_data* prevSurface = mg_surface_data_from_handle(__mgSelectedSurface);
if(prevSurface && prevSurface->deselect)
{
prevSurface->deselect(prevSurface);
}
__mgSelectedSurface = mg_surface_nil();
}
void mg_surface_prepare(mg_surface surface)
{
DEBUG_ASSERT(__mgData.init);
if(surface.h != __mgSelectedSurface.h)
{
mg_surface_deselect();
}
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->prepare)
{
surfaceData->prepare(surfaceData);
__mgSelectedSurface = surface;
}
}
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_get_size(mg_surface surface)
{
DEBUG_ASSERT(__mgData.init);
vec2 size = {0};
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->getSize)
{
size = surfaceData->getSize(surfaceData);
}
return(size);
}
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_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(surface.h != __mgSelectedSurface.h)
{
log_error("surface is not selected. Make sure to call mg_surface_prepare() before drawing onto a surface.\n");
}
else 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(surface.h != __mgSelectedSurface.h)
{
log_error("surface is not selected. Make sure to call mg_surface_prepare() before modifying graphics resources.\n");
}
else 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)
{
if(imageData->surface.h != __mgSelectedSurface.h)
{
log_error("surface is not selected. Make sure to call mg_surface_prepare() before modifying graphics resources.\n");
}
else
{
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)
{
if(imageData->surface.h != __mgSelectedSurface.h)
{
log_error("surface is not selected. Make sure to call mg_surface_prepare() before modifying graphics resources.\n");
}
else
{
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,112 +1,112 @@
/************************************************************//**
*
* @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_deselect_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_get_size_proc)(mg_surface_data* surface);
typedef vec2 (*mg_surface_contents_scaling_proc)(mg_surface_data* surface);
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_deselect_proc deselect;
mg_surface_swap_interval_proc swapInterval;
mg_surface_get_size_proc getSize;
mg_surface_contents_scaling_proc contentsScaling;
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 void (*mg_canvas_backend_destroy_proc)(mg_canvas_backend* backend);
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_canvas_backend_destroy_proc destroy;
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"app/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_deselect_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_get_size_proc)(mg_surface_data* surface);
typedef vec2 (*mg_surface_contents_scaling_proc)(mg_surface_data* surface);
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_deselect_proc deselect;
mg_surface_swap_interval_proc swapInterval;
mg_surface_get_size_proc getSize;
mg_surface_contents_scaling_proc contentsScaling;
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 void (*mg_canvas_backend_destroy_proc)(mg_canvas_backend* backend);
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_canvas_backend_destroy_proc destroy;
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_

View File

@ -12,8 +12,8 @@
#include<simd/simd.h>
#include"graphics_surface.h"
#include"macro_helpers.h"
#include"osx_app.h"
#include"util/macro_helpers.h"
#include"app/osx_app.h"
typedef struct mg_mtl_surface
{

View File

@ -6,12 +6,12 @@
* @revision:
*
*****************************************************************/
#include"win32_app.h"
#include"app/win32_app.h"
#include"graphics_surface.h"
#include"gl_loader.h"
#include<GL/wglext.h>
#include"macro_helpers.h"
#include"util/macro_helpers.h"
#define WGL_PROC_LIST \
WGL_PROC(WGLCHOOSEPIXELFORMATARB, wglChoosePixelFormatARB) \

View File

@ -60,24 +60,24 @@
//---------------------------------------------------------------
#if PLATFORM_WINDOWS
#include"win32_app.c"
#include"graphics_common.c"
#include"graphics_surface.c"
#include"app/win32_app.c"
#include"graphics/graphics_common.c"
#include"graphics/graphics_surface.c"
#if MG_COMPILE_GL || MG_COMPILE_GLES
#include"gl_loader.c"
#include"graphics/gl_loader.c"
#endif
#if MG_COMPILE_GL
#include"wgl_surface.c"
#include"graphics/wgl_surface.c"
#endif
#if MG_COMPILE_CANVAS
#include"gl_canvas.c"
#include"graphics/gl_canvas.c"
#endif
#if MG_COMPILE_GLES
#include"egl_surface.c"
#include"graphics/egl_surface.c"
#endif
#elif PLATFORM_MACOS
@ -86,5 +86,5 @@
#error "Unsupported platform"
#endif
#include"input_state.c"
#include"ui.c"
#include"ui/input_state.c"
#include"ui/ui.c"

View File

@ -38,15 +38,13 @@
//----------------------------------------------------------------
// application/graphics layer
//----------------------------------------------------------------
#include"mp_app.h"
#include"graphics.h"
#include"input_state.h"
#include"ui.h"
#include"app/mp_app.h"
#include"graphics/graphics.h"
#include"ui/input_state.h"
#include"ui/ui.h"
#ifdef MG_INCLUDE_GL_API
#include"gl_api.h"
#include"graphics/gl_api.h"
#endif
//#include"ui.h"
#endif //__MILEPOST_H_

View File

@ -7,20 +7,20 @@
*
*****************************************************************/
#include"osx_path.m"
#include"osx_app.m"
#include"graphics_common.c"
#include"graphics_surface.c"
#include"platform/osx_path.m"
#include"app/osx_app.m"
#include"graphics/graphics_common.c"
#include"graphics/graphics_surface.c"
#if MG_COMPILE_METAL
#include"mtl_surface.m"
#include"graphics/mtl_surface.m"
#endif
#if MG_COMPILE_CANVAS
#include"mtl_renderer.m"
#include"graphics/mtl_renderer.m"
#endif
#if MG_COMPILE_GLES
#include"gl_loader.c"
#include"egl_surface.c"
#include"graphics/gl_loader.c"
#include"graphics/egl_surface.c"
#endif

View File

@ -12,7 +12,7 @@
#include"util/typedefs.h"
#include"util/strings.h"
#include"util/utf8.h"
#include"mp_app.h"
#include"app/mp_app.h"
typedef struct mp_key_state
{

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,4 @@
#include <keys.h>
#include <graphics.h>
#include <math.h>
#include <orca.h>

View File

@ -11,6 +11,7 @@ else
CLANG=clang
fi
ORCA_DIR=../..
STDLIB_DIR=../../cstdlib
ORCA_SDK_DIR=../../sdk
MILEPOST_DIR=../../milepost
@ -25,6 +26,7 @@ wasmFlags="--target=wasm32 \
-mbulk-memory \
-D__ORCA__ \
-I $STDLIB_DIR/include \
-I $ORCA_DIR/ext \
-I $ORCA_SDK_DIR \
-I $MILEPOST_DIR/ext -I $MILEPOST_DIR -I $MILEPOST_DIR/src"

View File

@ -1,5 +1,4 @@
#include <keys.h>
#include <graphics.h>
#include <math.h>
#include <orca.h>

View File

@ -11,6 +11,8 @@ else
CLANG=clang
fi
ORCA_DIR=../..
wasmFlags="--target=wasm32 \
--no-standard-libraries \
-fno-builtin \
@ -20,6 +22,7 @@ wasmFlags="--target=wasm32 \
-O2 \
-mbulk-memory \
-D__ORCA__ \
-I $ORCA_DIR/ext \
-isystem ../../cstdlib/include -I ../../sdk -I../../milepost/ext -I ../../milepost -I ../../milepost/src"
$CLANG $wasmFlags -o ./module.wasm ../../sdk/orca.c ../../cstdlib/src/*.c src/main.c

View File

@ -1,6 +1,4 @@
#include"keys.h"
#include"graphics.h"
#include"ui.h"
#include"orca.h"

View File

@ -168,7 +168,7 @@ def build_milepost_lib_mac(release):
# TODO: shaderFlagParam
"-fno-fast-math", "-c",
"-o", "../build/mtl_renderer.air",
"src/mtl_renderer.metal",
"src/graphics/mtl_renderer.metal",
], check=True)
subprocess.run([
"xcrun", "-sdk", "macosx", "metallib",
@ -302,7 +302,8 @@ def build_orca_win(release):
includes = [
"/I", "src",
"/I", "sdk",
"/I", "ext"
"/I", "ext",
"/I", "ext/angle/include",
"/I", "ext/wasm3/source",
"/I", "milepost/src"
]
@ -332,6 +333,7 @@ def build_orca_mac(release):
"-Imilepost/src/util",
"-Imilepost/src/platform",
"-Iext",
"-Iext/angle/include",
"-Iext/wasm3/source"
]
libs = ["-Lbuild/bin", "-Lbuild/lib", "-lmilepost", "-lwasm3"]
@ -380,12 +382,12 @@ def gen_all_bindings():
bindgen("canvas", "src/canvas_api.json",
guest_stubs="sdk/orca_surface.c",
guest_include="graphics.h",
guest_include="graphics/graphics.h",
wasm3_bindings="src/canvas_api_bind_gen.c",
)
bindgen("clock", "src/clock_api.json",
guest_stubs="sdk/orca_clock.c",
guest_include="platform_clock.h",
guest_include="platform/platform_clock.h",
wasm3_bindings="src/clock_api_bind_gen.c",
)
bindgen("io", "src/io_api.json",

View File

@ -18,10 +18,10 @@
#include"util/strings.c"
#include"util/utf8.c"
#include"graphics_common.c"
#include"input_state.c"
#include"graphics/graphics_common.c"
#include"ui/input_state.c"
#include"ui/ui.c"
#include"orca_exports.c"
#include"orca_surface.c"
#include"ui.c"
#include"io_stubs.c"

View File

@ -21,7 +21,10 @@
#include"math.h"
#include"graphics.h"
#include"graphics/graphics.h"
#include"ui/input_state.h"
#include"ui/ui.h"
#include"gl31.h"
#if COMPILER_CLANG

View File

@ -11,7 +11,7 @@
#define MG_INCLUDE_GL_API
#include"milepost.h"
#include"graphics_common.h"
#include"graphics/graphics_common.h"
#include"orca_app.h"