1 /*
2 * This software is licensed under the terms of the MIT License.
3 * See COPYING for further information.
4 * ---
5 * Copyright (c) 2011-2019, Lukas Weber <laochailan@web.de>.
6 * Copyright (c) 2012-2019, Andrei Alexeyev <akari@taisei-project.org>.
7 */
8
9 #include "taisei.h"
10
11 #include "backend.h"
12
13 #undef R
14 #define R(x) extern RendererBackend _r_backend_##x;
15 TAISEI_BUILDCONF_RENDERER_BACKENDS
16 #undef R
17
18 RendererBackend *_r_backends[] = {
19 #define R(x) &_r_backend_##x,
20 TAISEI_BUILDCONF_RENDERER_BACKENDS
21 #undef R
22 NULL,
23 };
24
25 RendererBackend _r_backend;
26
_r_set_backend(RendererBackend * backend)27 static void _r_set_backend(RendererBackend *backend) {
28 memcpy(&_r_backend, backend, sizeof(_r_backend));
29 }
30
_r_find_backend(const char * name)31 static RendererBackend* _r_find_backend(const char *name) {
32 for(RendererBackend **b = _r_backends; *b; ++b) {
33 if(!strcmp((*b)->name, name)) {
34 return *b;
35 }
36 }
37
38 log_fatal("Unknown renderer backend '%s'", name);
39 }
40
_r_backend_init(void)41 void _r_backend_init(void) {
42 static bool initialized;
43
44 if(initialized) {
45 return;
46 }
47
48 const char *backend = env_get("TAISEI_RENDERER", "");
49
50 if(!*backend) {
51 backend = TAISEI_BUILDCONF_RENDERER_DEFAULT;
52 }
53
54 RendererBackend *bptr = _r_find_backend(backend);
55 bptr->funcs.init();
56 _r_set_backend(bptr);
57
58 initialized = true;
59 }
60
_r_backend_inherit(RendererBackend * dest,const RendererBackend * base)61 void _r_backend_inherit(RendererBackend *dest, const RendererBackend *base) {
62 static const uint num_funcs = sizeof(dest->funcs)/sizeof(dest->funcs.init);
63 inherit_missing_pointers(num_funcs, (void**)&dest->funcs, (void**)&base->funcs);
64 }
65