]> icculus.org git repositories - btb/d2x.git/blob - include/gr.h
Redefined lotsa variables, functions and parameters as const and/or static - patch...
[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) ((uint16_t)(m>>16))
99 #define SM_H(m) ((uint16_t)(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 #include "../2d/bitmap.h"
174 #include "grdef.h"
175 #include "ibitblt.h"
176 #include "palette.h"
177 #include "pcx.h"
178 #include "rle.h"
179
180
181 //=========================================================================
182 // System functions:
183 // setup and set mode. this creates a grs_screen structure and sets
184 // grd_curscreen to point to it.  grs_curcanv points to this screen's
185 // canvas.  Saves the current VGA state and screen mode.
186
187 int gr_init(void);
188
189 // This function sets up the main screen.  It should be called whenever
190 // the video mode changes.
191 int gr_init_screen(int mode, int w, int h, int x, int y, int rowsize, ubyte *data);
192
193 // These 4 functions actuall change screen colors.
194
195 extern void gr_pal_fade_out(unsigned char * pal);
196 extern void gr_pal_fade_in(unsigned char * pal);
197 extern void gr_pal_clear(void);
198 extern void gr_pal_setblock( int start, int number, unsigned char * pal );
199 extern void gr_pal_getblock( int start, int number, unsigned char * pal );
200
201
202 extern unsigned char *gr_video_memory;
203                                                     // All graphic modules will define this value.
204
205 //shut down the 2d.  Restore the screen mode.
206 void gr_close(void);
207
208 //=========================================================================
209 // Canvas functions:
210
211 // Makes a new canvas. allocates memory for the canvas and its bitmap,
212 // including the raw pixel buffer.
213
214 grs_canvas *gr_create_canvas(int w, int h);
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 // Creates a bitmap which is part of another bitmap
253 grs_bitmap *gr_create_sub_bitmap(grs_bitmap *bm,int x,int y,int w, int h);
254
255 // Free the bitmap and its pixel data
256 void gr_free_bitmap(grs_bitmap *bm);
257
258 // Free the bitmap's data
259 void gr_free_bitmap_data (grs_bitmap *bm);
260 void gr_init_bitmap_data (grs_bitmap *bm);
261
262 // Free the bitmap, but not the pixel data buffer
263 void gr_free_sub_bitmap(grs_bitmap *bm);
264
265 void gr_bm_pixel( grs_bitmap * bm, int x, int y, unsigned char color );
266 void gr_bm_upixel( grs_bitmap * bm, int x, int y, unsigned char color );
267 void gr_bm_bitblt(int w, int h, int dx, int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest);
268 void gr_bm_ubitblt( int w, int h, int dx, int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest);
269 void gr_bm_ubitbltm(int w, int h, int dx, int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest);
270
271 void gr_update_buffer( void * sbuf1, void * sbuf2, void * dbuf, int size );
272
273 void gr_set_transparent(grs_bitmap *pbm, int bTransparent);
274
275 //=========================================================================
276 // Color functions:
277
278 // When this function is called, the guns are set to gr_palette, and
279 // the palette stays the same until gr_close is called
280
281 void gr_use_palette_table(char * filename );
282 void gr_copy_palette(ubyte *gr_palette, ubyte *pal, int size);
283
284 //=========================================================================
285 // Drawing functions:
286
287 // For solid, XOR, or other fill modes.
288 int gr_set_drawmode(int mode);
289
290 // Sets the color in the current canvas.  should be a macro
291 // Use: gr_setcolor(int color);
292 void gr_setcolor(int color);
293
294 // Draw a polygon into the current canvas in the current color and drawmode.
295 // verts points to an ordered list of x,y pairs.  the polygon should be
296 // convex; a concave polygon will be handled in some reasonable manner,
297 // but not necessarily shaded as a concave polygon. It shouldn't hang.
298 // probably good solution is to shade from minx to maxx on each scan line.
299 // int should really be fix
300 int gr_poly(int nverts,int *verts);
301 int gr_upoly(int nverts,int *verts);
302
303
304 // Draws a point into the current canvas in the current color and drawmode.
305 void gr_pixel(int x,int y);
306 void gr_upixel(int x,int y);
307
308 // Gets a pixel;
309 unsigned char gr_gpixel( grs_bitmap * bitmap, int x, int y );
310 unsigned char gr_ugpixel( grs_bitmap * bitmap, int x, int y );
311
312 // Draws a line into the current canvas in the current color and drawmode.
313 int gr_line(fix x0,fix y0,fix x1,fix y1);
314 int gr_uline(fix x0,fix y0,fix x1,fix y1);
315
316 // Draws an anti-aliased line into the current canvas in the current color and drawmode.
317 int gr_aaline(fix x0,fix y0,fix x1,fix y1);
318 int gr_uaaline(fix x0,fix y0,fix x1,fix y1);
319
320 // Draw the bitmap into the current canvas at the specified location.
321 void gr_bitmap(int x,int y,grs_bitmap *bm);
322 void gr_ubitmap(int x,int y,grs_bitmap *bm);
323 void gr_bitmap_scale_line(unsigned char *in, unsigned char *out, int ilen, int olen);
324 void gr_bitmap_scale_to(grs_bitmap *src, grs_bitmap *dst);
325 void gr_bitmap_fullscr(grs_bitmap *bm);
326
327 // bitmap function with transparency
328 void gr_bitmapm( int x, int y, grs_bitmap *bm );
329 void gr_ubitmapm( int x, int y, grs_bitmap *bm );
330
331 // Draw a rectangle into the current canvas.
332 void gr_rect(int left,int top,int right,int bot);
333 void gr_urect(int left,int top,int right,int bot);
334
335 // Draw a filled circle
336 int gr_disk(fix x,fix y,fix r);
337 int gr_udisk(fix x,fix y,fix r);
338
339 // Draw an outline circle
340 int gr_circle(fix x,fix y,fix r);
341 int gr_ucircle(fix x,fix y,fix r);
342
343 // Draw an unfilled rectangle into the current canvas
344 void gr_box(int left,int top,int right,int bot);
345 void gr_ubox(int left,int top,int right,int bot);
346
347 void gr_scanline( int x1, int x2, int y );
348 void gr_uscanline( int x1, int x2, int y );
349
350
351 // Reads in a font file... current font set to this one.
352 grs_font * gr_init_font( const char * fontfile );
353 void gr_close_font( grs_font * font );
354
355 //remap a font, re-reading its data & palette
356 void gr_remap_font( grs_font *font, char * fontname, char *font_data );
357
358 //remap (by re-reading) all the color fonts
359 void gr_remap_color_fonts();
360 void gr_remap_mono_fonts();
361
362 // Writes a string using current font. Returns the next column after last char.
363 void gr_set_fontcolor( int fg, int bg );
364 void gr_set_curfont( grs_font * new );
365 int gr_string(int x, int y, const char *s );
366 int gr_ustring(int x, int y, const char *s );
367 int gr_printf( int x, int y, const char * format, ... );
368 int gr_uprintf( int x, int y, const char * format, ... );
369 void gr_get_string_size(const char *s, int *string_width, int *string_height, int *average_width );
370
371
372 //  From roller.c
373 void rotate_bitmap(grs_bitmap *bp, grs_point *vertbuf, int light_value);
374
375 // From scale.c
376 void scale_bitmap(grs_bitmap *bp, grs_point *vertbuf, int orientation );
377
378 //===========================================================================
379 // Global variables
380 extern grs_canvas *grd_curcanv;             //active canvas
381 extern grs_screen *grd_curscreen;           //active screen
382 extern unsigned char Test_bitmap_data[64*64];
383
384 //shortcut to look at current font
385 #define grd_curfont grd_curcanv->cv_font
386
387 extern int gr_renderstats;
388
389 extern unsigned int FixDivide( unsigned int x, unsigned int y );
390
391 extern void gr_show_canvas( grs_canvas *canv );
392 extern void gr_set_current_canvas( grs_canvas *canv );
393
394 //flags for fonts
395 #define FT_COLOR        1
396 #define FT_PROPORTIONAL 2
397 #define FT_KERNED       4
398
399 extern void gr_vesa_update( grs_bitmap * source1, grs_bitmap * dest, grs_bitmap * source2 );
400
401 // Special effects
402 extern void gr_snow_out(int num_dots);
403
404 extern void test_rotate_bitmap(void);
405 extern void rotate_bitmap(grs_bitmap *bp, grs_point *vertbuf, int light_value);
406
407 extern ubyte gr_palette[256*3];
408 extern ubyte gr_fade_table[256*GR_FADE_LEVELS];
409 extern ubyte gr_inverse_table[32*32*32];
410
411 extern ushort gr_palette_selector;
412 extern ushort gr_inverse_table_selector;
413 extern ushort gr_fade_table_selector;
414
415 // Remaps a bitmap into the current palette. If transparent_color is
416 // between 0 and 255 then all occurances of that color are mapped to
417 // whatever color the 2d uses for transparency. This is normally used
418 // right after a call to iff_read_bitmap like this:
419 //              iff_error = iff_read_bitmap(filename,new,BM_LINEAR,newpal);
420 //              if (iff_error != IFF_NO_ERROR) Error("Can't load IFF file <%s>, error=%d",filename,iff_error);
421 //              if ( iff_has_transparency )
422 //                      gr_remap_bitmap( new, newpal, iff_transparent_color );
423 //              else
424 //                      gr_remap_bitmap( new, newpal, -1 );
425 extern void gr_remap_bitmap( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color );
426
427 // Same as above, but searches using gr_find_closest_color which uses
428 // 18-bit accurracy instead of 15bit when translating colors.
429 extern void gr_remap_bitmap_good( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color );
430
431 extern void build_colormap_good( ubyte * palette, ubyte * colormap, int * freq );
432
433 extern void gr_palette_step_up( int r, int g, int b );
434
435 extern void gr_bitmap_check_transparency( grs_bitmap * bmp );
436
437 // Allocates a selector that has a base address at 'address' and length 'size'.
438 // Returns 0 if successful... BE SURE TO CHECK the return value since there
439 // is a limited number of selectors available!!!
440 extern int get_selector( void * address, int size, unsigned int * selector );
441
442 // Assigns a selector to a bitmap. Returns 0 if successful.  BE SURE TO CHECK
443 // this return value since there is a limited number of selectors!!!!!!!
444 extern int gr_bitmap_assign_selector( grs_bitmap * bmp );
445
446 //#define GR_GETCOLOR(r,g,b) (gr_inverse_table[( (((r)&31)<<10) | (((g)&31)<<5) | ((b)&31) )])
447 //#define gr_getcolor(r,g,b) (gr_inverse_table[( (((r)&31)<<10) | (((g)&31)<<5) | ((b)&31) )])
448 //#define BM_XRGB(r,g,b) (gr_inverse_table[( (((r)&31)<<10) | (((g)&31)<<5) | ((b)&31) )])
449
450 #define BM_RGB(r,g,b) ( (((r)&31)<<10) | (((g)&31)<<5) | ((b)&31) )
451 #define BM_XRGB(r,g,b) gr_find_closest_color( (r)*2,(g)*2,(b)*2 )
452 #define GR_GETCOLOR(r,g,b) gr_find_closest_color( (r)*2,(g)*2,(b)*2 )
453 #define gr_getcolor(r,g,b) gr_find_closest_color( (r)*2,(g)*2,(b)*2 )
454
455 // Given: r,g,b, each in range of 0-63, return the color index that
456 // best matches the input.
457 int gr_find_closest_color( int r, int g, int b );
458 int gr_find_closest_color_15bpp( int rgb );
459
460 extern void gr_merge_textures( ubyte * lower, ubyte * upper, ubyte * dest );
461 extern void gr_merge_textures_1( ubyte * lower, ubyte * upper, ubyte * dest );
462 extern void gr_merge_textures_2( ubyte * lower, ubyte * upper, ubyte * dest );
463 extern void gr_merge_textures_3( ubyte * lower, ubyte * upper, ubyte * dest );
464
465
466 #endif /* def _GR_H */