[win32] reflected changes to canvas surface interface on GL backend. But implementation still uses the (slower) Loop-Blinn + triangle fan method.

This commit is contained in:
martinfouilleul 2023-05-16 18:06:22 +02:00
parent b6db5107a3
commit 2bec7a633a
4 changed files with 3443 additions and 1886 deletions

View File

@ -1,209 +1,210 @@
/************************************************************//** /************************************************************//**
* *
* @file: main.cpp * @file: main.cpp
* @author: Martin Fouilleul * @author: Martin Fouilleul
* @date: 30/07/2022 * @date: 30/07/2022
* @revision: * @revision:
* *
*****************************************************************/ *****************************************************************/
#include<stdlib.h> #include<stdlib.h>
#include<string.h> #include<stdio.h>
#include<errno.h> #include<string.h>
#include<errno.h>
#define _USE_MATH_DEFINES //NOTE: necessary for MSVC
#include<math.h> #define _USE_MATH_DEFINES //NOTE: necessary for MSVC
#include<math.h>
#include"milepost.h"
#include"milepost.h"
#define LOG_SUBSYSTEM "Main"
#define LOG_SUBSYSTEM "Main"
mg_font create_font()
{ mg_font create_font()
//NOTE(martin): create font {
str8 fontPath = mp_app_get_resource_path(mem_scratch(), "../resources/OpenSansLatinSubset.ttf"); //NOTE(martin): create font
char* fontPathCString = str8_to_cstring(mem_scratch(), fontPath); str8 fontPath = mp_app_get_resource_path(mem_scratch(), "../resources/OpenSansLatinSubset.ttf");
char* fontPathCString = str8_to_cstring(mem_scratch(), fontPath);
FILE* fontFile = fopen(fontPathCString, "r");
if(!fontFile) FILE* fontFile = fopen(fontPathCString, "r");
{ if(!fontFile)
log_error("Could not load font file '%s': %s\n", fontPathCString, strerror(errno)); {
return(mg_font_nil()); log_error("Could not load font file '%s': %s\n", fontPathCString, strerror(errno));
} return(mg_font_nil());
unsigned char* fontData = 0; }
fseek(fontFile, 0, SEEK_END); unsigned char* fontData = 0;
u32 fontDataSize = ftell(fontFile); fseek(fontFile, 0, SEEK_END);
rewind(fontFile); u32 fontDataSize = ftell(fontFile);
fontData = (unsigned char*)malloc(fontDataSize); rewind(fontFile);
fread(fontData, 1, fontDataSize, fontFile); fontData = (unsigned char*)malloc(fontDataSize);
fclose(fontFile); fread(fontData, 1, fontDataSize, fontFile);
fclose(fontFile);
unicode_range ranges[5] = {UNICODE_RANGE_BASIC_LATIN,
UNICODE_RANGE_C1_CONTROLS_AND_LATIN_1_SUPPLEMENT, unicode_range ranges[5] = {UNICODE_RANGE_BASIC_LATIN,
UNICODE_RANGE_LATIN_EXTENDED_A, UNICODE_RANGE_C1_CONTROLS_AND_LATIN_1_SUPPLEMENT,
UNICODE_RANGE_LATIN_EXTENDED_B, UNICODE_RANGE_LATIN_EXTENDED_A,
UNICODE_RANGE_SPECIALS}; UNICODE_RANGE_LATIN_EXTENDED_B,
UNICODE_RANGE_SPECIALS};
mg_font font = mg_font_create_from_memory(fontDataSize, fontData, 5, ranges);
free(fontData); mg_font font = mg_font_create_from_memory(fontDataSize, fontData, 5, ranges);
free(fontData);
return(font);
} return(font);
}
int main()
{ int main()
mp_init(); {
mp_clock_init(); //TODO put that in mp_init()? mp_init();
mp_clock_init(); //TODO put that in mp_init()?
mp_rect windowRect = {.x = 100, .y = 100, .w = 810, .h = 610};
mp_window window = mp_window_create(windowRect, "test", 0); mp_rect windowRect = {.x = 100, .y = 100, .w = 810, .h = 610};
mp_window window = mp_window_create(windowRect, "test", 0);
mp_rect contentRect = mp_window_get_content_rect(window);
mp_rect contentRect = mp_window_get_content_rect(window);
//NOTE: create surface
mg_surface surface = mg_surface_create_for_window(window, MG_CANVAS); //NOTE: create surface
mg_surface_swap_interval(surface, 0); mg_surface surface = mg_surface_create_for_window(window, MG_CANVAS);
mg_surface_swap_interval(surface, 0);
mg_canvas canvas = mg_canvas_create();
mg_canvas canvas = mg_canvas_create();
if(mg_canvas_is_nil(canvas))
{ if(mg_canvas_is_nil(canvas))
printf("Error: couldn't create canvas\n"); {
return(-1); printf("Error: couldn't create canvas\n");
} return(-1);
}
mg_font font = create_font();
mg_font font = create_font();
// start app
mp_window_bring_to_front(window); // start app
mp_window_focus(window); mp_window_bring_to_front(window);
mp_window_focus(window);
f32 x = 400, y = 300;
f32 speed = 0; f32 x = 400, y = 300;
f32 dx = speed, dy = speed; f32 speed = 0;
f64 frameTime = 0; f32 dx = speed, dy = speed;
f64 frameTime = 0;
while(!mp_should_quit())
{ while(!mp_should_quit())
f64 startTime = mp_get_time(MP_CLOCK_MONOTONIC); {
f64 startTime = mp_get_time(MP_CLOCK_MONOTONIC);
mp_pump_events(0);
mp_event* event = 0; mp_pump_events(0);
while((event = mp_next_event(mem_scratch())) != 0) mp_event* event = 0;
{ while((event = mp_next_event(mem_scratch())) != 0)
switch(event->type) {
{ switch(event->type)
case MP_EVENT_WINDOW_CLOSE: {
{ case MP_EVENT_WINDOW_CLOSE:
mp_request_quit(); {
} break; mp_request_quit();
} break;
case MP_EVENT_KEYBOARD_KEY:
{ case MP_EVENT_KEYBOARD_KEY:
if(event->key.action == MP_KEY_PRESS || event->key.action == MP_KEY_REPEAT) {
{ if(event->key.action == MP_KEY_PRESS || event->key.action == MP_KEY_REPEAT)
f32 factor = (event->key.mods & MP_KEYMOD_SHIFT) ? 10 : 1; {
f32 factor = (event->key.mods & MP_KEYMOD_SHIFT) ? 10 : 1;
if(event->key.code == MP_KEY_LEFT)
{ if(event->key.code == MP_KEY_LEFT)
x-=0.3*factor; {
} x-=0.3*factor;
else if(event->key.code == MP_KEY_RIGHT) }
{ else if(event->key.code == MP_KEY_RIGHT)
x+=0.3*factor; {
} x+=0.3*factor;
else if(event->key.code == MP_KEY_UP) }
{ else if(event->key.code == MP_KEY_UP)
y-=0.3*factor; {
} y-=0.3*factor;
else if(event->key.code == MP_KEY_DOWN) }
{ else if(event->key.code == MP_KEY_DOWN)
y+=0.3*factor; {
} y+=0.3*factor;
} }
} break; }
} break;
default:
break; default:
} break;
} }
}
if(x-200 < 0)
{ if(x-200 < 0)
x = 200; {
dx = speed; x = 200;
} dx = speed;
if(x+200 > contentRect.w) }
{ if(x+200 > contentRect.w)
x = contentRect.w - 200; {
dx = -speed; x = contentRect.w - 200;
} dx = -speed;
if(y-200 < 0) }
{ if(y-200 < 0)
y = 200; {
dy = speed; y = 200;
} dy = speed;
if(y+200 > contentRect.h) }
{ if(y+200 > contentRect.h)
y = contentRect.h - 200; {
dy = -speed; y = contentRect.h - 200;
} dy = -speed;
x += dx; }
y += dy; x += dx;
y += dy;
// background
mg_set_color_rgba(0, 1, 1, 1); // background
mg_clear(); mg_set_color_rgba(0, 1, 1, 1);
mg_clear();
// head
mg_set_color_rgba(1, 1, 0, 1); // head
mg_set_color_rgba(1, 1, 0, 1);
mg_circle_fill(x, y, 200);
mg_circle_fill(x, y, 200);
// smile
f32 frown = frameTime > 0.033 ? -100 : 0; // smile
f32 frown = frameTime > 0.033 ? -100 : 0;
mg_set_color_rgba(0, 0, 0, 1);
mg_set_width(20); mg_set_color_rgba(0, 0, 0, 1);
mg_move_to(x-100, y+100); mg_set_width(20);
mg_cubic_to(x-50, y+150+frown, x+50, y+150+frown, x+100, y+100); mg_move_to(x-100, y+100);
mg_stroke(); 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); // eyes
mg_ellipse_fill(x+70, y-50, 30, 50); 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); // text
mg_set_font(font); mg_set_color_rgba(0, 0, 1, 1);
mg_set_font_size(12); mg_set_font(font);
mg_move_to(50, 600-50); mg_set_font_size(12);
mg_move_to(50, 600-50);
str8 text = str8_pushf(mem_scratch(),
"Milepost vector graphics test program (frame time = %fs, fps = %f)...", str8 text = str8_pushf(mem_scratch(),
frameTime, "Milepost vector graphics test program (frame time = %fs, fps = %f)...",
1./frameTime); frameTime,
mg_text_outlines(text); 1./frameTime);
mg_fill(); mg_text_outlines(text);
mg_fill();
printf("Milepost vector graphics test program (frame time = %fs, fps = %f)...\n",
frameTime, printf("Milepost vector graphics test program (frame time = %fs, fps = %f)...\n",
1./frameTime); frameTime,
1./frameTime);
mg_surface_prepare(surface);
mg_render(surface, canvas); mg_surface_prepare(surface);
mg_surface_present(surface); mg_render(surface, canvas);
mg_surface_present(surface);
mem_arena_clear(mem_scratch());
frameTime = mp_get_time(MP_CLOCK_MONOTONIC) - startTime; mem_arena_clear(mem_scratch());
} frameTime = mp_get_time(MP_CLOCK_MONOTONIC) - startTime;
}
mg_font_destroy(font);
mg_canvas_destroy(canvas); mg_font_destroy(font);
mg_surface_destroy(surface); mg_canvas_destroy(canvas);
mp_window_destroy(window); mg_surface_destroy(surface);
mp_window_destroy(window);
mp_terminate();
mp_terminate();
return(0);
} return(0);
}

File diff suppressed because it is too large Load Diff

View File

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

File diff suppressed because it is too large Load Diff