graphics: Add gs_effect_loop helper function

This function greatly simplifies the use of effects by making it so you
can call this function in a simple loop.  This reduces boilerplate and
makes drawing with effects much easier.  The gs_effect_loop function
will now automatically handle all the functions required to do drawing.

---------------------
Before:

gs_technique_t *technique = gs_effect_get_technique("technique");

size_t passes = gs_technique_begin(technique);
for (size_t pass = 0; pass < passes; pass++) {
	gs_technique_begin_pass(technique, pass);

	[draw]

	gs_technique_end_pass(technique);
}
gs_technique_end(technique);

---------------------
After:

while (gs_effect_loop(effect, "technique")) {
	[draw]
}
This commit is contained in:
jp9000
2014-11-19 19:12:01 -08:00
parent cdf36cf8ba
commit e994d6d498
3 changed files with 48 additions and 0 deletions

View File

@@ -50,6 +50,46 @@ gs_technique_t *gs_effect_get_current_technique(const gs_effect_t *effect)
return effect->cur_technique;
}
bool gs_effect_loop(gs_effect_t *effect, const char *name)
{
if (!effect) {
return false;
}
if (!effect->looping) {
gs_technique_t *tech;
if (!!gs_get_effect()) {
blog(LOG_WARNING, "gs_effect_loop: An effect is "
"already active");
return false;
}
tech = gs_effect_get_technique(effect, name);
if (!tech) {
blog(LOG_WARNING, "gs_effect_loop: Technique '%s' "
"not found.", name);
return false;
}
gs_technique_begin(tech);
effect->looping = true;
} else {
gs_technique_end_pass(effect->cur_technique);
}
if (!gs_technique_begin_pass(effect->cur_technique,
effect->loop_pass++)) {
gs_technique_end(effect->cur_technique);
effect->looping = false;
effect->loop_pass = 0;
return false;
}
return true;
}
size_t gs_technique_begin(gs_technique_t *tech)
{
if (!tech) return 0;

View File

@@ -147,6 +147,9 @@ struct gs_effect {
gs_eparam_t *view_proj, *world, *scale;
graphics_t *graphics;
size_t loop_pass;
bool looping;
};
static inline void effect_init(gs_effect_t *effect)

View File

@@ -358,6 +358,11 @@ EXPORT gs_eparam_t *gs_effect_get_param_by_idx(const gs_effect_t *effect,
EXPORT gs_eparam_t *gs_effect_get_param_by_name(const gs_effect_t *effect,
const char *name);
/** Helper function to simplify effect usage. Use with a while loop that
* contains drawing functions. Automatically handles techniques, passes, and
* unloading. */
EXPORT bool gs_effect_loop(gs_effect_t *effect, const char *name);
/** used internally */
EXPORT void gs_effect_update_params(gs_effect_t *effect);