summaryrefslogtreecommitdiff
path: root/applications/init/source/main.cpp
blob: 6aa85a416e99053e59e05a74fdec8b780aa26066 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <daguerre.hpp>

void overlay_encode(
  daguerre::hilbert_color &dest, const daguerre::rgb24 &src) {
  if (src.r != 0xff || src.g != 0x00 || src.b != 0xff)
    daguerre::encode(dest, src);
}

void invert(daguerre::rgb24 &dest, const daguerre::rgb24 &src) {
  dest.r = 255 - src.r;
  dest.g = 255 - src.g;
  dest.b = 255 - src.b;
}

int main(int, char **) {

  auto framebuffer = daguerre::get_hilbert_framebuffer();

  daguerre::image<daguerre::rgb24> burden;
  std::FILE *burden_file = std::fopen("/assets/burden.ppm", "r");
  daguerre::try_load_ppm(burden_file, burden);
  std::fclose(burden_file);

  daguerre::image<daguerre::rgb24> pointer;
  std::FILE *pointer_file = std::fopen("/assets/pointer.ppm", "r");
  daguerre::try_load_ppm(pointer_file, pointer);
  std::fclose(pointer_file);

  int32_t width = burden.width < framebuffer.width
                ? burden.width : framebuffer.width;
  int32_t height = burden.height < framebuffer.height
                 ? burden.height : framebuffer.height;
  unsigned x = (framebuffer.width - width) / 2;
  unsigned y = (framebuffer.height - height) / 2;

  int32_t new_mouse_x = width / 2;
  int32_t new_mouse_y = height / 2;
  int32_t old_mouse_x = new_mouse_x;
  int32_t old_mouse_y = new_mouse_y;
  bool was_left_mouse_down = false;

  daguerre::overlay_region<
    daguerre::hilbert_color, daguerre::rgb24, daguerre::encode>(
    framebuffer, x, y, burden, 0, 0, width, height);

  while (1) {

    __euler_mouse_buttons mouse_buttons;
    int16_t mouse_change_x, mouse_change_y;
    uint32_t key_packet;
    __euler_input_packet_type packet_type = __euler_get_input_packet(
      mouse_buttons, mouse_change_x, mouse_change_y, key_packet);

    bool anything_changed = false;
    bool should_invert = false;

    if (packet_type == __EULER_IPT_MOUSE) {

      if (mouse_change_x != 0 || mouse_change_y != 0) {

        old_mouse_x = new_mouse_x;
        old_mouse_y = new_mouse_y;
        new_mouse_x += mouse_change_x;
        new_mouse_y += mouse_change_y;

        if (new_mouse_x < 0)
          new_mouse_x = 0;
        else if ((unsigned)new_mouse_x > width - pointer.width)
          new_mouse_x = width - pointer.width;

        if (new_mouse_y < 0)
          new_mouse_y = 0;
        else if ((unsigned)new_mouse_y > height - pointer.height)
          new_mouse_y = height - pointer.height;

        anything_changed = true;

      }

      if (!was_left_mouse_down && (mouse_buttons & __EULER_MB_LEFT))
        should_invert = true;

      was_left_mouse_down = mouse_buttons & __EULER_MB_LEFT;

    }

    else if (packet_type == __EULER_IPT_KEYBOARD)
      if ((key_packet & 0x0400ff) == 0x00005a)
        should_invert = true;

    if (should_invert) {

      //this works with the current implementation of overlay_region, but
      //maybe it would be better to have a dedicated function for when the
      //two regions are exactly the same, given the comment on overlay_region.
      daguerre::overlay_region<daguerre::rgb24, daguerre::rgb24, invert>(
        burden, 0, 0, burden, 0, 0, width, height);

      daguerre::overlay_region<
        daguerre::hilbert_color, daguerre::rgb24, daguerre::encode>(
        framebuffer, x, y, burden, 0, 0, width, height);

      anything_changed = true;

    }

    if (anything_changed) {
      daguerre::overlay_region<
        daguerre::hilbert_color, daguerre::rgb24, daguerre::encode>(
        framebuffer, old_mouse_x + x, old_mouse_y + y, burden,
        old_mouse_x, old_mouse_y, pointer.width, pointer.height);
      daguerre::overlay_region<
        daguerre::hilbert_color, daguerre::rgb24, overlay_encode>(
        framebuffer, new_mouse_x + x, new_mouse_y + y,
        pointer, 0, 0, pointer.width, pointer.height);
    }

  }

}