Skip to content

Commit

Permalink
Add window.setTitle (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
skejeton authored Jul 9, 2024
1 parent 53d0469 commit 764f47c
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 9 deletions.
33 changes: 33 additions & 0 deletions samples/window/main.um
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import (
"window.um"
"canvas.um"
"th.um"
"input.um"
"image.um"
)

i := 0
var icon: image::Image

fn init*() {
window::setup("Test window", 800, 600)

icon = image::load("../../etc/test.png").item0

window::onFrame.register({
canvas::drawText("Press space to change window title", {0, 0}, th::black, 2)
canvas::drawText("Press z to change window icon", {0, 10}, th::black, 2)
canvas::drawText("Press x to change window size", {0, 20}, th::black, 2)

if input::isJustPressed(.space) {
i++
window::setTitle(sprintf("New title %d", i))
}
if input::isJustPressed(.z) {
window::setIcon(icon)
}
if input::isJustPressed(.x) {
window::setDims({640, 480})
}
})
}
8 changes: 8 additions & 0 deletions src/bindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,13 @@ umth_window_get_fullscreen(UmkaStackSlot *p, UmkaStackSlot *r)
umkaGetResult(p, r)->uintVal = th_window_is_fullscreen();
}

// fn umth_window_set_title(title: str)
void
umth_window_set_title(UmkaStackSlot *p, UmkaStackSlot *r)
{
th_window_set_title(umkaGetParam(p, 0)->ptrVal);
}

// fn umth_window_set_dims(dm: th::Vf2)
void
umth_window_set_dims(UmkaStackSlot *p, UmkaStackSlot *r)
Expand Down Expand Up @@ -1466,6 +1473,7 @@ _th_umka_bind(void *umka)
umkaAddFunc(umka, "umth_window_setup", &umth_window_setup);
umkaAddFunc(umka, "umth_window_get_dimensions", &umth_window_get_dimensions);
umkaAddFunc(umka, "umth_window_set_viewport", &umth_window_set_viewport);
umkaAddFunc(umka, "umth_window_set_title", &umth_window_set_title);
umkaAddFunc(umka, "umth_window_set_dims", &umth_window_set_dims);
umkaAddFunc(umka, "umth_window_set_icon", &umth_window_set_icon);
umkaAddFunc(umka, "umth_window_show_cursor", &umth_window_show_cursor);
Expand Down
50 changes: 42 additions & 8 deletions src/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,50 @@ th_vector_normalize(float *x, float *y)
}
}

// implemented in umka_common.c
bool
moduleRegularizePath(const char *path, const char *curFolder, char *regularizedPath, int size);

