Alphish / gm-community-toolbox

A collection of utility functions for GameMaker.
MIT License
34 stars 6 forks source link

vertex_save_buffer() and vertex_load_buffer() #16

Open Gizmo199 opened 1 year ago

Gizmo199 commented 1 year ago

Very useful when working with 3D. Especially if you are importing .obj files and want to cache them for quicker load times after the initial import.

/// @func vertex_load_buffer(filename, vertex_format)
/// @arg filename
/// @arg vertex_format
/// @desc loads a vertex buffer file 
/// @returns {Vertex Buffer ID}
function vertex_load_buffer(_filename, _format){
    var _buffer = buffer_load(_filename);
    var _vbuff  = vertex_create_buffer_from_buffer(_buffer, _format);
    buffer_delete(_buffer);
    return _vbuff;
}

/// @func vertex_save_buffer(filename, vertex_buffer_id)
/// @arg filename
/// @arg vertex_buffer_id
/// @desc saves a vertex buffer file
/// @returns {undefined}
function vertex_save_buffer(_filename, _vertex_buffer){
    var _buffer = buffer_create_from_vertex_buffer(_vertex_buffer, buffer_grow, 1);
    buffer_save(_buffer, _filename);
    buffer_delete(_buffer);
}
Alphish commented 1 year ago

Not sure how broadly applicable this one is, it's not something I needed to use personally, at least. 3D is a pretty niche area to begin with, but I can also accept that some 3D-adjacent functionalities may work pretty well in 2D games too, and vertex buffers are one of these.

If enough people upvote this one, I don't mind getting this added, especially with the functionality being pretty straightforward and hardly any similar/better alternatives being available.

I was considering names like vertex_buffer_save and vertex_buffer_load instead, but I see that built-in functions have ordering like vertex_create_buffer(...), so I don't mind either way.

Gizmo199 commented 1 year ago

No worries. Yeah I just named it similar to how its named in the IDE. :) I just know I use these a lot so I figured I would share. :)

tabularelf commented 1 year ago

Only downside I can see with this is that they're not truly crossplatform compliant. (Console/HTML5) You'll need to provide asynchronous counterparts in, and without getting into the complex bits, requires holding onto a buffer of some kind for both loading and saving.