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