1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9 
10 
11 
12 #ifndef _GRAPHICS_H
13 #define _GRAPHICS_H
14 
15 #include "globalincs/pstypes.h"
16 #include "graphics/tmapper.h"
17 #include "cfile/cfile.h"
18 #include "bmpman/bmpman.h"
19 
20 extern const float Default_min_draw_distance;
21 extern const float Default_max_draw_distance;
22 extern float Min_draw_distance;
23 extern float Max_draw_distance;
24 extern int Gr_inited;
25 
26 // z-buffering stuff
27 extern int gr_zbuffering, gr_zbuffering_mode;
28 extern int gr_global_zbuffering;
29 
30 // Shader flags
31 #define SDR_FLAG_LIGHT			(1<<0)
32 #define SDR_FLAG_FOG			(1<<1)
33 #define SDR_FLAG_DIFFUSE_MAP	(1<<2)
34 #define SDR_FLAG_GLOW_MAP		(1<<3)
35 #define SDR_FLAG_SPEC_MAP		(1<<4)
36 #define SDR_FLAG_NORMAL_MAP		(1<<5)
37 #define SDR_FLAG_HEIGHT_MAP		(1<<6)
38 #define SDR_FLAG_ENV_MAP		(1<<7)
39 #define SDR_FLAG_ANIMATED		(1<<8)
40 #define SDR_FLAG_SOFT_QUAD		(1<<9)
41 #define SDR_FLAG_DISTORTION		(1<<10)
42 #define SDR_FLAG_MISC_MAP		(1<<11)
43 #define SDR_FLAG_TEAMCOLOR		(1<<12)
44 #define SDR_FLAG_THRUSTER		(1<<13)
45 
46 // stencil buffering stuff
47 extern int gr_stencil_mode;
48 
49 // alpha test
50 extern int gr_alpha_test;
51 
52 /**
53  * This is a structure used by the shader to keep track
54  * of the values you want to use in the shade primitive.
55  */
56 typedef struct shader {
57 	uint	screen_sig;					// current mode this is in
58 	ubyte	r,g,b,c;						// factors and constant
59 	ubyte	lookup[256];
60 } shader;
61 
62 #define AC_TYPE_NONE		0		// Not an alphacolor
63 #define AC_TYPE_HUD		1		// Doesn't change hue depending on background.  Used for HUD stuff.
64 #define AC_TYPE_BLEND	2		// Changes hue depending on background.  Used for stars, etc.
65 
66 // NEVER REFERENCE THESE VALUES OUTSIDE OF THE GRAPHICS LIBRARY!!!
67 // If you need to get the rgb values of a "color" struct call
68 // gr_get_colors after calling gr_set_colors_fast.
69 typedef struct color {
70 	uint		screen_sig;
71 	ubyte		red;
72 	ubyte		green;
73 	ubyte		blue;
74 	ubyte		alpha;
75 	ubyte		ac_type;							// The type of alphacolor.  See AC_TYPE_??? defines
76 	int		is_alphacolor;
77 	ubyte		raw8;
78 	int		alphacolor;
79 	int		magic;
80 } color;
81 
82 // Used by the team coloring code
83 typedef struct team_color {
84 	struct {
85 		float r, g, b;
86 	} base;
87 	struct {
88 		float r, g, b;
89 	} stripe;
90 } team_color;
91 
92 
93 
94 typedef struct tsb_t {
95 	vec3d tangent;
96 	float scaler;
97 } tsb_t;
98 
99 /**
100  * This should be basicly just like it is in the VB
101  * a list of triangles and their associated normals
102  */
103 class poly_list
104 {
105 public:
poly_list()106 	poly_list(): n_verts(0), vert(NULL), norm(NULL), tsb(NULL), currently_allocated(0) {}
107 	~poly_list();
108 	poly_list& operator = (poly_list&);
109 
110 	void allocate(int size);
111 	void make_index_buffer(SCP_vector<int> &vertex_list);
112 	void calculate_tangent();
113 	int n_verts;
114 	vertex *vert;
115 	vec3d *norm;
116 	tsb_t *tsb;
117 
118 	int find_index(poly_list *plist, int idx);
119 
120 private:
121 	int currently_allocated;
122 	int find_first_vertex(int idx);
123 };
124 
125 
126 class colored_vector
127 {
128 public:
colored_vector()129 	colored_vector()
130 		: pad(1.0f)
131 	{}
132 
133 	vec3d vec;
134 	float pad;	//needed so I can just memcpy it in d3d
135 	ubyte color[4];
136 };
137 
138 
139 struct line_list {
140 	int n_line;
141 	vertex *vert;
142 };
143 
144 class buffer_data
145 {
146 public:
147 	int flags;
148 
149 	int texture;		// this is the texture the vertex buffer will use
150 	int n_verts;
151 
152 	size_t index_offset;
153 
get_index()154 	const uint *get_index() const
155 	{
156 		return index;
157 	}
158 
159         uint i_first, i_last;
160 
release()161 	void release()
162 	{
163 		if (index) {
164 			delete [] index;
165 			index = NULL;
166 		}
167 	}
168 
assign(int i,uint j)169 	void assign(int i, uint j)
170 	{
171 		const_cast<uint *>(index)[i] = j;
172 		if (i_first > i_last)
173 			i_first = i_last = j;
174 		else if (i_first > j)
175 			i_first = j;
176 		else if (i_last < j)
177 			i_last = j;
178 	}
179 
180 	// Constructor
buffer_data(int n_vrts)181 	buffer_data(int n_vrts) :
182 		flags(0), texture(-1), n_verts(n_vrts), index_offset(0),
183 		i_first(1), i_last(0)
184 	{
185 		index = new(std::nothrow) uint[n_verts];
186 	}
187 
188 	// Copy-constructor
buffer_data(const buffer_data & other)189 	buffer_data(const buffer_data& other)
190 	{
191 		index = new(std::nothrow) uint[other.n_verts];
192 		for (size_t i=0; i < (size_t) other.n_verts; i++)
193 		{
194 			index[i] = other.index[i];
195 		}
196 
197 		flags   = other.flags;
198 		texture = other.texture;
199 		n_verts = other.n_verts;
200 
201 		i_first = other.i_first;
202 		i_last  = other.i_last;
203 
204 		index_offset = other.index_offset;
205 	}
206 
207 	// Copy-assignment operator
208 	buffer_data& operator=(const buffer_data& rhs)
209 	{
210 		if (this != &rhs)
211 		{
212 			delete [] index;
213 
214 			index = new(std::nothrow) uint[rhs.n_verts];
215 			for (size_t i=0; i < (size_t) rhs.n_verts; i++)
216 			{
217 				index[i] = rhs.index[i];
218 			}
219 
220 			flags   = rhs.flags;
221 			texture = rhs.texture;
222 			n_verts = rhs.n_verts;
223 
224 			i_first = rhs.i_first;
225 			i_last  = rhs.i_last;
226 
227 			index_offset = rhs.index_offset;
228 		}
229 		return *this;
230 	}
231 
232 	// Destructor
~buffer_data()233 	~buffer_data()
234 	{
235 		release();
236 	}
237 
238 private:
239 	uint *index;
240 };
241 
242 class vertex_buffer
243 {
244 public:
245 	int flags;
246 
247 	uint stride;
248 	size_t vertex_offset;
249 
250 	poly_list *model_list;
251 
252 	SCP_vector<buffer_data> tex_buf;
253 
vertex_buffer()254 	vertex_buffer() :
255 		flags(0), stride(0), vertex_offset(0), model_list(NULL)
256 	{
257 	}
258 
~vertex_buffer()259 	~vertex_buffer()
260 	{
261 		clear();
262 	}
263 
release()264 	void release()
265 	{
266 		if (model_list) {
267 			delete model_list;
268 			model_list = NULL;
269 		}
270 
271 		for (SCP_vector<buffer_data>::iterator tbi = tex_buf.begin(); tbi != tex_buf.end(); ++tbi) {
272 			tbi->release();
273 		}
274 	}
275 
clear()276 	void clear()
277 	{
278 		release();
279 		tex_buf.clear();
280 	}
281 };
282 
283 struct light;
284 
285 
286 #define GR_ALPHABLEND_NONE			0		// no blending
287 #define GR_ALPHABLEND_FILTER		1		// 50/50 mix of foreground, background, using intensity as alpha
288 
289 #define GR_BITBLT_MODE_NORMAL		0		// Normal bitblting
290 #define GR_BITBLT_MODE_RLE			1		// RLE would be faster
291 
292 // fog modes
293 #define GR_FOGMODE_NONE				0		// set this to turn off fog
294 #define GR_FOGMODE_FOG				1		// linear fog
295 
296 typedef struct screen {
297 	uint	signature;			// changes when mode or palette or width or height changes
298 	int	max_w, max_h;		// Width and height
299 	int max_w_unscaled, max_h_unscaled;		// Width and height, should be 1024x768 or 640x480 in non-standard resolutions
300 	int	save_max_w, save_max_h;		// Width and height
301 	int save_max_w_unscaled, save_max_h_unscaled;
302 	int	res;					// GR_640 or GR_1024
303 	int	mode;					// What mode gr_init was called with.
304 	float	aspect, clip_aspect;				// Aspect ratio, aspect of clip_width/clip_height
305 	int	rowsize;				// What you need to add to go to next row (includes bytes_per_pixel)
306 	int	bits_per_pixel;	// How many bits per pixel it is. (7,8,15,16,24,32)
307 	int	bytes_per_pixel;	// How many bytes per pixel (1,2,3,4)
308 	int	offset_x, offset_y;		// The offsets into the screen
309 	int offset_x_unscaled, offset_y_unscaled;	// Offsets into the screen, in 1024x768 or 640x480 dimensions
310 	int	clip_width, clip_height;
311 	int clip_width_unscaled, clip_height_unscaled;	// Height and width of clip aread, in 1024x768 or 640x480 dimensions
312 	// center of clip area
313 	float	clip_center_x, clip_center_y;
314 
315 	float fog_near, fog_far;
316 
317 	// the clip_l,r,t,b are used internally.  left and top are
318 	// actually always 0, but it's nice to have the code work with
319 	// arbitrary clipping regions.
320 	int		clip_left, clip_right, clip_top, clip_bottom;
321 	// same as above except in 1024x768 or 640x480 dimensions
322 	int		clip_left_unscaled, clip_right_unscaled, clip_top_unscaled, clip_bottom_unscaled;
323 
324 	int		current_alphablend_mode;		// See GR_ALPHABLEND defines above
325 	int		current_bitblt_mode;				// See GR_BITBLT_MODE defines above
326 	int		current_fog_mode;					// See GR_FOGMODE_* defines above
327 	int		current_bitmap;
328 	color		current_color;
329 	color		current_fog_color;				// current fog color
330 	color		current_clear_color;				// current clear color
331 	shader	current_shader;
332 	float		current_alpha;
333 
334 	bool custom_size;
335 	int		rendering_to_texture;		//wich texture we are rendering to, -1 if the back buffer
336 	int		rendering_to_face;			//wich face of the texture we are rendering to, -1 if the back buffer
337 
338 	int envmap_render_target;
339 
340 	bool recording_state_block;
341 	int current_state_block;
342 
343 	void (*gf_start_state_block)();
344 	int (*gf_end_state_block)();
345 	void (*gf_set_state_block)(int);
346 
347 	//switch onscreen, offscreen
348 	void (*gf_flip)();
349 
350 	// Sets the current palette
351 	void (*gf_set_palette)(ubyte * new_pal, int restrict_alphacolor);
352 
353 	// Fade the screen in/out
354 	void (*gf_fade_in)(int instantaneous);
355 	void (*gf_fade_out)(int instantaneous);
356 
357 	// Flash the screen
358 	void (*gf_flash)( int r, int g, int b );
359 	void (*gf_flash_alpha)(int r, int g, int b, int a);
360 
361 	// sets the clipping region
362 	void (*gf_set_clip)(int x, int y, int w, int h, int resize_mode);
363 
364 	// resets the clipping region to entire screen
365 	void (*gf_reset_clip)();
366 
367 	// clears entire clipping region to current color
368 	void (*gf_clear)();
369 
370 	// void (*gf_bitmap)(int x, int y, int resize_mode);
371 	void (*gf_bitmap_ex)(int x, int y, int w, int h, int sx, int sy, int resize_mode);
372 
373 	void (*gf_aabitmap)(int x, int y, int resize_mode, bool mirror);
374 	void (*gf_aabitmap_ex)(int x, int y, int w, int h, int sx, int sy, int resize_mode, bool mirror);
375 
376 	void (*gf_string)(int x, int y, const char * text,int resize_mode);
377 
378 	// Draw a gradient line... x1,y1 is bright, x2,y2 is transparent.
379 	void (*gf_gradient)(int x1, int y1, int x2, int y2, int resize_mode);
380 
381 	void (*gf_circle)(int x, int y, int r, int resize_mode);
382 	void (*gf_unfilled_circle)(int x, int y, int r, int resize_mode);
383 	void (*gf_arc)(int x, int y, float r, float angle_start, float angle_end, bool fill, int resize_mode);
384 	void (*gf_curve)(int x, int y, int r, int direction, int resize_mode);
385 
386 	// Integer line. Used to draw a fast but pixely line.
387 	void (*gf_line)(int x1, int y1, int x2, int y2, int resize_mode);
388 
389 	// Draws an antialiased line is the current color is an
390 	// alphacolor, otherwise just draws a fast line.  This
391 	// gets called internally by g3_draw_line.   This assumes
392 	// the vertex's are already clipped, so call g3_draw_line
393 	// not this if you have two 3d points.
394 	void (*gf_aaline)(vertex *v1, vertex *v2);
395 
396 	void (*gf_pixel)( int x, int y, int resize_mode );
397 
398 	// Scales current bitmap between va and vb with clipping
399 	void (*gf_scaler)(vertex *va, vertex *vb, bool bw_bitmap );
400 
401 	// Scales current bitmap between va and vb with clipping, draws an aabitmap
402 	void (*gf_aascaler)(vertex *va, vertex *vb );
403 
404 	// Texture maps the current bitmap.  See TMAP_FLAG_?? defines for flag values
405 	void (*gf_tmapper)(int nv, vertex *verts[], uint flags );
406 
407 	// Texture maps the current bitmap.  See TMAP_FLAG_?? defines for flag values
408 	void (*gf_render)(int nv, vertex *verts, uint flags);
409 
410 	void (*gf_render_effect)(int nv, vertex *verts, float *radius_list, uint flags);
411 
412 	// dumps the current screen to a file
413 	void (*gf_print_screen)(const char * filename);
414 
415 	// Call once before rendering anything.
416 	void (*gf_start_frame)();
417 
418 	// Call after rendering is over.
419 	void (*gf_stop_frame)();
420 
421 	// Retrieves the zbuffer mode.
422 	int (*gf_zbuffer_get)();
423 
424 	// Sets mode.  Returns previous mode.
425 	int (*gf_zbuffer_set)(int mode);
426 
427 	// Clears the zbuffer.  If use_zbuffer is FALSE, then zbuffering mode is ignored and zbuffer is always off.
428 	void (*gf_zbuffer_clear)(int use_zbuffer);
429 
430 	// Set the stencil buffer mode. Returns previous mode
431 	int (*gf_stencil_set)(int mode);
432 
433 	// Clears the stencil buffer.
434 	void (*gf_stencil_clear)();
435 
436 	int (*gf_alpha_mask_set)(int mode, float alpha);
437 
438 	// Saves screen. Returns an id you pass to restore and free.
439 	int (*gf_save_screen)();
440 
441 	// Resets clip region and copies entire saved screen to the screen.
442 	void (*gf_restore_screen)(int id);
443 
444 	// Frees up a saved screen.
445 	void (*gf_free_screen)(int id);
446 
447 	// CODE FOR DUMPING FRAMES TO A FILE
448 	// Begin frame dumping
449 	void (*gf_dump_frame_start)( int first_frame_number, int nframes_between_dumps );
450 
451 	// Dump the current frame to file
452 	void (*gf_dump_frame)();
453 
454 	// Dump the current frame to file
455 	void (*gf_dump_frame_stop)();
456 
457 	// Sets the gamma
458 	void (*gf_set_gamma)(float gamma);
459 
460 	// Lock/unlock the screen
461 	// Returns non-zero if sucessful (memory pointer)
462 	uint (*gf_lock)();
463 	void (*gf_unlock)();
464 
465 	// grab a region of the screen. assumes data is large enough
466 	void (*gf_get_region)(int front, int w, int h, ubyte *data);
467 
468 	// set fog attributes
469 	void (*gf_fog_set)(int fog_mode, int r, int g, int b, float fog_near, float fog_far);
470 
471 	// poly culling
472 	int (*gf_set_cull)(int cull);
473 
474 	// color buffer writes
475 	int (*gf_set_color_buffer)(int mode);
476 
477 	// cross fade
478 	void (*gf_cross_fade)(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct, int resize_mode);
479 
480 	// set a texture into cache. for sectioned bitmaps, pass in sx and sy to set that particular section of the bitmap
481 	int (*gf_tcache_set)(int bitmap_id, int bitmap_type, float *u_scale, float *v_scale, int stage);
482 
483 	// preload a bitmap into texture memory
484 	int (*gf_preload)(int bitmap_num, int is_aabitmap);
485 
486 	// set the color to be used when clearing the background
487 	void (*gf_set_clear_color)(int r, int g, int b);
488 
489 	// Here be the bitmap functions
490 	void (*gf_bm_free_data)(int n, bool release);
491 	void (*gf_bm_create)(int n);
492 	int (*gf_bm_load)(ubyte type, int n, const char *filename, CFILE *img_cfp, int *w, int *h, int *bpp, ubyte *c_type, int *mm_lvl, int *size);
493 	void (*gf_bm_init)(int n);
494 	void (*gf_bm_page_in_start)();
495 	int (*gf_bm_lock)(const char *filename, int handle, int bitmapnum, ubyte bpp, ubyte flags, bool nodebug);
496 
497 	int (*gf_bm_make_render_target)(int n, int *width, int *height, ubyte *bpp, int *mm_lvl, int flags );
498 	int (*gf_bm_set_render_target)(int n, int face);
499 
500 	void (*gf_translate_texture_matrix)(int unit, vec3d *shift);
501 	void (*gf_push_texture_matrix)(int unit);
502 	void (*gf_pop_texture_matrix)(int unit);
503 
504 	void (*gf_set_texture_addressing)(int);
505 
506 	int (*gf_create_buffer)();
507 	bool (*gf_pack_buffer)(const int buffer_id, vertex_buffer *vb);
508 	bool (*gf_config_buffer)(const int buffer_id, vertex_buffer *vb);
509 	void (*gf_destroy_buffer)(int);
510 	void (*gf_set_buffer)(int);
511 	void (*gf_render_buffer)(int, const vertex_buffer*, int, int);
512 
513 	int (*gf_create_stream_buffer)();
514 	void (*gf_update_stream_buffer)(int buffer, effect_vertex *buffer_data, uint size);
515 	void (*gf_render_stream_buffer)(int offset, int n_verts, int flags);
516 	void (*gf_render_stream_buffer_start)(int buffer_id);
517 	void (*gf_render_stream_buffer_end)();
518 
519 	int	 (*gf_make_flat_buffer)(poly_list*);
520 	int	 (*gf_make_line_buffer)(line_list*);
521 
522 
523 	//the projection matrix; fov, aspect ratio, near, far
524  	void (*gf_set_proj_matrix)(float, float, float, float);
525   	void (*gf_end_proj_matrix)();
526 	//the view matrix
527  	void (*gf_set_view_matrix)(vec3d *, matrix*);
528   	void (*gf_end_view_matrix)();
529 	//object scaleing
530 	void (*gf_push_scale_matrix)(vec3d *);
531  	void (*gf_pop_scale_matrix)();
532 	//object position and orientation
533 	void (*gf_start_instance_matrix)(vec3d *, matrix*);
534 	void (*gf_start_angles_instance_matrix)(vec3d *, angles*);
535 	void (*gf_end_instance_matrix)();
536 
537 	int	 (*gf_make_light)(light*, int, int );
538 	void (*gf_modify_light)(light*, int, int );
539 	void (*gf_destroy_light)(int);
540 	void (*gf_set_light)(light*);
541 	void (*gf_reset_lighting)();
542 	void (*gf_set_ambient_light)(int,int,int);
543 
544 	// postprocessing effects
545 	void (*gf_post_process_set_effect)(const char*, int);
546 	void (*gf_post_process_set_defaults)();
547 
548 	void (*gf_post_process_begin)();
549 	void (*gf_post_process_end)();
550 	void (*gf_post_process_save_zbuffer)();
551 
552 	void (*gf_scene_texture_begin)();
553 	void (*gf_scene_texture_end)();
554 
555 	void (*gf_lighting)(bool,bool);
556 	void (*gf_center_alpha)(int);
557 
558 	void (*gf_start_clip_plane)();
559 	void (*gf_end_clip_plane)();
560 
561 	void (*gf_zbias)(int zbias);
562 	void (*gf_setup_background_fog)(bool);
563 
564 	void (*gf_set_fill_mode)(int);
565 	void (*gf_set_texture_panning)(float u, float v, bool enable);
566 
567 	void (*gf_draw_line_list)(colored_vector*lines, int num);
568 
569 	void (*gf_set_line_width)(float width);
570 
571 	void (*gf_line_htl)(vec3d *start, vec3d* end);
572 	void (*gf_sphere_htl)(float rad);
573 
574 	int (*gf_maybe_create_shader)(int flags);
575 
576 	void (*gf_flush_data_states)();
577 
578 	void (*gf_set_team_color)(const SCP_string &team, const SCP_string &secondaryteam, fix timestamp, int fadetime);
579 	void (*gf_enable_team_color)();
580 	void (*gf_disable_team_color)();
581 
582 	void (*gf_update_texture)(int bitmap_handle, int bpp, ubyte* data, int width, int height);
583 } screen;
584 
585 // handy macro
586 #define GR_MAYBE_CLEAR_RES(bmap)		do  { int bmw = -1; int bmh = -1; if(bmap != -1){ bm_get_info( bmap, &bmw, &bmh, NULL, NULL, NULL); if((bmw != gr_screen.max_w) || (bmh != gr_screen.max_h)){gr_clear();} } else {gr_clear();} } while(0);
587 
588 //Window's interface to set up graphics:
589 //--------------------------------------
590 // Call this at application startup
591 
592 // # Software Re-added by Kazan --- THIS HAS TO STAY -- It is used by standalone!
593 #define GR_DEFAULT				(-1)		// set to use default settings
594 #define GR_STUB					(100)
595 #define GR_OPENGL				(104)		// Use OpenGl hardware renderer
596 
597 // resolution constants   - always keep resolutions in ascending order and starting from 0
598 #define GR_NUM_RESOLUTIONS			2
599 #define GR_640							0		// 640 x 480
600 #define GR_1024						1		// 1024 x 768
601 
602 extern const char *Resolution_prefixes[GR_NUM_RESOLUTIONS];
603 
604 extern bool gr_init(int d_mode = GR_DEFAULT, int d_width = GR_DEFAULT, int d_height = GR_DEFAULT, int d_depth = GR_DEFAULT);
605 extern void gr_screen_resize(int width, int height);
606 
607 // Call this when your app ends.
608 extern void gr_close();
609 
610 extern screen gr_screen;
611 
612 #define GR_FILL_MODE_WIRE 1
613 #define GR_FILL_MODE_SOLID 2
614 
615 #define GR_ZBUFF_NONE	0
616 #define GR_ZBUFF_WRITE	(1<<0)
617 #define GR_ZBUFF_READ	(1<<1)
618 #define GR_ZBUFF_FULL	(GR_ZBUFF_WRITE|GR_ZBUFF_READ)
619 
620 #define GR_STENCIL_NONE		0
621 #define GR_STENCIL_READ		1
622 #define GR_STENCIL_WRITE	2
623 
624 #define GR_RESIZE_NONE				0
625 #define GR_RESIZE_FULL				1
626 #define GR_RESIZE_MENU				2
627 #define GR_RESIZE_MENU_NO_OFFSET	3
628 
629 void gr_set_screen_scale(int x, int y);
630 void gr_set_screen_scale(int x, int y, int max_x, int max_y);
631 void gr_reset_screen_scale();
632 bool gr_unsize_screen_pos(int *x, int *y, int *w = NULL, int *h = NULL, int resize_mode = GR_RESIZE_FULL);
633 bool gr_resize_screen_pos(int *x, int *y, int *w = NULL, int *h = NULL, int resize_mode = GR_RESIZE_FULL);
634 bool gr_unsize_screen_posf(float *x, float *y, float *w = NULL, float *h = NULL, int resize_mode = GR_RESIZE_FULL);
635 bool gr_resize_screen_posf(float *x, float *y, float *w = NULL, float *h = NULL, int resize_mode = GR_RESIZE_FULL);
636 
637 // Does formatted printing.  This calls gr_string after formatting,
638 // so if you don't need to format the string, then call gr_string
639 // directly.
640 extern void _cdecl gr_printf( int x, int y, const char * format, ... );
641 // same as gr_printf but positions text correctly in menus
642 extern void _cdecl gr_printf_menu( int x, int y, const char * format, ... );
643 // same as gr_printf but doesn't resize for non-standard resolutions
644 extern void _cdecl gr_printf_no_resize( int x, int y, const char * format, ... );
645 
646 // Returns the size of the string in pixels in w and h
647 extern void gr_get_string_size( int *w, int *h, const char * text, int len = 9999 );
648 
649 // Returns the height of the current font
650 extern int gr_get_font_height();
651 
652 extern void gr_set_palette(const char *name, ubyte *palette, int restrict_to_128 = 0);
653 
654 // These two functions use a Windows mono font.  Only for use
655 // in the editor, please.
656 void gr_get_string_size_win(int *w, int *h, const char *text);
657 void gr_string_win(int x, int y, const char *s );
658 
659 // set the mouse pointer to a specific bitmap, used for animating cursors
660 #define GR_CURSOR_LOCK		1
661 #define GR_CURSOR_UNLOCK	2
662 void gr_set_cursor_bitmap(int n, int lock = 0);
663 void gr_unset_cursor_bitmap(int n);
664 int gr_get_cursor_bitmap();
665 extern int Web_cursor_bitmap;
666 
667 // Called by OS when application gets/looses focus
668 extern void gr_activate(int active);
669 
670 #define GR_CALL(x)			(*x)
671 
672 // These macros make the function indirection look like the
673 // old Descent-style gr_xxx calls.
674 
675 #define gr_print_screen		GR_CALL(gr_screen.gf_print_screen)
676 
677 //#define gr_flip				GR_CALL(gr_screen.gf_flip)
678 void gr_flip();
679 
680 //#define gr_set_clip			GR_CALL(gr_screen.gf_set_clip)
681 __inline void gr_set_clip(int x, int y, int w, int h, int resize_mode=GR_RESIZE_FULL)
682 {
683 	(*gr_screen.gf_set_clip)(x,y,w,h,resize_mode);
684 }
685 #define gr_reset_clip		GR_CALL(gr_screen.gf_reset_clip)
686 
687 void gr_set_bitmap(int bitmap_num, int alphablend = GR_ALPHABLEND_NONE, int bitbltmode = GR_BITBLT_MODE_NORMAL, float alpha = 1.0f);
688 
689 #define gr_clear				GR_CALL(gr_screen.gf_clear)
690 __inline void gr_aabitmap(int x, int y, int resize_mode = GR_RESIZE_FULL, bool mirror = false)
691 {
692 	(*gr_screen.gf_aabitmap)(x,y,resize_mode,mirror);
693 }
694 
695 __inline void gr_aabitmap_ex(int x, int y, int w, int h, int sx, int sy, int resize_mode = GR_RESIZE_FULL, bool mirror = false)
696 {
697 	(*gr_screen.gf_aabitmap_ex)(x,y,w,h,sx,sy,resize_mode,mirror);
698 }
699 
700 __inline void gr_bitmap_ex(int x, int y, int w, int h, int sx, int sy, int resize_mode = GR_RESIZE_FULL)
701 {
702 	(*gr_screen.gf_bitmap_ex)(x, y, w, h, sx, sy, resize_mode);
703 }
704 
705 void gr_shield_icon(coord2d coords[6], const int resize_mode = GR_RESIZE_FULL);
706 void gr_rect(int x, int y, int w, int h, int resize_mode = GR_RESIZE_FULL);
707 void gr_shade(int x, int y, int w, int h, int resize_mode = GR_RESIZE_FULL);
708 
709 __inline void gr_string(int x, int y, const char* string, int resize_mode = GR_RESIZE_FULL)
710 {
711 	(*gr_screen.gf_string)(x,y,string,resize_mode);
712 }
713 
714 __inline void gr_circle(int xc, int yc, int d, int resize_mode = GR_RESIZE_FULL)
715 {
716 	(*gr_screen.gf_circle)(xc,yc,d,resize_mode);
717 }
718 
719 __inline void gr_unfilled_circle(int xc, int yc, int d, int resize_mode = GR_RESIZE_FULL)
720 {
721 	(*gr_screen.gf_unfilled_circle)(xc,yc,d,resize_mode);
722 }
723 
724 __inline void gr_arc(int xc, int yc, float r, float angle_start, float angle_end, bool fill, int resize_mode = GR_RESIZE_FULL)
725 {
726 	(*gr_screen.gf_arc)(xc,yc,r,angle_start,angle_end,fill,resize_mode);
727 }
728 
729 #define gr_curve				GR_CALL(gr_screen.gf_curve)
730 
731 __inline void gr_line(int x1, int y1, int x2, int y2, int resize_mode = GR_RESIZE_FULL)
732 {
733 	(*gr_screen.gf_line)(x1, y1, x2, y2, resize_mode);
734 }
735 
736 #define gr_aaline				GR_CALL(gr_screen.gf_aaline)
737 
738 __inline void gr_pixel(int x, int y, int resize_mode = GR_RESIZE_FULL)
739 {
740 	(*gr_screen.gf_pixel)(x, y, resize_mode);
741 }
742 #define gr_scaler				GR_CALL(gr_screen.gf_scaler)
743 #define gr_aascaler			GR_CALL(gr_screen.gf_aascaler)
744 #define gr_tmapper			GR_CALL(gr_screen.gf_tmapper)
745 #define gr_render			GR_CALL(gr_screen.gf_render)
746 #define gr_render_effect	GR_CALL(gr_screen.gf_render_effect)
747 
748 __inline void gr_gradient(int x1, int y1, int x2, int y2, int resize_mode = GR_RESIZE_FULL)
749 {
750 	(*gr_screen.gf_gradient)(x1, y1, x2, y2, resize_mode);
751 }
752 
753 #define gr_fade_in			GR_CALL(gr_screen.gf_fade_in)
754 #define gr_fade_out			GR_CALL(gr_screen.gf_fade_out)
755 #define gr_flash			GR_CALL(gr_screen.gf_flash)
756 #define gr_flash_alpha		GR_CALL(gr_screen.gf_flash_alpha)
757 
758 #define gr_zbuffer_get		GR_CALL(gr_screen.gf_zbuffer_get)
759 #define gr_zbuffer_set		GR_CALL(gr_screen.gf_zbuffer_set)
760 #define gr_zbuffer_clear	GR_CALL(gr_screen.gf_zbuffer_clear)
761 
762 #define gr_stencil_set		GR_CALL(gr_screen.gf_stencil_set)
763 #define gr_stencil_clear	GR_CALL(gr_screen.gf_stencil_clear)
764 
765 #define gr_alpha_mask_set	GR_CALL(gr_screen.gf_alpha_mask_set)
766 
767 #define gr_save_screen		GR_CALL(gr_screen.gf_save_screen)
768 #define gr_restore_screen	GR_CALL(gr_screen.gf_restore_screen)
769 #define gr_free_screen		GR_CALL(gr_screen.gf_free_screen)
770 
771 #define gr_dump_frame_start	GR_CALL(gr_screen.gf_dump_frame_start)
772 #define gr_dump_frame_stop		GR_CALL(gr_screen.gf_dump_frame_stop)
773 #define gr_dump_frame			GR_CALL(gr_screen.gf_dump_frame)
774 
775 #define gr_set_gamma			GR_CALL(gr_screen.gf_set_gamma)
776 
777 #define gr_get_region		GR_CALL(gr_screen.gf_get_region)
778 
779 __inline void gr_fog_set(int fog_mode, int r, int g, int b, float fog_near = -1.0f, float fog_far = -1.0f)
780 {
781 	(*gr_screen.gf_fog_set)(fog_mode, r, g, b, fog_near, fog_far);
782 }
783 
784 #define gr_set_cull			GR_CALL(gr_screen.gf_set_cull)
785 #define gr_set_color_buffer	GR_CALL(gr_screen.gf_set_color_buffer)
786 
787 #define gr_cross_fade		GR_CALL(gr_screen.gf_cross_fade)
788 
789 __inline int gr_tcache_set(int bitmap_id, int bitmap_type, float *u_scale, float *v_scale, int stage = 0)
790 {
791 	return (*gr_screen.gf_tcache_set)(bitmap_id, bitmap_type, u_scale, v_scale, stage);
792 }
793 
794 #define gr_preload			GR_CALL(gr_screen.gf_preload)
795 
796 #define gr_set_clear_color	GR_CALL(gr_screen.gf_set_clear_color)
797 
798 #define gr_translate_texture_matrix		GR_CALL(gr_screen.gf_translate_texture_matrix)
799 #define gr_push_texture_matrix			GR_CALL(gr_screen.gf_push_texture_matrix)
800 #define gr_pop_texture_matrix			GR_CALL(gr_screen.gf_pop_texture_matrix)
801 
802 
803 // Here be the bitmap functions
804 #define gr_bm_free_data				GR_CALL(*gr_screen.gf_bm_free_data)
805 #define gr_bm_create				GR_CALL(*gr_screen.gf_bm_create)
806 #define gr_bm_init					GR_CALL(*gr_screen.gf_bm_init)
807 __inline int gr_bm_load(ubyte type, int n, const char *filename, CFILE *img_cfp = NULL, int *w = 0, int *h = 0, int *bpp = 0, ubyte *c_type = 0, int *mm_lvl = 0, int *size = 0)
808 {
809 	return (*gr_screen.gf_bm_load)(type, n, filename, img_cfp, w, h, bpp, c_type, mm_lvl, size);
810 }
811 #define gr_bm_page_in_start			GR_CALL(*gr_screen.gf_bm_page_in_start)
812 #define gr_bm_lock					GR_CALL(*gr_screen.gf_bm_lock)
813 
814 #define gr_bm_make_render_target					GR_CALL(*gr_screen.gf_bm_make_render_target)
815 
816 __inline int gr_bm_set_render_target(int n, int face = -1)
817 {
818 	return (*gr_screen.gf_bm_set_render_target)(n, face);
819 }
820 
821 #define gr_set_texture_addressing					 GR_CALL(*gr_screen.gf_set_texture_addressing)
822 
823 #define gr_create_buffer				GR_CALL(*gr_screen.gf_create_buffer)
824 #define gr_pack_buffer					GR_CALL(*gr_screen.gf_pack_buffer)
825 #define gr_config_buffer				GR_CALL(*gr_screen.gf_config_buffer)
826 #define gr_destroy_buffer				 GR_CALL(*gr_screen.gf_destroy_buffer)
827 __inline void gr_render_buffer(int start, const vertex_buffer *bufferp, int texi, int flags = TMAP_FLAG_TEXTURED)
828 {
829 	(*gr_screen.gf_render_buffer)(start, bufferp, texi, flags);
830 }
831 
832 #define gr_create_stream_buffer			GR_CALL(*gr_screen.gf_create_stream_buffer)
833 #define gr_update_stream_buffer			GR_CALL(*gr_screen.gf_update_stream_buffer)
834 #define gr_render_stream_buffer			GR_CALL(*gr_screen.gf_render_stream_buffer)
835 #define gr_render_stream_buffer_start	GR_CALL(*gr_screen.gf_render_stream_buffer_start)
836 #define gr_render_stream_buffer_end		GR_CALL(*gr_screen.gf_render_stream_buffer_end)
837 
838 #define gr_set_buffer					GR_CALL(*gr_screen.gf_set_buffer)
839 
840 #define gr_make_flat_buffer				GR_CALL(*gr_screen.gf_make_flat_buffer)
841 #define gr_make_line_buffer				GR_CALL(*gr_screen.gf_make_line_buffer)
842 
843 #define gr_set_proj_matrix					GR_CALL(*gr_screen.gf_set_proj_matrix)
844 #define gr_end_proj_matrix					GR_CALL(*gr_screen.gf_end_proj_matrix)
845 #define gr_set_view_matrix					GR_CALL(*gr_screen.gf_set_view_matrix)
846 #define gr_end_view_matrix					GR_CALL(*gr_screen.gf_end_view_matrix)
847 #define gr_push_scale_matrix				GR_CALL(*gr_screen.gf_push_scale_matrix)
848 #define gr_pop_scale_matrix					GR_CALL(*gr_screen.gf_pop_scale_matrix)
849 #define gr_start_instance_matrix			GR_CALL(*gr_screen.gf_start_instance_matrix)
850 #define gr_start_angles_instance_matrix		GR_CALL(*gr_screen.gf_start_angles_instance_matrix)
851 #define gr_end_instance_matrix				GR_CALL(*gr_screen.gf_end_instance_matrix)
852 
853 #define	gr_make_light					GR_CALL(*gr_screen.gf_make_light)
854 #define	gr_modify_light					GR_CALL(*gr_screen.gf_modify_light)
855 #define	gr_destroy_light				GR_CALL(*gr_screen.gf_destroy_light)
856 #define	gr_set_light					GR_CALL(*gr_screen.gf_set_light)
857 #define gr_reset_lighting				GR_CALL(*gr_screen.gf_reset_lighting)
858 #define gr_set_ambient_light			GR_CALL(*gr_screen.gf_set_ambient_light)
859 
860 #define gr_scene_texture_begin			GR_CALL(*gr_screen.gf_scene_texture_begin)
861 #define gr_scene_texture_end			GR_CALL(*gr_screen.gf_scene_texture_end)
862 
863 #define gr_post_process_set_effect		GR_CALL(*gr_screen.gf_post_process_set_effect)
864 #define gr_post_process_set_defaults	GR_CALL(*gr_screen.gf_post_process_set_defaults)
865 #define gr_post_process_begin			GR_CALL(*gr_screen.gf_post_process_begin)
866 #define gr_post_process_end				GR_CALL(*gr_screen.gf_post_process_end)
867 #define gr_post_process_save_zbuffer	GR_CALL(*gr_screen.gf_post_process_save_zbuffer)
868 
869 #define	gr_set_lighting					GR_CALL(*gr_screen.gf_lighting)
870 #define	gr_center_alpha					GR_CALL(*gr_screen.gf_center_alpha)
871 
872 #define	gr_start_clip					GR_CALL(*gr_screen.gf_start_clip_plane)
873 #define	gr_end_clip						GR_CALL(*gr_screen.gf_end_clip_plane)
874 
875 #define	gr_zbias						GR_CALL(*gr_screen.gf_zbias)
876 #define	gr_set_fill_mode				GR_CALL(*gr_screen.gf_set_fill_mode)
877 #define	gr_set_texture_panning			GR_CALL(*gr_screen.gf_set_texture_panning)
878 
879 #define	gr_start_state_block			GR_CAL(*gr_screen.gf_start_state_block)
880 #define	gr_end_state_block				GR_CALL(*gr_screen.gf_end_state_block)
881 #define	gr_set_state_block				GR_CALL(*gr_screen.gf_set_state_block)
882 
883 #define gr_setup_background_fog			GR_CALL(*gr_screen.gf_setup_background_fog)
884 
885 #define gr_draw_line_list				GR_CALL(*gr_screen.gf_draw_line_list)
886 
887 #define gr_set_line_width				GR_CALL(*gr_screen.gf_set_line_width)
888 
889 #define gr_line_htl						GR_CALL(*gr_screen.gf_line_htl)
890 #define gr_sphere_htl					GR_CALL(*gr_screen.gf_sphere_htl)
891 
892 #define gr_maybe_create_shader			GR_CALL(*gr_screen.gf_maybe_create_shader)
893 
894 #define gr_flush_data_states			GR_CALL(*gr_screen.gf_flush_data_states)
895 
896 #define gr_set_team_color				GR_CALL(*gr_screen.gf_set_team_color)
897 #define gr_disable_team_color			GR_CALL(*gr_screen.gf_disable_team_color)
898 
899 #define gr_update_texture				GR_CALL(*gr_screen.gf_update_texture)
900 
901 // color functions
902 void gr_get_color( int *r, int *g, int  b );
903 void gr_init_color(color *c, int r, int g, int b);
904 void gr_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type = AC_TYPE_HUD );
905 void gr_set_color( int r, int g, int b );
906 void gr_set_color_fast(color *dst);
907 
908 // shader functions
909 void gr_create_shader(shader *shade, ubyte r, ubyte g, ubyte b, ubyte c);
910 void gr_set_shader(shader *shade);
911 
912 // new bitmap functions
913 void gr_bitmap(int x, int y, int resize_mode = GR_RESIZE_FULL);
914 void gr_bitmap_uv(int _x, int _y, int _w, int _h, float _u0, float _v0, float _u1, float _v1, int resize_mode = GR_RESIZE_FULL);
915 void gr_bitmap_list(bitmap_2d_list* list, int n_bm, int resize_mode);
916 void gr_bitmap_list(bitmap_rect_list* list, int n_bm, int resize_mode);
917 
918 // texture update functions
919 ubyte* gr_opengl_get_texture_update_pointer(int bitmap_handle);
920 void gr_opengl_update_texture(int bitmap_handle, int bpp, ubyte* data, int width, int height);
921 
922 // special function for drawing polylines. this function is specifically intended for
923 // polylines where each section is no more than 90 degrees away from a previous section.
924 // Moreover, it is _really_ intended for use with 45 degree angles.
925 void gr_pline_special(vec3d **pts, int num_pts, int thickness,int resize_mode=GR_RESIZE_FULL);
926 
927 #define VB_FLAG_POSITION	(1<<0)
928 #define VB_FLAG_RHW			(1<<1)	//incompatable with the next normal
929 #define VB_FLAG_NORMAL		(1<<2)
930 #define VB_FLAG_DIFUSE		(1<<3)
931 #define VB_FLAG_SPECULAR	(1<<4)
932 #define VB_FLAG_UV1			(1<<5)	//how many UV coords, only use one of these
933 #define VB_FLAG_UV2			(1<<6)
934 #define VB_FLAG_UV3			(1<<7)
935 #define VB_FLAG_UV4			(1<<8)
936 #define VB_FLAG_TANGENT		(1<<9)
937 #define VB_FLAG_LARGE_INDEX	(1<<10)
938 
939 void gr_clear_shaders_cache();
940 
941 #endif
942