Hi , I am beginner to SDL. My use case is need to build large table using SDL. and I’m looking for smooth scroll like browser does. Anyone have idea ? how to implement smooth scrolling in SDL ?. I tried with SDL_MouseWheel event . but it doesn’t look smooth.
Thanks
Smooth scrolling is achieved by interpolation: instead of directly setting the new position every time you scroll, you need to interpolate the current position until it reaches the target position, so instead of:
//...
case (event.type == SDL_MOUSEWHEEL) {
if(event.wheel.y > 0) {
table.y -= 10; // scroll up by 10px
}
else if(event.wheel.y < 0) {
table.y += 10; //scroll down by 10px
}
}
//...
You’d set something like table.target_y += 10
and each frame you add 1
to table.y
until table.y == table.target_y
You can use floats to make it less fast but the general idea is this: each frame you’ll have to interpolate.
Shameless self-promotion
I happen to have two interpolation libraries to handle things just like these, one of them is Curvy which is C-only and the other one is Tweeny for C++ so you can write things like
auto tween = tweeny::from((float) table.y)
.to(table.y + 10.0f)
.during(100);
//...
while (!SDL_QuitRequested()) {
table.y = tween.step();
}
//...
I hope it helps.