aboutsummaryrefslogtreecommitdiff
path: root/src/script_sys.c
blob: 4c8732769be30d75a7411f4656deef0a2e9336d3 (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
#include "script_sys.h"
#include "texture_sys.h"
#include "shader_sys.h"

#include <libtcc.h>
#include <stddef.h>
#include <stdio.h>

#include <raylib.h>

static TCCState* state = NULL;

static void(*script_init)(void) = NULL;
static void(*script_update)(void) = NULL;
static void(*script_exit)(void) = NULL;

static i32 current_mod = -1;

static void restart(void)
{
	TCCState* new_state = tcc_new();
	tcc_set_output_type(new_state, TCC_OUTPUT_MEMORY);
	
	tcc_add_symbol(new_state, "load_new_tex", load_new_tex);
 	tcc_add_symbol(new_state, "load_new_shader", load_new_shader);
	
	tcc_add_symbol(new_state, "draw_text", DrawText);
	tcc_add_symbol(new_state, "set_active_shader", set_active_shader);
	tcc_add_symbol(new_state, "draw_texture", draw_texture);
	tcc_add_symbol(new_state, "reset_active_shader", reset_active_shader);
	
	if(tcc_add_file(new_state, "assets/src/main.c") == -1){
		TRACELOG(LOG_ERROR, "Compilation failed!\n");
		return;
	}
	
	if(state != NULL)
		deinit_script_sys();
	
	state = new_state;
	
	tcc_relocate(state, TCC_RELOCATE_AUTO);
	script_init = tcc_get_symbol(state, "init");
	script_update = tcc_get_symbol(state, "update");
	script_exit = tcc_get_symbol(state, "exit");
	script_init();
}

void init_script_sys(void)
{
	current_mod = GetFileModTime("assets/src/main.c");
	restart();
}

void update_script(void)
{
	i32 new_mod = GetFileModTime("assets/src/main.c");
            
	if(new_mod != current_mod){
		current_mod = new_mod;
		
		restart();
	}
	script_update();
}

void deinit_script_sys(void)
{
	script_exit();
	tcc_delete(state);
	unload_active_textures();
	unload_active_shaders();
}