summaryrefslogtreecommitdiff
path: root/applications/goldman/source/renderer.hpp
blob: c2e82b213928d72f87b5c2574036abf8682b0d66 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#pragma once

#include <daguerre/image.hpp>
#include "window.hpp"
#include <mutex>
#include <list>

class renderer {

  daguerre::image<daguerre::hilbert_color> framebuffer;
  daguerre::image<daguerre::hilbert_color> double_buffer;

  daguerre::image<daguerre::hilbert_color> background;
  daguerre::image<daguerre::hilbert_color> cursor;
  daguerre::hilbert_color cursor_background;

  int cursor_x;
  int cursor_y;

  std::mutex mut;

  euler::syscall::stream_handle
    dispatcher_handle_1, dispatcher_handle_2;

  void do_render();

public:
  //all of the shown windows, sorted from furthest back to furthest front.
  //while the renderer is not locked, the contents of this can change and
  //iterators can be invalided. this should not be reordered, added to, or
  //have elements removed from outside the renderer.
  std::list<window *> windows;

  renderer(
    daguerre::image<daguerre::hilbert_color> &&framebuffer,
    daguerre::image<daguerre::hilbert_color> &&background,
    daguerre::hilbert_color background_color,
    daguerre::image<daguerre::hilbert_color> &&cursor,
    daguerre::hilbert_color cursor_background);

  inline ~renderer() {
    euler::syscall::close_stream(dispatcher_handle_1);
    euler::syscall::close_stream(dispatcher_handle_2);
  }

  renderer(const renderer &) = delete;
  renderer &operator =(const renderer &) = delete;

  [[noreturn]] void render_thread_main();

  inline void lock()   { mut.lock();   }
  inline void unlock() { mut.unlock(); }

  inline void dispatch_render() {
    uint8_t byte = 0;
    euler::syscall::write_to_stream(dispatcher_handle_1, 1, &byte);
  }

  //gets the current position of the cursor. this should
  //only be called while the renderer is locked.
  void get_cursor(int &x_out, int &y_out);

  //this adds x_offset and y_offset to the current cursor position, and then
  //clamps the cursor position to have its top-left pixel inside the frame.
  //this should only be called while the renderer is locked.
  void bump_cursor(int x_offset, int y_offset);

  //moves the pointed to window to the front of the window stack.
  //this should only be called while the renderer is locked.
  void move_window_to_front(std::list<window *>::iterator w);

  void    add_window(window *w);
  void remove_window(window *w);

  inline bool is_top(window *w) {
    return windows.size() != 0 && w == windows.back();
  }

};