[canvas] removing unused old atlasing api

This commit is contained in:
Martin Fouilleul 2023-02-28 18:47:45 +01:00
parent 3615775168
commit 5a19c03ad6
2 changed files with 4 additions and 275 deletions

View File

@ -3753,39 +3753,12 @@ void mg_image_draw_region(mg_image image, mp_rect srcRegion, mp_rect dstRegion)
} }
} }
void mg_image_draw_region_rounded(mg_image image, mp_rect srcRegion, mp_rect dstRegion, f32 roundness)
{
mg_canvas_data* canvas = __mgCurrentCanvas;
if(canvas)
{
mg_image oldImage = canvas->attributes.image;
mp_rect oldSrcRegion = canvas->attributes.srcRegion;
mg_color oldColor = canvas->attributes.color;
canvas->attributes.image = image;
canvas->attributes.srcRegion = srcRegion;
canvas->attributes.color = (mg_color){1, 1, 1, 1};
mg_primitive primitive = {.cmd = MG_CMD_ROUND_RECT_FILL,
.roundedRect = {dstRegion.x, dstRegion.y, dstRegion.w, dstRegion.h, roundness}};
mg_push_command(canvas, primitive);
}
}
void mg_image_draw(mg_image image, mp_rect rect) void mg_image_draw(mg_image image, mp_rect rect)
{ {
vec2 size = mg_image_size(image); vec2 size = mg_image_size(image);
mg_image_draw_region(image, (mp_rect){0, 0, size.x, size.y}, rect); mg_image_draw_region(image, (mp_rect){0, 0, size.x, size.y}, rect);
} }
void mg_image_draw_rounded(mg_image image, mp_rect rect, f32 roundness)
{
vec2 size = mg_image_size(image);
mg_image_draw_region_rounded(image, (mp_rect){0, 0, size.x, size.y}, rect, roundness);
}
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
//NOTE(martin): atlasing //NOTE(martin): atlasing
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
@ -3889,229 +3862,4 @@ void mg_image_atlas_recycle(mg_rect_atlas* atlas, mg_image_region imageRgn)
mg_rect_atlas_recycle(atlas, imageRgn.rect); mg_rect_atlas_recycle(atlas, imageRgn.rect);
} }
//------------------------------------------------------------------------------------------
//NOTE(martin): image atlas
//------------------------------------------------------------------------------------------
/*
mg_image_atlas mg_image_atlas_nil(){ return((mg_image_atlas){.h = 0}); }
bool mg_image_atlas_is_nil(mg_image_atlas atlas) { return(atlas.h == 0); }
mg_image mg_image_nil(){ return((mg_image){.h = 0}); }
bool mg_image_is_nil(mg_image atlas) { return(atlas.h == 0); }
typedef struct mg_image_atlas_data
{
mg_image image;
vec2 pos;
u32 lineHeight;
} mg_image_atlas_data;
typedef struct mg_image_data
{
mg_image_atlas atlas;
mg_image image;
mp_rect rect;
} mg_image_data;
mg_image_atlas_data* mg_image_atlas_data_from_handle(mg_canvas_data* canvas, mg_image_atlas handle)
{
mg_image_atlas_data* data = 0;
mg_resource_slot* slot = mg_resource_slot_from_handle(&canvas->imageAtlasPool, handle.h);
if(slot)
{
data = slot->atlas;
}
return(data);
}
mg_image_data* mg_image_data_from_handle(mg_canvas_data* canvas, mg_image handle)
{
mg_image_data* data = 0;
mg_resource_slot* slot = mg_resource_slot_from_handle(&canvas->imagePool, handle.h);
if(slot)
{
data = slot->image;
}
return(data);
}
mg_image_atlas mg_image_atlas_create(u32 width, u32 height)
{
mg_image_atlas handle = mg_image_atlas_nil();
mg_canvas_data* canvas = __mgCurrentCanvas;
if(canvas)
{
mg_resource_slot* slot = mg_resource_slot_alloc(&canvas->imageAtlasPool);
if(slot)
{
mg_image_atlas_data* atlas = malloc_type(mg_image_atlas_data);
if(atlas)
{
memset(atlas, 0, sizeof(mg_image_atlas_data));
atlas->image = mg_image_create(width, height);
slot->atlas = atlas;
handle.h = mg_resource_handle_from_slot(&canvas->imageAtlasPool, slot);
}
else
{
mg_resource_slot_recycle(&canvas->imageAtlasPool, slot);
}
}
}
return(handle);
}
void mg_image_atlas_destroy(mg_image_atlas handle)
{
mg_canvas_data* canvas = __mgCurrentCanvas;
if(canvas)
{
mg_image_atlas_data* atlas = mg_image_atlas_data_from_handle(canvas, handle);
if(atlas)
{
mg_image_destroy(atlas->image);
free(atlas);
mg_resource_handle_recycle(&canvas->imageAtlasPool, handle.h);
}
}
}
mp_rect mg_image_atlas_allocate(mg_image_atlas_data* atlas, u32 width, u32 height)
{
mp_rect rect = {0, 0, 0, 0};
vec2 atlasSize = mg_image_size(atlas->image);
if(atlas->pos.x + width >= atlasSize.x)
{
atlas->pos.x = 0;
atlas->pos.y += (atlas->lineHeight + 1);
atlas->lineHeight = 0;
}
if( atlas->pos.x + width < atlasSize.x
&& atlas->pos.y + height < atlasSize.y)
{
rect = (mp_rect){atlas->pos.x, atlas->pos.y, width, height};
atlas->pos.x += (width + 1);
atlas->lineHeight = maximum(atlas->lineHeight, height);
}
return(rect);
}
mg_image mg_image_upload_from_rgba8(mg_image_atlas atlasHandle, u32 width, u32 height, u8* pixels)
{
mg_image res = mg_image_nil();
mg_canvas_data* canvas = __mgCurrentCanvas;
if(canvas)
{
mg_image_atlas_data* atlas = mg_image_atlas_data_from_handle(canvas, atlasHandle);
if(atlas)
{
mg_resource_slot* slot = mg_resource_slot_alloc(&canvas->imagePool);
if(slot)
{
mg_image_data* image = malloc_type(mg_image_data);
if(image)
{
memset(image, 0, sizeof(mg_image_data));
mp_rect rect = mg_image_atlas_allocate(atlas, width, height);
if(rect.w == width && rect.h == height)
{
image->rect = rect;
image->image = atlas->image;
image->atlas = atlasHandle;
mg_image_upload_region_rgba8(atlas->image, rect, pixels);
slot->image = image;
res.h = mg_resource_handle_from_slot(&canvas->imagePool, slot);
}
else
{
free(image);
mg_resource_slot_recycle(&canvas->imagePool, slot);
}
}
else
{
mg_resource_slot_recycle(&canvas->imagePool, slot);
}
}
}
}
return(res);
}
void mg_image_recycle(mg_image handle)
{
mg_canvas_data* canvas = __mgCurrentCanvas;
if(canvas)
{
mg_image_data* image = mg_image_data_from_handle(canvas, handle);
if(image)
{
//TODO recycle rect
free(image);
mg_resource_handle_recycle(&canvas->imagePool, handle.h);
}
}
}
void mg_image_draw(mg_image handle, mp_rect rect)
{
mg_canvas_data* canvas = __mgCurrentCanvas;
if(canvas)
{
mg_image_data* image = mg_image_data_from_handle(canvas, handle);
if(image)
{
mg_image_draw_region(image->image, image->rect, rect);
}
}
}
// helpers
mg_image mg_image_upload_from_data(mg_image_atlas atlas, str8 data, bool flip)
{
mg_image image = mg_image_nil();
int width, height, channels;
stbi_set_flip_vertically_on_load(flip ? 1 : 0);
u8* pixels = stbi_load_from_memory((u8*)data.ptr, data.len, &width, &height, &channels, 4);
if(pixels)
{
image = mg_image_upload_from_rgba8(atlas, width, height, pixels);
free(pixels);
}
return(image);
}
mg_image mg_image_upload_from_file(mg_image_atlas atlas, str8 path, bool flip)
{
mg_image image = mg_image_nil();
int width, height, channels;
const char* cpath = str8_to_cstring(mem_scratch(), path);
stbi_set_flip_vertically_on_load(flip ? 1 : 0);
u8* pixels = stbi_load(cpath, &width, &height, &channels, 4);
if(pixels)
{
image = mg_image_upload_from_rgba8(atlas, width, height, pixels);
free(pixels);
}
return(image);
}
*/
#undef LOG_SUBSYSTEM #undef LOG_SUBSYSTEM

