mirror of
https://github.com/love2d/megasource.git
synced 2026-08-18 19:54:37 +02:00
update SDL3 to 3.4.10.
This commit is contained in:
+147
-73
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
@@ -97,12 +97,13 @@ static SDL_FPoint ProjectVec3ToRect(const Vector3 *v, const SDL_FRect *rect)
|
||||
const float cameraZ = 4.0f; /* Camera is at(0, 0, +4), looking toward origin */
|
||||
float aspect = rect->w / rect->h;
|
||||
|
||||
float fovScaleY = SDL_tanf((verticalFOV_deg * SDL_PI_F / 180.0f) * 0.5f);
|
||||
float fovScaleY = SDL_tanf(((verticalFOV_deg / 180.0f) * SDL_PI_F) * 0.5f);
|
||||
float fovScaleX = fovScaleY * aspect;
|
||||
|
||||
float relZ = cameraZ - v->z;
|
||||
if (relZ < 0.01f)
|
||||
if (relZ < 0.01f) {
|
||||
relZ = 0.01f; /* Prevent division by 0 or negative depth */
|
||||
}
|
||||
|
||||
float ndc_x = (v->x / relZ) / fovScaleX;
|
||||
float ndc_y = (v->y / relZ) / fovScaleY;
|
||||
@@ -207,6 +208,39 @@ void DrawGyroDebugCircle(SDL_Renderer *renderer, const Quaternion *orientation,
|
||||
SDL_SetRenderDrawColor(renderer, r, g, b, a);
|
||||
}
|
||||
|
||||
|
||||
void DrawGyroDebugAxes(SDL_Renderer *renderer, const Quaternion *orientation, const SDL_FRect *bounds)
|
||||
{
|
||||
/* Store current color */
|
||||
Uint8 r, g, b, a;
|
||||
SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
|
||||
|
||||
Vector3 origin = { 0.0f, 0.0f, 0.0f };
|
||||
|
||||
Vector3 right = { 1.0f, 0.0f, 0.0f };
|
||||
Vector3 up = { 0.0f, 1.0f, 0.0f };
|
||||
Vector3 back = { 0.0f, 0.0f, 1.0f };
|
||||
|
||||
Vector3 world_right = RotateVectorByQuaternion(&right, orientation);
|
||||
Vector3 world_up = RotateVectorByQuaternion(&up, orientation);
|
||||
Vector3 world_back = RotateVectorByQuaternion(&back, orientation);
|
||||
|
||||
SDL_FPoint origin_screen = ProjectVec3ToRect(&origin, bounds);
|
||||
SDL_FPoint right_screen = ProjectVec3ToRect(&world_right, bounds);
|
||||
SDL_FPoint up_screen = ProjectVec3ToRect(&world_up, bounds);
|
||||
SDL_FPoint back_screen = ProjectVec3ToRect(&world_back, bounds);
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, GYRO_COLOR_RED);
|
||||
SDL_RenderLine(renderer, origin_screen.x, origin_screen.y, right_screen.x, right_screen.y);
|
||||
SDL_SetRenderDrawColor(renderer, GYRO_COLOR_GREEN);
|
||||
SDL_RenderLine(renderer, origin_screen.x, origin_screen.y, up_screen.x, up_screen.y);
|
||||
SDL_SetRenderDrawColor(renderer, GYRO_COLOR_BLUE);
|
||||
SDL_RenderLine(renderer, origin_screen.x, origin_screen.y, back_screen.x, back_screen.y);
|
||||
|
||||
/* Restore current color */
|
||||
SDL_SetRenderDrawColor(renderer, r, g, b, a);
|
||||
}
|
||||
|
||||
void DrawAccelerometerDebugArrow(SDL_Renderer *renderer, const Quaternion *gyro_quaternion, const float *accel_data, const SDL_FRect *bounds)
|
||||
{
|
||||
/* Store current color */
|
||||
@@ -335,6 +369,7 @@ struct GamepadImage
|
||||
bool showing_front;
|
||||
bool showing_touchpad;
|
||||
SDL_GamepadType type;
|
||||
SDL_GamepadButtonLabel east_label;
|
||||
ControllerDisplayMode display_mode;
|
||||
|
||||
bool elements[SDL_GAMEPAD_ELEMENT_MAX];
|
||||
@@ -353,7 +388,7 @@ static SDL_Texture *CreateTexture(SDL_Renderer *renderer, unsigned char *data, u
|
||||
SDL_Surface *surface;
|
||||
SDL_IOStream *src = SDL_IOFromConstMem(data, len);
|
||||
if (src) {
|
||||
surface = SDL_LoadBMP_IO(src, true);
|
||||
surface = SDL_LoadPNG_IO(src, true);
|
||||
if (surface) {
|
||||
texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||
SDL_DestroySurface(surface);
|
||||
@@ -367,32 +402,32 @@ GamepadImage *CreateGamepadImage(SDL_Renderer *renderer)
|
||||
GamepadImage *ctx = SDL_calloc(1, sizeof(*ctx));
|
||||
if (ctx) {
|
||||
ctx->renderer = renderer;
|
||||
ctx->front_texture = CreateTexture(renderer, gamepad_front_bmp, gamepad_front_bmp_len);
|
||||
ctx->back_texture = CreateTexture(renderer, gamepad_back_bmp, gamepad_back_bmp_len);
|
||||
ctx->front_texture = CreateTexture(renderer, gamepad_front_png, gamepad_front_png_len);
|
||||
ctx->back_texture = CreateTexture(renderer, gamepad_back_png, gamepad_back_png_len);
|
||||
SDL_GetTextureSize(ctx->front_texture, &ctx->gamepad_width, &ctx->gamepad_height);
|
||||
|
||||
ctx->face_abxy_texture = CreateTexture(renderer, gamepad_face_abxy_bmp, gamepad_face_abxy_bmp_len);
|
||||
ctx->face_axby_texture = CreateTexture(renderer, gamepad_face_axby_bmp, gamepad_face_axby_bmp_len);
|
||||
ctx->face_bayx_texture = CreateTexture(renderer, gamepad_face_bayx_bmp, gamepad_face_bayx_bmp_len);
|
||||
ctx->face_sony_texture = CreateTexture(renderer, gamepad_face_sony_bmp, gamepad_face_sony_bmp_len);
|
||||
ctx->face_abxy_texture = CreateTexture(renderer, gamepad_face_abxy_png, gamepad_face_abxy_png_len);
|
||||
ctx->face_axby_texture = CreateTexture(renderer, gamepad_face_axby_png, gamepad_face_axby_png_len);
|
||||
ctx->face_bayx_texture = CreateTexture(renderer, gamepad_face_bayx_png, gamepad_face_bayx_png_len);
|
||||
ctx->face_sony_texture = CreateTexture(renderer, gamepad_face_sony_png, gamepad_face_sony_png_len);
|
||||
SDL_GetTextureSize(ctx->face_abxy_texture, &ctx->face_width, &ctx->face_height);
|
||||
|
||||
ctx->connection_texture[0] = CreateTexture(renderer, gamepad_wired_bmp, gamepad_wired_bmp_len);
|
||||
ctx->connection_texture[1] = CreateTexture(renderer, gamepad_wireless_bmp, gamepad_wireless_bmp_len);
|
||||
ctx->connection_texture[0] = CreateTexture(renderer, gamepad_wired_png, gamepad_wired_png_len);
|
||||
ctx->connection_texture[1] = CreateTexture(renderer, gamepad_wireless_png, gamepad_wireless_png_len);
|
||||
SDL_GetTextureSize(ctx->connection_texture[0], &ctx->connection_width, &ctx->connection_height);
|
||||
|
||||
ctx->battery_texture[0] = CreateTexture(renderer, gamepad_battery_bmp, gamepad_battery_bmp_len);
|
||||
ctx->battery_texture[1] = CreateTexture(renderer, gamepad_battery_wired_bmp, gamepad_battery_wired_bmp_len);
|
||||
ctx->battery_texture[0] = CreateTexture(renderer, gamepad_battery_png, gamepad_battery_png_len);
|
||||
ctx->battery_texture[1] = CreateTexture(renderer, gamepad_battery_wired_png, gamepad_battery_wired_png_len);
|
||||
SDL_GetTextureSize(ctx->battery_texture[0], &ctx->battery_width, &ctx->battery_height);
|
||||
|
||||
ctx->touchpad_texture = CreateTexture(renderer, gamepad_touchpad_bmp, gamepad_touchpad_bmp_len);
|
||||
ctx->touchpad_texture = CreateTexture(renderer, gamepad_touchpad_png, gamepad_touchpad_png_len);
|
||||
SDL_GetTextureSize(ctx->touchpad_texture, &ctx->touchpad_width, &ctx->touchpad_height);
|
||||
|
||||
ctx->button_texture = CreateTexture(renderer, gamepad_button_bmp, gamepad_button_bmp_len);
|
||||
ctx->button_texture = CreateTexture(renderer, gamepad_button_png, gamepad_button_png_len);
|
||||
SDL_GetTextureSize(ctx->button_texture, &ctx->button_width, &ctx->button_height);
|
||||
SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
|
||||
|
||||
ctx->axis_texture = CreateTexture(renderer, gamepad_axis_bmp, gamepad_axis_bmp_len);
|
||||
ctx->axis_texture = CreateTexture(renderer, gamepad_axis_png, gamepad_axis_png_len);
|
||||
SDL_GetTextureSize(ctx->axis_texture, &ctx->axis_width, &ctx->axis_height);
|
||||
SDL_SetTextureColorMod(ctx->axis_texture, 10, 255, 21);
|
||||
|
||||
@@ -640,6 +675,7 @@ void UpdateGamepadImageFromGamepad(GamepadImage *ctx, SDL_Gamepad *gamepad)
|
||||
}
|
||||
|
||||
ctx->type = SDL_GetGamepadType(gamepad);
|
||||
ctx->east_label = SDL_GetGamepadButtonLabel(gamepad, SDL_GAMEPAD_BUTTON_EAST);
|
||||
char *mapping = SDL_GetGamepadMapping(gamepad);
|
||||
if (mapping) {
|
||||
if (SDL_strstr(mapping, "SDL_GAMECONTROLLER_USE_BUTTON_LABELS")) {
|
||||
@@ -761,7 +797,7 @@ void RenderGamepadImage(GamepadImage *ctx)
|
||||
dst.w = ctx->face_width;
|
||||
dst.h = ctx->face_height;
|
||||
|
||||
switch (SDL_GetGamepadButtonLabelForType(ctx->type, SDL_GAMEPAD_BUTTON_EAST)) {
|
||||
switch (ctx->east_label) {
|
||||
case SDL_GAMEPAD_BUTTON_LABEL_B:
|
||||
SDL_RenderTexture(ctx->renderer, ctx->face_abxy_texture, NULL, &dst);
|
||||
break;
|
||||
@@ -890,6 +926,7 @@ void DestroyGamepadImage(GamepadImage *ctx)
|
||||
SDL_DestroyTexture(ctx->touchpad_texture);
|
||||
SDL_DestroyTexture(ctx->button_texture);
|
||||
SDL_DestroyTexture(ctx->axis_texture);
|
||||
SDL_free(ctx->fingers);
|
||||
SDL_free(ctx);
|
||||
}
|
||||
}
|
||||
@@ -964,10 +1001,10 @@ GamepadDisplay *CreateGamepadDisplay(SDL_Renderer *renderer)
|
||||
if (ctx) {
|
||||
ctx->renderer = renderer;
|
||||
|
||||
ctx->button_texture = CreateTexture(renderer, gamepad_button_small_bmp, gamepad_button_small_bmp_len);
|
||||
ctx->button_texture = CreateTexture(renderer, gamepad_button_small_png, gamepad_button_small_png_len);
|
||||
SDL_GetTextureSize(ctx->button_texture, &ctx->button_width, &ctx->button_height);
|
||||
|
||||
ctx->arrow_texture = CreateTexture(renderer, gamepad_axis_arrow_bmp, gamepad_axis_arrow_bmp_len);
|
||||
ctx->arrow_texture = CreateTexture(renderer, gamepad_axis_arrow_png, gamepad_axis_arrow_png_len);
|
||||
SDL_GetTextureSize(ctx->arrow_texture, &ctx->arrow_width, &ctx->arrow_height);
|
||||
|
||||
ctx->element_highlighted = SDL_GAMEPAD_ELEMENT_INVALID;
|
||||
@@ -990,11 +1027,15 @@ struct GyroDisplay
|
||||
/* This part displays extra info from the IMUstate in order to figure out actual polling rates. */
|
||||
float gyro_drift_solution[3];
|
||||
int reported_sensor_rate_hz; /*hz - comes from HIDsdl implementation. Could be fixed, platform time, or true sensor time*/
|
||||
Uint64 next_reported_sensor_time; /* SDL ticks used to throttle the display */
|
||||
|
||||
int estimated_sensor_rate_hz; /*hz - our estimation of the actual polling rate by observing packets received*/
|
||||
float euler_displacement_angles[3]; /* pitch, yaw, roll */
|
||||
Quaternion gyro_quaternion; /* Rotation since startup/reset, comprised of each gyro speed packet times sensor delta time. */
|
||||
float drift_calibration_progress_frac; /* [0..1] */
|
||||
EGyroCalibrationPhase current_calibration_phase;
|
||||
float calibration_phase_progress_fraction; /* [0..1] */
|
||||
float accelerometer_noise_sq; /* Distance between last noise and new noise. Used to indicate motion.*/
|
||||
float accelerometer_noise_tolerance_sq; /* Maximum amount of noise detected during the Noise Profiling Phase */
|
||||
|
||||
GamepadButton *reset_gyro_button;
|
||||
GamepadButton *calibrate_gyro_button;
|
||||
@@ -1009,7 +1050,12 @@ GyroDisplay *CreateGyroDisplay(SDL_Renderer *renderer)
|
||||
SDL_zeroa(ctx->gyro_drift_solution);
|
||||
Quaternion quat_identity = { 0.0f, 0.0f, 0.0f, 1.0f };
|
||||
ctx->gyro_quaternion = quat_identity;
|
||||
|
||||
ctx->reported_sensor_rate_hz = 0;
|
||||
ctx->next_reported_sensor_time = 0;
|
||||
ctx->current_calibration_phase = GYRO_CALIBRATION_PHASE_OFF;
|
||||
ctx->calibration_phase_progress_fraction = 0.0f; /* [0..1] */
|
||||
ctx->accelerometer_noise_sq = 0.0f;
|
||||
ctx->accelerometer_noise_tolerance_sq = ACCELEROMETER_NOISE_THRESHOLD; /* Will be overwritten but this avoids divide by zero. */
|
||||
ctx->reset_gyro_button = CreateGamepadButton(renderer, "Reset View");
|
||||
ctx->calibrate_gyro_button = CreateGamepadButton(renderer, "Recalibrate Drift");
|
||||
}
|
||||
@@ -1024,7 +1070,6 @@ void SetGyroDisplayArea(GyroDisplay *ctx, const SDL_FRect *area)
|
||||
}
|
||||
|
||||
SDL_copyp(&ctx->area, area);
|
||||
|
||||
/* Place the reset button to the bottom right of the gyro display area.*/
|
||||
SDL_FRect reset_button_area;
|
||||
reset_button_area.w = SDL_max(MINIMUM_BUTTON_WIDTH, GetGamepadButtonLabelWidth(ctx->reset_gyro_button) + 2 * BUTTON_PADDING);
|
||||
@@ -1324,32 +1369,29 @@ static void RenderGamepadElementHighlight(GamepadDisplay *ctx, int element, cons
|
||||
}
|
||||
}
|
||||
|
||||
bool BHasCachedGyroDriftSolution(GyroDisplay *ctx)
|
||||
{
|
||||
if (!ctx) {
|
||||
return false;
|
||||
}
|
||||
return (ctx->gyro_drift_solution[0] != 0.0f ||
|
||||
ctx->gyro_drift_solution[1] != 0.0f ||
|
||||
ctx->gyro_drift_solution[2] != 0.0f);
|
||||
}
|
||||
|
||||
void SetGamepadDisplayIMUValues(GyroDisplay *ctx, float *gyro_drift_solution, float *euler_displacement_angles, Quaternion *gyro_quaternion, int reported_senor_rate_hz, int estimated_sensor_rate_hz, float drift_calibration_progress_frac, float accelerometer_noise_sq)
|
||||
void SetGamepadDisplayIMUValues(GyroDisplay *ctx, float *gyro_drift_solution, float *euler_displacement_angles, Quaternion *gyro_quaternion, int reported_senor_rate_hz, int estimated_sensor_rate_hz, EGyroCalibrationPhase calibration_phase, float drift_calibration_progress_frac, float accelerometer_noise_sq, float accelerometer_noise_tolerance_sq)
|
||||
{
|
||||
if (!ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
const int SENSOR_UPDATE_INTERVAL_MS = 100;
|
||||
Uint64 now = SDL_GetTicks();
|
||||
if (now > ctx->next_reported_sensor_time) {
|
||||
ctx->estimated_sensor_rate_hz = estimated_sensor_rate_hz;
|
||||
if (reported_senor_rate_hz != 0) {
|
||||
ctx->reported_sensor_rate_hz = reported_senor_rate_hz;
|
||||
}
|
||||
ctx->next_reported_sensor_time = now + SENSOR_UPDATE_INTERVAL_MS;
|
||||
}
|
||||
|
||||
SDL_memcpy(ctx->gyro_drift_solution, gyro_drift_solution, sizeof(ctx->gyro_drift_solution));
|
||||
ctx->estimated_sensor_rate_hz = estimated_sensor_rate_hz;
|
||||
|
||||
if (reported_senor_rate_hz != 0)
|
||||
ctx->reported_sensor_rate_hz = reported_senor_rate_hz;
|
||||
|
||||
SDL_memcpy(ctx->euler_displacement_angles, euler_displacement_angles, sizeof(ctx->euler_displacement_angles));
|
||||
ctx->gyro_quaternion = *gyro_quaternion;
|
||||
ctx->drift_calibration_progress_frac = drift_calibration_progress_frac;
|
||||
ctx->current_calibration_phase = calibration_phase;
|
||||
ctx->calibration_phase_progress_fraction = drift_calibration_progress_frac;
|
||||
ctx->accelerometer_noise_sq = accelerometer_noise_sq;
|
||||
ctx->accelerometer_noise_tolerance_sq = accelerometer_noise_tolerance_sq;
|
||||
}
|
||||
|
||||
extern GamepadButton *GetGyroResetButton(GyroDisplay *ctx)
|
||||
@@ -1638,7 +1680,6 @@ void RenderGamepadDisplay(GamepadDisplay *ctx, SDL_Gamepad *gamepad)
|
||||
SDL_snprintf(text, sizeof(text), "[%.2f,%.2f,%.2f]%s/s", ctx->gyro_data[0] * RAD_TO_DEG, ctx->gyro_data[1] * RAD_TO_DEG, ctx->gyro_data[2] * RAD_TO_DEG, DEGREE_UTF8);
|
||||
SDLTest_DrawString(ctx->renderer, x + center + 2.0f, y, text);
|
||||
|
||||
|
||||
/* Display the testcontroller tool's evaluation of drift. This is also useful to get an average rate of turn in calibrated turntable tests. */
|
||||
if (ctx->gyro_drift_correction_data[0] != 0.0f && ctx->gyro_drift_correction_data[2] != 0.0f && ctx->gyro_drift_correction_data[2] != 0.0f )
|
||||
{
|
||||
@@ -1648,10 +1689,7 @@ void RenderGamepadDisplay(GamepadDisplay *ctx, SDL_Gamepad *gamepad)
|
||||
SDL_snprintf(text, sizeof(text), "[%.2f,%.2f,%.2f]%s/s", ctx->gyro_drift_correction_data[0] * RAD_TO_DEG, ctx->gyro_drift_correction_data[1] * RAD_TO_DEG, ctx->gyro_drift_correction_data[2] * RAD_TO_DEG, DEGREE_UTF8);
|
||||
SDLTest_DrawString(ctx->renderer, x + center + 2.0f, y, text);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
SDL_free(mapping);
|
||||
@@ -1673,7 +1711,7 @@ void RenderSensorTimingInfo(GyroDisplay *ctx, GamepadDisplay *gamepad_display)
|
||||
/* Sensor timing section */
|
||||
char text[128];
|
||||
const float new_line_height = gamepad_display->button_height + 2.0f;
|
||||
const float text_offset_x = ctx->area.x + ctx->area.w / 4.0f + 40.0f;
|
||||
const float text_offset_x = ctx->area.x + ctx->area.w / 4.0f + 35.0f;
|
||||
/* Anchor to bottom left of principle rect. */
|
||||
float text_y_pos = ctx->area.y + ctx->area.h - new_line_height * 2;
|
||||
/*
|
||||
@@ -1719,7 +1757,6 @@ void RenderGyroDriftCalibrationButton(GyroDisplay *ctx, GamepadDisplay *gamepad_
|
||||
float log_y = ctx->area.y + BUTTON_PADDING;
|
||||
const float new_line_height = gamepad_display->button_height + 2.0f;
|
||||
GamepadButton *start_calibration_button = GetGyroCalibrateButton(ctx);
|
||||
bool bHasCachedDriftSolution = BHasCachedGyroDriftSolution(ctx);
|
||||
|
||||
/* Show the recalibration progress bar. */
|
||||
float recalibrate_button_width = GetGamepadButtonLabelWidth(start_calibration_button) + 2 * BUTTON_PADDING;
|
||||
@@ -1729,24 +1766,44 @@ void RenderGyroDriftCalibrationButton(GyroDisplay *ctx, GamepadDisplay *gamepad_
|
||||
recalibrate_button_area.w = GetGamepadButtonLabelWidth(start_calibration_button) + 2.0f * BUTTON_PADDING;
|
||||
recalibrate_button_area.h = gamepad_display->button_height + BUTTON_PADDING * 2.0f;
|
||||
|
||||
if (!bHasCachedDriftSolution) {
|
||||
SDL_snprintf(label_text, sizeof(label_text), "Progress: %3.0f%% ", ctx->drift_calibration_progress_frac * 100.0f);
|
||||
} else {
|
||||
SDL_strlcpy(label_text, "Calibrate Drift", sizeof(label_text));
|
||||
/* Above button */
|
||||
SDL_strlcpy(label_text, "Gyro Orientation:", sizeof(label_text));
|
||||
SDLTest_DrawString(ctx->renderer, recalibrate_button_area.x, recalibrate_button_area.y - new_line_height, label_text);
|
||||
|
||||
/* Button label vs state */
|
||||
if (ctx->current_calibration_phase == GYRO_CALIBRATION_PHASE_OFF) {
|
||||
SDL_strlcpy(label_text, "Start Gyro Calibration", sizeof(label_text));
|
||||
} else if (ctx->current_calibration_phase == GYRO_CALIBRATION_PHASE_NOISE_PROFILING) {
|
||||
SDL_snprintf(label_text, sizeof(label_text), "Noise Progress: %3.0f%% ", ctx->calibration_phase_progress_fraction * 100.0f);
|
||||
} else if (ctx->current_calibration_phase == GYRO_CALIBRATION_PHASE_DRIFT_PROFILING) {
|
||||
SDL_snprintf(label_text, sizeof(label_text), "Drift Progress: %3.0f%% ", ctx->calibration_phase_progress_fraction * 100.0f);
|
||||
} else if (ctx->current_calibration_phase == GYRO_CALIBRATION_PHASE_COMPLETE) {
|
||||
SDL_strlcpy(label_text, "Recalibrate Gyro", sizeof(label_text));
|
||||
}
|
||||
|
||||
SetGamepadButtonLabel(start_calibration_button, label_text);
|
||||
SetGamepadButtonArea(start_calibration_button, &recalibrate_button_area);
|
||||
RenderGamepadButton(start_calibration_button);
|
||||
|
||||
/* Above button */
|
||||
SDL_strlcpy(label_text, "Gyro Orientation:", sizeof(label_text));
|
||||
SDLTest_DrawString(ctx->renderer, recalibrate_button_area.x, recalibrate_button_area.y - new_line_height, label_text);
|
||||
bool bExtremeNoise = ctx->accelerometer_noise_sq > ACCELEROMETER_MAX_NOISE_G_SQ;
|
||||
/* Explicit warning message if we detect too much movement */
|
||||
if (ctx->current_calibration_phase == GYRO_CALIBRATION_PHASE_OFF) {
|
||||
if (bExtremeNoise) {
|
||||
SDL_strlcpy(label_text, "GamePad Must Be Still", sizeof(label_text));
|
||||
SDLTest_DrawString(ctx->renderer, recalibrate_button_area.x, recalibrate_button_area.y + recalibrate_button_area.h + new_line_height, label_text);
|
||||
SDL_strlcpy(label_text, "Place GamePad On Table", sizeof(label_text));
|
||||
SDLTest_DrawString(ctx->renderer, recalibrate_button_area.x, recalibrate_button_area.y + recalibrate_button_area.h + new_line_height * 2, label_text);
|
||||
}
|
||||
}
|
||||
|
||||
if (!bHasCachedDriftSolution) {
|
||||
if (ctx->current_calibration_phase == GYRO_CALIBRATION_PHASE_NOISE_PROFILING ||
|
||||
ctx->current_calibration_phase == GYRO_CALIBRATION_PHASE_DRIFT_PROFILING)
|
||||
{
|
||||
float flAbsoluteNoiseFraction = SDL_clamp(ctx->accelerometer_noise_sq / ACCELEROMETER_MAX_NOISE_G_SQ, 0.0f, 1.0f);
|
||||
float flAbsoluteToleranceFraction = SDL_clamp(ctx->accelerometer_noise_tolerance_sq / ACCELEROMETER_MAX_NOISE_G_SQ, 0.0f, 1.0f);
|
||||
|
||||
float flNoiseFraction = SDL_clamp(SDL_sqrtf(ctx->accelerometer_noise_sq) / ACCELEROMETER_NOISE_THRESHOLD, 0.0f, 1.0f);
|
||||
bool bTooMuchNoise = (flNoiseFraction == 1.0f);
|
||||
float flMaxNoiseForThisPhase = ctx->current_calibration_phase == GYRO_CALIBRATION_PHASE_NOISE_PROFILING ? ACCELEROMETER_MAX_NOISE_G_SQ : ctx->accelerometer_noise_tolerance_sq;
|
||||
float flRelativeNoiseFraction = SDL_clamp(ctx->accelerometer_noise_sq / flMaxNoiseForThisPhase, 0.0f, 1.0f);
|
||||
|
||||
float noise_bar_height = gamepad_display->button_height;
|
||||
SDL_FRect noise_bar_rect;
|
||||
@@ -1755,25 +1812,38 @@ void RenderGyroDriftCalibrationButton(GyroDisplay *ctx, GamepadDisplay *gamepad_
|
||||
noise_bar_rect.w = recalibrate_button_area.w;
|
||||
noise_bar_rect.h = noise_bar_height;
|
||||
|
||||
/* Adjust the noise bar rectangle based on the accelerometer noise value */
|
||||
SDL_snprintf(label_text, sizeof(label_text), "Accelerometer Noise Tolerance: %3.3fG ", SDL_sqrtf(ctx->accelerometer_noise_tolerance_sq) );
|
||||
SDLTest_DrawString(ctx->renderer, recalibrate_button_area.x, recalibrate_button_area.y + recalibrate_button_area.h + new_line_height * 2, label_text);
|
||||
|
||||
float noise_bar_fill_width = flNoiseFraction * noise_bar_rect.w; /* Scale the width based on the noise value */
|
||||
/* Adjust the noise bar rectangle based on the accelerometer noise value */
|
||||
float noise_bar_fill_width = flAbsoluteNoiseFraction * noise_bar_rect.w; /* Scale the width based on the noise value */
|
||||
SDL_FRect noise_bar_fill_rect;
|
||||
noise_bar_fill_rect.x = noise_bar_rect.x + (noise_bar_rect.w - noise_bar_fill_width) * 0.5f;
|
||||
noise_bar_fill_rect.y = noise_bar_rect.y;
|
||||
noise_bar_fill_rect.w = noise_bar_fill_width;
|
||||
noise_bar_fill_rect.h = noise_bar_height;
|
||||
|
||||
/* Set the color based on the noise value */
|
||||
Uint8 red = (Uint8)(flNoiseFraction * 255.0f);
|
||||
Uint8 green = (Uint8)((1.0f - flNoiseFraction) * 255.0f);
|
||||
/* Set the color based on the noise value vs the tolerance */
|
||||
Uint8 red = (Uint8)(flRelativeNoiseFraction * 255.0f);
|
||||
Uint8 green = (Uint8)((1.0f - flRelativeNoiseFraction) * 255.0f);
|
||||
SDL_SetRenderDrawColor(ctx->renderer, red, green, 0, 255); /* red when high noise, green when low noise */
|
||||
SDL_RenderFillRect(ctx->renderer, &noise_bar_fill_rect); /* draw the filled rectangle */
|
||||
|
||||
float tolerance_bar_fill_width = flAbsoluteToleranceFraction * noise_bar_rect.w; /* Scale the width based on the noise value */
|
||||
SDL_FRect tolerance_bar_rect;
|
||||
tolerance_bar_rect.x = noise_bar_rect.x + (noise_bar_rect.w - tolerance_bar_fill_width) * 0.5f;
|
||||
tolerance_bar_rect.y = noise_bar_rect.y;
|
||||
tolerance_bar_rect.w = tolerance_bar_fill_width;
|
||||
tolerance_bar_rect.h = noise_bar_height;
|
||||
|
||||
SDL_SetRenderDrawColor(ctx->renderer, 128, 128, 0, 255);
|
||||
SDL_RenderRect(ctx->renderer, &tolerance_bar_rect); /* draw the tolerance rectangle */
|
||||
|
||||
SDL_SetRenderDrawColor(ctx->renderer, 100, 100, 100, 255); /* gray box */
|
||||
SDL_RenderRect(ctx->renderer, &noise_bar_rect); /* draw the outline rectangle */
|
||||
|
||||
/* Explicit warning message if we detect too much movement */
|
||||
bool bTooMuchNoise = (flAbsoluteNoiseFraction == 1.0f);
|
||||
if (bTooMuchNoise) {
|
||||
SDL_strlcpy(label_text, "Place GamePad Down!", sizeof(label_text));
|
||||
SDLTest_DrawString(ctx->renderer, recalibrate_button_area.x, noise_bar_rect.y + noise_bar_rect.h + new_line_height, label_text);
|
||||
@@ -1788,7 +1858,7 @@ void RenderGyroDriftCalibrationButton(GyroDisplay *ctx, GamepadDisplay *gamepad_
|
||||
progress_bar_rect.h = BUTTON_PADDING * 0.5f;
|
||||
|
||||
/* Adjust the drift bar rectangle based on the drift calibration progress fraction */
|
||||
float drift_bar_fill_width = bTooMuchNoise ? 1.0f : ctx->drift_calibration_progress_frac * progress_bar_rect.w;
|
||||
float drift_bar_fill_width = bTooMuchNoise ? 1.0f : ctx->calibration_phase_progress_fraction * progress_bar_rect.w;
|
||||
SDL_FRect progress_bar_fill;
|
||||
progress_bar_fill.x = progress_bar_rect.x;
|
||||
progress_bar_fill.y = progress_bar_rect.y;
|
||||
@@ -1797,7 +1867,6 @@ void RenderGyroDriftCalibrationButton(GyroDisplay *ctx, GamepadDisplay *gamepad_
|
||||
|
||||
/* Set the color based on the drift calibration progress fraction */
|
||||
SDL_SetRenderDrawColor(ctx->renderer, GYRO_COLOR_GREEN); /* red when too much noise, green when low noise*/
|
||||
|
||||
/* Now draw the bars with the filled, then empty rectangles */
|
||||
SDL_RenderFillRect(ctx->renderer, &progress_bar_fill); /* draw the filled rectangle*/
|
||||
SDL_SetRenderDrawColor(ctx->renderer, 100, 100, 100, 255); /* gray box*/
|
||||
@@ -1823,20 +1892,26 @@ float RenderEulerReadout(GyroDisplay *ctx, GamepadDisplay *gamepad_display )
|
||||
const float new_line_height = gamepad_display->button_height + 2.0f;
|
||||
float log_gyro_euler_text_x = gyro_calibrate_button_rect.x;
|
||||
|
||||
Uint8 r, g, b, a;
|
||||
SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
|
||||
/* Pitch Readout */
|
||||
SDL_SetRenderDrawColor(ctx->renderer, GYRO_COLOR_RED);
|
||||
SDL_snprintf(text, sizeof(text), "Pitch: %6.2f%s", ctx->euler_displacement_angles[0], DEGREE_UTF8);
|
||||
SDLTest_DrawString(ctx->renderer, log_gyro_euler_text_x + 2.0f, log_y, text);
|
||||
|
||||
/* Yaw Readout */
|
||||
SDL_SetRenderDrawColor(ctx->renderer, GYRO_COLOR_GREEN);
|
||||
log_y += new_line_height;
|
||||
SDL_snprintf(text, sizeof(text), " Yaw: %6.2f%s", ctx->euler_displacement_angles[1], DEGREE_UTF8);
|
||||
SDLTest_DrawString(ctx->renderer, log_gyro_euler_text_x + 2.0f, log_y, text);
|
||||
|
||||
/* Roll Readout */
|
||||
SDL_SetRenderDrawColor(ctx->renderer, GYRO_COLOR_BLUE);
|
||||
log_y += new_line_height;
|
||||
SDL_snprintf(text, sizeof(text), " Roll: %6.2f%s", ctx->euler_displacement_angles[2], DEGREE_UTF8);
|
||||
SDLTest_DrawString(ctx->renderer, log_gyro_euler_text_x + 2.0f, log_y, text);
|
||||
|
||||
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
|
||||
return log_y + new_line_height; /* Return the next y position for further rendering */
|
||||
}
|
||||
|
||||
@@ -1859,6 +1934,9 @@ void RenderGyroGizmo(GyroDisplay *ctx, SDL_Gamepad *gamepad, float top)
|
||||
/* Draw the rotated cube */
|
||||
DrawGyroDebugCube(ctx->renderer, &ctx->gyro_quaternion, &gizmoRect);
|
||||
|
||||
/* Draw positive axes */
|
||||
DrawGyroDebugAxes(ctx->renderer, &ctx->gyro_quaternion, &gizmoRect);
|
||||
|
||||
/* Overlay the XYZ circles */
|
||||
DrawGyroDebugCircle(ctx->renderer, &ctx->gyro_quaternion, &gizmoRect);
|
||||
|
||||
@@ -1899,14 +1977,12 @@ void RenderGyroDisplay(GyroDisplay *ctx, GamepadDisplay *gamepadElements, SDL_Ga
|
||||
SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
|
||||
|
||||
RenderSensorTimingInfo(ctx, gamepadElements);
|
||||
|
||||
RenderGyroDriftCalibrationButton(ctx, gamepadElements);
|
||||
|
||||
bool bHasCachedDriftSolution = BHasCachedGyroDriftSolution(ctx);
|
||||
if (bHasCachedDriftSolution) {
|
||||
/* Render Gyro calibration phases */
|
||||
if (ctx->current_calibration_phase == GYRO_CALIBRATION_PHASE_COMPLETE) {
|
||||
float bottom = RenderEulerReadout(ctx, gamepadElements);
|
||||
RenderGyroGizmo(ctx, gamepad, bottom);
|
||||
|
||||
}
|
||||
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
|
||||
}
|
||||
@@ -2116,10 +2192,10 @@ JoystickDisplay *CreateJoystickDisplay(SDL_Renderer *renderer)
|
||||
if (ctx) {
|
||||
ctx->renderer = renderer;
|
||||
|
||||
ctx->button_texture = CreateTexture(renderer, gamepad_button_small_bmp, gamepad_button_small_bmp_len);
|
||||
ctx->button_texture = CreateTexture(renderer, gamepad_button_small_png, gamepad_button_small_png_len);
|
||||
SDL_GetTextureSize(ctx->button_texture, &ctx->button_width, &ctx->button_height);
|
||||
|
||||
ctx->arrow_texture = CreateTexture(renderer, gamepad_axis_arrow_bmp, gamepad_axis_arrow_bmp_len);
|
||||
ctx->arrow_texture = CreateTexture(renderer, gamepad_axis_arrow_png, gamepad_axis_arrow_png_len);
|
||||
SDL_GetTextureSize(ctx->arrow_texture, &ctx->arrow_width, &ctx->arrow_height);
|
||||
}
|
||||
return ctx;
|
||||
@@ -2552,7 +2628,7 @@ GamepadButton *CreateGamepadButton(SDL_Renderer *renderer, const char *label)
|
||||
if (ctx) {
|
||||
ctx->renderer = renderer;
|
||||
|
||||
ctx->background = CreateTexture(renderer, gamepad_button_background_bmp, gamepad_button_background_bmp_len);
|
||||
ctx->background = CreateTexture(renderer, gamepad_button_background_png, gamepad_button_background_png_len);
|
||||
SDL_GetTextureSize(ctx->background, &ctx->background_width, &ctx->background_height);
|
||||
|
||||
SetGamepadButtonLabel(ctx, label);
|
||||
@@ -2566,9 +2642,7 @@ void SetGamepadButtonLabel(GamepadButton *ctx, const char *label)
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx->label) {
|
||||
SDL_free(ctx->label);
|
||||
}
|
||||
SDL_free(ctx->label);
|
||||
|
||||
ctx->label = SDL_strdup(label);
|
||||
ctx->label_width = (float)(FONT_CHARACTER_SIZE * SDL_strlen(label));
|
||||
|
||||
Reference in New Issue
Block a user