diff --git a/README.md b/README.md index 3f00913..92696d9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ lallegro ======== -Lua bindings for the [Allegro](http://liballeg.org/) game programming library +Lua bindings for the [Allegro 5.2](http://liballeg.org/) game programming library using [SWIG](http://swig.org/). @@ -29,10 +29,9 @@ al.destroy_display (disp) Bindings -------- -1 to 1 function binding is provided to almost all the Allegro API. The -"Primitives" addon is the only one missing, for now. Functions are renamed so -the prefix `al_` is removed, so the calls to functions use the Lua module as -the Allegro namespace. +1 to 1 function binding is provided to almost all the Allegro API, including +addons. Functions are renamed so the prefix `al_` is removed, so the calls to +functions use the Lua module as the Allegro namespace. Differences between the Allegro C API and this one is detailed in the [API diff document](apidiff.md). @@ -77,4 +76,4 @@ The [Allegro Documentation](http://liballeg.org/a5docs/trunk/index.html) should be widly used, as _lallegro_ provides 1 to 1 bindings. [LDoc](https://github.com/stevedonovan/LDoc) used for documenting the Lua -specific stuff. Generate with `ldoc .`. +specific stuff. Generate with `ldoc .` diff --git a/apidiff.md b/apidiff.md index 559acdb..9d5d15d 100644 --- a/apidiff.md +++ b/apidiff.md @@ -1,6 +1,13 @@ C and Lua API differences ========================= +al\_init +-------- +When exiting without calling `al_uninstall_system`, a segfault occurs when +initializing with the `al_init` macro. `lallegro.init` calls `al_install_system` +with a nil atexit\_ptr, to avoid this. The macro is still available as +`lallegro._init` + Platform Specific Functions --------------------------- The platform specific functions require other headers, and other libraries to diff --git a/lallegro-scm-1.rockspec b/lallegro-scm-1.rockspec index 834e593..4ff23ab 100644 --- a/lallegro-scm-1.rockspec +++ b/lallegro-scm-1.rockspec @@ -4,9 +4,9 @@ source = { url = 'git://github.com/gilzoide/lallegro', } description = { - summary = 'Lua bindings for the Allegro 5 game programming library', + summary = 'Lua bindings for the Allegro 5.2 game programming library', detailed = [[ - Lua bindings for the Allegro 5 game programming library + Lua bindings for the Allegro 5.2 game programming library ]], license = 'LGPLv3', maintainer = 'gilzoide ' diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a6ee6aa..919781a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -27,6 +27,7 @@ swig_please_wrap_it (image) swig_please_wrap_it (memfile) swig_please_wrap_it (dialog) swig_please_wrap_it (physfs) +swig_please_wrap_it (primitives) swig_please_wrap_it (video) # Copy Lua files @@ -35,7 +36,8 @@ file (COPY ${luaSrc} DESTINATION ${CMAKE_PROJECT_NAME}) ## Install ## set (LUA_VER "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}") -install (TARGETS core audio acodec color font ttf image memfile dialog physfs video +install (TARGETS core audio acodec color font ttf image memfile dialog physfs + primitives video LIBRARY DESTINATION lib/lua/${LUA_VER}/${CMAKE_PROJECT_NAME}) install (FILES ${luaSrc} DESTINATION share/lua/${LUA_VER}/${CMAKE_PROJECT_NAME}) diff --git a/src/audio.i b/src/audio.i index e59745c..6f46e76 100644 --- a/src/audio.i +++ b/src/audio.i @@ -102,7 +102,7 @@ ALLEGRO_SAMPLE *al_create_sample(void *buf, unsigned int samples void al_destroy_sample(ALLEGRO_SAMPLE *spl); -%apply ALLEGRO_SAMPLE_ID *OUTPUT { ALLEGRO_SAMPLE_ID *ret_id }; +%rename al_play_sample _play_sample; bool al_play_sample(ALLEGRO_SAMPLE *spl, float gain, float pan, float speed , ALLEGRO_PLAYMODE loop, ALLEGRO_SAMPLE_ID *ret_id); diff --git a/src/common.i b/src/common.i index fe50143..1b901c8 100644 --- a/src/common.i +++ b/src/common.i @@ -34,5 +34,37 @@ %apply unsigned int { uint32_t }; %apply long int { int64_t }; +// C arrays (no pointers) +%typemap (in) float[ANY] (float temp[$1_dim0]) { + int i, size; + // assert length + lua_len (L, $input); + size = lua_tointeger (L, -1); + if (size != $1_dim0) { + luaL_error (L, "expected %d floats in table, not %d", $1_dim0, size); + } + for (i = 0; i < $1_dim0; i++) { + lua_geti (L, $input, i + 1); + temp[i] = luaL_checknumber (L, -1); + } + lua_pop (L, $1_dim0 + 1); + $1 = temp; +} +%typemap (in) int[ANY] (int temp[$1_dim0]) { + int i, size; + // assert length + lua_len (L, $input); + size = lua_tointeger (L, -1); + if (size != $1_dim0) { + luaL_error (L, "expected %d ints in table, not %d", $1_dim0, size); + } + for (i = 0; i < $1_dim0; i++) { + lua_geti (L, $input, i + 1); + temp[i] = luaL_checkinteger (L, -1); + } + lua_pop (L, $1_dim0 + 1); + $1 = temp; +} + // Strip the prepending "al_" from functions, so we use Lua's module as namespace %rename ("%(strip:[al_])s") ""; diff --git a/src/core.i b/src/core.i index 40cc87a..6fa04c4 100644 --- a/src/core.i +++ b/src/core.i @@ -30,7 +30,7 @@ %include // Time: OUTPUT -%rename al_init_timeout al__init_timeout; +%rename al_init_timeout _init_timeout; %include // Bitmap: OUTPUT %apply int *OUTPUT { int *x, int *y, int *w, int *h }; @@ -76,18 +76,18 @@ %newobject al_get_current_directory; %include // Fullscreen modes: OUTPUT on Lua -%rename al_get_display_mode al__get_display_mode; +%rename al_get_display_mode _get_display_mode; %include // Haptic: OUTPUT on Lua -%rename al_upload_haptic_effect al__upload_haptic_effect; -%rename al_upload_and_play_haptic_effect al__upload_and_play_haptic_effect; -%rename al_rumble_haptic al__rumble_haptic; +%rename al_upload_haptic_effect _upload_haptic_effect; +%rename al_upload_and_play_haptic_effect _upload_and_play_haptic_effect; +%rename al_rumble_haptic _rumble_haptic; %include // Joystick: OUTPUT on Lua -%rename al_get_joystick_state al__get_joystick_state; +%rename al_get_joystick_state _get_joystick_state; %include // Keyboard: OUTPUT on Lua -%rename al_get_keyboard_state al__get_keyboard_state; +%rename al_get_keyboard_state _get_keyboard_state; %include /* Memory: macros * I don't think Lua devs will be `malloc`ing stuff, but who knows @@ -99,16 +99,16 @@ %ignore al_calloc; %include // Monitor: OUTPUT on Lua -%rename al_get_monitor_info al__get_monitor_info; +%rename al_get_monitor_info _get_monitor_info; %include // Mouse: OUTPUT on Lua, OUTPUT -%rename al_get_mouse_state al__get_mouse_state; +%rename al_get_mouse_state _get_mouse_state; %apply int *OUTPUT { int *ret_x, int *ret_y }; %include // %include // Path: default argument -%rename al_path_cstr al__path_cstr; +%rename al_path_cstr _path_cstr; %include // %include @@ -128,16 +128,16 @@ %apply int *OUTPUT { int *op, int *src, int *dst, int *alpha_op, int *alpha_src, int *alpha_dst } -%rename al_store_state al__store_state; +%rename al_store_state _store_state; %include // Touch Input: OUTPUT on Lua -%rename al_get_touch_input_state al__get_touch_input_state; +%rename al_get_touch_input_state _get_touch_input_state; %include // Transformations: OUTPUT on Lua, INOUT -%rename al_copy_transform al__copy_transform; -%rename al_identity_transform al__identity_transform; -%rename al_build_transform al__build_transform; -%rename al_build_camera_transform al__build_camera_transform; +%rename al_copy_transform _copy_transform; +%rename al_identity_transform _identity_transform; +%rename al_build_transform _build_transform; +%rename al_build_camera_transform _build_camera_transform; %apply float *INOUT { float *x, float *y, float *z }; %include /* UTF-8: Lua 5.3 gives us basic UTF-8 encoding, and Allegro API doesn't really @@ -155,5 +155,6 @@ int my_init (lua_State *L) { %} #ifdef ALLEGRO_UNSTABLE -%constant bool UNSTABLE = true; +#undef ALLEGRO_UNSTABLE +%constant bool ALLEGRO_UNSTABLE = true; #endif diff --git a/src/dialog.i b/src/dialog.i index 3e6564c..17f7c8c 100644 --- a/src/dialog.i +++ b/src/dialog.i @@ -62,7 +62,7 @@ void al_close_native_text_log(ALLEGRO_TEXTLOG *textlog); // This function originally have varargs, but SWIG doesn't like them much. // Format your string before passing it here, and it's all ok. We assume it'll // be called from the wrapper, with format as "%s", and the string -%rename al_append_native_text_log al__append_native_text_log; +%rename al_append_native_text_log _append_native_text_log; void al_append_native_text_log(ALLEGRO_TEXTLOG *textlog, char const *format , char const *text); diff --git a/src/font.i b/src/font.i index 9890f4c..c851734 100644 --- a/src/font.i +++ b/src/font.i @@ -69,7 +69,7 @@ void al_get_text_dimensions(const ALLEGRO_FONT *f, char const *text, uint32_t al_get_allegro_font_version(void); -%rename al_get_font_ranges al__get_font_ranges; +%rename al_get_font_ranges _get_font_ranges; int al_get_font_ranges(ALLEGRO_FONT *f, int ranges_count, int *ranges); void al_set_fallback_font(ALLEGRO_FONT *font, ALLEGRO_FONT *fallback); @@ -97,8 +97,9 @@ void al_draw_multiline_text(const ALLEGRO_FONT *font, ALLEGRO_COLOR color //////////////////////////////////////////////////////////////////////////////// // Bitmap fonts -%rename al_grab_font_from_bitmap al__grab_font_from_bitmap; -ALLEGRO_FONT *al_grab_font_from_bitmap(ALLEGRO_BITMAP *bmp, int ranges_n, const int ranges[]); +%rename al_grab_font_from_bitmap _grab_font_from_bitmap; +ALLEGRO_FONT *al_grab_font_from_bitmap(ALLEGRO_BITMAP *bmp, int ranges_n + , const int *ranges); ALLEGRO_FONT *al_load_bitmap_font(const char *fname); diff --git a/src/init.lua b/src/init.lua index bebbe37..23ab310 100644 --- a/src/init.lua +++ b/src/init.lua @@ -26,9 +26,11 @@ local al = require 'lallegro.core' --- Initializes Allegro and whatever addon you want -- -- Valid addons are, for now: 'audio', 'acodec', 'font', 'ttf', 'image', --- 'native_dialog', 'video' +-- 'native_dialog', 'primitives', 'video' function al.init (...) - if not al._init () then return nil, "[lallegro.init] Couldn't initialize Allegro" end + if not al.install_system (al.ALLEGRO_VERSION_INT, nil) then + return nil, "[lallegro.init] Couldn't initialize Allegro" + end for _, addon in ipairs { ... } do local mod = al['init_' .. addon .. '_addon'] if not mod then return nil, "[lallegro.init] Addon \"" .. addon .. "\" not supported" end @@ -40,7 +42,7 @@ end --- Shutdowns whatever addon you want -- -- Valid addons are, for now: 'audio', 'acodec', 'font', 'ttf', 'image', --- 'native_dialog', 'video' +-- 'native_dialog', 'primitives', 'video' function al.shutdown (...) for _, addon in ipairs { ... } do local mod = al['shutdown_' .. addon .. '_addon'] @@ -82,7 +84,7 @@ function al.get_display_mode (index, mode) end -- Unstable API with default output parameters -if al.UNSTABLE then +if al.ALLEGRO_UNSTABLE then --- Wrapper for `al_upload_haptic_effect` with default 3rd parameter function al.upload_haptic_effect (hap, effect, id) id = id or al.ALLEGRO_HAPTIC_EFFECT_ID () @@ -268,6 +270,13 @@ local function import_all (submod) end import_all (require 'lallegro.audio') + +--- Wrapper for `al_play_sample` with default 6th parameter +function al.play_sample (spl, gain, pan, speed, loop, ret_id) + ret_id = ret_id or al.ALLEGRO_SAMPLE_ID () + return al._play_sample (spl, gain, pan, speed, loop, ret_id), ret_id +end + import_all (require 'lallegro.acodec') import_all (require 'lallegro.color') import_all (require 'lallegro.font') @@ -298,7 +307,7 @@ end import_all (require 'lallegro.ttf') -if al.UNSTABLE then +if al.ALLEGRO_UNSTABLE then --- Wrapper for `al_get_glyph` with default 4th parameter function al.get_glyph (f, prev_codepoint, codepoint, glyph) glyph = glyph or al.ALLEGRO_GLYPH () @@ -315,10 +324,124 @@ import_all (require 'lallegro.dialog') -- Calls `al_append_native_text_log (textlog, "%s", text)`. -- Format `text` in Lua, and you're good to go function al.append_native_text_log (textlog, text) + assert (type (text) == 'string', "[lallegro.append_native_text_log] Only strings are supported") al._append_native_text_log (textlog, '%s', text) end import_all (require 'lallegro.physfs') +import_all (require 'lallegro.primitives') + +--- Default stride: 2 floats +al.default_stride = 2 * al.float_size + +--- Wrapper for `al_calculate_arc` with 'C array -> table' conversion +function al.calculate_arc (stride, cx, cy, rx, ry, start_theta, delta_theta, + thickness, num_points) + local arr_size = 2 * num_points + local arr = al.new_float (arr_size) + al._calculate_arc (arr, stride, cx, cy, rx, ry, start_theta, delta_theta, + thickness, num_points) + local ret = {} + for i = 0, arr_size - 1 do + table.insert (ret, al.float_getitem (arr, i)) + end + al.delete_float (arr) + return ret +end + +--- Wrapper for `al_calculate_spline` with 'C array -> table' conversion +function al.calculate_spline (stride, points, thickness, num_points) + local arr_size = thickness <= 0 and num_points or 2 * num_points + local arr = al.new_float (arr_size) + al._calculate_spline (arr, stride, points, thickness, num_points) + local ret = {} + for i = 0, arr_size - 1 do + table.insert (ret, al.float_getitem (arr, i)) + end + al.delete_float (arr) + return ret +end + +--- Wrapper for `al_calculate_ribbon` with 'C array -> table' conversion +function al.calculate_ribbon (dest_stride, points, points_stride, thickness) + local num_segments = #points + local arr_size = thickness <= 0 and num_segments or 2 * num_segments + local arr = al.new_float (arr_size) + local arr_points = al.new_float (num_segments) + for i = 0, num_segments - 1 do + al.float_setitem (arr_points, i, points[i + 1]) + end + al._calculate_ribbon (arr, dest_stride, arr_points, points_stride + , thickness, num_segments) + al.delete_float (arr_points) + local ret = {} + for i = 0, arr_size - 1 do + table.insert (ret, al.float_getitem (arr, i)) + end + al.delete_float (arr) + return ret +end + +--- Wrapper for `al_draw_ribbon` with 'C array -> table' conversion +function al.draw_ribbon (points, points_stride, color, thickness) + local arr = al.new_float (#points) + for i, x in ipairs (points) do + al.float_setitem (arr, i - 1, x) + end + al._draw_ribbon (arr, points_stride, color, thickness, #points / 2) + al.delete_float (arr) +end + +--- Wrapper for `al_draw_polyline` with 'C array -> table' conversion +function al.draw_polyline (vertices, vertex_stride, join_style, cap_style, color + , thickness, miter_limit) + local arr_size = #vertices + local arr = al.new_float (arr_size) + for i, x in ipairs (vertices) do + al.float_setitem (arr, i - 1, x) + end + al._draw_polyline (arr, vertex_stride, arr_size, join_style, cap_style + , color, thickness, miter_limit) + al.delete_float (arr) +end + +--- Wrapper for `al_draw_polygon` with 'C array -> table' conversion +function al.draw_polygon (vertices, join_style, color + , thickness, miter_limit) + local arr = al.new_float (#vertices) + for i, x in ipairs (vertices) do + al.float_setitem (arr, i - 1, x) + end + al._draw_polygon (arr, #vertices / 2, join_style, color, thickness + , miter_limit) + al.delete_float (arr) +end + +--- Wrapper for `al_draw_filled_polygon` with 'C array -> table' conversion +function al.draw_filled_polygon (vertices, color) + local arr = al.new_float (#vertices) + for i, x in ipairs (vertices) do + al.float_setitem (arr, i - 1, x) + end + al._draw_filled_polygon (arr, #vertices / 2, color) + al.delete_float (arr) +end + +--- Wrapper for `al_draw_filled_polygon_with_holes` with 'C array -> table' conversion +function al.draw_filled_polygon_with_holes (vertices, vertex_counts, color) + local arr = al.new_float (#vertices) + for i, x in ipairs (vertices) do + al.float_setitem (arr, i - 1, x) + end + local arr_counts = al.new_int (#vertex_counts) + for i, x in ipairs (vertex_counts) do + al.int_setitem (arr_counts, i - 1, x) + end + al._draw_filled_polygon_with_holes (arr, arr_counts, color) + al.delete_float (arr) + al.delete_int (arr_counts) +end + import_all (require 'lallegro.video') return al diff --git a/src/primitives.i b/src/primitives.i new file mode 100644 index 0000000..e841bed --- /dev/null +++ b/src/primitives.i @@ -0,0 +1,283 @@ +/* Copyright 2016 Gil Barbosa Reis + * This file is part of Lallegro. + * + * Lallegro is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Lallegro is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Lallegro. If not, see . + */ + +/** Allegro Primitives addon **/ +%module lallegro_primitives + +%include "common.i" + +%{ +#include +%} + +// For functions that use C arrays and typemaps are difficult to use +%include +%array_functions (float, float) + +/* Manually included functions, as SWIG is only preprocessing + * ALLEGRO_PRIM_FUNC to AL_FUNC, and complaining that AL_FUNC isn't valid + * C + */ +//////////////////////////////////////////////////////////////////////////////// +// Types +typedef struct ALLEGRO_VERTEX ALLEGRO_VERTEX; + +typedef struct ALLEGRO_VERTEX_DECL ALLEGRO_VERTEX_DECL; + +typedef struct ALLEGRO_VERTEX_ELEMENT ALLEGRO_VERTEX_ELEMENT; + +typedef struct ALLEGRO_VERTEX_BUFFER ALLEGRO_VERTEX_BUFFER; + +typedef struct ALLEGRO_INDEX_BUFFER ALLEGRO_INDEX_BUFFER; + +//////////////////////////////////////////////////////////////////////////////// +// General +uint32_t al_get_allegro_primitives_version(void); + +bool al_init_primitives_addon(void); + +void al_shutdown_primitives_addon(void); + +//////////////////////////////////////////////////////////////////////////////// +// High level drawing routines +void al_draw_line(float x1, float y1, float x2, float y2 + , ALLEGRO_COLOR color, float thickness); + +void al_draw_triangle(float x1, float y1, float x2, float y2 + , float x3, float y3, ALLEGRO_COLOR color, float thickness); + +void al_draw_filled_triangle(float x1, float y1, float x2, float y2 + , float x3, float y3, ALLEGRO_COLOR color); + +void al_draw_rectangle(float x1, float y1, float x2, float y2 + , ALLEGRO_COLOR color, float thickness); + +void al_draw_filled_rectangle(float x1, float y1, float x2, float y2 + , ALLEGRO_COLOR color); + +void al_draw_rounded_rectangle(float x1, float y1, float x2, float y2 + , float rx, float ry, ALLEGRO_COLOR color, float thickness); + +void al_draw_filled_rounded_rectangle(float x1, float y1, float x2, float y2 + , float rx, float ry, ALLEGRO_COLOR color); + +/// Float size, for calculating strides +%constant int float_size = sizeof (float); + +%rename al_calculate_arc _calculate_arc; +void al_calculate_arc(float* dest, int stride, float cx, float cy + , float rx, float ry, float start_theta, float delta_theta + , float thickness, int num_points); + +void al_draw_pieslice(float cx, float cy, float r, float start_theta + , float delta_theta, ALLEGRO_COLOR color, float thickness); + +void al_draw_filled_pieslice(float cx, float cy, float r, float start_theta + , float delta_theta, ALLEGRO_COLOR color); + +void al_draw_ellipse(float cx, float cy, float rx, float ry + , ALLEGRO_COLOR color, float thickness); + +void al_draw_filled_ellipse(float cx, float cy, float rx, float ry + , ALLEGRO_COLOR color); + +void al_draw_circle(float cx, float cy, float r, ALLEGRO_COLOR color + , float thickness); + +void al_draw_filled_circle(float cx, float cy, float r, ALLEGRO_COLOR color); + +void al_draw_arc(float cx, float cy, float r, float start_theta + , float delta_theta, ALLEGRO_COLOR color, float thickness); + +void al_draw_elliptical_arc(float cx, float cy, float rx, float ry + , float start_theta, float delta_theta, ALLEGRO_COLOR color + , float thickness); + +%rename al_calculate_spline _calculate_spline; +void al_calculate_spline(float* dest, int stride, float points[8] + , float thickness, int num_segments); + +void al_draw_spline(float points[8], ALLEGRO_COLOR color, float thickness); + +%rename al_calculate_ribbon _calculate_ribbon; +void al_calculate_ribbon(float* dest, int dest_stride, const float *points + , int points_stride, float thickness, int num_segments); + +%rename al_draw_ribbon _draw_ribbon; +void al_draw_ribbon(const float *points, int points_stride, ALLEGRO_COLOR color + , float thickness, int num_segments); + +//////////////////////////////////////////////////////////////////////////////// +// Low level drawing routines +int al_draw_prim(const void* vtxs, const ALLEGRO_VERTEX_DECL* decl + , ALLEGRO_BITMAP* texture, int start, int end, int type); + +%apply (int *INPUT, int) { (const int *indices, int num_vtx) }; +int al_draw_indexed_prim(const void* vtxs, const ALLEGRO_VERTEX_DECL* decl + , ALLEGRO_BITMAP* texture, const int* indices, int num_vtx, int type); + +int al_draw_vertex_buffer(ALLEGRO_VERTEX_BUFFER* vertex_buffer + , ALLEGRO_BITMAP* texture, int start, int end, int type); + +int al_draw_indexed_buffer(ALLEGRO_VERTEX_BUFFER* vertex_buffer + , ALLEGRO_BITMAP* texture, ALLEGRO_INDEX_BUFFER* index_buffer + , int start, int end, int type); + +//////////////////////////////////////////////////////////////////////////////// +// Custom vertex declaration routines +ALLEGRO_VERTEX_DECL* al_create_vertex_decl(const ALLEGRO_VERTEX_ELEMENT* elements + , int stride); + +void al_destroy_vertex_decl(ALLEGRO_VERTEX_DECL* decl); + +//////////////////////////////////////////////////////////////////////////////// +// Vertex buffer routines +ALLEGRO_VERTEX_BUFFER* al_create_vertex_buffer(ALLEGRO_VERTEX_DECL* decl + , const void* initial_data, int num_vertices, int flags); + +void al_destroy_vertex_buffer(ALLEGRO_VERTEX_BUFFER* buffer); + +void* al_lock_vertex_buffer(ALLEGRO_VERTEX_BUFFER* buffer, int offset + , int length, int flags); + +void al_unlock_vertex_buffer(ALLEGRO_VERTEX_BUFFER* buffer); + +int al_get_vertex_buffer_size(ALLEGRO_VERTEX_BUFFER* buffer); + +//////////////////////////////////////////////////////////////////////////////// +// Index buffer routines +ALLEGRO_INDEX_BUFFER* al_create_index_buffer(int index_size + , const void* initial_data, int num_indices, int flags); + +void al_destroy_index_buffer(ALLEGRO_INDEX_BUFFER* buffer); + +void* al_lock_index_buffer(ALLEGRO_INDEX_BUFFER* buffer, int offset + , int length, int flags); + +void al_unlock_index_buffer(ALLEGRO_INDEX_BUFFER* buffer); + +int al_get_index_buffer_size(ALLEGRO_INDEX_BUFFER* buffer); + +//////////////////////////////////////////////////////////////////////////////// +// Polygon routines +%rename al_draw_polyline _draw_polyline; +void al_draw_polyline(const float* vertices, int vertex_stride + , int vertex_count, int join_style, int cap_style, ALLEGRO_COLOR color + , float thickness, float miter_limit); + +%rename al_draw_polygon _draw_polygon; +void al_draw_polygon(const float *vertices, int vertex_count, int join_style + , ALLEGRO_COLOR color, float thickness, float miter_limit); + +%rename al_draw_filled_polygon _draw_filled_polygon; +void al_draw_filled_polygon(const float *vertices, int vertex_count + , ALLEGRO_COLOR color); + +%rename al_draw_filled_polygon_with_holes _draw_filled_polygon_with_holes; +void al_draw_filled_polygon_with_holes(const float *vertices + , const int *vertex_counts, ALLEGRO_COLOR color); + +/////////////////////////////////////////////////////////////////////////////// +// Macros and Enums, again by hand +typedef enum ALLEGRO_PRIM_TYPE +{ + ALLEGRO_PRIM_LINE_LIST, + ALLEGRO_PRIM_LINE_STRIP, + ALLEGRO_PRIM_LINE_LOOP, + ALLEGRO_PRIM_TRIANGLE_LIST, + ALLEGRO_PRIM_TRIANGLE_STRIP, + ALLEGRO_PRIM_TRIANGLE_FAN, + ALLEGRO_PRIM_POINT_LIST, + ALLEGRO_PRIM_NUM_TYPES +} ALLEGRO_PRIM_TYPE; + +enum +{ + ALLEGRO_PRIM_MAX_USER_ATTR = _ALLEGRO_PRIM_MAX_USER_ATTR +}; + +/* Enum: ALLEGRO_PRIM_ATTR + */ +typedef enum ALLEGRO_PRIM_ATTR +{ + ALLEGRO_PRIM_POSITION = 1, + ALLEGRO_PRIM_COLOR_ATTR, + ALLEGRO_PRIM_TEX_COORD, + ALLEGRO_PRIM_TEX_COORD_PIXEL, + ALLEGRO_PRIM_USER_ATTR, + ALLEGRO_PRIM_ATTR_NUM = ALLEGRO_PRIM_USER_ATTR + ALLEGRO_PRIM_MAX_USER_ATTR +} ALLEGRO_PRIM_ATTR; + +/* Enum: ALLEGRO_PRIM_STORAGE + */ +typedef enum ALLEGRO_PRIM_STORAGE +{ + ALLEGRO_PRIM_FLOAT_2, + ALLEGRO_PRIM_FLOAT_3, + ALLEGRO_PRIM_SHORT_2, + ALLEGRO_PRIM_FLOAT_1, + ALLEGRO_PRIM_FLOAT_4, + ALLEGRO_PRIM_UBYTE_4, + ALLEGRO_PRIM_SHORT_4, + ALLEGRO_PRIM_NORMALIZED_UBYTE_4, + ALLEGRO_PRIM_NORMALIZED_SHORT_2, + ALLEGRO_PRIM_NORMALIZED_SHORT_4, + ALLEGRO_PRIM_NORMALIZED_USHORT_2, + ALLEGRO_PRIM_NORMALIZED_USHORT_4, + ALLEGRO_PRIM_HALF_FLOAT_2, + ALLEGRO_PRIM_HALF_FLOAT_4 +} ALLEGRO_PRIM_STORAGE; + +/* Enum: ALLEGRO_LINE_JOIN + */ +typedef enum ALLEGRO_LINE_JOIN +{ + ALLEGRO_LINE_JOIN_NONE, + ALLEGRO_LINE_JOIN_BEVEL, + ALLEGRO_LINE_JOIN_ROUND, + ALLEGRO_LINE_JOIN_MITER, + ALLEGRO_LINE_JOIN_MITRE = ALLEGRO_LINE_JOIN_MITER +} ALLEGRO_LINE_JOIN; + +/* Enum: ALLEGRO_LINE_CAP + */ +typedef enum ALLEGRO_LINE_CAP +{ + ALLEGRO_LINE_CAP_NONE, + ALLEGRO_LINE_CAP_SQUARE, + ALLEGRO_LINE_CAP_ROUND, + ALLEGRO_LINE_CAP_TRIANGLE, + ALLEGRO_LINE_CAP_CLOSED +} ALLEGRO_LINE_CAP; + +/* Enum: ALLEGRO_PRIM_BUFFER_FLAGS + */ +typedef enum ALLEGRO_PRIM_BUFFER_FLAGS +{ + ALLEGRO_PRIM_BUFFER_STREAM = 0x01, + ALLEGRO_PRIM_BUFFER_STATIC = 0x02, + ALLEGRO_PRIM_BUFFER_DYNAMIC = 0x04, + ALLEGRO_PRIM_BUFFER_READWRITE = 0x08 +} ALLEGRO_PRIM_BUFFER_FLAGS; + +/* Enum: ALLEGRO_VERTEX_CACHE_SIZE + */ +#define ALLEGRO_VERTEX_CACHE_SIZE 256 + +/* Enum: ALLEGRO_PRIM_QUALITY + */ +#define ALLEGRO_PRIM_QUALITY 10 diff --git a/src/ttf.i b/src/ttf.i index 2746f2e..11e8aeb 100644 --- a/src/ttf.i +++ b/src/ttf.i @@ -45,7 +45,7 @@ ALLEGRO_FONT *al_load_ttf_font_stretch_f(ALLEGRO_FILE *file uint32_t al_get_allegro_ttf_version(void); #ifdef ALLEGRO_UNSTABLE -%rename al_get_glyph al__get_glyph; +%rename al_get_glyph _get_glyph; bool al_get_glyph(const ALLEGRO_FONT *f, int prev_codepoint, int codepoint , ALLEGRO_GLYPH *glyph); #endif diff --git a/teste.lua b/teste.lua index 18777d5..15a5ff6 100755 --- a/teste.lua +++ b/teste.lua @@ -2,13 +2,7 @@ al = require 'lallegro' -assert (al.init ('image', 'font', 'ttf', 'native_dialog')) -local log = al.open_native_text_log ('LOG', al.ALLEGRO_TEXTLOG_MONOSPACE) -print = function (what) - al.append_native_text_log (log, tostring (what) .. '\n') -end - - +assert (al.init ('image', 'font', 'ttf', 'native_dialog', 'primitives')) assert (al.install ('mouse')) local function printf (fmt, ...) @@ -16,25 +10,25 @@ local function printf (fmt, ...) end printf ('Funcionando com Allegro %d %s, num sistema com %d cpus; %d RAM; Pasta: %s' , al.get_allegro_version () >> 24, al.UNSTABLE and '(UNSTABLE)' or '' - , al.get_cpu_count (), al.get_ram_size () - , al.get_current_directory ()) + , al.get_cpu_count (), al.get_ram_size () + , al.get_current_directory ()) al.set_app_name 'lallegro teste' -- Monitores local vid_adap = al.ALLEGRO_MONITOR_INFO () print ('Monitores disponíveis:') for i = 0, al.get_num_video_adapters () - 1 do - al.get_monitor_info (i, vid_adap) - printf (' %d - (%d, %d) até (%d, %d)', i, vid_adap.x1, vid_adap.y1 - , vid_adap.x2, vid_adap.y2) + al.get_monitor_info (i, vid_adap) + printf (' %d - (%d, %d) até (%d, %d)', i, vid_adap.x1, vid_adap.y1 + , vid_adap.x2, vid_adap.y2) end -- Modos de display local disp_data = al.ALLEGRO_DISPLAY_MODE () print ('Modos de display disponíveis:') for i = 0, al.get_num_display_modes () - 1 do - al.get_display_mode (i, disp_data) - printf (' %d - %dx%d, formato: %d, refresh: %d', i, disp_data.width - , disp_data.height, disp_data.format, disp_data.refresh_rate) + al.get_display_mode (i, disp_data) + printf (' %d - %dx%d, formato: %d, refresh: %d', i, disp_data.width + , disp_data.height, disp_data.format, disp_data.refresh_rate) end @@ -42,8 +36,9 @@ al.get_display_mode (0, disp_data) al.set_new_display_flags (al.ALLEGRO_PROGRAMMABLE_PIPELINE) local display = assert (al.create_display (disp_data.width, disp_data.height)) printf ('Criando janela %dx%d na posição %dx%d', disp_data.width, disp_data.height - , al.get_window_position (display)) + , al.get_window_position (display)) local preto = al.map_rgb (0, 0, 0) +local branco = al.map_rgb (255, 255, 255) printf ('Limpando tela de preto, que é (%d, %d, %d, %d)', al.unmap_rgba (preto)) -- Teste do mouse @@ -51,33 +46,13 @@ printf ('Mouse na posição (%d, %d)', select (2, al.get_mouse_cursor_position ( -- Teste dos shaders -local sh = al.create_shader (al.ALLEGRO_SHADER_AUTO) -if not al.attach_shader_source (sh, al.ALLEGRO_PIXEL_SHADER, [[ -#version 130 - -uniform sampler2D al_tex; -varying vec4 varying_color; -varying vec2 varying_texcoord; - -void main () { - gl_FragColor = vec4 (texture (al_tex, varying_texcoord).xyz, 0.41); -} -]]) or not al.attach_shader_source (sh, al.ALLEGRO_VERTEX_SHADER, al.get_default_shader_source (al.ALLEGRO_SHADER_GLSL, al.ALLEGRO_VERTEX_SHADER)) then - print (al.get_shader_log (sh)) - return -end --- print ('\n', al.get_default_shader_source (al.ALLEGRO_SHADER_GLSL, al.ALLEGRO_VERTEX_SHADER), '\n') --- print ('\n', al.get_default_shader_source (al.ALLEGRO_SHADER_GLSL, al.ALLEGRO_PIXEL_SHADER), '\n') -assert (al.build_shader (sh), 'Deu bosta no build_shader =/') al.set_blender (al.ALLEGRO_ADD, al.ALLEGRO_ALPHA, al.ALLEGRO_INVERSE_ALPHA) -al.set_shader_float_vector ('teste', 1, { 1, 1, 0, 1 }) -al.set_shader_int_vector ('testei', 1, { 2, 4, 0 }) -al.use_shader (sh) -local bmp = al.load_bitmap ('../flango.png') +local bmp = al.load_bitmap ('flango.png') al.set_target_backbuffer (display) al.clear_to_color (preto) al.draw_bitmap (bmp, 0, 0, 0) +al.draw_filled_polygon_with_holes ({ 0, 0, 0, 100, 100, 100, 100, 0, 10, 10, 90, 10, 90, 90 }, { 4, 3, 0 }, branco) al.flip_display () -- Teste dos eventos @@ -87,7 +62,7 @@ al.register_event_source (queue, al.get_mouse_event_source ()) print ('Táca-le X (botão de fechar mesmo) pra fechar a janela') local ev = al.ALLEGRO_EVENT () while true do - if al.get_next_event (queue, ev) then + if al.get_next_event (queue, ev) then if ev.type == al.ALLEGRO_EVENT_DISPLAY_CLOSE then break elseif ev.type == al.ALLEGRO_EVENT_MOUSE_BUTTON_DOWN then @@ -97,27 +72,29 @@ while true do end al.destroy_display (display) al.destroy_event_queue (queue) +al.destroy_bitmap (bmp) +al.destroy_shader (sh) --- Teste do arquivo de config -cfg = al.load_config_file '../configTest.cfg' +local cfg = al.load_config_file 'configTest.cfg' sec, it = al.get_first_config_section (cfg) repeat - print (sec) - entry, it2 = al.get_first_config_entry (cfg, sec) - repeat - print ('', entry, '=', al.get_config_value (cfg, sec, entry)) - entry, it2 = al.get_next_config_entry (it2) - until not it2 - sec, it = al.get_next_config_section (it) + print (sec) + entry, it2 = al.get_first_config_entry (cfg, sec) + repeat + print ('', entry, '=', al.get_config_value (cfg, sec, entry)) + entry, it2 = al.get_next_config_entry (it2) + until not it2 + sec, it = al.get_next_config_section (it) until not it al.destroy_config (cfg) --- Teste do haptic -if al.UNSTABLE then - al.install ('joystick', 'haptic') +if al.ALLEGRO_UNSTABLE then + al.install ('joystick', 'haptic') local num_joys = al.get_num_joysticks () printf ('Joystick instalado, %d disponíveis', num_joys) if num_joys > 0 then @@ -143,7 +120,7 @@ local font = assert (al.load_font ('/usr/share/fonts/TTF/DejaVuSansMono-Bold.ttf print (al.get_glyph_dimensions (font, 95)) local _, ranges = al.get_font_ranges (font, 2) for i, r in ipairs (ranges) do print (i, r) end +al.destroy_font (font) print (al.color_rgb_to_html (1, 1, 1)) - -al.show_native_message_box (nil, 'Título', 'Tiau', 'Já vai?', 'falooooooows', al.ALLEGRO_MESSAGEBOX_WARN) +al.uninstall_system ()