summaryrefslogtreecommitdiff
path: root/sources/main.c
blob: 4f899cea17927db5638ee16831f52a54d8d5b0e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/html5.h>
#endif

#include "raylib.h"
#include "s7/s7.h"
#include "rl/types.h"
#include "rl/text.h"
#include "rl/texture.h"
#include "rl/core.h"
#include "rl/enums.h"
#include "rl/shapes.h"
#include "repl/repl.h"

#include <math.h>
#include <stdio.h> 
#include <stdlib.h>

s7_scheme *s7;
s7_pointer s7_update_fn;
s7_pointer s7_draw_fn;

#ifdef __EMSCRIPTEN__
EM_BOOL main_loop_web(double time, void* userData) {

  s7_call(s7, s7_update_fn, s7_list(s7, 0));

  BeginDrawing();
  ClearBackground(BLUE);
  s7_call(s7, s7_draw_fn, s7_list(s7, 0));
  EndDrawing();
  PollInputEvents(); // <- this is necessary because of a web specific bug. See https://github.com/raysan5/raylib/issues/2379
  return EM_TRUE;
}
#else
void main_loop(){
  s7_call(s7, s7_update_fn, s7_list(s7, 0));
      
  BeginDrawing();
  ClearBackground(RAYWHITE);
  s7_call(s7, s7_draw_fn, s7_list(s7, 0));
  EndDrawing();
}
#endif


int main(int argc, char* argv[]) {
  s7 = s7_init();

  rl_register_types(s7);
  rl_register_enums(s7);
  rl_text_define_methods(s7);
  rl_texture_define_methods(s7);
  rl_core_define_methods(s7);
  rl_shapes_define_methods(s7);
  
  const int screen_width = 800;
  const int screen_height = 600;

  InitWindow(screen_width, screen_height, "SLGJ - 2024");
  SetTargetFPS(60);

  char filename[] = SCRIPTS_PATH"main.scm";

  s7_load(s7, filename);

  s7_update_fn = s7_name_to_value(s7, "update");
  s7_draw_fn = s7_name_to_value(s7, "draw");

#ifdef __EMSCRIPTEN__
  emscripten_request_animation_frame_loop(main_loop_web, 0);
#else
  while (!WindowShouldClose()) {
    main_loop();
  }
  
  CloseWindow();
#endif
  
  return 0;
}