Skip to content

Commit

Permalink
lots of fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelDeboni committed Jan 29, 2024
1 parent 7e057b3 commit e33d06d
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 133 deletions.
Binary file modified examples/example
Binary file not shown.
251 changes: 125 additions & 126 deletions examples/example.jai
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ main :: () {
ok := Simp.texture_load_from_memory(*ui_icons, icon_data);
assert(ok);

init_fonts :: () { // Init fonts
pixel_height := 18;
font_data :: #run cast([]u8)read_entire_file("./default_font.ttf");
my_font = Simp.get_font_at_size(font_data, pixel_height);
assert(my_font != null);
}
init_fonts();

context.mu_ctx = New(Mu.Context);
context.mu_ctx.text_width = (font: Mu.Font, str: string) -> int {
text_width := Simp.prepare_text(cast(*Simp.Dynamic_Font)font, str);
return text_width;
};
context.mu_ctx.text_height = (font) => (cast(*Simp.Dynamic_Font)font).character_height + 2;
context.mu_ctx.style.font = my_font;
init_fonts :: () { // Init fonts
pixel_height := 18;
font_data :: #run cast([]u8)read_entire_file("./default_font.ttf");
my_font = Simp.get_font_at_size(font_data, pixel_height);
assert(my_font != null);
}
init_fonts();

context.mu_ctx = New(Mu.Context);
context.mu_ctx.text_width = (font: Mu.Font, str: string) -> int {
text_width := Simp.prepare_text(cast(*Simp.Dynamic_Font)font, str);
return text_width;
};
context.mu_ctx.text_height = (font) => (cast(*Simp.Dynamic_Font)font).character_height + 2;
context.mu_ctx.style.font = my_font;

quit := false;
want_screenshot := false;
Expand All @@ -58,8 +58,8 @@ main :: () {
}
}

mouse_x, mouse_y := get_mouse_pointer_position();
Mu.input_mousemove(mouse_x, mouse_y);
mouse_x, mouse_y := get_mouse_pointer_position();
Mu.input_mousemove(mouse_x, mouse_y);

for Input.events_this_frame {
if it.type == .QUIT then quit = true;
Expand All @@ -70,44 +70,44 @@ main :: () {
quit = true;
}

if it.key_code == {
case .MOUSE_BUTTON_RIGHT;
if it.key_pressed Mu.input_mousedown(mouse_x, mouse_y, .RIGHT);
else Mu.input_mouseup(mouse_x, mouse_y, .RIGHT);
case .MOUSE_BUTTON_LEFT;
if it.key_pressed Mu.input_mousedown(mouse_x, mouse_y, .LEFT);
else Mu.input_mouseup(mouse_x, mouse_y, .LEFT);
case .MOUSE_BUTTON_MIDDLE;
if it.key_pressed Mu.input_mousedown(mouse_x, mouse_y, .MIDDLE);
else Mu.input_mouseup(mouse_x, mouse_y, .MIDDLE);
case .SHIFT;
if it.key_pressed Mu.input_keydown(.SHIFT);
else Mu.input_keyup (.SHIFT);
case .CTRL;
if it.key_pressed Mu.input_keydown(.CTRL);
else Mu.input_keyup (.CTRL);
case .ALT;
if it.key_pressed Mu.input_keydown(.ALT);
else Mu.input_keyup (.ALT);
case .BACKSPACE;
if it.key_pressed Mu.input_keydown(.BACKSPACE);
else Mu.input_keyup (.BACKSPACE);
case .ENTER;
if it.key_pressed Mu.input_keydown(.RETURN);
else Mu.input_keyup (.RETURN);
}

case .TEXT_INPUT;
buf: [16]u8;
str: string;
str.data = buf.data;
str.count = 16;
if it.key_code == {
case .MOUSE_BUTTON_RIGHT;
if it.key_pressed Mu.input_mousedown(mouse_x, mouse_y, .RIGHT);
else Mu.input_mouseup(mouse_x, mouse_y, .RIGHT);
case .MOUSE_BUTTON_LEFT;
if it.key_pressed Mu.input_mousedown(mouse_x, mouse_y, .LEFT);
else Mu.input_mouseup(mouse_x, mouse_y, .LEFT);
case .MOUSE_BUTTON_MIDDLE;
if it.key_pressed Mu.input_mousedown(mouse_x, mouse_y, .MIDDLE);
else Mu.input_mouseup(mouse_x, mouse_y, .MIDDLE);
case .SHIFT;
if it.key_pressed Mu.input_keydown(.SHIFT);
else Mu.input_keyup (.SHIFT);
case .CTRL;
if it.key_pressed Mu.input_keydown(.CTRL);
else Mu.input_keyup (.CTRL);
case .ALT;
if it.key_pressed Mu.input_keydown(.ALT);
else Mu.input_keyup (.ALT);
case .BACKSPACE;
if it.key_pressed Mu.input_keydown(.BACKSPACE);
else Mu.input_keyup (.BACKSPACE);
case .ENTER;
if it.key_pressed Mu.input_keydown(.RETURN);
else Mu.input_keyup (.RETURN);
}

case .TEXT_INPUT;
buf: [16]u8;
str: string;
str.data = buf.data;
str.count = 16;
Unicode.character_utf32_to_utf8(it.utf32, *str);
Mu.input_text(str);
Mu.input_text(str);

case .MOUSE_WHEEL;
print("yay %\n", it);
Mu.input_scroll(0, -it.wheel_delta);
case .MOUSE_WHEEL;
print("yay %\n", it);
Mu.input_scroll(0, -it.wheel_delta);
}
}

