]> icculus.org git repositories - divverent/darkplaces.git/blob - r_modules.c
393 (+53)
[divverent/darkplaces.git] / r_modules.c
1
2 #include "quakedef.h"
3
4 #define MAXRENDERMODULES 64
5
6 typedef struct rendermodule_s
7 {
8         int active; // set by start, cleared by shutdown
9         char *name;
10         void(*start)(void);
11         void(*shutdown)(void);
12         void(*newmap)(void);
13 }
14 rendermodule_t;
15
16 rendermodule_t rendermodule[MAXRENDERMODULES];
17
18 void R_Modules_Init(void)
19 {
20         int i;
21         Cmd_AddCommand("r_restart", R_Modules_Restart);
22         for (i = 0;i < MAXRENDERMODULES;i++)
23                 rendermodule[i].name = NULL;
24 }
25
26 void R_RegisterModule(char *name, void(*start)(void), void(*shutdown)(void), void(*newmap)(void))
27 {
28         int i;
29         for (i = 0;i < MAXRENDERMODULES;i++)
30         {
31                 if (rendermodule[i].name == NULL)
32                         break;
33                 if (!strcmp(name, rendermodule[i].name))
34                         Sys_Error("R_RegisterModule: module \"%s\" registered twice\n", name);
35         }
36         if (i >= MAXRENDERMODULES)
37                 Sys_Error("R_RegisterModule: ran out of renderer module slots (%i)\n", MAXRENDERMODULES);
38         rendermodule[i].active = 0;
39         rendermodule[i].name = name;
40         rendermodule[i].start = start;
41         rendermodule[i].shutdown = shutdown;
42         rendermodule[i].newmap = newmap;
43 }
44
45 void R_Modules_Start(void)
46 {
47         int i;
48         for (i = 0;i < MAXRENDERMODULES;i++)
49         {
50                 if (rendermodule[i].name == NULL)
51                         continue;
52                 if (rendermodule[i].active)
53                         Sys_Error("R_StartModules: module \"%s\" already active\n", rendermodule[i].name);
54                 rendermodule[i].active = 1;
55                 rendermodule[i].start();
56         }
57 }
58
59 void R_Modules_Shutdown(void)
60 {
61         int i;
62         // shutdown in reverse
63         for (i = MAXRENDERMODULES - 1;i >= 0;i--)
64         {
65                 if (rendermodule[i].name == NULL)
66                         continue;
67                 if (!rendermodule[i].active)
68                         continue;
69                 rendermodule[i].active = 0;
70                 rendermodule[i].shutdown();
71         }
72 }
73
74 void R_Modules_Restart(void)
75 {
76         Con_Print("restarting renderer\n");
77         R_Modules_Shutdown();
78         R_Modules_Start();
79 }
80
81 void R_Modules_NewMap(void)
82 {
83         int i;
84         for (i = 0;i < MAXRENDERMODULES;i++)
85         {
86                 if (rendermodule[i].name == NULL)
87                         continue;
88                 if (!rendermodule[i].active)
89                         continue;
90                 rendermodule[i].newmap();
91         }
92 }
93