summaryrefslogtreecommitdiff
path: root/web/hitomezashi_web.c
diff options
context:
space:
mode:
authorArjun Satarkar <me@arjunsatarkar.net>2023-06-01 13:31:24 +0000
committerArjun Satarkar <me@arjunsatarkar.net>2023-06-01 13:33:56 +0000
commit3a2fef94230f35d948634ffef83660afdffa0bad (patch)
treee4fb261e6fcf6d0b4b3d4b688aa94757700f45ab /web/hitomezashi_web.c
parent316382d74ed4a705c43e2380e7af20a500c24fa9 (diff)
downloadhitomezashi-3a2fef94230f35d948634ffef83660afdffa0bad.tar
hitomezashi-3a2fef94230f35d948634ffef83660afdffa0bad.tar.gz
hitomezashi-3a2fef94230f35d948634ffef83660afdffa0bad.zip
Complete libhitomezashi, reset project
Diffstat (limited to 'web/hitomezashi_web.c')
-rw-r--r--web/hitomezashi_web.c113
1 files changed, 0 insertions, 113 deletions
diff --git a/web/hitomezashi_web.c b/web/hitomezashi_web.c
deleted file mode 100644
index 7e756ff..0000000
--- a/web/hitomezashi_web.c
+++ /dev/null
@@ -1,113 +0,0 @@
-#include "hitomezashi_web.h"
-#include "SDL2/SDL.h"
-#include "emscripten.h"
-#include "hitomezashi.h"
-#include "hitomezashi_utils.h"
-#include <assert.h>
-#include <stdbool.h>
-#include <stdlib.h>
-#include <string.h>
-
-SDL_Window *window;
-
-int EMSCRIPTEN_KEEPALIVE main(void) {
- if (SDL_Init(SDL_INIT_VIDEO) != 0)
- return EXIT_FAILURE;
-
- // Stop SDL from capturing all input and stopping input elements from working
- SDL_SetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT, "#canvas");
-
- if (!(window = SDL_CreateWindow("", 0, 0, 100, 100, SDL_WINDOW_RESIZABLE)))
- return EXIT_FAILURE;
-
- emscripten_set_main_loop(main_loop, -1, true);
-
- assert(0); // This should never be reached
-}
-
-void EMSCRIPTEN_KEEPALIVE main_loop(void) {
- const char *x_pattern_raw = hitomezashi_web_get_x_pattern();
- const char *y_pattern_raw = hitomezashi_web_get_y_pattern();
- const int x_pattern_len = strlen(x_pattern_raw);
- const int y_pattern_len = strlen(y_pattern_raw);
- const char *x_pattern =
- hitomezashi_ascii_binary_str_to_ints(x_pattern_raw, x_pattern_len);
- const char *y_pattern =
- hitomezashi_ascii_binary_str_to_ints(y_pattern_raw, y_pattern_len);
-
- const int gap = hitomezashi_web_get_gap();
- const int line_thickness = hitomezashi_web_get_line_thickness();
-
- const Uint32 fg_colour = hitomezashi_web_get_fg_colour();
- const Uint32 bg_colour = hitomezashi_web_get_bg_colour();
-
- // If input was valid (if any of these are 0 it was invalid)
- if (x_pattern_len && y_pattern_len && gap && line_thickness) {
- struct Hitomezashi_State state;
- hitomezashi_state_init(&state, x_pattern_len, y_pattern_len, x_pattern,
- y_pattern, gap, line_thickness, fg_colour,
- bg_colour);
- SDL_SetWindowSize(window, state.output_width, state.output_height);
- hitomezashi_draw(&state);
-
- SDL_BlitSurface(state.surface, NULL, SDL_GetWindowSurface(window), NULL);
- SDL_UpdateWindowSurface(window);
-
- SDL_FreeSurface(state.surface);
- }
-
- free((char *)x_pattern_raw);
- free((char *)y_pattern_raw);
- free((char *)x_pattern);
- free((char *)y_pattern);
-}
-
-EM_JS(char *, hitomezashi_web_get_x_pattern, (void), {
- let result_js = document.querySelector("input[name=\"x_pattern\"]").value;
- for (let c of result_js) {
- if (c != "0" && c != "1") {
- result_js = "";
- break;
- }
- }
- const result_length_bytes = lengthBytesUTF8(result_js) + 1;
- let result_wasm_heap = _malloc(result_length_bytes);
- stringToUTF8(result_js, result_wasm_heap, result_length_bytes);
- return result_wasm_heap;
-})
-
-EM_JS(char *, hitomezashi_web_get_y_pattern, (void), {
- let result_js = document.querySelector("input[name=\"y_pattern\"]").value;
- for (let c of result_js) {
- if (c != "0" && c != "1") {
- result_js = "";
- break;
- }
- }
- const result_length_bytes = lengthBytesUTF8(result_js) + 1;
- let result_wasm_heap = _malloc(result_length_bytes);
- stringToUTF8(result_js, result_wasm_heap, result_length_bytes);
- return result_wasm_heap;
-})
-
-EM_JS(unsigned int, hitomezashi_web_get_gap, (void), {
- const result = +document.querySelector("input[name=\"gap\"").value;
- return (result > 0 ? result : 0);
-})
-
-EM_JS(unsigned int, hitomezashi_web_get_line_thickness, (void), {
- const result = +document.querySelector("input[name=\"line_thickness\"").value;
- return (result > 0 ? result : 0);
-})
-
-EM_JS(Uint32, hitomezashi_web_get_fg_colour, (void), {
- const result = parseInt(
- document.querySelector("input[name=\"fg_colour\"]").value.slice(1), 16);
- return (result ? result : 0);
-})
-
-EM_JS(Uint32, hitomezashi_web_get_bg_colour, (void), {
- const result = parseInt(
- document.querySelector("input[name=\"bg_colour\"]").value.slice(1), 16);
- return (result ? result : 0);
-})