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