]> icculus.org git repositories - btb/d2x.git/blob - include/gr.h
get rid of compiler warnings
[btb/d2x.git] / include / gr.h
1 /* $Id: gr.h,v 1.28 2004-08-28 23:17:45 schaffner Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Definitions for graphics lib.
18  *
19  */
20
21 #ifndef _GR_H
22 #define _GR_H
23
24 #include "pstypes.h"
25 #include "fix.h"
26
27 #if defined(MACINTOSH) || defined(MACDATA)
28 #error native mac data currently not supported
29 #define SWAP_0_255              // swap black and white
30 #define TRANSPARENCY_COLOR  0   // palette entry of transparency color -- 0 on the mac
31 #define TRANSPARENCY_COLOR_STR  "0"
32 #else /* defined(MACINTOSH) || defined(MACDATA) */
33 /* #undef  SWAP_0_255 */        // no swapping for PC people
34 #define TRANSPARENCY_COLOR  255 // palette entry of transparency color -- 255 on the PC
35 #define TRANSPARENCY_COLOR_STR  "255"
36 #endif /* defined(MACINTOSH) || defined(MACDATA) */
37
38 #define GR_FADE_LEVELS 34
39 #define GR_ACTUAL_FADE_LEVELS 32
40
41 #define GWIDTH  grd_curcanv->cv_bitmap.bm_w
42 #define GHEIGHT grd_curcanv->cv_bitmap.bm_h
43 #define SWIDTH  (grd_curscreen->sc_w)
44 #define SHEIGHT (grd_curscreen->sc_h)
45
46 #define MAX_BMP_SIZE(width, height) (4 + ((width) + 2) * (height))
47
48 extern int Gr_scanline_darkening_level;
49
50 typedef struct _grs_point {
51         fix x,y;
52 } grs_point;
53
54 //these are control characters that have special meaning in the font code
55
56 #define CC_COLOR        1   //next char is new foreground color
57 #define CC_LSPACING     2   //next char specifies line spacing
58 #define CC_UNDERLINE    3   //next char is underlined
59
60 //now have string versions of these control characters (can concat inside a string)
61
62 #define CC_COLOR_S      "\x1"   //next char is new foreground color
63 #define CC_LSPACING_S   "\x2"   //next char specifies line spacing
64 #define CC_UNDERLINE_S  "\x3"   //next char is underlined
65
66 #define BM_LINEAR   0
67 #define BM_MODEX    1
68 #define BM_SVGA     2
69 #define BM_RGB15    3   //5 bits each r,g,b stored at 16 bits
70 #define BM_SVGA15   4
71 #ifdef OGL
72 #define BM_OGL      5
73 #endif /* def OGL */
74
75 //@@// Define these modes for Gameplay too, since the game was developed under
76 //@@// DOS, we will adapt these modes to other systems thru rendering.
77 //@@#define SM_ORIGINAL         -1
78 //@@#define SM_320x200C     0
79 //@@#define SM_320x200U     1
80 //@@#define SM_320x240U     2
81 //@@#define SM_360x200U     3
82 //@@#define SM_360x240U     4
83 //@@#define SM_376x282U     5
84 //@@#define SM_320x400U     6
85 //@@#define SM_320x480U     7
86 //@@#define SM_360x400U     8
87 //@@#define SM_360x480U     9
88 //@@#define SM_360x360U     10
89 //@@#define SM_376x308U     11
90 //@@#define SM_376x564U     12
91 //@@#define SM_640x400V     13
92 //@@#define SM_640x480V     14
93 //@@#define SM_800x600V     15
94 //@@#define SM_1024x768V    16
95 //@@#define SM_640x480V15   17
96 //@@#define SM_800x600V15   18
97
98 #define SM(w,h) ((((u_int32_t)w)<<16)+(((u_int32_t)h)&0xFFFF))
99 #define SM_W(m) (m>>16)
100 #define SM_H(m) (m&0xFFFF)
101
102
103 #define BM_FLAG_TRANSPARENT         1
104 #define BM_FLAG_SUPER_TRANSPARENT   2
105 #define BM_FLAG_NO_LIGHTING         4
106 #define BM_FLAG_RLE                 8   // A run-length encoded bitmap.
107 #define BM_FLAG_PAGED_OUT           16  // This bitmap's data is paged out.
108 #define BM_FLAG_RLE_BIG             32  // for bitmaps that RLE to > 255 per row (i.e. cockpits)
109
110 typedef struct _grs_bitmap {
111         short   bm_x,bm_y;  // Offset from parent's origin
112         short   bm_w,bm_h;  // width,height
113         sbyte   bm_type;    // 0=Linear, 1=ModeX, 2=SVGA
114         sbyte   bm_flags;   // bit 0 on means it has transparency.
115                             // bit 1 on means it has supertransparency
116                             // bit 2 on means it doesn't get passed through lighting.
117         short   bm_rowsize; // unsigned char offset to next row
118         unsigned char *     bm_data;    // ptr to pixel data...
119                                         //   Linear = *parent+(rowsize*y+x)
120                                         //   ModeX = *parent+(rowsize*y+x/4)
121                                         //   SVGA = *parent+(rowsize*y+x)
122         unsigned short      bm_handle;  //for application.  initialized to 0
123         ubyte   avg_color;  //  Average color of all pixels in texture map.
124         sbyte   unused;     // to 4-byte align.
125 #ifdef OGL
126         struct _ogl_texture *gltexture;
127         struct _grs_bitmap  *bm_parent;
128 #endif /* def OGL */
129 } grs_bitmap;
130
131 //font structure
132 typedef struct _grs_font {
133         short       ft_w;           // Width in pixels
134         short       ft_h;           // Height in pixels
135         short       ft_flags;       // Proportional?
136         short       ft_baseline;    //
137         ubyte       ft_minchar;     // First char defined by this font
138         ubyte       ft_maxchar;     // Last char defined by this font
139         short       ft_bytewidth;   // Width in unsigned chars
140         ubyte     * ft_data;        // Ptr to raw data.
141         ubyte    ** ft_chars;       // Ptrs to data for each char (required for prop font)
142         short     * ft_widths;      // Array of widths (required for prop font)
143         ubyte     * ft_kerndata;    // Array of kerning triplet data
144 #ifdef OGL
145         // These fields do not participate in disk i/o!
146         grs_bitmap *ft_bitmaps;
147         grs_bitmap ft_parent_bitmap;
148 #endif /* def OGL */
149 } __pack__ grs_font;
150
151 #define GRS_FONT_SIZE 28    // how much space it takes up on disk
152
153 typedef struct _grs_canvas {
154         grs_bitmap  cv_bitmap;      // the bitmap for this canvas
155         short       cv_color;       // current color
156         short       cv_drawmode;    // fill,XOR,etc.
157         grs_font *  cv_font;        // the currently selected font
158         short       cv_font_fg_color;   // current font foreground color (-1==Invisible)
159         short       cv_font_bg_color;   // current font background color (-1==Invisible)
160 } grs_canvas;
161
162 //shortcuts
163 #define cv_w cv_bitmap.bm_w
164 #define cv_h cv_bitmap.bm_h
165
166 typedef struct _grs_screen {    // This is a video screen
167         grs_canvas  sc_canvas;  // Represents the entire screen
168         int     sc_mode;        // Video mode number
169         short   sc_w, sc_h;     // Actual Width and Height
170         fix     sc_aspect;      //aspect ratio (w/h) for this screen
171 } grs_screen;
172
173
174 //=========================================================================
175 // System functions:
176 // setup and set mode. this creates a grs_screen structure and sets
177 // grd_curscreen to point to it.  grs_curcanv points to this screen's
178 // canvas.  Saves the current VGA state and screen mode.
179
180 int gr_init(void);
181
182 // This function sets up the main screen.  It should be called whenever
183 // the video mode changes.
184 int gr_init_screen(int mode, int w, int h, int x, int y, int rowsize, ubyte *data);
185
186 int gr_check_mode(u_int32_t mode);
187 int gr_set_mode(u_int32_t mode);
188
189
190 // These 4 functions actuall change screen colors.
191
192 extern void gr_pal_fade_out(unsigned char * pal);
193 extern void gr_pal_fade_in(unsigned char * pal);
194 extern void gr_pal_clear(void);
195 extern void gr_pal_setblock( int start, int number, unsigned char * pal );
196 extern void gr_pal_getblock( int start, int number, unsigned char * pal );
197
198
199 extern unsigned char *gr_video_memory;
200                                                     // All graphic modules will define this value.
201
202 //shut down the 2d.  Restore the screen mode.
203 void gr_close(void);
204
205 //=========================================================================
206 // Canvas functions:
207
208 // Makes a new canvas. allocates memory for the canvas and its bitmap,
209 // including the raw pixel buffer.
210
211 grs_canvas *gr_create_canvas(int w, int h);
212 #if defined(POLY_ACC)
213 grs_canvas *gr_create_canvas2(int w, int h, int type);
214 #endif /* def POLY_ACC */
215
216 // Creates a canvas that is part of another canvas.  this can be used to make
217 // a window on the screen.  the canvas structure is malloc'd; the address of
218 // the raw pixel data is inherited from the parent canvas.
219
220 grs_canvas *gr_create_sub_canvas(grs_canvas *canv,int x,int y,int w, int h);
221
222 // Initialize the specified canvas. the raw pixel data buffer is passed as
223 // a parameter. no memory allocation is performed.
224
225 void gr_init_canvas(grs_canvas *canv,unsigned char *pixdata,int pixtype, int w,int h);
226
227 // Initialize the specified sub canvas. no memory allocation is performed.
228
229 void gr_init_sub_canvas(grs_canvas *new,grs_canvas *src,int x,int y,int w, int h);
230
231 // Free up the canvas and its pixel data.
232
233 void gr_free_canvas(grs_canvas *canv);
234
235 // Free up the canvas. do not free the pixel data, which belongs to the
236 // parent canvas.
237
238 void gr_free_sub_canvas(grs_canvas *canv);
239
240 // Clear the current canvas to the specified color
241 void gr_clear_canvas(int color);
242
243 //=========================================================================
244 // Bitmap functions:
245
246 // Allocate a bitmap and its pixel data buffer.
247 grs_bitmap *gr_create_bitmap(int w,int h);
248
249 // Allocated a bitmap and makes its data be raw_data that is already somewhere.
250 grs_bitmap *gr_create_bitmap_raw(int w, int h, unsigned char * raw_data );
251
252 #if defined(POLY_ACC)
253 // Allocates a bitmap of a specific type. data is either NULL or raw data.
254 grs_bitmap *gr_create_bitmap2(int w, int h, int type, void *data );
255 #endif /* def POLY_ACC */
256
257 // Creates a bitmap which is part of another bitmap
258 grs_bitmap *gr_create_sub_bitmap(grs_bitmap *bm,int x,int y,int w, int h);
259
260 // Free the bitmap and its pixel data
261 void gr_free_bitmap(grs_bitmap *bm);
262
263 // Free the bitmap's data
264 void gr_free_bitmap_data (grs_bitmap *bm);
265 void gr_init_bitmap_data (grs_bitmap *bm);
266
267 // Free the bitmap, but not the pixel data buffer
268 void gr_free_sub_bitmap(grs_bitmap *bm);
269
270 void gr_bm_pixel( grs_bitmap * bm, int x, int y, unsigned char color );
271 void gr_bm_upixel( grs_bitmap * bm, int x, int y, unsigned char color );
272 void gr_bm_bitblt(int w, int h, int dx, int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest);
273 void gr_bm_ubitblt( int w, int h, int dx, int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest);
274 void gr_bm_ubitbltm(int w, int h, int dx, int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest);
275
276 #ifdef MACINTOSH
277 void gr_bm_ubitblt_double(int w, int h, int dx, int dy, int sx, int sy, grs_bitmap *src, grs_bitmap *dest);
278 void gr_linear_movsd_double(ubyte *src, ubyte *dest, int num_pixels);
279 #endif /* def MACINTOSH */
280
281 void gr_update_buffer( void * sbuf1, void * sbuf2, void * dbuf, int size );
282
283 void gr_set_transparent(grs_bitmap *pbm, int bTransparent);
284
285 //=========================================================================
286 // Color functions:
287
288 // When this function is called, the guns are set to gr_palette, and
289 // the palette stays the same until gr_close is called
290
291 void gr_use_palette_table(char * filename );
292 void gr_copy_palette(ubyte *gr_palette, ubyte *pal, int size);
293
294 //=========================================================================
295 // Drawing functions:
296
297 // For solid, XOR, or other fill modes.
298 int gr_set_drawmode(int mode);
299
300 // Sets the color in the current canvas.  should be a macro
301 // Use: gr_setcolor(int color);
302 void gr_setcolor(int color);
303
304 // Draw a polygon into the current canvas in the current color and drawmode.
305 // verts points to an ordered list of x,y pairs.  the polygon should be
306 // convex; a concave polygon will be handled in some reasonable manner,
307 // but not necessarily shaded as a concave polygon. It shouldn't hang.
308 // probably good solution is to shade from minx to maxx on each scan line.
309 // int should really be fix
310 int gr_poly(int nverts,int *verts);
311 int gr_upoly(int nverts,int *verts);
312
313
314 // Draws a point into the current canvas in the current color and drawmode.
315 void gr_pixel(int x,int y);
316 void gr_upixel(int x,int y);
317
318 // Gets a pixel;
319 unsigned char gr_gpixel( grs_bitmap * bitmap, int x, int y );
320 unsigned char gr_ugpixel( grs_bitmap * bitmap, int x, int y );
321
322 // Draws a line into the current canvas in the current color and drawmode.
323 int gr_line(fix x0,fix y0,fix x1,fix y1);
324 int gr_uline(fix x0,fix y0,fix x1,fix y1);
325
326 // Draws an anti-aliased line into the current canvas in the current color and drawmode.
327 int gr_aaline(fix x0,fix y0,fix x1,fix y1);
328 int gr_uaaline(fix x0,fix y0,fix x1,fix y1);
329
330 // Draw the bitmap into the current canvas at the specified location.
331 void gr_bitmap(int x,int y,grs_bitmap *bm);
332 void gr_ubitmap(int x,int y,grs_bitmap *bm);
333 void gr_bitmap_scale_to(grs_bitmap *src, grs_bitmap *dst);
334 void show_fullscr(grs_bitmap *bm);
335
336 // bitmap function with transparency
337 void gr_bitmapm( int x, int y, grs_bitmap *bm );
338 void gr_ubitmapm( int x, int y, grs_bitmap *bm );
339
340 // Draw a rectangle into the current canvas.
341 void gr_rect(int left,int top,int right,int bot);
342 void gr_urect(int left,int top,int right,int bot);
343
344 // Draw a filled circle
345 int gr_disk(fix x,fix y,fix r);
346 int gr_udisk(fix x,fix y,fix r);
347
348 // Draw an outline circle
349 int gr_circle(fix x,fix y,fix r);
350 int gr_ucircle(fix x,fix y,fix r);
351
352 // Draw an unfilled rectangle into the current canvas
353 void gr_box(int left,int top,int right,int bot);
354 void gr_ubox(int left,int top,int right,int bot);
355
356 void gr_scanline( int x1, int x2, int y );
357 void gr_uscanline( int x1, int x2, int y );
358
359
360 // Reads in a font file... current font set to this one.
361 grs_font * gr_init_font( char * fontfile );
362 void gr_close_font( grs_font * font );
363
364 //remap a font, re-reading its data & palette
365 void gr_remap_font( grs_font *font, char * fontname, char *font_data );
366
367 //remap (by re-reading) all the color fonts
368 void gr_remap_color_fonts();
369 void gr_remap_mono_fonts();
370
371 // Writes a string using current font. Returns the next column after last char.
372 void gr_set_fontcolor( int fg, int bg );
373 void gr_set_curfont( grs_font * new );
374 int gr_string(int x, int y, char *s );
375 int gr_ustring(int x, int y, char *s );
376 int gr_printf( int x, int y, char * format, ... );
377 int gr_uprintf( int x, int y, char * format, ... );
378 void gr_get_string_size(char *s, int *string_width, int *string_height, int *average_width );
379
380
381 //  From roller.c
382 void rotate_bitmap(grs_bitmap *bp, grs_point *vertbuf, int light_value);
383
384 // From scale.c
385 void scale_bitmap(grs_bitmap *bp, grs_point *vertbuf, int orientation );
386
387 //===========================================================================
388 // Global variables
389 extern grs_canvas *grd_curcanv;             //active canvas
390 extern grs_screen *grd_curscreen;           //active screen
391 extern unsigned char Test_bitmap_data[64*64];
392
393 //shortcut to look at current font
394 #define grd_curfont grd_curcanv->cv_font
395
396 extern int gr_renderstats;
397
398 extern unsigned int FixDivide( unsigned int x, unsigned int y );
399
400 extern void gr_show_canvas( grs_canvas *canv );
401 extern void gr_set_current_canvas( grs_canvas *canv );
402
403 //flags for fonts
404 #define FT_COLOR        1
405 #define FT_PROPORTIONAL 2
406 #define FT_KERNED       4
407
408 extern void gr_vesa_update( grs_bitmap * source1, grs_bitmap * dest, grs_bitmap * source2 );
409
410 // Special effects
411 extern void gr_snow_out(int num_dots);
412
413 extern void test_rotate_bitmap(void);
414 extern void rotate_bitmap(grs_bitmap *bp, grs_point *vertbuf, int light_value);
415
416 extern ubyte gr_palette[256*3];
417 extern ubyte gr_fade_table[256*GR_FADE_LEVELS];
418 extern ubyte gr_inverse_table[32*32*32];
419
420 extern ushort gr_palette_selector;
421 extern ushort gr_inverse_table_selector;
422 extern ushort gr_fade_table_selector;
423
424 // Remaps a bitmap into the current palette. If transparent_color is
425 // between 0 and 255 then all occurances of that color are mapped to
426 // whatever color the 2d uses for transparency. This is normally used
427 // right after a call to iff_read_bitmap like this:
428 //              iff_error = iff_read_bitmap(filename,new,BM_LINEAR,newpal);
429 //              if (iff_error != IFF_NO_ERROR) Error("Can't load IFF file <%s>, error=%d",filename,iff_error);
430 //              if ( iff_has_transparency )
431 //                      gr_remap_bitmap( new, newpal, iff_transparent_color );
432 //              else
433 //                      gr_remap_bitmap( new, newpal, -1 );
434 extern void gr_remap_bitmap( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color );
435
436 // Same as above, but searches using gr_find_closest_color which uses
437 // 18-bit accurracy instead of 15bit when translating colors.
438 extern void gr_remap_bitmap_good( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color );
439
440 extern void build_colormap_good( ubyte * palette, ubyte * colormap, int * freq );
441
442 extern void gr_palette_step_up( int r, int g, int b );
443
444 extern void gr_bitmap_check_transparency( grs_bitmap * bmp );
445
446 // Allocates a selector that has a base address at 'address' and length 'size'.
447 // Returns 0 if successful... BE SURE TO CHECK the return value since there
448 // is a limited number of selectors available!!!
449 extern int get_selector( void * address, int size, unsigned int * selector );
450
451 // Assigns a selector to a bitmap. Returns 0 if successful.  BE SURE TO CHECK
452 // this return value since there is a limited number of selectors!!!!!!!
453 extern int gr_bitmap_assign_selector( grs_bitmap * bmp );
454
455 //#define GR_GETCOLOR(r,g,b) (gr_inverse_table[( (((r)&31)<<10) | (((g)&31)<<5) | ((b)&31) )])
456 //#define gr_getcolor(r,g,b) (gr_inverse_table[( (((r)&31)<<10) | (((g)&31)<<5) | ((b)&31) )])
457 //#define BM_XRGB(r,g,b) (gr_inverse_table[( (((r)&31)<<10) | (((g)&31)<<5) | ((b)&31) )])
458
459 #define BM_RGB(r,g,b) ( (((r)&31)<<10) | (((g)&31)<<5) | ((b)&31) )
460 #define BM_XRGB(r,g,b) gr_find_closest_color( (r)*2,(g)*2,(b)*2 )
461 #define GR_GETCOLOR(r,g,b) gr_find_closest_color( (r)*2,(g)*2,(b)*2 )
462 #define gr_getcolor(r,g,b) gr_find_closest_color( (r)*2,(g)*2,(b)*2 )
463
464 // Given: r,g,b, each in range of 0-63, return the color index that
465 // best matches the input.
466 int gr_find_closest_color( int r, int g, int b );
467 int gr_find_closest_color_15bpp( int rgb );
468
469 extern void gr_merge_textures( ubyte * lower, ubyte * upper, ubyte * dest );
470 extern void gr_merge_textures_1( ubyte * lower, ubyte * upper, ubyte * dest );
471 extern void gr_merge_textures_2( ubyte * lower, ubyte * upper, ubyte * dest );
472 extern void gr_merge_textures_3( ubyte * lower, ubyte * upper, ubyte * dest );
473
474 extern void gr_update(void);
475
476 /*
477  * currently SDL and OGL are the only things that supports toggling
478  * fullscreen.  otherwise add other checks to the #if -MPM
479  */
480 #if (defined(SDL_VIDEO) || defined(OGL))
481 #define GR_SUPPORTS_FULLSCREEN_TOGGLE
482
483 /*
484  * must return 0 if windowed, 1 if fullscreen
485  */
486 int gr_check_fullscreen(void);
487
488 /*
489  * returns state after toggling (ie, same as if you had called
490  * check_fullscreen immediatly after)
491  */
492 int gr_toggle_fullscreen(void);
493
494 #endif /* defined(SDL_VIDEO) || defined(OGL)) */
495
496 /* currently only OGL can toggle in the menus, because its screen data
497  * is not used (and stays in the same place).  whereas software modes,
498  * toggling fullscreen would very likely make the data buffer point to
499  * a different location, and all the subbitmaps of it would have
500  * invalid addresses in them. */
501 #ifdef OGL
502 #define GR_SUPPORTS_FULLSCREEN_MENU_TOGGLE
503 #endif
504 int gr_toggle_fullscreen_menu(void);//returns state after toggling (ie, same as if you had called check_fullscreen immediatly after)
505
506 #endif /* def _GR_H */