[Change] Tried to fix stupid fucking animation jitter

This commit is contained in:
Ritchie Cunningham 2025-09-29 20:36:49 +01:00
parent 4a6d33292a
commit d34c13ef8b
3 changed files with 30 additions and 15 deletions

View File

@ -154,7 +154,11 @@ void GameState::update(void) {
} }
} }
if(_desktop) { if(_desktop) {
_desktop->update(); /*
* TODO: These fuck'in window dimensions just need to be global at this point!!
* Pass GameState by reference ?
*/
_desktop->update(_screen_width, _screen_height);
} }
break; break;
} }

View File

@ -1,4 +1,5 @@
#include <algorithm> #include <algorithm>
#include <chrono>
#include <ctime> #include <ctime>
#include <fstream> #include <fstream>
#include <cmath> #include <cmath>
@ -40,12 +41,14 @@ Desktop::Desktop(int screen_width, int screen_height, ClientNetwork* network) {
} }
/* Init animated background. */ /* Init animated background. */
srand(time(NULL));
for(int i = 0; i < 100; ++i) { for(int i = 0; i < 100; ++i) {
float y_pos = (float)(rand() % screen_height);
_background_text.push_back( _background_text.push_back(
{get_random_snippet(_snippets), (float)(rand() % screen_width), {get_random_snippet(_snippets), (float)(rand() % screen_width),
(float)(rand() % screen_height), y_pos,
(float)(rand() % 30 + 10) / 100.0f}); (float)(rand() % 30 + 10) / 100.0f,
(int)y_pos
});
} }
} }
@ -141,8 +144,9 @@ void Desktop::handle_event(SDL_Event* event, int screen_width, int screen_height
} }
} }
void Desktop::update(void) { void Desktop::update(int screen_width, int screen_height) {
/* Remove closed windows. */ /* Remove closed windows. */
_update_wallpaper(screen_width, screen_height);
_windows.erase(std::remove_if(_windows.begin(), _windows.end(), _windows.erase(std::remove_if(_windows.begin(), _windows.end(),
[this](const std::unique_ptr<UIWindow>& w) { [this](const std::unique_ptr<UIWindow>& w) {
if (w->should_close()) { if (w->should_close()) {
@ -182,14 +186,19 @@ void Desktop::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer,
void Desktop::_render_wallpaper(TextRenderer* txt_renderer) { void Desktop::_render_wallpaper(TextRenderer* txt_renderer) {
const Color wallpaper_color = { 0.0f, 0.15f, 0.08f }; const Color wallpaper_color = { 0.0f, 0.15f, 0.08f };
for(auto& line : _background_text) { for(auto& line : _background_text) {
line.y -= line.speed;
if(line.y < 0) {
line.y = 720; /* TODO: Use screen_height. */
line.x = (float)(rand() % 1280); /* TODO: Use screen_width. */
line.text = get_random_snippet(_snippets);
}
/* Round positions to nearest int to prevent jitter. */
txt_renderer->render_text(line.text.c_str(), std::round(line.x), txt_renderer->render_text(line.text.c_str(), std::round(line.x),
std::round(line.y), 1.0f, wallpaper_color); line.render_y, 1.0f, wallpaper_color);
}
}
void Desktop::_update_wallpaper(int screen_width, int screen_height) {
for(auto& line : _background_text) {
line.y_precise -= line.speed;
line.render_y = static_cast<int>(std::round(line.y_precise));
if(line.render_y < 0) {
line.y_precise = screen_height;
line.x = (float)(rand() % screen_width);
line.text = get_random_snippet(_snippets);
}
} }
} }

View File

@ -15,8 +15,9 @@
struct ScrollingText { struct ScrollingText {
std::string text; std::string text;
float x; float x;
float y; float y_precise; /* sub-pixel animation. */
float speed; float speed;
int render_y; /* For pixel-perfect rendering. */
}; };
class Desktop { class Desktop {
@ -26,7 +27,7 @@ public:
void add_window(std::unique_ptr<UIWindow> window); void add_window(std::unique_ptr<UIWindow> window);
void handle_event(SDL_Event* event, int screen_width, int screen_height); void handle_event(SDL_Event* event, int screen_width, int screen_height);
void update(void); void update(int screen_width, int screen_height);
void render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, int screen_height, void render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, int screen_height,
bool show_cursor); bool show_cursor);
@ -35,6 +36,7 @@ public:
private: private:
void _set_focused_window(UIWindow* window); void _set_focused_window(UIWindow* window);
void _render_wallpaper(TextRenderer* txt_renderer); void _render_wallpaper(TextRenderer* txt_renderer);
void _update_wallpaper(int screen_width, int screen_height);
std::vector<std::unique_ptr<UIWindow>> _windows; std::vector<std::unique_ptr<UIWindow>> _windows;
std::unique_ptr<Taskbar> _taskbar; std::unique_ptr<Taskbar> _taskbar;