1 /**
2  * @file
3  */
4 
5 /*
6 Copyright(c) 1997-2001 Id Software, Inc.
7 Copyright(c) 2006 Quake2World.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or(at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23 
24 */
25 
26 #include "r_local.h"
27 #include "../../ports/system.h"
28 
29 #include <unistd.h>
30 #include <SDL_thread.h>
31 
32 renderer_threadstate_t r_threadstate;
33 
34 #define THREAD_SLEEP_INTERVAL 5
35 
R_RunThread(void * p)36 int R_RunThread (void* p)
37 {
38 	while (r_threads->integer) {
39 		if (!refdef.ready) {
40 			Sys_Sleep(THREAD_SLEEP_INTERVAL);
41 			continue;
42 		}
43 
44 		/* the renderer is up, so busy-wait for it */
45 		while (r_threadstate.state != THREAD_BSP)
46 			Sys_Sleep(0);
47 
48 		if (!r_threads->integer)
49 			break;
50 
51 		R_SetupFrustum();
52 
53 		/* draw brushes on current worldlevel */
54 		R_GetLevelSurfaceLists();
55 
56 		/** @todo - update per-model dynamic light list sorting here */
57 
58 		r_threadstate.state = THREAD_RENDERER;
59 	}
60 
61 	return 0;
62 }
63 
64 /**
65  * @sa R_InitThreads
66  */
R_ShutdownThreads(void)67 void R_ShutdownThreads (void)
68 {
69 	if (r_threadstate.thread) {
70 		const int old = r_threads->integer;
71 		r_threads->integer = 0;
72 		r_threadstate.state = THREAD_BSP;
73 		SDL_WaitThread(r_threadstate.thread, nullptr);
74 		r_threads->integer = old;
75 	}
76 
77 	r_threadstate.thread = nullptr;
78 }
79 
80 /**
81  * @sa R_ShutdownThreads
82  */
R_InitThreads(void)83 void R_InitThreads (void)
84 {
85 #if SDL_VERSION_ATLEAST(2,0,0)
86 	r_threadstate.thread = SDL_CreateThread(R_RunThread, "RendererThread", nullptr);
87 #else
88 	r_threadstate.thread = SDL_CreateThread(R_RunThread, nullptr);
89 #endif
90 }
91