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