/*==========================================================================
 *                                                                         *
 *  Copyright (C) 2001 Dymchenko L.A.                                      *
 *                                                                         *
 *  VirtualRay (Spherical Engine) header file                              *
 *                                                                         *
 ***************************************************************************/


#ifndef Spherical_Engine_Interface
#define Spherical_Engine_Interface

#define export_specificator __declspec(dllexport)
#define import_specificator __declspec(dllimport)
#define callconv_specificator __stdcall


#ifndef spherical_engine_dll  

#include <string> 
using namespace std;                       

#include "Types@.h"
#include "LinAlg@.h"

#define se_api  import_specificator
#define callconv callconv_specificator

#else

#define se_api  export_specificator
#define callconv __stdcall

#endif


#include "DInput_.h"

//=====================================================================
//=====================================================================

typedef w32 sphguidt;
typedef w32 entguidt;
typedef w32 triangleguidt;
typedef w32 lightguidt;
typedef w32 sceneguidt;
typedef w32 renderguidt;
typedef w32 texturerguidt;
typedef i32 secantguidt;
typedef w32 holderguidt;


struct  ddrawinfot
{
    i32 screensizex;
    i32 screensizey;
    i32 flwindow;
  HWND ddrawwinh;
    i32 flddrawwait;
  i32 flsystemmemory;
    i32 bpp;
};

struct ddrawwindowinfot
{
  i32 destx1,desty1,destx2,desty2;
  i32 soursex1,soursey1,soursex2,soursey2;
  w32 backsurfp;
};


struct  initrenderinfot
{
    rnumt viewsizex,viewsizey;
    i32 screensizex,screensizey;
    i32 bpp;
};

struct renderinfot
{
    basisobj viewbasis;     // axis Z looks forward.
    rvectort viewpoint;
//      i32 fltransparentscene;
  i32 flautointersection;
  i32 instructionset;
  i32 flsseapproximation;
  i32 x0,y0;
    i32 flshadows;
  i32 flqualityshadows;
  i32 fltextures;
  i32 flinternalddraw;
    i32 flexternalddrawsurface;
    w32 ddrawsurface;
    w32 screensurfacep;
    i32 wpitch;
};

//=====================================================================

se_api void callconv setspherevisible(sphguidt sphguid, bool flvisible);  

se_api void callconv setsphereposvf(sphguidt sphguid, const rvectort& centerp, rnumt radius);
se_api void callconv setspherepos4f(sphguidt sphguid, float x, float y, float z, float radius);
se_api void callconv setsphereposv(sphguidt sphguid, const rvectort& centerp);
se_api void callconv setspherepos3f(sphguidt sphguid, float x, float y, float z);
se_api void callconv setsphereposfp(sphguidt sphguid, float* centerpp);
se_api void callconv getsphereposfp(sphguidt sphguid, float* centerpp);
se_api void callconv setsphereradius(sphguidt sphguid, float sphereradius); 
se_api float callconv getsphereradius(sphguidt sphguid);
se_api void callconv setsphereposgs(sphguidt sphguid, const gspheret& gsphere);
se_api gspheret callconv getsphereposgs(sphguidt sphguid);

se_api void callconv setsphereclipspherefp(sphguidt sphguid, float* clipspherecenterpp, float clipsphereradius);  
se_api void callconv setsphereclipsphere4f(sphguidt sphguid, float x, float y, float z, float clipsphereradius);  
se_api void callconv setsphereclipspherevf(sphguidt sphguid, const rvectort& clipspherecenterp, float clipsphereradius);  
    

se_api void callconv setspherebasisfp(sphguidt sphguid, float*  basisp);  
se_api rnumpt callconv getspherebasisfp(sphguidt sphguid);
se_api void callconv setspherebasis(sphguidt sphguid, const basisobj& basis);
se_api basisobj callconv getspherebasis(sphguidt sphguid);
se_api void callconv setspherebasispointer(sphguidt sphguid, basisobj* basisp);
se_api void callconv spheremulbasisright(sphguidt sphguid, float* rightmatrixp);
se_api void callconv spheremulbasisleft(sphguidt sphguid, float* leftmatrixp);
se_api void callconv onspherebasischange(sphguidt sphguid);

