1#include <stdbool.h>
2#include <stdint.h>
3#include <stdlib.h>
4
5#include "maus.h"
6#include "maus_font.h"
7
8int mx, my;
9bool cur_visible = true;
10bool cur_locked = false;
11bool mb1_pressed = false;
12char txtbuf[4096] = {'\0'};
13int current_char = 0;
14bool resize = false;
15int rszw;
16int rszh;
17
18void handle_ev(Maus* mw, MausEvent* ev)
19{
20 switch (ev->type) {
21 case MAUS_EV_CLOSE:
22 maus_close(mw);
23 exit(EXIT_SUCCESS);
24 break;
25 case MAUS_EV_KEY: {
26 (void)0;
27
28 bool* keys = mw->key_syms;
29 if (keys[MAUS_KEY_CONTROL_L] && keys[MAUS_KEY_Q]) {
30 maus_close(mw);
31 exit(EXIT_SUCCESS);
32 };
33 if (keys[MAUS_KEY_CONTROL_L] && keys[MAUS_KEY_P]) {
34 cur_visible ?
35 maus_cur_set_mode(mw, MAUS_CURSOR_STATE_HIDDEN) :
36 maus_cur_set_mode(mw, MAUS_CURSOR_STATE_VISIBLE);
37
38 cur_visible = !cur_visible;
39 }
40 if (keys[MAUS_KEY_CONTROL_L] && keys[MAUS_KEY_L]) {
41 cur_locked ?
42 maus_cur_set_mode(mw, MAUS_CURSOR_STATE_FREE) :
43 maus_cur_set_mode(mw, MAUS_CURSOR_STATE_LOCKED);
44
45 cur_locked = !cur_locked;
46 }
47
48 if (ev->key.pressed == false)
49 break;
50
51 if (!((ev->key.key >= 32 && ev->key.key <= 126) || ev->key.key == '\n'))
52 break;
53
54 if (current_char + 1 < 4095 && current_char >= 0)
55 txtbuf[current_char++] = ev->key.text;
56 else
57 maus_log(stderr, "txtbuf full");
58
59 break;
60 }
61 case MAUS_EV_MOUSE_BUTTON: {
62 mb1_pressed =
63 mw->mouse_buttons[MAUS_MOUSE_BUTTON_LEFT] ?
64 true : false;
65 break;
66 }
67 case MAUS_EV_MOUSE_MOTION: {
68 mx = ev->mouse.motion.x;
69 my = ev->mouse.motion.y;
70
71 } break;
72 case MAUS_EV_RESIZE: {
73 resize = true;
74 rszw = ev->resize.width;
75 rszh = ev->resize.height;
76 } break;
77 case MAUS_EV_NONE:
78 break;
79 }
80}
81
82int main(void)
83{
84 Maus* mw = maus_init("basic window", 0, 0, 800, 600);
85 if (!mw)
86 return EXIT_FAILURE;
87 maus_create_window(mw);
88 MausFont* font = maus_font_load("assets/fonts/font1.bdf");
89 if (!font)
90 return EXIT_FAILURE;
91
92 MausEvent ev;
93 MausColor red = { 255, 255, 0, 0 };
94 /* unsigned long long ticks = 0; */
95 for (;;) {
96 while (maus_event_poll(mw, &ev))
97 (void) handle_ev(mw, &ev);
98
99 /* only resize window once per fram */
100 if (resize) {
101 maus_resize(mw, rszw, rszh);
102 resize = false;
103 }
104
105 maus_clear(mw, MAUS_COL_RGBA(255, 255, 255, 255));
106
107 uint32_t red_unpacked = MAUS_UNPACK_COL(red);
108 if (mx >= 0 && my >= 0 &&
109 mx < (int32_t)mw->width &&
110 my < (int32_t)mw->height && mb1_pressed) {
111 MAUS_PIXEL_AT(mw, mx, my) = red_unpacked;
112 }
113
114 for (uint32_t y = 0; y < mw->height/2; y++) {
115 for (uint32_t x = 0; x < mw->width/2; x++) {
116 MAUS_PIXEL_AT(mw, x, y) = red_unpacked;
117 }
118 }
119 red.b++;
120
121 /* maus_draw_text(mw, font, 700, 50, "maus font\nloading example", red); */
122
123 /* char buf[512] = {0}; */
124 /* snprintf(buf, 512, "maus' basic-window has been running for %lld ticks!", ticks); */
125 /* maus_clipboard_set_text(mw, buf); */
126
127 maus_draw_text(mw, font, 700, 50, txtbuf, red);
128
129 maus_target_fps(mw, 60);
130 maus_present(mw);
131 }
132 maus_font_free(font);
133 maus_close(mw);
134}
135