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