se_api void callconv enablespheretexture(sphguidt sphguid, bool fltexture); 
se_api void callconv setspherecolorfp(sphguidt sphguid, float* col);  
se_api void callconv setspherecolor3f(sphguidt sphguid, float r, float g, float b); 
se_api void callconv setspherecolor3b(sphguidt sphguid, byte r, byte g, byte b);  
se_api void callconv setspherecolorb(sphguidt sphguid, bytecolor3t col);
se_api void callconv setspherelotexture(sphguidt sphguid, i16 lotxtype);  
se_api void callconv setspherehitexture(sphguidt sphguid, i16 hitxtype);  
se_api void callconv setspheretexture(sphguidt sphguid, i16 lotxtype, i16 hitxtype);  


se_api void callconv enablespherelighting(sphguidt sphguid, bool flspherelighting); 
se_api void callconv enablesphereownbgillum(sphguidt sphguid, bool flownbgillum);
se_api void callconv setspherebgillumfp(sphguidt sphguid, float* backgroundillump); 
se_api void callconv setspherebgillum3f(sphguidt sphguid, float r, float g, float b);
se_api void callconv setspherebgillumbp(sphguidt sphguid, byte* backgroundillump);
se_api void callconv setspherebgillum3b(sphguidt sphguid, byte r, byte g, byte b);
se_api void callconv setspherebgillumb(sphguidt sphguid, bytecolor3t backgroundillum);
    
se_api void callconv enablespheretransparency(sphguidt sphguid, bool fltransparency); 
se_api void callconv setspheretransparencyfp(sphguidt sphguid, float* coefp); 
se_api void callconv setspheretransparency3f(sphguidt sphguid, float r, float g, float b);
se_api void callconv setspheretransparencybp(sphguidt sphguid, byte* coefp);  
se_api void callconv setspheretransparency3b(sphguidt sphguid, byte r, byte g, byte b); 
se_api void callconv setspheretransparencyb(sphguidt sphguid, bytecolor3t coef);  
se_api void callconv enablespherevoltransparency(sphguidt sphguid, bool flvoltransparency);
se_api void callconv setspherevoltransptable(sphguidt sphguid, void* voltablep);

se_api void callconv enablesphereshadowcasting(sphguidt sphguid, bool flcastingshadow); 
se_api void callconv enablesphereshading(sphguidt sphguid, bool flshading);
se_api void callconv setspherelocalshadowing(sphguidt sphguid, bool fllocalshadowcasting);
se_api void callconv setsphereshadowtype(sphguidt sphguid, bool flcastingshadow, bool flshading, bool fllocalshadowcasting);
se_api void callconv setsphereshadowid(sphguidt sphguid, i32 shadowidentifier); 
se_api void callconv setsphereshadowedinterval(sphguidt sphguid, i32 lobound, i32 hibound); 

struct sphereinfot
{
  bool flvisible;
  rnumt radius;
  rvectort centerp;
  basisobj basis;
  basisobj* basispointer;
  bool fltexture;
  bytecolor3t color;
  i32 lotxtype,hitxtype;
  bool fltransparent;
  bytecolor3t alfa;
  bool fllighting;
  bool flownbgillum;
  bytecolor3t bgillum;
  bool flshadowcasting;
  bool flshading;
  bool fllocalshadowing;
};
typedef sphereinfot* sphereinfopt;

se_api void callconv setsphere(sphguidt sphguid, sphereinfot* sphereinfop);

    
se_api void callconv sphereaddsecantsphere(sphguidt sphguid, sphguidt secantsphereguid); 
se_api bool callconv sphereremovesecantsphere(sphguidt sphguid, sphguidt secantsphereguid);
se_api void callconv sphereremoveallsecantspheres(sphguidt sphguid);
se_api void callconv sphereupdatesecantsphereinfo(sphguidt sphguid, sphguidt secantsphereguid);
se_api void callconv sphereupdateallsecantsphereinfo(sphguidt sphguid);

se_api secantguidt callconv sphereinsertsecantspherefp(sphguidt sphguid, float* secantcenterpp, float secantradius);
se_api secantguidt callconv sphereinsertsecantspheref(sphguidt sphguid, float x, float y, float z, float secantradius);
se_api secantguidt callconv sphereinsertsecantspherev(sphguidt sphguid, const rvectort& secantcenterp, float secantradius);

se_api secantguidt callconv sphereinsertrelativesecantspherefp(sphguidt sphguid, float* secantcenterpp, float secantradius);
se_api secantguidt callconv sphereinsertrelativesecantspheref(sphguidt sphguid, float x, float y, float z, float secantradius);
se_api secantguidt callconv sphereinsertrelativesecantspherev(sphguidt sphguid, const rvectort& secantcenterp, float secantradius);

se_api secantguidt callconv sphereinsertrelativesecantplanefp(sphguidt sphguid, float* planenormalp, float d);
se_api secantguidt callconv sphereinsertrelativesecantplanef(sphguidt sphguid, float x, float y, float z, float d);
se_api secantguidt callconv sphereinsertrelativesecantplanev(sphguidt sphguid, const rvectort& planenormal, float d);

se_api bool callconv spheredeletesecantentity(sphguidt sphguid, secantguidt secantentityguid);  
se_api void callconv spheredeleteallsecantentities(sphguidt sphguid);  

se_api void callconv sphereclearsecantinfo(sphguidt sphguid);
    
se_api void callconv sphereactivateintersection(sphguidt sphguid);
se_api void callconv enablesphereintersection(sphguidt sphguid, bool flenable);
se_api void callconv spheredeactivateintersection(sphguidt sphguid);

se_api void callconv sphereexcludefromintersect(sphguidt sphguid); 
se_api void callconv sphereupdatedependentsecantinfo(sphguidt sphguid);

se_api void callconv setspheresecantinfocelladdend(sphguidt sphguid,  byte addend);
se_api void callconv sphereenlargesecantinfo(sphguidt sphguid, byte addend);
se_api void callconv spherecreatesecantinfo(sphguidt sphguid, byte apprsecantspherequ, byte addendqu);
se_api void callconv spheredestroysecantinfo(sphguidt sphguid);


se_api void callconv copysphere(sphguidt dest, sphguidt sourse);
se_api void callconv initsphere(sphguidt sphguid);
se_api void callconv sphereunallocate(sphguidt sphguid);

se_api sphguidt callconv sphereallocatesphere(sphguidt sphguid);
se_api void callconv sphereunallocatesphere(sphguidt sphguid, sphguidt unallocatesphguid);

//=====================================================================

se_api void callconv setentityvisible(entguidt entguid, bool flentityvisible);

se_api void callconv setentityspherevf(entguidt entguid, const rvectort& centerp, rnumt radius);
se_api void callconv setentitysphere4f(entguidt entguid, float x, float y, float z, float radius);
se_api void callconv setentityspherev(entguidt entguid, const rvectort& centerp);
se_api void callconv setentitysphere3f(entguidt entguid, float x, float y, float z);
se_api void callconv setentitysphereradius(entguidt entguid, float radius);
se_api float callconv getentitysphereradius(entguidt entguid);
se_api void callconv setentityspheregs(entguidt entguid, const gspheret& gsphere);
se_api gspheret callconv getentitysphere(entguidt entguid);
se_api  rvectorpt callconv calculateentityspherecenter(entguidt entguid);
se_api rnumt callconv calculateentitysphereradius(entguidt entguid);
se_api  gspherept callconv calculateentitysphere(entguidt entguid);

se_api i32 callconv getentityshadowid(entguidt entguid);

se_api sphguidt callconv entityallocatesphere(entguidt entguid);
se_api void callconv entityunallocatesphere(entguidt entguid, sphguidt sphguid);

se_api void callconv clearentity(entguidt entguid);

//=====================================================================

struct lightinfot
{
  i32 flenable;
  rvectort centerp;
  float distance;
  i32 flautodistance;
  float minintense;
  float minspherer;
  i32 lighttype;
  i32 fllocallight;
  float intense;
  float a0,a1,a2;
  float r,g,b;
};
typedef lightinfot* lightinfopt;

se_api void callconv setlight(lightguidt lightguid, lightinfot* lightinfop);
se_api void callconv enablelight(lightguidt lightguid, bool flenable);
se_api void callconv setlightcenterv(lightguidt lightguid, const rvectort& centerp);
se_api void callconv setlightcenter3f(lightguidt lightguid, float x, float y, float z);
se_api void callconv setlightcenterfp(lightguidt lightguid, float* centerpp);
se_api void callconv setlightdistance(lightguidt lightguid, float distance);
se_api void callconv enablelightautodistance(lightguidt lightguid, bool flautodistance);
se_api void callconv setlightminintense(lightguidt lightguid, byte minintense);
se_api void callconv setlightminspherer(lightguidt lightguid, float minsphereradius);
se_api void callconv setlightintense(lightguidt lightguid, float intense);
se_api void callconv setlightcoef(lightguidt lightguid, float a0, float a1, float a2);
se_api void callconv setlightcolor3b(lightguidt lightguid, byte r, byte g, byte b);
se_api void callconv setlightcolorb(lightguidt lightguid, bytecolor3t color);
se_api void callconv setlightcolorbp(lightguidt lightguid, byte* colorp);
se_api void callconv setlightcolor3f(lightguidt lightguid, float r, float g, float b); 
se_api void callconv setlightcolorfp(lightguidt lightguid, float* colorp);

//=====================================================================

se_api void callconv setscenebgillumfp(sceneguidt sceneguid, float* backgroundillump);  
se_api void callconv setscenebgillum3f(sceneguidt sceneguid, float r, float g, float b);
se_api void callconv setscenebgillumbp(sceneguidt sceneguid, byte* backgroundillump);
se_api void callconv setscenebgillum3b(sceneguidt sceneguid, byte r, byte g, byte b);
se_api void callconv setscenebgillumb(sceneguidt sceneguid, bytecolor3t backgroundillum);

se_api sphguidt callconv scenegetskysphere(sceneguidt sceneguid);

se_api sphguidt callconv scenegetdefaultsphere(sceneguidt sceneguid);

se_api lightguidt callconv sceneallocatelight(sceneguidt sceneguid);
se_api void callconv sceneunallocatelight(sceneguidt sceneguid, lightguidt lightguid);

se_api entguidt callconv sceneallocateentity(sceneguidt sceneguid, i32 maxspherequ);
se_api void callconv sceneunallocateentity(sceneguidt sceneguid, entguidt entityguid);

se_api sphguidt callconv sceneallocatesphere(sceneguidt sceneguid);
se_api void callconv sceneunallocatesphere(sceneguidt sceneguid, sphguidt sphereguid);

se_api void callconv clearscene(sceneguidt sceneguid);

se_api void callconv scenesetnewpixelformat(sceneguidt sceneguid, pixelformatt pixelforamt);

//=====================================================================

se_api sphguidt callconv allocatesphere(holderguidt holderguid);
se_api void callconv unallocatesphere(holderguidt holderguid, sphguidt sphguid);

//=====================================================================

