1 #pragma once
2 
3 //********************************************************************************************
4 //*
5 //*    This file is part of Egoboo.
6 //*
7 //*    Egoboo is free software: you can redistribute it and/or modify it
8 //*    under the terms of the GNU General Public License as published by
9 //*    the Free Software Foundation, either version 3 of the License, or
10 //*    (at your option) any later version.
11 //*
12 //*    Egoboo is distributed in the hope that it will be useful, but
13 //*    WITHOUT ANY WARRANTY; without even the implied warranty of
14 //*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 //*    General Public License for more details.
16 //*
17 //*    You should have received a copy of the GNU General Public License
18 //*    along with Egoboo.  If not, see <http://www.gnu.org/licenses/>.
19 //*
20 //********************************************************************************************
21 
22 #include "egoboo_typedef.h"
23 #include "extensions/ogl_texture.h"
24 #include "file_formats/module_file.h"
25 #include "mesh.h"
26 #include "mad.h"
27 
28 #include "egoboo.h"
29 
30 #include <SDL.h>
31 
32 //--------------------------------------------------------------------------------------------
33 //--------------------------------------------------------------------------------------------
34 struct s_chr;
35 struct s_camera;
36 struct s_egoboo_config;
37 struct s_chr_instance;
38 struct s_oglx_texture_parameters;
39 struct s_egoboo_config;
40 struct Font;
41 
42 //--------------------------------------------------------------------------------------------
43 //--------------------------------------------------------------------------------------------
44 
45 /// special return values
46 enum e_gfx_rv
47 {
48     gfx_error   = -1,
49     gfx_fail    = bfalse,
50     gfx_success = btrue
51 };
52 
53 typedef enum e_gfx_rv gfx_rv;
54 
55 #define GFX_ERROR_MAX 256
56 
57 struct s_gfx_error_state
58 {
59     const char * file;
60     const char * function;
61     int          line;
62 
63     int          type;
64     const char * string;
65 };
66 typedef struct s_gfx_error_state gfx_error_state_t;
67 
68 #define GFX_ERROR_STATE_INIT { "UNKNOWN", "UNKNOWN", -1, -1, "NONE" }
69 
70 struct s_gfx_error_stack
71 {
72     size_t count;
73     gfx_error_state_t lst[GFX_ERROR_MAX];
74 };
75 typedef struct s_gfx_error_stack gfx_error_stack_t;
76 
77 #define GFX_ERROR_STACK_INIT { 0, GFX_ERROR_STATE_INIT }
78 
79 egoboo_rv           gfx_error_add( const char * file, const char * function, int line, int id, const char * sz );
80 gfx_error_state_t * gfx_error_pop();
81 void                gfx_error_clear();
82 
83 //--------------------------------------------------------------------------------------------
84 //--------------------------------------------------------------------------------------------
85 #define DOLIST_SIZE (MAX_CHR + MAX_PRT)
86 
87 #define MAXMESHRENDER             1024                       ///< Max number of tiles to draw
88 
89 #define MAPSIZE 96
90 
91 #define TABX                            32// 16      ///< Size of little name tag on the bar
92 #define BARX                            112// 216         ///< Size of bar
93 #define BARY                            16// 8
94 #define NUMTICK                         10// 50          ///< Number of ticks per row
95 #define TICKX                           8// 4           ///< X size of each tick
96 #define MAXTICK                         (NUMTICK*10) ///< Max number of ticks to draw
97 #define XPTICK                          6.00f
98 
99 #define NUMBAR                          6               ///< Number of status bars
100 #define NUMXPBAR                        2               ///< Number of xp bars
101 
102 #define MAXLIGHTLEVEL                   16          ///< Number of premade light intensities
103 #define MAXSPEKLEVEL                    16          ///< Number of premade specularities
104 #define MAXLIGHTROTATION                256         ///< Number of premade light maps
105 
106 #define DONTFLASH                       255
107 #define SEEKURSEAND                     31          ///< Blacking flash
108 
109 extern int GFX_WIDTH;
110 extern int GFX_HEIGHT;
111 
112 //#define GFX_WIDTH                       800         ///< 640
113 //#define GFX_HEIGHT                      600         ///< 480
114 
115 #define SHADOWRAISE                       5
116 
117 /// The supported colors of bars and blips
118 enum e_color
119 {
120     COLOR_WHITE = 0,
121     COLOR_RED,
122     COLOR_YELLOW,
123     COLOR_GREEN,
124     COLOR_BLUE,
125     COLOR_PURPLE,
126     COLOR_MAX
127 };
128 
129 //--------------------------------------------------------------------------------------------
130 //--------------------------------------------------------------------------------------------
131 
132 /// An element of the do-list, an all encompassing list of all objects to be drawn by the renderer
133 struct s_do_list_data
134 {
135     float   dist;
136     CHR_REF chr;
137 };
138 typedef struct s_do_list_data do_list_data_t;
139 
140 //--------------------------------------------------------------------------------------------
141 
142 /// Structure for sorting both particles and characters based on their position from the camera
143 struct s_obj_registry_entity
144 {
145     CHR_REF ichr;
146     PRT_REF iprt;
147     float   dist;
148 };
149 typedef struct s_obj_registry_entity obj_registry_entity_t;
150 
151 #define OBJ_REGISTRY_ENTITY_INIT { MAX_CHR, MAX_PRT, 0.0f }
152 
153 int obj_registry_entity_cmp( const void * pleft, const void * pright );
154 
155 //--------------------------------------------------------------------------------------------
156 //--------------------------------------------------------------------------------------------
157 
158 /// OPENGL VERTEX
159 struct s_GLvertex
160 {
161     GLfloat pos[4];
162     GLfloat nrm[3];
163     GLfloat env[2];
164     GLfloat tex[2];
165 
166     GLfloat col[4];      ///< generic per-vertex lighting
167     GLint   color_dir;   ///< "optimized" per-vertex directional lighting
168 };
169 typedef struct s_GLvertex GLvertex;
170 
171 //--------------------------------------------------------------------------------------------
172 //--------------------------------------------------------------------------------------------
173 
174 // Which tiles are to be drawn, arranged by MPDFX_* bits
175 struct s_renderlist
176 {
177     ego_mpd_t * pmesh;
178 
179     int     all_count;                               ///< Number to render, total
180     int     ref_count;                               ///< ..., is reflected in the floor
181     int     sha_count;                               ///< ..., is not reflected in the floor
182     int     drf_count;                               ///< ..., draws character reflections
183     int     ndr_count;                               ///< ..., draws no character reflections
184     int     wat_count;                               ///< ..., draws no character reflections
185 
186     Uint32  all[MAXMESHRENDER];                      ///< List of which to render, total
187 
188     Uint32  ref[MAXMESHRENDER];                      ///< ..., is reflected in the floor
189     Uint32  sha[MAXMESHRENDER];                      ///< ..., is not reflected in the floor
190 
191     Uint32  drf[MAXMESHRENDER];                      ///< ..., draws character reflections
192     Uint32  ndr[MAXMESHRENDER];                      ///< ..., draws no character reflections
193 
194     Uint32  wat[MAXMESHRENDER];                      ///< ..., draws a water tile
195 };
196 typedef struct s_renderlist renderlist_t;
197 
198 #define RENDERLIST_INIT   \
199     { \
200         NULL,           /* pmesh */  \
201         0,              /* all_count */  \
202         0,              /* ref_count */  \
203         0,              /* sha_count */  \
204         0,              /* drf_count */  \
205         0,              /* ndr_count */  \
206         0,              /* wat_count */  \
207         {INVALID_TILE}, /* all[MAXMESHRENDER] */  \
208         {INVALID_TILE}, /* ref[MAXMESHRENDER] */  \
209         {INVALID_TILE}, /* sha[MAXMESHRENDER] */  \
210         {INVALID_TILE}, /* drf[MAXMESHRENDER] */  \
211         {INVALID_TILE}, /* ndr[MAXMESHRENDER] */  \
212         {INVALID_TILE}  /* wat[MAXMESHRENDER] */  \
213     }
214 
215 extern renderlist_t renderlist;
216 
217 //--------------------------------------------------------------------------------------------
218 //extern Uint8           lightdirectionlookup[65536];                        ///< For lighting characters
219 //extern float           lighttable_local[MAXLIGHTROTATION][EGO_NORMAL_COUNT];
220 //extern float           lighttable_global[MAXLIGHTROTATION][EGO_NORMAL_COUNT];
221 extern float           indextoenvirox[EGO_NORMAL_COUNT];                    ///< Environment map
222 extern float           lighttoenviroy[256];                                ///< Environment map
223 //extern Uint32          lighttospek[MAXSPEKLEVEL][256];
224 
225 //--------------------------------------------------------------------------------------------
226 // Display messages
227 extern int    msgtimechange;
228 
229 /// A display messages
230 struct s_msg
231 {
232     int             time;                            ///< The time for this message
233     char            textdisplay[MESSAGESIZE];        ///< The displayed text
234 };
235 typedef struct s_msg msg_t;
236 
237 DECLARE_STATIC_ARY_TYPE( DisplayMsgAry, msg_t, MAX_MESSAGE );
238 
239 DECLARE_EXTERN_STATIC_ARY( DisplayMsgAry, DisplayMsg );
240 
241 //--------------------------------------------------------------------------------------------
242 // camera optimization
243 
244 // For figuring out what to draw
245 #define CAM_ROTMESH_TOPSIDE                  50
246 #define CAM_ROTMESH_BOTTOMSIDE               50
247 #define CAM_ROTMESH_UP                       30
248 #define CAM_ROTMESH_DOWN                     30
249 
250 // The ones that get used
251 extern int rotmesh_topside;
252 extern int rotmesh_bottomside;
253 extern int rotmesh_up;
254 extern int rotmesh_down;
255 
256 //--------------------------------------------------------------------------------------------
257 // encapsulation of all graphics options
258 struct s_gfx_config
259 {
260     GLuint shading;
261     bool_t refon;
262     Uint8  reffadeor;
263     bool_t antialiasing;
264     bool_t dither;
265     bool_t perspective;
266     bool_t phongon;
267     bool_t shaon;
268     bool_t shasprite;
269 
270     bool_t clearson;          ///< Do we clear every time?
271     bool_t draw_background;   ///< Do we draw the background image?
272     bool_t draw_overlay;      ///< Draw overlay?
273     bool_t draw_water_0;      ///< Do we draw water layer 1 (TX_WATER_LOW)
274     bool_t draw_water_1;      ///< Do we draw water layer 2 (TX_WATER_TOP)
275 
276     int    dynalist_max;     ///< Max number of dynamic lights to draw
277     bool_t exploremode;       ///< fog of war mode for mesh display
278     bool_t usefaredge;        ///< Far edge maps? (Outdoor)
279 
280     // virtual window parameters
281     float vw, vh;
282     float vdw, vdh;
283 };
284 typedef struct s_gfx_config gfx_config_t;
285 
286 extern gfx_config_t gfx;
287 
288 bool_t gfx_config_init( gfx_config_t * pgfx );
289 bool_t gfx_set_virtual_screen( gfx_config_t * pgfx );
290 bool_t gfx_synch_config( gfx_config_t * pgfx, struct s_egoboo_config * pcfg );
291 
292 //--------------------------------------------------------------------------------------------
293 //--------------------------------------------------------------------------------------------
294 
295 /// Minimap stuff
296 #define MAXBLIP        128                          ///<Max blips on the screen
297 extern Uint8           mapon;
298 extern Uint8           mapvalid;
299 extern Uint8           youarehereon;
300 
301 extern size_t          blip_count;
302 extern float           blip_x[MAXBLIP];
303 extern float           blip_y[MAXBLIP];
304 extern Uint8           blip_c[MAXBLIP];
305 
306 #define BILLBOARD_COUNT     (2 * MAX_CHR)
307 #define INVALID_BILLBOARD   BILLBOARD_COUNT
308 
309 enum e_bb_opt
310 {
311     bb_opt_none          = EMPTY_BIT_FIELD,
312     bb_opt_randomize_pos = ( 1 << 0 ),      // Randomize the position of the bb to witin 1 grid
313     bb_opt_randomize_vel = ( 1 << 1 ),      // Randomize the velocity of the bb. Enough to move it by 2 tiles within its lifetime.
314     bb_opt_fade          = ( 1 << 2 ),      // Make the billboard fade out
315     bb_opt_burn          = ( 1 << 3 ),      // Make the tint fully saturate over time.
316     bb_opt_all           = FULL_BIT_FIELD   //(size_t)(~0)   Enum doesn't support unsigned integers, size_t is also unsigned.
317 };
318 
319 /// Description of a generic bilboarded object.
320 /// Any graphics that can be composited onto a SDL_surface can be used
321 struct s_billboard_data
322 {
323     bool_t    valid;        ///< has the billboard data been initialized?
324 
325     Uint32    time;         ///< the time when the billboard will expire
326     TX_REF    tex_ref;      ///< our texture index
327     fvec3_t   pos;          ///< the position of the bottom-missle of the box
328 
329     CHR_REF   ichr;         ///< the character we are attached to
330 
331     GLXvector4f tint;       ///< a color to modulate the billboard's r,g,b, and a channels
332     GLXvector4f tint_add;   ///< the change in tint per update
333 
334     GLXvector4f offset;     ///< an offset to the billboard's position in world coordinates
335     GLXvector4f offset_add; ///<
336 
337     float       size;
338     float       size_add;
339 };
340 typedef struct s_billboard_data billboard_data_t;
341 
342 billboard_data_t * billboard_data_init( billboard_data_t * pbb );
343 bool_t             billboard_data_free( billboard_data_t * pbb );
344 bool_t             billboard_data_update( billboard_data_t * pbb );
345 bool_t             billboard_data_printf_ttf( billboard_data_t * pbb, struct Font *font, SDL_Color color, const char * format, ... );
346 
347 DECLARE_LIST_EXTERN( billboard_data_t, BillboardList, BILLBOARD_COUNT );
348 
349 void               BillboardList_init_all();
350 void               BillboardList_update_all();
351 void               BillboardList_free_all();
352 size_t             BillboardList_get_free( Uint32 lifetime_secs );
353 bool_t             BillboardList_free_one( size_t ibb );
354 billboard_data_t * BillboardList_get_ptr( const BBOARD_REF  ibb );
355 
356 #define VALID_BILLBOARD_RANGE( IBB ) ( ( (IBB) >= 0 ) && ( (IBB) < BILLBOARD_COUNT ) )
357 #define VALID_BILLBOARD( IBB )       ( VALID_BILLBOARD_RANGE( IBB ) && BillboardList.lst[IBB].valid )
358 
359 //--------------------------------------------------------------------------------------------
360 //--------------------------------------------------------------------------------------------
361 // some lines to be drawn in the display
362 
363 #define LINE_COUNT 100
364 struct s_line_data
365 {
366     fvec3_t   dst;
367     fvec4_t   src, color;
368     int time;
369 };
370 typedef struct s_line_data line_data_t;
371 
372 //--------------------------------------------------------------------------------------------
373 //--------------------------------------------------------------------------------------------
374 
375 extern float time_render_scene_init;
376 extern float time_render_scene_mesh;
377 extern float time_render_scene_solid;
378 extern float time_render_scene_water;
379 extern float time_render_scene_trans;
380 
381 extern float time_render_scene_init_renderlist_make;
382 extern float time_render_scene_init_dolist_make;
383 extern float time_render_scene_init_do_grid_dynalight;
384 extern float time_render_scene_init_light_fans;
385 extern float time_render_scene_init_update_all_chr_instance;
386 extern float time_render_scene_init_update_all_prt_instance;
387 
388 extern float time_render_scene_mesh_dolist_sort;
389 extern float time_render_scene_mesh_ndr;
390 extern float time_render_scene_mesh_drf_back;
391 extern float time_render_scene_mesh_ref;
392 extern float time_render_scene_mesh_ref_chr;
393 extern float time_render_scene_mesh_drf_solid;
394 extern float time_render_scene_mesh_render_shadows;
395 
396 //--------------------------------------------------------------------------------------------
397 //--------------------------------------------------------------------------------------------
398 // Function prototypes
399 
400 void   gfx_system_begin();
401 void   gfx_system_end();
402 
403 int    ogl_init();
404 void   gfx_main();
405 void   gfx_begin_3d( struct s_camera * pcam );
406 void   gfx_end_3d();
407 bool_t gfx_synch_oglx_texture_parameters( struct s_oglx_texture_parameters * ptex, struct s_egoboo_config * pcfg );
408 void   gfx_reload_all_textures();
409 
410 void   request_clear_screen();
411 void   do_clear_screen();
412 bool_t flip_pages_requested();
413 void   request_flip_pages();
414 void   do_flip_pages();
415 
416 void  draw_one_icon( const TX_REF icontype, float x, float y, Uint8 sparkle );
417 void  draw_one_font( oglx_texture_t * ptex, int fonttype, float x, float y );
418 void  draw_map_texture( float x, float y );
419 float draw_one_bar( Uint8 bartype, float x, float y, int ticks, int maxticks );
420 float draw_string( float x, float y, const char *format, ... );
421 float draw_wrap_string( const char *szText, float x, float y, int maxx );
422 float draw_status( const CHR_REF character, float x, float y );
423 void  draw_text();
424 void  draw_one_character_icon( const CHR_REF item, float x, float y, bool_t draw_ammo );
425 void  draw_cursor();
426 void  draw_blip( float sizeFactor, Uint8 color, float x, float y, bool_t mini_map );
427 void  draw_all_lines( struct s_camera * pcam );
428 
429 void      render_world( struct s_camera * pcam );
430 void      render_shadow( const CHR_REF character );
431 void      render_bad_shadow( const CHR_REF character );
432 gfx_rv render_scene( ego_mpd_t * pmesh, struct s_camera * pcam );
433 bool_t    render_oct_bb( oct_bb_t * bb, bool_t draw_square, bool_t draw_diamond );
434 bool_t    render_aabb( aabb_t * pbbox );
435 gfx_rv render_all_billboards( struct s_camera * pcam );
436 
437 void   make_enviro();
438 void   clear_messages();
439 bool_t dump_screenshot();
440 void   make_lightdirectionlookup();
441 
442 int  DisplayMsg_get_free();
443 
444 int debug_printf( const char *format, ... );
445 
446 gfx_rv renderlist_reset( renderlist_t * prlist );
447 gfx_rv renderlist_make( renderlist_t * prlist, ego_mpd_t * pmesh, struct s_camera * pcam );
448 
449 bool_t grid_lighting_interpolate( ego_mpd_t * pmesh, lighting_cache_t * dst, float fx, float fy );
450 float  grid_lighting_test( ego_mpd_t * pmesh, GLXvector3f pos, float * low_diff, float * hgh_diff );
451 
452 int  get_free_line();
453 
454 void init_all_graphics();
455 void release_all_graphics();
456 void delete_all_graphics();
457 
458 void release_all_profile_textures();
459 
460 void   load_graphics();
461 bool_t load_blips();
462 void   load_bars();
463 void   load_map();
464 bool_t load_all_global_icons();
465 void   load_basic_textures( );
466 
467 float  get_ambient_level();
468 
469 gfx_rv flash_character( const CHR_REF character, Uint8 value );