diff --git a/client/src/game_state.cpp b/client/src/game_state.cpp index 9c937ff..7b59683 100644 --- a/client/src/game_state.cpp +++ b/client/src/game_state.cpp @@ -57,6 +57,8 @@ void GameState::init(int screen_width, int screen_height) { } void GameState::start_single_player_now(int screen_width, int screen_height) { + _screen_width = screen_width; + _screen_height = screen_height; fprintf(stdout, "Starting in single-player mode...\n"); std::thread server_thread(&GameState::_run_server, this); server_thread.detach(); diff --git a/client/src/main.cpp b/client/src/main.cpp index e257b37..f5082aa 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -106,7 +106,7 @@ int main(int argc, char** argv) { } /* Clear screen. */ - glClearColor(0.1f, 0.1f, 0.1, 1.0f); + glClearColor(0.04f, 0.05f, 0.06, 1.0f); glClear(GL_COLOR_BUFFER_BIT); game_state->render(shape_renderer_instance, txt_render_instance, SCREEN_HEIGHT, show_cursor); diff --git a/client/src/ui/desktop.cpp b/client/src/ui/desktop.cpp index a8c3640..4472150 100644 --- a/client/src/ui/desktop.cpp +++ b/client/src/ui/desktop.cpp @@ -1,5 +1,7 @@ #include #include +#include +#include #include #include "desktop.h" @@ -12,9 +14,35 @@ #include #include "ui/ui_window.h" +static const std::string& get_random_snippet(const std::vector& snippets) { + if(snippets.empty()) { + static const std::string err = "ERR"; + return err; + } + return snippets[rand() % snippets.size()]; +} + Desktop::Desktop(int screen_width, int screen_height) { -_taskbar = std::make_unique(screen_width, screen_height); + _taskbar = std::make_unique(screen_width, screen_height); _focused_window = nullptr; + + /* Load snippets for temp wallpaper. */ + std::ifstream snippet_file("assets/menu_background_snippets.txt"); + if(snippet_file.is_open()) { + std::string line; + while(std::getline(snippet_file, line)) { + if(!line.empty()) { _snippets.push_back(line); } + } + } + + /* Init animated background. */ + srand(time(NULL)); + for(int i = 0; i < 100; ++i) { + _background_text.push_back( + {get_random_snippet(_snippets), (float)(rand() % screen_width), + (float)(rand() % screen_height), + (float)(rand() % 30 + 10) / 100.0f}); + } } /* Desktop owns UIWindow, make sure we delete them. */ @@ -106,10 +134,17 @@ void Desktop::handle_event(SDL_Event* event, int screen_width, int screen_height void Desktop::update(void) { /* Remove closed windows. */ - _windows.erase( - std::remove_if(_windows.begin(), _windows.end(), - [](const std::unique_ptr& w) { return w->should_close(); }), - _windows.end()); + _windows.erase(std::remove_if(_windows.begin(), _windows.end(), + [this](const std::unique_ptr& w) { + if (w->should_close()) { + if (w.get() == _focused_window) { + _focused_window = nullptr; + } + return true; + } + return false; + }), + _windows.end()); } UIWindow* Desktop::get_focused_window(void) { @@ -118,7 +153,7 @@ UIWindow* Desktop::get_focused_window(void) { void Desktop::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, int screen_height, bool show_cursor) { - /* TODO: Render wallpaper here. */ + _render_wallpaper(txt_renderer); _taskbar->render(shape_renderer, txt_renderer, _windows, _focused_window); for(const auto& win : _windows) { if(!win->is_minimized()) { @@ -126,3 +161,18 @@ void Desktop::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, } } } + +void Desktop::_render_wallpaper(TextRenderer* txt_renderer) { + const Color wallpaper_color = { 0.0f, 0.15f, 0.08f }; + 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), + std::round(line.y), 1.0f, wallpaper_color); + } +} diff --git a/client/src/ui/desktop.h b/client/src/ui/desktop.h index 2418bf1..b3e5ec2 100644 --- a/client/src/ui/desktop.h +++ b/client/src/ui/desktop.h @@ -9,6 +9,14 @@ #include "ui/ui_window.h" #include "ui/taskbar.h" +/* Animated background stuff. */ +struct ScollingText { + std::string text; + float x; + float y; + float speed; +}; + class Desktop { public: Desktop(int screen_width, int screen_height); @@ -24,7 +32,11 @@ public: private: void _set_focused_window(UIWindow* window); + void _render_wallpaper(TextRenderer* txt_renderer); + std::vector> _windows; - std::unique_ptr _taskbar; - UIWindow* _focused_window; + std::unique_ptr _taskbar; + UIWindow* _focused_window; + std::vector _background_text; + std::vector _snippets; }; diff --git a/client/src/ui/main_menu.cpp b/client/src/ui/main_menu.cpp index 2c09b5f..9d98f5e 100644 --- a/client/src/ui/main_menu.cpp +++ b/client/src/ui/main_menu.cpp @@ -8,7 +8,7 @@ #include "main_menu.h" /* Get a random snippet form loaded word list. */ -const std::string& get_random_snippet(const std::vector& snippets) { +static const std::string& get_random_snippet(const std::vector& snippets) { /* If empty, return a default to avoid a crash? */ if(snippets.empty()) { static const std::string empty_str = "ERROR: snippets file not found"; diff --git a/client/src/ui/taskbar.cpp b/client/src/ui/taskbar.cpp index 9292d7f..724ca80 100644 --- a/client/src/ui/taskbar.cpp +++ b/client/src/ui/taskbar.cpp @@ -1,5 +1,10 @@ #include "taskbar.h" #include +#include +#include +#include +#include + #include "gfx/shape_renderer.h" #include "gfx/txt_renderer.h" #include "gfx/types.h" @@ -39,6 +44,16 @@ void Taskbar::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, txt_renderer->render_text(window->get_title().c_str(), x_offset + 10, _y_pos + 11, 1.0f, button_text_color); x_offset += button_width + padding; + + /* Draw clock. */ + auto now = std::chrono::system_clock::now(); + auto in_time_t = std::chrono::system_clock::to_time_t(now); + std::stringstream ss; + ss << std::put_time(std::localtime(&in_time_t), "%H:%M"); + std::string time_str = ss.str(); + + txt_renderer->render_text(time_str.c_str(), _width-50, _y_pos+11, 1.0f, + button_text_color); } } diff --git a/client/src/ui/ui_window.cpp b/client/src/ui/ui_window.cpp index f1479cc..b6bf0e6 100644 --- a/client/src/ui/ui_window.cpp +++ b/client/src/ui/ui_window.cpp @@ -151,7 +151,7 @@ void UIWindow::handle_event(SDL_Event* event, int screen_width, _state = WindowState::NORMAL; } else { _pre_maximize_rect = { _x, _y, _width, _height }; - _x = 0; _y = taskbar_height; + _x = 0; _y = 0; _width = screen_width; _height = screen_height - taskbar_height; _state = WindowState::MAXIMIZED; }