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 --- README.asciidoc | 2 +- c_src/esdl2.h | 17 ++++++++++ c_src/sdl_blend_mode.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ c_src/sdl_renderer.c | 9 ------ ebin/esdl2.app | 2 +- src/esdl2.erl | 8 +++++ src/sdl_blend_mode.erl | 36 ++++++++++++++++++++++ src/sdl_renderer.erl | 7 ++--- 8 files changed, 149 insertions(+), 16 deletions(-) create mode 100644 c_src/sdl_blend_mode.c create mode 100644 src/sdl_blend_mode.erl diff --git a/README.asciidoc b/README.asciidoc index f4707bc..e5e47bc 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -9,13 +9,13 @@ corresponding to the public headers. == Fully implemented * 'SDL.h' +* 'SDL_blendmode.h' * 'SDL_mouse.h' * 'SDL_filesystem.h' * 'SDL_power.h' == Partially implemented -* 'SDL_blendmode.h': `SDL_BlendMode` is currently located in `sdl_renderer`. Move it. Everything else is missing. * 'SDL_clipboard.h': We currently do not support UTF-8. We should probably switch to binaries as input/output to support it. * 'SDL_cpuinfo.h': `SDL_HasAVX2` and `SDL_HasNEON` must be implemented. * 'SDL_events.h': Most of it is missing. 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) \ diff --git a/ebin/esdl2.app b/ebin/esdl2.app index 92ddcc9..ccb9664 100644 --- a/ebin/esdl2.app +++ b/ebin/esdl2.app @@ -1,7 +1,7 @@ {application, 'esdl2', [ {description, "SDL2 Erlang NIF."}, {vsn, "0.1.0"}, - {modules, ['esdl2','esdl2_app','esdl2_callbacks','esdl2_sup','sdl','sdl_clipboard','sdl_cpu_info','sdl_cursor','sdl_events','sdl_filesystem','sdl_gl','sdl_hints','sdl_keyboard','sdl_mouse','sdl_power','sdl_renderer','sdl_surface','sdl_texture','sdl_version','sdl_window']}, + {modules, ['esdl2','esdl2_app','esdl2_callbacks','esdl2_sup','sdl','sdl_blend_mode','sdl_clipboard','sdl_cpu_info','sdl_cursor','sdl_events','sdl_filesystem','sdl_gl','sdl_hints','sdl_keyboard','sdl_mouse','sdl_power','sdl_renderer','sdl_surface','sdl_texture','sdl_version','sdl_window']}, {registered, [esdl2_sup]}, {applications, [kernel,stdlib]}, {mod, {esdl2_app, []}}, diff --git a/src/esdl2.erl b/src/esdl2.erl index e18a7fc..815c12c 100644 --- a/src/esdl2.erl +++ b/src/esdl2.erl @@ -25,6 +25,9 @@ -export([set_main_ready/0]). -export([was_init/1]). +%% sdl_blendmode +-export([compose_custom_blend_mode/6]). + %% sdl_clipboard -export([get_clipboard_text/0]). -export([has_clipboard_text/0]). @@ -200,6 +203,11 @@ set_main_ready() -> was_init(_) -> erlang:nif_error({not_loaded, ?MODULE}). +%% sdl_blendmode + +compose_custom_blend_mode(_, _, _, _, _, _) -> + erlang:nif_error({not_loaded, ?MODULE}). + %% sdl_clipboard get_clipboard_text() -> diff --git a/src/sdl_blend_mode.erl b/src/sdl_blend_mode.erl new file mode 100644 index 0000000..ce6b854 --- /dev/null +++ b/src/sdl_blend_mode.erl @@ -0,0 +1,36 @@ +%% 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. + +-module(sdl_blend_mode). + +-export([compose/6]). + +-type blend_mode() :: none | blend | add | mod | invalid | integer(). +-export_type([blend_mode/0]). + +-type blend_operation() :: add | substract | rev_substract | minimum | maximum. +-type blend_factor() :: zero | one + | src_color | one_minus_src_color + | src_alpha | one_minus_src_alpha + | dst_color | one_minus_dst_color + | dst_alpha | one_minus_dst_alpha. + +-spec compose(blend_factor(), blend_factor(), blend_operation(), + blend_factor(), blend_factor(), blend_operation()) -> blend_mode(). +compose(SrcColorFactor, DstColorFactor, ColorOp, + SrcAlphaFactor, DstAlphaFactor, AlphaOp) -> + esdl2:compose_custom_blend_mode( + SrcColorFactor, DstColorFactor, ColorOp, + SrcAlphaFactor, DstAlphaFactor, AlphaOp), + receive {'_nif_thread_ret_', Ret} -> Ret end. diff --git a/src/sdl_renderer.erl b/src/sdl_renderer.erl index 9727743..8bf51fc 100644 --- a/src/sdl_renderer.erl +++ b/src/sdl_renderer.erl @@ -59,9 +59,6 @@ -type point() :: #{x=>integer(), y=>integer()}. -type rect() :: #{x=>integer(), y=>integer(), w=>integer(), h=>integer()}. --type blend_mode() :: none | blend | add | mod. --export_type([blend_mode/0]). - -spec clear(renderer()) -> ok | sdl:error(). clear(Renderer) -> esdl2:render_clear(Renderer), @@ -157,7 +154,7 @@ get_clip_rect(Renderer) -> esdl2:render_get_clip_rect(Renderer), receive {'_nif_thread_ret_', Ret} -> Ret end. --spec get_draw_blend_mode(renderer()) -> blend_mode(). +-spec get_draw_blend_mode(renderer()) -> sdl_blend_mode:blend_mode(). get_draw_blend_mode(Renderer) -> esdl2:get_render_draw_blend_mode(Renderer), receive {'_nif_thread_ret_', Ret} -> @@ -214,7 +211,7 @@ set_clip_rect(Renderer, X, Y, W, H) -> esdl2:render_set_clip_rect(Renderer, X, Y, W, H), receive {'_nif_thread_ret_', Ret} -> Ret end. --spec set_draw_blend_mode(renderer(), blend_mode()) -> ok | sdl:error(). +-spec set_draw_blend_mode(renderer(), sdl_blend_mode:blend_mode()) -> ok | sdl:error(). set_draw_blend_mode(Renderer, BlendMode) -> esdl2:set_render_draw_blend_mode(Renderer, BlendMode), receive {'_nif_thread_ret_', Ret} -> Ret end. -- cgit v1.2.3