Add scroll wheel detection to SDL2 example

This commit is contained in:
Nic Barker 2024-12-28 19:08:49 +13:00
parent e758a9fd5c
commit 203d0081c1
2 changed files with 33 additions and 1 deletions

View File

@ -305,20 +305,39 @@ int main(void) {
int windowHeight = 0; int windowHeight = 0;
SDL_GetWindowSize(window, &windowWidth, &windowHeight); SDL_GetWindowSize(window, &windowWidth, &windowHeight);
Clay_Initialize(clayMemory, (Clay_Dimensions) { (float)windowWidth, (float)windowHeight }, (Clay_ErrorHandler) { HandleClayErrors }); Clay_Initialize(clayMemory, (Clay_Dimensions) { (float)windowWidth, (float)windowHeight }, (Clay_ErrorHandler) { HandleClayErrors });
Uint64 NOW = SDL_GetPerformanceCounter();
Uint64 LAST = 0;
double deltaTime = 0;
while (true) { while (true) {
Clay_Vector2 scrollDelta = {};
SDL_Event event; SDL_Event event;
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
switch (event.type) { switch (event.type) {
case SDL_QUIT: goto quit; case SDL_QUIT: { goto quit; }
case SDL_MOUSEWHEEL: {
scrollDelta.x = event.wheel.x;
scrollDelta.y = event.wheel.y;
break;
} }
} }
}
LAST = NOW;
NOW = SDL_GetPerformanceCounter();
deltaTime = (double)((NOW - LAST)*1000 / (double)SDL_GetPerformanceFrequency() );
int mouseX = 0; int mouseX = 0;
int mouseY = 0; int mouseY = 0;
Uint32 mouseState = SDL_GetMouseState(&mouseX, &mouseY); Uint32 mouseState = SDL_GetMouseState(&mouseX, &mouseY);
Clay_Vector2 mousePosition = (Clay_Vector2){ (float)mouseX, (float)mouseY }; Clay_Vector2 mousePosition = (Clay_Vector2){ (float)mouseX, (float)mouseY };
Clay_SetPointerState(mousePosition, mouseState & SDL_BUTTON(1)); Clay_SetPointerState(mousePosition, mouseState & SDL_BUTTON(1));
Clay_UpdateScrollContainers(
true,
(Clay_Vector2) { scrollDelta.x, scrollDelta.y },
deltaTime
);
SDL_GetWindowSize(window, &windowWidth, &windowHeight); SDL_GetWindowSize(window, &windowWidth, &windowHeight);
Clay_SetLayoutDimensions((Clay_Dimensions) { (float)windowWidth, (float)windowHeight }); Clay_SetLayoutDimensions((Clay_Dimensions) { (float)windowWidth, (float)windowHeight });

13
renderers/SDL2/README Normal file
View File

@ -0,0 +1,13 @@
Please note, the SDL2 renderer is not 100% feature complete. It is currently missing:
- Border rendering
- Image rendering
- Rounded rectangle corners
Note: on Mac OSX, SDL2 for some reason decides to automatically disable momentum scrolling on macbook trackpads.
You can re enable it in objective C using:
```C
[[NSUserDefaults standardUserDefaults] setBool: YES
forKey: @"AppleMomentumScrollSupported"];
```