]> icculus.org git repositories - btb/d2x.git/blob - include/gr.h
moved to main/
[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         int16_t         ft_w,ft_h;              // Width and height in pixels
58         int16_t         ft_flags;               // Proportional?
59         int16_t         ft_baseline;    //
60         ubyte           ft_minchar,             // The first and last chars defined by
61                                 ft_maxchar;             // This font
62         int16_t         ft_bytewidth;   // Width in unsigned chars
63         u_int32_t       ft_data;                        // Ptr to raw data.
64         u_int32_t       ft_chars;               // Ptrs to data for each char (required for prop font)
65         u_int32_t       ft_widths;              // Array of widths (required for prop font)
66         u_int32_t       ft_kerndata;    // Array of kerning triplet data
67 } __pack__ old_grs_font;
68
69 #define BM_LINEAR   0
70 #define BM_MODEX    1
71 #define BM_SVGA     2
72 #define BM_RGB15    3   //5 bits each r,g,b stored at 16 bits
73 #define BM_SVGA15   4
74 #ifdef OGL
75 #define BM_OGL      5
76 #endif
77
78 //@@//  Define these modes for Gameplay too, since the game was developed under
79 //@@//  DOS, we will adapt these modes to other systems thru rendering.
80 #define SM(w,h) ((((u_int32_t)w)<<16)+(((u_int32_t)h)&0xFFFF))
81 #define SM_W(m) (m>>16)
82 #define SM_H(m) (m&0xFFFF)
83
84 #define SM_ORIGINAL 0
85 /*
86 #define SM_ORIGINAL             -1
87 #define SM_320x200C     0
88 #define SM_320x200U     1
89 #define SM_320x240U     2
90 #define SM_360x200U     3
91 #define SM_360x240U     4
92 #define SM_376x282U     5
93 #define SM_320x400U     6
94 #define SM_320x480U     7
95 #define SM_360x400U     8
96 #define SM_360x480U     9
97 #define SM_360x360U     10
98 #define SM_376x308U     11
99 #define SM_376x564U     12
100 #define SM_640x400V     13
101 #define SM_640x480V     14
102 #define SM_800x600V     15
103 #define SM_1024x768V    16
104 #define SM_640x480V15   17
105 #define SM_800x600V15   18
106 #define SM_1280x1024V    19
107 */
108
109 #define BM_FLAG_TRANSPARENT                     1
110 #define BM_FLAG_SUPER_TRANSPARENT       2
111 #define BM_FLAG_NO_LIGHTING                     4
112 #define BM_FLAG_RLE                                     8                       // A run-length encoded bitmap.
113 #define BM_FLAG_PAGED_OUT                       16                      // This bitmap's data is paged out.
114 #define BM_FLAG_RLE_BIG                         32                      // for bitmaps that RLE to > 255 per row (i.e. cockpits)
115
116 typedef struct _grs_bitmap {
117         short       bm_x,bm_y;      // Offset from parent's origin
118         short       bm_w,bm_h;      // width,height
119         byte            bm_type;        // 0=Linear, 1=ModeX, 2=SVGA
120         byte                    bm_flags;               // bit 0 on means it has transparency.
121                                                                                 // bit 1 on means it has supertransparency
122                                                                                 // bit 2 on means it doesn't get passed through lighting.
123         short       bm_rowsize;     // unsigned char offset to next row
124         unsigned char *      bm_data;        // ptr to pixel data...
125                                                                 //   Linear = *parent+(rowsize*y+x)
126                                                                 //   ModeX = *parent+(rowsize*y+x/4)
127                                                                 //   SVGA = *parent+(rowsize*y+x)
128         unsigned short bm_handle;       //for application.  initialized to 0
129         ubyte                   avg_color;              //      Average color of all pixels in texture map.
130         byte                    unused;                 //      to 4-byte align.
131 #ifdef OGL
132         struct _ogl_texture *gltexture;
133         struct _grs_bitmap *bm_parent;
134 #endif
135
136 } grs_bitmap;
137
138 //new font structure, which does not suffer from the inability to add new items
139 typedef struct _new_grs_font {
140         short           ft_w,ft_h;              // Width and height in pixels
141         short           ft_flags;               // Proportional?
142         short           ft_baseline;    //
143         ubyte           ft_minchar,             // The first and last chars defined by
144                                 ft_maxchar;             // This font
145         short           ft_bytewidth;   // Width in unsigned chars
146         ubyte   *       ft_data;                        // Ptr to raw data.
147         ubyte   **      ft_chars;               // Ptrs to data for each char (required for prop font)
148         short   *       ft_widths;              // Array of widths (required for prop font)
149         ubyte *  ft_kerndata;   // Array of kerning triplet data
150         old_grs_font * oldfont;
151 #ifdef OGL
152         grs_bitmap *ft_bitmaps;
153         grs_bitmap ft_parent_bitmap;
154 #endif
155 } grs_font;
156
157 typedef struct _grs_canvas {
158         grs_bitmap  cv_bitmap;      // the bitmap for this canvas
159         short       cv_color;       // current color
160         short       cv_drawmode;    // fill,XOR,etc.
161         grs_font *  cv_font;        // the currently selected font
162         short       cv_font_fg_color;   // current font foreground color (-1==Invisible)
163         short       cv_font_bg_color;   // current font background color (-1==Invisible)
164 } grs_canvas;
165
166 //shortcuts
167 #define cv_w cv_bitmap.bm_w
168 #define cv_h cv_bitmap.bm_h
169
170 typedef struct _grs_screen {     // This is a video screen
171         grs_canvas  sc_canvas;      // Represents the entire screen
172         int       sc_mode;        // Video mode number
173         short       sc_w, sc_h;     // Actual Width and Height
174         fix                     sc_aspect;              //aspect ratio (w/h) for this screen
175 } grs_screen;
176
177
178 //=========================================================================
179 // System functions:
180 // setup and set mode. this creates a grs_screen structure and sets
181 // grd_curscreen to point to it.  grs_curcanv points to this screen's
182 // canvas.  Saves the current VGA state and screen mode.
183
184 int gr_init(void);
185
186 // This function sets up the main screen.  It should be called whenever
187 // the video mode changes.
188 int gr_init_screen(int mode, int w, int h, int x, int y, int rowsize, ubyte *data);
189
190 int gr_check_mode(u_int32_t mode);
191 int gr_set_mode(u_int32_t mode);
192
193
194 // These 4 functions actuall change screen colors.
195
196 extern void gr_pal_fade_out(unsigned char * pal);
197 extern void gr_pal_fade_in(unsigned char * pal);
198 extern void gr_pal_clear(void);
199 extern void gr_pal_setblock( int start, int number, unsigned char * pal );
200 extern void gr_pal_getblock( int start, int number, unsigned char * pal );
201
202
203 extern unsigned char *gr_video_memory;
204                                                                                                 // All graphic modules will define this value.
205
206 //shut down the 2d.  Restore the screen mode.
207 void gr_close(void);
208
209 //=========================================================================
210 // Canvas functions:
211
212 // Makes a new canvas. allocates memory for the canvas and its bitmap,
213 // including the raw pixel buffer.
214
215 grs_canvas *gr_create_canvas(int w, int h);
216 #if defined(POLY_ACC)
217 grs_canvas *gr_create_canvas2(int w, int h, int type);
218 #endif
219
220 // Creates a canvas that is part of another canvas.  this can be used to make
221 // a window on the screen.  the canvas structure is malloc'd; the address of
222 // the raw pixel data is inherited from the parent canvas.
223
224 grs_canvas *gr_create_sub_canvas(grs_canvas *canv,int x,int y,int w, int h);
225
226 // Initialize the specified canvas. the raw pixel data buffer is passed as
227 // a parameter. no memory allocation is performed.
228
229 void gr_init_canvas(grs_canvas *canv,unsigned char *pixdata,int pixtype, int w,int h);
230
231 // Initialize the specified sub canvas. no memory allocation is performed.
232
233 void gr_init_sub_canvas(grs_canvas *new,grs_canvas *src,int x,int y,int w, int h);
234
235 // Free up the canvas and its pixel data.
236
237 void gr_free_canvas(grs_canvas *canv);
238
239 // Free up the canvas. do not free the pixel data, which belongs to the
240 // parent canvas.
241
242 void gr_free_sub_canvas(grs_canvas *canv);
243
244 // Clear the current canvas to the specified color
245 void gr_clear_canvas(int color);
246
247 //=========================================================================
248 // Bitmap functions:
249
250 // Allocate a bitmap and its pixel data buffer.
251 grs_bitmap *gr_create_bitmap(int w,int h);
252
253 // Allocated a bitmap and makes its data be raw_data that is already somewhere.
254 grs_bitmap *gr_create_bitmap_raw(int w, int h, unsigned char * raw_data );
255
256 #if defined(POLY_ACC)
257 // Allocates a bitmap of a specific type. data is either NULL or raw data.
258 grs_bitmap *gr_create_bitmap2(int w, int h, int type, void *data );
259 #endif
260
261 // Creates a bitmap which is part of another bitmap
262 grs_bitmap *gr_create_sub_bitmap(grs_bitmap *bm,int x,int y,int w, int h);
263
264 // Free the bitmap and its pixel data
265 void gr_free_bitmap(grs_bitmap *bm);
266
267 // Free the bitmap's data
268 void gr_free_bitmap_data (grs_bitmap *bm);
269 void gr_init_bitmap_data (grs_bitmap *bm);
270
271 // Free the bitmap, but not the pixel data buffer
272 void gr_free_sub_bitmap(grs_bitmap *bm);
273
274 void gr_bm_pixel( grs_bitmap * bm, int x, int y, unsigned char color );
275 void gr_bm_upixel( grs_bitmap * bm, int x, int y, unsigned char color );
276 void gr_bm_ubitblt( int w, int h, int dx, int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest);
277 void gr_bm_ubitbltm(int w, int h, int dx, int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest);
278
279 #ifdef MACINTOSH
280 void gr_bm_ubitblt_double(int w, int h, int dx, int dy, int sx, int sy, grs_bitmap *src, grs_bitmap *dest);
281 void gr_linear_movsd_double(ubyte *src, ubyte *dest, int num_pixels);
282 #endif
283
284 void gr_update_buffer( void * sbuf1, void * sbuf2, void * dbuf, int size );
285
286 //=========================================================================
287 // Color functions:
288
289 // When this function is called, the guns are set to gr_palette, and
290 // the palette stays the same until gr_close is called
291
292 void gr_use_palette_table(char * filename );
293 void gr_copy_palette(ubyte *gr_palette, ubyte *pal, int size);
294
295 //=========================================================================
296 // Drawing functions:
297
298 // For solid, XOR, or other fill modes.
299 int gr_set_drawmode(int mode);
300
301 // Sets the color in the current canvas.  should be a macro
302 // Use: gr_setcolor(int color);
303 void gr_setcolor(int color);
304
305 // Draw a polygon into the current canvas in the current color and drawmode.
306 // verts points to an ordered list of x,y pairs.  the polygon should be
307 // convex; a concave polygon will be handled in some reasonable manner,
308 // but not necessarily shaded as a concave polygon. It shouldn't hang.
309 // probably good solution is to shade from minx to maxx on each scan line.
310 // int should really be fix
311 int gr_poly(int nverts,int *verts);
312 int gr_upoly(int nverts,int *verts);
313
314
315 // Draws a point into the current canvas in the current color and drawmode.
316 void gr_pixel(int x,int y);
317 void gr_upixel(int x,int y);
318
319 // Gets a pixel;
320 unsigned char gr_gpixel( grs_bitmap * bitmap, int x, int y );
321 unsigned char gr_ugpixel( grs_bitmap * bitmap, int x, int y );
322
323 // Draws a line into the current canvas in the current color and drawmode.
324 int gr_line(fix x0,fix y0,fix x1,fix y1);
325 int gr_uline(fix x0,fix y0,fix x1,fix y1);
326
327 // Draws an anti-aliased line into the current canvas in the current color and drawmode.
328 int gr_aaline(fix x0,fix y0,fix x1,fix y1);
329 int gr_uaaline(fix x0,fix y0,fix x1,fix y1);
330
331 // Draw the bitmap into the current canvas at the specified location.
332 void gr_bitmap(int x,int y,grs_bitmap *bm);
333 void gr_ubitmap(int x,int y,grs_bitmap *bm);
334 void gr_bitmap_scale_to(grs_bitmap *src, grs_bitmap *dst);
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 );
366
367 //remap (by re-reading) all the color fonts
368 void gr_remap_color_fonts();
369
370 // Writes a string using current font. Returns the next column after last char.
371 void gr_set_fontcolor( int fg, int bg );
372 void gr_set_curfont( grs_font * new );
373 int gr_string(int x, int y, char *s );
374 int gr_ustring(int x, int y, char *s );
375 int gr_printf( int x, int y, char * format, ... );
376 int gr_uprintf( int x, int y, char * format, ... );
377 void gr_get_string_size(char *s, int *string_width, int *string_height, int *average_width );
378
379
380 //      From roller.c
381 void rotate_bitmap(grs_bitmap *bp, grs_point *vertbuf, int light_value);
382
383 // From scale.c
384 void scale_bitmap(grs_bitmap *bp, grs_point *vertbuf, int orientation );
385
386 //===========================================================================
387 // Global variables
388 extern grs_canvas *grd_curcanv;             //active canvas
389 extern grs_screen *grd_curscreen;           //active screen
390 extern unsigned char Test_bitmap_data[64*64];
391
392 //shortcut to look at current font
393 #define grd_curfont grd_curcanv->cv_font
394
395 extern unsigned int FixDivide( unsigned int x, unsigned int y );
396
397 extern void gr_show_canvas( grs_canvas *canv );
398 extern void gr_set_current_canvas( grs_canvas *canv );
399
400 //flags for fonts
401 #define FT_COLOR                        1
402 #define FT_PROPORTIONAL 2
403 #define FT_KERNED                       4
404
405 extern void gr_vesa_update( grs_bitmap * source1, grs_bitmap * dest, grs_bitmap * source2 );
406
407 // Special effects
408 extern void gr_snow_out(int num_dots);
409
410 extern void test_rotate_bitmap(void);
411 extern void rotate_bitmap(grs_bitmap *bp, grs_point *vertbuf, int light_value);
412
413 extern ubyte gr_palette[256*3];
414 extern ubyte gr_fade_table[256*GR_FADE_LEVELS];
415 extern ubyte gr_inverse_table[32*32*32];
416
417 extern ushort gr_palette_selector;
418 extern ushort gr_inverse_table_selector;
419 extern ushort gr_fade_table_selector;
420
421 // Remaps a bitmap into the current palette. If transparent_color is between 0 and 255
422 // then all occurances of that color are mapped to whatever color the 2d uses for
423 // transparency. This is normally used right after a call to iff_read_bitmap like
424 // this:
425 //              iff_error = iff_read_bitmap(filename,new,BM_LINEAR,newpal);
426 //              if (iff_error != IFF_NO_ERROR) Error("Can't load IFF file <%s>, error=%d",filename,iff_error);
427 //              if ( iff_has_transparency )
428 //                      gr_remap_bitmap( new, newpal, iff_transparent_color );
429 //              else
430 //                      gr_remap_bitmap( new, newpal, -1 );
431 extern void gr_remap_bitmap( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color );
432
433 // Same as above, but searches using gr_find_closest_color which uses 18-bit accurracy
434 // instaed of 15bit when translating colors.
435 extern void gr_remap_bitmap_good( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color );
436
437 extern void gr_palette_step_up( int r, int g, int b );
438
439 extern void gr_bitmap_check_transparency( grs_bitmap * bmp );
440
441 // Allocates a selector that has a base address at 'address' and length 'size'.
442 // Returns 0 if successful... BE SURE TO CHECK the return value since there
443 // is a limited number of selectors available!!!
444 extern int get_selector( void * address, int size, unsigned int * selector );
445
446 // Assigns a selector to a bitmap. Returns 0 if successful.  BE SURE TO CHECK
447 // this return value since there is a limited number of selectors!!!!!!!
448 extern int gr_bitmap_assign_selector( grs_bitmap * bmp );
449
450 //#define GR_GETCOLOR(r,g,b) (gr_inverse_table[( (((r)&31)<<10) | (((g)&31)<<5) | ((b)&31) )])
451 //#define gr_getcolor(r,g,b) (gr_inverse_table[( (((r)&31)<<10) | (((g)&31)<<5) | ((b)&31) )])
452 //#define BM_XRGB(r,g,b) (gr_inverse_table[( (((r)&31)<<10) | (((g)&31)<<5) | ((b)&31) )])
453
454 #define BM_RGB(r,g,b) ( (((r)&31)<<10) | (((g)&31)<<5) | ((b)&31) )
455 #define BM_XRGB(r,g,b) gr_find_closest_color( (r)*2,(g)*2,(b)*2 )
456 #define GR_GETCOLOR(r,g,b) gr_find_closest_color( (r)*2,(g)*2,(b)*2 )
457 #define gr_getcolor(r,g,b) gr_find_closest_color( (r)*2,(g)*2,(b)*2 )
458
459 // Given: r,g,b, each in range of 0-63, return the color index that
460 // best matches the input.
461 int gr_find_closest_color( int r, int g, int b );
462 int gr_find_closest_color_15bpp( int rgb );
463
464 extern void gr_merge_textures( ubyte * lower, ubyte * upper, ubyte * dest );
465 extern void gr_merge_textures_1( ubyte * lower, ubyte * upper, ubyte * dest );
466 extern void gr_merge_textures_2( ubyte * lower, ubyte * upper, ubyte * dest );
467 extern void gr_merge_textures_3( ubyte * lower, ubyte * upper, ubyte * dest );
468
469 extern void gr_update(void);
470
471 /*
472  * currently SDL and OGL are the only things that supports toggling
473  * fullscreen.  otherwise add other checks to the #if -MPM
474  */
475 #if (defined(SDL_VIDEO) || defined(OGL))
476 #define GR_SUPPORTS_FULLSCREEN_TOGGLE
477
478 /*
479  * must return 0 if windowed, 1 if fullscreen
480  */
481 int gr_check_fullscreen(void);
482
483 /*
484  * returns state after toggling (ie, same as if you had called
485  * check_fullscreen immediatly after)
486  */
487 int gr_toggle_fullscreen(void);
488
489 #endif
490
491 int gr_toggle_fullscreen_menu(void);//returns state after toggling (ie, same as if you had called check_fullscreen immediatly after)
492
493 #endif