This commit is contained in:
2026-06-14 19:09:18 +01:00
parent 14bd1a9271
commit 13fa90a0e9
3958 changed files with 999286 additions and 4 deletions
@@ -0,0 +1,330 @@
/* -*- tab-width: 4; -*- */
/* vi: set sw=2 ts=4 expandtab: */
/*
* Copyright 2017-2020 Mark Callow.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @internal
* @class LoadTestSample
* @~English
*
* @brief Definition of a base class for texture loading test samples.
*
* @author Mark Callow, github.com/MarkCallow.
*/
#if defined(_WIN32)
#define _USE_MATH_DEFINES
#endif
#include "LoadTestSample.h"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/vector_angle.hpp>
#include <SDL3/SDL_log.h>
#if !defined(LOADTESTSAMPLE_LOG_GESTURE_DETECTION)
// Log detected and completed gestures.
#define LOADTESTSAMPLE_LOG_GESTURE_DETECTION 0
#endif
#if !defined(LOADTESTSAMPLE_LOG_GESTURE_EVENTS)
// Log events contributing to gesture detection and gestures.
#define LOADTESTSAMPLE_LOG_GESTURE_EVENTS 0
#endif
#if !defined(LOADTESTSAMPLE_LOG_MOUSE_UP_DOWN_EVENTS)
#define LOADTESTSAMPLE_LOG_MOUSE_UP_DOWN_EVENTS 0
#endif
#if !defined(LOADTESTSAMPLE_LOG_MOUSE_MOTION_EVENTS)
#define LOADTESTSAMPLE_LOG_MOUSE_MOTION_EVENTS 0
#endif
#if LOADTESTSAMPLE_LOG_GESTURE_EVENTS
#include <sstream>
const std::string printFingerIds(SDL_Finger* fingers[], uint32_t numFingers) {
std::stringstream msg;
assert(numFingers > 0);
msg << std::hex << std::showbase;
msg << "finger id" << (numFingers > 1 ? "s" : "") << ": ";
for (uint32_t f = 0; f < numFingers; f++) {
if (f > 0) {
if (f == numFingers - 1)
msg << " & ";
else
msg << ", ";
}
msg << fingers[f]->id;
}
return msg.str();
}
const std::string printVector(const std::string& name, glm::vec2 v) {
std::stringstream msg;
msg << name << " (" << v.x << ", " << v.y << ")";
return msg.str();
}
#endif
[[maybe_unused]] static const char*
buttonName(Uint8 button) {
switch(button) {
case SDL_BUTTON_LEFT: return "left";
case SDL_BUTTON_MIDDLE: return "middle";
case SDL_BUTTON_RIGHT: return "right";
default: return "other";
}
}
int
LoadTestSample::doEvent(SDL_Event* event)
{
switch (event->type) {
case SDL_EVENT_MOUSE_MOTION:
{
SDL_MouseMotionEvent& motion = event->motion;
#if LOADTESTSAMPLE_LOG_MOUSE_MOTION_EVENTS
SDL_Log("LTS: MOUSE_MOTION - x: %f, y: %f", motion.x, motion.y);
#endif
// On macOS with trackpad, SDL_TOUCH_MOUSEID is never set.
// Prefer mouse events on macOS because press is required. When
// finger motion events are used the object starts to rotate when
// you drag the cursor over the window. Not nice.
if (mouseButtons.left)
{
rotation.x -= yflip * (mousePos.y - (float)motion.y) * 1.25f;
rotation.y -= (mousePos.x - (float)motion.x) * 1.25f;
viewChanged();
}
if (mouseButtons.right)
{
zoom += (mousePos.y - (float)motion.y) * .005f;
viewChanged();
}
if (mouseButtons.middle)
{
cameraPos.x -= (mousePos.x - (float)motion.x) * 0.01f;
cameraPos.y += yflip * (mousePos.y - (float)motion.y) * 0.01f;
viewChanged();
}
mousePos = glm::vec2((float)motion.x, (float)motion.y);
return 0;
}
case SDL_EVENT_MOUSE_BUTTON_DOWN:
mousePos = glm::vec2((float)event->button.x, (float)event->button.y);
if (LOADTESTSAMPLE_LOG_MOUSE_UP_DOWN_EVENTS) {
SDL_Log("LTS: MOUSE_DOWN - button: %s, x: %f, y: %f", buttonName(event->button.button),
event->button.x, event->button.y);
}
switch (event->button.button) {
case SDL_BUTTON_LEFT:
mouseButtons.left = true;
break;
case SDL_BUTTON_MIDDLE:
mouseButtons.middle = true;
break;
case SDL_BUTTON_RIGHT:
mouseButtons.right = true;
break;
default:
return 1;
}
return 0;
case SDL_EVENT_MOUSE_BUTTON_UP:
if (LOADTESTSAMPLE_LOG_MOUSE_UP_DOWN_EVENTS) {
SDL_Log("LTS: MOUSE_UP - button: %s, x: %f, y: %f", buttonName(event->button.button),
event->button.x, event->button.y);
}
switch (event->button.button) {
case SDL_BUTTON_LEFT:
mouseButtons.left = false;
break;
case SDL_BUTTON_MIDDLE:
mouseButtons.middle = false;
break;
case SDL_BUTTON_RIGHT:
mouseButtons.right = false;
break;
default:
return 1;
}
return 0;
case SDL_EVENT_FINGER_DOWN: {
// Prevent multifingers from triggering the left button action and
// interfering with multigestures.
//
// On iOS you get a left button down event no matter how many fingers
// you touch to the screen. We want 1 finger mouse to work so
// behaviour is same as pressing the trackpad on macOS, etc. As iOS
// button_down events come before finger_down we can clear the left
// button down state, if we have multiple fingers. Hope this ordering
// is the same on other touch screen platforms that send a left-button
// event regardless of the number of fingers.
//
// On macOS button_down events come after finger_down so this code has
// no effect.
//
// Another way to handle this is to identify the platform and work
// differently for each platform.
int numFingers;
SDL_Finger** fingers = SDL_GetTouchFingers(event->tfinger.touchID, &numFingers);
int retVal = 0;
#if LOADTESTSAMPLE_LOG_GESTURE_EVENTS
SDL_Log("LTS: Finger: %#" SDL_PRIx64 " down - fingers: %i, %s, x: %f, y: %f",
event->tfinger.fingerID, numFingers,
printFingerIds(fingers, numFingers).c_str(),
event->tfinger.x, event->tfinger.y);
#endif
if (numFingers > 1) {
mouseButtons.left = false;
if (LOADTESTSAMPLE_LOG_GESTURE_EVENTS) {
SDL_Log("LTS: FINGER_DOWN with multiple fingers received."
" Resetting mouseButtons.left.");
}
if (numFingers == 2) {
firstFingerId = fingers[0]->id;
// Calc. difference vector between fingers.
glm::vec2 vDifference;
vDifference.x = fingers[1]->x - fingers[0]->x;
vDifference.y = fingers[1]->y - fingers[0]->y;
distanceStart = glm::length(vDifference);
distanceLast = distanceStart;
// Need normalized vectors for glm::orientedAngle
nvDifferenceStart = glm::normalize(vDifference);
nvDifferenceLast = nvDifferenceStart;
processingGesture = true;
#if LOADTESTSAMPLE_LOG_GESTURE_EVENTS
// Angle of vector to X axis.
xAngleStart = atan2f(vDifference.y, vDifference.x);
SDL_Log("LTS: FINGER_DOWN, start values: %s, Distance = %f, XAngle = %f°",
printVector("Difference", vDifference).c_str(),
distanceStart, xAngleStart * 180.0 / M_PI
);
#endif
retVal = 1;
}
}
// It is possible to somehow get out of the window without seeing
// FINGER_UP so as a safeguard stop any previous gesture.
zooming = rotating = false;
SDL_free(fingers);
return retVal;
}
case SDL_EVENT_FINGER_UP: {
int numFingers;
SDL_Finger** fingers = SDL_GetTouchFingers(event->tfinger.touchID, &numFingers);
#if LOADTESTSAMPLE_LOG_GESTURE_EVENTS
SDL_Log("LTS: Finger: %#" SDL_PRIx64 " up - fingers: %i, %s, x: %f, y: %f",
event->tfinger.fingerID, numFingers,
printFingerIds(fingers, numFingers).c_str(),
event->tfinger.x, event->tfinger.y);
#endif
if (processingGesture && numFingers == 2) {
// There may still be one finger down. Even so the action is completed.
if (LOADTESTSAMPLE_LOG_GESTURE_DETECTION) {
SDL_Log("-------------- LTS: %s complete. -----------------",
zooming ? "zooming" : rotating ? "rotating" : "gesture");
}
zooming = rotating = processingGesture = false;
}
SDL_free(fingers);
break;
}
case SDL_EVENT_FINGER_MOTION: {
int numFingers;
SDL_Finger** fingers = SDL_GetTouchFingers(event->tfinger.touchID, &numFingers);
if (numFingers != 2)
return 1;
if (!processingGesture) {
// Protect against FINGER_MOTION without FINGER_DOWN. This can
// happen when the sample is switched by a swipe and the new sample
// receives the tail end of the swipe motion.
return 1;
}
// With two fingers down, events come in pairs. No point in processing
// both.
if (event->tfinger.fingerID == firstFingerId) {
return 0;
}
glm::vec2 vDifference; // Difference vector between the fingers.
vDifference.x = fingers[1]->x - fingers[0]->x;
vDifference.y = fingers[1]->y - fingers[0]->y;
float distance = glm::length(vDifference);
// Normalized vectors required by glm::orientedAngle
glm::vec2 nvDifference = glm::normalize(vDifference);
// Angle between start and current difference vectors
float sAngle = glm::orientedAngle(nvDifferenceStart, nvDifference);
// Angle between current and previous difference vectors
float dAngle = glm::orientedAngle(nvDifferenceLast, nvDifference);
// Difference in distance since last motion event.
float dDist = distance - distanceLast;
// Difference in distance since start.
float dDistStart = distance - distanceStart;
#if LOADTESTSAMPLE_LOG_GESTURE_EVENTS
if (!(rotating || zooming)) {
// Angle from X axis to vDifference vector
float xAngle = atan2f(vDifference.y, vDifference.x);
SDL_Log("LTS FINGER_MOTION: Not zooming or rotating. "
" timestamp = %" SDL_PRIu64 ", %s, %s",
event->tfinger.timestamp,
printFingerIds(fingers, numFingers).c_str(),
printVector("Difference", vDifference).c_str());
SDL_Log("... distanceLast = %f, distance = %f, dDist = %f, dDistStart = %f, xAngle = %f°, sAngle = %f°, dAngle = %f°",
distanceLast, distance, dDist, dDistStart,
xAngle * 180.0 / M_PI, sAngle * 180.0 / M_PI, dAngle * 180.0 / M_PI);
}
#endif
nvDifferenceLast = nvDifference;
distanceLast = distance;
// This is all heuristics derived from use.
if (zooming) {
zoom += dDist * 10.0f;
if (LOADTESTSAMPLE_LOG_GESTURE_EVENTS) {
SDL_Log("LTS MG: Zooming. zoom = %f", zoom);
}
} else if (!rotating) {
if (fabs(dDistStart) >= 0.1 && fabs(dAngle) < 0.5 * M_PI / 180.0) {
zooming = true;
zoom += dDist * 10.0f;
if (LOADTESTSAMPLE_LOG_GESTURE_DETECTION) {
SDL_Log("---------------- LTS MG: pinch/zoom detected ---------------\n"
" dAngle = %f°, dDistStart = %f, dDist = %f, zoom = %f",
dAngle * 180.0 / M_PI, dDistStart, dDist, zoom);
}
}
}
if (rotating) {
rotation.z +=
static_cast<float>(dAngle * 180.0 / M_PI);
if (LOADTESTSAMPLE_LOG_GESTURE_EVENTS) {
SDL_Log("LTS MG: Rotating around Z. rotation.z = %f°", rotation.z);
}
} else if (!zooming) {
if (fabs(sAngle) > 15 * M_PI / 180.0 && fabs(dDistStart) < 0.1) {
rotating = true;
rotation.z += static_cast<float>(dAngle * 180.0 / M_PI);
if (LOADTESTSAMPLE_LOG_GESTURE_DETECTION) {
SDL_Log("---------------- LTS MG: rotation detected ---------------\n"
" sAngle = %f°, dAngle = %f°, dDistStart = %f, rotation.z = %f°",
sAngle * 180 / M_PI, dAngle * 180.0 / M_PI, dDistStart, rotation.z);
}
}
}
viewChanged();
SDL_free(fingers);
return 0;
}
case SDL_EVENT_KEY_UP:
if (event->key.key == 'q')
quit = true;
keyPressed(event->key.key);
return 0;
default:
break;
}
return 1;
}
@@ -0,0 +1,91 @@
/* -*- tab-width: 4; -*- */
/* vi: set sw=2 ts=4 expandtab: */
/*
* Copyright 2017-2020 Mark Callow, <khronos at callow dot im>.
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _LOAD_TEST_SAMPLE_H
#define _LOAD_TEST_SAMPLE_H
#include <string>
#include <SDL3/SDL.h>
#define GLM_FORCE_RADIANS
#include "disable_glm_warnings.h"
#include <glm/glm.hpp>
#include "reenable_warnings.h"
#define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
class LoadTestSample {
public:
typedef uint64_t ticks_t;
LoadTestSample(uint32_t width, uint32_t height,
const std::string sBasePath,
int32_t yflip = 1)
: w_width(width), w_height(height), yflip(yflip),
sBasePath(sBasePath)
{
}
virtual ~LoadTestSample() { };
virtual int doEvent(SDL_Event* event);
virtual void resize(uint32_t width, uint32_t height) = 0;
virtual void run(uint32_t msTicks) = 0;
//virtual void getOverlayText(TextOverlay *textOverlay) { };
typedef LoadTestSample* (*PFN_create)(uint32_t width, uint32_t height,
const char* const szArgs,
const std::string sBasePath);
protected:
virtual void keyPressed(uint32_t /*keyCode*/) { }
virtual void viewChanged() { }
const std::string getAssetPath() { return sBasePath; }
glm::vec3 rotation;
glm::vec3 cameraPos;
glm::vec2 mousePos;
glm::vec2 nvDifferenceStart; // Normalized difference between fingers at start of gesture.
float distanceStart = 0.0; // Distance between fingers at start of gesture.
float xAngleStart = 0.0; // Angle between x-axis and nvDifferenceStart. Unused unless event logging is enabled.
glm::vec2 nvDifferenceLast; // Normalized difference between fingers at last motion event.
float distanceLast = 0.0; // Distance between fingers at last motion event.
Uint64 firstFingerId = 0;
bool processingGesture = false;
struct {
bool left = false;
bool right = false;
bool middle = false;
} mouseButtons;
bool quit = false;
bool rotating = false;
bool zooming = false;
bool paused = false;
float zoom = 0;
uint32_t w_width;
uint32_t w_height;
// Defines a frame rate independent timer value clamped from -1.0...1.0
// For use in animations, rotations, etc.
float timer = 0.0f;
// Multiplier for speeding up (or slowing down) the global timer
float timerSpeed = 0.25f;
// Use to adjust mouse rotation speed
float rotationSpeed = 1.0f;
// Use to adjust mouse zoom speed
float zoomSpeed = 1.0f;
// multiplier to decide if Y increases down or up
int32_t yflip;
const std::string sBasePath;
};
#endif /* _LOAD_TEST_SAMPLE_H */
@@ -0,0 +1,146 @@
/* -*- tab-width: 4; -*- */
/* vi: set sw=2 ts=4 expandtab: */
/*
* Copyright 2017-2020 Mark Callow.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @internal
* @class SwipeDetector
* @~English
*
* @brief Definition of a class for detecting swipes.
*
* @author Mark Callow, github.com/MarkCallow.
*/
#include <assert.h>
#include "SwipeDetector.h"
#include <SDL3/SDL_log.h>
#if !defined(SWIPEDETECTOR_LOG_GESTURE_EVENTS)
#define SWIPEDETECTOR_LOG_GESTURE_EVENTS 0
#endif
#if !defined(SWIPEDETECTOR_LOG_GESTURE_DETECTION)
#define SWIPEDETECTOR_LOG_GESTURE_DETECTION 0
#endif
bool
SwipeDetector::doEvent(SDL_Event* event)
{
bool result = false;
switch (event->type) {
case SDL_EVENT_FINGER_UP: {
int numFingers;
SDL_Finger** fingers = SDL_GetTouchFingers(event->tfinger.touchID, &numFingers);
if (SWIPEDETECTOR_LOG_GESTURE_EVENTS) {
SDL_Log("SD: Finger: %" SDL_PRIx64 " UP - fingers: %i, x: %f, y: %f",
event->tfinger.fingerID, numFingers, event->tfinger.x, event->tfinger.y);
}
// SDL_GetTouchFingers appears to return the number of fingers
// down *before* the event was generated, so 1 means the last finger
// just lifted.
if (numFingers == 1 && gestureStart.time != 0) {
gestureStart.time = 0;
gestureSwipe = false;
if (SWIPEDETECTOR_LOG_GESTURE_DETECTION) {
SDL_Log("***************** SD: FINGER_UP, %smultigesture done *****************",
gestureSwipe ? "Swipe complete & " : "");
}
} else {
result = true;
}
SDL_free(fingers);
break;
}
case GESTURE_MULTIGESTURE: {
Gesture_MultiGestureEvent& mgesture = *(Gesture_MultiGestureEvent *)event;
if (SWIPEDETECTOR_LOG_GESTURE_EVENTS) {
SDL_Log("SD: MG Event: x = %f, y = %f, dAng = %f (%f), dR = %f, numFingers = %i, time = %" SDL_PRIu64,
mgesture.x,
mgesture.y,
mgesture.dTheta * 180.0 / M_PI,
mgesture.dTheta,
mgesture.dDist,
mgesture.numFingers,
mgesture.timestamp);
}
if (SWIPEDETECTOR_LOG_GESTURE_DETECTION) {
SDL_Log("SD: mgestureSwipe = %i, time = %" SDL_PRIu64,
gestureSwipe,
(mgesture.timestamp - gestureStart.time) / 1000000);
}
if (gestureStart.time == 0) {
if (SWIPEDETECTOR_LOG_GESTURE_DETECTION) {
SDL_Log("************ SD: Multigesture detection start **************");
}
gestureStart.time = mgesture.timestamp;
gestureStart.point.x = mgesture.x;
gestureStart.point.y = mgesture.y;
lastVector.reset();
gestureSwipe = false;
} else {
if (!gestureSwipe) {
vector sv; // Vector from start point to current position
float velocity;
float theta; // Angle between current vector and previous vector.
float duration;
sv.w = mgesture.x - gestureStart.point.x;
sv.h = mgesture.y - gestureStart.point.y;
float distance = sv.length();
if (lastVector.has_value()) {
// SDL2 timestamps were in milliseconds, SDL3 are nanoseconds. Given the
// normalized distances reported, using nanoseconds leads to 0 velocitySq.
duration = static_cast<float>(
(mgesture.timestamp - gestureStart.time) / 1000000.0);
velocity = distance / duration;
assert(!std::isinf(velocity));
theta = static_cast<float>(lastVector->getAngle(sv));
if (SWIPEDETECTOR_LOG_GESTURE_DETECTION) {
SDL_Log("SD: Detection: distance = %f, velocity = %f, theta = %f, sv angle = %f, sv angle normalized = %f, lastv angle = %f",
distance, velocity, theta,
sv.getAngle(),
sv.getAngleNormalized(),
lastVector->getAngle());
}
lastVector = sv;
// Multiple events with the same timestamp is a possibility
// hence the isinf() check.
if (std::abs(theta) < 3.0 && std::abs(mgesture.dDist) > 0.01 && !std::isinf(velocity) && velocity > 0.0007) {
if (SWIPEDETECTOR_LOG_GESTURE_DETECTION)
SDL_Log("----------------- SD: Swipe %s detected -----------------",
toString(sv.getDirection()).c_str());
gestureSwipe = true;
if (SDL_EventEnabled(SDL_EVENT_USER)) {
SDL_Event user_event;
// SDL will copy this entire struct! Initialize to keep memory
// checkers happy.
SDL_zero(user_event);
user_event.type = SDL_EVENT_USER;
user_event.user.code = swipeGesture;
user_event.user.data1 = SwipeDetector::directionToPointer(sv.getDirection());
user_event.user.data2 = NULL;
SDL_PushEvent(&user_event);
}
} else {
if (SWIPEDETECTOR_LOG_GESTURE_DETECTION) SDL_Log("SD: No swipe detected.");
result = true;
}
} else {
lastVector = sv;
result = true;
}
}
}
break;
}
default:
result = true;
}
return result;
}
+200
View File
@@ -0,0 +1,200 @@
/* -*- tab-width: 4; -*- */
/* vi: set sw=2 ts=4 expandtab: */
/*
* Copyright 2017-2020 Mark Callow, <khronos at callow dot im>.
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _SWIPE_DETECTOR_H
#define _SWIPE_DETECTOR_H
#if defined(_WIN32)
#define _USE_MATH_DEFINES
#endif
#include <optional>
#include <string>
#include <math.h>
#include <SDL3/SDL.h>
#include "SDL_gesture.h"
class SwipeDetector {
public:
enum class Direction { up, down, left, right };
SwipeDetector() : gestureSwipe(false) {}
bool doEvent(SDL_Event* event);
#if defined(_MSC_VER) && !defined(__clang__)
// Not clangcl
#pragma warning(push)
#pragma warning(disable : 4311)
#pragma warning(disable : 4302)
#pragma warning(disable : 4312)
#endif
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wvoid-pointer-to-int-cast"
#endif
// These conversions allow storing a Direction in a pointer.
// Ugly. Horrible. But preferable to allocating and freeing memory
// when passing the information in user events.
static inline Direction pointerToDirection(void* p) {
return static_cast<SwipeDetector::Direction>(reinterpret_cast<long>(p));
}
// Only preserves the low 32-bits of the pointer; perfect for this use.
static inline void* directionToPointer(SwipeDetector::Direction d) {
return reinterpret_cast<void*>(static_cast<long>(d));
}
#if defined(_MSC_VER) && !defined(__clang__)
#pragma warning(pop)
#endif
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
static const Uint32 swipeGesture = 0x01;
class vector {
public:
float w;
float h;
vector() : w(0.0), h(0.0) { }
vector(float _w, float _h) : w(_w), h(_h) { }
/**
* @~English
* @internal
* @brief Find the angle between the vector and the X-axis
*
* Positive angles increase counter-clockwise from the X-axis
* which has +x to the right.
*
* @return the angle between the vector and the x axis in degrees.
*/
double getAngle() {
double rad = atan2(h, w);
return rad * 180/M_PI;
}
/**
* @~English
* @internal
* @brief Find the angle between this vector and another.
*
* Positive angles increase counter-clockwise.
*
* @return the angle between the 2 vectors in degrees.
*/
double getAngle(const vector& v2) {
// Reputed to be more accurate but in our use so far both approaches give same answer.
//double rad = atan2f(v2.h, v2.w) - SDL_atan2(h, w);
double rad = atan2f(w * v2.h - h * v2.w, w * v2.w + h * v2.h);
return rad * 180/M_PI;
}
/**
* @~English
* @internal
* @brief Find the angle between the vector and the X-axis
*
* Positive angles increase counter-clockwise from the X-axis
* which has +x to the right. Value is normalized to the range 0 to 360.
*
* @return the angle between the vector and the x axis in degrees.
*/
double getAngleNormalized() {
double rad = atan2(h, w) + M_PI;
return fmod(rad*180/M_PI + 180, 360);
}
/**
* @~English
* @internal
* @brief Return the length of the vector.
*
* @return the length of the vector.
*/
float length() { return sqrt(w * w + h * h); }
/**
* @~English
* @internal
* @brief Return the direction of the vector.
*
* @return the direction
*/
Direction getDirection() {
double angle = getAngleNormalized();
return getDirection(angle);
}
/**
* @~English
* @internal
* @brief Return a direction given an angle.
*
* Directions are defined as follows:
*
* Up: [45, 135]
* Right: [0,45] and [315, 360]
* Down: [225, 315]
* Left: [135, 225]
*
* @param angle an angle from 0 to 360°
* @return the direction of an angle
*/
static Direction getDirection(double angle){
if (inRange(angle, 45, 135)) {
return Direction::down;
} else if (inRange(angle, 0, 45) || inRange(angle, 315, 360)) {
return Direction::right;
} else if (inRange(angle, 225, 315)) {
return Direction::up;
} else {
return Direction::left;
}
}
protected:
/**
* @~English
* @internal
* @brief Check if angle falls within an interval.
*
* @param angle an angle
* @param init the initial bound
* @param end the final bound
*
* @return true if the given angle is in the interval [init, end), false
* otherwise.
*/
static bool inRange(double angle, float init, float end){
return (angle >= init) && (angle < end);
}
};
protected:
struct gestureStart {
Uint64 time;
SDL_FPoint point;
gestureStart() { time = 0; point.x = point.y = 0.0; }
} gestureStart;
std::optional<vector> lastVector;
bool gestureSwipe;
};
[[nodiscard]] inline std::string toString(SwipeDetector::Direction dir) {
switch (dir) {
case SwipeDetector::Direction::up: return "up";
case SwipeDetector::Direction::down: return "down";
case SwipeDetector::Direction::left: return "left";
case SwipeDetector::Direction::right: return "right";
// This is to hide a warning from MSVC. According to the solution given
// in https://developercommunity.visualstudio.com/t/Visual-Studio-warning-on-Strongly-typed-/96302
// it is possible to construct an enum class with any value. Thus warning.
default: return "unknown";
}
}
#endif /* _SWIPE_DETECTOR_H */
@@ -0,0 +1,61 @@
/* -*- tab-width: 4; -*- */
/* vi: set sw=2 ts=4 expandtab: */
/*
* Copyright 2017-2020 Mark Callow, <khronos at callow dot im>.
* SPDX-License-Identifier: Apache-2.0
*/
#include <assert.h>
#include <ktx.h>
#include "TranscodeTargetStrToFmt.h"
ktx_transcode_fmt_e
TranscodeTargetStrToFmt(std::string format)
{
if (!format.compare("ETC1_RGB"))
return KTX_TTF_ETC1_RGB;
else if (!format.compare("ETC2_RGBA"))
return KTX_TTF_ETC2_RGBA;
else if (!format.compare("BC1_RGB"))
return KTX_TTF_BC1_RGB;
else if (!format.compare("BC3_RGBA"))
return KTX_TTF_BC3_RGBA;
else if (!format.compare("BC4_R"))
return KTX_TTF_BC4_R;
else if (!format.compare("BC5_RG"))
return KTX_TTF_BC5_RG;
else if (!format.compare("BC7_M6_RGB"))
return KTX_TTF_BC7_M6_RGB;
else if (!format.compare("BC7_M5_RGBA"))
return KTX_TTF_BC7_M5_RGBA;
else if (!format.compare("PVRTC1_4_RGB"))
return KTX_TTF_PVRTC1_4_RGB;
else if (!format.compare("PVRTC1_4_RGBA"))
return KTX_TTF_PVRTC1_4_RGBA;
else if (!format.compare("ASTC_4x4_RGBA"))
return KTX_TTF_ASTC_4x4_RGBA;
else if (!format.compare("PVRTC2_4_RGB"))
return KTX_TTF_PVRTC2_4_RGB;
else if (!format.compare("PVRTC2_4_RGBA"))
return KTX_TTF_PVRTC2_4_RGBA;
else if (!format.compare("ETC2_EAC_R11"))
return KTX_TTF_ETC2_EAC_R11;
else if (!format.compare("ETC2_EAC_RG11"))
return KTX_TTF_ETC2_EAC_RG11;
else if (!format.compare("RGBA32"))
return KTX_TTF_RGBA32;
else if (!format.compare("RGB565"))
return KTX_TTF_RGB565;
else if (!format.compare("BGR565"))
return KTX_TTF_BGR565;
else if (!format.compare("RGBA4444"))
return KTX_TTF_RGBA4444;
else if (!format.compare("ETC"))
return KTX_TTF_ETC;
else if (!format.compare("BC1_OR_3"))
return KTX_TTF_BC1_OR_3;
assert(false); // Error in args in sample table.
return static_cast<ktx_transcode_fmt_e>(-1); // To keep compilers happy.
}
@@ -0,0 +1,16 @@
/* -*- tab-width: 4; -*- */
/* vi: set sw=2 ts=4 expandtab: */
/*
* Copyright 2017-2020 Mark Callow, <khronos at callow dot im>.
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _TRANSCODE_TARGET_STR_TO_FMT_
#define _TRANSCODE_TARGET_STR_TO_FMT_
#include <string>
ktx_transcode_fmt_e TranscodeTargetStrToFmt(std::string format);
#endif /* _TRANSCODE_TARGET_STR_TO_FMT_ */
@@ -0,0 +1,21 @@
/* -*- tab-width: 4; -*- */
/* vi: set sw=2 ts=4 expandtab: */
/*
* Copyright 2021 Mark Callow.
* SPDX-License-Identifier: Apache-2.0
*/
#if !defined DISABLE_GLM_WARNINGS_H
// Temporarily disable the warnings caused by the GLM code.
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable: 4201)
#elif defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
#pragma clang diagnostic ignored "-Wnested-anon-types"
#endif
#endif /* DISABLE_GLM_WARNINGS_H */
+66
View File
@@ -0,0 +1,66 @@
/* -*- tab-width: 4; -*- */
/* vi: set sw=2 ts=4 expandtab: */
/*
* Copyright 2018-2020 Mark Callow, <khronos at callow dot im>.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @internal
* @file
* @~English
*
* @brief Custom exceptions for the load tests.
*/
#include <stdexcept>
#include <string>
#define OUT_OF_HOST_MEMORY -1
#define OUT_OF_DEVICE_MEMORY -2
#define FRAGMENTED_POOL -12
#define OUT_OF_POOL_MEMORY -1000069000
class bad_vulkan_alloc : public std::bad_alloc {
public:
bad_vulkan_alloc(int which, const char* _message) : std::bad_alloc() {
if (which == FRAGMENTED_POOL) {
message << "Pool fragmented when allocating for " << _message << ".";
} else {
std::string memtype;
switch (which) {
case OUT_OF_HOST_MEMORY: memtype = "host"; break;
case OUT_OF_DEVICE_MEMORY: memtype = "device"; break;
case OUT_OF_POOL_MEMORY: memtype = "pool"; break;
default: break;
}
message << "Out of " << memtype << " memory for " << _message << ".";
}
_what = message.str();
}
bad_vulkan_alloc(const bad_vulkan_alloc& in)
: std::bad_alloc()
, message{in.message.str()}
, _what{in._what}
{}
virtual const char* what() const throw() {
return _what.c_str();
}
protected:
std::stringstream message;
std::string _what;
};
class unsupported_ttype : public std::runtime_error {
public:
unsupported_ttype()
: std::runtime_error("Implementation does not support needed operations on image format") { }
unsupported_ttype(std::string& message) : std::runtime_error(message) { }
};
class unsupported_ctype : public std::runtime_error {
public:
unsupported_ctype()
: std::runtime_error("Unsupported compression format") { }
};
@@ -0,0 +1,63 @@
# Copyright 2020 The Khronos Group Inc.
# SPDX-License-Identifier: Apache-2.0
# Max2Obj Version 4.0 Mar 10th, 2001
#
# object default to come ...
#
v -5.000000 -5.000000 -5.000000
v 5.000000 -5.000000 -5.000000
v -5.000000 5.000000 -5.000000
v 5.000000 5.000000 -5.000000
v -5.000000 -5.000000 5.000000
v 5.000000 -5.000000 5.000000
v -5.000000 5.000000 5.000000
v 5.000000 5.000000 5.000000
# 8 vertices
vt 0.000000 0.000000 0.000000
vt 1.000000 0.000000 0.000000
vt 0.000000 1.000000 0.000000
vt 1.000000 1.000000 0.000000
vt 0.000000 0.000000 0.000000
vt 1.000000 0.000000 0.000000
vt 0.000000 1.000000 0.000000
vt 1.000000 1.000000 0.000000
vt 0.000000 0.000000 0.000000
vt 1.000000 0.000000 0.000000
vt 0.000000 1.000000 0.000000
vt 1.000000 1.000000 0.000000
# 12 texture vertices
vn 0.000000 0.000000 -2.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -2.000000
vn 0.000000 -0.000000 2.000000
vn 0.000000 -0.000000 1.000000
vn 0.000000 -0.000000 1.000000
vn 0.000000 -0.000000 2.000000
# 8 vertex normals
g default
s 2
f 1/10/1 3/12/3 4/11/4
f 4/11/4 2/9/2 1/10/1
s 4
f 5/9/5 6/10/6 8/12/8
f 8/12/8 7/11/7 5/9/5
s 8
f 1/5/1 2/6/2 6/8/6
f 6/8/6 5/7/5 1/5/1
s 16
f 2/1/2 4/2/4 8/4/8
f 8/4/8 6/3/6 2/1/2
s 32
f 4/5/4 3/6/3 7/8/7
f 7/8/7 8/7/8 4/5/4
s 64
f 3/1/3 1/2/1 5/4/5
f 5/4/5 7/3/7 3/1/3
# 12 faces
g
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,18 @@
/* -*- tab-width: 4; -*- */
/* vi: set sw=2 ts=4 expandtab: */
/*
* Copyright 2021 Mark Callow.
* SPDX-License-Identifier: Apache-2.0
*/
#if !defined REENABLE_WARNINGS_H
// Reenable warnings disabled by, e.g. disable_glm_warnings.h.
#if defined(_MSC_VER)
#pragma warning(pop)
#elif defined(__clang__)
#pragma clang diagnostic pop
#endif
#endif /* REENABLE_WARNINGS_H */
+448
View File
@@ -0,0 +1,448 @@
/* -*- tab-width: 4; -*- */
/* vi: set sw=2 ts=4 expandtab: */
/*
* Copyright 2016-2020 Mark Callow
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef VECMATH_9B7E1CFE346D11E6AFA2D7DC87495A69_H
#define VECMATH_9B7E1CFE346D11E6AFA2D7DC87495A69_H
/**
* @file
* @~English
*
* @brief Vector math package modelled after GLSL.
*/
#include <assert.h>
#include <math.h>
struct vec2 {
// Anonymous unions are portable.
union {
float x;
float r;
};
union {
float y;
float g;
};
vec2() { };
};
struct vec3 {
union {
float x;
float r;
};
union {
float y;
float g;
};
union {
float z;
float b;
};
vec3() { }
vec3(float x, float y, float z) : x(x), y(y), z(z) { }
vec3(const vec3& value)
: x(value.x), y(value.y), z(value.z) { }
vec3 operator-() const
{
return vec3(-x, -y, -z);
}
vec3 operator-(const vec3& value) const
{
return vec3(this->x - value.x, this->y - value.y, this->z - value.z);
}
vec3 operator/(float divisor) const
{
return vec3(x / divisor, y / divisor, z / divisor);
}
vec3& operator/=(float divisor)
{
x /= divisor;
y /= divisor;
z /= divisor;
return *this;
}
vec3& operator=(const vec3& value)
{
x = value.x;
y = value.y;
z = value.z;
return *this;
}
float operator[](int i) const
{
switch (i) {
case 0: return x;
case 1: return y;
case 2: return z;
default:
assert(false);
return z;
}
}
float& operator[](int i)
{
switch (i) {
case 0: return x;
case 1: return y;
case 2: return z;
default:
assert(false);
return z;
}
}
vec3 cross(const vec3& value) const
{
return vec3::cross(*this, value);
}
float dot(const vec3& vec) const
{
return vec3::dot(*this, vec);
}
float length() const
{
return sqrt(dot(*this));
}
vec3& normalize()
{
float length = this->length();
return (length > 0.0f ? *this /= length : *this);
}
static vec3 cross(const vec3& a, const vec3& b)
{
return vec3(a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x);
}
static float dot(const vec3& a, const vec3& b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
static vec3 normalize (const vec3& input)
{
float length = input.length();
return (length > 0.0f ? input / length : input);
}
};
struct vec4 {
union {
float x;
float r;
};
union {
float y;
float g;
};
union {
float z;
float b;
};
union {
float w;
float a;
};
vec4() { }
vec4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) { }
vec4(const vec3& value, float w)
: x(value.x), y(value.y), z(value.z), w(w) { }
vec4(const vec4& value)
: x(value.x), y(value.y), z(value.z), w(value.w) { }
vec4 operator/(float divisor) const
{
return vec4(x / divisor, y / divisor, z / divisor, w / divisor);
}
vec4& operator/=(float divisor)
{
x /= divisor;
y /= divisor;
z /= divisor;
w /= divisor;
return *this;
}
vec4 operator*(float multiplicand) const
{
return vec4(x * multiplicand, y * multiplicand, z * multiplicand,
w * multiplicand);
}
vec4& operator=(const vec4& value)
{
x = value.x;
y = value.y;
z = value.z;
w = value.w;
return *this;
}
float operator[](int i) const
{
switch (i) {
case 0: return x;
case 1: return y;
case 2: return z;
case 3: return w;
default:
assert(false);
return z;
}
}
float& operator[](int i)
{
switch (i) {
case 0: return x;
case 1: return y;
case 2: return z;
case 3: return w;
default:
assert(false);
return z;
}
}
float dot(const vec4& vec) const
{
return vec4::dot(*this, vec);
}
float length() const
{
return sqrt(dot(*this));
}
static float dot(const vec4& a, const vec4& b)
{
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
}
static vec4 normalize (const vec4& input)
{
float length = input.length();
return (length > 0.0f ? input / length : input);
}
};
struct mat3 {
vec3 m[3];
mat3()
{
m[0] = vec3(1.0f, 0.0f, 0.0f);
m[1] = vec3(0.0f, 1.0f, 0.0f);
m[2] = vec3(0.0f, 0.0f, 1.0f);
}
mat3(const vec3& value1, const vec3& value2, const vec3& value3)
{
m[0] = value1;
m[1] = value2;
m[2] = value3;
}
mat3 transpose() const
{
return mat3(vec3(m[0].x, m[1].x, m[2].x),
vec3(m[0].y, m[1].y, m[2].y),
vec3(m[0].z, m[1].z, m[2].z));
}
};
struct mat4 {
vec4 m[4];
mat4()
{
m[0] = vec4(1.0f, 0.0f, 0.0f, 0.0f);
m[1] = vec4(0.0f, 1.0f, 0.0f, 0.0f);
m[2] = vec4(0.0f, 0.0f, 1.0f, 0.0f);
m[3] = vec4(0.0f, 0.0f, 0.0f, 1.0f);
}
mat4(const vec4& value1, const vec4& value2,
const vec4& value3, const vec4& value4)
{
m[0] = value1;
m[1] = value2;
m[2] = value3;
m[3] = value4;
}
mat4 operator*(float value) const
{
return mat4(m[0] * value, m[1] * value, m[2] * value, m[3] * value);
}
mat4 operator*(const mat4& value) const
{
mat4 right = value.transpose();
return mat4(
vec4(m[0].dot(right.m[0]), m[0].dot(right.m[1]),
m[0].dot(right.m[2]), m[0].dot(right.m[3])),
vec4(m[1].dot(right.m[0]), m[1].dot(right.m[1]),
m[1].dot(right.m[2]), m[1].dot(right.m[3])),
vec4(m[2].dot(right.m[0]), m[2].dot(right.m[1]),
m[2].dot(right.m[2]), m[2].dot(right.m[3])),
vec4(m[3].dot(right.m[0]), m[3].dot(right.m[1]),
m[3].dot(right.m[2]), m[3].dot(right.m[3]))
);
}
vec4 operator*(const vec4& value) const
{
return vec4(m[0].dot(value), m[1].dot(value),
m[2].dot(value), m[3].dot(value));
}
vec4& operator[](int i)
{
return m[i];
}
mat4 transpose() const
{
return mat4(vec4(m[0][0], m[1][0], m[2][0], m[3][0]),
vec4(m[0][1], m[1][1], m[2][1], m[3][1]),
vec4(m[0][2], m[1][2], m[2][2], m[3][2]),
vec4(m[0][3], m[1][3], m[2][3], m[3][3]));
}
static mat4 translate(const vec3& trans)
{
return mat4(vec4(1.0f, 0.0f, 0.0f, trans.x),
vec4(0.0f, 1.0f, 0.0f, trans.y),
vec4(0.0f, 0.0f, 1.0f, trans.z),
vec4(0.0f, 0.0f, 0.0f, 1.0f));
}
static mat4 translate(float x, float y, float z)
{
return mat4(vec4(1.0f, 0.0f, 0.0f, x),
vec4(0.0f, 1.0f, 0.0f, y),
vec4(0.0f, 0.0f, 1.0f, z),
vec4(0.0f, 0.0f, 0.0f, 1.0f));
}
static mat4 scale(const vec3& scale)
{
return mat4(vec4(scale.x, 0.0f, 0.0f, 0.0f),
vec4(0.0f, scale.y, 0.0f, 0.0f),
vec4(0.0f, 0.0f, scale.z, 0.0f),
vec4(0.0f, 0.0f, 0.0f, 1.0f));
}
static mat4 scale(float x, float y, float z)
{
return mat4(vec4(x, 0.0f, 0.0f, 0.0f),
vec4(0.0f, y, 0.0f, 0.0f),
vec4(0.0f, 0.0f, z, 0.0f),
vec4(0.0f, 0.0f, 0.0f, 1.0f));
}
static mat4 frustum(float left, float right, float bottom, float top,
float zNear, float zFar)
{
return mat4(
vec4(2.0f * zNear / (right - left), 0.0f,
(right + left) / (right - left), 0.0f),
vec4(0.0f, 2.0f * zNear / (top - bottom),
(top + bottom) / (top - bottom), 0.0f),
vec4(0.0f, 0.0f, (zFar + zNear) / (zNear - zFar),
2.0f * zFar * zNear / (zNear - zFar)),
vec4(0.0f, 0.0f, -1.0f, 0.0f)
);
}
static mat4 ortho(float left, float right, float bottom, float top,
float zNear, float zFar)
{
return mat4(
vec4(2.0f / (right - left), 0.0f, 0.0f,
(right + left) / (left - right)),
vec4(0.0f, 2.0f / (top - bottom), 0.0f,
(top + bottom) / (bottom - top)),
vec4(0.0f, 0.0f, 2.0f / (zNear - zFar),
(zFar + zNear) / (zNear - zFar)),
vec4(0.0f, 0.0f, 0.0f, 1.0f)
);
}
static mat4 lookAt(const vec3& eye, const vec3& center, const vec3& up)
{
vec3 const forward(vec3::normalize(center - eye));
vec3 const side(vec3::normalize(vec3::cross(forward, up)));
vec3 const u(vec3::cross(side, forward));
mat4 result;
result[0][0] = side.x;
result[1][0] = side.y;
result[2][0] = side.z;
result[0][1] = u.x;
result[1][1] = u.y;
result[2][1] = u.z;
result[0][2] =-forward.x;
result[1][2] =-forward.y;
result[2][2] =-forward.z;
result[3][0] =-vec3::dot(side, eye);
result[3][1] =-vec3::dot(u, eye);
result[3][2] = vec3::dot(forward, eye);
return result;
}
static mat4 lookAt(float eyeX, float eyeY, float eyeZ,
float centerX, float centerY, float centerZ,
float upX, float upY, float upZ)
{
return mat4::lookAt(vec3(eyeX, eyeY, eyeZ),
vec3(centerX, centerY, centerZ),
vec3(upX, upY, upZ));
}
static mat4 perspective(float fovY, float aspect, float zNear, float zFar)
{
float scaleY = 1.0f / tan(fovY * 3.1415962f / 360.0f);
return mat4(
vec4(scaleY / aspect, 0.0f, 0.0f, 0.0f),
vec4(0.0f, scaleY, 0.0f, 0.0f),
vec4(0.0f, 0.0f, (zFar + zNear) / (zNear - zFar),
(2.0f * zFar * zNear) / (zNear - zFar)),
vec4(0.0f, 0.0f, -1.0f, 0.0f)
);
}
};
#endif /* VECMATH_9B7E1CFE-346D-11E6-AFA2-D7DC87495A69_H */