Compare commits

...

2 Commits

Author SHA1 Message Date
c212e5ce1e add stb image 2025-05-01 13:34:08 +03:00
b33e491293 add sokol version of default input module. 2025-05-01 13:33:31 +03:00
18 changed files with 16985 additions and 0 deletions

266
modules/Input/module.jai Normal file
View File

@ -0,0 +1,266 @@
// Input handler routines, platform-independent.
// The Jai Input module ported to use sokol events.
#load "sokol.jai";
Event_Type :: enum u32 { // If we set this to u8, our packing will stop matching C's.
UNINITIALIZED :: 0;
KEYBOARD :: 1;
TEXT_INPUT :: 2;
WINDOW :: 3;
MOUSE_WHEEL :: 4;
QUIT :: 5;
DRAG_AND_DROP_FILES :: 6;
TOUCH :: 7;
}
Key_Current_State :: enum_flags u32 {
NONE :: 0x0;
DOWN :: 0x1;
START :: 0x4;
END :: 0x8;
}
// We reserve 32 buttons for each gamepad.
GAMEPAD_BUTTON_COUNT :: 32;
Key_Code :: enum u32 {
UNKNOWN :: 0;
// Non-textual keys that have placements in the ASCII table
// (and thus in Unicode):
BACKSPACE :: 8;
TAB :: 9;
LINEFEED :: 10;
ENTER :: 13;
ESCAPE :: 27;
SPACEBAR :: 32;
// The letters A-Z live in here as well and may be returned
// by keyboard events.
DELETE :: 127;
ARROW_UP :: 128;
ARROW_DOWN :: 129;
ARROW_LEFT :: 130;
ARROW_RIGHT :: 131;
PAGE_UP :: 132;
PAGE_DOWN :: 133;
HOME :: 134;
END :: 135;
INSERT :: 136;
PAUSE :: 137;
SCROLL_LOCK :: 138;
ALT;
CTRL;
SHIFT;
CMD;
META :: CMD;
F1;
F2;
F3;
F4;
F5;
F6;
F7;
F8;
F9;
F10;
F11;
F12;
F13;
F14;
F15;
F16;
F17;
F18;
F19;
F20;
F21;
F22;
F23;
F24;
PRINT_SCREEN;
MOUSE_BUTTON_LEFT;
MOUSE_BUTTON_MIDDLE;
MOUSE_BUTTON_RIGHT;
MOUSE_WHEEL_UP;
MOUSE_WHEEL_DOWN;
// We reserve button codes for up to 4 gamepads.
GAMEPAD_0_BEGIN;
GAMEPAD_0_END :: GAMEPAD_0_BEGIN + xx GAMEPAD_BUTTON_COUNT;
GAMEPAD_1_BEGIN;
GAMEPAD_1_END :: GAMEPAD_1_BEGIN + xx GAMEPAD_BUTTON_COUNT;
GAMEPAD_2_BEGIN;
GAMEPAD_2_END :: GAMEPAD_2_BEGIN + xx GAMEPAD_BUTTON_COUNT;
GAMEPAD_3_BEGIN;
GAMEPAD_3_END :: GAMEPAD_3_BEGIN + xx GAMEPAD_BUTTON_COUNT;
TOUCH;
// WARNING!
//
// We make an array whose size is controlled
// by the last enum value in this array, so if you make
// really big values to match Unicode code points, our
// memory usage will become quite sorry.
//
// -jblow, 19 March 2017
//
}
// Modifier_Flags used to use #place, but I rewrote it to use a union
// instead .... it is not necessarily clearer though! So I am switching
// it back for now...
Event :: struct {
Modifier_Flags :: union {
// Eventually we'd like the *_pressed modifiers to be 1 bit each,
// but still be nameable as booleans. But for now they're 1 byte each.
// You can compare them as a u32 using the 'packed' member.
struct {
// @@ This is confusing. Below key_pressed means the key was just pressed, here _pressed means the key is held down.
shift_pressed := false;
ctrl_pressed := false;
alt_pressed := false;
cmd_meta_pressed := false; // Cmd on macOS, Meta on Linux
}
packed: u32 = 0;
}
type: Event_Type = Event_Type.UNINITIALIZED;
// If keyboard event:
key_pressed: u32; // If not pressed, it's a key release.
key_code := Key_Code.UNKNOWN;
using modifier_flags: Modifier_Flags; // Only set for Event_Type.KEYBOARD.
utf32: u32; // If TEXT_INPUT.
repeat := false; // If KEYBOARD event.
text_input_count: u16; // If KEYBOARD event that also generated TEXT_INPUT events, this will tell you how many TEXT_INPUT events after this KEYBOARD event were generated.
typical_wheel_delta: s32; // Used only for mouse events.
wheel_delta: s32; // Used only for mouse events.
files: [..] string; // Used only for drag and drop events. Both the array and its contents are heap-allocated, lives until events are reset for the next frame.
touch_type: enum u8 { // Used only for touch events.
MOVED :: 0;
PRESSED :: 1;
RELEASED :: 2;
}
touch_index: s32; // Used only for touch events. Index of which touch this is.
}
// Per-frame mouse deltas:
mouse_delta_x: int;
mouse_delta_y: int;
mouse_delta_z: int;
events_this_frame: [..] Event;
input_button_states: [NUM_BUTTON_STATES] Key_Current_State;
input_application_has_focus := false;
NUM_BUTTON_STATES :: #run enum_highest_value(Key_Code) + 1;
Window_Resize_Record :: struct {
window: Window_Type;
width: s32;
height: s32;
}
Window_Move_Record :: struct {
window: Window_Type;
x: s32;
y: s32;
}
get_window_resizes :: () -> [] Window_Resize_Record {
// The return value here will stick around in memory until the next call
// to get_window_resizes (from any thread. Actually this whole module does
// not deal with threading, so don't do that!)
if resizes_to_free array_reset(*resizes_to_free);
if !pending_resizes return .[];
array_copy(*resizes_to_free, pending_resizes);
this_allocation_is_not_a_leak(resizes_to_free.data);
pending_resizes.count = 0;
return resizes_to_free;
}
get_window_moves :: () -> [] Window_Move_Record {
// See notes on get_window_resizes. This works the same way.
if moves_to_free array_reset(*moves_to_free);
if !pending_moves return .[];
array_copy(*moves_to_free, pending_moves);
pending_moves.count = 0;
return moves_to_free;
}
input_per_frame_event_and_flag_update :: () {
// Called once per frame, probably.
for events_this_frame {
allocator := it.files.allocator; // Same allocator for filenames and the array itself.
for file: it.files {
free(file,, allocator);
}
array_free(it.files,, allocator);
}
array_reset(*events_this_frame);
mask := ~Key_Current_State.START;
end_mask := ~(Key_Current_State.END | .DOWN | .START);
// @Speed: Could just keep a list of who is not currently set.
for * input_button_states {
if it.* & .END {
it.* &= end_mask;
} else {
it.* &= mask;
}
}
mouse_delta_x = 0;
mouse_delta_y = 0;
mouse_delta_z = 0;
}
#scope_module
pending_moves: [..] Window_Move_Record;
moves_to_free: [..] Window_Move_Record;
pending_resizes: [..] Window_Resize_Record;
resizes_to_free: [..] Window_Resize_Record;
#import "Basic";
#import "Window_Type";

