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