[Add] Temp desktop background and clock widget.
This commit is contained in:
		
							parent
							
								
									0e7f97ca81
								
							
						
					
					
						commit
						996bf1c62c
					
				@ -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();
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,7 @@
 | 
			
		||||
#include <algorithm>
 | 
			
		||||
#include <ctime>
 | 
			
		||||
#include <fstream>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <memory>
 | 
			
		||||
 | 
			
		||||
#include "desktop.h"
 | 
			
		||||
@ -12,9 +14,35 @@
 | 
			
		||||
#include <ui/cursor_manager.h>
 | 
			
		||||
#include "ui/ui_window.h"
 | 
			
		||||
 | 
			
		||||
static const std::string& get_random_snippet(const std::vector<std::string>& 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<Taskbar>(screen_width, screen_height);
 | 
			
		||||
  _taskbar = std::make_unique<Taskbar>(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<UIWindow>& w) { return w->should_close(); }),
 | 
			
		||||
  _windows.end());
 | 
			
		||||
  _windows.erase(std::remove_if(_windows.begin(), _windows.end(),
 | 
			
		||||
                                [this](const std::unique_ptr<UIWindow>& 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);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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<std::unique_ptr<UIWindow>> _windows;
 | 
			
		||||
  std::unique_ptr<Taskbar> _taskbar;
 | 
			
		||||
  UIWindow* _focused_window;
 | 
			
		||||
  std::unique_ptr<Taskbar>  _taskbar;
 | 
			
		||||
  UIWindow*                 _focused_window;
 | 
			
		||||
  std::vector<ScollingText> _background_text;
 | 
			
		||||
  std::vector<std::string>  _snippets;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@ -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<std::string>& snippets) {
 | 
			
		||||
static const std::string& get_random_snippet(const std::vector<std::string>& snippets) {
 | 
			
		||||
  /* If empty, return a default to avoid a crash? */
 | 
			
		||||
  if(snippets.empty()) {
 | 
			
		||||
    static const std::string empty_str = "ERROR: snippets file not found";
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,10 @@
 | 
			
		||||
#include "taskbar.h"
 | 
			
		||||
#include <memory>
 | 
			
		||||
#include <chrono>
 | 
			
		||||
#include <ctime>
 | 
			
		||||
#include <iomanip>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
 | 
			
		||||
#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);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user