1 #ifndef GRAVITY_H
2 #define GRAVITY_H
3 
4 #include <thread>
5 #include <mutex>
6 #include <condition_variable>
7 #include "Config.h"
8 
9 #ifdef GRAVFFT
10 #include <fftw3.h>
11 #endif
12 
13 class Simulation;
14 
15 class Gravity
16 {
17 private:
18 
19 	bool enabled = false;
20 
21 	// Maps to be processed by the gravity thread
22 	float *th_ogravmap = nullptr;
23 	float *th_gravmap = nullptr;
24 	float *th_gravx = nullptr;
25 	float *th_gravy = nullptr;
26 	float *th_gravp = nullptr;
27 
28 	int th_gravchanged = 0;
29 
30 	std::thread gravthread;
31 	std::mutex gravmutex;
32 	std::condition_variable gravcv;
33 	int grav_ready = 0;
34 	int gravthread_done = 0;
35 	bool ignoreNextResult = false;
36 
37 #ifdef GRAVFFT
38 	bool grav_fft_status = false;
39 	float *th_ptgravx = nullptr;
40 	float *th_ptgravy = nullptr;
41 	float *th_gravmapbig = nullptr;
42 	float *th_gravxbig = nullptr;
43 	float *th_gravybig = nullptr;
44 
45 	fftwf_complex *th_ptgravxt, *th_ptgravyt, *th_gravmapbigt, *th_gravxbigt, *th_gravybigt;
46 	fftwf_plan plan_gravmap, plan_gravx_inverse, plan_gravy_inverse;
47 #endif
48 
49 	struct mask_el {
50 		char *shape;
51 		char shapeout;
52 		mask_el *next;
53 	};
54 	using mask_el = struct mask_el;
55 
56 	bool grav_mask_r(int x, int y, char checkmap[YRES/CELL][XRES/CELL], char shape[YRES/CELL][XRES/CELL]);
57 	void mask_free(mask_el *c_mask_el);
58 
59 	void update_grav();
60 	void update_grav_async();
61 
62 
63 #ifdef GRAVFFT
64 	void grav_fft_init();
65 	void grav_fft_cleanup();
66 #endif
67 
68 public:
69 	//Maps to be used by the main thread
70 	float *gravmap = nullptr;
71 	float *gravp = nullptr;
72 	float *gravy = nullptr;
73 	float *gravx = nullptr;
74 	unsigned *gravmask = nullptr;
75 
76 	unsigned char (*bmap)[XRES/CELL];
77 
IsEnabled()78 	bool IsEnabled() { return enabled; }
79 
80 	void Clear();
81 
82 	void gravity_update_async();
83 
84 	void start_grav_async();
85 	void stop_grav_async();
86 	void gravity_mask();
87 
88 	Gravity();
89 	~Gravity();
90 };
91 
92 #endif
93