Expand All @@ -118,76 +118,76 @@ main :: () {
}

draw_one_frame :: () {
// @TODO: The example is very imcomplete, maybe I should port the official demo...
{
Mu.begin();
test_window();
Mu.end();
}
// @TODO: The example is very imcomplete, maybe I should port the official demo...
{
Mu.begin();
test_window();
Mu.end();
}

Simp.clear_render_target(.15, .08, .08, 1);

time := get_time();

cmd: *Mu.Command;
while Mu.next_command(*cmd) {
if cmd.type == {
case .RECT;
r := cmd.rect.rect;
color := cmd.rect.color;
r.y = cast(s32)window_height - r.y;
c := Vector4.{
cast(float)color.r,
cast(float)color.g,
cast(float)color.b,
cast(float)color.a,
} / 255.0;

Simp.set_shader_for_color();
Simp.immediate_quad(
cast(float) r.x,
cast(float) r.y - r.h,
cast(float) r.x + r.w,
cast(float) r.y,
c,
);

case .TEXT;
font := cast(*Simp.Dynamic_Font)cmd.text.font;
Simp.set_shader_for_text();
text_width := Simp.prepare_text(font, cmd.text.str);
text_x := cast(s64)cmd.text.pos.x;
text_y := window_height - cast(s64)cmd.text.pos.y;
text_y -= font.character_height - font.y_offset_for_centering / 2;
Simp.draw_prepared_text(font, text_x, text_y, Vector4.{1,1,1,1});

case .CLIP;
Simp.immediate_flush();
if cmd.clip.rect.w > window_width && cmd.clip.rect.h > window_height {
Simp.clear_scissor();
} else {
y := window_height - cmd.clip.rect.y;
Simp.set_scissor(
cmd.clip.rect.x,
y - cmd.clip.rect.h,
cmd.clip.rect.x + cmd.clip.rect.w,
y,
);
}
time := seconds_since_init();

cmd: *Mu.Command;
while Mu.next_command(*cmd) {
if cmd.type == {
case .RECT;
r := cmd.rect.rect;
color := cmd.rect.color;
r.y = cast(s32)window_height - r.y;
c := Vector4.{
cast(float)color.r,
cast(float)color.g,
cast(float)color.b,
cast(float)color.a,
} / 255.0;

Simp.set_shader_for_color();
Simp.immediate_quad(
cast(float) r.x,
cast(float) r.y - r.h,
cast(float) r.x + r.w,
cast(float) r.y,
c,
);

case .TEXT;
font := cast(*Simp.Dynamic_Font)cmd.text.font;
Simp.set_shader_for_text();
text_width := Simp.prepare_text(font, cmd.text.str);
text_x := cast(s64)cmd.text.pos.x;
text_y := window_height - cast(s64)cmd.text.pos.y;
text_y -= font.character_height - font.y_offset_for_centering / 2;
Simp.draw_prepared_text(font, text_x, text_y, Vector4.{1,1,1,1});

case .CLIP;
Simp.immediate_flush();
if cmd.clip.rect.w > window_width && cmd.clip.rect.h > window_height {
Simp.clear_scissor();
} else {
y := window_height - cmd.clip.rect.y;
Simp.set_scissor(
cmd.clip.rect.x,
y - cmd.clip.rect.h,
cmd.clip.rect.x + cmd.clip.rect.w,
y,
);
}
case .ICON;
r := cmd.icon.rect;
color := cmd.icon.color;
r.y = cast(s32)window_height - r.y;
c := Vector4.{
cast(float)color.r,
cast(float)color.g,
cast(float)color.b,
r := cmd.icon.rect;
color := cmd.icon.color;
r.y = cast(s32)window_height - r.y;
c := Vector4.{
cast(float)color.r,
cast(float)color.g,
cast(float)color.b,
255,
} / 255.0;
} / 255.0;

Simp.set_shader_for_images(*ui_icons);
//Simp.set_shader_for_color();
//Simp.set_shader_for_color();

x_width :: 32.0 / 196.0;
x_offset := cast(float)cmd.icon.id / 6.0;
Expand All @@ -203,10 +203,10 @@ draw_one_frame :: () {
Vector2.{x_offset + x_width, 0},
Vector2.{x_offset, 0},
);
Simp.immediate_flush();
}
Simp.immediate_flush();
}

}
}

Simp.swap_buffers(my_window);
}
Expand All @@ -232,14 +232,13 @@ test_window :: () {

if Mu.header("Window Info", true) {
win := Mu.get_current_container();

Mu.column(.[85, -1], 0, #code {
Mu.label("Position:");
Mu.labelf("%d, %d", win.rect.x, win.rect.y);

Mu.label("Size:");
Mu.labelf("%d, %d", win.rect.w, win.rect.h);
});
Mu.layout_row(.[85, -1], 0);
Mu.label("Position:");
Mu.labelf("%d, %d", win.rect.x, win.rect.y);

Mu.label("Size:");
Mu.labelf("%d, %d", win.rect.w, win.rect.h);
}

Mu.number(*number_box_value, 1);
Expand Down
Loading

0 comments on commit e33d06d

Please sign in to comment.