Merge upstream

This commit is contained in:
Luiz Pestana 2022-05-27 20:56:18 -03:00
commit 03534f2ec6
15 changed files with 72287 additions and 50935 deletions

View file

@ -536,9 +536,15 @@ ifeq ($(PLATFORM),PLATFORM_DESKTOP)
endif
ifeq ($(PLATFORM),PLATFORM_RPI)
LDLIBS = -lbrcmGLESv2 -lbrcmEGL -lpthread -lrt -lm -lbcm_host -ldl
ifeq ($(RAYLIB_MODULE_AUDIO),TRUE)
LDLIBS += -latomic
endif
endif
ifeq ($(PLATFORM),PLATFORM_DRM)
LDLIBS = -lGLESv2 -lEGL -ldrm -lgbm -lpthread -lrt -lm -ldl
ifeq ($(RAYLIB_MODULE_AUDIO),TRUE)
LDLIBS += -latomic
endif
endif
ifeq ($(PLATFORM),PLATFORM_ANDROID)
LDLIBS = -llog -landroid -lEGL -lGLESv2 -lOpenSLES -lc -lm
@ -696,17 +702,26 @@ nxusb.o : nxusb.c nxusb.h raylib.h
raygui.o : raygui.c
$(CC) -c $< $(CFLAGS) $(INCLUDE_PATHS)
raygui.c:
echo #define RAYGUI_IMPLEMENTATION > raygui.c
echo #include "$(RAYLIB_MODULE_RAYGUI_PATH)/raygui.h" >> raygui.c
ifeq ($(PLATFORM_SHELL), cmd)
@echo #define RAYGUI_IMPLEMENTATION > raygui.c
@echo #include "$(RAYLIB_MODULE_RAYGUI_PATH)/raygui.h" >> raygui.c
else
@echo "#define RAYGUI_IMPLEMENTATION" > raygui.c
@echo "#include \"$(RAYLIB_MODULE_RAYGUI_PATH)/raygui.h\"" >> raygui.c
endif
# Compile physac module
# NOTE: physac header should be distributed with raylib.h
physac.o : physac.c
$(CC) -c $< $(CFLAGS) $(INCLUDE_PATHS)
physac.c:
ifeq ($(PLATFORM_SHELL), cmd)
@echo #define PHYSAC_IMPLEMENTATION > physac.c
@echo #include "$(RAYLIB_MODULE_PHYSAC_PATH)/physac.h" >> physac.c
else
@echo "#define PHYSAC_IMPLEMENTATION" > physac.c
@echo "#include \"$(RAYLIB_MODULE_PHYSAC_PATH)/physac.h\"" >> physac.c
endif
# Compile android_native_app_glue module
android_native_app_glue.o : $(NATIVE_APP_GLUE)/android_native_app_glue.c
$(CC) -c $< $(CFLAGS) $(INCLUDE_PATHS)

View file

