Skip to content

Instantly share code, notes, and snippets.

@klange
Last active October 27, 2021 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klange/f6a87254bfab2b5356a794f8e6911266 to your computer and use it in GitHub Desktop.
Save klange/f6a87254bfab2b5356a794f8e6911266 to your computer and use it in GitHub Desktop.
#include "os.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <toaru/yutani.h>
#include <toaru/graphics.h>
#include <toaru/decorations.h>
#include <toaru/menu.h>
static yutani_t * yctx;
static yutani_window_t * window = NULL;
static gfx_context_t * ctx = NULL;
static char * window_title = NULL;
static void decors(void) {
render_decorations(window, ctx, window_title);
}
bool should_exit = false;
int main(int argc, char **argv) {
return our_main(argc, argv);
}
void os_create_window(const char *name, int width, int height) {
yctx = yutani_init();
init_decorations();
struct decor_bounds bounds;
decor_get_bounds(NULL, &bounds);
window = yutani_window_create(yctx, width + bounds.width, height + bounds.height);
ctx = init_graphics_yutani(window);
draw_fill(ctx, rgb(0,0,0));
window_title = name ? strdup(name) : "";
decors();
yutani_window_advertise(yctx, window, window_title);
}
uint32_t *my_colors;
uint16_t ftoi8(float f) { return (uint16_t) (f * 255); }
void os_create_colormap(const float *_rgb, int length) {
my_colors = malloc(length * sizeof(uint32_t));
for (int i = 0; i < length; i++) {
// Get color from list
float r = _rgb[i * 3 + 0];
float g = _rgb[i * 3 + 1];
float b = _rgb[i * 3 + 2];
my_colors[i] = rgb(ftoi8(r),ftoi8(g),ftoi8(b));
}
}
bool os_choose_bin(char* path, int pathLength) {
return false; // Not supported on Linux
}
bool os_should_exit(void) {
return should_exit; // Set in poll event
}
bool os_poll_event(struct event *ev) {
yutani_msg_t * m = yutani_poll_async(yctx);
while (m) {
if (menu_process_event(yctx, m)) {
decors();
yutani_flip(yctx, window);
}
switch (m->type) {
case YUTANI_MSG_KEY_EVENT:
{
struct yutani_msg_key_event * ke = (void*)m->data;
if (ke->event.action == KEY_ACTION_DOWN) {
ev->type = ET_KEYPRESS;
ev->kp_key = ke->event.keycode;
free(m);
return true;
}
}
break;
case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
{
struct yutani_msg_window_focus_change * wf = (void*)m->data;
yutani_window_t * win = hashmap_get(yctx->windows, (void*)(uintptr_t)wf->wid);
if (win && win == window) {
win->focused = wf->focused;
decors();
yutani_flip(yctx, window);
}
}
break;
case YUTANI_MSG_WINDOW_MOUSE_EVENT:
{
struct yutani_msg_window_mouse_event * me = (void*)m->data;
int result = decor_handle_event(yctx, m);
switch (result) {
case DECOR_CLOSE:
should_exit = true;
break;
case DECOR_RIGHT:
/* right click in decoration, show appropriate menu */
decor_show_default_menu(window, window->x + me->new_x, window->y + me->new_y);
break;
default:
/* Other actions */
break;
}
}
break;
case YUTANI_MSG_WINDOW_CLOSE:
case YUTANI_MSG_SESSION_END:
should_exit = true;
break;
default:
break;
}
free(m);
m = yutani_poll_async(yctx);
}
return false;
}
void os_draw_rect(int x, int y, int w, int h, const float* rgb, int color) {
struct decor_bounds bounds;
decor_get_bounds(window, &bounds);
draw_rectangle(ctx, x + bounds.left_width, y + bounds.top_height, w, h, my_colors[color]);
}
void os_present(void) {
yutani_flip(yctx, window);
}
void os_close() {
}
@klange
Copy link
Author

klange commented Oct 27, 2021

x86_64-pc-toaru-gcc -o 6502 -g -Os -Wall main.c toaru.c -ltoaru_decorations -ltoaru_menu -ltoaru_yutani -ltoaru_graphics -ltoaru_hashmap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment