Jump to content

  • Log In with Google      Sign In   
  • Create Account





The Z3D API

Posted by Norman Barrows, 05 September 2013 · 321 views

This is the current API for the Z3D game library. Note that this is a constantly evolving API.








// Z3D.h

#ifndef z3d_h
#define z3d_h


#include <SDKDDKVer.h>
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <stdio.h>
#include <io.h>
#include <time.h>
#include <d3dx9.h>



//####################### PUBLIC GLOBAL DEFINITIONS ##################################

#define pi D3DX_PI



//################## PUBLIC GLOBAL VARIABLES #####################################################

extern HWND Zprogram_window_handle;

extern int Zalphatestlvl;

extern int mousewheel;

// d3d font objects
extern ID3DXFont *pFont;
extern ID3DXFont *pFont2;
extern ID3DXFont *pFont3;

// d3d sprite obj
extern ID3DXSprite *Zsprite;

extern D3DPRESENT_PARAMETERS Zparams;
extern LARGE_INTEGER Ztimer_freq;

extern int Ztrace[20];

extern DWORD Znum_verts,Znum_primitives;

extern int Zcurrentmesh,Zcurrenttex;

extern int old_mousex,old_mousey;



//######################### MATERIALS #####################################


#define ZMAXMATERIALS 10

extern D3DMATERIAL9 Zmaterial[ZMAXMATERIALS];

void Zsetmaterial(int materialID);

void Zinit_materials();



//############################# MENU STRINGS ###################################

#define MaxMenuStrings 50
#define MenuStringLength 100

extern char Zmenu[MaxMenuStrings][MenuStringLength]; // menu strings
extern int LastMenuOption;

void Zgetstring(char *s,char *s2);
int Zdomenu(int mousetex);
void Zclearmenu();
void Zaddmenu(char *s);
void Znewmenu(char *s); // clear + add


//############################# SPRITES ######################################


extern ID3DXSprite *Zsprite; // d3d sprite obj

void Zbeginsprite();
void Zendsprite();
void Zdrawsprite(int texID,int x,int y,float sx,float sy); // x,y are UL corner of sprite
void Zdrawsprite2(int texID,int x,int y,float sx,float sy,float rz); // draw a rotated sprite. x,y are screen coords of the center.
void Zflushsprites();


//########################## tex DB ############################################

#define Zmaxtextures 350

struct Ztexrec
{
char name[100];
LPDIRECT3DTEXTURE9 tex;
};

extern Ztexrec Ztex[Zmaxtextures];
extern int numtextures;

void Zloadtex(char *s);
void Zloadbitmap(char *s);
void Zloadsprite(char *s);
void Zload_dynamic_bitmap(char *s);
void Zloadspritetex(char *s); // alpha transparency
void Zloadspritetex2(char *s); // alpha transparency, 5 mip lvls in dds file.
void Zloadspritetex3(char *s); // colorkey transparency
void Zunload_last_bitmap();

void Zload_textures();



//########################### mesh DB ###########################################

#define maxmeshes 300

struct Zmesh
{
IDirect3DVertexBuffer9 *vb;
IDirect3DIndexBuffer9 *ib;
int numverts,numfaces;
};


// meshDB record
struct meshrec
{
char name[100];
//LPD3DXMESH mesh;
Zmesh mesh2;
};

// mesh DB
extern meshrec mesh[maxmeshes];

void Zloadmesh(char *s,int dynamic); // dynamic = 0 or 1.

extern int DBnummeshes;

void Zload_meshes();


//######################### DRAWLIST #################################################

struct Zdrawinfo
{
int type, // 0=mesh, 1=model, 2=2d billboard, 3=3d billboard
meshID, // for models: modelID
texID, // for models: aniID
alphatest,cull,clamp,materialID,rad,cliprng;
float sx,sy,sz,x,y,z,rx,ry,rz,range; // range is rng to camera. not currently used.
D3DXMATRIX mWorld;
};

extern int Znumdrawrecs;
//extern int alphatest_numdrawrecs;

void Zcleardrawlist();
void Zdrawlist();
void Zdraw(Zdrawinfo *info);
void Zpresent();
int Zshowscene(); // 1=success 0=failure
void Zdraw2(Zdrawinfo *info);

void Zd(Zdrawinfo *b); // draw mesh, model, 2d billbaord, or 3d billboard



//############################# models ###################################################



#define maxlimbs 20



