transparent-cube

Minimal cross-platform native/wasm graphics example.
git clone git://git.amin.space/transparent-cube.git
Log | Files | Refs | README | LICENSE

platform_wasm.c (1682B)


      1 #include "game.c"
      2 #include "platform.h"
      3 #include "platform_wasm.h"
      4 
      5 struct GameState g_game_state = {0};
      6 i32 g_width = PLATFORM_SCR_WIDTH;
      7 i32 g_height = PLATFORM_SCR_HEIGHT;
      8 char g_mem_buffer[1000] = {0};
      9 i32 g_mem_buffer_i = 0;
     10 u32 time = 0;
     11 
     12 bool init(void)
     13 {
     14     g_game_state.platform.platform_read_entire_file = &wasm_read_entire_file;
     15     g_game_state.platform.platform_print = &wasm_print;
     16     g_game_state.platform.platform_memory_free = &wasm_memory_free;
     17     game_init(&g_game_state, g_width, g_height);
     18     return true;
     19 }
     20 
     21 void render(void)
     22 {
     23     time += 16;
     24     game_update_and_render(&g_game_state, time/1000.0f, g_width, g_height);
     25 }
     26 
     27 void window_resize(int w, int h)
     28 {
     29     g_width = w;
     30     g_height = h;
     31     glViewport(0, 0, g_width, g_height);
     32 }
     33 
     34 i32 str_length(const char *str)
     35 {
     36     i32 i = 0;
     37     while(str[i] != '\0')
     38     {
     39         i++;
     40     }
     41     return i;
     42 }
     43 
     44 void *memset(void *s, int c, size_t n)
     45 {
     46     u8 *p = s;
     47     while(n > 0)
     48     {
     49         *p = (u8)c;
     50         p++;
     51         n--;
     52     }
     53     return s;
     54 }
     55 
     56 PLATFORM_READ_ENTIRE_FILE(wasm_read_entire_file)
     57 {
     58     g_mem_buffer_i++;
     59     char *buf = &g_mem_buffer[g_mem_buffer_i];
     60     if(js_read_entire_file((i32)file_path, str_length(file_path), buf))
     61     {
     62         i32 file_data_length = str_length(buf);
     63         g_mem_buffer_i += file_data_length;
     64         g_mem_buffer[g_mem_buffer_i] = '\0';
     65         wasm_print("Succeeded in reading file.");
     66     }
     67     else
     68     {
     69         wasm_print("Failed to read file.");
     70     }
     71     return buf;
     72 }
     73 
     74 PLATFORM_PRINT(wasm_print)
     75 {
     76     i32 len = str_length(format);
     77     js_print((i32)format, len);
     78     return 0;
     79 }
     80 
     81 PLATFORM_MEMORY_FREE(wasm_memory_free)
     82 {
     83     g_mem_buffer_i = 0;
     84 }