summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2024-08-13 17:11:14 +0200
committerMitya Selivanov <automainint@guattari.tech>2024-08-13 17:11:14 +0200
commit7cc971f6b0011efea0f459948cf98ce2afed0cef (patch)
tree46deb41318cc0c73bae3b24b931e15462505de4b
parent90d96448ad8d6fd3a133ead74d61dcc52aa2de82 (diff)
downloadreduced_system_layer-7cc971f6b0011efea0f459948cf98ce2afed0cef.zip
Add Julia set example
-rwxr-xr-xexamples/julia_set.c66
-rwxr-xr-xreduced_system_layer.c51
2 files changed, 95 insertions, 22 deletions
diff --git a/examples/julia_set.c b/examples/julia_set.c
index 85a4674..9d12bb8 100755
--- a/examples/julia_set.c
+++ b/examples/julia_set.c
@@ -17,7 +17,7 @@ gcc \
-Wno-unused-but-set-variable \
-Wno-unused-parameter \
-Wno-overlength-strings \
- -O3 \
+ -g -O3 \
-fsanitize=undefined,address,leak -mshstk \
-lX11 -lm \
-o $BIN $SRC && \
@@ -39,10 +39,70 @@ i32 main(i32 argc, c8 **argv) {
p_init();
+ i64 p = 4;
+
+ f64 view_x = 0.;
+ f64 view_y = 0.;
+ f64 view_s = 1.;
+
+ f64 cx = -.8;
+ f64 cy = .1;
+ f64 radius = 2.;
+ i64 limit = 1024;
+
while (!platform.done) {
- p_handle_events();
+ p_wait_events();
+
+ if (platform.key_pressed['\n'])
+ p = (p == 1 ? 4 : 1);
+
+ if (platform.key_pressed[KEY_ESCAPE]) {
+ view_x = 0.;
+ view_y = 0.;
+ view_s = 1.;
+ }
+
+ f64 d = platform.key_down[MOD_CTRL] ? .0005 : .01;
+
+ if (platform.key_pressed['q']) cx -= d;
+ if (platform.key_pressed['w']) cx += d;
+ if (platform.key_pressed['a']) cy -= d;
+ if (platform.key_pressed['s']) cy += d;
+
+
+ if (platform.key_down[BUTTON_LEFT]) {
+ view_x += platform.cursor_dx * view_s;
+ view_y += platform.cursor_dy * view_s;
+ }
+
+ view_s += .1 * platform.wheel_dy * view_s;
+
+ for (i32 j = 0; j + p <= platform.frame_height; j += p)
+ for (i32 i = 0; i + p <= platform.frame_width; i += p) {
+ f64 x = .003 * ((i - platform.frame_width * .5) * view_s - view_x);
+ f64 y = .003 * ((j - platform.frame_height * .5) * view_s - view_y);
+
+ i64 n = 0;
+
+ for (; x * x + y * y < radius * radius && n < limit; ++n) {
+ f64 z = x * x - y * y;
+ y = 2. * x * y + cy;
+ x = z + cx;
+ }
+
+ u32 c;
+
+ if (n == limit)
+ c = 0;
+ else
+ c = 0xffffff - n * 8 - n * 256 * 4;
+
+ for (i32 jj = 0; jj < p; ++jj)
+ for (i32 ii = 0; ii < p; ++ii)
+ platform.pixels[(j + jj) * platform.frame_width + (i + ii)] = c;
+ }
+
p_render_frame();
- p_sleep_for(0);
}
p_cleanup();
diff --git a/reduced_system_layer.c b/reduced_system_layer.c
index decd327..ffcec97 100755
--- a/reduced_system_layer.c
+++ b/reduced_system_layer.c
@@ -198,8 +198,6 @@ typedef struct {
};
} IP_Address;
-typedef i64 (*Thread_Proc)(void *user_data);
-
// UTF-8
i32 utf8_size(c32 c);
c32 utf8_read(i64 len, c8 *s);
@@ -626,6 +624,7 @@ i64 p_send(u16 slot, IP_Address address, i64 size, u8 *data, u16 *local_port) {
#include <X11/Xatom.h>
static i16 _key_table[512] = {0};
+static b8 _key_repeat[512] = {0};
static u32 _buffer[MAX_NUM_PIXELS] = {0};
static Input_Key _input[MAX_INPUT_SIZE] = {0};
static c8 _clipboard_buffer[MAX_CLIPBOARD_SIZE] = {0};
@@ -757,6 +756,11 @@ void p_init(void) {
i32 _display_width = DisplayWidth (_display, screen);
i32 _display_height = DisplayHeight(_display, screen);
+ if (platform.frame_width <= 0)
+ platform.frame_width = 400;
+ if (platform.frame_height <= 0)
+ platform.frame_height = 300;
+
i32 x = (_display_width - platform.frame_width) / 2;
i32 y = (_display_height - platform.frame_height) / 2;
@@ -804,6 +808,9 @@ void p_init(void) {
XStoreName(_display, _window, platform.title);
XMapWindow(_display, _window);
+
+ XPutImage(_display, _window, _gc, &_image, 0, 0, 0, 0, platform.frame_width, platform.frame_height);
+ XFlush(_display);
}
void p_cleanup(void) {
@@ -825,6 +832,7 @@ i32 p_handle_events(void) {
i32 num_events = 0;
memset(platform.key_pressed, 0, sizeof platform.key_pressed);
+ memset(_key_repeat, 0, sizeof _key_repeat);
platform.input_size = 0;
platform.cursor_dx = 0;
@@ -858,16 +866,16 @@ i32 p_handle_events(void) {
platform.cursor_y = ev.xbutton.y;
switch (ev.xbutton.button) {
case Button1:
- platform.key_down[BUTTON_LEFT] = 1;
- ++platform.key_pressed[BUTTON_LEFT];
+ platform.key_down[BUTTON_LEFT] = 1;
+ platform.key_pressed[BUTTON_LEFT] = 1;
break;
case Button2:
- platform.key_down[BUTTON_MIDDLE] = 1;
- ++platform.key_pressed[BUTTON_MIDDLE];
+ platform.key_down[BUTTON_MIDDLE] = 1;
+ platform.key_pressed[BUTTON_MIDDLE] = 1;
break;
case Button3:
- platform.key_down[BUTTON_RIGHT] = 1;
- ++platform.key_pressed[BUTTON_RIGHT];
+ platform.key_down[BUTTON_RIGHT] = 1;
+ platform.key_pressed[BUTTON_RIGHT] = 1;
break;
case Button4: ++platform.wheel_dy; break;
case Button5: --platform.wheel_dy; break;
@@ -894,14 +902,18 @@ i32 p_handle_events(void) {
i16 k = _key_table[ev.xkey.keycode];
platform.cursor_x = ev.xkey.x;
platform.cursor_y = ev.xkey.y;
- platform.key_down [k] = 1;
- platform.key_pressed[k]++;
+
+ platform.key_down[k] = 1;
+ if (!_key_repeat[k])
+ platform.key_pressed[k] = 1;
+
platform.key_down[MOD_CTRL] = !!(ev.xkey.state & ControlMask);
platform.key_down[MOD_SHIFT] = !!(ev.xkey.state & ShiftMask);
platform.key_down[MOD_ALT] = !!(ev.xkey.state & Mod1Mask);
platform.key_down[MOD_CAPS] = !!(ev.xkey.state & LockMask);
platform.key_down[MOD_NUM] = !!(ev.xkey.state & Mod2Mask);
platform.key_down[MOD_SCROLL] = !!(ev.xkey.state & Mod3Mask);
+
if (platform.input_size < MAX_INPUT_SIZE) {
if (k < 32 || k >= 128)
platform.input[platform.input_size++] = (Input_Key) {
@@ -930,6 +942,7 @@ i32 p_handle_events(void) {
};
}
}
+
if (!requested_clipboard) {
XConvertSelection(_display, _clipboard, _targets, _clipboard, _window, CurrentTime);
requested_clipboard = 1;
@@ -938,10 +951,13 @@ i32 p_handle_events(void) {
case KeyRelease: {
i16 k = _key_table[ev.xkey.keycode];
+
platform.cursor_x = ev.xkey.x;
platform.cursor_y = ev.xkey.y;
- platform.key_down [k] = 0;
- platform.key_pressed[k]--;
+
+ platform.key_down[k] = 0;
+ _key_repeat[k] = 1;
+
platform.key_down[MOD_CTRL] = !!(ev.xkey.state & ControlMask);
platform.key_down[MOD_SHIFT] = !!(ev.xkey.state & ShiftMask);
platform.key_down[MOD_ALT] = !!(ev.xkey.state & Mod1Mask);
@@ -995,15 +1011,15 @@ i32 p_handle_events(void) {
case SelectionNotify:
if (ev.xselection.property != None) {
- i64 len;
- u8 *data;
+ i64 len = 0;
+ u8 *data = NULL;
XGetWindowProperty(
_display,
_window,
_clipboard,
0,
- MAX_CLIPBOARD_SIZE,
+ MAX_CLIPBOARD_SIZE / 4,
False,
AnyPropertyType,
&(Atom) {0},
@@ -1017,7 +1033,7 @@ i32 p_handle_events(void) {
_target = None;
Atom *list = (Atom *) data;
- for (i64 i = 0; i < len; i++)
+ for (i64 i = 0; i < len / 4; i++)
if (list[i] == XA_STRING)
_target = XA_STRING;
else if (list[i] == _utf8_string) {
@@ -1065,9 +1081,6 @@ i32 p_handle_events(void) {
}
}
- for (i64 k = 0; k < (i64) (sizeof platform.key_pressed / sizeof *platform.key_pressed); ++k)
- platform.key_pressed[k] = platform.key_pressed[k] > 0 ? 1 : 0;
-
XWindowAttributes attrs;
XGetWindowAttributes(_display, _window, &attrs);