// limb record
struct limbrec
{
int parent,meshID,texID,alphatest; // parent = index of parent limb. -1=no parent.
float x,y,z,xr,yr,zr,sx,sy,sz; // location of limbs origin (in parent's coords). current orientation of limb.
float xr0,yr0,zr0; // "at rest" orientation of the limb
char name[20];
};

// model record
struct modelrec
{
int numlimbs;
limbrec limb[maxlimbs];
char name[20];
};

void Zdrawmodel(Zdrawinfo *info); // meshID=modelID. uses x,y,z,xr,yr,zr.
void Zdrawmodel2(Zdrawinfo *info); // meshID=modelID. uses mWorld.




//################################### models datatbase ##########################################



#define maxmodels 200
extern modelrec model[maxmodels];
extern int nummodels;

void loadmodel(char *s);
void savemodel(int modelID,char *s);



//####################### animations ################################################


#define maxkeyframes 20


// a 3d vector (position, orientation, etc)
struct xyzrec
{
float x,y,z;
};

// keyframe record
struct keyframerec
{
int numframes,numlimbs;
xyzrec rotation[maxlimbs];
};

// animation record
struct anirec
{
int numkeyframes;
keyframerec keyframe[maxkeyframes];
};

int startani(int aniID,int modelID);
void stopani(int playerID);
int set_animation_frame(int playerID);


// animation player record
struct aniplayerrec
{
int active,aniID,modelID,curkeyframe,curframe;
};

#define maxaniplayers 200


// animation player
extern aniplayerrec aniplayer[maxaniplayers];



//############################ animations DB ####################################

#define maxanimations 50

extern anirec animation[maxanimations];
extern int numanimations;

void loadani(char *s);
void saveani(int aniID,char *s);


//########################## MATRIX MANIPULATOR ################################

extern D3DXMATRIX Mmat;
void Mstart();
void Mrotate(int axis,int degrees);
void MrotateRADS(int axis,float radians);
void Mmove(float x,float y,float z);
void Mscale(float x,float y,float z);
void Mtrans(float *x,float *y,float *z); // transform point x,y,z by matrix Mmat


//#################### STRING MANIPULATOR #################################

extern char S[1000];
void Ss(char *s2);
void Sa(char *s2);

// string add int
void Sai(int i);

// string add float
void Saf(float f);


//###################### GRAPHICS ENGINE #########################################

const DWORD ZFVF=(D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE2(0) ); // D3DFVF_DIFFUSE | D3DFVF_SPECULAR |




/*
struct Zvertexrec
{
float x,y,z;
float nx,ny,nz;
float tu,tv;
DWORD diffuse,specular;
};
*/


struct Zvertexrec
{
D3DVECTOR position,normal;
//D3DCOLOR diffuse,specular;
float tu,tv;
};


extern IDirect3DDevice9 *Zd3d_device_ptr;
extern D3DXMATRIX ZmView; // view matrix
extern D3DXMATRIX Zprojection_matrix; // projection matrix
extern float Zcamx,Zcamy,Zcamz,Zcamxr,Zcamyr,Zcamzr; // camera position



int Zinit(HINSTANCE program_instance_handle,int width,int height,char *progname,float nearplane,int farplane); // ,int zbuf24bit);
void Zshutdown();
void Zsetlite(DWORD liteID,float x,float y,float z,float brightness);
void Zsetcam(float x,float y,float z,float rx,float ry,float rz);
void Zsetcam2(float x,float y,float z,D3DXMATRIX *m); // m is rot matrix
void Zclearscreen(int r,int g,int b);
void Zclearscreen2(); // clears screen to z game engine default color (blacklight)
void Zclearzbuf();
void Zbeginscene();
void Zambient(int r,int g,int b);
void Zlighting(int onoff);
void Zalphablend(int onoff);
void zbuftest(int onoff);
void zbufwrite(int onoff);
void Zcull(int onoff);
void Znormalize(int onoff);
void Zlite(DWORD liteID,int onoff);
void Zalphatest(int onoff);
void Zmipmaps(int onoff);
void Zclamp(int onoff);
//void Zanisotropic(int onoff);
void Zminmagfilter(int filter_type); // filter_type: 0=point, 1=linear, 2=anisotropic
void Zspecular(int onoff);

int Zsuperclip(int x,int y,int z,int rad,int cliprng);

void Zset_clip_planes(float nearplane,int farplane);

