Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/0kalekale/syskfb

fork of miniFB for sysk
https://github.com/0kalekale/syskfb

Last synced: 22 days ago
JSON representation

fork of miniFB for sysk

Awesome Lists containing this project

README

        

# syskFB (fork of MiniFB with just X11 and wayland support)

NOTE: dont bother with this, just use MiniFB, this only includes patches that i personally need, and may not keep up with upstream.
syskFB (sysk FrameBuffer) is a small cross platform library that makes it easy to render (32-bit) pixels in a window.

An example is the best way to show how it works:

```c
struct mfb_window *window = mfb_open_ex("my display", 800, 600, WF_RESIZABLE);
if (!window)
return 0;

buffer = (uint32_t *) malloc(800 * 600 * 4);

do {
int state;

// TODO: add some fancy rendering to the buffer of size 800 * 600

state = mfb_update_ex(window, buffer, 800, 600);

if (state < 0) {
window = NULL;
break;
}
} while(mfb_wait_sync(window));
```

1. First the application creates a **window** calling **mfb_open** or **mfb_open_ex**.
2. Next it's the application responsibility to allocate a buffer to work with.
3. Next calling **mfb_update** or **mfb_update_ex** the buffer will be copied over to the window and displayed. If this function return a value lower than 0 the window will have been destroyed internally and cannot be used anymore.
4. Last the code waits to synchronize with the monitor calling **mfb_wait_sync**.

Note that, by default, if ESC key is pressed **mfb_update** / **mfb_update_ex** will return -1 (and the window will have been destroyed internally).

See https://github.com/emoon/minifb/blob/master/tests/noise.c for a complete example.

# Supported Backends:

- X11
- Wayland (Linux) [there are some issues]

# Features:

- Window creation
- Callbacks to window events
- Get information from windows
- Add per window data
- Timers and target FPS
- C interface

## Callbacks to window events:

You can _add callbacks to the windows_:

```c
void active(struct mfb_window *window, bool isActive) {
...
}

void resize(struct mfb_window *window, int width, int height) {
...
// Optionally you can also change the viewport size
mfb_set_viewport(window, x, y, width, height);
// or let mfb caclculate the best fit from your original framebuffer size
mfb_set_viewport_best_fit(window, old_width, old_height);

}

bool close(struct mfb_window *window) {
...
return true; // true => confirm close
// false => don't close
}

void keyboard(struct mfb_window *window, mfb_key key, mfb_key_mod mod, bool isPressed) {
...
// Remember to close the window in some way
if(key == KB_KEY_ESCAPE) {
mfb_close(window);
}
}

void char_input(struct mfb_window *window, unsigned int charCode) {
...
}

void mouse_btn(struct mfb_window *window, mfb_mouse_button button, mfb_key_mod mod, bool isPressed) {
...
}

// Use wisely this event. It can be sent too often
void mouse_move(struct mfb_window *window, int x, int y) {
...
}

// Mouse wheel
void mouse_scroll(struct mfb_window *window, mfb_key_mod mod, float deltaX, float deltaY) {
...
}

int main(int argc, char argv[]) {

struct mfb_window *window = mfb_open_ex("my display", 800, 600, WF_RESIZABLE);
if (!window)
return 0;

mfb_set_active_callback(window, active);
mfb_set_resize_callback(window, resize);
mfb_set_close_callback(window, close);
mfb_set_keyboard_callback(window, keyboard);
mfb_set_char_input_callback(window, char_input);
mfb_set_mouse_button_callback(window, mouse_btn);
mfb_set_mouse_move_callback(window, mouse_move);
mfb_set_mouse_scroll_callback(window, mouse_scroll);

...
}
```
## Get information from windows (direct interface)

If you don't want to use callbacks, you can _get information about the window events directly_:

```c
bool mfb_is_window_active(struct mfb_window *window);

unsigned mfb_get_window_width(struct mfb_window *window);
unsigned mfb_get_window_height(struct mfb_window *window);

int mfb_get_mouse_x(struct mfb_window *window); // Last mouse pos X
int mfb_get_mouse_y(struct mfb_window *window); // Last mouse pos Y

float mfb_get_mouse_scroll_x(struct mfb_window *window); // Mouse wheel X as a sum. When you call this function it resets.
float mfb_get_mouse_scroll_y(struct mfb_window *window); // Mouse wheel Y as a sum. When you call this function it resets.

const uint8_t * mfb_get_mouse_button_buffer(struct mfb_window *window); // One byte for every button. Press (1), Release 0. (up to 8 buttons)

const uint8_t * mfb_get_key_buffer(struct mfb_window *window); // One byte for every key. Press (1), Release 0.
```

## Add per window data

Additionally you can _set per window data and recover it_:

```c
mfb_set_user_data(window, (void *) myData);
...
myData = (someCast *) mfb_get_user_data(window);
```

## Timers and target FPS

You can create timers for your own purposes.

```c
struct mfb_timer * mfb_timer_create();
void mfb_timer_destroy(struct mfb_timer *tmr);

void mfb_timer_reset(struct mfb_timer *tmr);
double mfb_timer_now(struct mfb_timer *tmr);
double mfb_timer_delta(struct mfb_timer *tmr);

double mfb_timer_get_frequency();
double mfb_timer_get_resolution();
```

Furthermore you can set (and get) a target fps for the application. The default is 60 frames per second.

```c
void mfb_set_target_fps(uint32_t fps);
unsigned mfb_get_target_fps();
```

This avoid the problem of update too fast the window collapsing the redrawing in fast processors.

Note: OpenGL and iOS have hardware support for syncing. Other systems will use software syncing. Including MacOS Metal.

In order to be able to use it you need to call the function:

```c
bool mfb_wait_sync(struct mfb_window *window);
```

Note that if you have several windows running on the same thread it makes no sense to wait them all...`