]> git.xonotic.org Git - xonotic/darkplaces.git/blob - r_modules.c
Optimize VectorNormalize macros by not doing the sqrt on 0 length, this also means...
[xonotic/darkplaces.git] / r_modules.c
1
2 #include "quakedef.h"
3
4 #define MAXRENDERMODULES 20
5
6 typedef struct rendermodule_s
7 {
8         int active; // set by start, cleared by shutdown
9         const char *name;
10         void(*start)(void);
11         void(*shutdown)(void);
12         void(*newmap)(void);
13         void(*devicelost)(void);
14         void(*devicerestored)(void);
15 }
16 rendermodule_t;
17
18 rendermodule_t rendermodule[MAXRENDERMODULES];
19
20 void R_Modules_Init(void)
21 {
22         Cmd_AddCommand(CF_CLIENT, "r_restart", R_Modules_Restart_f, "restarts renderer");
23 }
24
25 void R_RegisterModule(const char *name, void(*start)(void), void(*shutdown)(void), void(*newmap)(void), void(*devicelost)(void), void(*devicerestored)(void))
26 {
27         int i;
28         for (i = 0;i < MAXRENDERMODULES;i++)
29         {
30                 if (rendermodule[i].name == NULL)
31                         break;
32                 if (!strcmp(name, rendermodule[i].name))
33                 {
34                         Con_Printf("R_RegisterModule: module \"%s\" registered twice\n", name);
35                         return;
36                 }
37         }
38         if (i >= MAXRENDERMODULES)
39                 Sys_Error("R_RegisterModule: ran out of renderer module slots (%i)", MAXRENDERMODULES);
40         rendermodule[i].active = 0;
41         rendermodule[i].name = name;
42         rendermodule[i].start = start;
43         rendermodule[i].shutdown = shutdown;
44         rendermodule[i].newmap = newmap;
45         rendermodule[i].devicelost = devicelost;
46         rendermodule[i].devicerestored = devicerestored;
47 }
48
49 void R_Modules_Start(void)
50 {
51         int i;
52         for (i = 0;i < MAXRENDERMODULES;i++)
53         {
54                 if (rendermodule[i].name == NULL)
55                         continue;
56                 if (rendermodule[i].active)
57                 {
58                         Con_Printf ("R_Modules_Start: module \"%s\" already active\n", rendermodule[i].name);
59                         continue;
60                 }
61                 Con_DPrintf("Starting render module \"%s\"\n", rendermodule[i].name);
62                 rendermodule[i].active = 1;
63                 rendermodule[i].start();
64         }
65 }
66
67 void R_Modules_Shutdown(void)
68 {
69         int i;
70         // shutdown in reverse
71         for (i = MAXRENDERMODULES - 1;i >= 0;i--)
72         {
73                 if (rendermodule[i].name == NULL)
74                         continue;
75                 if (!rendermodule[i].active)
76                         continue;
77                 Con_DPrintf("Stopping render module \"%s\"\n", rendermodule[i].name);
78                 rendermodule[i].active = 0;
79                 rendermodule[i].shutdown();
80         }
81 }
82
83 void R_Modules_Restart_f(cmd_state_t *cmd)
84 {
85         CL_StartVideo();
86         Con_Print("Restarting renderer\n");
87         R_Modules_Shutdown();
88         R_Modules_Start();
89 }
90
91 void R_Modules_NewMap(void)
92 {
93         int i;
94         R_SkinFrame_PrepareForPurge();
95         for (i = 0;i < MAXRENDERMODULES;i++)
96         {
97                 if (rendermodule[i].name == NULL)
98                         continue;
99                 if (!rendermodule[i].active)
100                         continue;
101                 rendermodule[i].newmap();
102         }
103         R_SkinFrame_Purge();
104 }
105
106 void R_Modules_DeviceLost(void)
107 {
108         int i;
109         for (i = 0;i < MAXRENDERMODULES;i++)
110         {
111                 if (rendermodule[i].name == NULL)
112                         continue;
113                 if (!rendermodule[i].active)
114                         continue;
115                 if (!rendermodule[i].devicelost)
116                         continue;
117                 rendermodule[i].devicelost();
118         }
119 }
120
121
122 void R_Modules_DeviceRestored(void)
123 {
124         int i;
125         for (i = 0;i < MAXRENDERMODULES;i++)
126         {
127                 if (rendermodule[i].name == NULL)
128                         continue;
129                 if (!rendermodule[i].active)
130                         continue;
131                 if (!rendermodule[i].devicerestored)
132                         continue;
133                 rendermodule[i].devicerestored();
134         }
135 }
136