void Zcalc_frustum_planes(); // must set proj and view mats first!

int Zsuperclip2(int x, int y, int z, int rad, int cliprng); // must call Zcalc_frustum_planes first!

int Zarea_in_frustum(int x1,int z1,int x2,int z2); // must call Zcalc_frustum_planes first!

void Zsetmesh(int meshID);

void Zcreate_mesh(int vbsize,int maxverts);

void Zsettex(int texID);

void Zdrawimmediate(Zdrawinfo *a);

void Zdrawimmediate2(Zdrawinfo *a); // uses Zdrawinfo's mWorld mat

void Zcalc_frustum_planes2(); // rotation only




//############################ TEXT ########################################


void Ztextcolor(int r,int g,int b);
void Ztext(int x,int y,char *s);
void Ztext3d(int x,int y,char *s);
void Zsetfont(int font); // 0=comic ms sans 1= ariel 2=times new roman
int textwidth(char *s); // returns width of string s in pixels. uses current font setting.




//################################ MOUSE #########################################

void Zgetmouse(int *x,int *y,int *b);
void Zputmouse(int x,int y);
void nobutton();
void pause();

//########################## WINDOWS MESSAGE PROCESSOR ############################

void Zdomessages();

//########################### GENERIC GUI COMPONENTS ###################################

void Zmsg(char *s,char *title);
void Zmsg2(char *s);


//############################ KEYBOARD ##########################################

int Zkeypressed(int vkey);

//####################### STRING CONVERSION ROUTINES #############################

int s2i(char *s);
void i2s(int i, char *s);
void f2s(float f,char *s);
float s2f(char *s);

//############################ DICE ######################################

int dice(int a);

//####################### TIMERS ########################################

void Zstarttimer(int a);
int Zelapsedtime(int a);
int Zelapsedticks(int a);


//####################### FILEIO ##########################################

FILE* infile(char *s);
FILE* infilebin(char *s);
FILE* outfile(char *s);
FILE* outfilebin(char *s);
void closefile(FILE *f);
int readfile(FILE *f,char *s); // returns: 0=fail 1=success
int readfilebin(FILE *f,unsigned char *buf,int numbytes); // returns numbytes read
void writefile(FILE *f,char *s);
void writefilebin(FILE *f,unsigned char *buf,int numbytes);
int readfileint(FILE *f);
void writefileint(FILE *f,int i);
float readfilefloat(FILE *f);
void writefilefloat(FILE *f,float i);
int filefound(char *s);
void writeFBint(FILE *f,int a);
void writeFBfloat(FILE *f,float a);
int readFBint(FILE *f);
float readFBfloat(FILE *f);
time_t timestamp(char *s); // returns file create time (UTC format: secs since 1970). returns -1 if file not found.






//####################### GENERIC MATH ROUTINES #############################

int isin(int x,int y,int x1,int y1,int x2,int y2);
int dist(int x1,int z1,int x2,int z2); // diamond bounding box dist
int dist3d(int x1,int y1,int z1,int x2,int y2,int z2);
int BBdist(int x1,int z1,int x2,int z2); // sq bounding box dist
float deg2rad(float deg);
float rad2deg(float rad);
int heading(int x1,int z1,int x2,int z2);
int relheading(int x1,int z1,int h1,int x2,int z2);
void llim(int *a,int b);
void ulim(int *a,int b);
void limit(int *a,int b,int c);
int angle(int x1,int y1,int z1,int x2,int y2,int z2);
int rng2D(int x1,int z1,int x2,int z2);
int BBdist3d(int x1,int y1,int z1,int x2,int y2,int z2); // 3d bounding box distance
void normangl(float *a); // normalize angle to 0 to 2 pi
float rng3d(float x1,float y1,float z1,float x2,float y2,float z2);


//################################ TEXT COLORS ###############################

void redtext();
void greentext();
void bluetext();
void yellowtext();
void magentatext();
void cyantext();
void whitetext();
void blacktext();
void greytext();




// ################################### mouse aimed camera ###################################

void Zresetmouse();

extern float cam_xr;
extern float cam_yr;

int Zmouse_aim_camera(); // returns mouse button pressed: 0 none, 1 LB, 2 RB, etc.


// load meshes and textures using a list of filenames stored on disk in a text file


//################################## SCREEN DUMP ##############################################

void Zscreendump();


