From 371bc5f677b3c763407fc79f01cb9d0c598199cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Mon, 25 Dec 2017 16:42:35 +0100 Subject: Add everything from sdl_blend_mode.h --- c_src/esdl2.h | 17 ++++++++++ c_src/sdl_blend_mode.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ c_src/sdl_renderer.c | 9 ------ 3 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 c_src/sdl_blend_mode.c (limited to 'c_src') diff --git a/c_src/esdl2.h b/c_src/esdl2.h index 8fcca31..2993549 100644 --- a/c_src/esdl2.h +++ b/c_src/esdl2.h @@ -38,6 +38,8 @@ A(crosshair) \ A(data) \ A(direction) \ + A(dst_alpha) \ + A(dst_color) \ A(enter) \ A(error) \ A(event) \ @@ -60,6 +62,7 @@ A(ibeam) \ A(input_focus) \ A(input_grabbed) \ + A(invalid) \ A(joystick) \ A(key_down) \ A(key_up) \ @@ -70,9 +73,15 @@ A(left_gui) \ A(left_shift) \ A(ok) \ + A(one_minus_dst_alpha) \ + A(one_minus_dst_color) \ + A(one_minus_src_alpha) \ + A(one_minus_src_color) \ A(maximized) \ + A(maximum) \ A(middle) \ A(minimized) \ + A(minimum) \ A(mod) \ A(mode) \ A(mouse_capture) \ @@ -88,6 +97,7 @@ A(normal) \ A(num) \ A(on_battery) \ + A(one) \ A(opengl) \ A(present_vsync) \ A(quit) \ @@ -95,6 +105,7 @@ A(resizable) \ A(resized) \ A(restored) \ + A(rev_substract) \ A(right) \ A(right_alt) \ A(right_ctrl) \ @@ -109,7 +120,10 @@ A(size_nwse) \ A(size_we) \ A(software) \ + A(src_alpha) \ + A(src_color) \ A(state) \ + A(substract) \ A(sym) \ A(target_texture) \ A(touch) \ @@ -134,6 +148,7 @@ A(xrel) \ A(y) \ A(yrel) \ + A(zero) \ A(_nif_thread_ret_) // List of resources used by this NIF. @@ -160,6 +175,8 @@ F(quit_subsystem, 1) \ F(set_main_ready, 0) \ F(was_init, 1) \ + /* sdl_blendmode */ \ + F(compose_custom_blend_mode, 6) \ /* sdl_clipboard */ \ F(get_clipboard_text, 0) \ F(has_clipboard_text, 0) \ diff --git a/c_src/sdl_blend_mode.c b/c_src/sdl_blend_mode.c new file mode 100644 index 0000000..be83aaf --- /dev/null +++ b/c_src/sdl_blend_mode.c @@ -0,0 +1,84 @@ +// Copyright (c) 2017, Loïc Hoguin +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +#include "esdl2.h" + +#define BLEND_MODE_ENUM(E) \ + E(none, SDL_BLENDMODE_NONE) \ + E(blend, SDL_BLENDMODE_BLEND) \ + E(add, SDL_BLENDMODE_ADD) \ + E(mod, SDL_BLENDMODE_MOD) \ + E(invalid, SDL_BLENDMODE_INVALID) + +NIF_ATOM_TO_ENUM_FUNCTION(atom_to_blend_mode, SDL_BlendMode, BLEND_MODE_ENUM) +NIF_ENUM_TO_ATOM_FUNCTION(blend_mode_to_atom, SDL_BlendMode, BLEND_MODE_ENUM) + +#define BLEND_OPERATION_ENUM(E) \ + E(add, SDL_BLENDOPERATION_ADD) \ + E(substract, SDL_BLENDOPERATION_SUBTRACT) \ + E(rev_substract, SDL_BLENDOPERATION_REV_SUBTRACT) \ + E(minimum, SDL_BLENDOPERATION_MINIMUM) \ + E(maximum, SDL_BLENDOPERATION_MAXIMUM) + +static NIF_ATOM_TO_ENUM_FUNCTION(atom_to_blend_operation, SDL_BlendOperation, BLEND_OPERATION_ENUM) + +#define BLEND_FACTOR_ENUM(E) \ + E(zero, SDL_BLENDFACTOR_ZERO) \ + E(one, SDL_BLENDFACTOR_ONE) \ + E(src_color, SDL_BLENDFACTOR_SRC_COLOR) \ + E(one_minus_src_color, SDL_BLENDFACTOR_ONE_MINUS_SRC_COLOR) \ + E(src_alpha, SDL_BLENDFACTOR_SRC_ALPHA) \ + E(one_minus_src_alpha, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA) \ + E(dst_color, SDL_BLENDFACTOR_DST_COLOR) \ + E(one_minus_dst_color, SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR) \ + E(dst_alpha, SDL_BLENDFACTOR_DST_ALPHA) \ + E(one_minus_dst_alpha, SDL_BLENDFACTOR_ONE_MINUS_DST_ALPHA) + +static NIF_ATOM_TO_ENUM_FUNCTION(atom_to_blend_factor, SDL_BlendFactor, BLEND_FACTOR_ENUM) + +// compose_custom_blend_mode + +NIF_CALL_HANDLER(thread_compose_custom_blend_mode) +{ + SDL_BlendMode mode; + ERL_NIF_TERM term; + + mode = SDL_ComposeCustomBlendMode( + (long)args[0], (long)args[1], (long)args[2], + (long)args[3], (long)args[4], (long)args[5]); + + term = blend_mode_to_atom(mode); + + if (!enif_is_identical(term, atom_undefined)) + return term; + + return enif_make_int(env, mode); +} + +NIF_FUNCTION(compose_custom_blend_mode) +{ + SDL_BlendFactor srcColorFactor, dstColorFactor, srcAlphaFactor, dstAlphaFactor; + SDL_BlendOperation colorOp, alphaOp; + + BADARG_IF(!atom_to_blend_factor(env, argv[0], &srcColorFactor)); + BADARG_IF(!atom_to_blend_factor(env, argv[1], &dstColorFactor)); + BADARG_IF(!atom_to_blend_operation(env, argv[2], &colorOp)); + BADARG_IF(!atom_to_blend_factor(env, argv[3], &srcAlphaFactor)); + BADARG_IF(!atom_to_blend_factor(env, argv[4], &dstAlphaFactor)); + BADARG_IF(!atom_to_blend_operation(env, argv[5], &alphaOp)); + + return nif_thread_call(env, thread_compose_custom_blend_mode, 6, + srcColorFactor, dstColorFactor, colorOp, + srcAlphaFactor, dstAlphaFactor, alphaOp); +} diff --git a/c_src/sdl_renderer.c b/c_src/sdl_renderer.c index 23b41ca..4ab350f 100644 --- a/c_src/sdl_renderer.c +++ b/c_src/sdl_renderer.c @@ -30,15 +30,6 @@ void dtor_Renderer(ErlNifEnv* env, void* obj) static NIF_LIST_TO_FLAGS_FUNCTION(list_to_renderer_flags, Uint32, RENDERER_FLAGS) -#define BLEND_MODE_ENUM(E) \ - E(none, SDL_BLENDMODE_NONE) \ - E(blend, SDL_BLENDMODE_BLEND) \ - E(add, SDL_BLENDMODE_ADD) \ - E(mod, SDL_BLENDMODE_MOD) - -NIF_ATOM_TO_ENUM_FUNCTION(atom_to_blend_mode, SDL_BlendMode, BLEND_MODE_ENUM) -NIF_ENUM_TO_ATOM_FUNCTION(blend_mode_to_atom, SDL_BlendMode, BLEND_MODE_ENUM) - #define FLIP_FLAGS(F) \ F(none, SDL_FLIP_NONE) \ F(horizontal, SDL_FLIP_HORIZONTAL) \ -- cgit v1.2.3