struct renderstatinfot
{
  i32 overallsec1000;
  i32 prerenderingsec1000;
  i32 raytracingsec1000;
  i32 preanalysissec1000;
  i32 lightingsec1000;
  i32 intersectingsec1000; 
  i32 intersectionqu;
  i64 overalltick;
  i64 prerenderingtick;
  i64 raytracingtick;
  i64 preanalysistick;
  i64 lightingtick;
  i64 intersectingtick; 
}; 
typedef renderstatinfot* renderstatinfopt;

se_api void callconv renderscene(renderguidt renderguid, sceneguidt sceneguid, const renderinfot& renderinfo);

se_api void callconv setrenderopt(renderguidt renderguid, i32 param1, i32 param2);
se_api void callconv setdefaultrenderopt(renderguidt renderguid);

se_api void callconv clearrenderstat(renderguidt renderguid);
se_api void callconv getrenderstat(renderguidt renderguid, renderstatinfopt renderstatinfop);

se_api void callconv cleardevrenderstat(renderguidt renderguid);
se_api void callconv getdevrenderstat(renderguidt renderguid, void* devrenderstatinfop);

se_api void callconv initrender(renderguidt renderguid, const initrenderinfot& initrenderinfo);

//=====================================================================

se_api bool callconv texturerinserttexture(texturerguidt texturerguid, i16  texturetype, char* filename);
se_api void callconv texturerdeletetexture(texturerguidt texturerguid, i16 texturetype);

se_api void callconv texturersetnewpixelformat(texturerguidt texturerguid, pixelformatt newpixelformat);
se_api void callconv attachtexturertoscene(sceneguidt sceneguid, texturerguidt texturerguid);

//=====================================================================

se_api bool callconv ddrawflip(w32 surfp, bool flwait);
se_api w32 callconv ddrawgetsurfacep();
se_api i32 callconv ddrawgetsurfacewpitch();
se_api bool callconv ddrawinitpaint(w32 surfp, bool flwait);
se_api bool callconv ddrawdonepaint(w32 surfp);
se_api HDC  callconv ddrawgetdc(w32 surfp);
se_api bool callconv ddrawreleasedc(w32 surfp, HDC dc);
se_api bool callconv ddrawclear(w32 surfp);
se_api bool callconv ddrawinit(HWND hwnd,i32 w,i32 h,i32 bpp);
se_api bool callconv ddrawdone();

se_api bool callconv ddrawscreen();
se_api bool callconv ddrawwindow(ddrawwindowinfot info);
se_api w32 callconv ddrawgetprimarysurface();
se_api w32 callconv ddrawgetbacksurface();
se_api bool callconv ddrawsetwindow(HWND hwnd);
se_api w32 callconv ddrawcreatewindowsurface();
se_api bool callconv ddrawdestroywindowsurface(w32 surfp);
se_api bool callconv ddrawcreate(ddrawinfot initddrawinfo);
se_api bool callconv ddrawdestroy();

//=====================================================================

se_api void callconv directinput(dinputinfopt dinputp);
se_api  void callconv initdirectinput(bool fldinputmouse);
se_api void callconv donedirectinput();
se_api  bool callconv createdirectinput(HWND winh);
se_api  void callconv destroydirectinput();

//=====================================================================

se_api bool callconv SSEsupport();
se_api bool callconv MMXEXTsupport();

//=====================================================================

se_api sceneguidt callconv seallocatescene(i32 apprspherequ, i32 apprentityqu, pixelformatt pixelformat);
se_api void callconv seunallocatescene(sceneguidt sceneguid);

se_api renderguidt callconv seallocaterender(i32 apprspherequ);
se_api void callconv seunallocaterender(renderguidt renderguid);

se_api texturerguidt callconv seallocatetexturer(i16 maxtexturetypenum, pixelformatt pixelformat);
se_api void callconv seunallocatetexturer(texturerguidt texturerguid);

se_api bool callconv activatesphericalengine(i32 apprvisiblespherequ);
se_api void callconv deactivatesphericalengine();


#undef  se_api


#endif