@ -1,89 +1,81 @@
const std = @import("std");
pub fn Pkg(srcdir: []const u8) type {
return struct {
pub fn addRaylib(b: *std.build.Builder, target: std.zig.CrossTarget) *std.build.LibExeObjStep {
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
pub fn addRaylib(b: *std.build.Builder, target: std.zig.CrossTarget) *std.build.LibExeObjStep {
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const raylib_flags = &[_][]const u8{
"-std=gnu99",
"-DPLATFORM_DESKTOP",
"-DGL_SILENCE_DEPRECATION=199309L",
"-fno-sanitize=undefined", // https://github.com/raysan5/raylib/issues/1891
};
const raylib = b.addStaticLibrary("raylib", srcdir ++ "/raylib.h");
raylib.setTarget(target);
raylib.setBuildMode(mode);
raylib.linkLibC();
raylib.addIncludeDir(srcdir ++ "/external/glfw/include");
raylib.addCSourceFiles(&.{
srcdir ++ "/raudio.c",
srcdir ++ "/rcore.c",
srcdir ++ "/rmodels.c",
srcdir ++ "/rshapes.c",
srcdir ++ "/rtext.c",
srcdir ++ "/rtextures.c",
srcdir ++ "/utils.c",
}, raylib_flags);
switch (raylib.target.toTarget().os.tag) {
.windows => {
raylib.addCSourceFiles(&.{srcdir ++ "/rglfw.c"}, raylib_flags);
raylib.linkSystemLibrary("winmm");
raylib.linkSystemLibrary("gdi32");
raylib.linkSystemLibrary("opengl32");
raylib.addIncludeDir("external/glfw/deps/mingw");
},
.linux => {
raylib.addCSourceFiles(&.{srcdir ++ "/rglfw.c"}, raylib_flags);
raylib.linkSystemLibrary("GL");
raylib.linkSystemLibrary("rt");
raylib.linkSystemLibrary("dl");
raylib.linkSystemLibrary("m");
raylib.linkSystemLibrary("X11");
},
.freebsd, .openbsd, .netbsd, .dragonfly => {
raylib.addCSourceFiles(&.{srcdir ++ "/rglfw.c"}, raylib_flags);
raylib.linkSystemLibrary("GL");
raylib.linkSystemLibrary("rt");
raylib.linkSystemLibrary("dl");
raylib.linkSystemLibrary("m");
raylib.linkSystemLibrary("X11");
raylib.linkSystemLibrary("Xrandr");
raylib.linkSystemLibrary("Xinerama");
raylib.linkSystemLibrary("Xi");
raylib.linkSystemLibrary("Xxf86vm");
raylib.linkSystemLibrary("Xcursor");
},
.macos => {
// On macos rglfw.c include Objective-C files.
const raylib_flags_extra_macos = &[_][]const u8{
"-ObjC",
};
raylib.addCSourceFiles(
&.{srcdir ++ "/rglfw.c"},
raylib_flags ++ raylib_flags_extra_macos,
);
raylib.linkFramework("Foundation");
},
else => {
@panic("Unsupported OS");
},
}
raylib.setOutputDir("./");
raylib.install();
return raylib;
}
const raylib_flags = &[_][]const u8{
"-std=gnu99",
"-DPLATFORM_DESKTOP",
"-DGL_SILENCE_DEPRECATION=199309L",
"-fno-sanitize=undefined", // https://github.com/raysan5/raylib/issues/1891
};
}
const lib = Pkg(".");
const raylib = b.addStaticLibrary("raylib", srcdir ++ "/raylib.h");
raylib.setTarget(target);
raylib.setBuildMode(mode);
raylib.linkLibC();
raylib.addIncludeDir(srcdir ++ "/external/glfw/include");
raylib.addCSourceFiles(&.{
srcdir ++ "/raudio.c",
srcdir ++ "/rcore.c",
srcdir ++ "/rmodels.c",
srcdir ++ "/rshapes.c",
srcdir ++ "/rtext.c",
srcdir ++ "/rtextures.c",
srcdir ++ "/utils.c",
}, raylib_flags);
switch (raylib.target.toTarget().os.tag) {
.windows => {
raylib.addCSourceFiles(&.{srcdir ++ "/rglfw.c"}, raylib_flags);
raylib.linkSystemLibrary("winmm");
raylib.linkSystemLibrary("gdi32");
raylib.linkSystemLibrary("opengl32");
raylib.addIncludeDir("external/glfw/deps/mingw");
},
.linux => {
raylib.addCSourceFiles(&.{srcdir ++ "/rglfw.c"}, raylib_flags);
raylib.linkSystemLibrary("GL");
raylib.linkSystemLibrary("rt");
raylib.linkSystemLibrary("dl");
raylib.linkSystemLibrary("m");
raylib.linkSystemLibrary("X11");
},
.freebsd, .openbsd, .netbsd, .dragonfly => {
raylib.addCSourceFiles(&.{srcdir ++ "/rglfw.c"}, raylib_flags);
raylib.linkSystemLibrary("GL");
raylib.linkSystemLibrary("rt");
raylib.linkSystemLibrary("dl");
raylib.linkSystemLibrary("m");
raylib.linkSystemLibrary("X11");
raylib.linkSystemLibrary("Xrandr");
raylib.linkSystemLibrary("Xinerama");
raylib.linkSystemLibrary("Xi");
raylib.linkSystemLibrary("Xxf86vm");
raylib.linkSystemLibrary("Xcursor");
},
.macos => {
// On macos rglfw.c include Objective-C files.
const raylib_flags_extra_macos = &[_][]const u8{
"-ObjC",
};
raylib.addCSourceFiles(
&.{srcdir ++ "/rglfw.c"},
raylib_flags ++ raylib_flags_extra_macos,
);
raylib.linkFramework("Foundation");
},
else => {
@panic("Unsupported OS");
},
}
return raylib;
}
pub fn build(b: *std.build.Builder) void {
// Standard target options allows the person running `zig build` to choose
@ -92,5 +84,13 @@ pub fn build(b: *std.build.Builder) void {
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
_ = lib.addRaylib(b, target);
const lib = addRaylib(b, target);
lib.setOutputDir(srcdir);
lib.install();
}
const srcdir = getSrcDir();
fn getSrcDir() []const u8 {
return std.fs.path.dirname(@src().file) orelse ".";
}

34535
src/external/miniaudio.h vendored

File diff suppressed because it is too large Load diff

View file

@ -101,47 +101,47 @@ extern "C" { // Prevents name mangling of functions
#endif
// Linear Easing functions
EASEDEF float EaseLinearNone(float t, float b, float c, float d) { return (c*t/d + b); }
EASEDEF float EaseLinearIn(float t, float b, float c, float d) { return (c*t/d + b); }
EASEDEF float EaseLinearOut(float t, float b, float c, float d) { return (c*t/d + b); }
EASEDEF float EaseLinearInOut(float t,float b, float c, float d) { return (c*t/d + b); }
EASEDEF float EaseLinearNone(float t, float b, float c, float d) { return (c*t/d + b); } // Ease: Linear
EASEDEF float EaseLinearIn(float t, float b, float c, float d) { return (c*t/d + b); } // Ease: Linear In
EASEDEF float EaseLinearOut(float t, float b, float c, float d) { return (c*t/d + b); } // Ease: Linear Out
EASEDEF float EaseLinearInOut(float t, float b, float c, float d) { return (c*t/d + b); } // Ease: Linear In Out
// Sine Easing functions
EASEDEF float EaseSineIn(float t, float b, float c, float d) { return (-c*cosf(t/d*(PI/2.0f)) + c + b); }
EASEDEF float EaseSineOut(float t, float b, float c, float d) { return (c*sinf(t/d*(PI/2.0f)) + b); }
EASEDEF float EaseSineInOut(float t, float b, float c, float d) { return (-c/2.0f*(cosf(PI*t/d) - 1.0f) + b); }
EASEDEF float EaseSineIn(float t, float b, float c, float d) { return (-c*cosf(t/d*(PI/2.0f)) + c + b); } // Ease: Sine In
EASEDEF float EaseSineOut(float t, float b, float c, float d) { return (c*sinf(t/d*(PI/2.0f)) + b); } // Ease: Sine Out
EASEDEF float EaseSineInOut(float t, float b, float c, float d) { return (-c/2.0f*(cosf(PI*t/d) - 1.0f) + b); } // Ease: Sine Out
// Circular Easing functions
EASEDEF float EaseCircIn(float t, float b, float c, float d) { t /= d; return (-c*(sqrtf(1.0f - t*t) - 1.0f) + b); }
EASEDEF float EaseCircOut(float t, float b, float c, float d) { t = t/d - 1.0f; return (c*sqrtf(1.0f - t*t) + b); }
EASEDEF float EaseCircInOut(float t, float b, float c, float d)
EASEDEF float EaseCircIn(float t, float b, float c, float d) { t /= d; return (-c*(sqrtf(1.0f - t*t) - 1.0f) + b); } // Ease: Circular In
EASEDEF float EaseCircOut(float t, float b, float c, float d) { t = t/d - 1.0f; return (c*sqrtf(1.0f - t*t) + b); } // Ease: Circular Out
EASEDEF float EaseCircInOut(float t, float b, float c, float d) // Ease: Circular In Out
{
if ((t/=d/2.0f) < 1.0f) return (-c/2.0f*(sqrtf(1.0f - t*t) - 1.0f) + b);
t -= 2.0f; return (c/2.0f*(sqrtf(1.0f - t*t) + 1.0f) + b);
}
// Cubic Easing functions
EASEDEF float EaseCubicIn(float t, float b, float c, float d) { t /= d; return (c*t*t*t + b); }
EASEDEF float EaseCubicOut(float t, float b, float c, float d) { t = t/d - 1.0f; return (c*(t*t*t + 1.0f) + b); }
EASEDEF float EaseCubicInOut(float t, float b, float c, float d)
EASEDEF float EaseCubicIn(float t, float b, float c, float d) { t /= d; return (c*t*t*t + b); } // Ease: Cubic In
EASEDEF float EaseCubicOut(float t, float b, float c, float d) { t = t/d - 1.0f; return (c*(t*t*t + 1.0f) + b); } // Ease: Cubic Out
EASEDEF float EaseCubicInOut(float t, float b, float c, float d) // Ease: Cubic In Out
{
if ((t/=d/2.0f) < 1.0f) return (c/2.0f*t*t*t + b);
t -= 2.0f; return (c/2.0f*(t*t*t + 2.0f) + b);
}
// Quadratic Easing functions
EASEDEF float EaseQuadIn(float t, float b, float c, float d) { t /= d; return (c*t*t + b); }
EASEDEF float EaseQuadOut(float t, float b, float c, float d) { t /= d; return (-c*t*(t - 2.0f) + b); }
EASEDEF float EaseQuadInOut(float t, float b, float c, float d)
EASEDEF float EaseQuadIn(float t, float b, float c, float d) { t /= d; return (c*t*t + b); } // Ease: Quadratic In
EASEDEF float EaseQuadOut(float t, float b, float c, float d) { t /= d; return (-c*t*(t - 2.0f) + b); } // Ease: Quadratic Out
EASEDEF float EaseQuadInOut(float t, float b, float c, float d) // Ease: Quadratic In Out
{
if ((t/=d/2) < 1) return (((c/2)*(t*t)) + b);
return (-c/2.0f*(((t - 1.0f)*(t - 3.0f)) - 1.0f) + b);
}
// Exponential Easing functions
EASEDEF float EaseExpoIn(float t, float b, float c, float d) { return (t == 0.0f) ? b : (c*powf(2.0f, 10.0f*(t/d - 1.0f)) + b); }
EASEDEF float EaseExpoOut(float t, float b, float c, float d) { return (t == d) ? (b + c) : (c*(-powf(2.0f, -10.0f*t/d) + 1.0f) + b); }
EASEDEF float EaseExpoInOut(float t, float b, float c, float d)
EASEDEF float EaseExpoIn(float t, float b, float c, float d) { return (t == 0.0f) ? b : (c*powf(2.0f, 10.0f*(t/d - 1.0f)) + b); } // Ease: Exponential In
EASEDEF float EaseExpoOut(float t, float b, float c, float d) { return (t == d) ? (b + c) : (c*(-powf(2.0f, -10.0f*t/d) + 1.0f) + b); } // Ease: Exponential Out
EASEDEF float EaseExpoInOut(float t, float b, float c, float d) // Ease: Exponential In Out
{
if (t == 0.0f) return b;
if (t == d) return (b + c);
@ -151,21 +151,21 @@ EASEDEF float EaseExpoInOut(float t, float b, float c, float d)
}
// Back Easing functions
EASEDEF float EaseBackIn(float t, float b, float c, float d)
EASEDEF float EaseBackIn(float t, float b, float c, float d) // Ease: Back In
{
float s = 1.70158f;
float postFix = t/=d;
return (c*(postFix)*t*((s + 1.0f)*t - s) + b);
}
EASEDEF float EaseBackOut(float t, float b, float c, float d)
EASEDEF float EaseBackOut(float t, float b, float c, float d) // Ease: Back Out
{
float s = 1.70158f;
t = t/d - 1.0f;
return (c*(t*t*((s + 1.0f)*t + s) + 1.0f) + b);
}
EASEDEF float EaseBackInOut(float t, float b, float c, float d)
EASEDEF float EaseBackInOut(float t, float b, float c, float d) // Ease: Back In Out
{
float s = 1.70158f;
if ((t/=d/2.0f) < 1.0f)
@ -180,7 +180,7 @@ EASEDEF float EaseBackInOut(float t, float b, float c, float d)
}
// Bounce Easing functions
EASEDEF float EaseBounceOut(float t, float b, float c, float d)
EASEDEF float EaseBounceOut(float t, float b, float c, float d) // Ease: Bounce Out
{
if ((t/=d) < (1.0f/2.75f))
{
@ -203,15 +203,15 @@ EASEDEF float EaseBounceOut(float t, float b, float c, float d)
}
}
EASEDEF float EaseBounceIn(float t, float b, float c, float d) { return (c - EaseBounceOut(d - t, 0.0f, c, d) + b); }
EASEDEF float EaseBounceInOut(float t, float b, float c, float d)
EASEDEF float EaseBounceIn(float t, float b, float c, float d) { return (c - EaseBounceOut(d - t, 0.0f, c, d) + b); } // Ease: Bounce In
EASEDEF float EaseBounceInOut(float t, float b, float c, float d) // Ease: Bounce In Out
{
if (t < d/2.0f) return (EaseBounceIn(t*2.0f, 0.0f, c, d)*0.5f + b);
else return (EaseBounceOut(t*2.0f - d, 0.0f, c, d)*0.5f + c*0.5f + b);
}
// Elastic Easing functions
EASEDEF float EaseElasticIn(float t, float b, float c, float d)
EASEDEF float EaseElasticIn(float t, float b, float c, float d) // Ease: Elastic In
{
if (t == 0.0f) return b;
if ((t/=d) == 1.0f) return (b + c);
@ -224,7 +224,7 @@ EASEDEF float EaseElasticIn(float t, float b, float c, float d)
return (-(postFix*sinf((t*d-s)*(2.0f*PI)/p )) + b);
}
EASEDEF float EaseElasticOut(float t, float b, float c, float d)
EASEDEF float EaseElasticOut(float t, float b, float c, float d) // Ease: Elastic Out
{
if (t == 0.0f) return b;
if ((t/=d) == 1.0f) return (b + c);
@ -236,7 +236,7 @@ EASEDEF float EaseElasticOut(float t, float b, float c, float d)
return (a*powf(2.0f,-10.0f*t)*sinf((t*d-s)*(2.0f*PI)/p) + c + b);
}
EASEDEF float EaseElasticInOut(float t, float b, float c, float d)
EASEDEF float EaseElasticInOut(float t, float b, float c, float d) // Ease: Elastic In Out
{
if (t == 0.0f) return b;
if ((t/=d/2.0f) == 2.0f) return (b + c);

View file

@ -1999,7 +1999,6 @@ bool GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode)
{
keyCount--;
text[keyCount] = '\0';
if (keyCount < 0) keyCount = 0;
}
}
@ -2177,7 +2176,6 @@ bool GuiValueBox(Rectangle bounds, const char *text, int *value, int minValue, i
{
keyCount--;
textValue[keyCount] = '\0';
if (keyCount < 0) keyCount = 0;
valueHasChanged = true;
}
}

View file

@ -247,10 +247,6 @@ typedef struct tagBITMAPINFOHEADER {
#include "external/dr_flac.h" // FLAC loading functions
#endif
#if defined(_MSC_VER)
#undef bool
#endif
//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
@ -311,10 +307,13 @@ typedef enum {
AUDIO_BUFFER_USAGE_STREAM
} AudioBufferUsage;
// Audio buffer structure
// Audio buffer struct
struct rAudioBuffer {
ma_data_converter converter; // Audio data converter
AudioCallback callback; // Audio buffer callback for buffer filling on audio threads
rAudioProcessor *processor; // Audio processor
float volume; // Audio buffer volume
float pitch; // Audio buffer pitch
float pan; // Audio buffer pan (0.0f to 1.0f)
@ -335,6 +334,14 @@ struct rAudioBuffer {
rAudioBuffer *prev; // Previous audio buffer on the list
};
// Audio processor struct
// NOTE: Useful to apply effects to an AudioBuffer
struct rAudioProcessor {
AudioCallback process; // Processor callback function
rAudioProcessor *next; // Next audio processor on the list
rAudioProcessor *prev; // Previous audio processor on the list
};
#define AudioBuffer rAudioBuffer // HACK: To avoid CoreAudio (macOS) symbol collision
// Audio data context
@ -372,7 +379,7 @@ static AudioData AUDIO = { // Global AUDIO context
//----------------------------------------------------------------------------------
// Module specific Functions Declaration
//----------------------------------------------------------------------------------
static void OnLog(ma_context *pContext, ma_device *pDevice, ma_uint32 logLevel, const char *message);
static void OnLog(void *pUserData, ma_uint32 level, const char *pMessage);
static void OnSendAudioDataToDevice(ma_device *pDevice, void *pFramesOut, const void *pFramesInput, ma_uint32 frameCount);
static void MixAudioFrames(float *framesOut, const float *framesIn, ma_uint32 frameCount, AudioBuffer *buffer);
@ -411,7 +418,7 @@ void InitAudioDevice(void)
{
// Init audio context
ma_context_config ctxConfig = ma_context_config_init();
ctxConfig.logCallback = OnLog;
ma_log_callback_init(OnLog, NULL);
ma_result result = ma_context_init(NULL, 0, &ctxConfig, &AUDIO.System.context);
if (result != MA_SUCCESS)
@ -465,8 +472,7 @@ void InitAudioDevice(void)
// Init dummy audio buffers pool for multichannel sound playing
for (int i = 0; i < MAX_AUDIO_BUFFER_POOL_CHANNELS; i++)
{
// WARNING: An empty audio buffer is created (data = 0)
// AudioBuffer data just points to loaded sound data
// WARNING: An empty audio buffer is created (data = 0) and added to list, AudioBuffer data is filled on PlaySoundMulti()
AUDIO.MultiChannel.pool[i] = LoadAudioBuffer(AUDIO_DEVICE_FORMAT, AUDIO_DEVICE_CHANNELS, AUDIO.System.device.sampleRate, 0, AUDIO_BUFFER_USAGE_STATIC);
}
@ -492,7 +498,7 @@ void CloseAudioDevice(void)
//UnloadAudioBuffer(AUDIO.MultiChannel.pool[i]);
if (AUDIO.MultiChannel.pool[i] != NULL)
{
ma_data_converter_uninit(&AUDIO.MultiChannel.pool[i]->converter);
ma_data_converter_uninit(&AUDIO.MultiChannel.pool[i]->converter, NULL);
UntrackAudioBuffer(AUDIO.MultiChannel.pool[i]);
//RL_FREE(buffer->data); // Already unloaded by UnloadSound()
RL_FREE(AUDIO.MultiChannel.pool[i]);
@ -541,9 +547,9 @@ AudioBuffer *LoadAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sam
// Audio data runs through a format converter
ma_data_converter_config converterConfig = ma_data_converter_config_init(format, AUDIO_DEVICE_FORMAT, channels, AUDIO_DEVICE_CHANNELS, sampleRate, AUDIO.System.device.sampleRate);
converterConfig.resampling.allowDynamicSampleRate = true; // Pitch shifting
converterConfig.allowDynamicSampleRate = true;
ma_result result = ma_data_converter_init(&converterConfig, &audioBuffer->converter);
ma_result result = ma_data_converter_init(&converterConfig, NULL, &audioBuffer->converter);
if (result != MA_SUCCESS)
{
@ -557,9 +563,13 @@ AudioBuffer *LoadAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sam
audioBuffer->pitch = 1.0f;
audioBuffer->pan = 0.5f;
audioBuffer->callback = NULL;
audioBuffer->processor = NULL;
audioBuffer->playing = false;
audioBuffer->paused = false;
audioBuffer->looping = false;
audioBuffer->usage = usage;
audioBuffer->frameCursorPos = 0;
audioBuffer->sizeInFrames = sizeInFrames;
@ -580,7 +590,7 @@ void UnloadAudioBuffer(AudioBuffer *buffer)
{
if (buffer != NULL)
{
ma_data_converter_uninit(&buffer->converter);
ma_data_converter_uninit(&buffer->converter, NULL);
UntrackAudioBuffer(buffer);
RL_FREE(buffer->data);
RL_FREE(buffer);
@ -654,8 +664,8 @@ void SetAudioBufferPitch(AudioBuffer *buffer, float pitch)
// Note that this changes the duration of the sound:
// - higher pitches will make the sound faster
// - lower pitches make it slower
ma_uint32 outputSampleRate = (ma_uint32)((float)buffer->converter.config.sampleRateOut/pitch);
ma_data_converter_set_rate(&buffer->converter, buffer->converter.config.sampleRateIn, outputSampleRate);
ma_uint32 outputSampleRate = (ma_uint32)((float)buffer->converter.sampleRateOut/pitch);
ma_data_converter_set_rate(&buffer->converter, buffer->converter.sampleRateIn, outputSampleRate);
buffer->pitch = pitch;
}
@ -894,7 +904,7 @@ void UpdateSound(Sound sound, const void *data, int sampleCount)
StopAudioBuffer(sound.stream.buffer);
// TODO: May want to lock/unlock this since this data buffer is read at mixing time
memcpy(sound.stream.buffer->data, data, sampleCount*ma_get_bytes_per_frame(sound.stream.buffer->converter.config.formatIn, sound.stream.buffer->converter.config.channelsIn));
memcpy(sound.stream.buffer->data, data, sampleCount*ma_get_bytes_per_frame(sound.stream.buffer->converter.formatIn, sound.stream.buffer->converter.channelsIn));
}
}
@ -1068,7 +1078,8 @@ void PlaySoundMulti(Sound sound)
AUDIO.MultiChannel.pool[index]->isSubBufferProcessed[0] = false;
AUDIO.MultiChannel.pool[index]->isSubBufferProcessed[1] = false;
AUDIO.MultiChannel.pool[index]->sizeInFrames = sound.stream.buffer->sizeInFrames;
AUDIO.MultiChannel.pool[index]->data = sound.stream.buffer->data;
AUDIO.MultiChannel.pool[index]->data = sound.stream.buffer->data; // Fill dummy track with data for playing
PlayAudioBuffer(AUDIO.MultiChannel.pool[index]);
}
@ -1191,8 +1202,7 @@ Wave WaveCopy(Wave wave)
// NOTE: Security check in case of out-of-range
void WaveCrop(Wave *wave, int initSample, int finalSample)
{
if ((initSample >= 0) && (initSample < finalSample) &&
(finalSample > 0) && ((unsigned int)finalSample < (wave->frameCount*wave->channels)))
if ((initSample >= 0) && (initSample < finalSample) && ((unsigned int)finalSample < (wave->frameCount*wave->channels)))
{
int sampleCount = finalSample - initSample;
@ -1503,16 +1513,14 @@ Music LoadMusicStreamFromMemory(const char *fileType, const unsigned char *data,
jar_xm_set_max_loop_count(ctxXm, 0); // Set infinite number of loops
unsigned int bits = 32;
if (AUDIO_DEVICE_FORMAT == ma_format_s16)
bits = 16;
else if (AUDIO_DEVICE_FORMAT == ma_format_u8)
bits = 8;
if (AUDIO_DEVICE_FORMAT == ma_format_s16) bits = 16;
else if (AUDIO_DEVICE_FORMAT == ma_format_u8) bits = 8;
// NOTE: Only stereo is supported for XM
music.stream = LoadAudioStream(AUDIO.System.device.sampleRate, bits, 2);
music.frameCount = (unsigned int)jar_xm_get_remaining_samples(ctxXm); // NOTE: Always 2 channels (stereo)
music.looping = true; // Looping enabled by default
jar_xm_reset(ctxXm); // make sure we start at the beginning of the song
jar_xm_reset(ctxXm); // make sure we start at the beginning of the song
music.ctxData = ctxXm;
musicLoaded = true;
@ -1533,7 +1541,7 @@ Music LoadMusicStreamFromMemory(const char *fileType, const unsigned char *data,
for (int i = 0; i < it; i++) newData[i] = data[i];
// Memory loaded version for jar_mod_load_file()
if (dataSize && dataSize < 32*1024*1024)
if (dataSize && (dataSize < 32*1024*1024))
{
ctxMod->modfilesize = dataSize;
ctxMod->modfile = newData;
@ -2028,22 +2036,86 @@ void SetAudioStreamBufferSizeDefault(int size)
AUDIO.Buffer.defaultSize = size;
}
// Audio thread callback to request new data
void SetAudioStreamCallback(AudioStream stream, AudioCallback callback)
{
if (stream.buffer != NULL) stream.buffer->callback = callback;
}
// Add processor to audio stream. Contrary to buffers, the order of processors is important.
// The new processor must be added at the end. As there aren't supposed to be a lot of processors attached to
// a given stream, we iterate through the list to find the end. That way we don't need a pointer to the last element.
void AttachAudioStreamProcessor(AudioStream stream, AudioCallback process)
{
ma_mutex_lock(&AUDIO.System.lock);
rAudioProcessor *processor = (rAudioProcessor *)RL_CALLOC(1, sizeof(rAudioProcessor));
processor->process = process;
rAudioProcessor *last = stream.buffer->processor;
while (last && last->next)
{
last = last->next;
}
if (last)
{
processor->prev = last;
last->next = processor;
}
else stream.buffer->processor = processor;
ma_mutex_unlock(&AUDIO.System.lock);
}
void DetachAudioStreamProcessor(AudioStream stream, AudioCallback process)
{
ma_mutex_lock(&AUDIO.System.lock);
rAudioProcessor *processor = stream.buffer->processor;
while (processor)
{
rAudioProcessor *next = processor->next;
rAudioProcessor *prev = processor->prev;
if (processor->process == process)
{
if (stream.buffer->processor == processor) stream.buffer->processor = next;
if (prev) prev->next = next;
if (next) next->prev = prev;
RL_FREE(processor);
}
processor = next;
}
ma_mutex_unlock(&AUDIO.System.lock);
}
//----------------------------------------------------------------------------------
// Module specific Functions Definition
//----------------------------------------------------------------------------------
// Log callback function
static void OnLog(ma_context *pContext, ma_device *pDevice, ma_uint32 logLevel, const char *message)
static void OnLog(void *pUserData, ma_uint32 level, const char *pMessage)
{
(void)pContext;
(void)pDevice;
TRACELOG(LOG_WARNING, "miniaudio: %s", message); // All log messages from miniaudio are errors
TRACELOG(LOG_WARNING, "miniaudio: %s", pMessage); // All log messages from miniaudio are errors
}
// Reads audio data from an AudioBuffer object in internal format.
static ma_uint32 ReadAudioBufferFramesInInternalFormat(AudioBuffer *audioBuffer, void *framesOut, ma_uint32 frameCount)
{
// Using audio buffer callback
if (audioBuffer->callback)
{
audioBuffer->callback(framesOut, frameCount);
audioBuffer->framesProcessed += frameCount;
return frameCount;
}
ma_uint32 subBufferSizeInFrames = (audioBuffer->sizeInFrames > 1)? audioBuffer->sizeInFrames/2 : audioBuffer->sizeInFrames;
ma_uint32 currentSubBufferIndex = audioBuffer->frameCursorPos/subBufferSizeInFrames;
@ -2055,7 +2127,7 @@ static ma_uint32 ReadAudioBufferFramesInInternalFormat(AudioBuffer *audioBuffer,
isSubBufferProcessed[0] = audioBuffer->isSubBufferProcessed[0];
isSubBufferProcessed[1] = audioBuffer->isSubBufferProcessed[1];
ma_uint32 frameSizeInBytes = ma_get_bytes_per_frame(audioBuffer->converter.config.formatIn, audioBuffer->converter.config.channelsIn);
ma_uint32 frameSizeInBytes = ma_get_bytes_per_frame(audioBuffer->converter.formatIn, audioBuffer->converter.channelsIn);
// Fill out every frame until we find a buffer that's marked as processed. Then fill the remainder with 0
ma_uint32 framesRead = 0;
@ -2135,20 +2207,21 @@ static ma_uint32 ReadAudioBufferFramesInMixingFormat(AudioBuffer *audioBuffer, f
// detail to remember here is that we never, ever attempt to read more input data than is required for the specified number of output
// frames. This can be achieved with ma_data_converter_get_required_input_frame_count().
ma_uint8 inputBuffer[4096] = { 0 };
ma_uint32 inputBufferFrameCap = sizeof(inputBuffer)/ma_get_bytes_per_frame(audioBuffer->converter.config.formatIn, audioBuffer->converter.config.channelsIn);
ma_uint32 inputBufferFrameCap = sizeof(inputBuffer)/ma_get_bytes_per_frame(audioBuffer->converter.formatIn, audioBuffer->converter.channelsIn);
ma_uint32 totalOutputFramesProcessed = 0;
while (totalOutputFramesProcessed < frameCount)
{
ma_uint64 outputFramesToProcessThisIteration = frameCount - totalOutputFramesProcessed;
ma_uint64 inputFramesToProcessThisIteration = 0;
ma_uint64 inputFramesToProcessThisIteration = ma_data_converter_get_required_input_frame_count(&audioBuffer->converter, outputFramesToProcessThisIteration);
ma_result result = ma_data_converter_get_required_input_frame_count(&audioBuffer->converter, outputFramesToProcessThisIteration, &inputFramesToProcessThisIteration);
if (inputFramesToProcessThisIteration > inputBufferFrameCap)
{
inputFramesToProcessThisIteration = inputBufferFrameCap;
}
float *runningFramesOut = framesOut + (totalOutputFramesProcessed*audioBuffer->converter.config.channelsOut);
float *runningFramesOut = framesOut + (totalOutputFramesProcessed*audioBuffer->converter.channelsOut);
/* At this point we can convert the data to our mixing format. */
ma_uint64 inputFramesProcessedThisIteration = ReadAudioBufferFramesInInternalFormat(audioBuffer, inputBuffer, (ma_uint32)inputFramesToProcessThisIteration); /* Safe cast. */
@ -2217,6 +2290,14 @@ static void OnSendAudioDataToDevice(ma_device *pDevice, void *pFramesOut, const
float *framesOut = (float *)pFramesOut + (framesRead*AUDIO.System.device.playback.channels);
float *framesIn = tempBuffer;
// Apply processors chain if defined
rAudioProcessor *processor = audioBuffer->processor;
while (processor)
{
processor->process(framesIn, framesJustRead);
processor = processor->next;
}
MixAudioFrames(framesOut, framesIn, framesJustRead, audioBuffer);
framesToRead -= framesJustRead;
@ -2257,41 +2338,44 @@ static void OnSendAudioDataToDevice(ma_device *pDevice, void *pFramesOut, const
ma_mutex_unlock(&AUDIO.System.lock);
}
// This is the main mixing function. Mixing is pretty simple in this project - it's just an accumulation.
// NOTE: framesOut is both an input and an output. It will be initially filled with zeros outside of this function.
// Main mixing function, pretty simple in this project, just an accumulation
// NOTE: framesOut is both an input and an output, it is initially filled with zeros outside of this function
static void MixAudioFrames(float *framesOut, const float *framesIn, ma_uint32 frameCount, AudioBuffer *buffer)
{
const float localVolume = buffer->volume;
const ma_uint32 channels = AUDIO.System.device.playback.channels;
const ma_uint32 nChannels = AUDIO.System.device.playback.channels;
if (nChannels == 2)
if (channels == 2) // We consider panning
{
const float left = buffer->pan;
const float right = 1.0f - left;
// fast sine approximation in [0..1] for pan law: y = 0.5f * x * (3 - x * x);
const float levels[2] = { localVolume*0.5f*left*(3.0f-left*left), localVolume*0.5f*right*(3.0f-right*right) };
// Fast sine approximation in [0..1] for pan law: y = 0.5f*x*(3 - x*x);
const float levels[2] = { localVolume*0.5f*left*(3.0f - left*left), localVolume*0.5f*right*(3.0f - right*right) };
float *frameOut = framesOut;
const float *frameIn = framesIn;
for (ma_uint32 iFrame = 0; iFrame < frameCount; ++iFrame)
for (ma_uint32 frame = 0; frame < frameCount; frame++)
{
frameOut[0] += (frameIn[0]*levels[0]);
frameOut[1] += (frameIn[1]*levels[1]);
frameOut += 2;
frameIn += 2;
}
}
else // pan is kinda meaningless
else // We do not consider panning
{
for (ma_uint32 iFrame = 0; iFrame < frameCount; ++iFrame)
for (ma_uint32 frame = 0; frame < frameCount; frame++)
{
for (ma_uint32 iChannel = 0; iChannel < nChannels; ++iChannel)
for (ma_uint32 c = 0; c < channels; c++)
{
float *frameOut = framesOut + (iFrame * nChannels);
const float *frameIn = framesIn + (iFrame * nChannels);
float *frameOut = framesOut + (frame*channels);
const float *frameIn = framesIn + (frame*channels);
frameOut[iChannel] += (frameIn[iChannel] * localVolume);
// Output accumulates input multiplied by volume to provided output (usually 0)
frameOut[c] += (frameIn[c]*localVolume);
}
}
}

View file

@ -33,8 +33,8 @@
*
* OPTIONAL DEPENDENCIES (included):
* [rcore] msf_gif (Miles Fogle) for GIF recording
* [rcore] sinfl (Micha Mettke) for DEFLATE decompression algorythm
* [rcore] sdefl (Micha Mettke) for DEFLATE compression algorythm
* [rcore] sinfl (Micha Mettke) for DEFLATE decompression algorithm
* [rcore] sdefl (Micha Mettke) for DEFLATE compression algorithm
* [rtextures] stb_image (Sean Barret) for images loading (BMP, TGA, PNG, JPEG, HDR...)
* [rtextures] stb_image_write (Sean Barret) for image writing (BMP, TGA, PNG, JPG)
* [rtextures] stb_image_resize (Sean Barret) for image resizing algorithms
@ -177,10 +177,10 @@
// Structures Definition
//----------------------------------------------------------------------------------
// Boolean type
#if defined(__STDC__) && __STDC_VERSION__ >= 199901L
#if (defined(__STDC__) && __STDC_VERSION__ >= 199901L) || (defined(_MSC_VER) && _MSC_VER >= 1800)
#include <stdbool.h>
#elif !defined(__cplusplus) && !defined(bool)
typedef enum bool { false, true } bool;
typedef enum bool { false = 0, true = !false } bool;
#define RL_BOOL_TYPE
#endif
@ -322,7 +322,7 @@ typedef struct Mesh {
// Vertex attributes data
float *vertices; // Vertex position (XYZ - 3 components per vertex) (shader-location = 0)
float *texcoords; // Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1)
float *texcoords2; // Vertex second texture coordinates (useful for lightmaps) (shader-location = 5)
float *texcoords2; // Vertex texture second coordinates (UV - 2 components per vertex) (shader-location = 5)
float *normals; // Vertex normals (XYZ - 3 components per vertex) (shader-location = 2)
float *tangents; // Vertex tangents (XYZW - 4 components per vertex) (shader-location = 4)
unsigned char *colors; // Vertex colors (RGBA - 4 components per vertex) (shader-location = 3)
@ -425,11 +425,15 @@ typedef struct Wave {
void *data; // Buffer data pointer
} Wave;
// Opaque structs declaration
// NOTE: Actual structs are defined internally in raudio module
typedef struct rAudioBuffer rAudioBuffer;
typedef struct rAudioProcessor rAudioProcessor;
// AudioStream, custom audio stream
typedef struct AudioStream {
rAudioBuffer *buffer; // Pointer to internal data used by the audio system
rAudioProcessor *processor; // Pointer to internal data processor, useful for audio effects
unsigned int sampleRate; // Frequency (samples per second)
unsigned int sampleSize; // Bit depth (bits per sample): 8, 16, 32 (24 not supported)
@ -945,6 +949,8 @@ RLAPI Vector2 GetWindowScaleDPI(void); // Get window
RLAPI const char *GetMonitorName(int monitor); // Get the human-readable, UTF-8 encoded name of the primary monitor
RLAPI void SetClipboardText(const char *text); // Set clipboard text content
RLAPI const char *GetClipboardText(void); // Get clipboard text content
RLAPI void EnableEventWaiting(void); // Enable waiting for events on EndDrawing(), no automatic event polling
RLAPI void DisableEventWaiting(void); // Disable waiting for events on EndDrawing(), automatic events polling
// Custom frame control functions
// NOTE: Those functions are intended for advance users that want full control over the frame processing
@ -1033,9 +1039,10 @@ RLAPI void SetLoadFileTextCallback(LoadFileTextCallback callback); // Set custom
RLAPI void SetSaveFileTextCallback(SaveFileTextCallback callback); // Set custom file text data saver
// Files management functions
RLAPI unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead); // Load file data as byte array (read)
RLAPI unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead); // Load file data as byte array (read)
RLAPI void UnloadFileData(unsigned char *data); // Unload file data allocated by LoadFileData()
RLAPI bool SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite); // Save data to file from byte array (write), returns true on success
RLAPI bool SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite); // Save data to file from byte array (write), returns true on success
RLAPI bool ExportDataAsCode(const char *data, unsigned int size, const char *fileName); // Export data to code (.h), returns true on success
RLAPI char *LoadFileText(const char *fileName); // Load text data from file (read), returns a '\0' terminated string
RLAPI void UnloadFileText(char *text); // Unload file text data allocated by LoadFileText()
RLAPI bool SaveFileText(const char *fileName, char *text); // Save text data to file (write), string must be '\0' terminated, returns true on success
@ -1050,19 +1057,19 @@ RLAPI const char *GetDirectoryPath(const char *filePath); // Get full pa
RLAPI const char *GetPrevDirectoryPath(const char *dirPath); // Get previous directory path for a given path (uses static string)
RLAPI const char *GetWorkingDirectory(void); // Get current working directory (uses static string)
RLAPI const char *GetApplicationDirectory(void); // Get the directory if the running application (uses static string)
RLAPI char **GetDirectoryFiles(const char *dirPath, int *count); // Get filenames in a directory path (memory should be freed)
RLAPI char **GetDirectoryFiles(const char *dirPath, int *count); // Get filenames in a directory path (memory must be freed)
RLAPI void ClearDirectoryFiles(void); // Clear directory files paths buffers (free memory)
RLAPI bool ChangeDirectory(const char *dir); // Change working directory, return true on success
RLAPI bool IsFileDropped(void); // Check if a file has been dropped into window
RLAPI char **GetDroppedFiles(int *count); // Get dropped files names (memory should be freed)
RLAPI char **GetDroppedFiles(int *count); // Get dropped files names (memory must be freed)
RLAPI void ClearDroppedFiles(void); // Clear dropped files paths buffer (free memory)
RLAPI long GetFileModTime(const char *fileName); // Get file modification time (last write time)
// Compression/Encoding functionality
RLAPI unsigned char *CompressData(const unsigned char *data, int dataLength, int *compDataLength); // Compress data (DEFLATE algorithm)
RLAPI unsigned char *DecompressData(const unsigned char *compData, int compDataLength, int *dataLength); // Decompress data (DEFLATE algorithm)
RLAPI char *EncodeDataBase64(const unsigned char *data, int dataLength, int *outputLength); // Encode data to Base64 string
RLAPI unsigned char *DecodeDataBase64(const unsigned char *data, int *outputLength); // Decode Base64 string data
RLAPI unsigned char *CompressData(const unsigned char *data, int dataSize, int *compDataSize); // Compress data (DEFLATE algorithm), memory must be MemFree()
RLAPI unsigned char *DecompressData(const unsigned char *compData, int compDataSize, int *dataSize); // Decompress data (DEFLATE algorithm), memory must be MemFree()
RLAPI char *EncodeDataBase64(const unsigned char *data, int dataSize, int *outputSize); // Encode data to Base64 string, memory must be MemFree()
RLAPI unsigned char *DecodeDataBase64(const unsigned char *data, int *outputSize); // Decode Base64 string data, memory must be MemFree()
// Persistent storage management
RLAPI bool SaveStorageValue(unsigned int position, int value); // Save integer value to storage file (to defined position), returns true on success
@ -1331,7 +1338,6 @@ RLAPI void UnloadFontData(GlyphInfo *chars, int glyphCount);
RLAPI void UnloadFont(Font font); // Unload font from GPU memory (VRAM)
RLAPI bool ExportFontAsCode(Font font, const char *fileName); // Export font as code file, returns true on success
// Text drawing functions
RLAPI void DrawFPS(int posX, int posY); // Draw current FPS
RLAPI void DrawText(const char *text, int posX, int posY, int fontSize, Color color); // Draw text (using default font)
@ -1465,7 +1471,6 @@ RLAPI bool CheckCollisionBoxes(BoundingBox box1, BoundingBox box2);
RLAPI bool CheckCollisionBoxSphere(BoundingBox box, Vector3 center, float radius); // Check collision between box and sphere
RLAPI RayCollision GetRayCollisionSphere(Ray ray, Vector3 center, float radius); // Get collision info between ray and sphere
RLAPI RayCollision GetRayCollisionBox(Ray ray, BoundingBox box); // Get collision info between ray and box
RLAPI RayCollision GetRayCollisionModel(Ray ray, Model model); // Get collision info between ray and model
RLAPI RayCollision GetRayCollisionMesh(Ray ray, Mesh mesh, Matrix transform); // Get collision info between ray and mesh
RLAPI RayCollision GetRayCollisionTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3); // Get collision info between ray and triangle
RLAPI RayCollision GetRayCollisionQuad(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4); // Get collision info between ray and quad
@ -1473,6 +1478,7 @@ RLAPI RayCollision GetRayCollisionQuad(Ray ray, Vector3 p1, Vector3 p2, Vector3
//------------------------------------------------------------------------------------
// Audio Loading and Playing Functions (Module: audio)
//------------------------------------------------------------------------------------
typedef void (*AudioCallback)(void *bufferData, unsigned int frames);
// Audio device management functions
RLAPI void InitAudioDevice(void); // Initialize audio device and context
@ -1540,6 +1546,10 @@ RLAPI void SetAudioStreamVolume(AudioStream stream, float volume); // Set vol
RLAPI void SetAudioStreamPitch(AudioStream stream, float pitch); // Set pitch for audio stream (1.0 is base level)
RLAPI void SetAudioStreamPan(AudioStream stream, float pan); // Set pan for audio stream (0.5 is centered)
RLAPI void SetAudioStreamBufferSizeDefault(int size); // Default size for new audio streams
RLAPI void SetAudioStreamCallback(AudioStream stream, AudioCallback callback); // Audio thread callback to request new data
RLAPI void AttachAudioStreamProcessor(AudioStream stream, AudioCallback processor);
RLAPI void DetachAudioStreamProcessor(AudioStream stream, AudioCallback processor);
#if defined(__cplusplus)
}

View file

@ -18,7 +18,7 @@
* - Functions are always self-contained, no function use another raymath function inside,
* required code is directly re-implemented inside
* - Functions input parameters are always received by value (2 unavoidable exceptions)
* - Functions use always a "result" anmed variable for return
* - Functions use always a "result" variable for return
* - Functions are always defined inline
* - Angles are always in radians (DEG2RAD/RAD2DEG macros provided for convenience)
*
@ -77,6 +77,10 @@
#define PI 3.14159265358979323846f
#endif
#ifndef EPSILON
#define EPSILON 0.000001f
#endif
#ifndef DEG2RAD
#define DEG2RAD (PI/180.0f)
#endif
@ -194,6 +198,14 @@ RMAPI float Remap(float value, float inputStart, float inputEnd, float outputSta
return result;
}
// Check whether two given floats are almost equal
RMAPI int FloatEquals(float x, float y)
{
int result = (fabsf(x - y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(x), fabsf(y))));
return result;
}
//----------------------------------------------------------------------------------
// Module Functions Definition - Vector2 math
//----------------------------------------------------------------------------------
@ -345,16 +357,16 @@ RMAPI Vector2 Vector2Normalize(Vector2 v)
// Transforms a Vector2 by a given Matrix
RMAPI Vector2 Vector2Transform(Vector2 v, Matrix mat)
{
Vector2 result = { 0 };
Vector2 result = { 0 };
float x = v.x;
float y = v.y;
float z = 0;
float x = v.x;
float y = v.y;
float z = 0;
result.x = mat.m0*x + mat.m4*y + mat.m8*z + mat.m12;
result.y = mat.m1*x + mat.m5*y + mat.m9*z + mat.m13;
result.x = mat.m0*x + mat.m4*y + mat.m8*z + mat.m12;
result.y = mat.m1*x + mat.m5*y + mat.m9*z + mat.m13;
return result;
return result;
}
// Calculate linear interpolation between two vectors
@ -414,6 +426,62 @@ RMAPI Vector2 Vector2MoveTowards(Vector2 v, Vector2 target, float maxDistance)
return result;
}
// Invert the given vector
RMAPI Vector2 Vector2Invert(Vector2 v)
{
Vector2 result = { 1.0f/v.x, 1.0f/v.y };
return result;
}
// Clamp the components of the vector between
// min and max values specified by the given vectors
RMAPI Vector2 Vector2Clamp(Vector2 v, Vector2 min, Vector2 max)
{
Vector2 result = { 0 };
result.x = fminf(max.x, fmaxf(min.x, v.x));
result.y = fminf(max.y, fmaxf(min.y, v.y));
return result;
}
// Clamp the magnitude of the vector between two min and max values
RMAPI Vector2 Vector2ClampValue(Vector2 v, float min, float max)
{
Vector2 result = { 0 };
float length = (v.x*v.x) + (v.y*v.y);
if (length > 0.0f)
{
length = sqrtf(length);
if (length < min)
{
float scale = min/length;
result.x = v.x*scale;
result.y = v.y*scale;
}
else if (length > max)
{
float scale = max/length;
result.x = v.x*scale;
result.y = v.y*scale;
}
}
return result;
}
// Check whether two given vectors are almost equal
RMAPI int Vector2Equals(Vector2 p, Vector2 q)
{
int result = ((fabsf(p.x - q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
((fabsf(p.y - q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y)))));
return result;
}
//----------------------------------------------------------------------------------
// Module Functions Definition - Vector3 math
//----------------------------------------------------------------------------------
@ -498,14 +566,14 @@ RMAPI Vector3 Vector3Perpendicular(Vector3 v)
float min = (float) fabs(v.x);
Vector3 cardinalAxis = {1.0f, 0.0f, 0.0f};
if (fabs(v.y) < min)
if (fabsf(v.y) < min)
{
min = (float) fabs(v.y);
Vector3 tmp = {0.0f, 1.0f, 0.0f};
cardinalAxis = tmp;
}
if (fabs(v.z) < min)
if (fabsf(v.z) < min)
{
Vector3 tmp = {0.0f, 0.0f, 1.0f};
cardinalAxis = tmp;
@ -851,6 +919,92 @@ RMAPI float3 Vector3ToFloatV(Vector3 v)
return buffer;
}
// Invert the given vector
RMAPI Vector3 Vector3Invert(Vector3 v)
{
Vector3 result = { 1.0f/v.x, 1.0f/v.y, 1.0f/v.z };
return result;
}
// Clamp the components of the vector between
// min and max values specified by the given vectors
RMAPI Vector3 Vector3Clamp(Vector3 v, Vector3 min, Vector3 max)
{
Vector3 result = { 0 };
result.x = fminf(max.x, fmaxf(min.x, v.x));
result.y = fminf(max.y, fmaxf(min.y, v.y));
result.z = fminf(max.z, fmaxf(min.z, v.z));
return result;
}
// Clamp the magnitude of the vector between two values
RMAPI Vector3 Vector3ClampValue(Vector3 v, float min, float max)
{
Vector3 result = { 0 };
float length = (v.x*v.x) + (v.y*v.y) + (v.z*v.z);
if (length > 0.0f)
{
length = sqrtf(length);
if (length < min)
{
float scale = min/length;
result.x = v.x*scale;
result.y = v.y*scale;
result.z = v.z*scale;
}
else if (length > max)
{
float scale = max/length;
result.x = v.x*scale;
result.y = v.y*scale;
result.z = v.z*scale;
}
}
return result;
}
// Check whether two given vectors are almost equal
RMAPI int Vector3Equals(Vector3 p, Vector3 q)
{
int result = ((fabsf(p.x - q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
((fabsf(p.y - q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y))))) &&
((fabsf(p.z - q.z)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.z), fabsf(q.z)))));
return result;
}
// Compute the direction of a refracted ray where v specifies the
// normalized direction of the incoming ray, n specifies the
// normalized normal vector of the interface of two optical media,
// and r specifies the ratio of the refractive index of the medium
// from where the ray comes to the refractive index of the medium
// on the other side of the surface
RMAPI Vector3 Vector3Refract(Vector3 v, Vector3 n, float r)
{
Vector3 result = { 0 };
float dot = v.x*n.x + v.y*n.y + v.z*n.z;
float d = 1.0f - r*r*(1.0f - dot*dot);
if (d >= 0.0f)
{
d = sqrtf(d);
v.x = r*v.x - (r*dot + d)*n.x;
v.y = r*v.y - (r*dot + d)*n.y;
v.z = r*v.z - (r*dot + d)*n.z;
result = v;
}
return result;
}
//----------------------------------------------------------------------------------
// Module Functions Definition - Matrix math
//----------------------------------------------------------------------------------
@ -956,45 +1110,6 @@ RMAPI Matrix MatrixInvert(Matrix mat)
return result;
}
// Normalize provided matrix
RMAPI Matrix MatrixNormalize(Matrix mat)
{
Matrix result = { 0 };
// Cache the matrix values (speed optimization)
float a00 = mat.m0, a01 = mat.m1, a02 = mat.m2, a03 = mat.m3;
float a10 = mat.m4, a11 = mat.m5, a12 = mat.m6, a13 = mat.m7;
float a20 = mat.m8, a21 = mat.m9, a22 = mat.m10, a23 = mat.m11;
float a30 = mat.m12, a31 = mat.m13, a32 = mat.m14, a33 = mat.m15;
// MatrixDeterminant(mat)
float det = a30*a21*a12*a03 - a20*a31*a12*a03 - a30*a11*a22*a03 + a10*a31*a22*a03 +
a20*a11*a32*a03 - a10*a21*a32*a03 - a30*a21*a02*a13 + a20*a31*a02*a13 +
a30*a01*a22*a13 - a00*a31*a22*a13 - a20*a01*a32*a13 + a00*a21*a32*a13 +
a30*a11*a02*a23 - a10*a31*a02*a23 - a30*a01*a12*a23 + a00*a31*a12*a23 +
a10*a01*a32*a23 - a00*a11*a32*a23 - a20*a11*a02*a33 + a10*a21*a02*a33 +
a20*a01*a12*a33 - a00*a21*a12*a33 - a10*a01*a22*a33 + a00*a11*a22*a33;
result.m0 = mat.m0/det;
result.m1 = mat.m1/det;
result.m2 = mat.m2/det;
result.m3 = mat.m3/det;
result.m4 = mat.m4/det;
result.m5 = mat.m5/det;
result.m6 = mat.m6/det;
result.m7 = mat.m7/det;
result.m8 = mat.m8/det;
result.m9 = mat.m9/det;
result.m10 = mat.m10/det;
result.m11 = mat.m11/det;
result.m12 = mat.m12/det;
result.m13 = mat.m13/det;
result.m14 = mat.m14/det;
result.m15 = mat.m15/det;
return result;
}
// Get identity matrix
RMAPI Matrix MatrixIdentity(void)
{
@ -1516,7 +1631,7 @@ RMAPI Quaternion QuaternionInvert(Quaternion q)
float lengthSq = q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w;
if (lengthSq != 0.0)
if (lengthSq != 0.0f)
{
float invLength = 1.0f/lengthSq;
@ -1550,12 +1665,10 @@ RMAPI Quaternion QuaternionScale(Quaternion q, float mul)
{
Quaternion result = { 0 };
float qax = q.x, qay = q.y, qaz = q.z, qaw = q.w;
result.x = qax*mul + qaw*mul + qay*mul - qaz*mul;
result.y = qay*mul + qaw*mul + qaz*mul - qax*mul;
result.z = qaz*mul + qaw*mul + qax*mul - qay*mul;
result.w = qaw*mul - qax*mul - qay*mul - qaz*mul;
result.x = q.x*mul;
result.y = q.y*mul;
result.z = q.z*mul;
result.w = q.w*mul;
return result;
}
@ -1619,14 +1732,14 @@ RMAPI Quaternion QuaternionSlerp(Quaternion q1, Quaternion q2, float amount)
cosHalfTheta = -cosHalfTheta;
}
if (fabs(cosHalfTheta) >= 1.0f) result = q1;
if (fabsf(cosHalfTheta) >= 1.0f) result = q1;
else if (cosHalfTheta > 0.95f) result = QuaternionNlerp(q1, q2, amount);
else
{
float halfTheta = acosf(cosHalfTheta);
float sinHalfTheta = sqrtf(1.0f - cosHalfTheta*cosHalfTheta);
if (fabs(sinHalfTheta) < 0.001f)
if (fabsf(sinHalfTheta) < 0.001f)
{
result.x = (q1.x*0.5f + q2.x*0.5f);
result.y = (q1.y*0.5f + q2.y*0.5f);
@ -1792,7 +1905,7 @@ RMAPI Quaternion QuaternionFromAxisAngle(Vector3 axis, float angle)
// Get the rotation angle and axis for a given quaternion
RMAPI void QuaternionToAxisAngle(Quaternion q, Vector3 *outAxis, float *outAngle)
{
if (fabs(q.w) > 1.0f)
if (fabsf(q.w) > 1.0f)
{
// QuaternionNormalize(q);
float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
@ -1885,4 +1998,15 @@ RMAPI Quaternion QuaternionTransform(Quaternion q, Matrix mat)
return result;
}
// Check whether two given quaternions are almost equal
RMAPI int QuaternionEquals(Quaternion p, Quaternion q)
{
int result = ((fabsf(p.x - q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
((fabsf(p.y - q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y))))) &&
((fabsf(p.z - q.z)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.z), fabsf(q.z))))) &&
((fabsf(p.w - q.w)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.w), fabsf(q.w)))));
return result;
}
#endif // RAYMATH_H

View file

@ -184,7 +184,7 @@
#include <stdlib.h> // Required for: srand(), rand(), atexit()
#include <stdio.h> // Required for: sprintf() [Used in OpenURL()]
#include <string.h> // Required for: strrchr(), strcmp(), strlen()
#include <string.h> // Required for: strrchr(), strcmp(), strlen(), memset()
#include <time.h> // Required for: time() [Used in InitTimer()]
#include <math.h> // Required for: tan() [Used in BeginMode3D()], atan2f() [Used in LoadVrStereoConfig()]
@ -227,7 +227,7 @@
unsigned int __stdcall timeEndPeriod(unsigned int uPeriod);
#endif
#endif
#if defined(__linux__) || defined(__FreeBSD__)
#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__)
#include <sys/time.h> // Required for: timespec, nanosleep(), select() - POSIX
//#define GLFW_EXPOSE_NATIVE_X11 // WARNING: Exposing Xlib.h > X.h results in dup symbols for Font type
@ -247,6 +247,7 @@
//#include <android/sensor.h> // Required for: Android sensors functions (accelerometer, gyroscope, light...)
#include <android/window.h> // Required for: AWINDOW_FLAG_FULLSCREEN definition and others
#include <android_native_app_glue.h> // Required for: android_app struct and activity management
#include <jni.h> // Required for: JNIEnv and JavaVM [Used in OpenURL()]
#include <EGL/egl.h> // Native platform windowing system interface
//#include <GLES2/gl2.h> // OpenGL ES 2.0 library (not required in this module, only in rlgl)
@ -411,6 +412,7 @@ typedef struct CoreData {
bool fullscreen; // Check if fullscreen mode is enabled
bool shouldClose; // Check if window set for closing
bool resizedLastFrame; // Check if window has been resized last frame
bool eventWaiting; // Wait for events before ending frame
Point position; // Window position on screen (required on fullscreen toggle)
Size display; // Display width and height (monitor, device-screen, LCD, ...)
@ -778,11 +780,15 @@ void InitWindow(int width, int height, const char *title)
if ((title != NULL) && (title[0] != 0)) CORE.Window.title = title;
// Initialize required global values different than 0
// Initialize global input state
memset(&CORE.Input, 0, sizeof(CORE.Input));
CORE.Input.Keyboard.exitKey = KEY_ESCAPE;
CORE.Input.Mouse.scale = (Vector2){ 1.0f, 1.0f };
CORE.Input.Mouse.cursor = MOUSE_CURSOR_ARROW;
CORE.Input.Gamepad.lastButtonPressed = -1;
#if defined(SUPPORT_EVENTS_WAITING)
CORE.Window.eventWaiting = true;
#endif
#if defined(PLATFORM_ANDROID)
CORE.Window.screen.width = width;
@ -1211,39 +1217,39 @@ bool IsWindowState(unsigned int flag)
void ToggleFullscreen(void)
{
#if defined(PLATFORM_DESKTOP)
// NOTE: glfwSetWindowMonitor() doesn't work properly (bugs)
// TODO: glfwSetWindowMonitor() doesn't work properly (bugs)
if (!CORE.Window.fullscreen)
{
// Store previous window position (in case we exit fullscreen)
glfwGetWindowPos(CORE.Window.handle, &CORE.Window.position.x, &CORE.Window.position.y);
int monitorCount = 0;
GLFWmonitor** monitors = glfwGetMonitors(&monitorCount);
int monitorIndex = GetCurrentMonitor();
GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
// Use current monitor, so we correctly get the display the window is on
GLFWmonitor* monitor = monitorIndex < monitorCount ? monitors[monitorIndex] : NULL;
GLFWmonitor *monitor = (monitorIndex < monitorCount)? monitors[monitorIndex] : NULL;
if (!monitor)
if (monitor == NULL)
{
TRACELOG(LOG_WARNING, "GLFW: Failed to get monitor");
CORE.Window.fullscreen = false; // Toggle fullscreen flag
CORE.Window.fullscreen = false;
CORE.Window.flags &= ~FLAG_FULLSCREEN_MODE;
glfwSetWindowMonitor(CORE.Window.handle, NULL, 0, 0, CORE.Window.screen.width, CORE.Window.screen.height, GLFW_DONT_CARE);
return;
}
else
{
CORE.Window.fullscreen = true;
CORE.Window.flags |= FLAG_FULLSCREEN_MODE;
CORE.Window.fullscreen = true; // Toggle fullscreen flag
CORE.Window.flags |= FLAG_FULLSCREEN_MODE;
glfwSetWindowMonitor(CORE.Window.handle, monitor, 0, 0, CORE.Window.screen.width, CORE.Window.screen.height, GLFW_DONT_CARE);
glfwSetWindowMonitor(CORE.Window.handle, monitor, 0, 0, CORE.Window.screen.width, CORE.Window.screen.height, GLFW_DONT_CARE);
}
}
else
{
CORE.Window.fullscreen = false; // Toggle fullscreen flag
CORE.Window.fullscreen = false;
CORE.Window.flags &= ~FLAG_FULLSCREEN_MODE;
glfwSetWindowMonitor(CORE.Window.handle, NULL, CORE.Window.position.x, CORE.Window.position.y, CORE.Window.screen.width, CORE.Window.screen.height, GLFW_DONT_CARE);
@ -1934,6 +1940,29 @@ const char *GetMonitorName(int monitor)
return "";
}
// Set clipboard text content
void SetClipboardText(const char *text)
{
#if defined(PLATFORM_DESKTOP)
glfwSetClipboardString(CORE.Window.handle, text);
#endif
#if defined(PLATFORM_WEB)
emscripten_run_script(TextFormat("navigator.clipboard.writeText('%s')", text));
#endif
}
// Enable waiting for events on EndDrawing(), no automatic event polling
void EnableEventWaiting(void)
{
CORE.Window.eventWaiting = true;
}
// Disable waiting for events on EndDrawing(), automatic events polling
RLAPI void DisableEventWaiting(void)
{
CORE.Window.eventWaiting = false;
}
// Get clipboard text content
// NOTE: returned string is allocated and freed by GLFW
const char *GetClipboardText(void)
@ -1947,16 +1976,7 @@ const char *GetClipboardText(void)
return NULL;
}
// Set clipboard text content
void SetClipboardText(const char *text)
{
#if defined(PLATFORM_DESKTOP)
glfwSetClipboardString(CORE.Window.handle, text);
#endif
#if defined(PLATFORM_WEB)
emscripten_run_script(TextFormat("navigator.clipboard.writeText('%s')", text));
#endif
}
// Show mouse cursor
void ShowCursor(void)
@ -2066,8 +2086,9 @@ void EndDrawing(void)
{
// Get image data for the current frame (from backbuffer)
// NOTE: This process is quite slow... :(
unsigned char *screenData = rlReadScreenPixels(CORE.Window.screen.width, CORE.Window.screen.height);
msf_gif_frame(&gifState, screenData, 10, 16, CORE.Window.screen.width*4);
Vector2 scale = GetWindowScaleDPI();
unsigned char *screenData = rlReadScreenPixels(CORE.Window.render.width*scale.x, CORE.Window.render.height*scale.y);
msf_gif_frame(&gifState, screenData, 10, 16, CORE.Window.render.width*scale.x*4);
RL_FREE(screenData); // Free image data
}
@ -2236,8 +2257,10 @@ void BeginTextureMode(RenderTexture2D target)
rlEnableFramebuffer(target.id); // Enable render target
// Set viewport to framebuffer size
// Set viewport and RLGL internal framebuffer size
rlViewport(0, 0, target.texture.width, target.texture.height);
rlSetFramebufferWidth(target.texture.width);
rlSetFramebufferHeight(target.texture.height);
rlMatrixMode(RL_PROJECTION); // Switch to projection matrix
rlLoadIdentity(); // Reset current matrix (projection)
@ -2383,8 +2406,8 @@ VrStereoConfig LoadVrStereoConfig(VrDeviceInfo device)
// Fovy is normally computed with: 2*atan2f(device.vScreenSize, 2*device.eyeToScreenDistance)
// ...but with lens distortion it is increased (see Oculus SDK Documentation)
//float fovy = 2.0f*atan2f(device.vScreenSize*0.5f*distortionScale, device.eyeToScreenDistance); // Really need distortionScale?
float fovy = 2.0f*(float)atan2f(device.vScreenSize*0.5f, device.eyeToScreenDistance);
float fovy = 2.0f*atan2f(device.vScreenSize*0.5f*distortionScale, device.eyeToScreenDistance); // Really need distortionScale?
// float fovy = 2.0f*(float)atan2f(device.vScreenSize*0.5f, device.eyeToScreenDistance);
// Compute camera projection matrices
float projOffset = 4.0f*lensShift; // Scaled to projection space coordinates [-1..1]
@ -2710,7 +2733,7 @@ void SetTargetFPS(int fps)
if (fps < 1) CORE.Time.target = 0.0;
else CORE.Time.target = 1.0/(double)fps;
TRACELOG(LOG_INFO, "TIMER: Target time per frame: %02.03f milliseconds", (float)CORE.Time.target*1000);
TRACELOG(LOG_INFO, "TIMER: Target time per frame: %02.03f milliseconds", (float)CORE.Time.target*1000.0f);
}
// Get current FPS
@ -2787,8 +2810,9 @@ void SetConfigFlags(unsigned int flags)
void TakeScreenshot(const char *fileName)
{
#if defined(SUPPORT_MODULE_RTEXTURES)
unsigned char *imgData = rlReadScreenPixels(CORE.Window.render.width, CORE.Window.render.height);
Image image = { imgData, CORE.Window.render.width, CORE.Window.render.height, 1, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 };
Vector2 scale = GetWindowScaleDPI();
unsigned char *imgData = rlReadScreenPixels(CORE.Window.render.width*scale.x, CORE.Window.render.height*scale.y);
Image image = { imgData, CORE.Window.render.width*scale.x, CORE.Window.render.height*scale.y, 1, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 };
char path[2048] = { 0 };
strcpy(path, TextFormat("%s/%s", CORE.Storage.basePath, fileName));
@ -2943,7 +2967,7 @@ const char *GetFileName(const char *filePath)
// Get filename string without extension (uses static string)
const char *GetFileNameWithoutExt(const char *filePath)
{
#define MAX_FILENAMEWITHOUTEXT_LENGTH 128
#define MAX_FILENAMEWITHOUTEXT_LENGTH 256
static char fileName[MAX_FILENAMEWITHOUTEXT_LENGTH] = { 0 };
memset(fileName, 0, MAX_FILENAMEWITHOUTEXT_LENGTH);
@ -3222,8 +3246,8 @@ long GetFileModTime(const char *fileName)
return 0;
}
// Compress data (DEFLATE algorythm)
unsigned char *CompressData(const unsigned char *data, int dataLength, int *compDataLength)
// Compress data (DEFLATE algorithm)
unsigned char *CompressData(const unsigned char *data, int dataSize, int *compDataSize)
{
#define COMPRESSION_QUALITY_DEFLATE 8
@ -3232,40 +3256,40 @@ unsigned char *CompressData(const unsigned char *data, int dataLength, int *comp
#if defined(SUPPORT_COMPRESSION_API)
// Compress data and generate a valid DEFLATE stream
struct sdefl sdefl = { 0 };
int bounds = sdefl_bound(dataLength);
int bounds = sdefl_bound(dataSize);
compData = (unsigned char *)RL_CALLOC(bounds, 1);
*compDataLength = sdeflate(&sdefl, compData, data, dataLength, COMPRESSION_QUALITY_DEFLATE); // Compression level 8, same as stbwi
*compDataSize = sdeflate(&sdefl, compData, data, dataSize, COMPRESSION_QUALITY_DEFLATE); // Compression level 8, same as stbwi
TraceLog(LOG_INFO, "SYSTEM: Compress data: Original size: %i -> Comp. size: %i", dataLength, *compDataLength);
TraceLog(LOG_INFO, "SYSTEM: Compress data: Original size: %i -> Comp. size: %i", dataSize, *compDataSize);
#endif
return compData;
}
// Decompress data (DEFLATE algorythm)
unsigned char *DecompressData(const unsigned char *compData, int compDataLength, int *dataLength)
// Decompress data (DEFLATE algorithm)
unsigned char *DecompressData(const unsigned char *compData, int compDataSize, int *dataSize)
{
unsigned char *data = NULL;
#if defined(SUPPORT_COMPRESSION_API)
// Decompress data from a valid DEFLATE stream
data = RL_CALLOC(MAX_DECOMPRESSION_SIZE*1024*1024, 1);
int length = sinflate(data, MAX_DECOMPRESSION_SIZE, compData, compDataLength);
int length = sinflate(data, MAX_DECOMPRESSION_SIZE, compData, compDataSize);
unsigned char *temp = RL_REALLOC(data, length);
if (temp != NULL) data = temp;
else TRACELOG(LOG_WARNING, "SYSTEM: Failed to re-allocate required decompression memory");
*dataLength = length;
*dataSize = length;
TraceLog(LOG_INFO, "SYSTEM: Decompress data: Comp. size: %i -> Original size: %i", compDataLength, *dataLength);
TraceLog(LOG_INFO, "SYSTEM: Decompress data: Comp. size: %i -> Original size: %i", compDataSize, *dataSize);
#endif
return data;
}
// Encode data to Base64 string
char *EncodeDataBase64(const unsigned char *data, int dataLength, int *outputLength)
char *EncodeDataBase64(const unsigned char *data, int dataSize, int *outputSize)
{
static const unsigned char base64encodeTable[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
@ -3275,17 +3299,17 @@ char *EncodeDataBase64(const unsigned char *data, int dataLength, int *outputLen
static const int modTable[] = { 0, 2, 1 };
*outputLength = 4*((dataLength + 2)/3);
*outputSize = 4*((dataSize + 2)/3);
char *encodedData = RL_MALLOC(*outputLength);
char *encodedData = RL_MALLOC(*outputSize);
if (encodedData == NULL) return NULL;
for (int i = 0, j = 0; i < dataLength;)
for (int i = 0, j = 0; i < dataSize;)
{
unsigned int octetA = (i < dataLength)? (unsigned char)data[i++] : 0;
unsigned int octetB = (i < dataLength)? (unsigned char)data[i++] : 0;
unsigned int octetC = (i < dataLength)? (unsigned char)data[i++] : 0;
unsigned int octetA = (i < dataSize)? (unsigned char)data[i++] : 0;
unsigned int octetB = (i < dataSize)? (unsigned char)data[i++] : 0;
unsigned int octetC = (i < dataSize)? (unsigned char)data[i++] : 0;
unsigned int triple = (octetA << 0x10) + (octetB << 0x08) + octetC;
@ -3295,13 +3319,13 @@ char *EncodeDataBase64(const unsigned char *data, int dataLength, int *outputLen
encodedData[j++] = base64encodeTable[(triple >> 0*6) & 0x3F];
}
for (int i = 0; i < modTable[dataLength%3]; i++) encodedData[*outputLength - 1 - i] = '='; // Padding character
for (int i = 0; i < modTable[dataSize%3]; i++) encodedData[*outputSize - 1 - i] = '='; // Padding character
return encodedData;
}
// Decode Base64 string data
unsigned char *DecodeDataBase64(const unsigned char *data, int *outputLength)
unsigned char *DecodeDataBase64(const unsigned char *data, int *outputSize)
{
static const unsigned char base64decodeTable[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@ -3311,21 +3335,21 @@ unsigned char *DecodeDataBase64(const unsigned char *data, int *outputLength)
};
// Get output size of Base64 input data
int outLength = 0;
int outSize = 0;
for (int i = 0; data[4*i] != 0; i++)
{
if (data[4*i + 3] == '=')
{
if (data[4*i + 2] == '=') outLength += 1;
else outLength += 2;
if (data[4*i + 2] == '=') outSize += 1;
else outSize += 2;
}
else outLength += 3;
else outSize += 3;
}
// Allocate memory to store decoded Base64 data
unsigned char *decodedData = (unsigned char *)RL_MALLOC(outLength);
unsigned char *decodedData = (unsigned char *)RL_MALLOC(outSize);
for (int i = 0; i < outLength/3; i++)
for (int i = 0; i < outSize/3; i++)
{
unsigned char a = base64decodeTable[(int)data[4*i]];
unsigned char b = base64decodeTable[(int)data[4*i + 1]];
@ -3337,24 +3361,24 @@ unsigned char *DecodeDataBase64(const unsigned char *data, int *outputLength)
decodedData[3*i + 2] = (c << 6) | d;
}
if (outLength%3 == 1)
if (outSize%3 == 1)
{
int n = outLength/3;
int n = outSize/3;
unsigned char a = base64decodeTable[(int)data[4*n]];
unsigned char b = base64decodeTable[(int)data[4*n + 1]];
decodedData[outLength - 1] = (a << 2) | (b >> 4);
decodedData[outSize - 1] = (a << 2) | (b >> 4);
}
else if (outLength%3 == 2)
else if (outSize%3 == 2)
{
int n = outLength/3;
int n = outSize/3;
unsigned char a = base64decodeTable[(int)data[4*n]];
unsigned char b = base64decodeTable[(int)data[4*n + 1]];
unsigned char c = base64decodeTable[(int)data[4*n + 2]];
decodedData[outLength - 2] = (a << 2) | (b >> 4);
decodedData[outLength - 1] = (b << 4) | (c >> 2);
decodedData[outSize - 2] = (a << 2) | (b >> 4);
decodedData[outSize - 1] = (b << 4) | (c >> 2);
}
*outputLength = outLength;
*outputSize = outSize;
return decodedData;
}
@ -3480,9 +3504,9 @@ void OpenURL(const char *url)
#if defined(PLATFORM_DESKTOP)
char *cmd = (char *)RL_CALLOC(strlen(url) + 10, sizeof(char));
#if defined(_WIN32)
sprintf(cmd, "explorer %s", url);
sprintf(cmd, "explorer \"%s\"", url);
#endif
#if defined(__linux__) || defined(__FreeBSD__)
#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__)
sprintf(cmd, "xdg-open '%s'", url); // Alternatives: firefox, x-www-browser
#endif
#if defined(__APPLE__)
@ -3494,6 +3518,29 @@ void OpenURL(const char *url)
#endif
#if defined(PLATFORM_WEB)
emscripten_run_script(TextFormat("window.open('%s', '_blank')", url));
#endif
#if defined(PLATFORM_ANDROID)
JNIEnv *env = NULL;
JavaVM *vm = CORE.Android.app->activity->vm;
(*vm)->AttachCurrentThread(vm, &env, NULL);
jstring urlString = (*env)->NewStringUTF(env, url);
jclass uriClass = (*env)->FindClass(env, "android/net/Uri");
jmethodID uriParse = (*env)->GetStaticMethodID(env, uriClass, "parse", "(Ljava/lang/String;)Landroid/net/Uri;");
jobject uri = (*env)->CallStaticObjectMethod(env, uriClass, uriParse, urlString);
jclass intentClass = (*env)->FindClass(env, "android/content/Intent");
jfieldID actionViewId = (*env)->GetStaticFieldID(env, intentClass, "ACTION_VIEW", "Ljava/lang/String;");
jobject actionView = (*env)->GetStaticObjectField(env, intentClass, actionViewId);
jmethodID newIntent = (*env)->GetMethodID(env, intentClass, "<init>", "(Ljava/lang/String;Landroid/net/Uri;)V");
jobject intent = (*env)->AllocObject(env, intentClass);
(*env)->CallVoidMethod(env, intent, newIntent, actionView, uri);
jclass activityClass = (*env)->FindClass(env, "android/app/Activity");
jmethodID startActivity = (*env)->GetMethodID(env, activityClass, "startActivity", "(Landroid/content/Intent;)V");
(*env)->CallVoidMethod(env, CORE.Android.app->activity->clazz, startActivity, intent);
(*vm)->DetachCurrentThread(vm);
#endif
}
}
@ -4848,7 +4895,7 @@ void WaitTime(float ms)
#if defined(_WIN32)
Sleep((unsigned int)ms);
#endif
#if defined(__linux__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__)
#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMSCRIPTEN__)
struct timespec req = { 0 };
time_t sec = (int)(ms/1000.0f);
ms -= (sec*1000);
@ -4867,7 +4914,7 @@ void WaitTime(float ms)
double currentTime = 0.0;
// Partial busy wait loop (only a fraction of the total wait time)
while ((currentTime - previousTime) < busyWait/1000.0f) currentTime = GetTime();
while ((currentTime - previousTime) < (busyWait/1000.0f)) currentTime = GetTime();
#endif
#endif
}
@ -4883,53 +4930,31 @@ void SwapScreenBuffer(void)
eglSwapBuffers(CORE.Window.device, CORE.Window.surface);
#if defined(PLATFORM_DRM)
if (!CORE.Window.gbmSurface || (-1 == CORE.Window.fd) || !CORE.Window.connector || !CORE.Window.crtc)
{
TRACELOG(LOG_ERROR, "DISPLAY: DRM initialization failed to swap");
abort();
}
if (!CORE.Window.gbmSurface || (-1 == CORE.Window.fd) || !CORE.Window.connector || !CORE.Window.crtc) TRACELOG(LOG_ERROR, "DISPLAY: DRM initialization failed to swap");
struct gbm_bo *bo = gbm_surface_lock_front_buffer(CORE.Window.gbmSurface);
if (!bo)
{
TRACELOG(LOG_ERROR, "DISPLAY: Failed GBM to lock front buffer");
abort();
}
if (!bo) TRACELOG(LOG_ERROR, "DISPLAY: Failed GBM to lock front buffer");
uint32_t fb = 0;
int result = drmModeAddFB(CORE.Window.fd, CORE.Window.connector->modes[CORE.Window.modeIndex].hdisplay,
CORE.Window.connector->modes[CORE.Window.modeIndex].vdisplay, 24, 32, gbm_bo_get_stride(bo), gbm_bo_get_handle(bo).u32, &fb);
if (0 != result)
{
TRACELOG(LOG_ERROR, "DISPLAY: drmModeAddFB() failed with result: %d", result);
abort();
}
int result = drmModeAddFB(CORE.Window.fd, CORE.Window.connector->modes[CORE.Window.modeIndex].hdisplay, CORE.Window.connector->modes[CORE.Window.modeIndex].vdisplay, 24, 32, gbm_bo_get_stride(bo), gbm_bo_get_handle(bo).u32, &fb);
if (result != 0) TRACELOG(LOG_ERROR, "DISPLAY: drmModeAddFB() failed with result: %d", result);
result = drmModeSetCrtc(CORE.Window.fd, CORE.Window.crtc->crtc_id, fb, 0, 0,
&CORE.Window.connector->connector_id, 1, &CORE.Window.connector->modes[CORE.Window.modeIndex]);
if (0 != result)
{
TRACELOG(LOG_ERROR, "DISPLAY: drmModeSetCrtc() failed with result: %d", result);
abort();
}
result = drmModeSetCrtc(CORE.Window.fd, CORE.Window.crtc->crtc_id, fb, 0, 0, &CORE.Window.connector->connector_id, 1, &CORE.Window.connector->modes[CORE.Window.modeIndex]);
if (result != 0) TRACELOG(LOG_ERROR, "DISPLAY: drmModeSetCrtc() failed with result: %d", result);
if (CORE.Window.prevFB)
{
result = drmModeRmFB(CORE.Window.fd, CORE.Window.prevFB);
if (0 != result)
{
TRACELOG(LOG_ERROR, "DISPLAY: drmModeRmFB() failed with result: %d", result);
abort();
}
if (result != 0) TRACELOG(LOG_ERROR, "DISPLAY: drmModeRmFB() failed with result: %d", result);
}
CORE.Window.prevFB = fb;
if (CORE.Window.prevBO)
{
gbm_surface_release_buffer(CORE.Window.gbmSurface, CORE.Window.prevBO);
}
if (CORE.Window.prevBO) gbm_surface_release_buffer(CORE.Window.gbmSurface, CORE.Window.prevBO);
CORE.Window.prevBO = bo;
#endif // PLATFORM_DRM
#endif // PLATFORM_ANDROID || PLATFORM_RPI || PLATFORM_DRM || PLATFORM_NX
}
@ -5077,8 +5102,8 @@ void PollInputEvents(void)
}
// Register buttons for 2nd triggers (because GLFW doesn't count these as buttons but rather axis)
CORE.Input.Gamepad.currentButtonState[i][GAMEPAD_BUTTON_LEFT_TRIGGER_2] = (char)(CORE.Input.Gamepad.axisState[i][GAMEPAD_AXIS_LEFT_TRIGGER] > 0.1);
CORE.Input.Gamepad.currentButtonState[i][GAMEPAD_BUTTON_RIGHT_TRIGGER_2] = (char)(CORE.Input.Gamepad.axisState[i][GAMEPAD_AXIS_RIGHT_TRIGGER] > 0.1);
CORE.Input.Gamepad.currentButtonState[i][GAMEPAD_BUTTON_LEFT_TRIGGER_2] = (char)(CORE.Input.Gamepad.axisState[i][GAMEPAD_AXIS_LEFT_TRIGGER] > 0.1f);
CORE.Input.Gamepad.currentButtonState[i][GAMEPAD_BUTTON_RIGHT_TRIGGER_2] = (char)(CORE.Input.Gamepad.axisState[i][GAMEPAD_AXIS_RIGHT_TRIGGER] > 0.1f);
CORE.Input.Gamepad.axisCount = GLFW_GAMEPAD_AXIS_LAST + 1;
}
@ -5086,11 +5111,8 @@ void PollInputEvents(void)
CORE.Window.resizedLastFrame = false;
#if defined(SUPPORT_EVENTS_WAITING)
glfwWaitEvents();
#else
glfwPollEvents(); // Register keyboard/mouse events (callbacks)... and window events!
#endif
if (CORE.Window.eventWaiting) glfwWaitEvents(); // Wait for in input events before continue (drawing is paused)
else glfwPollEvents(); // Poll input events: keyboard/mouse/window events (callbacks)
#endif // PLATFORM_DESKTOP
#if defined(PLATFORM_WEB)
@ -5514,7 +5536,8 @@ static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, i
gifRecording = true;
gifFrameCounter = 0;
msf_gif_begin(&gifState, CORE.Window.screen.width, CORE.Window.screen.height);
Vector2 scale = GetWindowScaleDPI();
msf_gif_begin(&gifState, CORE.Window.render.width*scale.x, CORE.Window.render.height*scale.y);
screenshotCounter++;
TRACELOG(LOG_INFO, "SYSTEM: Start animated GIF recording: %s", TextFormat("screenrec%03i.gif", screenshotCounter));
@ -5632,10 +5655,10 @@ static void MouseCursorPosCallback(GLFWwindow *window, double x, double y)
#endif
}
// GLFW3 Srolling Callback, runs on mouse wheel
// GLFW3 Scrolling Callback, runs on mouse wheel
static void MouseScrollCallback(GLFWwindow *window, double xoffset, double yoffset)
{
if (xoffset != 0.0) CORE.Input.Mouse.currentWheelMove = (float)xoffset;
if (fabs(xoffset) > fabs(yoffset)) CORE.Input.Mouse.currentWheelMove = (float)xoffset;
else CORE.Input.Mouse.currentWheelMove = (float)yoffset;
}
@ -6227,7 +6250,8 @@ static void InitEvdevInput(void)
{
while ((entity = readdir(directory)) != NULL)
{
if (strncmp("event", entity->d_name, strlen("event")) == 0) // Search for devices named "event*"
if ((strncmp("event", entity->d_name, strlen("event")) == 0) || // Search for devices named "event*"
(strncmp("mouse", entity->d_name, strlen("mouse")) == 0)) // Search for devices named "mouse*"
{
sprintf(path, "%s%s", DEFAULT_EVDEV_PATH, entity->d_name);
ConfigureEvdevDevice(path); // Configure the device if appropriate
@ -6387,7 +6411,7 @@ static void ConfigureEvdevDevice(char *device)
// Decide what to do with the device
//-------------------------------------------------------------------------------------------------------
if (worker->isKeyboard && CORE.Input.Keyboard.fd == -1)
if (worker->isKeyboard && (CORE.Input.Keyboard.fd == -1))
{
// Use the first keyboard encountered. This assumes that a device that says it's a keyboard is just a
// keyboard. The keyboard is polled synchronously, whereas other input devices are polled in separate
@ -6699,7 +6723,7 @@ static void InitGamepad(void)
{
sprintf(gamepadDev, "%s%i", DEFAULT_GAMEPAD_DEV, i);
if ((CORE.Input.Gamepad.streamId[i] = open(gamepadDev, O_RDONLY|O_NONBLOCK)) < 0)
if ((CORE.Input.Gamepad.streamId[i] = open(gamepadDev, O_RDONLY | O_NONBLOCK)) < 0)
{
// NOTE: Only show message for first gamepad
if (i == 0) TRACELOG(LOG_WARNING, "RPI: Failed to open Gamepad device, no gamepad available");
@ -6838,7 +6862,7 @@ static int FindNearestConnectorMode(const drmModeConnector *connector, uint widt
TRACELOG(LOG_TRACE, "DISPLAY: DRM mode: %d %ux%u@%u %s", i, mode->hdisplay, mode->vdisplay, mode->vrefresh,
(mode->flags & DRM_MODE_FLAG_INTERLACE) ? "interlaced" : "progressive");
if ((mode->hdisplay < width) || (mode->vdisplay < height) || (mode->vrefresh < fps))
if ((mode->hdisplay < width) || (mode->vdisplay < height))
{
TRACELOG(LOG_TRACE, "DISPLAY: DRM mode is too small");
continue;
@ -6850,23 +6874,22 @@ static int FindNearestConnectorMode(const drmModeConnector *connector, uint widt
continue;
}
if ((mode->hdisplay >= width) && (mode->vdisplay >= height) && (mode->vrefresh >= fps))
if (nearestIndex < 0)
{
const int widthDiff = mode->hdisplay - width;
const int heightDiff = mode->vdisplay - height;
const int fpsDiff = mode->vrefresh - fps;
nearestIndex = i;
continue;
}
if (nearestIndex < 0)
{
nearestIndex = i;
continue;
}
const int widthDiff = abs(mode->hdisplay - width);
const int heightDiff = abs(mode->vdisplay - height);
const int fpsDiff = abs(mode->vrefresh - fps);
const int nearestWidthDiff = CORE.Window.connector->modes[nearestIndex].hdisplay - width;
const int nearestHeightDiff = CORE.Window.connector->modes[nearestIndex].vdisplay - height;
const int nearestFpsDiff = CORE.Window.connector->modes[nearestIndex].vrefresh - fps;
const int nearestWidthDiff = abs(CORE.Window.connector->modes[nearestIndex].hdisplay - width);
const int nearestHeightDiff = abs(CORE.Window.connector->modes[nearestIndex].vdisplay - height);
const int nearestFpsDiff = abs(CORE.Window.connector->modes[nearestIndex].vrefresh - fps);
if ((widthDiff < nearestWidthDiff) || (heightDiff < nearestHeightDiff) || (fpsDiff < nearestFpsDiff)) nearestIndex = i;
if ((widthDiff < nearestWidthDiff) || (heightDiff < nearestHeightDiff) || (fpsDiff < nearestFpsDiff)) {
nearestIndex = i;
}
}
@ -6876,6 +6899,7 @@ static int FindNearestConnectorMode(const drmModeConnector *connector, uint widt
#if defined(SUPPORT_EVENTS_AUTOMATION)
// NOTE: Loading happens over AutomationEvent *events
// TODO: This system should probably be redesigned
static void LoadAutomationEvents(const char *fileName)
{
//unsigned char fileId[4] = { 0 };

View file

@ -62,10 +62,10 @@
// NOTE: Below types are required for GESTURES_STANDALONE usage
//----------------------------------------------------------------------------------
// Boolean type
#if defined(__STDC__) && __STDC_VERSION__ >= 199901L
#if (defined(__STDC__) && __STDC_VERSION__ >= 199901L) || (defined(_MSC_VER) && _MSC_VER >= 1800)
#include <stdbool.h>
#elif !defined(__cplusplus) && !defined(bool) && !defined(RL_BOOL_TYPE)
typedef enum bool { false, true } bool;
typedef enum bool { false = 0, true = !false } bool;
#endif
#if !defined(RL_VECTOR2_TYPE)

View file

@ -358,11 +358,11 @@ typedef struct rlRenderBatch {
float currentDepth; // Current depth value for next draw
} rlRenderBatch;
#if defined(__STDC__) && __STDC_VERSION__ >= 199901L
#if (defined(__STDC__) && __STDC_VERSION__ >= 199901L) || (defined(_MSC_VER) && _MSC_VER >= 1800)
#include <stdbool.h>
#elif !defined(__cplusplus) && !defined(bool) && !defined(RL_BOOL_TYPE)
// Boolean type
typedef enum bool { false, true } bool;
typedef enum bool { false = 0, true = !false } bool;
#endif
#if !defined(RL_MATRIX_TYPE)
@ -598,7 +598,9 @@ RLAPI void rlglInit(int width, int height); // Initialize rlgl (buffer
RLAPI void rlglClose(void); // De-inititialize rlgl (buffers, shaders, textures)
RLAPI void rlLoadExtensions(void *loader); // Load OpenGL extensions (loader function required)
RLAPI int rlGetVersion(void); // Get current OpenGL version
RLAPI void rlSetFramebufferWidth(int width); // Set current framebuffer width
RLAPI int rlGetFramebufferWidth(void); // Get default framebuffer width
RLAPI void rlSetFramebufferHeight(int height); // Set current framebuffer height
RLAPI int rlGetFramebufferHeight(void); // Get default framebuffer height
RLAPI unsigned int rlGetTextureIdDefault(void); // Get default texture id
@ -927,8 +929,8 @@ typedef struct rlglData {
int glBlendDstFactor; // Blending destination factor
int glBlendEquation; // Blending equation
int framebufferWidth; // Default framebuffer width
int framebufferHeight; // Default framebuffer height
int framebufferWidth; // Current framebuffer width
int framebufferHeight; // Current framebuffer height
} State; // Renderer state
struct {
@ -1230,6 +1232,7 @@ void rlOrtho(double left, double right, double bottom, double top, double znear,
#endif
// Set the viewport area (transformation from normalized device coordinates to window coordinates)
// NOTE: We store current viewport dimensions
void rlViewport(int x, int y, int width, int height)
{
glViewport(x, y, width, height);
@ -1537,7 +1540,7 @@ void rlTextureParameters(unsigned int id, int param, int value)
if (value <= RLGL.ExtSupported.maxAnisotropyLevel) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)value);
else if (RLGL.ExtSupported.maxAnisotropyLevel > 0.0f)
{
TRACELOG(RL_LOG_WARNING, "GL: Maximum anisotropic filter level supported is %iX", id, RLGL.ExtSupported.maxAnisotropyLevel);
TRACELOG(RL_LOG_WARNING, "GL: Maximum anisotropic filter level supported is %iX", id, (int)RLGL.ExtSupported.maxAnisotropyLevel);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)value);
}
else TRACELOG(RL_LOG_WARNING, "GL: Anisotropic filtering not supported");
@ -2219,6 +2222,22 @@ int rlGetVersion(void)
return glVersion;
}
// Set current framebuffer width
void rlSetFramebufferWidth(int width)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
RLGL.State.framebufferWidth = width;
#endif
}
// Set current framebuffer height
void rlSetFramebufferHeight(int height)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
RLGL.State.framebufferHeight = height;
#endif
}
// Get default framebuffer width
int rlGetFramebufferWidth(void)
{
@ -2601,6 +2620,9 @@ void rlDrawRenderBatch(rlRenderBatch *batch)
glUseProgram(0); // Unbind shader program
}
// Restore viewport to default measures
if (eyeCount == 2) rlViewport(0, 0, RLGL.State.framebufferWidth, RLGL.State.framebufferHeight);
//------------------------------------------------------------------------------------------------------------
// Reset batch buffers
@ -3088,7 +3110,7 @@ void rlGenTextureMipmaps(unsigned int id, int width, int height, int format, int
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
if ((texIsPOT) || (RLGL.ExtSupported.texNPOT))
{
//glHint(GL_GENERATE_MIPMAP_HINT, GL_DONT_CARE); // Hint for mipmaps generation algorythm: GL_FASTEST, GL_NICEST, GL_DONT_CARE
//glHint(GL_GENERATE_MIPMAP_HINT, GL_DONT_CARE); // Hint for mipmaps generation algorithm: GL_FASTEST, GL_NICEST, GL_DONT_CARE
glGenerateMipmap(GL_TEXTURE_2D); // Generate mipmaps automatically
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

View file

@ -3284,10 +3284,10 @@ void DrawModelEx(Model model, Vector3 position, Vector3 rotationAxis, float rota
Color color = model.materials[model.meshMaterial[i]].maps[MATERIAL_MAP_DIFFUSE].color;
Color colorTint = WHITE;
colorTint.r = (unsigned char)((((float)color.r/255.0)*((float)tint.r/255.0))*255.0f);
colorTint.g = (unsigned char)((((float)color.g/255.0)*((float)tint.g/255.0))*255.0f);
colorTint.b = (unsigned char)((((float)color.b/255.0)*((float)tint.b/255.0))*255.0f);
colorTint.a = (unsigned char)((((float)color.a/255.0)*((float)tint.a/255.0))*255.0f);
colorTint.r = (unsigned char)((((float)color.r/255.0f)*((float)tint.r/255.0f))*255.0f);
colorTint.g = (unsigned char)((((float)color.g/255.0f)*((float)tint.g/255.0f))*255.0f);
colorTint.b = (unsigned char)((((float)color.b/255.0f)*((float)tint.b/255.0f))*255.0f);
colorTint.a = (unsigned char)((((float)color.a/255.0f)*((float)tint.a/255.0f))*255.0f);
model.materials[model.meshMaterial[i]].maps[MATERIAL_MAP_DIFFUSE].color = colorTint;
DrawMesh(model.meshes[i], model.materials[model.meshMaterial[i]], model.transform);
@ -3640,31 +3640,12 @@ RayCollision GetRayCollisionMesh(Ray ray, Mesh mesh, Matrix transform)
return collision;
}
// Get collision info between ray and model
RayCollision GetRayCollisionModel(Ray ray, Model model)
{
RayCollision collision = { 0 };
for (int m = 0; m < model.meshCount; m++)
{
RayCollision meshHitInfo = GetRayCollisionMesh(ray, model.meshes[m], model.transform);
if (meshHitInfo.hit)
{
// Save the closest hit mesh
if ((!collision.hit) || (collision.distance > meshHitInfo.distance)) collision = meshHitInfo;
}
}
return collision;
}
// Get collision info between ray and triangle
// NOTE: The points are expected to be in counter-clockwise winding
// NOTE: Based on https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
RayCollision GetRayCollisionTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3)
{
#define EPSILON 0.000001 // A small number
#define EPSILON 0.000001f // A small number
RayCollision collision = { 0 };
Vector3 edge1 = { 0 };
@ -4662,7 +4643,6 @@ static Model LoadGLTF(const char *fileName)
// Load our model data: meshes and materials
model.meshCount = primitivesCount;
model.meshes = RL_CALLOC(model.meshCount, sizeof(Mesh));
for (int i = 0; i < model.meshCount; i++) model.meshes[i].vboId = (unsigned int*)RL_CALLOC(MAX_MESH_VERTEX_BUFFERS, sizeof(unsigned int));
// NOTE: We keep an extra slot for default material, in case some mesh requires it
model.materialCount = (int)data->materials_count + 1;

View file

@ -25,7 +25,7 @@
*
* DEPENDENCIES:
* stb_truetype - Load TTF file and rasterize characters data
* stb_rect_pack - Rectangles packing algorythms, required for font atlas generation
* stb_rect_pack - Rectangles packing algorithms, required for font atlas generation
*
*
* LICENSE: zlib/libpng
@ -229,7 +229,7 @@ extern void LoadFontDefault(void)
//------------------------------------------------------------------------------
// Allocate space for our characters info data
// NOTE: This memory should be freed at end! --> CloseWindow()
// NOTE: This memory must be freed at end! --> Done by CloseWindow()
defaultFont.glyphs = (GlyphInfo *)RL_MALLOC(defaultFont.glyphCount*sizeof(GlyphInfo));
defaultFont.recs = (Rectangle *)RL_MALLOC(defaultFont.glyphCount*sizeof(Rectangle));
@ -695,7 +695,7 @@ Image GenImageFontAtlas(const GlyphInfo *chars, Rectangle **charRecs, int glyphC
// DEBUG: We can see padding in the generated image setting a gray background...
//for (int i = 0; i < atlas.width*atlas.height; i++) ((unsigned char *)atlas.data)[i] = 100;
if (packMethod == 0) // Use basic packing algorythm
if (packMethod == 0) // Use basic packing algorithm
{
int offsetX = padding;
int offsetY = padding;
@ -746,7 +746,7 @@ Image GenImageFontAtlas(const GlyphInfo *chars, Rectangle **charRecs, int glyphC
}
}
}
else if (packMethod == 1) // Use Skyline rect packing algorythm (stb_pack_rect)
else if (packMethod == 1) // Use Skyline rect packing algorithm (stb_pack_rect)
{
stbrp_context *context = (stbrp_context *)RL_MALLOC(sizeof(*context));
stbrp_node *nodes = (stbrp_node *)RL_MALLOC(glyphCount*sizeof(*nodes));
@ -992,11 +992,11 @@ bool ExportFontAsCode(Font font, const char *fileName)
// NOTE: Uses default font
void DrawFPS(int posX, int posY)
{
Color color = LIME; // good fps
Color color = LIME; // Good FPS
int fps = GetFPS();
if (fps < 30 && fps >= 15) color = ORANGE; // warning FPS
else if (fps < 15) color = RED; // bad FPS
if ((fps < 30) && (fps >= 15)) color = ORANGE; // Warning FPS
else if (fps < 15) color = RED; // Low FPS
DrawText(TextFormat("%2i FPS", GetFPS()), posX, posY, 20, color);
}
@ -1383,7 +1383,7 @@ const char *TextSubtext(const char *text, int position, int length)
// Replace text string
// REQUIRES: strlen(), strstr(), strncpy(), strcpy()
// WARNING: Returned buffer must be freed by the user (if return != NULL)
// WARNING: Allocated memory must be manually freed
char *TextReplace(char *text, const char *replace, const char *by)
{
// Sanity checks and initialization
@ -1432,7 +1432,7 @@ char *TextReplace(char *text, const char *replace, const char *by)
}
// Insert text in a specific position, moves all text forward
// WARNING: Allocated memory should be manually freed
// WARNING: Allocated memory must be manually freed
char *TextInsert(const char *text, const char *insert, int position)
{
int textLen = TextLength(text);
@ -1616,7 +1616,7 @@ const char *TextToPascal(const char *text)
// Encode text codepoint into UTF-8 text
// REQUIRES: memcpy()
// WARNING: Allocated memory should be manually freed
// WARNING: Allocated memory must be manually freed
char *TextCodepointsToUTF8(const int *codepoints, int length)
{
// We allocate enough memory fo fit all possible codepoints

View file

@ -37,7 +37,7 @@
* DEPENDENCIES:
* stb_image - Multiple image formats loading (JPEG, PNG, BMP, TGA, PSD, GIF, PIC)
* NOTE: stb_image has been slightly modified to support Android platform.
* stb_image_resize - Multiple image resize algorythms
* stb_image_resize - Multiple image resize algorithms
*
*
* LICENSE: zlib/libpng
@ -306,10 +306,9 @@ Image LoadImageFromMemory(const char *fileType, const unsigned char *fileData, i
{
Image image = { 0 };
#if defined(SUPPORT_FILEFORMAT_PNG)
if ((strcmp(fileType, ".png") == 0)
#else
if ((false)
#if defined(SUPPORT_FILEFORMAT_PNG)
|| (strcmp(fileType, ".png") == 0)
#endif
#if defined(SUPPORT_FILEFORMAT_BMP)
|| (strcmp(fileType, ".bmp") == 0)
@ -1890,10 +1889,10 @@ void ImageColorTint(Image *image, Color color)
unsigned char b = (unsigned char)(((float)pixels[index].b/255*cB)*255.0f);
unsigned char a = (unsigned char)(((float)pixels[index].a/255*cA)*255.0f);
pixels[y*image->width + x].r = r;
pixels[y*image->width + x].g = g;
pixels[y*image->width + x].b = b;
pixels[y*image->width + x].a = a;
pixels[index].r = r;
pixels[index].g = g;
pixels[index].b = b;
pixels[index].a = a;
}
}
@ -1959,25 +1958,25 @@ void ImageColorContrast(Image *image, float contrast)
for (int x = 0; x < image->width; x++)
{
float pR = (float)pixels[y*image->width + x].r/255.0f;
pR -= 0.5;
pR -= 0.5f;
pR *= contrast;
pR += 0.5;
pR += 0.5f;
pR *= 255;
if (pR < 0) pR = 0;
if (pR > 255) pR = 255;
float pG = (float)pixels[y*image->width + x].g/255.0f;
pG -= 0.5;
pG -= 0.5f;
pG *= contrast;
pG += 0.5;
pG += 0.5f;
pG *= 255;
if (pG < 0) pG = 0;
if (pG > 255) pG = 255;
float pB = (float)pixels[y*image->width + x].b/255.0f;
pB -= 0.5;
pB -= 0.5f;
pB *= contrast;
pB += 0.5;
pB += 0.5f;
pB *= 255;
if (pB < 0) pB = 0;
if (pB > 255) pB = 255;
@ -2412,7 +2411,20 @@ Color GetImageColor(Image image, int x, int y)
// Clear image background with given color
void ImageClearBackground(Image *dst, Color color)
{
for (int i = 0; i < dst->width*dst->height; ++i) ImageDrawPixel(dst, i%dst->width, i/dst->width, color);
// Security check to avoid program crash
if ((dst->data == NULL) || (dst->width == 0) || (dst->height == 0)) return;
// Fill in first pixel based on image format
ImageDrawPixel(dst, 0, 0, color);
unsigned char *pSrcPixel = (unsigned char *)dst->data;
int bytesPerPixel = GetPixelDataSize(1, 1, dst->format);
// Repeat the first pixel data throughout the image
for (int i = 1; i < dst->width * dst->height; i++)
{
memcpy(pSrcPixel + i * bytesPerPixel, pSrcPixel, bytesPerPixel);
}
}
// Draw pixel within an image
@ -2686,13 +2698,21 @@ void ImageDrawRectangleRec(Image *dst, Rectangle rec, Color color)
int ey = sy + (int)rec.height;
int sx = (int)rec.x;
int ex = sx + (int)rec.width;
int bytesPerPixel = GetPixelDataSize(1, 1, dst->format);
for (int y = sy; y < ey; y++)
{
for (int x = sx; x < ex; x++)
// Fill in the first pixel of the row based on image format
ImageDrawPixel(dst, sx, y, color);
int bytesOffset = ((y * dst->width) + sx) * bytesPerPixel;
unsigned char *pSrcPixel = (unsigned char *)dst->data + bytesOffset;
// Repeat the first pixel data throughout the row
for (int x = 1; x < (int)rec.width; x++)
{
ImageDrawPixel(dst, x, y, color);
memcpy(pSrcPixel + x * bytesPerPixel, pSrcPixel, bytesPerPixel);
}
}
}
@ -3472,6 +3492,8 @@ void DrawTextureNPatch(Texture2D texture, NPatchInfo nPatchInfo, Rectangle dest,
coordD.x = (nPatchInfo.source.x + nPatchInfo.source.width)/width;
coordD.y = (nPatchInfo.source.y + nPatchInfo.source.height)/height;
rlCheckRenderBatchLimit(9 * 3 * 2); // Maxium number of verts that could happen
rlSetTexture(texture.id);
rlPushMatrix();

View file

@ -148,6 +148,7 @@ void TraceLog(int logType, const char *text, ...)
strcat(buffer, text);
strcat(buffer, "\n");
vprintf(buffer, args);
fflush(stdout);
#endif
va_end(args);
@ -268,6 +269,51 @@ bool SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite)
return success;
}
// Export data to code (.h), returns true on success
bool ExportDataAsCode(const char *data, unsigned int size, const char *fileName)
{
bool success = false;
#ifndef TEXT_BYTES_PER_LINE
#define TEXT_BYTES_PER_LINE 20
#endif
// NOTE: Text data buffer size is estimated considering raw data size in bytes
// and requiring 6 char bytes for every byte: "0x00, "
char *txtData = (char *)RL_CALLOC(size*6 + 2000, sizeof(char));
int byteCount = 0;
byteCount += sprintf(txtData + byteCount, "////////////////////////////////////////////////////////////////////////////////////////\n");
byteCount += sprintf(txtData + byteCount, "// //\n");
byteCount += sprintf(txtData + byteCount, "// DataAsCode exporter v1.0 - Raw data exported as an array of bytes //\n");
byteCount += sprintf(txtData + byteCount, "// //\n");
byteCount += sprintf(txtData + byteCount, "// more info and bugs-report: github.com/raysan5/raylib //\n");
byteCount += sprintf(txtData + byteCount, "// feedback and support: ray[at]raylib.com //\n");
byteCount += sprintf(txtData + byteCount, "// //\n");
byteCount += sprintf(txtData + byteCount, "// Copyright (c) 2022 Ramon Santamaria (@raysan5) //\n");
byteCount += sprintf(txtData + byteCount, "// //\n");
byteCount += sprintf(txtData + byteCount, "////////////////////////////////////////////////////////////////////////////////////////\n\n");
// Get file name from path and convert variable name to uppercase
char varFileName[256] = { 0 };
strcpy(varFileName, GetFileNameWithoutExt(fileName));
for (int i = 0; varFileName[i] != '\0'; i++) if ((varFileName[i] >= 'a') && (varFileName[i] <= 'z')) { varFileName[i] = varFileName[i] - 32; }
byteCount += sprintf(txtData + byteCount, "static unsigned char %s_DATA[%i] = { ", varFileName, size);
for (int i = 0; i < size - 1; i++) byteCount += sprintf(txtData + byteCount, ((i%TEXT_BYTES_PER_LINE == 0)? "0x%x,\n" : "0x%x, "), data[i]);
byteCount += sprintf(txtData + byteCount, "0x%x };\n", data[size - 1]);
// NOTE: Text data size exported is determined by '\0' (NULL) character
success = SaveFileText(fileName, txtData);
RL_FREE(txtData);
if (success != 0) TRACELOG(LOG_INFO, "FILEIO: [%s] Data as code exported successfully", fileName);
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to export data as code", fileName);
return success;
}
// Load text data from file, returns a '\0' terminated string
// NOTE: text chars array should be freed manually
char *LoadFileText(const char *fileName)