View File

@ -213,11 +213,6 @@ 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 void mg_image_upload_region_rgba8(mg_image image, mp_rect region, u8* pixels);
MP_API vec2 mg_image_size(mg_image image); MP_API vec2 mg_image_size(mg_image image);
MP_API void mg_image_draw_region(mg_image image, mp_rect srcRegion, mp_rect dstRegion);
MP_API void mg_image_draw_region_rounded(mg_image image, mp_rect srcRect, mp_rect dstRegion, f32 roundness);
MP_API void mg_image_draw(mg_image image, mp_rect rect);
MP_API void mg_image_draw_rounded(mg_image image, mp_rect rect, f32 roundness);
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
//NOTE(martin): atlasing //NOTE(martin): atlasing
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
@ -241,24 +236,6 @@ mg_image_region mg_image_atlas_alloc_from_data(mg_rect_atlas* atlas, mg_image ba
mg_image_region mg_image_atlas_alloc_from_file(mg_rect_atlas* atlas, mg_image backingImage, str8 path, bool flip); mg_image_region mg_image_atlas_alloc_from_file(mg_rect_atlas* atlas, mg_image backingImage, str8 path, bool flip);
void mg_image_atlas_recycle(mg_rect_atlas* atlas, mg_image_region imageRgn); void mg_image_atlas_recycle(mg_rect_atlas* atlas, mg_image_region imageRgn);
//------------------------------------------------------------------------------------------
//NOTE(martin): image
//------------------------------------------------------------------------------------------
/*
typedef struct mg_image_atlas { u64 h; } mg_image_atlas;
typedef struct mg_image { u64 h; } mg_image;
MP_API mg_image_atlas mg_image_atlas_create(u32 width, u32 height);
MP_API void mg_image_atlas_destroy(mg_image_atlas atlas);
MP_API mg_image mg_image_upload_from_rgba8(mg_image_atlas atlas, u32 width, u32 height, u8* pixels);
MP_API void mg_image_recycle(mg_image image);
MP_API void mg_image_draw(mg_image image, mp_rect rect);
// helpers
MP_API mg_image mg_image_upload_from_data(mg_image_atlas atlas, str8 data, bool flip);
MP_API mg_image mg_image_upload_from_file(mg_image_atlas atlas, str8 file, bool flip);
*/
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
//NOTE(martin): transform, viewport and clipping //NOTE(martin): transform, viewport and clipping
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
@ -330,4 +307,8 @@ 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_circle_stroke(f32 x, f32 y, f32 r);
MP_API void mg_arc(f32 x, f32 y, f32 r, f32 arcAngle, f32 startAngle); 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_ #endif //__GRAPHICS_H_