417
modules/Input/sokol.jai Normal file
View File

@ -0,0 +1,417 @@
#import "String"; // @Cleanup: Remove this dependency on String?
#import,dir "../sokol-jai/sokol/app";
input_mouse_x : float;
input_mouse_y : float;
set_custom_cursor_handling :: (is_custom: bool) {
}
get_key_code :: (key: sapp_keycode) -> Key_Code {
using Key_Code;
// non-ascii ordered as in ./module.jai/Key_Code
if key == .BACKSPACE
return BACKSPACE;
if key == .TAB
return TAB;
if key == .ENTER
return ENTER;
if key == .ESCAPE
return ESCAPE;
if key == .DELETE
return DELETE;
if key == .SPACE
return SPACEBAR;
if key == .UP
return ARROW_UP;
if key == .DOWN
return ARROW_DOWN;
if key == .LEFT
return ARROW_LEFT;
if key == .RIGHT
return ARROW_RIGHT;
if key == .PAGE_UP
return PAGE_UP;
if key == .PAGE_DOWN
return PAGE_DOWN;
if key == .HOME
return HOME;
if key == .END
return END;
if key == .INSERT
return INSERT;
if key == .PAUSE
return PAUSE;
if key == .SCROLL_LOCK
return SCROLL_LOCK;
if (key == .LEFT_ALT) || (key == .RIGHT_ALT)
return ALT;
if (key == .LEFT_CONTROL) || (key == .RIGHT_CONTROL)
return CTRL;
if (key == .LEFT_SHIFT) || (key == .RIGHT_SHIFT)
return SHIFT;
if (key == .LEFT_SUPER) || (key == .RIGHT_SUPER)
return META;
if key >= sapp_keycode.F1 && key <= sapp_keycode.F25
return (cast(Key_Code) (key - sapp_keycode.F1))+F1;
if key == .PRINT_SCREEN
return PRINT_SCREEN;
if (key >= #char "a") && (key <= #char "z") return cast(Key_Code) (key - 0x20);
if key > 32 && key < 127 return cast(Key_Code) key;
return UNKNOWN;
}
get_key_code_for_mouse :: (mb: sapp_mousebutton) -> Key_Code {
if mb == .LEFT
return .MOUSE_BUTTON_LEFT;
if mb == .RIGHT
return .MOUSE_BUTTON_RIGHT;
if mb == .MIDDLE
return .MOUSE_BUTTON_MIDDLE;
return .UNKNOWN;
}
#scope_export
handle_sokol_event :: (event: *sapp_event) {
new_event: Event;
if event.type == {
case .KEY_DOWN;
mapped := get_key_code(event.key_code);
new_event.type = .KEYBOARD;
new_event.key_code = mapped;
new_event.key_pressed = 1;
input_button_states[mapped] = .START | .DOWN;
case .KEY_UP;
mapped := get_key_code(event.key_code);
new_event.type = .KEYBOARD;
new_event.key_code = mapped;
new_event.key_pressed = 0;
input_button_states[mapped] = .END;
case .MOUSE_DOWN;
mapped := get_key_code_for_mouse(event.mouse_button);
new_event.type = .KEYBOARD;
new_event.key_code = mapped;
new_event.key_pressed = 1;
input_button_states[mapped] = .START | .DOWN;
case .MOUSE_UP;
mapped := get_key_code_for_mouse(event.mouse_button);
new_event.type = .KEYBOARD;
new_event.key_code = mapped;
new_event.key_pressed = 0;
input_button_states[mapped] = .END;
case .CHAR;
new_event.type = .TEXT_INPUT;
new_event.utf32 = event.char_code;
case .FOCUSED;
input_application_has_focus = true;
return;
case .UNFOCUSED;
input_application_has_focus = false;
return;
case .MOUSE_MOVE;
input_mouse_x = event.mouse_x;
input_mouse_y = event.mouse_y;
return;
}
array_add(*events_this_frame, new_event);
}
// using Key_Code;
// input_per_frame_event_and_flag_update();
// display := x_global_display;
// XLockDisplay(display);
// key_press_repeat := false;
// while XPending(display) {
// xev: XEvent;
// XNextEvent(display, *xev);
// if XFilterEvent(*xev, None) continue;
// if xev.type == {
// case ClientMessage;
// message_type := xev.xclient.message_type;
// if message_type == XdndEnter {
// drop_handled := maybe_handle_xdnd_drop_events(display, *xev, *drop_info);
// if drop_handled continue;
// } else if message_type == x_global_wm_protocols {
// message0 := cast(Atom) xev.xclient.data.l[0];
// // This can't be a switch, because the values are not constant!
// // Except come on guys, every single implementation of X11 in the universe
// // is going to agree on these values. Why are we pretending they are not constant?
// // How about if we just look them up once and then hardcode them into this program?
// // We'd save startup time...
// if message0 == x_global_wm_delete_window {
// event: Event;
// event.type = .QUIT;
// array_add(*events_this_frame, event);
// }
// }
// case KeyPress;
// event: Event;
// shift: u32;
// if xev.xkey.state & ShiftMask
// shift = 1;
// if xev.xkey.state & ControlMask
// event.ctrl_pressed = true;
// if xev.xkey.state & Mod1Mask
// event.alt_pressed = true;
// status: Status;
// keysym: KeySym;
// text: [16]u32;
// lookup_count := XwcLookupString(x_global_input_context, *xev.xkey, xx text.data, xx text.count, *keysym, *status);
// has_keysym := (status == XLookupKeySym || status == XLookupBoth);
// has_text := (status == XLookupChars || status == XLookupBoth);
// if has_keysym {
// if xev.xkey.state & ShiftMask event.shift_pressed = true;
// if xev.xkey.state & ControlMask event.ctrl_pressed = true;
// if xev.xkey.state & Mod1Mask event.alt_pressed = true;
// if xev.xkey.state & Mod4Mask event.cmd_meta_pressed = true;
// event.type = .KEYBOARD;
// event.key_pressed = 1;
// event.key_code = get_key_code(keysym);
// for 0..lookup_count - 1 {
// utf32 := text[it];
// if utf32 >= 32 && utf32 != 127 event.text_input_count += 1;
// }
// if event.key_code == {
// case .SHIFT; event.shift_pressed = true;
// case .CTRL; event.ctrl_pressed = true;
// case .ALT; event.alt_pressed = true;
// case .META; event.cmd_meta_pressed = true;
// }
// event.repeat = key_press_repeat;
// key_press_repeat = false;
// array_add(*events_this_frame, event);
// input_button_states[event.key_code] = .START | .DOWN;
// }
// event.shift_pressed = cast(bool) shift;
// if has_text {
// for 0..lookup_count - 1 {
// utf32 := text[it];
// if utf32 < 32 || utf32 == 127 continue;
// char_event: Event;
// char_event.type = .TEXT_INPUT;
// char_event.key_pressed = 1;
// char_event.key_code = get_key_code(keysym);
// char_event.utf32 = utf32;
// array_add(*events_this_frame, char_event);
// }
// }
// case KeyRelease;
// // For some odd reason X11 generates KeyRelease followed by a near identical KeyPress to simulate repeat events so we have to filter that out
// if XEventsQueued(display, QueuedAfterReading) {
// nev: XEvent;
// XPeekEvent(display, *nev);
// if nev.type == KeyPress
// && nev.xkey.time == xev.xkey.time
// && nev.xkey.keycode == xev.xkey.keycode {
// // This is a repeat, so we ignore the KeyRelease and set the repeat flag.
// key_press_repeat = true;
// continue;
// }
// }
// event: Event;
// shift: u32;
// if xev.xkey.state & ShiftMask
// shift = 1;
// if xev.xkey.state & ControlMask
// event.ctrl_pressed = true;
// if xev.xkey.state & Mod1Mask
// event.alt_pressed = true;
// keysym := XkbKeycodeToKeysym(display, xx xev.xkey.keycode, 0, 0 /* English lowercase group*/);
// event.type = .KEYBOARD;
// event.key_pressed = 0;
// event.shift_pressed = cast(bool) shift;
// event.key_code = get_key_code(keysym);
// input_button_states[event.key_code] = .END;
// array_add(*events_this_frame, event);
// case ButtonPress;
// event: Event;
// event.type = .KEYBOARD;
// event.key_pressed = 1;
// button := xev.xbutton.button;
// // 6/7 is horizontal/secondary wheel, don't handle it
// if (button == 6) || (button == 7) continue;
// // button 4/5 is mwheel up/down
// if (button == 4) || (button == 5) {
// event.type = .MOUSE_WHEEL;
// event.typical_wheel_delta = WHEEL_DELTA;
// event.wheel_delta = event.typical_wheel_delta * ifx button & 1 then cast(s32) - 1 else 1;
// array_add(*events_this_frame, event);
// mouse_delta_z += event.wheel_delta;
// continue;
// }
// if button == Button1 {
// event.key_code = MOUSE_BUTTON_LEFT;
// } else if button == Button2 {
// event.key_code = MOUSE_BUTTON_MIDDLE;
// } else if button == Button3 {
// event.key_code = MOUSE_BUTTON_RIGHT;
// }
// input_button_states[event.key_code] = .START | .DOWN;
// array_add(*events_this_frame, event);
// case ButtonRelease;
// // it seems that mouse input doesnt generate repeat events so we dont have to peek the queue
// event: Event;
// event.type = .KEYBOARD;
// event.key_pressed = 0;
// button := xev.xbutton.button;
// if (button >= 4) && (button <= 7) continue; // No action on button release of mouse wheels.
// if button == Button1 {
// event.key_code = MOUSE_BUTTON_LEFT;
// } else if button == Button2 {
// event.key_code = MOUSE_BUTTON_MIDDLE;
// } else if button == Button3 {
// event.key_code = MOUSE_BUTTON_RIGHT;
// }
// input_button_states[event.key_code] = .END;
// array_add(*events_this_frame, event);
// case SelectionRequest;
// selreq := cast(*XSelectionRequestEvent) *xev;
// out: XEvent;
// selnot := cast(*XSelectionEvent) *out;
// selnot.type = SelectionNotify;
// selnot.requestor = selreq.requestor;
// selnot.selection = selreq.selection;
// selnot.target = selreq.target;
// selnot.time = selreq.time;
// selnot.property = None;
// if x_window_is_ours(x_global_display, selreq.owner) {
// if selreq.target == x_global_xa_utf8 {
// selnot.property = selreq.property;
// text_data := x_global_clipboard_buffer.text_data;
// XChangeProperty(selreq.display, selreq.requestor, selreq.property, selreq.target, 8, PropModeReplace,
// text_data.data, cast(s32) text_data.count);
// } else if selreq.target == x_global_xa_targets {
// selnot.property = selreq.property;
// atoms: [..] Atom;
// array_add(*atoms, x_global_xa_utf8);
// array_add(*atoms, x_global_xa_targets);
// array_add(*atoms, x_global_xa_multiple);
// if x_global_clipboard_buffer.rgb_data {
// array_add(*atoms, x_global_image_bmp);
// }
// XChangeProperty(selreq.display, selreq.requestor, selreq.property, x_global_xa_atom, 32, PropModeReplace,
// xx atoms.data, cast(s32) atoms.count);
// array_reset(*atoms);
// } else if selreq.target == x_global_image_bmp {
// #import "stb_image_write";
// Data :: struct {
// _context: *#Context;
// data: [..] u8;
// }
// write_func :: (data_pointer: *void, _data: *void, size: s32) #c_call {
// data := cast(*Data) data_pointer;
// push_context data._context.* {
// data8 := cast(*u8) _data;
// for 0..size-1 {
// array_add(*data.data, data8[it]);
// }
// }
// }
// data: Data;
// data._context = *context;
// w := x_global_clipboard_buffer.width;
// h := x_global_clipboard_buffer.height;
// comp: s32 = 3;
// stride := x_global_clipboard_buffer.pitch;
// stbi_write_bmp_to_func(write_func, *data, w, h, comp, x_global_clipboard_buffer.rgb_data);
// selnot.property = selreq.property;
// XChangeProperty(selreq.display, selreq.requestor, selreq.property, selreq.target, 8, PropModeReplace,
// xx data.data.data, cast(s32) data.data.count);
// array_reset(*data.data);
// } else {
// // print("GOT REQ: %\n", to_string(XGetAtomName(x_global_display, selreq.target)));
// }
// }
// XSendEvent(selreq.display, selreq.requestor, True, 0, *out);
// case ConfigureNotify;
// config := cast(*XConfigureEvent) *xev;
// add_resize_record(config.window, config.width, config.height);
// case FocusIn;
// input_application_has_focus = true;
// case FocusOut;
// input_application_has_focus = false;
// }
// }
// XUnlockDisplay(display);
WHEEL_DELTA :: 120;
/*
Copyright 2021, Thekla, Inc. Modified to work with Sokol by Tuomas Katajisto.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

View File

@ -30,6 +30,7 @@ build_lib_wasm_release sokol_shape shape/sokol_shape_wasm_gl_release SOKOL
build_lib_wasm_release sokol_fontstash fontstash/sokol_fontstash_wasm_gl_release SOKOL_GLES3 build_lib_wasm_release sokol_fontstash fontstash/sokol_fontstash_wasm_gl_release SOKOL_GLES3
build_lib_wasm_release sokol_fetch fetch/sokol_fetch_wasm_gl_release SOKOL_GLES3 build_lib_wasm_release sokol_fetch fetch/sokol_fetch_wasm_gl_release SOKOL_GLES3
build_lib_wasm_release sokol_gl gl/sokol_gl_wasm_gl_release SOKOL_GLES3 build_lib_wasm_release sokol_gl gl/sokol_gl_wasm_gl_release SOKOL_GLES3
build_lib_wasm_release stb_image stbi/stb_image SOKOL_GLES3
# wasm + GL + Debug # wasm + GL + Debug
build_lib_wasm_debug sokol_log log/sokol_log_wasm_gl_debug SOKOL_GLES3 build_lib_wasm_debug sokol_log log/sokol_log_wasm_gl_debug SOKOL_GLES3
@ -43,5 +44,6 @@ build_lib_wasm_debug sokol_shape shape/sokol_shape_wasm_gl_debug SOKOL_G
build_lib_wasm_debug sokol_fontstash fontstash/sokol_fontstash_wasm_gl_debug SOKOL_GLES3 build_lib_wasm_debug sokol_fontstash fontstash/sokol_fontstash_wasm_gl_debug SOKOL_GLES3
build_lib_wasm_debug sokol_fetch fetch/sokol_fetch_wasm_gl_debug SOKOL_GLES3 build_lib_wasm_debug sokol_fetch fetch/sokol_fetch_wasm_gl_debug SOKOL_GLES3
build_lib_wasm_debug sokol_gl gl/sokol_gl_wasm_gl_debug SOKOL_GLES3 build_lib_wasm_debug sokol_gl gl/sokol_gl_wasm_gl_debug SOKOL_GLES3
build_lib_wasm_debug stb_image stbi/stb_image SOKOL_GLES3
rm *.o rm *.o

View File

@ -0,0 +1,11 @@
#ifdef WIN32
#define __EXPORT __declspec(dllexport)
#else
#define __EXPORT
#endif
#define STBIDEF extern __EXPORT
#define STBI_NO_STDIO
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

File diff suppressed because it is too large Load Diff

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,145 @@
//
// This file was auto-generated using the following command:
//
// jai generate.jai
//
STBI_VERSION :: 1;
STBI :: enum u32 {
default :: 0;
grey :: 1;
grey_alpha :: 2;
rgb :: 3;
rgb_alpha :: 4;
STBI_default :: default;
STBI_grey :: grey;
STBI_grey_alpha :: grey_alpha;
STBI_rgb :: rgb;
STBI_rgb_alpha :: rgb_alpha;
}
//
// load image by filename, open file, or memory buffer
//
stbi_io_callbacks :: struct {
read: #type (user: *void, data: *u8, size: s32) -> s32 #c_call; // fill 'data' with 'size' bytes. return number of bytes actually read
skip: #type (user: *void, n: s32) -> void #c_call; // skip the next 'n' bytes, or 'unget' the last -n bytes if negative
eof: #type (user: *void) -> s32 #c_call; // returns nonzero if we are at end of file/data
}
////////////////////////////////////
//
// 8-bits-per-channel interface
//
stbi_load_from_memory :: (buffer: *u8, len: s32, x: *s32, y: *s32, channels_in_file: *s32, desired_channels: s32) -> *u8 #foreign stb_image;
stbi_load_from_callbacks :: (clbk: *stbi_io_callbacks, user: *void, x: *s32, y: *s32, channels_in_file: *s32, desired_channels: s32) -> *u8 #foreign stb_image;
stbi_load :: (filename: *u8, x: *s32, y: *s32, channels_in_file: *s32, desired_channels: s32) -> *u8 #foreign stb_image;
stbi_load_from_file :: (f: *FILE, x: *s32, y: *s32, channels_in_file: *s32, desired_channels: s32) -> *u8 #foreign stb_image;
stbi_load_gif_from_memory :: (buffer: *u8, len: s32, delays: **s32, x: *s32, y: *s32, z: *s32, comp: *s32, req_comp: s32) -> *u8 #foreign stb_image;
////////////////////////////////////
//
// 16-bits-per-channel interface
//
stbi_load_16_from_memory :: (buffer: *u8, len: s32, x: *s32, y: *s32, channels_in_file: *s32, desired_channels: s32) -> *u16 #foreign stb_image;
stbi_load_16_from_callbacks :: (clbk: *stbi_io_callbacks, user: *void, x: *s32, y: *s32, channels_in_file: *s32, desired_channels: s32) -> *u16 #foreign stb_image;
stbi_load_16 :: (filename: *u8, x: *s32, y: *s32, channels_in_file: *s32, desired_channels: s32) -> *u16 #foreign stb_image;
stbi_load_from_file_16 :: (f: *FILE, x: *s32, y: *s32, channels_in_file: *s32, desired_channels: s32) -> *u16 #foreign stb_image;
stbi_loadf_from_memory :: (buffer: *u8, len: s32, x: *s32, y: *s32, channels_in_file: *s32, desired_channels: s32) -> *float #foreign stb_image;
stbi_loadf_from_callbacks :: (clbk: *stbi_io_callbacks, user: *void, x: *s32, y: *s32, channels_in_file: *s32, desired_channels: s32) -> *float #foreign stb_image;
stbi_loadf :: (filename: *u8, x: *s32, y: *s32, channels_in_file: *s32, desired_channels: s32) -> *float #foreign stb_image;
stbi_loadf_from_file :: (f: *FILE, x: *s32, y: *s32, channels_in_file: *s32, desired_channels: s32) -> *float #foreign stb_image;
stbi_hdr_to_ldr_gamma :: (gamma: float) -> void #foreign stb_image;
stbi_hdr_to_ldr_scale :: (scale: float) -> void #foreign stb_image;
stbi_ldr_to_hdr_gamma :: (gamma: float) -> void #foreign stb_image;
stbi_ldr_to_hdr_scale :: (scale: float) -> void #foreign stb_image;
// stbi_is_hdr is always defined, but always returns false if STBI_NO_HDR
stbi_is_hdr_from_callbacks :: (clbk: *stbi_io_callbacks, user: *void) -> s32 #foreign stb_image;
stbi_is_hdr_from_memory :: (buffer: *u8, len: s32) -> s32 #foreign stb_image;
stbi_is_hdr :: (filename: *u8) -> s32 #foreign stb_image;
stbi_is_hdr_from_file :: (f: *FILE) -> s32 #foreign stb_image;
// get a VERY brief reason for failure
// on most compilers (and ALL modern mainstream compilers) this is threadsafe
stbi_failure_reason :: () -> *u8 #foreign stb_image;
// free the loaded image -- this is just free()
stbi_image_free :: (retval_from_stbi_load: *void) -> void #foreign stb_image;
// get image dimensions & components without fully decoding
stbi_info_from_memory :: (buffer: *u8, len: s32, x: *s32, y: *s32, comp: *s32) -> s32 #foreign stb_image;
stbi_info_from_callbacks :: (clbk: *stbi_io_callbacks, user: *void, x: *s32, y: *s32, comp: *s32) -> s32 #foreign stb_image;
stbi_is_16_bit_from_memory :: (buffer: *u8, len: s32) -> s32 #foreign stb_image;
stbi_is_16_bit_from_callbacks :: (clbk: *stbi_io_callbacks, user: *void) -> s32 #foreign stb_image;
stbi_info :: (filename: *u8, x: *s32, y: *s32, comp: *s32) -> s32 #foreign stb_image;
stbi_info_from_file :: (f: *FILE, x: *s32, y: *s32, comp: *s32) -> s32 #foreign stb_image;
stbi_is_16_bit :: (filename: *u8) -> s32 #foreign stb_image;
stbi_is_16_bit_from_file :: (f: *FILE) -> s32 #foreign stb_image;
// for image formats that explicitly notate that they have premultiplied alpha,
// we just return the colors as stored in the file. set this flag to force
// unpremultiplication. results are undefined if the unpremultiply overflow.
stbi_set_unpremultiply_on_load :: (flag_true_if_should_unpremultiply: s32) -> void #foreign stb_image;
// indicate whether we should process iphone images back to canonical format,
// or just pass them through "as-is"
stbi_convert_iphone_png_to_rgb :: (flag_true_if_should_convert: s32) -> void #foreign stb_image;
// flip the image vertically, so the first pixel in the output array is the bottom left
stbi_set_flip_vertically_on_load :: (flag_true_if_should_flip: s32) -> void #foreign stb_image;
// as above, but only applies to images loaded on the thread that calls the function
// this function is only available if your compiler supports thread-local variables;
// calling it will fail to link if your compiler doesn't
stbi_set_unpremultiply_on_load_thread :: (flag_true_if_should_unpremultiply: s32) -> void #foreign stb_image;
stbi_convert_iphone_png_to_rgb_thread :: (flag_true_if_should_convert: s32) -> void #foreign stb_image;
stbi_set_flip_vertically_on_load_thread :: (flag_true_if_should_flip: s32) -> void #foreign stb_image;
// ZLIB client - used by PNG, available for other purposes
stbi_zlib_decode_malloc_guesssize :: (buffer: *u8, len: s32, initial_size: s32, outlen: *s32) -> *u8 #foreign stb_image;
stbi_zlib_decode_malloc_guesssize_headerflag :: (buffer: *u8, len: s32, initial_size: s32, outlen: *s32, parse_header: s32) -> *u8 #foreign stb_image;
stbi_zlib_decode_malloc :: (buffer: *u8, len: s32, outlen: *s32) -> *u8 #foreign stb_image;
stbi_zlib_decode_buffer :: (obuffer: *u8, olen: s32, ibuffer: *u8, ilen: s32) -> s32 #foreign stb_image;
stbi_zlib_decode_noheader_malloc :: (buffer: *u8, len: s32, outlen: *s32) -> *u8 #foreign stb_image;
stbi_zlib_decode_noheader_buffer :: (obuffer: *u8, olen: s32, ibuffer: *u8, ilen: s32) -> s32 #foreign stb_image;
#scope_file
#if OS == .WINDOWS {
stb_image :: #library "windows/stb_image";
} else #if OS == .LINUX {
stb_image :: #library "linux/stb_image";
} else #if OS == .MACOS {
stb_image :: #library "macos/stb_image";
} else #if OS == .ANDROID {
#if CPU == .X64 {
stb_image :: #library "android/x64/stb_image";
} else #if CPU == .ARM64 {
stb_image :: #library "android/arm64/stb_image";
}
} else #if OS == .PS5 {
stb_image :: #library "ps5/stb_image";
} else #if OS == .WASM {
stb_image :: #library "wasm/stb_image";
} else {
#assert false;
}

View File

@ -0,0 +1,151 @@
AT_COMPILE_TIME :: true;
SOURCE_PATH :: "source";
LIB_BASE_NAME :: "stb_image";
#if AT_COMPILE_TIME {
#run,stallable {
set_build_options_dc(.{do_output=false});
options := get_build_options();
args := options.compile_time_command_line;
if !generate_bindings(args, options.minimum_os_version) {
compiler_set_workspace_status(.FAILED);
}
}
} else {
#import "System";
main :: () {
set_working_directory(path_strip_filename(get_path_of_running_executable()));
if !generate_bindings(get_command_line_arguments(), #run get_build_options().minimum_os_version) {
exit(1);
}
}
}
generate_bindings :: (args: [] string, minimum_os_version: type_of(Build_Options.minimum_os_version)) -> bool {
target_android := array_find(args, "-android");
target_x64 := array_find(args, "-x64");
target_arm := array_find(args, "-arm64");
compile := array_find(args, "-compile");
compile_debug := array_find(args, "-debug");
os_target := OS;
cpu_target := CPU;
if target_android os_target = .ANDROID;
if target_x64 cpu_target = .X64;
if target_arm cpu_target = .ARM64;
lib_directory: string;
if os_target == {
case .WINDOWS;
lib_directory = "windows";
case .LINUX;
lib_directory = "linux";
case .MACOS;
lib_directory = "macos";
case .ANDROID;
lib_directory = ifx cpu_target == .X64 then "android/x64" else "android/arm64";
case .PS5;
lib_directory = "ps5";
case;
assert(false);
}
if compile {
source_file := tprint("%/stb_image.c", SOURCE_PATH);
make_directory_if_it_does_not_exist(lib_directory, recursive = true);
lib_path := tprint("%/%", lib_directory, LIB_BASE_NAME);
success := true;
if os_target == .MACOS {
lib_path_x64 := tprint("%_x64", lib_path);
lib_path_arm64 := tprint("%_arm64", lib_path);
macos_x64_version_arg := "-mmacos-version-min=10.13"; // Our current x64 min version
macos_arm64_version_arg := "-mmacos-version-min=11.0"; // Earliest version that supports arm64
// x64 variant
success &&= build_cpp_dynamic_lib(lib_path_x64, source_file, extra = .["-arch", "x86_64", macos_x64_version_arg], debug=compile_debug);
success &&= build_cpp_static_lib( lib_path_x64, source_file, extra = .["-arch", "x86_64", macos_x64_version_arg], debug=compile_debug);
// arm64 variant
success &&= build_cpp_dynamic_lib(lib_path_arm64, source_file, extra = .["-arch", "arm64", macos_arm64_version_arg], debug=compile_debug);
success &&= build_cpp_static_lib( lib_path_arm64, source_file, extra = .["-arch", "arm64", macos_arm64_version_arg], debug=compile_debug);
// create universal binaries
run_result := run_command("lipo", "-create", tprint("%.dylib", lib_path_x64), tprint("%.dylib", lib_path_arm64), "-output", tprint("%.dylib", lib_path));
success &&= (run_result.exit_code == 0);
run_result = run_command("lipo", "-create", tprint("%.a", lib_path_x64), tprint("%.a", lib_path_arm64), "-output", tprint("%.a", lib_path));
success &&= (run_result.exit_code == 0);
} else {
extra: [..] string;
if os_target == .ANDROID {
_, target_triple_with_sdk := get_android_target_triple(cpu_target);
array_add(*extra, "-target", target_triple_with_sdk);
}
if os_target != .WINDOWS {
array_add(*extra, "-fPIC");
}
if os_target != .PS5 && os_target != .WASM {
success &&= build_cpp_dynamic_lib(lib_path, source_file, target = os_target, debug = compile_debug, extra = extra);
}
success &&= build_cpp_static_lib(lib_path, source_file, target = os_target, debug = compile_debug, extra = extra);
}
if !success return false;
}
options: Generate_Bindings_Options;
options.os = os_target;
options.cpu = cpu_target;
{
using options;
array_add(*libpaths, lib_directory);
array_add(*libnames, LIB_BASE_NAME);
array_add(*source_files, tprint("%/stb_image.h", SOURCE_PATH));
array_add(*typedef_prefixes_to_unwrap, "stbi_");
generate_library_declarations = false;
footer = tprint(FOOTER_TEMPLATE, LIB_BASE_NAME);
auto_detect_enum_prefixes = true;
log_stripped_declarations = false;
generate_compile_time_struct_checks = false;
}
output_filename := "bindings.jai";
return generate_bindings(options, output_filename);
}
FOOTER_TEMPLATE :: #string END
#if OS == .WINDOWS {
%1 :: #library "windows/%1";
} else #if OS == .LINUX {
%1 :: #library "linux/%1";
} else #if OS == .MACOS {
%1 :: #library "macos/%1";
} else #if OS == .ANDROID {
#if CPU == .X64 {
%1 :: #library "android/x64/%1";
} else #if CPU == .ARM64 {
%1 :: #library "android/arm64/%1";
}
} else #if OS == .PS5 {
%1 :: #library "ps5/%1";
} else #if OS == .WASM {
// Wasm will be linked with emcc.
} else {
#assert false;
}
END
#import "Basic";
#import "Bindings_Generator";
#import "BuildCpp";
#import "Compiler";
#import "File";
#import "Process";
#import "Toolchains/Android";

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,12 @@
#load "bindings.jai";
#if OS == .WINDOWS || OS == .PS5 || OS == .WASM {
#scope_module
FILE :: void;
} else #if OS_IS_UNIX {
#import "POSIX";
#library,system,link_always "libm";
} else {
#assert false;
}

View File

@ -0,0 +1,11 @@
#ifdef WIN32
#define __EXPORT __declspec(dllexport)
#else
#define __EXPORT
#endif
#define STBIDEF extern __EXPORT
#define STBI_NO_STDIO
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

File diff suppressed because it is too large Load Diff

Binary file not shown.

Binary file not shown.