th_err
void
th_regularize_path(const char *path, const char *cur_folder, char *regularized_path, int size)
{
if (!moduleRegularizePath(path, cur_folder, regularized_path, size)) {
return 1;
size_t o = 0;

// for now simply convert all backslashes to forward slashes and ignore `./`
for (size_t i = 0; cur_folder[i] && size; i++) {
if ((i == 0 || cur_folder[i-1] == '/' || cur_folder[i-1] == '\\') && cur_folder[i] == '.' &&
(cur_folder[i + 1] == '/' || cur_folder[i + 1] == '\\')) {
i++;
continue;
}

size--;
if (cur_folder[i] == '\\') {
regularized_path[o] = '/';
} else {
regularized_path[o] = cur_folder[i];
}
o++;
}

for (size_t i = 0; path[i] && size; i++) {
if ((i == 0 || path[i-1] == '/' || path[i-1] == '\\') && path[i] == '.' &&
(path[i + 1] == '/' || path[i + 1] == '\\')) {
i++;
continue;
}

size--;
if (path[i] == '\\') {
regularized_path[o] = '/';
} else {
regularized_path[o] = path[i];
}
o++;
}

if (size) {
regularized_path[o] = '\0';
printf("regularized_path: %s\n", regularized_path);
return;
}

return 0;
th_error("Path too long: %s%s", cur_folder, path);
exit(-1);
}
18 changes: 18 additions & 0 deletions src/staembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -1748,7 +1748,17 @@ const char *th_em_modulesrc[] = {
"\tumth_window_set_target_fps(fps)\n"
"}\n"
"\n"
"fn umth_window_set_title(title: str)\n"
"\n"
"//~~fn setTitle\n"
"// Sets the title of the window.\n"
"fn setTitle*(title: str) {\n"
"//~~\n"
"\tumth_window_set_title(title)\n"
"}\n"
"\n"
"fn umth_window_set_dims(dm: th::Vf2)\n"
"\n"
"//~~fn setDims\n"
"// Sets the dimensions of the window.\n"
"//\n"
Expand Down Expand Up @@ -5272,6 +5282,14 @@ const char *th_em_moduledocs[] = {
"\n"
"---------\n"
"\n"
"fn setTitle\n"
"\n"
"fn setTitle*(title: str) {\n"
"\n"
"Sets the title of the window.\n"
"\n"
"---------\n"
"\n"
"fn setDims\n"
"\n"
"fn setDims*(dm: th::Vf2) {\n"
Expand Down
16 changes: 16 additions & 0 deletions src/thextdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,19 @@ THEXT(void, th_tmap_autotile, uu *, uu *, uu, uu, uu *, uu);

THEXT(void, th_utf8_decode, uint32_t *, const char *);
THEXT(void, th_utf8_encode, char *, uint32_t);

THEXT(fu, th_window_dpi_scale);
THEXT(void, th_window_setup, char *title, int w, int h);
THEXT(void, th_window_get_dimensions, int *w, int *h);
THEXT(th_window_handle, th_get_window_handle);
THEXT(void, th_window_set_title, const char *title);
THEXT(void, th_window_set_dims, th_vf2 dm);
THEXT(void, th_window_set_icon, th_image *img);
THEXT(void, th_window_show_cursor, bool show);
THEXT(void, th_window_freeze_cursor, bool freeze);
THEXT(void, th_window_set_cursor, int cursor);
THEXT(void, th_window_request_exit);
THEXT(bool, th_window_is_fullscreen);
THEXT(void, th_window_set_fullscreen, bool fullscreen);
THEXT(sapp_desc, th_window_sapp_desc);

6 changes: 5 additions & 1 deletion src/tophat.h
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,8 @@ th_input_update_gamepads();
void
th_error(char *text, ...);
void
th_check_err(th_err err);
void
th_info(char *text, ...);
void
th_calculate_scaling(float camw, float camh);
Expand All @@ -535,7 +537,7 @@ void
th_deinit();
void
th_print_umka_error_and_quit(int code);
th_err
void
th_regularize_path(const char *path, const char *cur_folder, char *regularized_path, int size);

// navmesh
Expand Down Expand Up @@ -606,6 +608,8 @@ th_window_get_dimensions(int *w, int *h);
th_window_handle
th_get_window_handle();
void
th_window_set_title(const char *);
void
th_window_set_dims(th_vf2 dm);
void
th_window_set_icon(th_image *img);
Expand Down
6 changes: 6 additions & 0 deletions src/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,12 @@ w32_get_client_window_size(int *w, int *h)
*h = rect.bottom - rect.top;
}

void
th_window_set_title(const char *title)
{
sapp_set_window_title(title);
}

void
th_window_set_dims(th_vf2 dm)
{
Expand Down
10 changes: 10 additions & 0 deletions umka/window.um
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,17 @@ fn setTargetFps*(fps: int) {
umth_window_set_target_fps(fps)
}

fn umth_window_set_title(title: str)

//~~fn setTitle
// Sets the title of the window.
fn setTitle*(title: str) {
//~~
umth_window_set_title(title)
}

fn umth_window_set_dims(dm: th::Vf2)

//~~fn setDims
// Sets the dimensions of the window.
//
Expand Down

0 comments on commit 764f47c

Please sign in to comment.