void Zload_models(); // load models from models.dat file
void Zload_anis(); // load anis from anis.dat file
void Zload_materials(); // load materials from materials.dat file




//############################ TARGET LIST #########################################################





// #################################################### TARGET LIST DATA STRUCTURES #####################################################

#define stepsize 0.1f


struct ttyperec // a target type record
{
float maxspd,turnrate;
int ai,rad,hp,mass,canfly,userID,value,animated,usefuel,maxfuel;
Zdrawinfo d;
};



struct tgtrec
{
float x,y,z,xr,yr,spd;
int type,dp,owner,fuel,alive,active,userID,aniplayerID,dontdraw;
};



#define maxttypes 50
#define maxtgts 200

extern ttyperec ttype[maxttypes]; // target types database

extern tgtrec tgt[maxtgts]; // targets database



extern int numtgts; // index of last active tgt + 1.
// max possible number of active tgts at the moment.
// search tgt list using: for a=0 a<numtgts a++.



// ####################################################### INIT TARGETS SYSTEM #################################################################

void Zinit_targets_system(); // initializes the z game library

void load_target_types(); // load ttypes from file ttypes.dat

void load_targets(char *s2); // load targets from file s2



// ##################################################### TARGET ROUTINES ###############################################################

// new, del, turn, pitch, move

int get_inactive_tgt(); // returns index of 1st inactive tgt. adjusts numtgts accordingly. returns -1 if none available.

void deactivate_tgt(int a); // deativates tgt a. adjusts numtgts accordingly.

void turntgt(int a,float amnt); // turns a target around y axis by amnt radians. amnt is limited by ttype.turnrate

void pitchtgt(int a,float amnt); // turns a target around x axis by amnt radians. amnt is limited by ttype.turnrate

// ##################################################### TARGET DRAWING ROUTINES ##############################################################

void Zdrawtgt(int a); // draws target a

void Zdrawtgts(); // draws all targets

// ########################################################### TARGET AI ROUTINES ##################################################################



float AI_turn_amount(int a,float x,float z); // returns amount to turn tgt to face location x,z. amnt=AI_turn_amount(a,x,z) turntgt(a,amnt)

float AI_turn_from_amount(int a,float x,float z); // returns amount to turn tgt to face away from location x,z. amnt=AI_turn_from_amount(a,x,z) turntgt(a,amnt)

void AI_turntgt(int a,float x,float z); // turn tgt towards x,z

void AI_turntgt_from(int a,float x,float z); // turn tgt away from x,z

void AI_turn_tgt_to_tgt(int a,int c); // turn tgt a towards tgt c

void AI_turn_tgt_from_tgt(int a,int c); // turn tgt a away from tgt c

void AI_turn_static_to_tgt(int a,int c); // turn static tgt a towards tgt c

void AI_turn_static_from_tgt(int a,int c); // turn static tgt a away from tgt c

float AI_pitch_amount(int a,float x,float y,float z); // returns amount (in rads) to pitch tgt a to face x,y,z

float AI_pitch_from_amount(int a,float x,float y,float z); // returns amount (in rads) to pitch tgt a to face away from x,z,y.
// assumes tgt is heading away from xyz.
// returns 360 - AI_pitch_amount, not AI_pitch_amount + 180.

void AI_pitch_tgt_towards_xyz(int a,float x,float y,float z); // pitches tgt a towards xyz

void AI_pitch_tgt_awayfrom_xyz(int a,float x,float y,float z); // pitches tgt a away from xyz

void AI_pitch_tgt_towards_tgt(int a,int c); // pitches tgt a towards tgt c

void AI_pitch_tgt_awayfrom_tgt(int a,int c); // pitches tgt a away from tgt c




//############################################################ FPS METER #############################################################



void Zinit_fps_meter();

void Zupdate_fps_meter();

float Zget_fps();


//################################################################### skybox ########################################################


void Zdrawskybox(int skybox_meshID,int skybox_texID);


//######################################## TGT LIST COLLISION TABLE #############################################################

void Zset_collision_table(int ttype1,int ttype2,int value); // sets the "collision table" values for a pair of target types.

int Zcan_collide(int ttype1,int ttype2);

int collided(int a,float x,float y,float z,int r); // a is the target being moved


//##############################################################################################################################



int Zinitall(HINSTANCE hInstance);






#endif




December 2014 »

S M T W T F S
 123456
78910111213
1415161718 19 20
21222324252627
28293031   

Recent Entries

PARTNERS