From 2196cb8419244947abfeae9c115cda69064f3a00 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Fri, 18 Jan 2019 17:04:17 +0100 Subject: [PATCH] runtime: Formatting --- Runtime/bbruntime_dll.cpp | 347 ++++--- Runtime/bbruntime_dll.hpp | 26 +- Runtime/bbruntime_dll.rc | 2 +- Runtime/blitz3d/animation.cpp | 167 +-- Runtime/blitz3d/animation.hpp | 34 +- Runtime/blitz3d/animator.cpp | 277 ++--- Runtime/blitz3d/animator.hpp | 82 +- Runtime/blitz3d/bd2model.cpp | 27 +- Runtime/blitz3d/bd2model.hpp | 9 +- Runtime/blitz3d/boxvis.cpp | 249 ++--- Runtime/blitz3d/brush.cpp | 247 +++-- Runtime/blitz3d/brush.hpp | 44 +- Runtime/blitz3d/cachedtexture.cpp | 198 ++-- Runtime/blitz3d/cachedtexture.hpp | 31 +- Runtime/blitz3d/camera.cpp | 159 +-- Runtime/blitz3d/camera.hpp | 74 +- Runtime/blitz3d/collision.cpp | 227 ++--- Runtime/blitz3d/collision.hpp | 20 +- Runtime/blitz3d/emitter.cpp | 48 +- Runtime/blitz3d/emitter.hpp | 24 +- Runtime/blitz3d/entity.cpp | 172 ++-- Runtime/blitz3d/entity.hpp | 119 ++- Runtime/blitz3d/frustum.cpp | 74 +- Runtime/blitz3d/frustum.hpp | 45 +- Runtime/blitz3d/geom.cpp | 9 +- Runtime/blitz3d/geom.hpp | 704 ++++++++----- Runtime/blitz3d/group.hpp | 12 +- Runtime/blitz3d/light.cpp | 38 +- Runtime/blitz3d/light.hpp | 30 +- Runtime/blitz3d/listener.cpp | 48 +- Runtime/blitz3d/listener.hpp | 20 +- Runtime/blitz3d/loader.cpp | 92 +- Runtime/blitz3d/loader_3ds.cpp | 643 ++++++------ Runtime/blitz3d/loader_3ds.hpp | 6 +- Runtime/blitz3d/loader_b3d.cpp | 420 ++++---- Runtime/blitz3d/loader_b3d.hpp | 6 +- Runtime/blitz3d/loader_x.cpp | 513 +++++----- Runtime/blitz3d/loader_x.hpp | 6 +- Runtime/blitz3d/md2model.cpp | 172 ++-- Runtime/blitz3d/md2model.hpp | 58 +- Runtime/blitz3d/md2model_bak.cpp | 765 +++++++------- Runtime/blitz3d/md2norms.cpp | 220 +--- Runtime/blitz3d/md2rep.cpp | 164 +-- Runtime/blitz3d/md2rep.hpp | 63 +- Runtime/blitz3d/meshcollider.cpp | 268 ++--- Runtime/blitz3d/meshcollider.hpp | 47 +- Runtime/blitz3d/meshloader.cpp | 166 +-- Runtime/blitz3d/meshloader.hpp | 23 +- Runtime/blitz3d/meshmodel.cpp | 204 ++-- Runtime/blitz3d/meshmodel.hpp | 60 +- Runtime/blitz3d/meshrenderer.hpp | 14 +- Runtime/blitz3d/meshutil.cpp | 389 +++---- Runtime/blitz3d/meshutil.hpp | 13 +- Runtime/blitz3d/mirror.cpp | 12 +- Runtime/blitz3d/mirror.hpp | 16 +- Runtime/blitz3d/model.cpp | 162 +-- Runtime/blitz3d/model.hpp | 161 ++- Runtime/blitz3d/object.cpp | 199 ++-- Runtime/blitz3d/object.hpp | 149 ++- Runtime/blitz3d/pivot.cpp | 9 +- Runtime/blitz3d/pivot.hpp | 11 +- Runtime/blitz3d/planemodel.cpp | 174 ++-- Runtime/blitz3d/planemodel.hpp | 30 +- Runtime/blitz3d/q3bspmodel.cpp | 45 +- Runtime/blitz3d/q3bspmodel.hpp | 32 +- Runtime/blitz3d/q3bsprep.cpp | 483 +++++---- Runtime/blitz3d/q3bsprep.hpp | 51 +- Runtime/blitz3d/rendercontext.hpp | 36 +- Runtime/blitz3d/skinmodel.cpp | 36 +- Runtime/blitz3d/skinmodel.hpp | 11 +- Runtime/blitz3d/sprite.cpp | 186 ++-- Runtime/blitz3d/sprite.hpp | 54 +- Runtime/blitz3d/std.hpp | 10 +- Runtime/blitz3d/surface.cpp | 230 +++-- Runtime/blitz3d/surface.hpp | 162 +-- Runtime/blitz3d/terrain.cpp | 43 +- Runtime/blitz3d/terrain.hpp | 31 +- Runtime/blitz3d/terrainrep.cpp | 837 ++++++++------- Runtime/blitz3d/terrainrep.hpp | 58 +- Runtime/blitz3d/texture.cpp | 201 ++-- Runtime/blitz3d/texture.hpp | 48 +- Runtime/blitz3d/world.cpp | 298 +++--- Runtime/blitz3d/world.hpp | 55 +- Runtime/lib/CMakeLists.txt | 17 +- Runtime/lib/basic.cpp | 459 ++++++--- Runtime/lib/basic.hpp | 177 ++-- Runtime/lib/bb_basic.hpp | 95 +- Runtime/lib/bbaudio.cpp | 205 ++-- Runtime/lib/bbaudio.hpp | 37 +- Runtime/lib/bbbank.cpp | 279 ++--- Runtime/lib/bbblitz3d.cpp | 1581 ++++++++++++++++++----------- Runtime/lib/bbblitz3d.hpp | 5 +- Runtime/lib/bbfilesystem.cpp | 220 ++-- Runtime/lib/bbfilesystem.hpp | 4 +- Runtime/lib/bbgraphics.cpp | 1049 ++++++++++++------- Runtime/lib/bbgraphics.hpp | 187 ++-- Runtime/lib/bbinput.cpp | 398 +++++--- Runtime/lib/bbinput.hpp | 76 +- Runtime/lib/bbmath.cpp | 159 ++- Runtime/lib/bbmath.hpp | 36 +- Runtime/lib/bbruntime.cpp | 180 ++-- Runtime/lib/bbruntime.hpp | 6 +- Runtime/lib/bbsockets.cpp | 446 +++++--- Runtime/lib/bbsockets.hpp | 3 +- Runtime/lib/bbstream.cpp | 229 +++-- Runtime/lib/bbstream.hpp | 18 +- Runtime/lib/bbstring.cpp | 285 +++--- Runtime/lib/bbstring.hpp | 36 +- Runtime/lib/bbsys.cpp | 6 +- Runtime/lib/bbsys.hpp | 18 +- Runtime/lib/multiplay.cpp | 2 +- Runtime/lib/multiplay_setup.cpp | 4 +- Runtime/lib/resource.hpp | 22 +- Runtime/lib/std.hpp | 11 +- Runtime/lib/userlib.cpp | 39 +- Runtime/lib/userlib.hpp | 2 +- Runtime/lib/userlibs.cpp | 108 +- Runtime/lib/userlibs.hpp | 6 +- Runtime/resource.hpp | 14 +- 119 files changed, 10326 insertions(+), 7798 deletions(-) diff --git a/Runtime/bbruntime_dll.cpp b/Runtime/bbruntime_dll.cpp index 1b47a7f..cbe09d5 100644 --- a/Runtime/bbruntime_dll.cpp +++ b/Runtime/bbruntime_dll.cpp @@ -1,234 +1,278 @@ -#pragma warning( disable:4786 ) +#pragma warning(disable : 4786) #include "bbruntime_dll.hpp" #include "../debugger/debugger.hpp" - using namespace std; -#include #include #include +#include #include "bbruntime.hpp" -class DummyDebugger : public Debugger{ -public: - virtual void debugRun(){} - virtual void debugStop(){}// bbruntime_panic(0); } - virtual void debugStmt( int srcpos,const char *file ){} - virtual void debugEnter( void *frame,void *env,const char *func ){} - virtual void debugLeave(){} - virtual void debugLog( const char *msg ){} - virtual void debugMsg( const char *e,bool serious ){ - if( serious ) MessageBox( 0,e,"Error!",MB_OK|MB_TOPMOST|MB_SETFOREGROUND ); +class DummyDebugger : public Debugger { + public: + virtual void debugRun() {} + virtual void debugStop() {} // bbruntime_panic(0); } + virtual void debugStmt(int srcpos, const char* file) {} + virtual void debugEnter(void* frame, void* env, const char* func) {} + virtual void debugLeave() {} + virtual void debugLog(const char* msg) {} + virtual void debugMsg(const char* e, bool serious) + { + if (serious) + MessageBox(0, e, "Error!", MB_OK | MB_TOPMOST | MB_SETFOREGROUND); } - virtual void debugSys( void *msg ){} + virtual void debugSys(void* msg) {} }; -static HINSTANCE hinst; -static map syms; -map::iterator sym_it; -static gxRuntime *gx_runtime; +static HINSTANCE hinst; +static map syms; +map::iterator sym_it; +static gxRuntime* gx_runtime; -static void rtSym( const char *sym,void *pc ){ - syms[sym]=pc; +static void rtSym(const char* sym, void* pc) +{ + syms[sym] = pc; } -static void _cdecl seTranslator( unsigned int u,EXCEPTION_POINTERS* pExp ){ - switch( u ){ +static void _cdecl seTranslator(unsigned int u, EXCEPTION_POINTERS* pExp) +{ + switch (u) { case EXCEPTION_INT_DIVIDE_BY_ZERO: - bbruntime_panic( "Integer divide by zero" ); + bbruntime_panic("Integer divide by zero"); case EXCEPTION_ACCESS_VIOLATION: - bbruntime_panic( "Memory access violation" ); + bbruntime_panic("Memory access violation"); case EXCEPTION_ILLEGAL_INSTRUCTION: - bbruntime_panic( "Illegal instruction" ); + bbruntime_panic("Illegal instruction"); case EXCEPTION_STACK_OVERFLOW: - bbruntime_panic( "Stack overflow!" ); + bbruntime_panic("Stack overflow!"); } - bbruntime_panic( "Unknown runtime exception" ); + bbruntime_panic("Unknown runtime exception"); } -int Runtime::version(){ +int Runtime::version() +{ return VERSION; } -const char *Runtime::nextSym(){ - if( !syms.size() ){ - bbruntime_link( rtSym ); - sym_it=syms.begin(); +const char* Runtime::nextSym() +{ + if (!syms.size()) { + bbruntime_link(rtSym); + sym_it = syms.begin(); } - if( sym_it==syms.end() ){ - syms.clear();return 0; + if (sym_it == syms.end()) { + syms.clear(); + return 0; } return (sym_it++)->first; } -int Runtime::symValue( const char *sym ){ - map::iterator it=syms.find( sym ); - if( it!=syms.end() ) return (int)it->second; +int Runtime::symValue(const char* sym) +{ + map::iterator it = syms.find(sym); + if (it != syms.end()) + return (int)it->second; return -1; } -void Runtime::startup( HINSTANCE h ){ - hinst=h; +void Runtime::startup(HINSTANCE h) +{ + hinst = h; } -void Runtime::shutdown(){ - trackmem( false ); +void Runtime::shutdown() +{ + trackmem(false); syms.clear(); } -void Runtime::execute( void (*pc)(),const char *args,Debugger *dbg ){ - - bool debug=!!dbg; +void Runtime::execute(void (*pc)(), const char* args, Debugger* dbg) +{ + bool debug = !!dbg; static DummyDebugger dummydebug; - if( !dbg ) dbg=&dummydebug; + if (!dbg) + dbg = &dummydebug; - trackmem( true ); + trackmem(true); _se_translator_function old_trans = _set_se_translator(seTranslator); - _control87(_RC_NEAR | _PC_24 | _EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW | _EM_UNDERFLOW | _EM_INEXACT | _EM_DENORMAL, 0xfffff); + _control87(_RC_NEAR | _PC_24 | _EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW | _EM_UNDERFLOW | _EM_INEXACT + | _EM_DENORMAL, + 0xfffff); //strip spaces from ends of args... - string params=args; - while( params.size() && params[0]==' ' ) params=params.substr( 1 ); - while( params.size() && params[params.size()-1]==' ' ) params=params.substr( 0,params.size()-1 ); + string params = args; + while (params.size() && params[0] == ' ') + params = params.substr(1); + while (params.size() && params[params.size() - 1] == ' ') + params = params.substr(0, params.size() - 1); - if( gx_runtime=gxRuntime::openRuntime( hinst,params,dbg ) ){ - bbruntime_run( gx_runtime,pc,debug ); + if (gx_runtime = gxRuntime::openRuntime(hinst, params, dbg)) { + bbruntime_run(gx_runtime, pc, debug); - gxRuntime *t=gx_runtime; - gx_runtime=0; - gxRuntime::closeRuntime( t ); + gxRuntime* t = gx_runtime; + gx_runtime = 0; + gxRuntime::closeRuntime(t); } _control87(_CW_DEFAULT, 0xfffff); _set_se_translator(old_trans); } -void Runtime::asyncStop(){ - if( gx_runtime ) gx_runtime->asyncStop(); +void Runtime::asyncStop() +{ + if (gx_runtime) + gx_runtime->asyncStop(); } -void Runtime::asyncRun(){ - if( gx_runtime ) gx_runtime->asyncRun(); +void Runtime::asyncRun() +{ + if (gx_runtime) + gx_runtime->asyncRun(); } -void Runtime::asyncEnd(){ - if( gx_runtime ) gx_runtime->asyncEnd(); +void Runtime::asyncEnd() +{ + if (gx_runtime) + gx_runtime->asyncEnd(); } -void Runtime::checkmem( streambuf *buf ){ - ostream out( buf ); - ::checkmem( out ); +void Runtime::checkmem(streambuf* buf) +{ + ostream out(buf); + ::checkmem(out); } -Runtime *_cdecl runtimeGetRuntime(){ +Runtime* _cdecl runtimeGetRuntime() +{ static Runtime runtime; return &runtime; } /********************** BUTT UGLY DLL->EXE HOOK! *************************/ -static void *module_pc; -static map module_syms; -static map runtime_syms; -static Runtime *runtime; +static void* module_pc; +static map module_syms; +static map runtime_syms; +static Runtime* runtime; -static void fail(){ - MessageBox( 0,"Unable to run Blitz Basic module",0,0 ); +static void fail() +{ + MessageBox(0, "Unable to run Blitz Basic module", 0, 0); ExitProcess(-1); } -struct Sym{ +struct Sym { string name; - int value; + int value; }; -static Sym getSym( void **p ){ - Sym sym; - char *t=(char*)*p; - while( char c=*t++ ) sym.name+=c; - sym.value=*(int*)t+(int)module_pc; - *p=t+4;return sym; +static Sym getSym(void** p) +{ + Sym sym; + char* t = (char*)*p; + while (char c = *t++) + sym.name += c; + sym.value = *(int*)t + (int)module_pc; + *p = t + 4; + return sym; } -static int findSym( const string &t ){ - map::iterator it; +static int findSym(const string& t) +{ + map::iterator it; - it=module_syms.find( t ); - if( it!=module_syms.end() ) return it->second; - it=runtime_syms.find( t ); - if( it!=runtime_syms.end() ) return it->second; + it = module_syms.find(t); + if (it != module_syms.end()) + return it->second; + it = runtime_syms.find(t); + if (it != runtime_syms.end()) + return it->second; - string err="Can't find symbol: "+t; - MessageBox( 0,err.c_str(),0,0 ); + string err = "Can't find symbol: " + t; + MessageBox(0, err.c_str(), 0, 0); ExitProcess(0); return 0; } -static void link(){ - - while( const char *sc=runtime->nextSym() ){ - +static void link() +{ + while (const char* sc = runtime->nextSym()) { string t(sc); - if( t[0]=='_' ){ - runtime_syms["_"+t]=runtime->symValue(sc); + if (t[0] == '_') { + runtime_syms["_" + t] = runtime->symValue(sc); continue; } - if( t[0]=='!' ) t=t.substr(1); + if (t[0] == '!') + t = t.substr(1); - if( !isalnum(t[0]) ) t=t.substr(1); + if (!isalnum(t[0])) + t = t.substr(1); - for( int k=0;ksymValue(sc); + runtime_syms["_f" + tolower(t)] = runtime->symValue(sc); } - HRSRC hres=FindResource( 0,MAKEINTRESOURCE(1111),RT_RCDATA );if( !hres ) fail(); - HGLOBAL hglo=LoadResource( 0,hres );if( !hglo ) fail(); - void *p=LockResource( hglo );if( !p ) fail(); + HRSRC hres = FindResource(0, MAKEINTRESOURCE(1111), RT_RCDATA); + if (!hres) + fail(); + HGLOBAL hglo = LoadResource(0, hres); + if (!hglo) + fail(); + void* p = LockResource(hglo); + if (!p) + fail(); - int sz=*(int*)p;p=(int*)p+1; + int sz = *(int*)p; + p = (int*)p + 1; //replace malloc for service pack 2 Data Execution Prevention (DEP). - module_pc=VirtualAlloc( 0,sz,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE ); + module_pc = VirtualAlloc(0, sz, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); - memcpy( module_pc,p,sz ); - p=(char*)p+sz; + memcpy(module_pc, p, sz); + p = (char*)p + sz; - int k,cnt; + int k, cnt; - cnt=*(int*)p;p=(int*)p+1; - for( k=0;k=(int)module_pc+sz ) fail(); - module_syms[sym.name]=sym.value; + cnt = *(int*)p; + p = (int*)p + 1; + for (k = 0; k < cnt; ++k) { + Sym sym = getSym(&p); + if (sym.value < (int)module_pc || sym.value >= (int)module_pc + sz) + fail(); + module_syms[sym.name] = sym.value; } - cnt=*(int*)p;p=(int*)p+1; - for( k=0;kstartup( inst ); + runtime = runtimeGetRuntime(); + runtime->startup(inst); link(); //get cmd_line and params - string cmd=GetCommandLine(),params; - while( cmd.size() && cmd[0]==' ' ) cmd=cmd.substr( 1 ); - if( cmd.find( '\"' )==0 ){ - int n=cmd.find( '\"',1 ); - if( n!=string::npos ){ - params=cmd.substr( n+1 ); - cmd=cmd.substr( 1,n-1 ); + string cmd = GetCommandLine(), params; + while (cmd.size() && cmd[0] == ' ') + cmd = cmd.substr(1); + if (cmd.find('\"') == 0) { + int n = cmd.find('\"', 1); + if (n != string::npos) { + params = cmd.substr(n + 1); + cmd = cmd.substr(1, n - 1); } - }else{ - int n=cmd.find( ' ' ); - if( n!=string::npos ){ - params=cmd.substr( n+1 ); - cmd=cmd.substr( 0,n ); + } else { + int n = cmd.find(' '); + if (n != string::npos) { + params = cmd.substr(n + 1); + cmd = cmd.substr(0, n); } } - runtime->execute( (void(*)())module_pc,params.c_str(),0 ); + runtime->execute((void (*)())module_pc, params.c_str(), 0); runtime->shutdown(); - _DllMainCRTStartup( inst,DLL_PROCESS_DETACH,0 ); + _DllMainCRTStartup(inst, DLL_PROCESS_DETACH, 0); ExitProcess(0); return 0; } - diff --git a/Runtime/bbruntime_dll.hpp b/Runtime/bbruntime_dll.hpp index bcf3e74..4493b44 100644 --- a/Runtime/bbruntime_dll.hpp +++ b/Runtime/bbruntime_dll.hpp @@ -10,21 +10,21 @@ class Debugger; -class Runtime{ -public: - virtual int version(); - virtual const char *nextSym(); - virtual int symValue( const char *sym ); - virtual void startup( HINSTANCE hinst ); - virtual void shutdown(); - virtual void asyncStop(); - virtual void asyncRun(); - virtual void asyncEnd(); - virtual void checkmem( std::streambuf *buf ); +class Runtime { + public: + virtual int version(); + virtual const char* nextSym(); + virtual int symValue(const char* sym); + virtual void startup(HINSTANCE hinst); + virtual void shutdown(); + virtual void asyncStop(); + virtual void asyncRun(); + virtual void asyncEnd(); + virtual void checkmem(std::streambuf* buf); - virtual void execute( void (*pc)(),const char *args,Debugger *dbg ); + virtual void execute(void (*pc)(), const char* args, Debugger* dbg); }; -extern "C" _declspec(dllexport) Runtime * _cdecl runtimeGetRuntime(); +extern "C" _declspec(dllexport) Runtime* _cdecl runtimeGetRuntime(); #endif \ No newline at end of file diff --git a/Runtime/bbruntime_dll.rc b/Runtime/bbruntime_dll.rc index 705b97c..6d9d4a7 100644 --- a/Runtime/bbruntime_dll.rc +++ b/Runtime/bbruntime_dll.rc @@ -7,7 +7,7 @@ // // Generated from the TEXTINCLUDE 2 resource. // -#include "afxres.hpp" +#include "afxres.h" ///////////////////////////////////////////////////////////////////////////// #undef APSTUDIO_READONLY_SYMBOLS diff --git a/Runtime/blitz3d/animation.cpp b/Runtime/blitz3d/animation.cpp index 959f69b..8e4d5ca 100644 --- a/Runtime/blitz3d/animation.cpp +++ b/Runtime/blitz3d/animation.cpp @@ -1,142 +1,163 @@ -#include "std.hpp" #include "animation.hpp" +#include "std.hpp" -struct Animation::Rep{ - +struct Animation::Rep { int ref_cnt; - typedef map KeyList; + typedef map KeyList; - KeyList scale_anim,rot_anim,pos_anim; + KeyList scale_anim, rot_anim, pos_anim; - Rep(): - ref_cnt(1){ - } + Rep() : ref_cnt(1) {} - Rep( const Rep &t ): - ref_cnt(1), - scale_anim(t.scale_anim),rot_anim(t.rot_anim),pos_anim(t.pos_anim){ - } + Rep(const Rep& t) : ref_cnt(1), scale_anim(t.scale_anim), rot_anim(t.rot_anim), pos_anim(t.pos_anim) {} - Vector getLinearValue( const KeyList &keys,float time )const{ - KeyList::const_iterator next,curr; + Vector getLinearValue(const KeyList& keys, float time) const + { + KeyList::const_iterator next, curr; //for( next=keys.begin();next!=keys.end() && time>=next->first;++next ){} - next=keys.upper_bound( (int)time ); + next = keys.upper_bound((int)time); - if( next==keys.begin() ) return next->second.v; - curr=next;--curr; - if( next==keys.end() ) return curr->second.v; + if (next == keys.begin()) + return next->second.v; + curr = next; + --curr; + if (next == keys.end()) + return curr->second.v; - float delta=( time-curr->first )/( next->first-curr->first ); - return ( next->second.v-curr->second.v )*delta+curr->second.v; + float delta = (time - curr->first) / (next->first - curr->first); + return (next->second.v - curr->second.v) * delta + curr->second.v; } - - Quat getSlerpValue( const KeyList &keys,float time )const{ - KeyList::const_iterator next,curr; + + Quat getSlerpValue(const KeyList& keys, float time) const + { + KeyList::const_iterator next, curr; //for( next=keys.begin();next!=keys.end() && time>=next->first;++next ){} - next=keys.upper_bound( (int)time ); + next = keys.upper_bound((int)time); - if( next==keys.begin() ) return next->second; - curr=next;--curr; - if( next==keys.end() ) return curr->second; + if (next == keys.begin()) + return next->second; + curr = next; + --curr; + if (next == keys.end()) + return curr->second; - float delta=( time-curr->first )/( next->first-curr->first ); - return curr->second.slerpTo( next->second,delta ); + float delta = (time - curr->first) / (next->first - curr->first); + return curr->second.slerpTo(next->second, delta); } - void setKey( KeyList &keys,int time,const Quat &value ){ - keys[time]=value; + void setKey(KeyList& keys, int time, const Quat& value) + { + keys[time] = value; } }; -Animation::Animation(): -rep( new Rep() ){ -} +Animation::Animation() : rep(new Rep()) {} -Animation::Animation( const Animation &t ): -rep( t.rep ){ +Animation::Animation(const Animation& t) : rep(t.rep) +{ ++rep->ref_cnt; } -Animation::Animation( const Animation &t,int first,int last ): -rep( new Rep() ){ +Animation::Animation(const Animation& t, int first, int last) : rep(new Rep()) +{ Rep::KeyList::const_iterator it; - for( it=t.rep->pos_anim.begin();it!=t.rep->pos_anim.end();++it ){ - if( it->firstfirst>last ) continue; - rep->setKey( rep->pos_anim,it->first-first,it->second ); + for (it = t.rep->pos_anim.begin(); it != t.rep->pos_anim.end(); ++it) { + if (it->first < first || it->first > last) + continue; + rep->setKey(rep->pos_anim, it->first - first, it->second); } - for( it=t.rep->scale_anim.begin();it!=t.rep->scale_anim.end();++it ){ - if( it->firstfirst>last ) continue; - rep->setKey( rep->scale_anim,it->first-first,it->second ); + for (it = t.rep->scale_anim.begin(); it != t.rep->scale_anim.end(); ++it) { + if (it->first < first || it->first > last) + continue; + rep->setKey(rep->scale_anim, it->first - first, it->second); } - for( it=t.rep->rot_anim.begin();it!=t.rep->rot_anim.end();++it ){ - if( it->firstfirst>last ) continue; - rep->setKey( rep->rot_anim,it->first-first,it->second ); + for (it = t.rep->rot_anim.begin(); it != t.rep->rot_anim.end(); ++it) { + if (it->first < first || it->first > last) + continue; + rep->setKey(rep->rot_anim, it->first - first, it->second); } } -Animation::~Animation(){ - if( !--rep->ref_cnt ) delete rep; +Animation::~Animation() +{ + if (!--rep->ref_cnt) + delete rep; } -Animation &Animation::operator=( const Animation &t ){ +Animation& Animation::operator=(const Animation& t) +{ ++t.rep->ref_cnt; - if( !--rep->ref_cnt ) delete rep; - rep=t.rep; + if (!--rep->ref_cnt) + delete rep; + rep = t.rep; return *this; } -Animation::Rep *Animation::write(){ - if( rep->ref_cnt>1 ){ +Animation::Rep* Animation::write() +{ + if (rep->ref_cnt > 1) { --rep->ref_cnt; - rep=new Rep( *rep ); + rep = new Rep(*rep); } return rep; } -void Animation::setScaleKey( int time,const Vector &q ){ +void Animation::setScaleKey(int time, const Vector& q) +{ write(); - rep->setKey( rep->scale_anim,time,Quat( 0,q ) ); + rep->setKey(rep->scale_anim, time, Quat(0, q)); } -void Animation::setPositionKey( int time,const Vector &q ){ +void Animation::setPositionKey(int time, const Vector& q) +{ write(); - rep->setKey( rep->pos_anim,time,Quat( 0,q ) ); + rep->setKey(rep->pos_anim, time, Quat(0, q)); } -void Animation::setRotationKey( int time,const Quat &q ){ +void Animation::setRotationKey(int time, const Quat& q) +{ write(); - rep->setKey( rep->rot_anim,time,q ); + rep->setKey(rep->rot_anim, time, q); } -int Animation::numScaleKeys()const{ +int Animation::numScaleKeys() const +{ return rep->scale_anim.size(); } -int Animation::numRotationKeys()const{ +int Animation::numRotationKeys() const +{ return rep->rot_anim.size(); } -int Animation::numPositionKeys()const{ +int Animation::numPositionKeys() const +{ return rep->pos_anim.size(); } -Vector Animation::getScale( float time )const{ - if( !rep->scale_anim.size() ) return Vector(1,1,1); - return rep->getLinearValue( rep->scale_anim,time ); +Vector Animation::getScale(float time) const +{ + if (!rep->scale_anim.size()) + return Vector(1, 1, 1); + return rep->getLinearValue(rep->scale_anim, time); } -Vector Animation::getPosition( float time )const{ - if( !rep->pos_anim.size() ) return Vector(0,0,0); - return rep->getLinearValue( rep->pos_anim,time ); +Vector Animation::getPosition(float time) const +{ + if (!rep->pos_anim.size()) + return Vector(0, 0, 0); + return rep->getLinearValue(rep->pos_anim, time); } -Quat Animation::getRotation( float time )const{ - if( !rep->rot_anim.size() ) return Quat(); - return rep->getSlerpValue( rep->rot_anim,time ); +Quat Animation::getRotation(float time) const +{ + if (!rep->rot_anim.size()) + return Quat(); + return rep->getSlerpValue(rep->rot_anim, time); } /* diff --git a/Runtime/blitz3d/animation.hpp b/Runtime/blitz3d/animation.hpp index 39d61d6..3793776 100644 --- a/Runtime/blitz3d/animation.hpp +++ b/Runtime/blitz3d/animation.hpp @@ -6,32 +6,32 @@ #include "geom.hpp" -class Animation{ -public: +class Animation { + public: Animation(); - Animation( const Animation &t ); - Animation( const Animation &t,int first,int last ); + Animation(const Animation& t); + Animation(const Animation& t, int first, int last); ~Animation(); - Animation &operator=( const Animation &t ); + Animation& operator=(const Animation& t); - void setScaleKey( int frame,const Vector &q ); - void setPositionKey( int frame,const Vector &p ); - void setRotationKey( int frame,const Quat &q ); + void setScaleKey(int frame, const Vector& q); + void setPositionKey(int frame, const Vector& p); + void setRotationKey(int frame, const Quat& q); - int numScaleKeys()const; - int numRotationKeys()const; - int numPositionKeys()const; + int numScaleKeys() const; + int numRotationKeys() const; + int numPositionKeys() const; - Vector getScale( float time )const; - Vector getPosition( float time )const; - Quat getRotation( float time )const; + Vector getScale(float time) const; + Vector getPosition(float time) const; + Quat getRotation(float time) const; -private: + private: struct Rep; - Rep *rep; + Rep* rep; - Rep *write(); + Rep* write(); }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/animator.cpp b/Runtime/blitz3d/animator.cpp index 97db3ba..7240518 100644 --- a/Runtime/blitz3d/animator.cpp +++ b/Runtime/blitz3d/animator.cpp @@ -1,218 +1,245 @@ -#include "std.hpp" #include "animator.hpp" #include "object.hpp" +#include "std.hpp" -Animator::Animator( Animator *t ):_seqs( t->_seqs ){ +Animator::Animator(Animator* t) : _seqs(t->_seqs) +{ + _objs.resize(t->_objs.size()); + _anims.resize(t->_anims.size()); - _objs.resize( t->_objs.size() ); - _anims.resize( t->_anims.size() ); - - for( int k=0;k_objs.size();++k ){ - _objs[k]=t->_objs[k]->getLastCopy(); - _anims[k].keys=t->_anims[k].keys; + for (int k = 0; k < t->_objs.size(); ++k) { + _objs[k] = t->_objs[k]->getLastCopy(); + _anims[k].keys = t->_anims[k].keys; } reset(); } -Animator::Animator( Object *obj,int frames ){ - addObjs( obj ); - _anims.resize( _objs.size() ); - addSeq( frames ); +Animator::Animator(Object* obj, int frames) +{ + addObjs(obj); + _anims.resize(_objs.size()); + addSeq(frames); reset(); } -Animator::Animator( const vector &objs,int frames ):_objs(objs){ - _anims.resize( _objs.size() ); - addSeq( frames ); +Animator::Animator(const vector& objs, int frames) : _objs(objs) +{ + _anims.resize(_objs.size()); + addSeq(frames); reset(); } -void Animator::reset(){ - _seq=_mode=_seq_len=_time=_speed=_trans_time=_trans_speed=0; +void Animator::reset() +{ + _seq = _mode = _seq_len = _time = _speed = _trans_time = _trans_speed = 0; } -void Animator::addObjs( Object *obj ){ - _objs.push_back( obj ); - for( Entity *e=obj->GetChildren();e;e=e->GetSuccessor() ){ - addObjs( e->getObject() ); +void Animator::addObjs(Object* obj) +{ + _objs.push_back(obj); + for (Entity* e = obj->GetChildren(); e; e = e->GetSuccessor()) { + addObjs(e->getObject()); } } -void Animator::addSeq( int frames ){ +void Animator::addSeq(int frames) +{ Seq seq; - seq.frames=frames; - _seqs.push_back( seq ); - for( int k=0;k<_objs.size();++k ){ - Object *obj=_objs[k]; - _anims[k].keys.push_back( obj->getAnimation() ); - obj->setAnimation( Animation() ); + seq.frames = frames; + _seqs.push_back(seq); + for (int k = 0; k < _objs.size(); ++k) { + Object* obj = _objs[k]; + _anims[k].keys.push_back(obj->getAnimation()); + obj->setAnimation(Animation()); } } -void Animator::addSeqs( Animator *t ){ - for( int n=0;n_seqs.size();++n ){ - _seqs.push_back( t->_seqs[n] ); - for( int k=0;k<_objs.size();++k ){ +void Animator::addSeqs(Animator* t) +{ + for (int n = 0; n < t->_seqs.size(); ++n) { + _seqs.push_back(t->_seqs[n]); + for (int k = 0; k < _objs.size(); ++k) { int j; - for( j=0;j_objs.size();++j ){ - if( _objs[k]->getName()==t->_objs[j]->getName() ) break; + for (j = 0; j < t->_objs.size(); ++j) { + if (_objs[k]->getName() == t->_objs[j]->getName()) + break; } - if( j==t->_objs.size() ){ - _anims[k].keys.push_back( Animation() ); + if (j == t->_objs.size()) { + _anims[k].keys.push_back(Animation()); continue; } - _anims[k].keys.push_back( t->_anims[j].keys[n] ); + _anims[k].keys.push_back(t->_anims[j].keys[n]); } } } -void Animator::extractSeq( int first,int last,int seq ){ +void Animator::extractSeq(int first, int last, int seq) +{ Seq sq; - sq.frames=last-first; - _seqs.push_back( sq ); + sq.frames = last - first; + _seqs.push_back(sq); - for( int k=0;k<_objs.size();++k ){ - Animation &keys=_anims[k].keys[seq]; - _anims[k].keys.push_back( Animation( keys,first,last ) ); + for (int k = 0; k < _objs.size(); ++k) { + Animation& keys = _anims[k].keys[seq]; + _anims[k].keys.push_back(Animation(keys, first, last)); } } -void Animator::updateAnim(){ +void Animator::updateAnim() +{ + for (int k = 0; k < _objs.size(); ++k) { + Object* obj = _objs[k]; + const Animation& keys = _anims[k].keys[_seq]; - for( int k=0;k<_objs.size();++k ){ - - Object *obj=_objs[k]; - const Animation &keys=_anims[k].keys[_seq]; - - if( keys.numPositionKeys() ){ - obj->SetLocalPosition( keys.getPosition( _time ) ); + if (keys.numPositionKeys()) { + obj->SetLocalPosition(keys.getPosition(_time)); } - if( keys.numScaleKeys() ){ - obj->SetLocalScale( keys.getScale( _time ) ); + if (keys.numScaleKeys()) { + obj->SetLocalScale(keys.getScale(_time)); } - if( keys.numRotationKeys() ){ - obj->SetLocalRotation( keys.getRotation( _time ) ); + if (keys.numRotationKeys()) { + obj->SetLocalRotation(keys.getRotation(_time)); } } } -void Animator::updateTrans(){ +void Animator::updateTrans() +{ + for (int k = 0; k < _objs.size(); ++k) { + Object* obj = _objs[k]; + const Anim& anim = _anims[k]; - for( int k=0;k<_objs.size();++k ){ - - Object *obj=_objs[k]; - const Anim &anim=_anims[k]; - - if( anim.pos ) obj->SetLocalPosition( (anim.dest_pos-anim.src_pos)*_trans_time+anim.src_pos ); - if( anim.scl ) obj->SetLocalScale( (anim.dest_scl-anim.src_scl)*_trans_time+anim.src_scl ); - if( anim.rot ) obj->SetLocalRotation( anim.src_rot.slerpTo( anim.dest_rot,_trans_time ) ); + if (anim.pos) + obj->SetLocalPosition((anim.dest_pos - anim.src_pos) * _trans_time + anim.src_pos); + if (anim.scl) + obj->SetLocalScale((anim.dest_scl - anim.src_scl) * _trans_time + anim.src_scl); + if (anim.rot) + obj->SetLocalRotation(anim.src_rot.slerpTo(anim.dest_rot, _trans_time)); } } -void Animator::beginTrans(){ +void Animator::beginTrans() +{ + for (int k = 0; k < _objs.size(); ++k) { + Object* obj = _objs[k]; + Anim& anim = _anims[k]; + const Animation& keys = _anims[k].keys[_seq]; - for( int k=0;k<_objs.size();++k ){ - - Object *obj=_objs[k]; - Anim &anim=_anims[k]; - const Animation &keys=_anims[k].keys[_seq]; - - if( anim.pos=!!keys.numPositionKeys() ){ - anim.src_pos=obj->GetLocalPosition(); - anim.dest_pos=keys.getPosition( _time ); + if (anim.pos = !!keys.numPositionKeys()) { + anim.src_pos = obj->GetLocalPosition(); + anim.dest_pos = keys.getPosition(_time); } - if( anim.scl=!!keys.numScaleKeys() ){ - anim.src_scl=obj->GetLocalScale(); - anim.dest_scl=keys.getScale( _time ); + if (anim.scl = !!keys.numScaleKeys()) { + anim.src_scl = obj->GetLocalScale(); + anim.dest_scl = keys.getScale(_time); } - if( anim.rot=!!keys.numRotationKeys() ){ - anim.src_rot=obj->GetLocalRotation(); - anim.dest_rot=keys.getRotation( _time ); + if (anim.rot = !!keys.numRotationKeys()) { + anim.src_rot = obj->GetLocalRotation(); + anim.dest_rot = keys.getRotation(_time); } } } -void Animator::setAnimTime( float time,int seq ){ - if( seq<0 || seq>_seqs.size() ) return; +void Animator::setAnimTime(float time, int seq) +{ + if (seq < 0 || seq > _seqs.size()) + return; - _mode=0; - _speed=0; - _seq=seq; - _seq_len=_seqs[_seq].frames; + _mode = 0; + _speed = 0; + _seq = seq; + _seq_len = _seqs[_seq].frames; //Ok, mod the anim time! - _time=fmod( time,_seq_len ); + _time = fmod(time, _seq_len); //if( time<0 || time>_seq_len ) time=fmod( time,_seq_len ); //_time=time; - - if( _time<0 ) _time+=+_seq_len; + + if (_time < 0) + _time += +_seq_len; updateAnim(); } -void Animator::animate( int mode,float speed,int seq,float trans ){ - if( !mode && !speed ){ _mode=0;return; } - - if( seq<0 || seq>=_seqs.size() ) return; - - _seq=seq; - _mode=mode; - _seq_len=_seqs[_seq].frames; - _speed=speed; - _time=_speed>=0 ? 0 : _seq_len; - - if( trans<=0 ){ - updateAnim(); - if( !_speed ) _mode=0; +void Animator::animate(int mode, float speed, int seq, float trans) +{ + if (!mode && !speed) { + _mode = 0; return; } - _mode|=0x8000; - _trans_time=0; - _trans_speed=1/trans; + if (seq < 0 || seq >= _seqs.size()) + return; + + _seq = seq; + _mode = mode; + _seq_len = _seqs[_seq].frames; + _speed = speed; + _time = _speed >= 0 ? 0 : _seq_len; + + if (trans <= 0) { + updateAnim(); + if (!_speed) + _mode = 0; + return; + } + + _mode |= 0x8000; + _trans_time = 0; + _trans_speed = 1 / trans; beginTrans(); } -void Animator::update( float elapsed ){ +void Animator::update(float elapsed) +{ + if (!_mode) + return; - if( !_mode ) return; - - if( _mode&0x8000 ){ - _trans_time+=_trans_speed*elapsed; - if( _trans_time<1 ){ + if (_mode & 0x8000) { + _trans_time += _trans_speed * elapsed; + if (_trans_time < 1) { updateTrans(); return; } - _mode&=0x7fff; - if( !_mode || !_speed ){ + _mode &= 0x7fff; + if (!_mode || !_speed) { updateAnim(); - _mode=0; + _mode = 0; return; } } //do anim... - _time+=_speed*elapsed; + _time += _speed * elapsed; - switch( _mode ){ + switch (_mode) { case ANIM_MODE_LOOP: - _time=fmod( _time,_seq_len ); - if( _time<0 ) _time+=_seq_len; + _time = fmod(_time, _seq_len); + if (_time < 0) + _time += _seq_len; break; case ANIM_MODE_PINGPONG: - _time=fmod( _time,_seq_len*2 ); - if( _time<0 ) _time+=_seq_len*2; - if( _time>=_seq_len ){ _time=_seq_len-(_time-_seq_len);_speed=-_speed; } + _time = fmod(_time, _seq_len * 2); + if (_time < 0) + _time += _seq_len * 2; + if (_time >= _seq_len) { + _time = _seq_len - (_time - _seq_len); + _speed = -_speed; + } break; case ANIM_MODE_ONESHOT: - if( _time<0 ){ _time=0;_mode=0; } - else if( _time>=_seq_len ){ _time=_seq_len;_mode=0; } + if (_time < 0) { + _time = 0; + _mode = 0; + } else if (_time >= _seq_len) { + _time = _seq_len; + _mode = 0; + } break; } updateAnim(); } - diff --git a/Runtime/blitz3d/animator.hpp b/Runtime/blitz3d/animator.hpp index 83a2df9..b2f2694 100644 --- a/Runtime/blitz3d/animator.hpp +++ b/Runtime/blitz3d/animator.hpp @@ -6,66 +6,80 @@ class Object; -class Animator{ -public: - enum{ - ANIM_MODE_LOOP=1, - ANIM_MODE_PINGPONG=2, - ANIM_MODE_ONESHOT=3 - }; +class Animator { + public: + enum { ANIM_MODE_LOOP = 1, ANIM_MODE_PINGPONG = 2, ANIM_MODE_ONESHOT = 3 }; - Animator( Animator *animator ); + Animator(Animator* animator); - Animator( Object *tree,int frames ); + Animator(Object* tree, int frames); - Animator( const vector &objs,int frames ); + Animator(const vector& objs, int frames); - void addSeq( int frames ); + void addSeq(int frames); - void addSeqs( Animator *t ); + void addSeqs(Animator* t); - void extractSeq( int first,int last,int seq ); + void extractSeq(int first, int last, int seq); - void setAnimTime( float time,int seq ); + void setAnimTime(float time, int seq); - void animate( int mode,float speed,int seq,float trans ); + void animate(int mode, float speed, int seq, float trans); - void update( float elapsed ); + void update(float elapsed); - int animSeq()const{ return _seq; } - int animLen()const{ return _seq_len; } - float animTime()const{ return _time; } - bool animating()const{ return !!_mode; } + int animSeq() const + { + return _seq; + } + int animLen() const + { + return _seq_len; + } + float animTime() const + { + return _time; + } + bool animating() const + { + return !!_mode; + } - int numSeqs()const{ return _seqs.size(); } - const vector &getObjects()const{ return _objs; } + int numSeqs() const + { + return _seqs.size(); + } + const vector& getObjects() const + { + return _objs; + } -private: - struct Seq{ + private: + struct Seq { int frames; }; - struct Anim{ + struct Anim { //anim keys vector keys; //for transitions... - bool pos,scl,rot; - Vector src_pos,dest_pos; - Vector src_scl,dest_scl; - Quat src_rot,dest_rot; - Anim():pos(false),scl(false),rot(false){} + bool pos, scl, rot; + Vector src_pos, dest_pos; + Vector src_scl, dest_scl; + Quat src_rot, dest_rot; + Anim() : pos(false), scl(false), rot(false) {} }; vector _seqs; - vector _anims; + vector _anims; vector _objs; - int _seq,_mode,_seq_len; - float _time,_speed,_trans_time,_trans_speed; + int _seq, _mode, _seq_len; + float _time, _speed, _trans_time, _trans_speed; void reset(); - void addObjs( Object *obj ); + void addObjs(Object* obj); void updateAnim(); void beginTrans(); void updateTrans(); diff --git a/Runtime/blitz3d/bd2model.cpp b/Runtime/blitz3d/bd2model.cpp index 1598079..0b51f42 100644 --- a/Runtime/blitz3d/bd2model.cpp +++ b/Runtime/blitz3d/bd2model.cpp @@ -1,27 +1,26 @@ #include "bd2model.hpp" -struct BD2Vert{ - unsigned char x,y,z,n,u,v; +struct BD2Vert { + unsigned char x, y, z, n, u, v; }; -struct BD2Tri{ - unsigned short v0,v1,v2; +struct BD2Tri { + unsigned short v0, v1, v2; }; -struct BD2Frame{ - float x_scale,y_scale,z_scale; - float x_offset,y_offset,z_offset; +struct BD2Frame { + float x_scale, y_scale, z_scale; + float x_offset, y_offset, z_offset; BD2Vert verts[1]; }; -struct BD2File{ - int id; //'BD2F' - float u_scale,v_scale; - int n_verts,n_tris,n_frames; - BD2Tri tris[1]; +struct BD2File { + int id; //'BD2F' + float u_scale, v_scale; + int n_verts, n_tris, n_frames; + BD2Tri tris[1]; BD2Frame frames[1]; }; -struct BD2Model::Rep{ -}; \ No newline at end of file +struct BD2Model::Rep {}; \ No newline at end of file diff --git a/Runtime/blitz3d/bd2model.hpp b/Runtime/blitz3d/bd2model.hpp index b9d139e..30bed82 100644 --- a/Runtime/blitz3d/bd2model.hpp +++ b/Runtime/blitz3d/bd2model.hpp @@ -2,12 +2,11 @@ #ifndef BD2MODEL_H #define BD2MODEL_H -class BD2Model : public Model{ -public: -private: +class BD2Model : public Model { + public: + private: struct Rep; - Rep *rep; + Rep* rep; }; - #endif \ No newline at end of file diff --git a/Runtime/blitz3d/boxvis.cpp b/Runtime/blitz3d/boxvis.cpp index 9d384c2..d7b2111 100644 --- a/Runtime/blitz3d/boxvis.cpp +++ b/Runtime/blitz3d/boxvis.cpp @@ -1,201 +1,206 @@ +#include #include "std.hpp" #include "world.hpp" -#include -static World *w; +static World* w; -struct Face{ +struct Face { Vector verts[4]; - Face( const Vector &v0,const Vector &v1,const Vector &v2,const Vector &v3 ){ - verts[0]=v0; - verts[1]=v1; - verts[2]=v2; - verts[3]=v3; + Face(const Vector& v0, const Vector& v1, const Vector& v2, const Vector& v3) + { + verts[0] = v0; + verts[1] = v1; + verts[2] = v2; + verts[3] = v3; } }; -static int face_verts[][4]={ - 2,3,1,0, - 3,7,5,1, - 7,6,4,5, - 6,2,0,4, - 6,7,3,2, - 0,1,5,4 +static int face_verts[][4] = {2, 3, 1, 0, 3, 7, 5, 1, 7, 6, 4, 5, 6, 2, 0, 4, 6, 7, 3, 2, 0, 1, 5, 4}; + +struct Coll { + int obj, surf, tri; + Coll(const ObjCollision& t) : obj((int)t.with), surf((int)t.collision.surface), tri(t.collision.index) {} }; -struct Coll{ - int obj,surf,tri; - Coll( const ObjCollision &t ):obj((int)t.with),surf((int)t.collision.surface),tri(t.collision.index){ - } -}; - -struct CollCmp{ - bool operator()( const Coll &a,const Coll &b )const{ - if( a.obj CollSet; +typedef set CollSet; //returns: 1 for visible, 0 for hidden, -1 for don't know -static int faceVis( const Face &src,const Face &dest ){ - +static int faceVis(const Face& src, const Face& dest) +{ static CollSet all; static CollSet colls[16]; all.clear(); - for( int k=0;k<4;++k ){ - for( int j=0;j<4;++j ){ - int n=k*4+j; + for (int k = 0; k < 4; ++k) { + for (int j = 0; j < 4; ++j) { + int n = k * 4 + j; colls[n].clear(); - Vector sv=src.verts[k]; - Vector dv=dest.verts[j]; - Vector adj=(dv-sv).normalized()*.01f; - dv-=adj; + Vector sv = src.verts[k]; + Vector dv = dest.verts[j]; + Vector adj = (dv - sv).normalized() * .01f; + dv -= adj; - for(;;){ - sv+=adj; - Line line( sv,dv-sv ); + for (;;) { + sv += adj; + Line line(sv, dv - sv); ObjCollision c; - if( !w->traceRay( line,EPSILON,&c ) ) break; + if (!w->traceRay(line, EPSILON, &c)) + break; - Coll t( c ); - all.insert( t ); - colls[n].insert( t ); + Coll t(c); + all.insert(t); + colls[n].insert(t); - sv=c.coords; + sv = c.coords; } - if( !colls[n].size() ) return 1; + if (!colls[n].size()) + return 1; } } CollSet::const_iterator it; - for( it=all.begin();it!=all.end();++it ){ - int k=0; - for( ;k<16;++k ){ - if( !colls[k].count( *it ) ) break; + for (it = all.begin(); it != all.end(); ++it) { + int k = 0; + for (; k < 16; ++k) { + if (!colls[k].count(*it)) + break; } - if( k==16 ) return 0; //definitely hidden! + if (k == 16) + return 0; //definitely hidden! } return -1; } -static void subdivide( list &lst ){ - int n=lst.size(); - while( n-- ){ - const Face &f=lst.front(); - Vector a( (f.verts[0]+f.verts[1])/2 ); - Vector b( (f.verts[1]+f.verts[2])/2 ); - Vector c( (f.verts[2]+f.verts[3])/2 ); - Vector d( (f.verts[3]+f.verts[0])/2 ); - Vector e( (f.verts[0]+f.verts[1]+f.verts[2]+f.verts[3])/4 ); - lst.push_back( Face( f.verts[0],a,e,d ) ); - lst.push_back( Face( a,f.verts[1],b,e ) ); - lst.push_back( Face( e,b,f.verts[2],c ) ); - lst.push_back( Face( d,e,c,f.verts[3] ) ); - lst.erase( lst.begin() ); +static void subdivide(list& lst) +{ + int n = lst.size(); + while (n--) { + const Face& f = lst.front(); + Vector a((f.verts[0] + f.verts[1]) / 2); + Vector b((f.verts[1] + f.verts[2]) / 2); + Vector c((f.verts[2] + f.verts[3]) / 2); + Vector d((f.verts[3] + f.verts[0]) / 2); + Vector e((f.verts[0] + f.verts[1] + f.verts[2] + f.verts[3]) / 4); + lst.push_back(Face(f.verts[0], a, e, d)); + lst.push_back(Face(a, f.verts[1], b, e)); + lst.push_back(Face(e, b, f.verts[2], c)); + lst.push_back(Face(d, e, c, f.verts[3])); + lst.erase(lst.begin()); } } -static int faceVis( const Face &src,const Face &dest,int recurs_limit ){ - - static list src_faces,dest_faces; +static int faceVis(const Face& src, const Face& dest, int recurs_limit) +{ + static list src_faces, dest_faces; src_faces.clear(); dest_faces.clear(); - src_faces.push_back( src ); - dest_faces.push_back( dest ); + src_faces.push_back(src); + dest_faces.push_back(dest); - while( recurs_limit-- ){ - list::iterator src_it,dest_it; - for( src_it=src_faces.begin();src_it!=src_faces.end();++src_it ){ - int cnt=0; - for( dest_it=dest_faces.begin();dest_it!=dest_faces.end();++dest_it ){ - int n=faceVis( *src_it,*dest_it ); - if( n==1 ) return 1; - if( !n ) ++cnt; + while (recurs_limit--) { + list::iterator src_it, dest_it; + for (src_it = src_faces.begin(); src_it != src_faces.end(); ++src_it) { + int cnt = 0; + for (dest_it = dest_faces.begin(); dest_it != dest_faces.end(); ++dest_it) { + int n = faceVis(*src_it, *dest_it); + if (n == 1) + return 1; + if (!n) + ++cnt; } - if( cnt==dest_faces.size() ){ + if (cnt == dest_faces.size()) { //source can't see ANY dest faces - src_it=src_faces.erase( src_it ); + src_it = src_faces.erase(src_it); --src_it; } } - if( !src_faces.size() ) return 0; + if (!src_faces.size()) + return 0; //ok, subdivide! - subdivide( src_faces ); - subdivide( dest_faces ); + subdivide(src_faces); + subdivide(dest_faces); } return -1; } -bool World::boxVis( const Box &src,const Box &dest,int recurs_limit ){ - - w=this; +bool World::boxVis(const Box& src, const Box& dest, int recurs_limit) +{ + w = this; Box big; - big.update( src ); - big.update( dest ); + big.update(src); + big.update(dest); Plane planes[6]; - for( int n=0;n<6;++n ){ - planes[n]=Plane( - big.corner( face_verts[n][0] ), - big.corner( face_verts[n][1] ), - big.corner( face_verts[n][2] )); + for (int n = 0; n < 6; ++n) { + planes[n] = Plane(big.corner(face_verts[n][0]), big.corner(face_verts[n][1]), big.corner(face_verts[n][2])); } - for( int k=0;k<6;++k ){ - Vector v0=src.corner( face_verts[k][0] ); - Vector v1=src.corner( face_verts[k][1] ); - Vector v2=src.corner( face_verts[k][2] ); - Vector v3=src.corner( face_verts[k][3] ); + for (int k = 0; k < 6; ++k) { + Vector v0 = src.corner(face_verts[k][0]); + Vector v1 = src.corner(face_verts[k][1]); + Vector v2 = src.corner(face_verts[k][2]); + Vector v3 = src.corner(face_verts[k][3]); int n; - for( n=0;n<6;++n ){ - const Plane &p=planes[n]; - if( fabs(p.distance(v0))<=EPSILON && - fabs(p.distance(v1))<=EPSILON && - fabs(p.distance(v2))<=EPSILON && - fabs(p.distance(v3))<=EPSILON ) break; + for (n = 0; n < 6; ++n) { + const Plane& p = planes[n]; + if (fabs(p.distance(v0)) <= EPSILON && fabs(p.distance(v1)) <= EPSILON && fabs(p.distance(v2)) <= EPSILON + && fabs(p.distance(v3)) <= EPSILON) + break; } - if( n<6 ) continue; + if (n < 6) + continue; - Face src_face( v0,v1,v2,v3 ); + Face src_face(v0, v1, v2, v3); - for( int j=0;j<6;++j ){ - Vector v0=dest.corner( face_verts[j][0] ); - Vector v1=dest.corner( face_verts[j][1] ); - Vector v2=dest.corner( face_verts[j][2] ); - Vector v3=dest.corner( face_verts[j][3] ); + for (int j = 0; j < 6; ++j) { + Vector v0 = dest.corner(face_verts[j][0]); + Vector v1 = dest.corner(face_verts[j][1]); + Vector v2 = dest.corner(face_verts[j][2]); + Vector v3 = dest.corner(face_verts[j][3]); int n; - for( n=0;n<6;++n ){ - const Plane &p=planes[n]; - if( fabs(p.distance(v0))<=EPSILON && - fabs(p.distance(v1))<=EPSILON && - fabs(p.distance(v2))<=EPSILON && - fabs(p.distance(v3))<=EPSILON ) break; + for (n = 0; n < 6; ++n) { + const Plane& p = planes[n]; + if (fabs(p.distance(v0)) <= EPSILON && fabs(p.distance(v1)) <= EPSILON + && fabs(p.distance(v2)) <= EPSILON && fabs(p.distance(v3)) <= EPSILON) + break; } - if( n<6 ) continue; + if (n < 6) + continue; - Face dest_face( v0,v1,v2,v3 ); + Face dest_face(v0, v1, v2, v3); - int t=faceVis( src_face,dest_face,recurs_limit ); + int t = faceVis(src_face, dest_face, recurs_limit); - if( t ) return true; + if (t) + return true; } } return false; diff --git a/Runtime/blitz3d/brush.cpp b/Runtime/blitz3d/brush.cpp index 25418ea..589e940 100644 --- a/Runtime/blitz3d/brush.cpp +++ b/Runtime/blitz3d/brush.cpp @@ -1,211 +1,240 @@ -#include "std.hpp" #include "brush.hpp" +#include "std.hpp" #include "../gxruntime/gxgraphics.hpp" -struct Brush::Rep{ - union{ int ref_cnt;Rep *next; }; - int blend,max_tex; - bool blend_valid; +struct Brush::Rep { + union { + int ref_cnt; + Rep* next; + }; + int blend, max_tex; + bool blend_valid; gxScene::RenderState rs; - Texture texs[gxScene::MAX_TEXTURES]; + Texture texs[gxScene::MAX_TEXTURES]; - static Rep *pool; + static Rep* pool; - Rep(): - ref_cnt(1),blend(0),max_tex(0),blend_valid(true){ - memset( &rs,0,sizeof(rs) ); - rs.blend=gxScene::BLEND_REPLACE; - rs.color[0]=rs.color[1]=rs.color[2]=rs.alpha=1; + Rep() : ref_cnt(1), blend(0), max_tex(0), blend_valid(true) + { + memset(&rs, 0, sizeof(rs)); + rs.blend = gxScene::BLEND_REPLACE; + rs.color[0] = rs.color[1] = rs.color[2] = rs.alpha = 1; } - Rep( const Rep &t ): - ref_cnt(1),blend(t.blend),max_tex(t.max_tex),rs(t.rs),blend_valid(t.blend_valid){ - for( int k=0;knext; + Rep* p = pool; + pool = p->next; return p; } - void operator delete( void *q ){ - Rep *p=(Rep*)q; - p->next=pool; - pool=p; + void operator delete(void* q) + { + Rep* p = (Rep*)q; + p->next = pool; + pool = p; } }; -Brush::Rep *Brush::Rep::pool; +Brush::Rep* Brush::Rep::pool; -Brush::Brush(): -rep( new Rep() ){ -} +Brush::Brush() : rep(new Rep()) {} -Brush::Brush( const Brush &t ): -rep( t.rep ){ +Brush::Brush(const Brush& t) : rep(t.rep) +{ ++rep->ref_cnt; } -Brush::Brush( const Brush &a,const Brush &b ): -rep( new Rep( *a.rep ) ){ +Brush::Brush(const Brush& a, const Brush& b) : rep(new Rep(*a.rep)) +{ + *(Vector*)rep->rs.color *= *(Vector*)b.rep->rs.color; - *(Vector*)rep->rs.color*=*(Vector*)b.rep->rs.color; + rep->rs.alpha *= b.rep->rs.alpha; + rep->rs.shininess += b.rep->rs.shininess; - rep->rs.alpha*=b.rep->rs.alpha; - rep->rs.shininess+=b.rep->rs.shininess; + if (b.rep->blend) + rep->blend = b.rep->blend; - if( b.rep->blend ) rep->blend=b.rep->blend; + rep->rs.fx |= b.rep->rs.fx; - rep->rs.fx|=b.rep->rs.fx; + if (b.rep->max_tex > rep->max_tex) + rep->max_tex = b.rep->max_tex; - if( b.rep->max_tex>rep->max_tex ) rep->max_tex=b.rep->max_tex; - - for( int k=0;kmax_tex;++k ){ - if( b.rep->rs.tex_states[k].canvas ){ - rep->rs.tex_states[k].canvas=b.rep->rs.tex_states[k].canvas; - rep->texs[k]=b.rep->texs[k]; + for (int k = 0; k < rep->max_tex; ++k) { + if (b.rep->rs.tex_states[k].canvas) { + rep->rs.tex_states[k].canvas = b.rep->rs.tex_states[k].canvas; + rep->texs[k] = b.rep->texs[k]; } } - rep->blend_valid=false; + rep->blend_valid = false; } -Brush::~Brush(){ - if( !--rep->ref_cnt ) delete rep; +Brush::~Brush() +{ + if (!--rep->ref_cnt) + delete rep; } -Brush &Brush::operator=( const Brush &t ){ +Brush& Brush::operator=(const Brush& t) +{ ++t.rep->ref_cnt; - if( !--rep->ref_cnt ) delete rep; - rep=t.rep;return *this; + if (!--rep->ref_cnt) + delete rep; + rep = t.rep; + return *this; } -Brush::Rep *Brush::write()const{ - if( rep->ref_cnt>1 ){ +Brush::Rep* Brush::write() const +{ + if (rep->ref_cnt > 1) { --rep->ref_cnt; - rep=new Rep( *rep ); + rep = new Rep(*rep); } return rep; } -void Brush::setColor( const Vector &color ){ - *(Vector*)write()->rs.color=color; +void Brush::setColor(const Vector& color) +{ + *(Vector*)write()->rs.color = color; } -void Brush::setAlpha( float alpha ){ - float a=rep->rs.alpha; - write()->rs.alpha=alpha; - if( (a<1)!=(alpha<1) ) rep->blend_valid=false; +void Brush::setAlpha(float alpha) +{ + float a = rep->rs.alpha; + write()->rs.alpha = alpha; + if ((a < 1) != (alpha < 1)) + rep->blend_valid = false; } -void Brush::setShininess( float n ){ - write()->rs.shininess=n; +void Brush::setShininess(float n) +{ + write()->rs.shininess = n; } -void Brush::setBlend( int blend ){ - write()->blend=blend; - rep->blend_valid=false; +void Brush::setBlend(int blend) +{ + write()->blend = blend; + rep->blend_valid = false; } -void Brush::setFX( int fx ){ - write()->rs.fx=fx; - rep->blend_valid=false; +void Brush::setFX(int fx) +{ + write()->rs.fx = fx; + rep->blend_valid = false; } -void Brush::setTexture( int index,const Texture &t,int n ){ +void Brush::setTexture(int index, const Texture& t, int n) +{ write(); - gxScene::RenderState &rs=rep->rs; + gxScene::RenderState& rs = rep->rs; - rep->texs[index]=t; - rs.tex_states[index].canvas=t.getCanvas( n ); + rep->texs[index] = t; + rs.tex_states[index].canvas = t.getCanvas(n); - rep->max_tex=0; - for( int k=0;kmax_tex=k+1; + rep->max_tex = 0; + for (int k = 0; k < gxScene::MAX_TEXTURES; ++k) { + if (rs.tex_states[k].canvas) + rep->max_tex = k + 1; } - rep->blend_valid=false; + rep->blend_valid = false; } -const Vector &Brush::getColor()const{ +const Vector& Brush::getColor() const +{ return *(Vector*)rep->rs.color; } -float Brush::getAlpha()const{ +float Brush::getAlpha() const +{ return rep->rs.alpha; } -float Brush::getShininess()const{ +float Brush::getShininess() const +{ return rep->rs.shininess; } -int Brush::getBlend()const{ - if( rep->blend_valid ) return rep->rs.blend; +int Brush::getBlend() const +{ + if (rep->blend_valid) + return rep->rs.blend; - rep->blend_valid=true; //well, it will be... + rep->blend_valid = true; //well, it will be... - gxScene::RenderState &rs=rep->rs; + gxScene::RenderState& rs = rep->rs; //alphatest - if( rep->texs[0].getCanvasFlags() & gxCanvas::CANVAS_TEX_MASK ){ - rs.fx|=gxScene::FX_ALPHATEST; - }else{ - rs.fx&=~gxScene::FX_ALPHATEST; + if (rep->texs[0].getCanvasFlags() & gxCanvas::CANVAS_TEX_MASK) { + rs.fx |= gxScene::FX_ALPHATEST; + } else { + rs.fx &= ~gxScene::FX_ALPHATEST; } //0 = default/replace //1 = alpha //2 = multiply //3 = add - if( rep->blend ){ - if( rep->blend!=gxScene::BLEND_ALPHA ){ - return rs.blend=rep->blend; + if (rep->blend) { + if (rep->blend != gxScene::BLEND_ALPHA) { + return rs.blend = rep->blend; } - for( int k=0;kmax_tex;++k ){ - if( rep->texs[k].isTransparent() ){ - return rs.blend=gxScene::BLEND_ALPHA; + for (int k = 0; k < rep->max_tex; ++k) { + if (rep->texs[k].isTransparent()) { + return rs.blend = gxScene::BLEND_ALPHA; } } - }else if( rep->max_tex==1 && rep->texs[0].isTransparent() ){ + } else if (rep->max_tex == 1 && rep->texs[0].isTransparent()) { //single transparent texture? - return rs.blend=gxScene::BLEND_ALPHA; + return rs.blend = gxScene::BLEND_ALPHA; } //vertex alpha or entityalpha? - if( (rs.fx&gxScene::FX_VERTEXALPHA) || rs.alpha<1 ){ - return rs.blend=gxScene::BLEND_ALPHA; + if ((rs.fx & gxScene::FX_VERTEXALPHA) || rs.alpha < 1) { + return rs.blend = gxScene::BLEND_ALPHA; } - return rs.blend=gxScene::BLEND_REPLACE; + return rs.blend = gxScene::BLEND_REPLACE; } -int Brush::getFX()const{ +int Brush::getFX() const +{ return rep->rs.fx; } -Texture Brush::getTexture( int index )const{ +Texture Brush::getTexture(int index) const +{ return rep->texs[index]; } -const gxScene::RenderState &Brush::getRenderState()const{ +const gxScene::RenderState& Brush::getRenderState() const +{ getBlend(); - for( int k=0;kmax_tex;++k ){ - gxScene::RenderState::TexState *ts=&rep->rs.tex_states[k]; - ts->matrix=rep->texs[k].getMatrix(); - ts->blend=rep->texs[k].getBlend(); - ts->flags=rep->texs[k].getFlags(); + for (int k = 0; k < rep->max_tex; ++k) { + gxScene::RenderState::TexState* ts = &rep->rs.tex_states[k]; + ts->matrix = rep->texs[k].getMatrix(); + ts->blend = rep->texs[k].getBlend(); + ts->flags = rep->texs[k].getFlags(); } return rep->rs; } -bool Brush::operator<( const Brush &t )const{ - return memcmp( &getRenderState(),&t.getRenderState(),sizeof(gxScene::RenderState) )<0; +bool Brush::operator<(const Brush& t) const +{ + return memcmp(&getRenderState(), &t.getRenderState(), sizeof(gxScene::RenderState)) < 0; } diff --git a/Runtime/blitz3d/brush.hpp b/Runtime/blitz3d/brush.hpp index b08c432..29d0b0e 100644 --- a/Runtime/blitz3d/brush.hpp +++ b/Runtime/blitz3d/brush.hpp @@ -5,38 +5,38 @@ #include "geom.hpp" #include "texture.hpp" -class Brush{ -public: +class Brush { + public: Brush(); - Brush( const Brush &t ); - Brush( const Brush &a,const Brush &b ); + Brush(const Brush& t); + Brush(const Brush& a, const Brush& b); ~Brush(); - Brush &operator=( const Brush &t ); + Brush& operator=(const Brush& t); - void setColor( const Vector &color ); - void setAlpha( float alpha ); - void setShininess( float shininess ); - void setBlend( int blend ); - void setFX( int fx ); - void setTexture( int index,const Texture &t,int frame ); + void setColor(const Vector& color); + void setAlpha(float alpha); + void setShininess(float shininess); + void setBlend(int blend); + void setFX(int fx); + void setTexture(int index, const Texture& t, int frame); - const Vector &getColor()const; - float getAlpha()const; - float getShininess()const; - int getBlend()const; - int getFX()const; - Texture getTexture( int index )const; + const Vector& getColor() const; + float getAlpha() const; + float getShininess() const; + int getBlend() const; + int getFX() const; + Texture getTexture(int index) const; - const gxScene::RenderState &getRenderState()const; + const gxScene::RenderState& getRenderState() const; - bool operator<( const Brush &b )const; + bool operator<(const Brush& b) const; -private: + private: struct Rep; - mutable Rep *rep; + mutable Rep* rep; - Rep *write()const; + Rep* write() const; }; #endif diff --git a/Runtime/blitz3d/cachedtexture.cpp b/Runtime/blitz3d/cachedtexture.cpp index 6321545..c0efc41 100644 --- a/Runtime/blitz3d/cachedtexture.cpp +++ b/Runtime/blitz3d/cachedtexture.cpp @@ -1,165 +1,187 @@ -#include "std.hpp" #include "cachedtexture.hpp" +#include "std.hpp" int active_texs; -extern gxRuntime *gx_runtime; -extern gxGraphics *gx_graphics; +extern gxRuntime* gx_runtime; +extern gxGraphics* gx_graphics; set CachedTextureFactory::rep_set; static string path; -struct CachedTextureFactory::CachedTexture{ - int ref_cnt; - string file; - int flags,w,h,first; +struct CachedTextureFactory::CachedTexture { + int ref_cnt; + string file; + int flags, w, h, first; vector frames; - CachedTexture( int w,int h,int flags,int cnt ): - ref_cnt(1),flags(flags),w(w),h(h),first(0){ + CachedTexture(int w, int h, int flags, int cnt) : ref_cnt(1), flags(flags), w(w), h(h), first(0) + { ++active_texs; - while( cnt-->0 ){ - if( gxCanvas *t=gx_graphics->createCanvas( w,h,flags ) ){ - frames.push_back( t ); - }else break; + while (cnt-- > 0) { + if (gxCanvas* t = gx_graphics->createCanvas(w, h, flags)) { + frames.push_back(t); + } else + break; } } - CachedTexture( const string &f,int flags,int w,int h,int first,int cnt ): - ref_cnt(1),file(f),flags(flags),w(w),h(h),first(first){ + CachedTexture(const string& f, int flags, int w, int h, int first, int cnt) + : ref_cnt(1), file(f), flags(flags), w(w), h(h), first(first) + { ++active_texs; - if( !(flags & gxCanvas::CANVAS_TEX_CUBE) ){ - if( w<=0 || h<=0 || first<0 || cnt<=0 ){ - w=h=first=0; - if( gxCanvas *t=gx_graphics->loadCanvas( f,flags ) ){ - frames.push_back( t ); + if (!(flags & gxCanvas::CANVAS_TEX_CUBE)) { + if (w <= 0 || h <= 0 || first < 0 || cnt <= 0) { + w = h = first = 0; + if (gxCanvas* t = gx_graphics->loadCanvas(f, flags)) { + frames.push_back(t); } return; } } - int t_flags=flags & ( - gxCanvas::CANVAS_TEX_RGB| - gxCanvas::CANVAS_TEX_ALPHA| - gxCanvas::CANVAS_TEX_MASK| - gxCanvas::CANVAS_TEX_HICOLOR ) | gxCanvas::CANVAS_NONDISPLAY; + int t_flags = flags + & (gxCanvas::CANVAS_TEX_RGB | gxCanvas::CANVAS_TEX_ALPHA | gxCanvas::CANVAS_TEX_MASK + | gxCanvas::CANVAS_TEX_HICOLOR) + | gxCanvas::CANVAS_NONDISPLAY; - gxCanvas *t=gx_graphics->loadCanvas( f,t_flags ); - if( !t ) return; - if( !t->getDepth() ){ - gx_graphics->freeCanvas( t ); + gxCanvas* t = gx_graphics->loadCanvas(f, t_flags); + if (!t) + return; + if (!t->getDepth()) { + gx_graphics->freeCanvas(t); return; } - if( flags & gxCanvas::CANVAS_TEX_CUBE ){ - int w=t->getWidth()/6; - if( w*6!=t->getWidth() ) return; - int h=t->getHeight(); + if (flags & gxCanvas::CANVAS_TEX_CUBE) { + int w = t->getWidth() / 6; + if (w * 6 != t->getWidth()) + return; + int h = t->getHeight(); - gxCanvas *tex=gx_graphics->createCanvas( w,h,flags ); - if( tex ){ - frames.push_back( tex ); + gxCanvas* tex = gx_graphics->createCanvas(w, h, flags); + if (tex) { + frames.push_back(tex); - for( int face=0;face<6;++face ){ + for (int face = 0; face < 6; ++face) { tex->setCubeFace(face); - gx_graphics->copy( tex,0,0,tex->getWidth(),tex->getHeight(),t,face*w,0,w,h ); + gx_graphics->copy(tex, 0, 0, tex->getWidth(), tex->getHeight(), t, face * w, 0, w, h); } tex->setCubeFace(1); } - }else{ - int x_tiles=t->getWidth()/w; - int y_tiles=t->getHeight()/h; - if( first+cnt>x_tiles*y_tiles ){ - gx_graphics->freeCanvas( t ); + } else { + int x_tiles = t->getWidth() / w; + int y_tiles = t->getHeight() / h; + if (first + cnt > x_tiles * y_tiles) { + gx_graphics->freeCanvas(t); return; } - int x=(first%x_tiles)*w; - int y=(first/x_tiles)*h; - while( cnt-- ){ - gxCanvas *p=gx_graphics->createCanvas( w,h,flags ); - gx_graphics->copy( p,0,0,p->getWidth(),p->getHeight(),t,x,y,w,h ); + int x = (first % x_tiles) * w; + int y = (first / x_tiles) * h; + while (cnt--) { + gxCanvas* p = gx_graphics->createCanvas(w, h, flags); + gx_graphics->copy(p, 0, 0, p->getWidth(), p->getHeight(), t, x, y, w, h); frames.push_back(p); - x=x+w;if( x+w>t->getWidth() ){ x=0;y=y+h; } + x = x + w; + if (x + w > t->getWidth()) { + x = 0; + y = y + h; + } } } - gx_graphics->freeCanvas( t ); + gx_graphics->freeCanvas(t); } - ~CachedTexture(){ + ~CachedTexture() + { --active_texs; - for( int k=0;kfreeCanvas( frames[k] ); + for (int k = 0; k < frames.size(); ++k) + gx_graphics->freeCanvas(frames[k]); } }; -CachedTextureFactory::CachedTexture *CachedTextureFactory::findRep( const string &f,int flags,int w,int h,int first,int cnt ){ +CachedTextureFactory::CachedTexture* CachedTextureFactory::findRep(const string& f, int flags, int w, int h, int first, + int cnt) +{ set::const_iterator it; - for( it=rep_set.begin();it!=rep_set.end();++it ){ - CachedTexture *rep=*it; - if( rep->file==f && rep->flags==flags && rep->w==w && rep->h==h && rep->first==first && rep->frames.size()==cnt ){ - ++rep->ref_cnt;return rep; + for (it = rep_set.begin(); it != rep_set.end(); ++it) { + CachedTexture* rep = *it; + if (rep->file == f && rep->flags == flags && rep->w == w && rep->h == h && rep->first == first + && rep->frames.size() == cnt) { + ++rep->ref_cnt; + return rep; } } return 0; } -CachedTextureFactory::CachedTextureFactory( int w,int h,int flags,int cnt ): -rep(new CachedTexture(w,h,flags,cnt)){ -} +CachedTextureFactory::CachedTextureFactory(int w, int h, int flags, int cnt) : rep(new CachedTexture(w, h, flags, cnt)) +{} -CachedTextureFactory::CachedTextureFactory( const string &f_,int flags,int w,int h,int first,int cnt ){ - string f=f_; - if( f.substr(0,2)==".\\" ) f=f.substr(2); - if( path.size() ){ - string t=path+tolower( filenamefile( f ) ); - if( rep=findRep( t,flags,w,h,first,cnt ) ) return; - rep=new CachedTexture( t,flags,w,h,first,cnt ); - if( rep->frames.size() ){ - rep_set.insert( rep ); +CachedTextureFactory::CachedTextureFactory(const string& f_, int flags, int w, int h, int first, int cnt) +{ + string f = f_; + if (f.substr(0, 2) == ".\\") + f = f.substr(2); + if (path.size()) { + string t = path + tolower(filenamefile(f)); + if (rep = findRep(t, flags, w, h, first, cnt)) + return; + rep = new CachedTexture(t, flags, w, h, first, cnt); + if (rep->frames.size()) { + rep_set.insert(rep); return; } delete rep; } - string t=tolower( fullfilename( f ) ); - if( rep=findRep( t,flags,w,h,first,cnt ) ) return; - rep=new CachedTexture( t,flags,w,h,first,cnt ); - rep_set.insert( rep ); + string t = tolower(fullfilename(f)); + if (rep = findRep(t, flags, w, h, first, cnt)) + return; + rep = new CachedTexture(t, flags, w, h, first, cnt); + rep_set.insert(rep); } -CachedTextureFactory::CachedTextureFactory( const CachedTextureFactory &t ): -rep(t.rep){ +CachedTextureFactory::CachedTextureFactory(const CachedTextureFactory& t) : rep(t.rep) +{ ++rep->ref_cnt; } -CachedTextureFactory::~CachedTextureFactory(){ - if( !--rep->ref_cnt ){ - rep_set.erase( rep ); +CachedTextureFactory::~CachedTextureFactory() +{ + if (!--rep->ref_cnt) { + rep_set.erase(rep); delete rep; } } -CachedTextureFactory &CachedTextureFactory::operator=( const CachedTextureFactory &t ){ +CachedTextureFactory& CachedTextureFactory::operator=(const CachedTextureFactory& t) +{ ++t.rep->ref_cnt; - if( !--rep->ref_cnt ){ - rep_set.erase( rep ); + if (!--rep->ref_cnt) { + rep_set.erase(rep); delete rep; } - rep=t.rep; + rep = t.rep; return *this; } -string CachedTextureFactory::getName()const{ +string CachedTextureFactory::getName() const +{ return rep->file; } -const vector &CachedTextureFactory::getFrames()const{ +const vector& CachedTextureFactory::getFrames() const +{ return rep->frames; } -void CachedTextureFactory::setPath( const string &t ){ - path=tolower(t); - if( int sz=path.size() ){ - if( path[sz-1]!='/' && path[sz-1]!='\\' ) path+='\\'; +void CachedTextureFactory::setPath(const string& t) +{ + path = tolower(t); + if (int sz = path.size()) { + if (path[sz - 1] != '/' && path[sz - 1] != '\\') + path += '\\'; } } \ No newline at end of file diff --git a/Runtime/blitz3d/cachedtexture.hpp b/Runtime/blitz3d/cachedtexture.hpp index c111294..bbd5cb7 100644 --- a/Runtime/blitz3d/cachedtexture.hpp +++ b/Runtime/blitz3d/cachedtexture.hpp @@ -2,30 +2,33 @@ #ifndef CACHEDTEXTURE_H #define CACHEDTEXTURE_H -#include "../gxruntime/gxcanvas.hpp" +#include "gxcanvas.hpp" -class CachedTextureFactory{ -public: - CachedTextureFactory( int w,int h,int flags,int cnt ); - CachedTextureFactory( const string &f,int flags,int w,int h,int first,int cnt ); - CachedTextureFactory( const CachedTextureFactory &t ); +class CachedTextureFactory { + public: + CachedTextureFactory(int w, int h, int flags, int cnt); + CachedTextureFactory(const string& f, int flags, int w, int h, int first, int cnt); + CachedTextureFactory(const CachedTextureFactory& t); ~CachedTextureFactory(); - CachedTextureFactory &operator=( const CachedTextureFactory &t ); + CachedTextureFactory& operator=(const CachedTextureFactory& t); - string getName()const; + string getName() const; - const vector &getFrames()const; + const vector& getFrames() const; - bool operator<( const CachedTextureFactory &t )const{ return rep rep_set; }; diff --git a/Runtime/blitz3d/camera.cpp b/Runtime/blitz3d/camera.cpp index 19960d2..e52c267 100644 --- a/Runtime/blitz3d/camera.cpp +++ b/Runtime/blitz3d/camera.cpp @@ -1,103 +1,132 @@ -#include "std.hpp" #include "camera.hpp" +#include "std.hpp" -extern gxScene *gx_scene; +extern gxScene* gx_scene; -Camera::Camera(){ - setZoom( 1 ); - setRange( 1,1000 ); - setViewport( 0,0,0,0 ); - setClsColor( Vector() ); - setClsMode( true,true ); - setProjMode( PROJ_PERSP ); - setFogRange( 1,1000 ); - setFogColor( Vector() ); - setFogMode( gxScene::FOG_NONE ); +Camera::Camera() +{ + setZoom(1); + setRange(1, 1000); + setViewport(0, 0, 0, 0); + setClsColor(Vector()); + setClsMode(true, true); + setProjMode(PROJ_PERSP); + setFogRange(1, 1000); + setFogColor(Vector()); + setFogMode(gxScene::FOG_NONE); } -void Camera::setZoom( float z ){ - zoom=z; - local_valid=false; +void Camera::setZoom(float z) +{ + zoom = z; + local_valid = false; } -void Camera::setRange( float n,float f ){ - frustum_nr=n;frustum_fr=f; - local_valid=false; +void Camera::setRange(float n, float f) +{ + frustum_nr = n; + frustum_fr = f; + local_valid = false; } -void Camera::setViewport( int x,int y,int w,int h ){ - vp_x=x;vp_y=y;vp_w=w;vp_h=h; - local_valid=false; +void Camera::setViewport(int x, int y, int w, int h) +{ + vp_x = x; + vp_y = y; + vp_w = w; + vp_h = h; + local_valid = false; } -void Camera::setClsColor( const Vector &v ){ - cls_color=v; +void Camera::setClsColor(const Vector& v) +{ + cls_color = v; } -void Camera::setClsMode( bool c,bool z ){ - cls_argb=c;cls_z=z; +void Camera::setClsMode(bool c, bool z) +{ + cls_argb = c; + cls_z = z; } -void Camera::setProjMode( int mode ){ - proj_mode=mode; +void Camera::setProjMode(int mode) +{ + proj_mode = mode; } -void Camera::setFogColor( const Vector &v ){ - fog_color=v; +void Camera::setFogColor(const Vector& v) +{ + fog_color = v; } -void Camera::setFogRange( float nr,float fr ){ - fog_nr=nr;fog_fr=fr; +void Camera::setFogRange(float nr, float fr) +{ + fog_nr = nr; + fog_fr = fr; } -void Camera::setFogMode( int mode ){ - fog_mode=mode; +void Camera::setFogMode(int mode) +{ + fog_mode = mode; } -const Frustum &Camera::getFrustum()const{ - if( !local_valid ){ - float ar=(float)vp_h/vp_w; - frustum_w=frustum_nr*2/zoom; - frustum_h=frustum_nr*2/zoom*ar; - new( &local_frustum ) Frustum( frustum_nr,frustum_fr,frustum_w,frustum_h ); - local_valid=true; +const Frustum& Camera::getFrustum() const +{ + if (!local_valid) { + float ar = (float)vp_h / vp_w; + frustum_w = frustum_nr * 2 / zoom; + frustum_h = frustum_nr * 2 / zoom * ar; + new (&local_frustum) Frustum(frustum_nr, frustum_fr, frustum_w, frustum_h); + local_valid = true; } return local_frustum; } -float Camera::getFrustumNear()const{ +float Camera::getFrustumNear() const +{ return frustum_nr; } -float Camera::getFrustumFar()const{ +float Camera::getFrustumFar() const +{ return frustum_fr; } -float Camera::getFrustumWidth()const{ - getFrustum();return frustum_w; -} - -float Camera::getFrustumHeight()const{ - getFrustum();return frustum_h; -} - -void Camera::getViewport( int *x,int *y,int *w,int *h )const{ - *x=vp_x;*y=vp_y;*w=vp_w;*h=vp_h; -} - -bool Camera::beginRenderFrame(){ - if( !proj_mode ) return false; +float Camera::getFrustumWidth() const +{ getFrustum(); - gx_scene->setViewport( vp_x,vp_y,vp_w,vp_h ); - gx_scene->clear( &(cls_color.x),1,1,cls_argb,cls_z ); - if( proj_mode==PROJ_ORTHO ){ - gx_scene->setOrthoProj( frustum_nr,frustum_fr,frustum_w,frustum_h ); - }else{ - gx_scene->setPerspProj( frustum_nr,frustum_fr,frustum_w,frustum_h ); + return frustum_w; +} + +float Camera::getFrustumHeight() const +{ + getFrustum(); + return frustum_h; +} + +void Camera::getViewport(int* x, int* y, int* w, int* h) const +{ + *x = vp_x; + *y = vp_y; + *w = vp_w; + *h = vp_h; +} + +bool Camera::beginRenderFrame() +{ + if (!proj_mode) + return false; + getFrustum(); + gx_scene->setViewport(vp_x, vp_y, vp_w, vp_h); + gx_scene->clear(&(cls_color.x), 1, 1, cls_argb, cls_z); + if (proj_mode == PROJ_ORTHO) { + gx_scene->setOrthoProj(frustum_nr, frustum_fr, frustum_w, frustum_h); + } else { + gx_scene->setPerspProj(frustum_nr, frustum_fr, frustum_w, frustum_h); } - gx_scene->setFogRange( fog_nr,fog_fr ); - gx_scene->setFogColor( (float*)&fog_color.x ); - gx_scene->setFogMode( fog_mode ); + gx_scene->setFogRange(fog_nr, fog_fr); + gx_scene->setFogColor((float*)&fog_color.x); + gx_scene->setFogMode(fog_mode); return true; } diff --git a/Runtime/blitz3d/camera.hpp b/Runtime/blitz3d/camera.hpp index f4479a3..b21428c 100644 --- a/Runtime/blitz3d/camera.hpp +++ b/Runtime/blitz3d/camera.hpp @@ -2,55 +2,59 @@ #ifndef CAMERA_H #define CAMERA_H -#include "model.hpp" #include "frustum.hpp" #include "mirror.hpp" +#include "model.hpp" -class Camera : public Object{ -public: - enum{ - PROJ_NONE=0,PROJ_PERSP=1,PROJ_ORTHO=2 - }; +class Camera : public Object { + public: + enum { PROJ_NONE = 0, PROJ_PERSP = 1, PROJ_ORTHO = 2 }; Camera(); - Camera *getCamera(){ return this; } + Camera* getCamera() + { + return this; + } //called by user - void setZoom( float z ); - void setRange( float nr,float fr ); - void setViewport( int x,int y,int w,int h ); - void setClsColor( const Vector &v ); - void setClsMode( bool cls_argb,bool cls_z ); - void setProjMode( int mode ); - void setFogColor( const Vector &v ); - void setFogRange( float nr,float fr ); - void setFogMode( int mode ); + void setZoom(float z); + void setRange(float nr, float fr); + void setViewport(int x, int y, int w, int h); + void setClsColor(const Vector& v); + void setClsMode(bool cls_argb, bool cls_z); + void setProjMode(int mode); + void setFogColor(const Vector& v); + void setFogRange(float nr, float fr); + void setFogMode(int mode); //called by world bool beginRenderFrame(); //Camera frustum... - float getFrustumNear()const; - float getFrustumFar()const; - float getFrustumWidth()const; - float getFrustumHeight()const; - const Frustum &getFrustum()const; - void getViewport( int *x,int *y,int *w,int *h )const; - int getProjMode()const{ return proj_mode; } + float getFrustumNear() const; + float getFrustumFar() const; + float getFrustumWidth() const; + float getFrustumHeight() const; + const Frustum& getFrustum() const; + void getViewport(int* x, int* y, int* w, int* h) const; + int getProjMode() const + { + return proj_mode; + } -private: - float zoom; - int vp_x,vp_y,vp_w,vp_h; - Vector cls_color; - bool cls_argb,cls_z; - int proj_mode; - Vector fog_color; - float fog_nr,fog_fr; - int fog_mode; - float frustum_nr,frustum_fr; - mutable float frustum_w,frustum_h; + private: + float zoom; + int vp_x, vp_y, vp_w, vp_h; + Vector cls_color; + bool cls_argb, cls_z; + int proj_mode; + Vector fog_color; + float fog_nr, fog_fr; + int fog_mode; + float frustum_nr, frustum_fr; + mutable float frustum_w, frustum_h; mutable Frustum local_frustum; - mutable bool local_valid; + mutable bool local_valid; }; #endif diff --git a/Runtime/blitz3d/collision.cpp b/Runtime/blitz3d/collision.cpp index 1d4a4ee..eb8e079 100644 --- a/Runtime/blitz3d/collision.cpp +++ b/Runtime/blitz3d/collision.cpp @@ -1,8 +1,8 @@ -#include "std.hpp" #include "collision.hpp" +#include "std.hpp" -const float COLLISION_FLT_EPSILON=.001f; +const float COLLISION_FLT_EPSILON = .001f; /* // @@ -195,167 +195,168 @@ bool Collision::boxCollide( const Line &line,float radius,const Box &box ){ } */ -bool Collision::update( const Line &line,float t,const Vector &n ){ +bool Collision::update(const Line& line, float t, const Vector& n) +{ + // if( t<0 || t>time ) return false; -// if( t<0 || t>time ) return false; + if (t > time) + return false; + Plane p(line * t, n); + if (p.n.dot(line.d) >= 0) + return false; + if (p.distance(line.o) < -COLLISION_FLT_EPSILON) + return false; - if( t>time ) return false; - Plane p(line*t,n); - if( p.n.dot( line.d )>=0 ) return false; - if( p.distance(line.o)<-COLLISION_FLT_EPSILON ) return false; - - time=t; - normal=n; + time = t; + normal = n; return true; } // // NEW VERSION // -extern gxRuntime *gx_runtime; +extern gxRuntime* gx_runtime; -bool Collision::sphereCollide( const Line &line,float radius,const Vector &dest,float dest_radius ){ +bool Collision::sphereCollide(const Line& line, float radius, const Vector& dest, float dest_radius) +{ + radius += dest_radius; + Line l(line.o - dest, line.d); - radius+=dest_radius; - Line l( line.o-dest,line.d ); + float a = l.d.dot(l.d); + if (!a) + return false; + float b = l.o.dot(l.d) * 2; + float c = l.o.dot(l.o) - radius * radius; + float d = b * b - 4 * a * c; + if (d < 0) + return false; - float a=l.d.dot(l.d); - if( !a ) return false; - float b=l.o.dot(l.d)*2; - float c=l.o.dot(l.o)-radius*radius; - float d=b*b-4*a*c; - if( d<0 ) return false; + float t1 = (-b + sqrt(d)) / (2 * a); + float t2 = (-b - sqrt(d)) / (2 * a); - float t1=(-b+sqrt(d))/(2*a); - float t2=(-b-sqrt(d))/(2*a); + float t = t1 < t2 ? t1 : t2; - float t=t1 time) + return false; - if( t>time ) return false; - - return update( line,t,(l*t).normalized() ); + return update(line, t, (l * t).normalized()); } //v0,v1 = edge verts //pn = poly normal //en = edge normal -static bool edgeTest( const Vector &v0,const Vector &v1,const Vector &pn,const Vector &en,const Line &line,float radius,Collision *curr_coll ){ - - Matrix tm=~Matrix( en,(v1-v0).normalized(),pn ); - Vector sv=tm*(line.o-v0),dv=tm*(line.o+line.d-v0); - Line l( sv,dv-sv ); +static bool edgeTest(const Vector& v0, const Vector& v1, const Vector& pn, const Vector& en, const Line& line, + float radius, Collision* curr_coll) +{ + Matrix tm = ~Matrix(en, (v1 - v0).normalized(), pn); + Vector sv = tm * (line.o - v0), dv = tm * (line.o + line.d - v0); + Line l(sv, dv - sv); //do cylinder test... - float a,b,c,d,t1,t2,t; - a=(l.d.x*l.d.x+l.d.z*l.d.z); - if( !a ) return false; //ray parallel to cylinder - b=(l.o.x*l.d.x+l.o.z*l.d.z)*2; - c=(l.o.x*l.o.x+l.o.z*l.o.z)-radius*radius; - d=b*b-4*a*c; - if( d<0 ) return false; //ray misses cylinder - t1=(-b+sqrt(d))/(2*a); - t2=(-b-sqrt(d))/(2*a); - t=t1curr_coll->time ) return false; //intersects too far away - Vector i=l*t,p; - if( i.y>v0.distance(v1) ) return false; //intersection above cylinder - if( i.y>=0 ){ - p.y=i.y; - }else{ + float a, b, c, d, t1, t2, t; + a = (l.d.x * l.d.x + l.d.z * l.d.z); + if (!a) + return false; //ray parallel to cylinder + b = (l.o.x * l.d.x + l.o.z * l.d.z) * 2; + c = (l.o.x * l.o.x + l.o.z * l.o.z) - radius * radius; + d = b * b - 4 * a * c; + if (d < 0) + return false; //ray misses cylinder + t1 = (-b + sqrt(d)) / (2 * a); + t2 = (-b - sqrt(d)) / (2 * a); + t = t1 < t2 ? t1 : t2; + if (t > curr_coll->time) + return false; //intersects too far away + Vector i = l * t, p; + if (i.y > v0.distance(v1)) + return false; //intersection above cylinder + if (i.y >= 0) { + p.y = i.y; + } else { //below bottom of cylinder...do sphere test... - a=l.d.dot(l.d); - if( !a ) return false; //ray parallel to sphere - b=l.o.dot(l.d)*2; - c=l.o.dot(l.o)-radius*radius; - d=b*b-4*a*c; - if( d<0 ) return false; //ray misses sphere - t1=(-b+sqrt(d))/(2*a); - t2=(-b-sqrt(d))/(2*a); - t=t1curr_coll->time ) return false; - i=l*t; + a = l.d.dot(l.d); + if (!a) + return false; //ray parallel to sphere + b = l.o.dot(l.d) * 2; + c = l.o.dot(l.o) - radius * radius; + d = b * b - 4 * a * c; + if (d < 0) + return false; //ray misses sphere + t1 = (-b + sqrt(d)) / (2 * a); + t2 = (-b - sqrt(d)) / (2 * a); + t = t1 < t2 ? t1 : t2; + if (t > curr_coll->time) + return false; + i = l * t; } - return curr_coll->update( line,t,(~tm*(i-p)).normalized() ); + return curr_coll->update(line, t, (~tm * (i - p)).normalized()); } -bool Collision::triangleCollide( const Line &line,float radius,const Vector &v0,const Vector &v1,const Vector &v2 ){ - +bool Collision::triangleCollide(const Line& line, float radius, const Vector& v0, const Vector& v1, const Vector& v2) +{ //triangle plane - Plane p( v0,v1,v2 ); - if( p.n.dot( line.d )>=0 ) return false; + Plane p(v0, v1, v2); + if (p.n.dot(line.d) >= 0) + return false; //move plane out - p.d-=radius; - float t=p.t_intersect( line ); - if( t>time ) return false; + p.d -= radius; + float t = p.t_intersect(line); + if (t > time) + return false; //edge planes - Plane p0( v0+p.n,v1,v0 ),p1( v1+p.n,v2,v1 ),p2( v2+p.n,v0,v2 ); + Plane p0(v0 + p.n, v1, v0), p1(v1 + p.n, v2, v1), p2(v2 + p.n, v0, v2); //intersects triangle? - Vector i=line*t; - if( p0.distance(i)>=0 && p1.distance(i)>=0 && p2.distance(i)>=0 ){ - return update( line,t,p.n ); + Vector i = line * t; + if (p0.distance(i) >= 0 && p1.distance(i) >= 0 && p2.distance(i) >= 0) { + return update(line, t, p.n); } - if( radius<=0 ) return false; + if (radius <= 0) + return false; - return - edgeTest( v0,v1,p.n,p0.n,line,radius,this )| - edgeTest( v1,v2,p.n,p1.n,line,radius,this )| - edgeTest( v2,v0,p.n,p2.n,line,radius,this ); + return edgeTest(v0, v1, p.n, p0.n, line, radius, this) | edgeTest(v1, v2, p.n, p1.n, line, radius, this) + | edgeTest(v2, v0, p.n, p2.n, line, radius, this); } -bool Collision::boxCollide( const Line &line,float radius,const Box &box ){ +bool Collision::boxCollide(const Line& line, float radius, const Box& box) +{ + static int quads[] = {2, 3, 1, 0, 3, 7, 5, 1, 7, 6, 4, 5, 6, 2, 0, 4, 6, 7, 3, 2, 0, 1, 5, 4}; - static int quads[]={ - 2,3,1,0, - 3,7,5,1, - 7,6,4,5, - 6,2,0,4, - 6,7,3,2, - 0,1,5,4 - }; + bool hit = false; - bool hit=false; - - for( int n=0;n<24;n+=4 ){ - Vector - v0( box.corner( quads[n] ) ), - v1( box.corner( quads[n+1] ) ), - v2( box.corner( quads[n+2] ) ), - v3( box.corner( quads[n+3] ) ); + for (int n = 0; n < 24; n += 4) { + Vector v0(box.corner(quads[n])), v1(box.corner(quads[n + 1])), v2(box.corner(quads[n + 2])), + v3(box.corner(quads[n + 3])); //quad plane - Plane p( v0,v1,v2 ); - if( p.n.dot( line.d )>=0 ) continue; - + Plane p(v0, v1, v2); + if (p.n.dot(line.d) >= 0) + continue; + //move plane out - p.d-=radius; - float t=p.t_intersect( line ); - if( t>time ) return false; + p.d -= radius; + float t = p.t_intersect(line); + if (t > time) + return false; //edge planes - Plane - p0( v0+p.n,v1,v0 ), - p1( v1+p.n,v2,v1 ), - p2( v2+p.n,v3,v2 ), - p3( v3+p.n,v0,v3 ); + Plane p0(v0 + p.n, v1, v0), p1(v1 + p.n, v2, v1), p2(v2 + p.n, v3, v2), p3(v3 + p.n, v0, v3); //intersects triangle? - Vector i=line*t; - if( p0.distance(i)>=0 && p1.distance(i)>=0 && p2.distance(i)>=0 && p3.distance(i)>=0 ){ - hit|=update( line,t,p.n ); + Vector i = line * t; + if (p0.distance(i) >= 0 && p1.distance(i) >= 0 && p2.distance(i) >= 0 && p3.distance(i) >= 0) { + hit |= update(line, t, p.n); continue; } - if( radius<=0 ) continue; + if (radius <= 0) + continue; - hit|= - edgeTest( v0,v1,p.n,p0.n,line,radius,this )| - edgeTest( v1,v2,p.n,p1.n,line,radius,this )| - edgeTest( v2,v3,p.n,p2.n,line,radius,this )| - edgeTest( v3,v0,p.n,p3.n,line,radius,this ); + hit |= edgeTest(v0, v1, p.n, p0.n, line, radius, this) | edgeTest(v1, v2, p.n, p1.n, line, radius, this) + | edgeTest(v2, v3, p.n, p2.n, line, radius, this) | edgeTest(v3, v0, p.n, p3.n, line, radius, this); } return hit; } diff --git a/Runtime/blitz3d/collision.hpp b/Runtime/blitz3d/collision.hpp index 940e4b0..0f7f769 100644 --- a/Runtime/blitz3d/collision.hpp +++ b/Runtime/blitz3d/collision.hpp @@ -6,22 +6,22 @@ extern const float COLLISION_FLT_EPSILON; -struct Collision{ - float time; - Vector normal; - void *surface; +struct Collision { + float time; + Vector normal; + void* surface; unsigned short index; - Collision():time(1),surface(0),index(~0){} + Collision() : time(1), surface(0), index(~0) {} - bool update( const Line &line,float time,const Vector &normal ); + bool update(const Line& line, float time, const Vector& normal); - bool sphereCollide( const Line &src_line,float src_radius,const Vector &dest,float dest_radius ); - bool sphereCollide( const Line &line,float radius,const Vector &dest,const Vector &radii ); + bool sphereCollide(const Line& src_line, float src_radius, const Vector& dest, float dest_radius); + bool sphereCollide(const Line& line, float radius, const Vector& dest, const Vector& radii); - bool triangleCollide( const Line &src_line,float src_radius,const Vector &v0,const Vector &v1,const Vector &v2 ); + bool triangleCollide(const Line& src_line, float src_radius, const Vector& v0, const Vector& v1, const Vector& v2); - bool boxCollide( const Line &src_line,float src_radius,const Box &box ); + bool boxCollide(const Line& src_line, float src_radius, const Box& box); }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/emitter.cpp b/Runtime/blitz3d/emitter.cpp index d9e72ff..dc36ea0 100644 --- a/Runtime/blitz3d/emitter.cpp +++ b/Runtime/blitz3d/emitter.cpp @@ -1,42 +1,42 @@ -#include "std.hpp" #include "emitter.hpp" +#include "std.hpp" -Emitter::Emitter(){ -} +Emitter::Emitter() {} -Emitter::Emitter( const Emitter &t ){ -} +Emitter::Emitter(const Emitter& t) {} -Emitter::~Emitter(){ -} +Emitter::~Emitter() {} -void Emitter::beginRender( float tween ){ - Object::beginRender( tween ); +void Emitter::beginRender(float tween) +{ + Object::beginRender(tween); - vel=getRenderTform().v-pos; - pos=getRenderTform().v; + vel = getRenderTform().v - pos; + pos = getRenderTform().v; - for( int k=0;kisPlaying() ){ - channels[k]=0; + for (int k = 0; k < channels.size(); ++k) { + gxChannel* chan = channels[k]; + if (!chan->isPlaying()) { + channels[k] = 0; continue; } - chan->set3d( &pos.x,&vel.x ); + chan->set3d(&pos.x, &vel.x); } } -gxChannel *Emitter::emitSound( gxSound *sound ){ +gxChannel* Emitter::emitSound(gxSound* sound) +{ + gxChannel* chan = sound->play3d(&pos.x, &vel.x); - gxChannel *chan=sound->play3d( &pos.x,&vel.x ); - - for( int k=0;k channels; }; diff --git a/Runtime/blitz3d/entity.cpp b/Runtime/blitz3d/entity.cpp index fa64955..f3ee3e9 100644 --- a/Runtime/blitz3d/entity.cpp +++ b/Runtime/blitz3d/entity.cpp @@ -1,79 +1,91 @@ -#include "std.hpp" #include "entity.hpp" +#include "std.hpp" //#include "stats.hpp" Entity *Entity::_orphans, *Entity::_last_orphan; -enum { - INVALID_LOCALTFORM = 1, - INVALID_WORLDTFORM = 2 -}; +enum { INVALID_LOCALTFORM = 1, INVALID_WORLDTFORM = 2 }; -void Entity::RemoveParent() { +void Entity::RemoveParent() +{ if (m_parent) { - if (m_parent->m_children == this) m_parent->m_children = m_listNext; - if (m_parent->m_last_child == this) m_parent->m_last_child = m_listPrev; + if (m_parent->m_children == this) + m_parent->m_children = m_listNext; + if (m_parent->m_last_child == this) + m_parent->m_last_child = m_listPrev; } else { - if (_orphans == this) _orphans = m_listNext; - if (_last_orphan == this) _last_orphan = m_listPrev; + if (_orphans == this) + _orphans = m_listNext; + if (_last_orphan == this) + _last_orphan = m_listPrev; } - if (m_listNext) m_listNext->m_listPrev = m_listPrev; - if (m_listPrev) m_listPrev->m_listNext = m_listNext; + if (m_listNext) + m_listNext->m_listPrev = m_listPrev; + if (m_listPrev) + m_listPrev->m_listNext = m_listNext; } -void Entity::InsertChildToParent() { +void Entity::InsertChildToParent() +{ m_listNext = 0; if (m_parent) { - if (m_listPrev = m_parent->m_last_child) m_listPrev->m_listNext = this; - else m_parent->m_children = this; + if (m_listPrev = m_parent->m_last_child) + m_listPrev->m_listNext = this; + else + m_parent->m_children = this; m_parent->m_last_child = this; } else { - if (m_listPrev = _last_orphan) m_listPrev->m_listNext = this; - else _orphans = this; + if (m_listPrev = _last_orphan) + m_listPrev->m_listNext = this; + else + _orphans = this; _last_orphan = this; } } -Entity::Entity() : - m_listNext(0), m_listPrev(0), m_parent(0), m_children(0), m_last_child(0), - m_isVisible(true), m_isEnabled(true), - m_localScale(1, 1, 1), - invalid(0) { +Entity::Entity() + : m_listNext(0), m_listPrev(0), m_parent(0), m_children(0), m_last_child(0), m_isVisible(true), m_isEnabled(true), + m_localScale(1, 1, 1), invalid(0) +{ InsertChildToParent(); } -Entity::Entity(const Entity &e) : - m_listNext(0), m_listPrev(0), m_parent(0), m_children(0), m_last_child(0), - m_name(e.m_name), m_isVisible(e.m_isVisible), m_isEnabled(e.m_isEnabled), - m_localPosition(e.m_localPosition), - m_localScale(e.m_localScale), - m_localRotation(e.m_localRotation), - invalid(INVALID_LOCALTFORM | INVALID_WORLDTFORM) { +Entity::Entity(const Entity& e) + : m_listNext(0), m_listPrev(0), m_parent(0), m_children(0), m_last_child(0), m_name(e.m_name), + m_isVisible(e.m_isVisible), m_isEnabled(e.m_isEnabled), m_localPosition(e.m_localPosition), + m_localScale(e.m_localScale), m_localRotation(e.m_localRotation), invalid(INVALID_LOCALTFORM | INVALID_WORLDTFORM) +{ InsertChildToParent(); } -Entity::~Entity() { - while (GetChildren()) delete GetChildren(); +Entity::~Entity() +{ + while (GetChildren()) + delete GetChildren(); RemoveParent(); } -void Entity::InvalidateWorldTransform() { - if (invalid & INVALID_WORLDTFORM) return; +void Entity::InvalidateWorldTransform() +{ + if (invalid & INVALID_WORLDTFORM) + return; invalid |= INVALID_WORLDTFORM; - for (Entity *e = m_children; e; e = e->m_listNext) { + for (Entity* e = m_children; e; e = e->m_listNext) { e->InvalidateWorldTransform(); } } -void Entity::invalidateLocal() { +void Entity::invalidateLocal() +{ invalid |= INVALID_LOCALTFORM; InvalidateWorldTransform(); } -const Transform &Entity::GetLocalTransform()const { - if (invalid&INVALID_LOCALTFORM) { +const Transform& Entity::GetLocalTransform() const +{ + if (invalid & INVALID_LOCALTFORM) { m_localTransform.m = Matrix(m_localRotation); m_localTransform.m.i *= m_localScale.x; m_localTransform.m.j *= m_localScale.y; @@ -84,16 +96,19 @@ const Transform &Entity::GetLocalTransform()const { return m_localTransform; } -const Transform &Entity::GetWorldTransform()const { - if (invalid&INVALID_WORLDTFORM) { +const Transform& Entity::GetWorldTransform() const +{ + if (invalid & INVALID_WORLDTFORM) { m_worldTransform = m_parent ? m_parent->GetWorldTransform() * GetLocalTransform() : GetLocalTransform(); invalid &= ~INVALID_WORLDTFORM; } return m_worldTransform; } -void Entity::SetParent(Entity *p) { - if (m_parent == p) return; +void Entity::SetParent(Entity* p) +{ + if (m_parent == p) + return; RemoveParent(); @@ -104,94 +119,117 @@ void Entity::SetParent(Entity *p) { InvalidateWorldTransform(); } -void Entity::SetName(const string &t) { +void Entity::SetName(const string& t) +{ m_name = t; } -void Entity::SetVisible(bool visible) { +void Entity::SetVisible(bool visible) +{ m_isVisible = visible; } -void Entity::SetEnabled(bool enabled) { +void Entity::SetEnabled(bool enabled) +{ m_isEnabled = enabled; } -void Entity::EnumerateVisible(std::list &out) { - if (!m_isVisible) return; - if (Object *o = getObject()) out.push_back(o); - for (Entity *e = m_children; e; e = e->m_listNext) { +void Entity::EnumerateVisible(std::list& out) +{ + if (!m_isVisible) + return; + if (Object* o = getObject()) + out.push_back(o); + for (Entity* e = m_children; e; e = e->m_listNext) { e->EnumerateVisible(out); } } -void Entity::EnumerateEnabled(std::list &out) { - if (!m_isEnabled) return; - if (Object *o = getObject()) out.push_back(o); - for (Entity *e = m_children; e; e = e->m_listNext) { +void Entity::EnumerateEnabled(std::list& out) +{ + if (!m_isEnabled) + return; + if (Object* o = getObject()) + out.push_back(o); + for (Entity* e = m_children; e; e = e->m_listNext) { e->EnumerateEnabled(out); } } -void Entity::SetLocalPosition(const Vector &v) { +void Entity::SetLocalPosition(const Vector& v) +{ m_localPosition = v; invalidateLocal(); } -void Entity::SetLocalScale(const Vector &v) { +void Entity::SetLocalScale(const Vector& v) +{ m_localScale = v; invalidateLocal(); } -void Entity::SetLocalRotation(const Quat &q) { +void Entity::SetLocalRotation(const Quat& q) +{ m_localRotation = q.normalized(); invalidateLocal(); } -void Entity::SetLocalTransform(const Transform &t) { +void Entity::SetLocalTransform(const Transform& t) +{ m_localPosition = t.v; - m_localScale = Vector(t.m.i.length(), t.m.j.length(), t.m.k.length()); + m_localScale = Vector(t.m.i.length(), t.m.j.length(), t.m.k.length()); m_localRotation = matrixQuat(t.m); invalidateLocal(); } -void Entity::SetWorldPosition(const Vector &v) { +void Entity::SetWorldPosition(const Vector& v) +{ SetLocalPosition(m_parent ? -m_parent->GetWorldTransform() * v : v); } -void Entity::SetWorldScale(const Vector &v) { +void Entity::SetWorldScale(const Vector& v) +{ SetLocalScale(m_parent ? v / m_parent->GetWorldScale() : v); } -void Entity::SetWorldRotation(const Quat &q) { +void Entity::SetWorldRotation(const Quat& q) +{ SetLocalRotation(m_parent ? -m_parent->GetWorldRotation() * q : q); } -void Entity::SetWorldTransform(const Transform &t) { +void Entity::SetWorldTransform(const Transform& t) +{ SetLocalTransform(m_parent ? -m_parent->GetWorldTransform() * t : t); } -const Vector &Entity::GetLocalPosition()const { +const Vector& Entity::GetLocalPosition() const +{ return m_localPosition; } -const Vector &Entity::GetLocalScale()const { +const Vector& Entity::GetLocalScale() const +{ return m_localScale; } -const Quat &Entity::GetLocalRotation()const { +const Quat& Entity::GetLocalRotation() const +{ return m_localRotation; } -const Vector &Entity::GetWorldPosition()const { +const Vector& Entity::GetWorldPosition() const +{ return GetWorldTransform().v; } -const Vector &Entity::GetWorldScale()const { +const Vector& Entity::GetWorldScale() const +{ m_worldScale = m_parent ? m_parent->GetWorldScale() * m_localScale : m_localScale; return m_worldScale; } -const Quat &Entity::GetWorldRotation()const { +const Quat& Entity::GetWorldRotation() const +{ m_worldRotation = m_parent ? m_parent->GetWorldRotation() * m_localRotation : m_localRotation; return m_worldRotation; } diff --git a/Runtime/blitz3d/entity.hpp b/Runtime/blitz3d/entity.hpp index 08893da..02cb8d8 100644 --- a/Runtime/blitz3d/entity.hpp +++ b/Runtime/blitz3d/entity.hpp @@ -18,58 +18,95 @@ class MD2Model; class Entity { public: - Entity(); - Entity(const Entity &e); + Entity(const Entity& e); virtual ~Entity(); - virtual Entity *clone() = 0; + virtual Entity* clone() = 0; //ugly casts! - virtual Object *getObject() { return nullptr; } - virtual Camera *getCamera() { return nullptr; } - virtual Light *getLight() { return nullptr; } - virtual Model *getModel() { return nullptr; } - virtual Mirror *getMirror() { return nullptr; } - virtual Listener *getListener() { return nullptr; } + virtual Object* getObject() + { + return nullptr; + } + virtual Camera* getCamera() + { + return nullptr; + } + virtual Light* getLight() + { + return nullptr; + } + virtual Model* getModel() + { + return nullptr; + } + virtual Mirror* getMirror() + { + return nullptr; + } + virtual Listener* getListener() + { + return nullptr; + } - void SetName(const std::string &t); - std::string getName()const { return m_name; } + void SetName(const std::string& t); + std::string getName() const + { + return m_name; + } - void SetParent(Entity *parent); - Entity *getParent()const { return m_parent; } + void SetParent(Entity* parent); + Entity* getParent() const + { + return m_parent; + } void SetVisible(bool vis); - bool IsVisible()const { return m_isVisible; } - void EnumerateVisible(std::list &out); + bool IsVisible() const + { + return m_isVisible; + } + void EnumerateVisible(std::list& out); void SetEnabled(bool ena); - bool IsEnabled()const { return m_isEnabled; } - void EnumerateEnabled(std::list &out); + bool IsEnabled() const + { + return m_isEnabled; + } + void EnumerateEnabled(std::list& out); - void SetLocalPosition(const Vector &v); - const Vector &GetLocalPosition()const; - void SetLocalScale(const Vector & v); - const Vector &GetLocalScale()const; - void SetLocalRotation(const Quat &q); - const Quat &GetLocalRotation()const; - void SetLocalTransform(const Transform &t); - const Transform &GetLocalTransform()const; + void SetLocalPosition(const Vector& v); + const Vector& GetLocalPosition() const; + void SetLocalScale(const Vector& v); + const Vector& GetLocalScale() const; + void SetLocalRotation(const Quat& q); + const Quat& GetLocalRotation() const; + void SetLocalTransform(const Transform& t); + const Transform& GetLocalTransform() const; - void SetWorldPosition(const Vector &v); - const Vector &GetWorldPosition()const; - void SetWorldScale(const Vector &v); - const Vector &GetWorldScale()const; - void SetWorldRotation(const Quat &q); - const Quat &GetWorldRotation()const; - void SetWorldTransform(const Transform &t); - const Transform &GetWorldTransform()const; + void SetWorldPosition(const Vector& v); + const Vector& GetWorldPosition() const; + void SetWorldScale(const Vector& v); + const Vector& GetWorldScale() const; + void SetWorldRotation(const Quat& q); + const Quat& GetWorldRotation() const; + void SetWorldTransform(const Transform& t); + const Transform& GetWorldTransform() const; - Entity* GetChildren()const { return m_children; } - Entity* GetSuccessor()const { return m_listNext; } + Entity* GetChildren() const + { + return m_children; + } + Entity* GetSuccessor() const + { + return m_listNext; + } - - static Entity* GetEntityOrphans() { return _orphans; } + static Entity* GetEntityOrphans() + { + return _orphans; + } private: Entity *m_listNext, *m_listPrev, *m_parent, *m_children, *m_last_child; @@ -82,12 +119,12 @@ class Entity { mutable int invalid; - Quat m_localRotation; - Vector m_localPosition, m_localScale; + Quat m_localRotation; + Vector m_localPosition, m_localScale; mutable Transform m_localTransform; - mutable Quat m_worldRotation; - mutable Vector m_worldPosition, m_worldScale; + mutable Quat m_worldRotation; + mutable Vector m_worldPosition, m_worldScale; mutable Transform m_worldTransform; void InsertChildToParent(); diff --git a/Runtime/blitz3d/frustum.cpp b/Runtime/blitz3d/frustum.cpp index 0c8f44e..367efc9 100644 --- a/Runtime/blitz3d/frustum.cpp +++ b/Runtime/blitz3d/frustum.cpp @@ -1,54 +1,62 @@ -#include "std.hpp" #include "frustum.hpp" +#include "std.hpp" -Frustum::Frustum(){ -} +Frustum::Frustum() {} -Frustum::Frustum( float nr,float fr,float w,float h ){ - verts[VERT_TLNEAR]=Vector( w*-.5f,h*+.5f,nr ); - verts[VERT_TRNEAR]=Vector( w*+.5f,h*+.5f,nr ); - verts[VERT_BRNEAR]=Vector( w*+.5f,h*-.5f,nr ); - verts[VERT_BLNEAR]=Vector( w*-.5f,h*-.5f,nr ); - float t=fr/nr; - verts[VERT_TLFAR]=verts[VERT_TLNEAR] * t; - verts[VERT_TRFAR]=verts[VERT_TRNEAR] * t; - verts[VERT_BRFAR]=verts[VERT_BRNEAR] * t; - verts[VERT_BLFAR]=verts[VERT_BLNEAR] * t; - verts[VERT_EYE]=Vector(); +Frustum::Frustum(float nr, float fr, float w, float h) +{ + verts[VERT_TLNEAR] = Vector(w * -.5f, h * +.5f, nr); + verts[VERT_TRNEAR] = Vector(w * +.5f, h * +.5f, nr); + verts[VERT_BRNEAR] = Vector(w * +.5f, h * -.5f, nr); + verts[VERT_BLNEAR] = Vector(w * -.5f, h * -.5f, nr); + float t = fr / nr; + verts[VERT_TLFAR] = verts[VERT_TLNEAR] * t; + verts[VERT_TRFAR] = verts[VERT_TRNEAR] * t; + verts[VERT_BRFAR] = verts[VERT_BRNEAR] * t; + verts[VERT_BLFAR] = verts[VERT_BLNEAR] * t; + verts[VERT_EYE] = Vector(); makePlanes(); } -Frustum::Frustum( const Frustum &f,const Transform &t ){ - for( int k=0;k<9;++k ){ - verts[k]=t*f.verts[k]; +Frustum::Frustum(const Frustum& f, const Transform& t) +{ + for (int k = 0; k < 9; ++k) { + verts[k] = t * f.verts[k]; } makePlanes(); } -bool Frustum::cull( const Vector v[],int cnt )const{ - for( int n=0;n<6;++n ){ +bool Frustum::cull(const Vector v[], int cnt) const +{ + for (int n = 0; n < 6; ++n) { int k; - for( k=0;k0 ){ - for( int k=0;k<6;++k ) planes[k]=-planes[k]; +void Frustum::makePlanes() +{ + planes[PLANE_TOP] = Plane(verts[VERT_EYE], verts[VERT_TRFAR], verts[VERT_TLFAR]); + planes[PLANE_LEFT] = Plane(verts[VERT_EYE], verts[VERT_TLFAR], verts[VERT_BLFAR]); + planes[PLANE_BOTTOM] = Plane(verts[VERT_EYE], verts[VERT_BLFAR], verts[VERT_BRFAR]); + planes[PLANE_RIGHT] = Plane(verts[VERT_EYE], verts[VERT_BRFAR], verts[VERT_TRFAR]); + planes[PLANE_NEAR] = Plane(verts[VERT_TRNEAR], verts[VERT_TLNEAR], verts[VERT_BLNEAR]); + planes[PLANE_FAR] = Plane(verts[VERT_TLFAR], verts[VERT_TRFAR], verts[VERT_BRFAR]); + if (planes[PLANE_NEAR].distance(verts[VERT_EYE]) > 0) { + for (int k = 0; k < 6; ++k) + planes[k] = -planes[k]; } } diff --git a/Runtime/blitz3d/frustum.hpp b/Runtime/blitz3d/frustum.hpp index de98ed1..b4c1d7d 100644 --- a/Runtime/blitz3d/frustum.hpp +++ b/Runtime/blitz3d/frustum.hpp @@ -4,29 +4,40 @@ #include "geom.hpp" -class Frustum{ -public: - enum{ - VERT_TLNEAR=0,VERT_TRNEAR,VERT_BRNEAR,VERT_BLNEAR, - VERT_TLFAR,VERT_TRFAR,VERT_BRFAR,VERT_BLFAR,VERT_EYE - }; - enum{ - PLANE_TOP=0,PLANE_LEFT,PLANE_BOTTOM,PLANE_RIGHT,PLANE_NEAR,PLANE_FAR +class Frustum { + public: + enum { + VERT_TLNEAR = 0, + VERT_TRNEAR, + VERT_BRNEAR, + VERT_BLNEAR, + VERT_TLFAR, + VERT_TRFAR, + VERT_BRFAR, + VERT_BLFAR, + VERT_EYE }; + enum { PLANE_TOP = 0, PLANE_LEFT, PLANE_BOTTOM, PLANE_RIGHT, PLANE_NEAR, PLANE_FAR }; Frustum(); - Frustum( float nr,float fr,float w,float h ); - Frustum( const Frustum &f,const Transform &t ); + Frustum(float nr, float fr, float w, float h); + Frustum(const Frustum& f, const Transform& t); - bool cull( const Box &box )const; - bool cull( const Vector vecs[],int cnt )const; + bool cull(const Box& box) const; + bool cull(const Vector vecs[], int cnt) const; - const Plane &getPlane( int n )const{ return planes[n]; } - const Vector &getVertex( int n )const{ return verts[n]; } + const Plane& getPlane(int n) const + { + return planes[n]; + } + const Vector& getVertex(int n) const + { + return verts[n]; + } -private: - Plane planes[6]; + private: + Plane planes[6]; Vector verts[9]; - void makePlanes(); + void makePlanes(); }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/geom.cpp b/Runtime/blitz3d/geom.cpp index 9240dcd..33e16ef 100644 --- a/Runtime/blitz3d/geom.cpp +++ b/Runtime/blitz3d/geom.cpp @@ -1,12 +1,13 @@ -#include "std.hpp" #include "geom.hpp" +#include "std.hpp" -Matrix Matrix::tmps[64]; +Matrix Matrix::tmps[64]; Transform Transform::tmps[64]; -Quat rotationQuat( float p,float y,float r ){ - return yawQuat(y)*pitchQuat(p)*rollQuat(r); +Quat rotationQuat(float p, float y, float r) +{ + return yawQuat(y) * pitchQuat(p) * rollQuat(r); } /* diff --git a/Runtime/blitz3d/geom.hpp b/Runtime/blitz3d/geom.hpp index 2421b07..b1f71ba 100644 --- a/Runtime/blitz3d/geom.hpp +++ b/Runtime/blitz3d/geom.hpp @@ -10,486 +10,666 @@ class Plane; class Matrix; class Transform; -static float PI = 3.14159265359f; //180 degrees -static float TWOPI = PI*2.0f; //360 degrees -static float HALFPI = PI*.5f; //90 degrees -static float QUARTERPI = PI*.25f; //45 degrees +static float PI = 3.14159265359f; //180 degrees +static float TWOPI = PI * 2.0f; //360 degrees +static float HALFPI = PI * .5f; //90 degrees +static float QUARTERPI = PI * .25f; //45 degrees class Vector { -public: + public: float x, y, z; - Vector() :x(0), y(0), z(0) { - } - Vector(float x, float y, float z) :x(x), y(y), z(z) { - } - operator float*() { + Vector() : x(0), y(0), z(0) {} + Vector(float x, float y, float z) : x(x), y(y), z(z) {} + operator float*() + { return &x; } - operator const float *() { + operator const float*() + { return &x; } - float &operator[](int n) { + float& operator[](int n) + { return (&x)[n]; } - float operator[](int n)const { + float operator[](int n) const + { return (&x)[n]; } - Vector operator-()const { + Vector operator-() const + { return Vector(-x, -y, -z); } - Vector operator*(float scale)const { - return Vector(x*scale, y*scale, z*scale); + Vector operator*(float scale) const + { + return Vector(x * scale, y * scale, z * scale); } - Vector operator*(const Vector &q)const { - return Vector(x*q.x, y*q.y, z*q.z); + Vector operator*(const Vector& q) const + { + return Vector(x * q.x, y * q.y, z * q.z); } - Vector operator/(float scale)const { + Vector operator/(float scale) const + { return Vector(x / scale, y / scale, z / scale); } - Vector operator/(const Vector &q)const { + Vector operator/(const Vector& q) const + { return Vector(x / q.x, y / q.y, z / q.z); } - Vector operator+(const Vector &q)const { + Vector operator+(const Vector& q) const + { return Vector(x + q.x, y + q.y, z + q.z); } - Vector operator-(const Vector &q)const { + Vector operator-(const Vector& q) const + { return Vector(x - q.x, y - q.y, z - q.z); } - Vector &operator*=(float scale) { - x *= scale; y *= scale; z *= scale; return *this; + Vector& operator*=(float scale) + { + x *= scale; + y *= scale; + z *= scale; + return *this; } - Vector &operator*=(const Vector &q) { - x *= q.x; y *= q.y; z *= q.z; return *this; + Vector& operator*=(const Vector& q) + { + x *= q.x; + y *= q.y; + z *= q.z; + return *this; } - Vector &operator/=(float scale) { - x /= scale; y /= scale; z /= scale; return *this; + Vector& operator/=(float scale) + { + x /= scale; + y /= scale; + z /= scale; + return *this; } - Vector &operator/=(const Vector &q) { - x /= q.x; y /= q.y; z /= q.z; return *this; + Vector& operator/=(const Vector& q) + { + x /= q.x; + y /= q.y; + z /= q.z; + return *this; } - Vector &operator+=(const Vector &q) { - x += q.x; y += q.y; z += q.z; return *this; + Vector& operator+=(const Vector& q) + { + x += q.x; + y += q.y; + z += q.z; + return *this; } - Vector &operator-=(const Vector &q) { - x -= q.x; y -= q.y; z -= q.z; return *this; + Vector& operator-=(const Vector& q) + { + x -= q.x; + y -= q.y; + z -= q.z; + return *this; } - bool operator<(const Vector &q)const { - if (fabs(x - q.x) > FLT_EPSILON) return x < q.x ? true : false; - if (fabs(y - q.y) > FLT_EPSILON) return y < q.y ? true : false; + bool operator<(const Vector& q) const + { + if (fabs(x - q.x) > FLT_EPSILON) + return x < q.x ? true : false; + if (fabs(y - q.y) > FLT_EPSILON) + return y < q.y ? true : false; return fabs(z - q.z) > FLT_EPSILON && z < q.z; } - bool operator==(const Vector &q)const { + bool operator==(const Vector& q) const + { return fabs(x - q.x) <= FLT_EPSILON && fabs(y - q.y) <= FLT_EPSILON && fabs(z - q.z) <= FLT_EPSILON; } - bool operator!=(const Vector &q)const { + bool operator!=(const Vector& q) const + { return fabs(x - q.x) > FLT_EPSILON || fabs(y - q.y) > FLT_EPSILON || fabs(z - q.z) > FLT_EPSILON; } - float dot(const Vector &q)const { - return x*q.x + y*q.y + z*q.z; + float dot(const Vector& q) const + { + return x * q.x + y * q.y + z * q.z; } - Vector cross(const Vector &q)const { - return Vector(y*q.z - z*q.y, z*q.x - x*q.z, x*q.y - y*q.x); + Vector cross(const Vector& q) const + { + return Vector(y * q.z - z * q.y, z * q.x - x * q.z, x * q.y - y * q.x); } - float length()const { - return sqrtf(x*x + y*y + z*z); + float length() const + { + return sqrtf(x * x + y * y + z * z); } - float distance(const Vector &q)const { - float dx = x - q.x, dy = y - q.y, dz = z - q.z; return sqrtf(dx*dx + dy*dy + dz*dz); + float distance(const Vector& q) const + { + float dx = x - q.x, dy = y - q.y, dz = z - q.z; + return sqrtf(dx * dx + dy * dy + dz * dz); } - Vector normalized()const { - float l = length(); return Vector(x / l, y / l, z / l); + Vector normalized() const + { + float l = length(); + return Vector(x / l, y / l, z / l); } - void normalize() { - float l = length(); x /= l; y /= l; z /= l; + void normalize() + { + float l = length(); + x /= l; + y /= l; + z /= l; } - float yaw()const { + float yaw() const + { return -atan2f(x, z); } - float pitch()const { - return -atan2f(y, sqrtf(x*x + z*z)); + float pitch() const + { + return -atan2f(y, sqrtf(x * x + z * z)); } - void clear() { + void clear() + { x = y = z = 0; } }; class Line { -public: + public: Vector o, d; - Line() { - } - Line(const Vector &o, const Vector &d) :o(o), d(d) { - } - Line operator+(const Vector &q)const { + Line() {} + Line(const Vector& o, const Vector& d) : o(o), d(d) {} + Line operator+(const Vector& q) const + { return Line(o + q, d); } - Line operator-(const Vector &q)const { + Line operator-(const Vector& q) const + { return Line(o - q, d); } - Vector operator*(float q)const { - return o + d*q; + Vector operator*(float q) const + { + return o + d * q; } - Vector nearest(const Vector &q)const { - return o + d*(d.dot(q - o) / d.dot(d)); + Vector nearest(const Vector& q) const + { + return o + d * (d.dot(q - o) / d.dot(d)); } }; class Plane { -public: + public: Vector n; - float d; + float d; - Plane() :d(0) { - } + Plane() : d(0) {} //normal/offset form - Plane(const Vector &n, float d) :n(n), d(d) { - } + Plane(const Vector& n, float d) : n(n), d(d) {} //point/normal form - Plane(const Vector &p, const Vector &n) :n(n), d(-n.dot(p)) { - } + Plane(const Vector& p, const Vector& n) : n(n), d(-n.dot(p)) {} //create plane from tri - Plane(const Vector &v0, const Vector &v1, const Vector &v2) { - n = (v1 - v0).cross(v2 - v0).normalized(); d = -n.dot(v0); + Plane(const Vector& v0, const Vector& v1, const Vector& v2) + { + n = (v1 - v0).cross(v2 - v0).normalized(); + d = -n.dot(v0); } - Plane operator-()const { + Plane operator-() const + { return Plane(-n, -d); } - float t_intersect(const Line &q)const { + float t_intersect(const Line& q) const + { return -distance(q.o) / n.dot(q.d); } - Vector intersect(const Line &q)const { - return q*t_intersect(q); + Vector intersect(const Line& q) const + { + return q * t_intersect(q); } - Line intersect(const Plane &q)const { + Line intersect(const Plane& q) const + { Vector lv = n.cross(q.n).normalized(); - return Line(q.intersect(Line(nearest(n*-d), n.cross(lv))), lv); + return Line(q.intersect(Line(nearest(n * -d), n.cross(lv))), lv); } - Vector nearest(const Vector &q)const { - return q - n*distance(q); + Vector nearest(const Vector& q) const + { + return q - n * distance(q); } - void negate() { - n = -n; d = -d; + void negate() + { + n = -n; + d = -d; } - float distance(const Vector &q)const { + float distance(const Vector& q) const + { return n.dot(q) + d; } }; struct Quat { - float w; + float w; Vector v; - Quat() :w(1) { - } - Quat(float w, const Vector &v) :w(w), v(v) { - } - Quat operator-()const { + Quat() : w(1) {} + Quat(float w, const Vector& v) : w(w), v(v) {} + Quat operator-() const + { return Quat(w, -v); } - Quat operator+(const Quat &q)const { + Quat operator+(const Quat& q) const + { return Quat(w + q.w, v + q.v); } - Quat operator-(const Quat &q)const { + Quat operator-(const Quat& q) const + { return Quat(w - q.w, v - q.v); } - Quat operator*(const Quat &q)const { - return Quat(w*q.w - v.dot(q.v), q.v.cross(v) + q.v*w + v*q.w); + Quat operator*(const Quat& q) const + { + return Quat(w * q.w - v.dot(q.v), q.v.cross(v) + q.v * w + v * q.w); } - Vector operator*(const Vector &q)const { + Vector operator*(const Vector& q) const + { return (*this * Quat(0, q) * -*this).v; } - Quat operator*(float q)const { - return Quat(w*q, v*q); + Quat operator*(float q) const + { + return Quat(w * q, v * q); } - Quat operator/(float q)const { + Quat operator/(float q) const + { return Quat(w / q, v / q); } - float dot(const Quat &q)const { - return v.x*q.v.x + v.y*q.v.y + v.z*q.v.z + w*q.w; + float dot(const Quat& q) const + { + return v.x * q.v.x + v.y * q.v.y + v.z * q.v.z + w * q.w; } - float length()const { - return sqrtf(w*w + v.x*v.x + v.y*v.y + v.z*v.z); + float length() const + { + return sqrtf(w * w + v.x * v.x + v.y * v.y + v.z * v.z); } - void normalize() { + void normalize() + { *this = *this / length(); } - Quat normalized()const { + Quat normalized() const + { return *this / length(); } - Quat slerpTo(const Quat &q, float a)const { - Quat t = q; + Quat slerpTo(const Quat& q, float a) const + { + Quat t = q; float d = dot(q), b = 1 - a; - if (d < 0) { t.w = -t.w; t.v = -t.v; d = -d; } + if (d < 0) { + t.w = -t.w; + t.v = -t.v; + d = -d; + } if (d < 1 - FLT_EPSILON) { float om = acosf(d); float si = sinf(om); - a = sinf(a*om) / si; - b = sinf(b*om) / si; + a = sinf(a * om) / si; + b = sinf(b * om) / si; } - return *this*b + t*a; + return *this * b + t * a; } - Vector i()const { - float xz = v.x*v.z, wy = w*v.y; - float xy = v.x*v.y, wz = w*v.z; - float yy = v.y*v.y, zz = v.z*v.z; + Vector i() const + { + float xz = v.x * v.z, wy = w * v.y; + float xy = v.x * v.y, wz = w * v.z; + float yy = v.y * v.y, zz = v.z * v.z; return Vector(1 - 2 * (yy + zz), 2 * (xy - wz), 2 * (xz + wy)); } - Vector j()const { - float yz = v.y*v.z, wx = w*v.x; - float xy = v.x*v.y, wz = w*v.z; - float xx = v.x*v.x, zz = v.z*v.z; + Vector j() const + { + float yz = v.y * v.z, wx = w * v.x; + float xy = v.x * v.y, wz = w * v.z; + float xx = v.x * v.x, zz = v.z * v.z; return Vector(2 * (xy + wz), 1 - 2 * (xx + zz), 2 * (yz - wx)); } - Vector k()const { - float xz = v.x*v.z, wy = w*v.y; - float yz = v.y*v.z, wx = w*v.x; - float xx = v.x*v.x, yy = v.y*v.y; + Vector k() const + { + float xz = v.x * v.z, wy = w * v.y; + float yz = v.y * v.z, wx = w * v.x; + float xx = v.x * v.x, yy = v.y * v.y; return Vector(2 * (xz - wy), 2 * (yz + wx), 1 - 2 * (xx + yy)); } }; class Matrix { - static Matrix tmps[64]; - static Matrix &alloc_tmp() { static int tmp = 0; return tmps[tmp++ & 63]; } + static Matrix tmps[64]; + static Matrix& alloc_tmp() + { + static int tmp = 0; + return tmps[tmp++ & 63]; + } friend class Transform; -public: + + public: Vector i, j, k; - Matrix() :i(Vector(1, 0, 0)), j(Vector(0, 1, 0)), k(Vector(0, 0, 1)) { - } - Matrix(const Vector &i, const Vector &j, const Vector &k) :i(i), j(j), k(k) { - } - Matrix(const Quat &q) { - float xx = q.v.x*q.v.x, yy = q.v.y*q.v.y, zz = q.v.z*q.v.z; - float xy = q.v.x*q.v.y, xz = q.v.x*q.v.z, yz = q.v.y*q.v.z; - float wx = q.w*q.v.x, wy = q.w*q.v.y, wz = q.w*q.v.z; + Matrix() : i(Vector(1, 0, 0)), j(Vector(0, 1, 0)), k(Vector(0, 0, 1)) {} + Matrix(const Vector& i, const Vector& j, const Vector& k) : i(i), j(j), k(k) {} + Matrix(const Quat& q) + { + float xx = q.v.x * q.v.x, yy = q.v.y * q.v.y, zz = q.v.z * q.v.z; + float xy = q.v.x * q.v.y, xz = q.v.x * q.v.z, yz = q.v.y * q.v.z; + float wx = q.w * q.v.x, wy = q.w * q.v.y, wz = q.w * q.v.z; i = Vector(1 - 2 * (yy + zz), 2 * (xy - wz), 2 * (xz + wy)), - j = Vector(2 * (xy + wz), 1 - 2 * (xx + zz), 2 * (yz - wx)), - k = Vector(2 * (xz - wy), 2 * (yz + wx), 1 - 2 * (xx + yy)); + j = Vector(2 * (xy + wz), 1 - 2 * (xx + zz), 2 * (yz - wx)), + k = Vector(2 * (xz - wy), 2 * (yz + wx), 1 - 2 * (xx + yy)); } - Matrix(float angle, const Vector &axis) { - const Vector &u = axis; - float c = cosf(angle), s = sinf(angle); - float x2 = axis.x*axis.x, y2 = axis.y*axis.y, z2 = axis.z*axis.z; - i = Vector(x2 + c*(1 - x2), u.x*u.y*(1 - c) - u.z*s, u.z*u.x*(1 - c) + u.y*s); - j = Vector(u.x*u.y*(1 - c) + u.z*s, y2 + c*(1 - y2), u.y*u.z*(1 - c) - u.x*s); - k = Vector(u.z*u.x*(1 - c) - u.y*s, u.y*u.z*(1 - c) + u.x*s, z2 + c*(1 - z2)); + Matrix(float angle, const Vector& axis) + { + const Vector& u = axis; + float c = cosf(angle), s = sinf(angle); + float x2 = axis.x * axis.x, y2 = axis.y * axis.y, z2 = axis.z * axis.z; + i = Vector(x2 + c * (1 - x2), u.x * u.y * (1 - c) - u.z * s, u.z * u.x * (1 - c) + u.y * s); + j = Vector(u.x * u.y * (1 - c) + u.z * s, y2 + c * (1 - y2), u.y * u.z * (1 - c) - u.x * s); + k = Vector(u.z * u.x * (1 - c) - u.y * s, u.y * u.z * (1 - c) + u.x * s, z2 + c * (1 - z2)); } - Vector &operator[](int n) { + Vector& operator[](int n) + { return (&i)[n]; } - const Vector &operator[](int n)const { + const Vector& operator[](int n) const + { return (&i)[n]; } - Matrix &operator~()const { - Matrix &m = alloc_tmp(); - m.i.x = i.x; m.i.y = j.x; m.i.z = k.x; - m.j.x = i.y; m.j.y = j.y; m.j.z = k.y; - m.k.x = i.z; m.k.y = j.z; m.k.z = k.z; + Matrix& operator~() const + { + Matrix& m = alloc_tmp(); + m.i.x = i.x; + m.i.y = j.x; + m.i.z = k.x; + m.j.x = i.y; + m.j.y = j.y; + m.j.z = k.y; + m.k.x = i.z; + m.k.y = j.z; + m.k.z = k.z; return m; } - float determinant()const { - return i.x*(j.y*k.z - j.z*k.y) - i.y*(j.x*k.z - j.z*k.x) + i.z*(j.x*k.y - j.y*k.x); + float determinant() const + { + return i.x * (j.y * k.z - j.z * k.y) - i.y * (j.x * k.z - j.z * k.x) + i.z * (j.x * k.y - j.y * k.x); } - Matrix &operator-()const { - Matrix &m = alloc_tmp(); - float t = 1.0f / determinant(); - m.i.x = t*(j.y*k.z - j.z*k.y); m.i.y = -t*(i.y*k.z - i.z*k.y); m.i.z = t*(i.y*j.z - i.z*j.y); - m.j.x = -t*(j.x*k.z - j.z*k.x); m.j.y = t*(i.x*k.z - i.z*k.x); m.j.z = -t*(i.x*j.z - i.z*j.x); - m.k.x = t*(j.x*k.y - j.y*k.x); m.k.y = -t*(i.x*k.y - i.y*k.x); m.k.z = t*(i.x*j.y - i.y*j.x); + Matrix& operator-() const + { + Matrix& m = alloc_tmp(); + float t = 1.0f / determinant(); + m.i.x = t * (j.y * k.z - j.z * k.y); + m.i.y = -t * (i.y * k.z - i.z * k.y); + m.i.z = t * (i.y * j.z - i.z * j.y); + m.j.x = -t * (j.x * k.z - j.z * k.x); + m.j.y = t * (i.x * k.z - i.z * k.x); + m.j.z = -t * (i.x * j.z - i.z * j.x); + m.k.x = t * (j.x * k.y - j.y * k.x); + m.k.y = -t * (i.x * k.y - i.y * k.x); + m.k.z = t * (i.x * j.y - i.y * j.x); return m; } - Matrix &cofactor()const { - Matrix &m = alloc_tmp(); - m.i.x = (j.y*k.z - j.z*k.y); m.i.y = -(j.x*k.z - j.z*k.x); m.i.z = (j.x*k.y - j.y*k.x); - m.j.x = -(i.y*k.z - i.z*k.y); m.j.y = (i.x*k.z - i.z*k.x); m.j.z = -(i.x*k.y - i.y*k.x); - m.k.x = (i.y*j.z - i.z*j.y); m.k.y = -(i.x*j.z - i.z*j.x); m.k.z = (i.x*j.y - i.y*j.x); + Matrix& cofactor() const + { + Matrix& m = alloc_tmp(); + m.i.x = (j.y * k.z - j.z * k.y); + m.i.y = -(j.x * k.z - j.z * k.x); + m.i.z = (j.x * k.y - j.y * k.x); + m.j.x = -(i.y * k.z - i.z * k.y); + m.j.y = (i.x * k.z - i.z * k.x); + m.j.z = -(i.x * k.y - i.y * k.x); + m.k.x = (i.y * j.z - i.z * j.y); + m.k.y = -(i.x * j.z - i.z * j.x); + m.k.z = (i.x * j.y - i.y * j.x); return m; } - bool operator==(const Matrix &q)const { + bool operator==(const Matrix& q) const + { return i == q.i && j == q.j && k == q.k; } - bool operator!=(const Matrix &q)const { + bool operator!=(const Matrix& q) const + { return i != q.i || j != q.j || k != q.k; } - Vector operator*(const Vector &q)const { - return Vector(i.x*q.x + j.x*q.y + k.x*q.z, i.y*q.x + j.y*q.y + k.y*q.z, i.z*q.x + j.z*q.y + k.z*q.z); + Vector operator*(const Vector& q) const + { + return Vector(i.x * q.x + j.x * q.y + k.x * q.z, i.y * q.x + j.y * q.y + k.y * q.z, + i.z * q.x + j.z * q.y + k.z * q.z); } - Matrix &operator*(const Matrix &q)const { - Matrix &m = alloc_tmp(); - m.i.x = i.x*q.i.x + j.x*q.i.y + k.x*q.i.z; m.i.y = i.y*q.i.x + j.y*q.i.y + k.y*q.i.z; m.i.z = i.z*q.i.x + j.z*q.i.y + k.z*q.i.z; - m.j.x = i.x*q.j.x + j.x*q.j.y + k.x*q.j.z; m.j.y = i.y*q.j.x + j.y*q.j.y + k.y*q.j.z; m.j.z = i.z*q.j.x + j.z*q.j.y + k.z*q.j.z; - m.k.x = i.x*q.k.x + j.x*q.k.y + k.x*q.k.z; m.k.y = i.y*q.k.x + j.y*q.k.y + k.y*q.k.z; m.k.z = i.z*q.k.x + j.z*q.k.y + k.z*q.k.z; + Matrix& operator*(const Matrix& q) const + { + Matrix& m = alloc_tmp(); + m.i.x = i.x * q.i.x + j.x * q.i.y + k.x * q.i.z; + m.i.y = i.y * q.i.x + j.y * q.i.y + k.y * q.i.z; + m.i.z = i.z * q.i.x + j.z * q.i.y + k.z * q.i.z; + m.j.x = i.x * q.j.x + j.x * q.j.y + k.x * q.j.z; + m.j.y = i.y * q.j.x + j.y * q.j.y + k.y * q.j.z; + m.j.z = i.z * q.j.x + j.z * q.j.y + k.z * q.j.z; + m.k.x = i.x * q.k.x + j.x * q.k.y + k.x * q.k.z; + m.k.y = i.y * q.k.x + j.y * q.k.y + k.y * q.k.z; + m.k.z = i.z * q.k.x + j.z * q.k.y + k.z * q.k.z; return m; } - void orthogonalize() { + void orthogonalize() + { k.normalize(); i = j.cross(k).normalized(); j = k.cross(i); } - Matrix &orthogonalized()const { - Matrix &m = alloc_tmp(); - m = *this; m.orthogonalize(); + Matrix& orthogonalized() const + { + Matrix& m = alloc_tmp(); + m = *this; + m.orthogonalize(); return m; } }; class Box { -public: + public: Vector a, b; - Box() :a(Vector(INFINITY, INFINITY, INFINITY)), b(Vector(-INFINITY, -INFINITY, -INFINITY)) { - } - Box(const Vector &q) :a(q), b(q) { - } - Box(const Vector &a, const Vector &b) :a(a), b(b) { - } - Box(const Line &l) :a(l.o), b(l.o) { + Box() : a(Vector(INFINITY, INFINITY, INFINITY)), b(Vector(-INFINITY, -INFINITY, -INFINITY)) {} + Box(const Vector& q) : a(q), b(q) {} + Box(const Vector& a, const Vector& b) : a(a), b(b) {} + Box(const Line& l) : a(l.o), b(l.o) + { update(l.o + l.d); } - void clear() { + void clear() + { a.x = a.y = a.z = INFINITY; b.x = b.y = b.z = -INFINITY; } - bool empty()const { + bool empty() const + { return b.x < a.x || b.y < a.y || b.z < a.z; } - Vector centre()const { - return Vector((a.x + b.x)*.5f, (a.y + b.y)*.5f, (a.z + b.z)*.5f); + Vector centre() const + { + return Vector((a.x + b.x) * .5f, (a.y + b.y) * .5f, (a.z + b.z) * .5f); } - Vector corner(int n)const { + Vector corner(int n) const + { return Vector(((n & 1) ? b : a).x, ((n & 2) ? b : a).y, ((n & 4) ? b : a).z); } - void update(const Vector &q) { - if (q.x < a.x) a.x = q.x; if (q.y < a.y) a.y = q.y; if (q.z < a.z) a.z = q.z; - if (q.x > b.x) b.x = q.x; if (q.y > b.y) b.y = q.y; if (q.z > b.z) b.z = q.z; + void update(const Vector& q) + { + if (q.x < a.x) + a.x = q.x; + if (q.y < a.y) + a.y = q.y; + if (q.z < a.z) + a.z = q.z; + if (q.x > b.x) + b.x = q.x; + if (q.y > b.y) + b.y = q.y; + if (q.z > b.z) + b.z = q.z; } - void update(const Box &q) { - if (q.a.x < a.x) a.x = q.a.x; if (q.a.y < a.y) a.y = q.a.y; if (q.a.z < a.z) a.z = q.a.z; - if (q.b.x > b.x) b.x = q.b.x; if (q.b.y > b.y) b.y = q.b.y; if (q.b.z > b.z) b.z = q.b.z; + void update(const Box& q) + { + if (q.a.x < a.x) + a.x = q.a.x; + if (q.a.y < a.y) + a.y = q.a.y; + if (q.a.z < a.z) + a.z = q.a.z; + if (q.b.x > b.x) + b.x = q.b.x; + if (q.b.y > b.y) + b.y = q.b.y; + if (q.b.z > b.z) + b.z = q.b.z; } - bool overlaps(const Box &q)const { - return - (b.x < q.b.x ? b.x : q.b.x) >= (a.x > q.a.x ? a.x : q.a.x) && - (b.y < q.b.y ? b.y : q.b.y) >= (a.y > q.a.y ? a.y : q.a.y) && - (b.z < q.b.z ? b.z : q.b.z) >= (a.z > q.a.z ? a.z : q.a.z); + bool overlaps(const Box& q) const + { + return (b.x < q.b.x ? b.x : q.b.x) >= (a.x > q.a.x ? a.x : q.a.x) + && (b.y < q.b.y ? b.y : q.b.y) >= (a.y > q.a.y ? a.y : q.a.y) + && (b.z < q.b.z ? b.z : q.b.z) >= (a.z > q.a.z ? a.z : q.a.z); } - void expand(float n) { - a.x -= n; a.y -= n; a.z -= n; b.x += n; b.y += n; b.z += n; + void expand(float n) + { + a.x -= n; + a.y -= n; + a.z -= n; + b.x += n; + b.y += n; + b.z += n; } - float width()const { + float width() const + { return b.x - a.x; } - float height()const { + float height() const + { return b.y - a.y; } - float depth()const { + float depth() const + { return b.z - a.z; } - bool contains(const Vector &q) { + bool contains(const Vector& q) + { return q.x >= a.x && q.x <= b.x && q.y >= a.y && q.y <= b.y && q.z >= a.z && q.z <= b.z; } }; class Transform { - static Transform tmps[64]; - static Transform &alloc_tmp() { static int tmp = 0; return tmps[tmp++ & 63]; } -public: + static Transform tmps[64]; + static Transform& alloc_tmp() + { + static int tmp = 0; + return tmps[tmp++ & 63]; + } + + public: Matrix m; Vector v; - Transform() { - } - Transform(const Matrix &m) :m(m) { - } - Transform(const Vector &v) :v(v) { - } - Transform(const Matrix &m, const Vector &v) :m(m), v(v) { - } - Transform &operator-()const { - Transform &t = alloc_tmp(); - t.m = -m; t.v = t.m*-v; + Transform() {} + Transform(const Matrix& m) : m(m) {} + Transform(const Vector& v) : v(v) {} + Transform(const Matrix& m, const Vector& v) : m(m), v(v) {} + Transform& operator-() const + { + Transform& t = alloc_tmp(); + t.m = -m; + t.v = t.m * -v; return t; } - Transform &operator~()const { - Transform &t = alloc_tmp(); - t.m = ~m; t.v = t.m*-v; + Transform& operator~() const + { + Transform& t = alloc_tmp(); + t.m = ~m; + t.v = t.m * -v; return t; } - Vector operator*(const Vector &q)const { - return m*q + v; + Vector operator*(const Vector& q) const + { + return m * q + v; } - Line operator*(const Line &q)const { - Vector t = (*this)*q.o; - return Line(t, (*this)*(q.o + q.d) - t); + Line operator*(const Line& q) const + { + Vector t = (*this) * q.o; + return Line(t, (*this) * (q.o + q.d) - t); } - Box operator*(const Box &q)const { - Box t((*this*q.corner(0))); - for (int k = 1; k < 8; ++k) t.update(*this*q.corner(k)); + Box operator*(const Box& q) const + { + Box t((*this * q.corner(0))); + for (int k = 1; k < 8; ++k) + t.update(*this * q.corner(k)); return t; } - Transform &operator*(const Transform &q)const { - Transform &t = alloc_tmp(); - t.m = m*q.m; t.v = m*q.v + v; + Transform& operator*(const Transform& q) const + { + Transform& t = alloc_tmp(); + t.m = m * q.m; + t.v = m * q.v + v; return t; } - bool operator==(const Transform &q)const { + bool operator==(const Transform& q) const + { return m == q.m && v == q.v; } - bool operator!=(const Transform &q)const { + bool operator!=(const Transform& q) const + { return !operator==(q); } }; -inline float transformRadius(float r, const Matrix &t) { +inline float transformRadius(float r, const Matrix& t) +{ static const float sq_3 = sqrtf(1.0f / 3.0f); - return (t * Vector(sq_3, sq_3, sq_3)).length()*r; + return (t * Vector(sq_3, sq_3, sq_3)).length() * r; } -inline Matrix pitchMatrix(float q) { +inline Matrix pitchMatrix(float q) +{ return Matrix(Vector(1, 0, 0), Vector(0, cosf(q), sinf(q)), Vector(0, -sinf(q), cosf(q))); } -inline Matrix yawMatrix(float q) { +inline Matrix yawMatrix(float q) +{ return Matrix(Vector(cosf(q), 0, sinf(q)), Vector(0, 1, 0), Vector(-sinf(q), 0, cosf(q))); } -inline Matrix rollMatrix(float q) { +inline Matrix rollMatrix(float q) +{ return Matrix(Vector(cosf(q), sinf(q), 0), Vector(-sinf(q), cosf(q), 0), Vector(0, 0, 1)); } -inline float matrixPitch(const Matrix &m) { +inline float matrixPitch(const Matrix& m) +{ return m.k.pitch(); // return asinf( -m.k.y ); } -inline float matrixYaw(const Matrix &m) { +inline float matrixYaw(const Matrix& m) +{ return m.k.yaw(); //return atan2f( -m.k.x,m.k.z ); } -inline float matrixRoll(const Matrix &m) { +inline float matrixRoll(const Matrix& m) +{ return atan2f(m.i.y, m.j.y); //Matrix t=pitchMatrix( -matrixPitch(m) )*yawMatrix( -matrixYaw(m) )*m; //return atan2f( t.i.y,t.i.x ); } -inline Matrix scaleMatrix(float x, float y, float z) { +inline Matrix scaleMatrix(float x, float y, float z) +{ return Matrix(Vector(x, 0, 0), Vector(0, y, 0), Vector(0, 0, z)); } -inline Matrix scaleMatrix(const Vector &scale) { +inline Matrix scaleMatrix(const Vector& scale) +{ return Matrix(Vector(scale.x, 0, 0), Vector(0, scale.y, 0), Vector(0, 0, scale.z)); } -inline Quat pitchQuat(float p) { +inline Quat pitchQuat(float p) +{ return Quat(cosf(p / -2), Vector(sinf(p / -2), 0, 0)); } -inline Quat yawQuat(float y) { +inline Quat yawQuat(float y) +{ return Quat(cosf(y / 2), Vector(0, sinf(y / 2), 0)); } -inline Quat rollQuat(float r) { +inline Quat rollQuat(float r) +{ return Quat(cosf(r / -2), Vector(0, 0, sinf(r / -2))); } @@ -499,29 +679,35 @@ inline Quat rollQuat(float r) { Quat rotationQuat(float p, float y, float r); -inline Matrix rotationMatrix(float p, float y, float r) { - return yawMatrix(y)*pitchMatrix(p)*rollMatrix(r); +inline Matrix rotationMatrix(float p, float y, float r) +{ + return yawMatrix(y) * pitchMatrix(p) * rollMatrix(r); } -inline Matrix rotationMatrix(const Vector &rot) { - return yawMatrix(rot.y)*pitchMatrix(rot.x)*rollMatrix(rot.z); +inline Matrix rotationMatrix(const Vector& rot) +{ + return yawMatrix(rot.y) * pitchMatrix(rot.x) * rollMatrix(rot.z); } -inline float quatPitch(const Quat &q) { +inline float quatPitch(const Quat& q) +{ return q.k().pitch(); } -inline float quatYaw(const Quat &q) { +inline float quatYaw(const Quat& q) +{ return q.k().yaw(); } -inline float quatRoll(const Quat &q) { +inline float quatRoll(const Quat& q) +{ // Vector i=q.i(),j=q.j(); // return atan2f( i.y,j.y ); return matrixRoll(q); } -inline Quat matrixQuat(const Matrix &p) { +inline Quat matrixQuat(const Matrix& p) +{ Matrix m = p; m.orthogonalize(); float t = m.i.x + m.j.y + m.k.z, w, x, y, z; diff --git a/Runtime/blitz3d/group.hpp b/Runtime/blitz3d/group.hpp index bebbbf1..29e7dfa 100644 --- a/Runtime/blitz3d/group.hpp +++ b/Runtime/blitz3d/group.hpp @@ -2,15 +2,17 @@ #ifndef GROUP_H #define GROUP_H -class Group{ - +class Group { vector _objs; -public: - Group( Object *obj ); + public: + Group(Object* obj); ~Group(); - const vector objs()const{ return _objs; } + const vector objs() const + { + return _objs; + } }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/light.cpp b/Runtime/blitz3d/light.cpp index 5f013d1..5fcb9a7 100644 --- a/Runtime/blitz3d/light.cpp +++ b/Runtime/blitz3d/light.cpp @@ -1,33 +1,39 @@ -#include "std.hpp" #include "light.hpp" #include "../gxruntime/gxscene.hpp" +#include "std.hpp" -extern gxScene *gx_scene; +extern gxScene* gx_scene; -Light::Light( int type ){ - light=gx_scene->createLight( type ); +Light::Light(int type) +{ + light = gx_scene->createLight(type); } -Light::~Light(){ - gx_scene->freeLight( light ); +Light::~Light() +{ + gx_scene->freeLight(light); } -void Light::setRange( float r ){ - light->setRange( r ); +void Light::setRange(float r) +{ + light->setRange(r); } -void Light::setColor( const Vector &v ){ - light->setColor( (float*)&v.x ); +void Light::setColor(const Vector& v) +{ + light->setColor((float*)&v.x); } -void Light::setConeAngles( float inner,float outer ){ - light->setConeAngles( inner,outer ); +void Light::setConeAngles(float inner, float outer) +{ + light->setConeAngles(inner, outer); } -bool Light::beginRender( float tween ){ - Object::beginRender( tween ); - light->setPosition( &getRenderTform().v.x ); - light->setDirection( &getRenderTform().m.k.x ); +bool Light::beginRender(float tween) +{ + Object::beginRender(tween); + light->setPosition(&getRenderTform().v.x); + light->setDirection(&getRenderTform().m.k.x); return true; } diff --git a/Runtime/blitz3d/light.hpp b/Runtime/blitz3d/light.hpp index 77f1f22..e48910a 100644 --- a/Runtime/blitz3d/light.hpp +++ b/Runtime/blitz3d/light.hpp @@ -3,29 +3,35 @@ #define LIGHT_H #include "geom.hpp" +#include "gxlight.hpp" #include "object.hpp" -#include "../gxruntime/gxlight.hpp" class World; -class Light : public Object{ -public: - Light( int type ); +class Light : public Object { + public: + Light(int type); ~Light(); - Light *getLight(){ return this; } + Light* getLight() + { + return this; + } - void setRange( float r ); - void setColor( const Vector &v ); - void setConeAngles( float inner,float outer ); + void setRange(float r); + void setColor(const Vector& v); + void setConeAngles(float inner, float outer); - bool beginRender( float tween ); + bool beginRender(float tween); - gxLight *getGxLight()const{ return light; } + gxLight* getGxLight() const + { + return light; + } -private: + private: friend class World; - gxLight *light; + gxLight* light; }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/listener.cpp b/Runtime/blitz3d/listener.cpp index 1eca5c0..6cfb7c2 100644 --- a/Runtime/blitz3d/listener.cpp +++ b/Runtime/blitz3d/listener.cpp @@ -1,34 +1,38 @@ -#include "std.hpp" #include "listener.hpp" +#include "std.hpp" -extern gxAudio *gx_audio; +extern gxAudio* gx_audio; -Listener::Listener( float roll,float dopp,float dist ){ - if( !gx_audio ) return; +Listener::Listener(float roll, float dopp, float dist) +{ + if (!gx_audio) + return; - gx_audio->set3dOptions( roll,dopp,dist ); + gx_audio->set3dOptions(roll, dopp, dist); renderListener(); } -Listener::Listener( const Listener &t ): -Object(t){ +Listener::Listener(const Listener& t) : Object(t) {} + +Listener::~Listener() +{ + if (!gx_audio) + return; + + Vector pos, vel, up(0, 1, 1), forward(0, 0, 1); + gx_audio->set3dListener(&pos.x, &vel.x, &forward.x, &up.x); } -Listener::~Listener(){ - if( !gx_audio ) return; +void Listener::renderListener() +{ + if (!gx_audio) + return; - Vector pos,vel,up(0,1,1),forward(0,0,1); - gx_audio->set3dListener( &pos.x,&vel.x,&forward.x,&up.x ); -} - -void Listener::renderListener(){ - if( !gx_audio ) return; - - const Vector &pos=GetWorldTransform().v; - const Vector &vel=getVelocity(); - const Vector &forward=GetWorldTransform().m.k.normalized(); - const Vector &up=GetWorldTransform().m.j.normalized(); - - gx_audio->set3dListener( &pos.x,&vel.x,&forward.x,&up.x ); + const Vector& pos = GetWorldTransform().v; + const Vector& vel = getVelocity(); + const Vector& forward = GetWorldTransform().m.k.normalized(); + const Vector& up = GetWorldTransform().m.j.normalized(); + + gx_audio->set3dListener(&pos.x, &vel.x, &forward.x, &up.x); } diff --git a/Runtime/blitz3d/listener.hpp b/Runtime/blitz3d/listener.hpp index 1562ac6..cee6779 100644 --- a/Runtime/blitz3d/listener.hpp +++ b/Runtime/blitz3d/listener.hpp @@ -4,20 +4,26 @@ #include "object.hpp" -class Listener : public Object{ -public: - Listener( float roll,float dopp,float dist ); - Listener( const Listener &t ); +class Listener : public Object { + public: + Listener(float roll, float dopp, float dist); + Listener(const Listener& t); ~Listener(); //Entity interface - Entity *clone(){ return new Listener( *this ); } - Listener *getListener(){ return this; } + Entity* clone() + { + return new Listener(*this); + } + Listener* getListener() + { + return this; + } //Listener interface void renderListener(); -private: + private: }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/loader.cpp b/Runtime/blitz3d/loader.cpp index 6f5e9f4..6ae9f8f 100644 --- a/Runtime/blitz3d/loader.cpp +++ b/Runtime/blitz3d/loader.cpp @@ -1,77 +1,89 @@ -#include "std.hpp" #include "meshloader.hpp" #include "meshmodel.hpp" +#include "std.hpp" -struct Surf{ +struct Surf { vector tris; }; -static map brush_map; +static map brush_map; static vector verts; -void MeshLoader::clear(){ - map::const_iterator it; - for( it=brush_map.begin();it!=brush_map.end();++it ){ +void MeshLoader::clear() +{ + map::const_iterator it; + for (it = brush_map.begin(); it != brush_map.end(); ++it) { delete it->second; } brush_map.clear(); verts.clear(); } -int MeshLoader::numVertices(){ +int MeshLoader::numVertices() +{ return verts.size(); } -void MeshLoader::addVertex( const Surface::Vertex &v ){ - verts.push_back( v ); +void MeshLoader::addVertex(const Surface::Vertex& v) +{ + verts.push_back(v); } -Surface::Vertex &refVertex( int n ){ +Surface::Vertex& refVertex(int n) +{ return verts[n]; } -void MeshLoader::addTriangle( const int verts[3],const Brush &b ){ - addTriangle( verts[0],verts[1],verts[2],b ); +void MeshLoader::addTriangle(const int verts[3], const Brush& b) +{ + addTriangle(verts[0], verts[1], verts[2], b); } -void MeshLoader::addTriangle( int v0,int v1,int v2,const Brush &b ){ +void MeshLoader::addTriangle(int v0, int v1, int v2, const Brush& b) +{ //find surface - Surf *surf; - map::const_iterator it=brush_map.find( b ); - if( it!=brush_map.end() ) surf=it->second; - else{ - surf=d_new Surf; - brush_map.insert( make_pair( b,surf ) ); + Surf* surf; + map::const_iterator it = brush_map.find(b); + if (it != brush_map.end()) + surf = it->second; + else { + surf = d_new Surf; + brush_map.insert(make_pair(b, surf)); } Surface::Triangle tri; - tri.verts[0]=v0;tri.verts[1]=v1;tri.verts[2]=v2; - surf->tris.push_back( tri ); + tri.verts[0] = v0; + tri.verts[1] = v1; + tri.verts[2] = v2; + surf->tris.push_back(tri); } -void MeshLoader::updateMesh( MeshModel *mesh ){ - map vert_map; - map::iterator it; - for( it=brush_map.begin();it!=brush_map.end();++it ){ +void MeshLoader::updateMesh(MeshModel* mesh) +{ + map vert_map; + map::iterator it; + for (it = brush_map.begin(); it != brush_map.end(); ++it) { vert_map.clear(); - Brush b=it->first; - Surf *t=it->second; - Surface *surf=mesh->findSurface( b ); - if( !surf ) surf=mesh->createSurface( b ); - for( int k=0;ktris.size();++k ){ - for( int j=0;j<3;++j ){ - int n=t->tris[k].verts[j],id; - map::const_iterator it=vert_map.find( n ); - if( it!=vert_map.end() ) id=it->second; - else{ - id=surf->numVertices(); - surf->addVertex( verts[n] ); - vert_map.insert( make_pair( n,id ) ); + Brush b = it->first; + Surf* t = it->second; + Surface* surf = mesh->findSurface(b); + if (!surf) + surf = mesh->createSurface(b); + for (int k = 0; k < t->tris.size(); ++k) { + for (int j = 0; j < 3; ++j) { + int n = t->tris[k].verts[j], id; + map::const_iterator it = vert_map.find(n); + if (it != vert_map.end()) + id = it->second; + else { + id = surf->numVertices(); + surf->addVertex(verts[n]); + vert_map.insert(make_pair(n, id)); } - t->tris[k].verts[j]=id; + t->tris[k].verts[j] = id; } - surf->addTriangle( t->tris[k] ); + surf->addTriangle(t->tris[k]); } } clear(); diff --git a/Runtime/blitz3d/loader_3ds.cpp b/Runtime/blitz3d/loader_3ds.cpp index 88f7898..e122dae 100644 --- a/Runtime/blitz3d/loader_3ds.cpp +++ b/Runtime/blitz3d/loader_3ds.cpp @@ -1,160 +1,173 @@ -#include "std.hpp" #include "loader_3ds.hpp" -#include "meshmodel.hpp" #include "animation.hpp" +#include "meshmodel.hpp" +#include "std.hpp" -extern gxRuntime *gx_runtime; +extern gxRuntime* gx_runtime; #ifdef BETA -#define _log( X ) gx_runtime->debugLog( (string(X)).c_str() ); +#define _log(X) gx_runtime->debugLog((string(X)).c_str()); #else -#define _log( X ) +#define _log(X) #endif class Box; -static filebuf in; -static int chunk_end; -static vector parent_end; +static filebuf in; +static int chunk_end; +static vector parent_end; static unsigned short anim_len; -static bool conv,flip_tris; +static bool conv, flip_tris; static Transform conv_tform; -static bool collapse,animonly; +static bool collapse, animonly; -struct Face3DS{ - int verts[3]; +struct Face3DS { + int verts[3]; Brush brush; }; static vector faces; //static vector vertices; -static map materials_map; -static map name_map; -static map id_map; +static map materials_map; +static map name_map; +static map id_map; -static int nextChunk(){ - in.pubseekoff( chunk_end,ios_base::beg ); - if( chunk_end==parent_end.back() ) return 0; - unsigned short id;int len; - in.sgetn( (char*)&id,2 ); - in.sgetn( (char*)&len,4 ); - chunk_end=(int)in.pubseekoff( 0,ios_base::cur )+len-6; +static int nextChunk() +{ + in.pubseekoff(chunk_end, ios_base::beg); + if (chunk_end == parent_end.back()) + return 0; + unsigned short id; + int len; + in.sgetn((char*)&id, 2); + in.sgetn((char*)&len, 4); + chunk_end = (int)in.pubseekoff(0, ios_base::cur) + len - 6; return id; } -static void enterChunk(){ - parent_end.push_back( chunk_end ); - chunk_end=(int)in.pubseekoff( 0,ios_base::cur ); +static void enterChunk() +{ + parent_end.push_back(chunk_end); + chunk_end = (int)in.pubseekoff(0, ios_base::cur); } -static void leaveChunk(){ - chunk_end=parent_end.back(); +static void leaveChunk() +{ + chunk_end = parent_end.back(); parent_end.pop_back(); } -static string parseString(){ +static string parseString() +{ string t; - while( int c=in.sbumpc() ) t+=char(c); + while (int c = in.sbumpc()) + t += char(c); return t; } enum { - CHUNK_RGBF = 0x0010, - CHUNK_RGBB = 0x0011, -// CHUNK_RBGB2 = 0x0012, // ?? NOT HLS. - CHUNK_MAIN = 0x4D4D, - CHUNK_SCENE = 0x3D3D, - CHUNK_BKGCOLOR = 0x1200, - CHUNK_AMBCOLOR = 0x2100, - CHUNK_OBJECT = 0x4000, - CHUNK_TRIMESH = 0x4100, - CHUNK_VERTLIST = 0x4110, - CHUNK_FACELIST = 0x4120, - CHUNK_FACEMAT = 0x4130, - CHUNK_MAPLIST = 0x4140, - CHUNK_SMOOLIST = 0x4150, - CHUNK_TRMATRIX = 0x4160, - CHUNK_LIGHT = 0x4600, - CHUNK_SPOTLIGHT = 0x4610, - CHUNK_CAMERA = 0x4700, - CHUNK_MATERIAL = 0xAFFF, - CHUNK_MATNAME = 0xA000, - CHUNK_AMBIENT = 0xA010, - CHUNK_DIFFUSE = 0xA020, - CHUNK_SPECULAR = 0xA030, - CHUNK_TEXTURE = 0xA200, - CHUNK_BUMPMAP = 0xA230, - CHUNK_MAPFILE = 0xA300, - CHUNK_KEYFRAMER = 0xB000, - CHUNK_MESHINFO = 0xB002, - CHUNK_HIERPOS = 0xB030, - CHUNK_HIERINFO = 0xB010, - CHUNK_FRAMES = 0xB008 + CHUNK_RGBF = 0x0010, + CHUNK_RGBB = 0x0011, + // CHUNK_RBGB2 = 0x0012, // ?? NOT HLS. + CHUNK_MAIN = 0x4D4D, + CHUNK_SCENE = 0x3D3D, + CHUNK_BKGCOLOR = 0x1200, + CHUNK_AMBCOLOR = 0x2100, + CHUNK_OBJECT = 0x4000, + CHUNK_TRIMESH = 0x4100, + CHUNK_VERTLIST = 0x4110, + CHUNK_FACELIST = 0x4120, + CHUNK_FACEMAT = 0x4130, + CHUNK_MAPLIST = 0x4140, + CHUNK_SMOOLIST = 0x4150, + CHUNK_TRMATRIX = 0x4160, + CHUNK_LIGHT = 0x4600, + CHUNK_SPOTLIGHT = 0x4610, + CHUNK_CAMERA = 0x4700, + CHUNK_MATERIAL = 0xAFFF, + CHUNK_MATNAME = 0xA000, + CHUNK_AMBIENT = 0xA010, + CHUNK_DIFFUSE = 0xA020, + CHUNK_SPECULAR = 0xA030, + CHUNK_TEXTURE = 0xA200, + CHUNK_BUMPMAP = 0xA230, + CHUNK_MAPFILE = 0xA300, + CHUNK_KEYFRAMER = 0xB000, + CHUNK_MESHINFO = 0xB002, + CHUNK_HIERPOS = 0xB030, + CHUNK_HIERINFO = 0xB010, + CHUNK_FRAMES = 0xB008 }; -static Vector parseColor(){ - Vector v; +static Vector parseColor() +{ + Vector v; unsigned char rgb[3]; enterChunk(); - while( int id=nextChunk() ){ - switch( id ){ + while (int id = nextChunk()) { + switch (id) { case CHUNK_RGBF: - in.sgetn( (char*)&v,12 ); + in.sgetn((char*)&v, 12); break; case CHUNK_RGBB: - in.sgetn( (char*)rgb,3 ); - v=Vector( rgb[0]/255.0f,rgb[1]/255.0f,rgb[2]/255.0f ); + in.sgetn((char*)rgb, 3); + v = Vector(rgb[0] / 255.0f, rgb[1] / 255.0f, rgb[2] / 255.0f); } } leaveChunk(); return v; } -static void parseVertList(){ +static void parseVertList() +{ unsigned short cnt; - in.sgetn( (char*)&cnt,2 ); - _log( "VertList cnt="+itoa(cnt) ); - while( cnt-- ){ + in.sgetn((char*)&cnt, 2); + _log("VertList cnt=" + itoa(cnt)); + while (cnt--) { Surface::Vertex v; - in.sgetn( (char*)&v.coords,12 ); - if( conv ) v.coords=conv_tform * v.coords; - MeshLoader::addVertex( v ); + in.sgetn((char*)&v.coords, 12); + if (conv) + v.coords = conv_tform * v.coords; + MeshLoader::addVertex(v); } } -static void parseFaceMat(){ - string name=parseString(); - _log( "FaceMat: "+name ); - Brush mat=materials_map[name]; +static void parseFaceMat() +{ + string name = parseString(); + _log("FaceMat: " + name); + Brush mat = materials_map[name]; unsigned short cnt; - in.sgetn( (char*)&cnt,2 ); - while( cnt-- ){ + in.sgetn((char*)&cnt, 2); + while (cnt--) { unsigned short face; - in.sgetn( (char*)&face,2 ); - faces[face].brush=mat; + in.sgetn((char*)&face, 2); + faces[face].brush = mat; } } -static void parseFaceList(){ +static void parseFaceList() +{ unsigned short cnt; - in.sgetn( (char*)&cnt,2 ); - _log( "FaceList cnt="+itoa(cnt) ); - while( cnt-- ){ + in.sgetn((char*)&cnt, 2); + _log("FaceList cnt=" + itoa(cnt)); + while (cnt--) { unsigned short v[4]; - in.sgetn( (char*)v,8 ); + in.sgetn((char*)v, 8); Face3DS face; - face.verts[0]=v[0]; - face.verts[1]=v[1]; - face.verts[2]=v[2]; - if( flip_tris ) std::swap( face.verts[1],face.verts[2] ); - faces.push_back( face ); + face.verts[0] = v[0]; + face.verts[1] = v[1]; + face.verts[2] = v[2]; + if (flip_tris) + std::swap(face.verts[1], face.verts[2]); + faces.push_back(face); } enterChunk(); - while( int id=nextChunk() ){ - switch( id ){ + while (int id = nextChunk()) { + switch (id) { case CHUNK_FACEMAT: parseFaceMat(); break; @@ -163,22 +176,24 @@ static void parseFaceList(){ leaveChunk(); } -static void parseMapList(){ - _log( "MapList" ); +static void parseMapList() +{ + _log("MapList"); unsigned short cnt; - in.sgetn( (char*)&cnt,2 ); - for( int k=0;ktex_coords[0]=v->tex_coords[1]=Vector( uv[0],1-uv[1],1 ); + in.sgetn((char*)uv, 8); + Surface::Vertex& v = MeshLoader::refVertex(k); + v.tex_coords[0][0] = v.tex_coords[1][0] = uv[0]; + v.tex_coords[0][1] = v.tex_coords[1][1] = 1 - uv[1]; + // v->tex_coords[0]=v->tex_coords[1]=Vector( uv[0],1-uv[1],1 ); } } -static void parseTriMesh( MeshModel *mesh ){ - _log( "TriMesh" ); +static void parseTriMesh(MeshModel* mesh) +{ + _log("TriMesh"); enterChunk(); Transform tform; @@ -186,87 +201,93 @@ static void parseTriMesh( MeshModel *mesh ){ MeshLoader::beginMesh(); - while( int id=nextChunk() ){ - switch( id ){ + while (int id = nextChunk()) { + switch (id) { case CHUNK_VERTLIST: - if( !animonly ) parseVertList(); + if (!animonly) + parseVertList(); break; case CHUNK_MAPLIST: - if( !animonly ) parseMapList(); + if (!animonly) + parseMapList(); break; case CHUNK_FACELIST: - if( !animonly ) parseFaceList(); + if (!animonly) + parseFaceList(); break; case CHUNK_TRMATRIX: - in.sgetn( (char*)&tform,48 ); - if( conv ) tform=conv_tform * tform * -conv_tform; + in.sgetn((char*)&tform, 48); + if (conv) + tform = conv_tform * tform * -conv_tform; break; } } leaveChunk(); //should really do something here... -// bool neg_x=tform.m.j.cross(tform.m.k).dot(tform.m.i)<0; + // bool neg_x=tform.m.j.cross(tform.m.k).dot(tform.m.i)<0; int k; - mesh->SetWorldTransform( tform ); + mesh->SetWorldTransform(tform); - if( animonly ){ - MeshLoader::endMesh( 0 ); + if (animonly) { + MeshLoader::endMesh(0); return; } - Transform inv_tform=-tform; - for( k=0;kupdateNormals(); faces.clear(); } -static void parseObject( MeshModel *root ){ +static void parseObject(MeshModel* root) +{ //skip name - string name=parseString(); - _log( "Object:"+name ); - MeshModel *mesh=0; + string name = parseString(); + _log("Object:" + name); + MeshModel* mesh = 0; enterChunk(); - while( int id=nextChunk() ){ - switch( id ){ + while (int id = nextChunk()) { + switch (id) { case CHUNK_TRIMESH: - mesh=new MeshModel(); - mesh->SetName( name ); - mesh->SetParent( root ); - name_map[name]=mesh; - parseTriMesh( mesh ); + mesh = new MeshModel(); + mesh->SetName(name); + mesh->SetParent(root); + name_map[name] = mesh; + parseTriMesh(mesh); break; } } leaveChunk(); } -static void parseMaterial(){ - _log( "Material" ); - Brush mat; - string name,tex_name; +static void parseMaterial() +{ + _log("Material"); + Brush mat; + string name, tex_name; enterChunk(); - while( int id=nextChunk() ){ - switch( id ){ + while (int id = nextChunk()) { + switch (id) { case CHUNK_MATNAME: - name=parseString(); + name = parseString(); break; case CHUNK_DIFFUSE: - mat.setColor( parseColor() ); + mat.setColor(parseColor()); break; case CHUNK_AMBIENT: break; @@ -274,10 +295,10 @@ static void parseMaterial(){ break; case CHUNK_TEXTURE: enterChunk(); - while( int id=nextChunk() ){ - switch( id ){ + while (int id = nextChunk()) { + switch (id) { case CHUNK_MAPFILE: - tex_name=parseString(); + tex_name = parseString(); break; } } @@ -285,208 +306,233 @@ static void parseMaterial(){ break; } } - if( tex_name.size() ){ - mat.setTexture( 0,Texture( tex_name,0 ),0 ); - mat.setColor( Vector( 1,1,1 ) ); + if (tex_name.size()) { + mat.setTexture(0, Texture(tex_name, 0), 0); + mat.setColor(Vector(1, 1, 1)); } - if( name.size() ){ - materials_map[name]=mat; + if (name.size()) { + materials_map[name] = mat; } leaveChunk(); } -static void parseScene( MeshModel *root ){ - _log( "Scene" ); +static void parseScene(MeshModel* root) +{ + _log("Scene"); enterChunk(); - while( int id=nextChunk() ){ - switch( id ){ + while (int id = nextChunk()) { + switch (id) { case CHUNK_OBJECT: - parseObject( root ); + parseObject(root); break; case CHUNK_MATERIAL: - if( !animonly ) parseMaterial(); + if (!animonly) + parseMaterial(); break; } } leaveChunk(); } -static void parseAnimKeys( Animation *anim,int type ){ - - int cnt=0; +static void parseAnimKeys(Animation* anim, int type) +{ + int cnt = 0; short t_flags; - in.sgetn( (char*)&t_flags,2 ); - in.pubseekoff( 8,ios_base::cur ); - in.sgetn( (char*)&cnt,2 ); - in.pubseekoff( 2,ios_base::cur ); - _log( "ANIM_TRACK: frames="+itoa( cnt ) ); - Vector pos,axis,scale; - float angle; - Quat quat; - for( int k=0;ksetPositionKey( time,pos ); + in.sgetn((char*)&time, 4); + in.sgetn((char*)&flags, 2); + float tens = 0, cont = 0, bias = 0, ease_to = 0, ease_from = 0; + if (flags & 1) + in.sgetn((char*)&tens, 4); + if (flags & 2) + in.sgetn((char*)&cont, 4); + if (flags & 4) + in.sgetn((char*)&bias, 4); + if (flags & 8) + in.sgetn((char*)&ease_to, 4); + if (flags & 16) + in.sgetn((char*)&ease_from, 4); + switch (type) { + case 0xb020: //POS_TRACK_TAG + in.sgetn((char*)&pos, 12); + if (conv) + pos = conv_tform * pos; + // _log( "POS_KEY: time="+itoa(time)+" pos="+ftoa( pos.x )+","+ftoa( pos.y )+","+ftoa( pos.z ) ); + if (time <= anim_len) + anim->setPositionKey(time, pos); break; - case 0xb021: //ROT_TRACK_TAG - in.sgetn( (char*)&angle,4 ); - in.sgetn( (char*)&axis,12 ); -// _log( "ROT_KEY: time="+itoa(time)+" angle="+ftoa(angle)+" axis="+ftoa(axis.x)+","+ftoa(axis.y)+","+ftoa(axis.z) ); - if( axis.length()>FLT_EPSILON ){ - if( flip_tris ) angle=-angle; - if( conv ) axis=conv_tform.m*axis; - quat=Quat( cosf( angle/2 ),axis.normalized()*sinf( angle/2 ) )*quat; + case 0xb021: //ROT_TRACK_TAG + in.sgetn((char*)&angle, 4); + in.sgetn((char*)&axis, 12); + // _log( "ROT_KEY: time="+itoa(time)+" angle="+ftoa(angle)+" axis="+ftoa(axis.x)+","+ftoa(axis.y)+","+ftoa(axis.z) ); + if (axis.length() > FLT_EPSILON) { + if (flip_tris) + angle = -angle; + if (conv) + axis = conv_tform.m * axis; + quat = Quat(cosf(angle / 2), axis.normalized() * sinf(angle / 2)) * quat; quat.normalize(); } - if( time<=anim_len ) anim->setRotationKey( time,quat ); + if (time <= anim_len) + anim->setRotationKey(time, quat); break; - case 0xb022: //SCL_TRACK_TAG - in.sgetn( (char*)&scale,12 ); - if( conv ) scale=conv_tform.m*scale; -// scale.x=fabs(scale.x);scale.y=fabs(scale.y);scale.z=fabs(scale.z); - _log( "SCL_KEY: time="+itoa(time)+" scale="+ftoa( scale.x )+","+ftoa( scale.y )+","+ftoa( scale.z ) ); - if( time<=anim_len ) anim->setScaleKey( time,scale ); + case 0xb022: //SCL_TRACK_TAG + in.sgetn((char*)&scale, 12); + if (conv) + scale = conv_tform.m * scale; + // scale.x=fabs(scale.x);scale.y=fabs(scale.y);scale.z=fabs(scale.z); + _log("SCL_KEY: time=" + itoa(time) + " scale=" + ftoa(scale.x) + "," + ftoa(scale.y) + "," + ftoa(scale.z)); + if (time <= anim_len) + anim->setScaleKey(time, scale); break; } } } -static void parseMeshInfo( MeshModel *root,float curr_time ){ - _log( "OBJECT_NODE_TAG" ); +static void parseMeshInfo(MeshModel* root, float curr_time) +{ + _log("OBJECT_NODE_TAG"); enterChunk(); - string name,inst; - Vector pivot; - Animation anim; - unsigned short id=65535,parent=65535,flags1,flags2; - Box box = Box( Vector(),Vector() ); - Vector box_centre; - while( int chunk_id=nextChunk() ){ - switch( chunk_id ){ - case 0xb030: //NODE_ID - in.sgetn( (char*)&id,2 ); - _log( "NODE_ID: "+itoa(id) ); + string name, inst; + Vector pivot; + Animation anim; + unsigned short id = 65535, parent = 65535, flags1, flags2; + Box box = Box(Vector(), Vector()); + Vector box_centre; + while (int chunk_id = nextChunk()) { + switch (chunk_id) { + case 0xb030: //NODE_ID + in.sgetn((char*)&id, 2); + _log("NODE_ID: " + itoa(id)); break; - case 0xb010: //NODE_HDR - name=parseString(); - in.sgetn( (char*)&flags1,2 ); - in.sgetn( (char*)&flags2,2 ); - in.sgetn( (char*)&parent,2 ); - _log( "NODE_HDR: name="+name+" parent="+itoa(parent) ); + case 0xb010: //NODE_HDR + name = parseString(); + in.sgetn((char*)&flags1, 2); + in.sgetn((char*)&flags2, 2); + in.sgetn((char*)&parent, 2); + _log("NODE_HDR: name=" + name + " parent=" + itoa(parent)); break; - case 0xb011: //INSTANCE NAME - inst=parseString(); - _log( "INSTANCE_NAME: "+inst ); + case 0xb011: //INSTANCE NAME + inst = parseString(); + _log("INSTANCE_NAME: " + inst); break; - case 0xb013: //PIVOT - in.sgetn( (char*)&pivot,12 ); - if( conv ) pivot=conv_tform * pivot; - _log( "PIVOT: "+ftoa(pivot.x)+","+ftoa(pivot.y)+","+ftoa(pivot.z) ); + case 0xb013: //PIVOT + in.sgetn((char*)&pivot, 12); + if (conv) + pivot = conv_tform * pivot; + _log("PIVOT: " + ftoa(pivot.x) + "," + ftoa(pivot.y) + "," + ftoa(pivot.z)); break; - case 0xb014: //BOUNDBOX - in.sgetn( (char*)&(box.a),12 ); - in.sgetn( (char*)&(box.b),12 ); - box_centre=box.centre(); - if( conv ) box_centre=conv_tform * box_centre; - _log( "BOUNDBOX: min="+ftoa(box.a.x)+","+ftoa(box.a.y)+","+ftoa(box.a.z)+" max="+ftoa(box.b.x)+","+ftoa(box.b.y)+","+ftoa(box.b.z) ); + case 0xb014: //BOUNDBOX + in.sgetn((char*)&(box.a), 12); + in.sgetn((char*)&(box.b), 12); + box_centre = box.centre(); + if (conv) + box_centre = conv_tform * box_centre; + _log("BOUNDBOX: min=" + ftoa(box.a.x) + "," + ftoa(box.a.y) + "," + ftoa(box.a.z) + " max=" + ftoa(box.b.x) + + "," + ftoa(box.b.y) + "," + ftoa(box.b.z)); break; - case 0xb020: //POS_TRACK_TAG - case 0xb021: //ROT_TRACK_TAG - case 0xb022: //SCALE_TRACK_TAG - if( !collapse ) parseAnimKeys( &anim,chunk_id ); + case 0xb020: //POS_TRACK_TAG + case 0xb021: //ROT_TRACK_TAG + case 0xb022: //SCALE_TRACK_TAG + if (!collapse) + parseAnimKeys(&anim, chunk_id); break; } } leaveChunk(); - MeshModel *p=root; - if( parent!=65535 ){ - map::const_iterator it=id_map.find( parent ); - if( it==id_map.end() ) return; - p=it->second; + MeshModel* p = root; + if (parent != 65535) { + map::const_iterator it = id_map.find(parent); + if (it == id_map.end()) + return; + p = it->second; } - MeshModel *mesh=0; - if( name=="$$$DUMMY" ){ - mesh=new MeshModel(); - mesh->SetName( inst ); - mesh->SetParent( p ); - }else{ - map::const_iterator it=name_map.find( name ); - if( it==name_map.end() ) return; - mesh=it->second; - name_map.erase( name ); - if( pivot!=Vector() ){ - mesh->transform( -pivot ); + MeshModel* mesh = 0; + if (name == "$$$DUMMY") { + mesh = new MeshModel(); + mesh->SetName(inst); + mesh->SetParent(p); + } else { + map::const_iterator it = name_map.find(name); + if (it == name_map.end()) + return; + mesh = it->second; + name_map.erase(name); + if (pivot != Vector()) { + mesh->transform(-pivot); } - Transform t= - mesh->GetWorldTransform(); - mesh->SetParent( p ); - mesh->SetWorldTransform( t ); + Transform t = mesh->GetWorldTransform(); + mesh->SetParent(p); + mesh->SetWorldTransform(t); } - mesh->setAnimation( anim ); + mesh->setAnimation(anim); - if( id!=65535 ) id_map[id]=mesh; + if (id != 65535) + id_map[id] = mesh; } -static void parseKeyFramer( MeshModel *root ){ - _log( "KeyFramer" ); +static void parseKeyFramer(MeshModel* root) +{ + _log("KeyFramer"); enterChunk(); - string file_3ds; - unsigned short rev,curr_time=0; - while( int id=nextChunk() ){ - switch( id ){ - case 0xb009: //CURR_TIME - in.sgetn( (char*)&curr_time,2 ); - _log( "CURR_TIME: "+itoa(curr_time) ); + string file_3ds; + unsigned short rev, curr_time = 0; + while (int id = nextChunk()) { + switch (id) { + case 0xb009: //CURR_TIME + in.sgetn((char*)&curr_time, 2); + _log("CURR_TIME: " + itoa(curr_time)); break; - case 0xb00a: //KFHDR - in.sgetn( (char*)&rev,2 ); - file_3ds=parseString(); - in.sgetn( (char*)&anim_len,2 ); - _log( "KFHDR: revision="+itoa(rev)+" 3dsfile="+file_3ds+" anim_len="+itoa(anim_len) ); + case 0xb00a: //KFHDR + in.sgetn((char*)&rev, 2); + file_3ds = parseString(); + in.sgetn((char*)&anim_len, 2); + _log("KFHDR: revision=" + itoa(rev) + " 3dsfile=" + file_3ds + " anim_len=" + itoa(anim_len)); break; - case 0xb002: //object keyframer data... - parseMeshInfo( root,curr_time ); + case 0xb002: //object keyframer data... + parseMeshInfo(root, curr_time); break; } } - if( !collapse ){ - root->setAnimator( new Animator( root,anim_len ) ); + if (!collapse) { + root->setAnimator(new Animator(root, anim_len)); } leaveChunk(); } -static MeshModel *parseFile(){ - unsigned short id;int len; - in.sgetn( (char*)&id,2 ); - in.sgetn( (char*)&len,4 ); - if( id!=CHUNK_MAIN ) return 0; - chunk_end=(int)in.pubseekoff( 0,ios_base::cur )+len-6; +static MeshModel* parseFile() +{ + unsigned short id; + int len; + in.sgetn((char*)&id, 2); + in.sgetn((char*)&len, 4); + if (id != CHUNK_MAIN) + return 0; + chunk_end = (int)in.pubseekoff(0, ios_base::cur) + len - 6; enterChunk(); - MeshModel *root=new MeshModel(); - while( int id=nextChunk() ){ - switch( id ){ + MeshModel* root = new MeshModel(); + while (int id = nextChunk()) { + switch (id) { case CHUNK_SCENE: - parseScene( root ); + parseScene(root); break; case CHUNK_KEYFRAMER: - parseKeyFramer( root ); + parseKeyFramer(root); break; } } @@ -494,23 +540,24 @@ static MeshModel *parseFile(){ return root; } -MeshModel *Loader_3DS::load( const string &filename,const Transform &t,int hint ){ - - conv_tform=t; - conv=flip_tris=false; - if( conv_tform!=Transform() ){ - conv=true; - if( conv_tform.m.i.cross(conv_tform.m.j).dot(conv_tform.m.k)<0 ) flip_tris=true; +MeshModel* Loader_3DS::load(const string& filename, const Transform& t, int hint) +{ + conv_tform = t; + conv = flip_tris = false; + if (conv_tform != Transform()) { + conv = true; + if (conv_tform.m.i.cross(conv_tform.m.j).dot(conv_tform.m.k) < 0) + flip_tris = true; } - collapse=!!(hint&MeshLoader::HINT_COLLAPSE); - animonly=!!(hint&MeshLoader::HINT_ANIMONLY); + collapse = !!(hint & MeshLoader::HINT_COLLAPSE); + animonly = !!(hint & MeshLoader::HINT_ANIMONLY); - if( !in.open( filename.c_str(),ios_base::in|ios_base::binary ) ){ + if (!in.open(filename.c_str(), ios_base::in | ios_base::binary)) { return 0; } - MeshModel *root=parseFile(); + MeshModel* root = parseFile(); in.close(); materials_map.clear(); diff --git a/Runtime/blitz3d/loader_3ds.hpp b/Runtime/blitz3d/loader_3ds.hpp index 4307c22..a066c22 100644 --- a/Runtime/blitz3d/loader_3ds.hpp +++ b/Runtime/blitz3d/loader_3ds.hpp @@ -4,9 +4,9 @@ #include "meshloader.hpp" -class Loader_3DS : public MeshLoader{ -public: - MeshModel *load( const string &f,const Transform &conv,int hint ); +class Loader_3DS : public MeshLoader { + public: + MeshModel* load(const string& f, const Transform& conv, int hint); }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/loader_b3d.cpp b/Runtime/blitz3d/loader_b3d.cpp index bdcd426..fc3767d 100644 --- a/Runtime/blitz3d/loader_b3d.cpp +++ b/Runtime/blitz3d/loader_b3d.cpp @@ -1,192 +1,233 @@ -#include "std.hpp" #include "loader_b3d.hpp" #include "meshmodel.hpp" -#include "pivot.hpp" #include "meshutil.hpp" +#include "pivot.hpp" +#include "std.hpp" //#define SHOW_BONES -static FILE *in; -static vector chunk_stack; +static FILE* in; +static vector chunk_stack; static vector textures; -static vector brushes; +static vector brushes; static vector bones; static bool collapse; static bool animonly; -static int swap_endian( int n ){ - return ((n&0xff)<<24)|((n&0xff00)<<8)|((n&0xff0000)>>8)|((n&0xff000000)>>24); +static int swap_endian(int n) +{ + return ((n & 0xff) << 24) | ((n & 0xff00) << 8) | ((n & 0xff0000) >> 8) | ((n & 0xff000000) >> 24); } -static void clear(){ +static void clear() +{ bones.clear(); brushes.clear(); textures.clear(); chunk_stack.clear(); } -static int readChunk(){ +static int readChunk() +{ int header[2]; - if( fread( header,8,1,in )<1 ) return 0; - chunk_stack.push_back( ftell( in )+header[1] ); - return swap_endian( header[0] ); + if (fread(header, 8, 1, in) < 1) + return 0; + chunk_stack.push_back(ftell(in) + header[1]); + return swap_endian(header[0]); } -static void exitChunk(){ - fseek( in,chunk_stack.back(),SEEK_SET ); +static void exitChunk() +{ + fseek(in, chunk_stack.back(), SEEK_SET); chunk_stack.pop_back(); } -static int chunkSize(){ - return chunk_stack.back()-ftell( in ); +static int chunkSize() +{ + return chunk_stack.back() - ftell(in); } -static void read( void *buf,int n ){ - fread( buf,n,1,in ); +static void read(void* buf, int n) +{ + fread(buf, n, 1, in); } -static void skip( int n ){ - fseek( in,n,SEEK_CUR ); +static void skip(int n) +{ + fseek(in, n, SEEK_CUR); } -static int readInt(){ +static int readInt() +{ int n; - read( &n,4 ); + read(&n, 4); return n; } -static void readIntArray( int t[],int n ){ - read( t,n*4 ); +static void readIntArray(int t[], int n) +{ + read(t, n * 4); } -static float readFloat(){ +static float readFloat() +{ float n; - read( &n,4 ); + read(&n, 4); return n; } -static void readFloatArray( float t[],int n ){ - read( t,n*4 ); +static void readFloatArray(float t[], int n) +{ + read(t, n * 4); } -static void readColor( unsigned *t ){ - float r=readFloat();if(r<0) r=0;else if(r>1) r=1; - float g=readFloat();if(g<0) g=0;else if(g>1) g=1; - float b=readFloat();if(b<0) b=0;else if(b>1) b=1; - float a=readFloat();if(a<0) a=0;else if(a>1) a=1; - *t=(int(a*255)<<24)|(int(r*255)<<16)|(int(g*255)<<8)|int(b*255); +static void readColor(unsigned* t) +{ + float r = readFloat(); + if (r < 0) + r = 0; + else if (r > 1) + r = 1; + float g = readFloat(); + if (g < 0) + g = 0; + else if (g > 1) + g = 1; + float b = readFloat(); + if (b < 0) + b = 0; + else if (b > 1) + b = 1; + float a = readFloat(); + if (a < 0) + a = 0; + else if (a > 1) + a = 1; + *t = (int(a * 255) << 24) | (int(r * 255) << 16) | (int(g * 255) << 8) | int(b * 255); } -static string readString(){ +static string readString() +{ string t; - for(;;){ + for (;;) { char c; - read( &c,1 ); - if( !c ) return t; - t+=c; + read(&c, 1); + if (!c) + return t; + t += c; } } -static void readTextures(){ - while( chunkSize() ){ - string name=readString(); - int flags=readInt(); - int blend=readInt(); - float pos[2],scl[2]; - readFloatArray( pos,2 ); - readFloatArray( scl,2 ); - float rot=readFloat(); +static void readTextures() +{ + while (chunkSize()) { + string name = readString(); + int flags = readInt(); + int blend = readInt(); + float pos[2], scl[2]; + readFloatArray(pos, 2); + readFloatArray(scl, 2); + float rot = readFloat(); //create texture - Texture tex( name,flags & 0xffff ); + Texture tex(name, flags & 0xffff); - tex.setBlend( blend ); - if( flags & 0x10000 ) tex.setFlags( gxScene::TEX_COORDS2 ); + tex.setBlend(blend); + if (flags & 0x10000) + tex.setFlags(gxScene::TEX_COORDS2); - if( pos[0]!=0 || pos[1]!=0 ) tex.setPosition( pos[0],pos[1] ); - if( scl[0]!=1 || scl[1]!=1 ) tex.setScale( scl[0],scl[1] ); - if( rot!=0 ) tex.setRotation( rot ); + if (pos[0] != 0 || pos[1] != 0) + tex.setPosition(pos[0], pos[1]); + if (scl[0] != 1 || scl[1] != 1) + tex.setScale(scl[0], scl[1]); + if (rot != 0) + tex.setRotation(rot); - textures.push_back( tex ); + textures.push_back(tex); } } -static void readBrushes(){ - int n_texs=readInt(); +static void readBrushes() +{ + int n_texs = readInt(); - int tex_id[8]={-1,-1,-1,-1,-1,-1,-1,-1}; + int tex_id[8] = {-1, -1, -1, -1, -1, -1, -1, -1}; - while( chunkSize() ){ - string name=readString(); - float col[4]; - readFloatArray( col,4 ); - float shi=readFloat(); - int blend=readInt(); - int fx=readInt(); - readIntArray( tex_id,n_texs ); + while (chunkSize()) { + string name = readString(); + float col[4]; + readFloatArray(col, 4); + float shi = readFloat(); + int blend = readInt(); + int fx = readInt(); + readIntArray(tex_id, n_texs); Brush bru; - bru.setColor( Vector( col[0],col[1],col[2] ) ); - bru.setAlpha( col[3] ); - bru.setShininess( shi ); - bru.setBlend( blend ); - bru.setFX( fx ); + bru.setColor(Vector(col[0], col[1], col[2])); + bru.setAlpha(col[3]); + bru.setShininess(shi); + bru.setBlend(blend); + bru.setFX(fx); - for( int k=0;k<8;++k ){ - if( tex_id[k]<0 ) continue; - bru.setTexture( k,textures[tex_id[k]],0 ); + for (int k = 0; k < 8; ++k) { + if (tex_id[k] < 0) + continue; + bru.setTexture(k, textures[tex_id[k]], 0); } - brushes.push_back( bru ); + brushes.push_back(bru); } } -static int readVertices(){ +static int readVertices() +{ + int flags = readInt(); + int tc_sets = readInt(); + int tc_size = readInt(); - int flags=readInt(); - int tc_sets=readInt(); - int tc_size=readInt(); - - float tc[4]={0}; + float tc[4] = {0}; Surface::Vertex t; - while( chunkSize() ){ - readFloatArray( t.coords,3 ); - if( flags&1 ){ - readFloatArray( t.normal,3 ); + while (chunkSize()) { + readFloatArray(t.coords, 3); + if (flags & 1) { + readFloatArray(t.normal, 3); } - if( flags&2 ){ - readColor( &t.color ); + if (flags & 2) { + readColor(&t.color); } - for( int k=0;k=0 ? brushes[brush_id] : Brush(); - while( chunkSize() ){ +static void readTriangles() +{ + int brush_id = readInt(); + Brush b = brush_id >= 0 ? brushes[brush_id] : Brush(); + while (chunkSize()) { int verts[3]; - readIntArray( verts,3 ); - MeshLoader::addTriangle( verts,b ); + readIntArray(verts, 3); + MeshLoader::addTriangle(verts, b); } } -static int readMesh(){ - int flags=0; - while( chunkSize() ){ - switch( readChunk() ){ +static int readMesh() +{ + int flags = 0; + while (chunkSize()) { + switch (readChunk()) { case 'VRTS': - flags=readVertices(); + flags = readVertices(); break; case 'TRIS': readTriangles(); @@ -197,149 +238,156 @@ static int readMesh(){ return flags; } -static Object *readBone(){ - +static Object* readBone() +{ #ifdef SHOW_BONES Brush b; - b.setColor( Vector( 1,0,0 ) ); - b.setAlpha( .75f ); - MeshModel *bone=MeshUtil::createSphere( b,16 ); - Transform t; - t.m.i.x=.1f; - t.m.j.y=.1f; - t.m.k.z=.1f; - bone->transform( t ); + b.setColor(Vector(1, 0, 0)); + b.setAlpha(.75f); + MeshModel* bone = MeshUtil::createSphere(b, 16); + Transform t; + t.m.i.x = .1f; + t.m.j.y = .1f; + t.m.k.z = .1f; + bone->transform(t); #else - Pivot *bone=new Pivot(); + Pivot* bone = new Pivot(); #endif - bones.push_back( bone ); + bones.push_back(bone); - while( chunkSize() ){ - int vert=readInt(); - float weight=readFloat(); - MeshLoader::addBone( vert,weight,bones.size() ); + while (chunkSize()) { + int vert = readInt(); + float weight = readFloat(); + MeshLoader::addBone(vert, weight, bones.size()); } return bone; } -static void readKeys( Animation &anim ){ - int flags=readInt(); - while( chunkSize() ){ - int frame=readInt(); - if( flags&1 ){ +static void readKeys(Animation& anim) +{ + int flags = readInt(); + while (chunkSize()) { + int frame = readInt(); + if (flags & 1) { float pos[3]; - readFloatArray( pos,3 ); - anim.setPositionKey( frame,Vector(pos[0],pos[1],pos[2]) ); + readFloatArray(pos, 3); + anim.setPositionKey(frame, Vector(pos[0], pos[1], pos[2])); } - if( flags&2 ){ + if (flags & 2) { float scl[3]; - readFloatArray( scl,3 ); - anim.setScaleKey( frame,Vector(scl[0],scl[1],scl[2]) ); + readFloatArray(scl, 3); + anim.setScaleKey(frame, Vector(scl[0], scl[1], scl[2])); } - if( flags&4 ){ + if (flags & 4) { float rot[4]; - readFloatArray( rot,4 ); - anim.setRotationKey( frame,Quat(rot[0],Vector(rot[1],rot[2],rot[3])) ); + readFloatArray(rot, 4); + anim.setRotationKey(frame, Quat(rot[0], Vector(rot[1], rot[2], rot[3]))); } } } -static Object *readObject( Object *parent ){ +static Object* readObject(Object* parent) +{ + Object* obj = 0; - Object *obj=0; + string name = readString(); + float pos[3], scl[3], rot[4]; + readFloatArray(pos, 3); + readFloatArray(scl, 3); + readFloatArray(rot, 4); - string name=readString(); - float pos[3],scl[3],rot[4]; - readFloatArray( pos,3 ); - readFloatArray( scl,3 ); - readFloatArray( rot,4 ); + Animation keys; + int anim_len = 0; + MeshModel* mesh = 0; + int mesh_flags, mesh_brush; - Animation keys; - int anim_len=0; - MeshModel *mesh=0; - int mesh_flags,mesh_brush; - - while( chunkSize() ){ - switch( readChunk() ){ + while (chunkSize()) { + switch (readChunk()) { case 'MESH': MeshLoader::beginMesh(); - obj=mesh=new MeshModel(); - mesh_brush=readInt(); - mesh_flags=readMesh(); + obj = mesh = new MeshModel(); + mesh_brush = readInt(); + mesh_flags = readMesh(); break; case 'BONE': - obj=readBone(); + obj = readBone(); break; case 'KEYS': - readKeys( keys ); + readKeys(keys); break; case 'ANIM': readInt(); - anim_len=readInt(); + anim_len = readInt(); readFloat(); break; case 'NODE': - if( !obj ) obj=new MeshModel(); - readObject( obj ); + if (!obj) + obj = new MeshModel(); + readObject(obj); break; } exitChunk(); } - if( !obj ) obj=new MeshModel(); + if (!obj) + obj = new MeshModel(); - obj->SetName( name ); - obj->SetLocalPosition( Vector( pos[0],pos[1],pos[2] ) ); - obj->SetLocalScale( Vector( scl[0],scl[1],scl[2] ) ); - obj->SetLocalRotation( Quat( rot[0],Vector( rot[1],rot[2],rot[3] ) ) ); - obj->setAnimation( keys ); + obj->SetName(name); + obj->SetLocalPosition(Vector(pos[0], pos[1], pos[2])); + obj->SetLocalScale(Vector(scl[0], scl[1], scl[2])); + obj->SetLocalRotation(Quat(rot[0], Vector(rot[1], rot[2], rot[3]))); + obj->setAnimation(keys); - if( mesh ){ - MeshLoader::endMesh( mesh ); - if( !(mesh_flags&1) ) mesh->updateNormals(); - if( mesh_brush!=-1 ) mesh->setBrush( brushes[mesh_brush] ); + if (mesh) { + MeshLoader::endMesh(mesh); + if (!(mesh_flags & 1)) + mesh->updateNormals(); + if (mesh_brush != -1) + mesh->setBrush(brushes[mesh_brush]); } - if( mesh && bones.size() ){ - bones.insert( bones.begin(),mesh ); - mesh->setAnimator( new Animator( bones,anim_len ) ); + if (mesh && bones.size()) { + bones.insert(bones.begin(), mesh); + mesh->setAnimator(new Animator(bones, anim_len)); mesh->createBones(); bones.clear(); - }else if( anim_len ){ - obj->setAnimator( new Animator( obj,anim_len ) ); + } else if (anim_len) { + obj->setAnimator(new Animator(obj, anim_len)); } - if( parent ) obj->SetParent( parent ); + if (parent) + obj->SetParent(parent); return obj; } -MeshModel *Loader_B3D::load( const string &f,const Transform &conv,int hint ){ +MeshModel* Loader_B3D::load(const string& f, const Transform& conv, int hint) +{ + collapse = !!(hint & MeshLoader::HINT_COLLAPSE); + animonly = !!(hint & MeshLoader::HINT_ANIMONLY); - collapse=!!(hint&MeshLoader::HINT_COLLAPSE); - animonly=!!(hint&MeshLoader::HINT_ANIMONLY); - - in=fopen( f.c_str(),"rb" ); - if( !in ) return 0; + in = fopen(f.c_str(), "rb"); + if (!in) + return 0; ::clear(); - int tag=readChunk(); - if( tag!='BB3D' ){ - fclose( in ); + int tag = readChunk(); + if (tag != 'BB3D') { + fclose(in); return 0; } - int version=readInt(); - if( version>1 ){ - fclose( in ); + int version = readInt(); + if (version > 1) { + fclose(in); return 0; } - Object *obj=0; - while( chunkSize() ){ - switch( readChunk() ){ + Object* obj = 0; + while (chunkSize()) { + switch (readChunk()) { case 'TEXS': readTextures(); break; @@ -347,12 +395,12 @@ MeshModel *Loader_B3D::load( const string &f,const Transform &conv,int hint ){ readBrushes(); break; case 'NODE': - obj=readObject( 0 ); + obj = readObject(0); break; } exitChunk(); } - fclose( in ); + fclose(in); ::clear(); diff --git a/Runtime/blitz3d/loader_b3d.hpp b/Runtime/blitz3d/loader_b3d.hpp index 564bd34..696f82c 100644 --- a/Runtime/blitz3d/loader_b3d.hpp +++ b/Runtime/blitz3d/loader_b3d.hpp @@ -4,9 +4,9 @@ #include "meshloader.hpp" -class Loader_B3D : public MeshLoader{ -public: - MeshModel *load( const string &f,const Transform &conv,int hint ); +class Loader_B3D : public MeshLoader { + public: + MeshModel* load(const string& f, const Transform& conv, int hint); }; #endif diff --git a/Runtime/blitz3d/loader_x.cpp b/Runtime/blitz3d/loader_x.cpp index d4647e0..1c75f06 100644 --- a/Runtime/blitz3d/loader_x.cpp +++ b/Runtime/blitz3d/loader_x.cpp @@ -1,102 +1,114 @@ -#include "std.hpp" #include "loader_x.hpp" -#include "meshmodel.hpp" #include "animation.hpp" +#include "meshmodel.hpp" #include "pivot.hpp" +#include "std.hpp" #include //#include -#include "../gxruntime/GraphicsRuntime.hpp" #include #include #include #include +#include "../gxruntime/GraphicsRuntime.hpp" -extern gxRuntime *gx_runtime; -static map frames_map; -static int anim_len; +extern gxRuntime* gx_runtime; +static map frames_map; +static int anim_len; -static bool conv,flip_tris; +static bool conv, flip_tris; static Transform conv_tform; -static bool collapse,animonly; +static bool collapse, animonly; -static void parseAnimKey(ID3DXFileData *fileData, MeshModel *e) { +static void parseAnimKey(ID3DXFileData* fileData, MeshModel* e) +{ + DWORD sz; + int* data; + if (fileData->GetData(0, &sz, (void**)&data) < 0) + return; - DWORD sz;int *data; - if( fileData->GetData( 0,&sz,(void**)&data )<0 ) return; - - int type=*data++; - int cnt=*data++; - Animation anim=e->getAnimation(); - for( int k=0;kanim_len ) anim_len=time; - switch( type ){ + int type = *data++; + int cnt = *data++; + Animation anim = e->getAnimation(); + for (int k = 0; k < cnt; ++k) { + int time = *data++; + int n = *data++; + if (time > anim_len) + anim_len = time; + switch (type) { case 0: - if( n==4 ){ - Quat rot=*(Quat*)data; - if( conv ){ - if( fabs(rot.w)<1-FLT_EPSILON ){ + if (n == 4) { + Quat rot = *(Quat*)data; + if (conv) { + if (fabs(rot.w) < 1 - FLT_EPSILON) { rot.normalize(); //quat-to-axis/angle - float half=acosf( rot.w ); - if( flip_tris ) half=-half; - rot=Quat( cosf( half ),(conv_tform.m*rot.v).normalized()*sinf( half ) ); - }else rot=Quat(); + float half = acosf(rot.w); + if (flip_tris) + half = -half; + rot = Quat(cosf(half), (conv_tform.m * rot.v).normalized() * sinf(half)); + } else + rot = Quat(); } - anim.setRotationKey( time,rot ); + anim.setRotationKey(time, rot); } break; case 1: - if( n==3 ){ - Vector scl=*(Vector*)data; - if( conv ) scl=conv_tform.m * scl; - scl.x=fabs(scl.x);scl.y=fabs(scl.y);scl.z=fabs(scl.z); - anim.setScaleKey( time,scl ); + if (n == 3) { + Vector scl = *(Vector*)data; + if (conv) + scl = conv_tform.m * scl; + scl.x = fabs(scl.x); + scl.y = fabs(scl.y); + scl.z = fabs(scl.z); + anim.setScaleKey(time, scl); } break; case 2: - if( n==3 ){ - Vector pos=*(Vector*)data; - if( conv ) pos=conv_tform*pos; - anim.setPositionKey( time,pos ); + if (n == 3) { + Vector pos = *(Vector*)data; + if (conv) + pos = conv_tform * pos; + anim.setPositionKey(time, pos); } break; } - data+=n; + data += n; } - e->setAnimation( anim ); + e->setAnimation(anim); } -static void parseAnim(ID3DXFileData *fileData) { - const GUID *guid; - IDirectXFileObject *childObj; - IDirectXFileData *childData; - IDirectXFileDataReference *childRef; - MeshModel *frame=0; +static void parseAnim(ID3DXFileData* fileData) +{ + const GUID* guid; + IDirectXFileObject* childObj; + IDirectXFileData* childData; + IDirectXFileDataReference* childRef; + MeshModel* frame = 0; //find the frame reference - for( ;fileData->GetNextObject( &childObj )>=0;childObj->Release() ){ - if( childObj->QueryInterface( IID_IDirectXFileDataReference,(void**)&childRef )>=0 ){ - if( childRef->Resolve( &childData )>=0 ){ - if( childData->GetType( &guid )>=0 ){ - if( *guid==TID_D3DRMFrame ){ - char name[80];DWORD len=80; - if( childData->GetName( name,&len )>=0 ){ - map::iterator it=frames_map.find( name ); - if( it!=frames_map.end() ) frame=it->second; + for (; fileData->GetNextObject(&childObj) >= 0; childObj->Release()) { + if (childObj->QueryInterface(IID_IDirectXFileDataReference, (void**)&childRef) >= 0) { + if (childRef->Resolve(&childData) >= 0) { + if (childData->GetType(&guid) >= 0) { + if (*guid == TID_D3DRMFrame) { + char name[80]; + DWORD len = 80; + if (childData->GetName(name, &len) >= 0) { + map::iterator it = frames_map.find(name); + if (it != frames_map.end()) + frame = it->second; } } } childData->Release(); } childRef->Release(); - }else if( frame && childObj->QueryInterface( IID_IDirectXFileData,(void**)&childData )>=0 ){ - if( childData->GetType( &guid )>=0 ){ - if( *guid==TID_D3DRMAnimationKey ){ - parseAnimKey( childData,frame ); + } else if (frame && childObj->QueryInterface(IID_IDirectXFileData, (void**)&childData) >= 0) { + if (childData->GetType(&guid) >= 0) { + if (*guid == TID_D3DRMAnimationKey) { + parseAnimKey(childData, frame); } } childData->Release(); @@ -104,43 +116,51 @@ static void parseAnim(ID3DXFileData *fileData) { } } -static void parseAnimSet( IDirectXFileData *fileData ){ - const GUID *guid; - IDirectXFileObject *childObj; - IDirectXFileData *childData; +static void parseAnimSet(IDirectXFileData* fileData) +{ + const GUID* guid; + IDirectXFileObject* childObj; + IDirectXFileData* childData; - for( ;fileData->GetNextObject( &childObj )>=0;childObj->Release() ){ - if( childObj->QueryInterface( IID_IDirectXFileData,(void**)&childData )<0 ) continue; - if( childData->GetType( &guid )>=0 ){ - if( *guid==TID_D3DRMAnimation ){ - parseAnim( childData ); + for (; fileData->GetNextObject(&childObj) >= 0; childObj->Release()) { + if (childObj->QueryInterface(IID_IDirectXFileData, (void**)&childData) < 0) + continue; + if (childData->GetType(&guid) >= 0) { + if (*guid == TID_D3DRMAnimation) { + parseAnim(childData); } } childData->Release(); } } -static Brush parseMaterial( IDirectXFileData *fileData ){ - const GUID *guid; - IDirectXFileObject *childObj; - IDirectXFileData *childData; +static Brush parseMaterial(IDirectXFileData* fileData) +{ + const GUID* guid; + IDirectXFileObject* childObj; + IDirectXFileData* childData; Brush brush; - DWORD sz;float *data; - if( fileData->GetData( 0,&sz,(void**)&data )<0 ) return brush; + DWORD sz; + float* data; + if (fileData->GetData(0, &sz, (void**)&data) < 0) + return brush; - brush.setColor( Vector( data[0],data[1],data[2] ) ); - if( data[3] ) brush.setAlpha( data[3] ); + brush.setColor(Vector(data[0], data[1], data[2])); + if (data[3]) + brush.setAlpha(data[3]); - for( ;fileData->GetNextObject( &childObj )>=0;childObj->Release() ){ - if( childObj->QueryInterface( IID_IDirectXFileData,(void**)&childData )<0 ) continue; - if( childData->GetType( &guid )>=0 ){ - if( *guid==TID_D3DRMTextureFilename ){ - DWORD sz;char **data; - if( childData->GetData( 0,&sz,(void**)&data )>=0 ){ - brush.setTexture( 0,Texture( *data,0 ),0 ); - brush.setColor( Vector( 1,1,1 ) ); + for (; fileData->GetNextObject(&childObj) >= 0; childObj->Release()) { + if (childObj->QueryInterface(IID_IDirectXFileData, (void**)&childData) < 0) + continue; + if (childData->GetType(&guid) >= 0) { + if (*guid == TID_D3DRMTextureFilename) { + DWORD sz; + char** data; + if (childData->GetData(0, &sz, (void**)&data) >= 0) { + brush.setTexture(0, Texture(*data, 0), 0); + brush.setColor(Vector(1, 1, 1)); } } } @@ -150,27 +170,27 @@ static Brush parseMaterial( IDirectXFileData *fileData ){ return brush; } -static void parseMaterialList( IDirectXFileData *fileData,vector &mats ){ - - const GUID *guid; - IDirectXFileObject *childObj; - IDirectXFileData *childData; - IDirectXFileDataReference *childRef; +static void parseMaterialList(IDirectXFileData* fileData, vector& mats) +{ + const GUID* guid; + IDirectXFileObject* childObj; + IDirectXFileData* childData; + IDirectXFileDataReference* childRef; //iterate through child objects... - for( ;fileData->GetNextObject( &childObj )>=0;childObj->Release() ){ - if( childObj->QueryInterface( IID_IDirectXFileData,(void**)&childData )>=0 ){ - if( childData->GetType( &guid )>=0 ){ - if( *guid==TID_D3DRMMaterial ){ - mats.push_back( parseMaterial( childData ) ); + for (; fileData->GetNextObject(&childObj) >= 0; childObj->Release()) { + if (childObj->QueryInterface(IID_IDirectXFileData, (void**)&childData) >= 0) { + if (childData->GetType(&guid) >= 0) { + if (*guid == TID_D3DRMMaterial) { + mats.push_back(parseMaterial(childData)); } - } + } childData->Release(); - }else if( childObj->QueryInterface( IID_IDirectXFileDataReference,(void**)&childRef )>=0 ){ - if( childRef->Resolve( &childData )>=0 ){ - if( childData->GetType( &guid )>=0 ){ - if( *guid==TID_D3DRMMaterial ){ - mats.push_back( parseMaterial( childData ) ); + } else if (childObj->QueryInterface(IID_IDirectXFileDataReference, (void**)&childRef) >= 0) { + if (childRef->Resolve(&childData) >= 0) { + if (childData->GetType(&guid) >= 0) { + if (*guid == TID_D3DRMMaterial) { + mats.push_back(parseMaterial(childData)); } } childData->Release(); @@ -180,19 +200,21 @@ static void parseMaterialList( IDirectXFileData *fileData,vector &mats ){ } } -struct FaceX{ - int *data,mat_index; - FaceX( int *d ):data(d),mat_index(0){} +struct FaceX { + int *data, mat_index; + FaceX(int* d) : data(d), mat_index(0) {} }; -static void parseMesh( IDirectXFileData *fileData,MeshModel *mesh ){ +static void parseMesh(IDirectXFileData* fileData, MeshModel* mesh) +{ + const GUID* guid; + IDirectXFileObject* childObj; + IDirectXFileData* childData; - const GUID *guid; - IDirectXFileObject *childObj; - IDirectXFileData *childData; - - DWORD sz;int *data; - if( fileData->GetData( 0,&sz,(void**)&data )<0 ) return; + DWORD sz; + int* data; + if (fileData->GetData(0, &sz, (void**)&data) < 0) + return; //stuff... vector faces; @@ -201,131 +223,144 @@ static void parseMesh( IDirectXFileData *fileData,MeshModel *mesh ){ MeshLoader::beginMesh(); //setup vertices - int num_verts=*data++; + int num_verts = *data++; int k; - for( k=0;kGetNextObject( &childObj )>=0;childObj->Release() ){ - if( childObj->QueryInterface( IID_IDirectXFileData,(void**)&childData )<0 ) continue; - if( childData->GetType( &guid )>=0 ){ - DWORD sz;int *data; - if( childData->GetData( 0,&sz,(void**)&data )>=0 ){ - if( *guid==TID_D3DRMMeshMaterialList ){ - int num_mats=*data++; - int num_faces=*data++; - for( int k=0;kGetNextObject(&childObj) >= 0; childObj->Release()) { + if (childObj->QueryInterface(IID_IDirectXFileData, (void**)&childData) < 0) + continue; + if (childData->GetType(&guid) >= 0) { + DWORD sz; + int* data; + if (childData->GetData(0, &sz, (void**)&data) >= 0) { + if (*guid == TID_D3DRMMeshMaterialList) { + int num_mats = *data++; + int num_faces = *data++; + for (int k = 0; k < num_faces; ++k) { + faces[k].mat_index = *data++; } - parseMaterialList( childData,mats ); - }else if( *guid==TID_D3DRMMeshTextureCoords ){ - int num_coords=*data++; - if( num_coords==num_verts ){ - float *coords=(float*)data; - for( int k=0;kRelease(); } - if( !mats.size() ) mats.push_back( Brush() ); + if (!mats.size()) + mats.push_back(Brush()); - for( size_t k=0;kupdateNormals(); + if (!normals) + mesh->updateNormals(); } -static MeshModel *parseFrame( IDirectXFileData *fileData ){ +static MeshModel* parseFrame(IDirectXFileData* fileData) +{ + MeshModel* e = new MeshModel(); - MeshModel *e=new MeshModel(); + const GUID* guid; + IDirectXFileObject* childObj; + IDirectXFileData* childData; - const GUID *guid; - IDirectXFileObject *childObj; - IDirectXFileData *childData; + char name[80]; + DWORD len = 80; + if (fileData->GetName(name, &len) < 0) + return e; - char name[80];DWORD len=80; - if( fileData->GetName( name,&len )<0 ) return e; - - e->SetName( name ); - frames_map[name]=e; + e->SetName(name); + frames_map[name] = e; //iterate through child objects... - for( ;fileData->GetNextObject( &childObj )>=0;childObj->Release() ){ - if( childObj->QueryInterface( IID_IDirectXFileData,(void**)&childData )<0 ) continue; - if( childData->GetType( &guid )>=0 ){ - if( *guid==TID_D3DRMFrameTransformMatrix ){ - DWORD size;D3DMATRIX *data; - if( childData->GetData( 0,&size,(void**)&data )>=0 ){ - Transform tform=Transform( Matrix( - Vector( data->_11,data->_12,data->_13 ), - Vector( data->_21,data->_22,data->_23 ), - Vector( data->_31,data->_32,data->_33 ) ), - Vector( data->_41,data->_42,data->_43 ) ); - if( conv ) tform=conv_tform * tform * -conv_tform; - e->SetLocalTransform( tform ); + for (; fileData->GetNextObject(&childObj) >= 0; childObj->Release()) { + if (childObj->QueryInterface(IID_IDirectXFileData, (void**)&childData) < 0) + continue; + if (childData->GetType(&guid) >= 0) { + if (*guid == TID_D3DRMFrameTransformMatrix) { + DWORD size; + D3DMATRIX* data; + if (childData->GetData(0, &size, (void**)&data) >= 0) { + Transform tform = Transform(Matrix(Vector(data->_11, data->_12, data->_13), + Vector(data->_21, data->_22, data->_23), + Vector(data->_31, data->_32, data->_33)), + Vector(data->_41, data->_42, data->_43)); + if (conv) + tform = conv_tform * tform * -conv_tform; + e->SetLocalTransform(tform); } - }else if( *guid==TID_D3DRMMesh ){ - if( !animonly ) parseMesh( childData,e ); - }else if( *guid==TID_D3DRMFrame ){ - MeshModel *t=parseFrame( childData ); - t->SetParent( e ); + } else if (*guid == TID_D3DRMMesh) { + if (!animonly) + parseMesh(childData, e); + } else if (*guid == TID_D3DRMFrame) { + MeshModel* t = parseFrame(childData); + t->SetParent(e); } } childData->Release(); @@ -333,39 +368,45 @@ static MeshModel *parseFrame( IDirectXFileData *fileData ){ return e; } -static MeshModel *parseFile( const string &file ){ +static MeshModel* parseFile(const string& file) +{ + const GUID* guid; + IDirectXFile* xfile; + IDirectXFileData* fileData; + IDirectXFileEnumObject* enumObj; - const GUID *guid; - IDirectXFile *xfile; - IDirectXFileData *fileData; - IDirectXFileEnumObject *enumObj; + if (DirectXFileCreate(&xfile) < 0) + return 0; - if( DirectXFileCreate( &xfile )<0 ) return 0; - - if( xfile->RegisterTemplates( (VOID*)D3DRM_XTEMPLATES,D3DRM_XTEMPLATE_BYTES )<0 ){ - xfile->Release();return 0; + if (xfile->RegisterTemplates((VOID*)D3DRM_XTEMPLATES, D3DRM_XTEMPLATE_BYTES) < 0) { + xfile->Release(); + return 0; } - if( xfile->CreateEnumObject( (void*)file.c_str(),DXFILELOAD_FROMFILE,&enumObj )<0 ){ - xfile->Release();return 0; + if (xfile->CreateEnumObject((void*)file.c_str(), DXFILELOAD_FROMFILE, &enumObj) < 0) { + xfile->Release(); + return 0; } - anim_len=0; - MeshModel *e=new MeshModel(); - for( ;enumObj->GetNextDataObject( &fileData )>=0;fileData->Release() ){ - if( fileData->GetType( &guid )<0 ) continue; + anim_len = 0; + MeshModel* e = new MeshModel(); + for (; enumObj->GetNextDataObject(&fileData) >= 0; fileData->Release()) { + if (fileData->GetType(&guid) < 0) + continue; - if( *guid==TID_D3DRMMesh ){ - if( !animonly) parseMesh( fileData,e ); - }else if( *guid==TID_D3DRMFrame ){ - MeshModel *t=parseFrame( fileData ); - t->SetParent( e ); - }else if( *guid==TID_D3DRMAnimationSet ){ - if( !collapse ) parseAnimSet( fileData ); + if (*guid == TID_D3DRMMesh) { + if (!animonly) + parseMesh(fileData, e); + } else if (*guid == TID_D3DRMFrame) { + MeshModel* t = parseFrame(fileData); + t->SetParent(e); + } else if (*guid == TID_D3DRMAnimationSet) { + if (!collapse) + parseAnimSet(fileData); } - } + } - if( !collapse ){ - e->setAnimator( new Animator( e,anim_len ) ); + if (!collapse) { + e->setAnimator(new Animator(e, anim_len)); } enumObj->Release(); @@ -373,17 +414,19 @@ static MeshModel *parseFile( const string &file ){ return e; } -MeshModel *Loader_X::load( const string &filename,const Transform &t,int hint ){ - conv_tform=t; - conv=flip_tris=false; - if( conv_tform!=Transform() ){ - conv=true; - if( conv_tform.m.i.cross(conv_tform.m.j).dot(conv_tform.m.k)<0 ) flip_tris=true; +MeshModel* Loader_X::load(const string& filename, const Transform& t, int hint) +{ + conv_tform = t; + conv = flip_tris = false; + if (conv_tform != Transform()) { + conv = true; + if (conv_tform.m.i.cross(conv_tform.m.j).dot(conv_tform.m.k) < 0) + flip_tris = true; } - collapse=!!(hint&MeshLoader::HINT_COLLAPSE); - animonly=!!(hint&MeshLoader::HINT_ANIMONLY); + collapse = !!(hint & MeshLoader::HINT_COLLAPSE); + animonly = !!(hint & MeshLoader::HINT_ANIMONLY); - MeshModel *e=parseFile( filename ); + MeshModel* e = parseFile(filename); frames_map.clear(); return e; } diff --git a/Runtime/blitz3d/loader_x.hpp b/Runtime/blitz3d/loader_x.hpp index 9e8dfed..e93f196 100644 --- a/Runtime/blitz3d/loader_x.hpp +++ b/Runtime/blitz3d/loader_x.hpp @@ -4,9 +4,9 @@ #include "meshloader.hpp" -class Loader_X : public MeshLoader{ -public: - MeshModel *load( const string &f,const Transform &conv,int hint ); +class Loader_X : public MeshLoader { + public: + MeshModel* load(const string& f, const Transform& conv, int hint); }; #endif diff --git a/Runtime/blitz3d/md2model.cpp b/Runtime/blitz3d/md2model.cpp index a73b275..b0159bb 100644 --- a/Runtime/blitz3d/md2model.cpp +++ b/Runtime/blitz3d/md2model.cpp @@ -1,129 +1,145 @@ -#include "std.hpp" -#include "md2rep.hpp" #include "md2model.hpp" +#include "md2rep.hpp" +#include "std.hpp" -struct MD2Model::Rep : public MD2Rep{ +struct MD2Model::Rep : public MD2Rep { int ref_cnt; - Rep( const string &f):MD2Rep( f ), - ref_cnt(1){ - } + Rep(const string& f) : MD2Rep(f), ref_cnt(1) {} }; -MD2Model::MD2Model( const string &f ): -rep( new Rep( f ) ), -anim_mode(0),anim_time(0), -render_a(0),render_b(0),render_t(0),trans_verts(0){ -} +MD2Model::MD2Model(const string& f) + : rep(new Rep(f)), anim_mode(0), anim_time(0), render_a(0), render_b(0), render_t(0), trans_verts(0) +{} -MD2Model::MD2Model( const MD2Model &t ): -Model(t),rep( t.rep ), -anim_mode(0),anim_time(0), -render_a(0),render_b(0),render_t(0),trans_verts(0){ +MD2Model::MD2Model(const MD2Model& t) + : Model(t), rep(t.rep), anim_mode(0), anim_time(0), render_a(0), render_b(0), render_t(0), trans_verts(0) +{ ++rep->ref_cnt; } -MD2Model::~MD2Model(){ - if( !--rep->ref_cnt ) delete rep; - if( trans_verts ) delete[] trans_verts; +MD2Model::~MD2Model() +{ + if (!--rep->ref_cnt) + delete rep; + if (trans_verts) + delete[] trans_verts; } -void MD2Model::startMD2Anim( int first,int last,int mode,float speed,float trans ){ +void MD2Model::startMD2Anim(int first, int last, int mode, float speed, float trans) +{ + if (last < first) + std::swap(first, last); - if( last= rep->numFrames()) + first = rep->numFrames() - 1; - if( first<0 ) first=0; - else if( first>=rep->numFrames() ) first=rep->numFrames()-1; + if (last < 0) + last = 0; + else if (last >= rep->numFrames()) + last = rep->numFrames() - 1; - if( last<0 ) last=0; - else if( last>=rep->numFrames() ) last=rep->numFrames()-1; + if (trans > 0) { + if (!trans_verts) + trans_verts = new MD2Rep::Vert[rep->numVertices()]; - if( trans>0 ){ - if( !trans_verts ) trans_verts=new MD2Rep::Vert[rep->numVertices()]; - - if( anim_mode & 0x8000 ) rep->render( trans_verts,anim_time,trans_time ); - else rep->render( trans_verts,render_a,render_b,render_t ); - trans_speed=1.0f/trans; - trans_time=0; - mode|=0x8000; + if (anim_mode & 0x8000) + rep->render(trans_verts, anim_time, trans_time); + else + rep->render(trans_verts, render_a, render_b, render_t); + trans_speed = 1.0f / trans; + trans_time = 0; + mode |= 0x8000; } - anim_first=first; - anim_last=last; - anim_len=last-first; - anim_speed=speed; - anim_time=((mode&0x7fff)==Animator::ANIM_MODE_LOOP || anim_speed>=0) ? anim_first : anim_last; - anim_mode=mode; + anim_first = first; + anim_last = last; + anim_len = last - first; + anim_speed = speed; + anim_time = ((mode & 0x7fff) == Animator::ANIM_MODE_LOOP || anim_speed >= 0) ? anim_first : anim_last; + anim_mode = mode; - if( !anim_speed || !anim_len ){ - render_a=render_b=anim_time; - render_t=0; - anim_mode&=0x8000; + if (!anim_speed || !anim_len) { + render_a = render_b = anim_time; + render_t = 0; + anim_mode &= 0x8000; } } -void MD2Model::animate( float e ){ - Model::animate( e ); - if( !anim_mode ) return; - if( anim_mode & 0x8000 ){ - trans_time+=trans_speed; - if( trans_time<1 ) return; - anim_mode&=~0x8000; - if( !anim_mode ) return; +void MD2Model::animate(float e) +{ + Model::animate(e); + if (!anim_mode) + return; + if (anim_mode & 0x8000) { + trans_time += trans_speed; + if (trans_time < 1) + return; + anim_mode &= ~0x8000; + if (!anim_mode) + return; } - anim_time=anim_time+anim_speed * e; - if( anim_time=anim_last ){ - switch( anim_mode ){ + } else if (anim_time >= anim_last) { + switch (anim_mode) { case Animator::ANIM_MODE_LOOP: - anim_time-=anim_len; + anim_time -= anim_len; break; case Animator::ANIM_MODE_PINGPONG: - anim_time=anim_last-(anim_time-anim_last); - anim_speed=-anim_speed; + anim_time = anim_last - (anim_time - anim_last); + anim_speed = -anim_speed; break; default: - anim_time=anim_last; - anim_mode=0; + anim_time = anim_last; + anim_mode = 0; break; } } - render_a=floor(anim_time);render_b=render_a+1; - if( anim_mode==Animator::ANIM_MODE_LOOP && render_b==anim_last ) render_b=anim_first; - render_t=anim_time-render_a; + render_a = floor(anim_time); + render_b = render_a + 1; + if (anim_mode == Animator::ANIM_MODE_LOOP && render_b == anim_last) + render_b = anim_first; + render_t = anim_time - render_a; } -bool MD2Model::render( const RenderContext &rc ){ +bool MD2Model::render(const RenderContext& rc) +{ static Frustum f; - new( &f ) Frustum( rc.getWorldFrustum(),-getRenderTform() ); - if( !f.cull( rep->getBox() ) ) return false; + new (&f) Frustum(rc.getWorldFrustum(), -getRenderTform()); + if (!f.cull(rep->getBox())) + return false; - if( anim_mode & 0x8000 ){ - rep->render( this,trans_verts,anim_time,trans_time ); - }else{ - rep->render( this,render_a,render_b,render_t ); + if (anim_mode & 0x8000) { + rep->render(this, trans_verts, anim_time, trans_time); + } else { + rep->render(this, render_a, render_b, render_t); } return false; } -int MD2Model::getMD2AnimLength()const{ +int MD2Model::getMD2AnimLength() const +{ return rep->numFrames(); } -bool MD2Model::getValid()const{ - return rep->numFrames()>0; +bool MD2Model::getValid() const +{ + return rep->numFrames() > 0; } diff --git a/Runtime/blitz3d/md2model.hpp b/Runtime/blitz3d/md2model.hpp index 6e5621c..90e13d1 100644 --- a/Runtime/blitz3d/md2model.hpp +++ b/Runtime/blitz3d/md2model.hpp @@ -2,50 +2,62 @@ #ifndef MD2MODEL_H #define MD2MODEL_H -#include "model.hpp" #include "md2rep.hpp" +#include "model.hpp" -class MD2Model : public Model{ -public: - MD2Model( const string &filename ); - MD2Model( const MD2Model &t ); +class MD2Model : public Model { + public: + MD2Model(const string& filename); + MD2Model(const MD2Model& t); ~MD2Model(); //Entity interface - Entity *clone(){ return new MD2Model( *this ); } - MD2Model *getMD2Model(){ return this; } + Entity* clone() + { + return new MD2Model(*this); + } + MD2Model* getMD2Model() + { + return this; + } //Object interface - void animate( float elapsed ); + void animate(float elapsed); //Model interface - bool render( const RenderContext &rc ); + bool render(const RenderContext& rc); //MD2 interface - void startMD2Anim( int first,int last,int mode,float speed,float trans ); + void startMD2Anim(int first, int last, int mode, float speed, float trans); - int getMD2AnimLength()const; - bool getMD2Animating()const{ return !!anim_mode; } - float getMD2AnimTime()const{ return anim_time; } + int getMD2AnimLength() const; + bool getMD2Animating() const + { + return !!anim_mode; + } + float getMD2AnimTime() const + { + return anim_time; + } - bool getValid()const; + bool getValid() const; -private: + private: struct Rep; - Rep *rep; + Rep* rep; - int anim_mode; - float anim_time,anim_speed; - int anim_first,anim_last,anim_len; + int anim_mode; + float anim_time, anim_speed; + int anim_first, anim_last, anim_len; float render_t; - int render_a,render_b; + int render_a, render_b; - float trans_time,trans_speed; - MD2Rep::Vert *trans_verts; + float trans_time, trans_speed; + MD2Rep::Vert* trans_verts; //Unimplemented - MD2Model &operator=( const MD2Model & ); + MD2Model& operator=(const MD2Model&); }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/md2model_bak.cpp b/Runtime/blitz3d/md2model_bak.cpp index d2f4a61..32d377e 100644 --- a/Runtime/blitz3d/md2model_bak.cpp +++ b/Runtime/blitz3d/md2model_bak.cpp @@ -1,473 +1,504 @@ -#include "std.h" #include "md2model.h" #include "md2norms.h" +#include "std.h" -static Vector *normals; -static float white[]={1,1,1}; +static Vector* normals; +static float white[] = {1, 1, 1}; -extern gxRuntime *gx_runtime; -extern gxGraphics *gx_graphics; +extern gxRuntime* gx_runtime; +extern gxGraphics* gx_graphics; -struct MD2Model::Rep{ - - struct md2_header{ - int magic; - int version; - int skinWidth; - int skinHeight; - int frameSize; - int numSkins; - int numVertices; - int numTexCoords; - int numTriangles; - int numGlCommands; - int numFrames; - int offsetSkins; - int offsetTexCoords; - int offsetTriangles; - int offsetFrames; - int offsetGlCommands; +struct MD2Model::Rep { + struct md2_header { + int magic; + int version; + int skinWidth; + int skinHeight; + int frameSize; + int numSkins; + int numVertices; + int numTexCoords; + int numTriangles; + int numGlCommands; + int numFrames; + int offsetSkins; + int offsetTexCoords; + int offsetTriangles; + int offsetFrames; + int offsetGlCommands; int offsetEnd; }; - - struct md2_vertex{ - unsigned char x,y,z,n; + + struct md2_vertex { + unsigned char x, y, z, n; }; - struct md2_texcoord{ - unsigned short s,t; + struct md2_texcoord { + unsigned short s, t; }; - struct md2_triangle{ - unsigned short verts[3],tex_coords[3]; + struct md2_triangle { + unsigned short verts[3], tex_coords[3]; }; - struct Frame{ - Vector scale,trans; + struct Frame { + Vector scale, trans; vector verts; }; - struct TexCoords{ - float u,v; + struct TexCoords { + float u, v; }; int ref_cnt; - int num_verts,num_frames; + int num_verts, num_frames; - vector frames; + vector frames; vector tex_coords; - gxMesh *mesh; + gxMesh* mesh; Box box; - ModelModel::Rep( const string &file ): - ref_cnt(1),mesh(0){ - + ModelModel::Rep(const string& file) : ref_cnt(1), mesh(0) + { filebuf in; - Header header; + Header header; - if( !in.open( file.c_str(),ios_base::in|ios_base::binary ) ) return; - if( in.sgetn( (char*)&header,sizeof(header) )!=sizeof(header) ) return; - if( header.magic!='2PDI' || header.version!=8 ) return; + if (!in.open(file.c_str(), ios_base::in | ios_base::binary)) + return; + if (in.sgetn((char*)&header, sizeof(header)) != sizeof(header)) + return; + if (header.magic != '2PDI' || header.version != 8) + return; //read tex coords - in.pubseekpos( header.offsetTexCoords ); - TexCoord *coords=d_new TexCoord[header.numTexCoords]; - in.sgetn( (char*)coords,header.numTexCoords*sizeof(TexCoord) ); + in.pubseekpos(header.offsetTexCoords); + TexCoord* coords = d_new TexCoord[header.numTexCoords]; + in.sgetn((char*)coords, header.numTexCoords * sizeof(TexCoord)); - vector verts; - map info_map; + vector verts; + map info_map; //build triangles vector triangles; - in.pubseekpos( header.offsetTriangles ); - triangles.resize( header.numTriangles ); - for( k=0;k::iterator it=info_map.find( i ); - if( it==info_map.end() ){ - info_map[i]=triangles[k].v[j]=verts.size(); - verts.push_back( i ); - }else{ - triangles[k].v[j]=it->second; + in.pubseekpos(header.offsetTriangles); + triangles.resize(header.numTriangles); + for (k = 0; k < triangles.size(); ++k) { + unsigned short v[3], t[3]; + in.sgetn((char*)v, 6); + in.sgetn((char*)t, 6); + for (int j = 0; j < 3; ++j) { + unsigned char tu = coords[t[j]].s * 256.0f / header.skinWidth; + unsigned char tv = coords[t[j]].t * 256.0f / header.skinHeight; + VertInfo i(v[j], tu, tv); + map::iterator it = info_map.find(i); + if (it == info_map.end()) { + info_map[i] = triangles[k].v[j] = verts.size(); + verts.push_back(i); + } else { + triangles[k].v[j] = it->second; } } } delete coords; //load frames - string tt="MD2 Frames:"+itoa( header.numFrames ); - gx_runtime->debugLog( tt.c_str() ); + string tt = "MD2 Frames:" + itoa(header.numFrames); + gx_runtime->debugLog(tt.c_str()); - in.pubseekpos( header.offsetFrames ); - frames.resize( header.numFrames ); - MD2Vertex *md2_verts=d_new MD2Vertex[header.numVertices]; + in.pubseekpos(header.offsetFrames); + frames.resize(header.numFrames); + MD2Vertex* md2_verts = d_new MD2Vertex[header.numVertices]; - for( k=0;kcreateMesh( verts.size(),triangles.size(),0 ); + mesh = gx_graphics->createMesh(verts.size(), triangles.size(), 0); mesh->lock(); - for( k=0;ksetTriangle( k,t.v[0],t.v[2],t.v[1] ); + for (k = 0; k < triangles.size(); ++k) { + const Triangle& t = triangles[k]; + mesh->setTriangle(k, t.v[0], t.v[2], t.v[1]); } mesh->unlock(); //calculate bounding box. - for( k=0;k vertices; + }; + + struct TexCoord { + short s, t; + }; + + struct VertInfo { + unsigned short index; + unsigned char u, v; + VertInfo(unsigned short i, char u, char v) : index(i), u(u), v(v) {} + bool operator<(const VertInfo& t) const + { + if (index < t.index) + return true; + if (t.index < index) + return false; + if (u < t.u) + return true; + if (t.u < u) + return false; + return v < t.v; + } + }; + +#pragma pack(pop) + + int ref_cnt; + Header header; + vector frames; + gxMesh* mesh; + Box box; + + Rep(const string& file); + ~Rep(); + + void render(MD2Model * model, float render_t, int render_a, int render_b); }; - struct MD2Vertex{ - unsigned char x,y,z,n; - }; - - struct Vertex{ - unsigned char x,y,z,u,v,n; - }; - - struct Triangle{ - unsigned short v[3]; //index into vertices - }; - - struct Frame{ - Vector scale; - Vector trans; - char name[16]; - vector vertices; - }; - - struct TexCoord{ - short s,t; - }; - - struct VertInfo{ - unsigned short index; - unsigned char u,v; - VertInfo( unsigned short i,char u,char v ):index(i),u(u),v(v){ + MD2Model::Rep::Rep(const string& file) : ref_cnt(1), mesh(0) + { + filebuf in; + if (!in.open(file.c_str(), ios_base::in | ios_base::binary)) { + return; } - bool operator<( const VertInfo &t )const{ - if( index frames; - gxMesh *mesh; - Box box; + int k; - Rep( const string &file ); - ~Rep(); + //read tex coords + in.pubseekpos(header.offsetTexCoords); + TexCoord* coords = d_new TexCoord[header.numTexCoords]; + in.sgetn((char*)coords, header.numTexCoords * sizeof(TexCoord)); - void render( MD2Model *model,float render_t,int render_a,int render_b ); -}; + vector verts; + map info_map; -MD2Model::Rep::Rep( const string &file ): -ref_cnt(1),mesh(0){ + //build triangles + vector triangles; + in.pubseekpos(header.offsetTriangles); + triangles.resize(header.numTriangles); + for (k = 0; k < triangles.size(); ++k) { + unsigned short v[3], t[3]; + in.sgetn((char*)v, 6); + in.sgetn((char*)t, 6); + for (int j = 0; j < 3; ++j) { + unsigned char tu = coords[t[j]].s * 256.0f / header.skinWidth; + unsigned char tv = coords[t[j]].t * 256.0f / header.skinHeight; + VertInfo i(v[j], tu, tv); + map::iterator it = info_map.find(i); + if (it == info_map.end()) { + info_map[i] = triangles[k].v[j] = verts.size(); + verts.push_back(i); + } else { + triangles[k].v[j] = it->second; + } + } + } + delete coords; - filebuf in; - if( !in.open( file.c_str(),ios_base::in|ios_base::binary ) ){ - return; - } + //load frames + string tt = "MD2 Frames:" + itoa(header.numFrames); + gx_runtime->debugLog(tt.c_str()); - if( in.sgetn( (char*)&header,sizeof(header) )!=sizeof(header) ){ - return; - } + in.pubseekpos(header.offsetFrames); + frames.resize(header.numFrames); + MD2Vertex* md2_verts = d_new MD2Vertex[header.numVertices]; - if( header.magic!='2PDI' || header.version!=8 ){ - return; - } + for (k = 0; k < frames.size(); ++k) { + Frame& frame = frames[k]; - int k; + //read frame header; + in.sgetn((char*)&frame.scale, 12); + in.sgetn((char*)&frame.trans, 12); + in.sgetn(frame.name, 16); - //read tex coords - in.pubseekpos( header.offsetTexCoords ); - TexCoord *coords=d_new TexCoord[header.numTexCoords]; - in.sgetn( (char*)coords,header.numTexCoords*sizeof(TexCoord) ); + frame.trans = Vector(frame.trans.y, frame.trans.z, frame.trans.x); + frame.scale = Vector(frame.scale.y, frame.scale.z, frame.scale.x); - vector verts; - map info_map; + //read frame verts... + in.sgetn((char*)md2_verts, header.numVertices * 4); - //build triangles - vector triangles; - in.pubseekpos( header.offsetTriangles ); - triangles.resize( header.numTriangles ); - for( k=0;k::iterator it=info_map.find( i ); - if( it==info_map.end() ){ - info_map[i]=triangles[k].v[j]=verts.size(); - verts.push_back( i ); - }else{ - triangles[k].v[j]=it->second; + frame.vertices.resize(verts.size()); + for (int j = 0; j < verts.size(); ++j) { + Vertex& v = frame.vertices[j]; + const VertInfo& i = verts[j]; + const MD2Vertex& m = md2_verts[i.index]; + v.x = m.y; + v.y = m.z; + v.z = m.x; + v.u = i.u; + v.v = i.v; + v.n = m.n; + } + } + delete md2_verts; + + //create initial mesh + mesh = gx_graphics->createMesh(verts.size(), triangles.size(), 0); + mesh->lock(); + for (k = 0; k < triangles.size(); ++k) { + const Triangle& t = triangles[k]; + mesh->setTriangle(k, t.v[0], t.v[2], t.v[1]); + } + mesh->unlock(); + + //calculate bounding box. + for (k = 0; k < header.numFrames; ++k) { + const Frame& frame = frames[k]; + const Vector& scale = frame.scale; + const Vector& trans = frame.trans; + for (int n = 0; n < frame.vertices.size(); ++n) { + const Vertex& v = frame.vertices[n]; + box.update(Vector(v.x, v.y, v.z) * scale + trans); + } + } + + if (!normals) { + normals = (Vector*)md2norms; + for (int k = 0; k < sizeof(md2norms) / 12; ++k) { + normals[k] = Vector(normals[k].y, normals[k].z, normals[k].x); } } } - delete coords; - //load frames - string tt="MD2 Frames:"+itoa( header.numFrames ); - gx_runtime->debugLog( tt.c_str() ); + MD2Model::Rep::~Rep() + { + if (mesh) + gx_graphics->freeMesh(mesh); + } - in.pubseekpos( header.offsetFrames ); - frames.resize( header.numFrames ); - MD2Vertex *md2_verts=d_new MD2Vertex[header.numVertices]; + void MD2Model::Rep::render(MD2Model* model, float render_t, int render_a, int render_b) + { + const Frame& frame_a = frames[render_a]; + const Vector& scale_a = frame_a.scale; + const Vector& trans_a = frame_a.trans; - for( k=0;klock(); + int k; + for (k = 0; k < frame_a.vertices.size(); ++k) { + const Vertex& v_a = frame_a.vertices[k]; + const Vector& n_a = normals[v_a.n]; + Vector t_a(v_a.x * scale_a.x + trans_a.x, v_a.y * scale_a.y + trans_a.y, v_a.z * scale_a.z + trans_a.z); - //read frame header; - in.sgetn( (char*)&frame.scale,12 ); - in.sgetn( (char*)&frame.trans,12 ); - in.sgetn( frame.name,16 ); + const Vertex& v_b = frame_b.vertices[k]; + const Vector& n_b = normals[v_b.n]; + Vector t_b(v_b.x * scale_b.x + trans_b.x, v_b.y * scale_b.y + trans_b.y, v_b.z * scale_b.z + trans_b.z); - frame.trans=Vector(frame.trans.y,frame.trans.z,frame.trans.x); - frame.scale=Vector(frame.scale.y,frame.scale.z,frame.scale.x); - - //read frame verts... - in.sgetn( (char*)md2_verts,header.numVertices*4 ); - - frame.vertices.resize( verts.size() ); - for( int j=0;jsetVertex(k, &t.x, &n.x, white, tex_coords); } - } - delete md2_verts; + mesh->unlock(); - //create initial mesh - mesh=gx_graphics->createMesh( verts.size(),triangles.size(),0 ); - mesh->lock(); - for( k=0;ksetTriangle( k,t.v[0],t.v[2],t.v[1] ); + model->enqueue(mesh, 0, frame_a.vertices.size(), 0, header.numTriangles); } - mesh->unlock(); - //calculate bounding box. - for( k=0;kref_cnt; + } + + MD2Model::~MD2Model() + { + if (!--rep->ref_cnt) + delete rep; + } + + void MD2Model::startMD2Anim(int first, int last, int mode, float speed) + { + if (!speed && !mode) { + anim_mode = 0; + return; } - } - - if( !normals ){ - normals=(Vector*)md2norms; - for( int k=0;k= rep->header.numFrames) + first = rep->header.numFrames - 1; + if (last < 0) + last = 0; + else if (last >= rep->header.numFrames) + last = rep->header.numFrames - 1; + if (first == last) { + anim_mode = 0; + render_a = render_b = first; + render_t = 0; + return; } + if (last < first) + std::swap(first, last); + anim_first = first; + anim_last = last; + anim_len = last - first; + anim_speed = speed; + anim_time = speed > 0 ? first : last; + anim_mode = mode; } -} -MD2Model::Rep::~Rep(){ - if( mesh ) gx_graphics->freeMesh( mesh ); -} - -void MD2Model::Rep::render( MD2Model *model,float render_t,int render_a,int render_b ){ - - const Frame &frame_a=frames[render_a]; - const Vector &scale_a=frame_a.scale; - const Vector &trans_a=frame_a.trans; - - const Frame &frame_b=frames[render_b]; - const Vector &scale_b=frame_b.scale; - const Vector &trans_b=frame_b.trans; - - mesh->lock(); - int k; - for( k=0;ksetVertex( k,&t.x,&n.x,white,tex_coords ); - } - mesh->unlock(); - - model->enqueue( mesh,0,frame_a.vertices.size(),0,header.numTriangles ); -} - -MD2Model::MD2Model( const string &f ): -rep( d_new Rep( f ) ), -anim_mode(0),anim_time(0), -render_a(0),render_b(0),render_t(0){ -} - -MD2Model::MD2Model( const MD2Model &t ): -Model(t),rep( t.rep ), -anim_mode(0),anim_time(0), -render_a(0),render_b(0),render_t(0){ - ++rep->ref_cnt; -} - -MD2Model::~MD2Model(){ - if( !--rep->ref_cnt ) delete rep; -} - -void MD2Model::startMD2Anim( int first,int last,int mode,float speed ){ - if( !speed && !mode ){ anim_mode=0;return; } - if( first<0 ) first=0; - else if( first>=rep->header.numFrames ) first=rep->header.numFrames-1; - if( last<0 ) last=0; - else if( last>=rep->header.numFrames ) last=rep->header.numFrames-1; - if( first==last ){ anim_mode=0;render_a=render_b=first;render_t=0;return; } - if( last0 ? first : last; - anim_mode=mode; -} - -void MD2Model::animate( float e ){ - Model::animate( e ); - if( !anim_mode ) return; - anim_time=anim_time+anim_speed * e; - if( anim_time=anim_last ){ - switch( anim_mode ){ - case ANIM_MODE_LOOP: - anim_time-=anim_len; - break; - case ANIM_MODE_PINGPONG: - anim_time=anim_last-(anim_time-anim_last); - anim_speed=-anim_speed; - break; - default: - anim_time=anim_last; - anim_mode=0; - break; + void MD2Model::animate(float e) + { + Model::animate(e); + if (!anim_mode) + return; + anim_time = anim_time + anim_speed * e; + if (anim_time < anim_first) { + switch (anim_mode) { + case ANIM_MODE_LOOP: + anim_time += anim_len; + break; + case ANIM_MODE_PINGPONG: + anim_time = anim_first + (anim_first - anim_time); + anim_speed = -anim_speed; + break; + default: + anim_time = anim_first; + anim_mode = 0; + break; + } + } else if (anim_time >= anim_last) { + switch (anim_mode) { + case ANIM_MODE_LOOP: + anim_time -= anim_len; + break; + case ANIM_MODE_PINGPONG: + anim_time = anim_last - (anim_time - anim_last); + anim_speed = -anim_speed; + break; + default: + anim_time = anim_last; + anim_mode = 0; + break; + } } + render_a = floor(anim_time); + render_b = render_a + 1; + if (anim_mode == ANIM_MODE_LOOP && render_b == anim_last) + render_b = anim_first; + render_t = anim_time - render_a; } - render_a=floor(anim_time);render_b=render_a+1; - if( anim_mode==ANIM_MODE_LOOP && render_b==anim_last ) render_b=anim_first; - render_t=anim_time-render_a; -} -void MD2Model::render( const RenderContext &rc ){ - static Frustum f; - new( &f ) Frustum( rc.getWorldFrustum(),-getRenderTform() ); - if( !f.cull( rep->box ) ) return; + void MD2Model::render(const RenderContext& rc) + { + static Frustum f; + new (&f) Frustum(rc.getWorldFrustum(), -getRenderTform()); + if (!f.cull(rep->box)) + return; - rep->render( this,render_t,render_a,render_b ); -} + rep->render(this, render_t, render_a, render_b); + } -int MD2Model::getMD2AnimLength()const{ - return rep->frames.size(); -} + int MD2Model::getMD2AnimLength() const + { + return rep->frames.size(); + } -bool MD2Model::getValid()const{ - return rep->mesh!=0; -} + bool MD2Model::getValid() const + { + return rep->mesh != 0; + } diff --git a/Runtime/blitz3d/md2norms.cpp b/Runtime/blitz3d/md2norms.cpp index f81e493..5430ece 100644 --- a/Runtime/blitz3d/md2norms.cpp +++ b/Runtime/blitz3d/md2norms.cpp @@ -1,167 +1,59 @@ -#include "std.hpp" #include "md2norms.hpp" +#include "std.hpp" -float md2norms[162][3]={ -{-0.525731f, 0.000000f, 0.850651f}, -{-0.442863f, 0.238856f, 0.864188f}, -{-0.295242f, 0.000000f, 0.955423f}, -{-0.309017f, 0.500000f, 0.809017f}, -{-0.162460f, 0.262866f, 0.951056f}, -{0.000000f, 0.000000f, 1.000000f}, -{0.000000f, 0.850651f, 0.525731f}, -{-0.147621f, 0.716567f, 0.681718f}, -{0.147621f, 0.716567f, 0.681718f}, -{0.000000f, 0.525731f, 0.850651f}, -{0.309017f, 0.500000f, 0.809017f}, -{0.525731f, 0.000000f, 0.850651f}, -{0.295242f, 0.000000f, 0.955423f}, -{0.442863f, 0.238856f, 0.864188f}, -{0.162460f, 0.262866f, 0.951056f}, -{-0.681718f, 0.147621f, 0.716567f}, -{-0.809017f, 0.309017f, 0.500000f}, -{-0.587785f, 0.425325f, 0.688191f}, -{-0.850651f, 0.525731f, 0.000000f}, -{-0.864188f, 0.442863f, 0.238856f}, -{-0.716567f, 0.681718f, 0.147621f}, -{-0.688191f, 0.587785f, 0.425325f}, -{-0.500000f, 0.809017f, 0.309017f}, -{-0.238856f, 0.864188f, 0.442863f}, -{-0.425325f, 0.688191f, 0.587785f}, -{-0.716567f, 0.681718f, -0.147621f}, -{-0.500000f, 0.809017f, -0.309017f}, -{-0.525731f, 0.850651f, 0.000000f}, -{0.000000f, 0.850651f, -0.525731f}, -{-0.238856f, 0.864188f, -0.442863f}, -{0.000000f, 0.955423f, -0.295242f}, -{-0.262866f, 0.951056f, -0.162460f}, -{0.000000f, 1.000000f, 0.000000f}, -{0.000000f, 0.955423f, 0.295242f}, -{-0.262866f, 0.951056f, 0.162460f}, -{0.238856f, 0.864188f, 0.442863f}, -{0.262866f, 0.951056f, 0.162460f}, -{0.500000f, 0.809017f, 0.309017f}, -{0.238856f, 0.864188f, -0.442863f}, -{0.262866f, 0.951056f, -0.162460f}, -{0.500000f, 0.809017f, -0.309017f}, -{0.850651f, 0.525731f, 0.000000f}, -{0.716567f, 0.681718f, 0.147621f}, -{0.716567f, 0.681718f, -0.147621f}, -{0.525731f, 0.850651f, 0.000000f}, -{0.425325f, 0.688191f, 0.587785f}, -{0.864188f, 0.442863f, 0.238856f}, -{0.688191f, 0.587785f, 0.425325f}, -{0.809017f, 0.309017f, 0.500000f}, -{0.681718f, 0.147621f, 0.716567f}, -{0.587785f, 0.425325f, 0.688191f}, -{0.955423f, 0.295242f, 0.000000f}, -{1.000000f, 0.000000f, 0.000000f}, -{0.951056f, 0.162460f, 0.262866f}, -{0.850651f, -0.525731f, 0.000000f}, -{0.955423f, -0.295242f, 0.000000f}, -{0.864188f, -0.442863f, 0.238856f}, -{0.951056f, -0.162460f, 0.262866f}, -{0.809017f, -0.309017f, 0.500000f}, -{0.681718f, -0.147621f, 0.716567f}, -{0.850651f, 0.000000f, 0.525731f}, -{0.864188f, 0.442863f, -0.238856f}, -{0.809017f, 0.309017f, -0.500000f}, -{0.951056f, 0.162460f, -0.262866f}, -{0.525731f, 0.000000f, -0.850651f}, -{0.681718f, 0.147621f, -0.716567f}, -{0.681718f, -0.147621f, -0.716567f}, -{0.850651f, 0.000000f, -0.525731f}, -{0.809017f, -0.309017f, -0.500000f}, -{0.864188f, -0.442863f, -0.238856f}, -{0.951056f, -0.162460f, -0.262866f}, -{0.147621f, 0.716567f, -0.681718f}, -{0.309017f, 0.500000f, -0.809017f}, -{0.425325f, 0.688191f, -0.587785f}, -{0.442863f, 0.238856f, -0.864188f}, -{0.587785f, 0.425325f, -0.688191f}, -{0.688191f, 0.587785f, -0.425325f}, -{-0.147621f, 0.716567f, -0.681718f}, -{-0.309017f, 0.500000f, -0.809017f}, -{0.000000f, 0.525731f, -0.850651f}, -{-0.525731f, 0.000000f, -0.850651f}, -{-0.442863f, 0.238856f, -0.864188f}, -{-0.295242f, 0.000000f, -0.955423f}, -{-0.162460f, 0.262866f, -0.951056f}, -{0.000000f, 0.000000f, -1.000000f}, -{0.295242f, 0.000000f, -0.955423f}, -{0.162460f, 0.262866f, -0.951056f}, -{-0.442863f, -0.238856f, -0.864188f}, -{-0.309017f, -0.500000f, -0.809017f}, -{-0.162460f, -0.262866f, -0.951056f}, -{0.000000f, -0.850651f, -0.525731f}, -{-0.147621f, -0.716567f, -0.681718f}, -{0.147621f, -0.716567f, -0.681718f}, -{0.000000f, -0.525731f, -0.850651f}, -{0.309017f, -0.500000f, -0.809017f}, -{0.442863f, -0.238856f, -0.864188f}, -{0.162460f, -0.262866f, -0.951056f}, -{0.238856f, -0.864188f, -0.442863f}, -{0.500000f, -0.809017f, -0.309017f}, -{0.425325f, -0.688191f, -0.587785f}, -{0.716567f, -0.681718f, -0.147621f}, -{0.688191f, -0.587785f, -0.425325f}, -{0.587785f, -0.425325f, -0.688191f}, -{0.000000f, -0.955423f, -0.295242f}, -{0.000000f, -1.000000f, 0.000000f}, -{0.262866f, -0.951056f, -0.162460f}, -{0.000000f, -0.850651f, 0.525731f}, -{0.000000f, -0.955423f, 0.295242f}, -{0.238856f, -0.864188f, 0.442863f}, -{0.262866f, -0.951056f, 0.162460f}, -{0.500000f, -0.809017f, 0.309017f}, -{0.716567f, -0.681718f, 0.147621f}, -{0.525731f, -0.850651f, 0.000000f}, -{-0.238856f, -0.864188f, -0.442863f}, -{-0.500000f, -0.809017f, -0.309017f}, -{-0.262866f, -0.951056f, -0.162460f}, -{-0.850651f, -0.525731f, 0.000000f}, -{-0.716567f, -0.681718f, -0.147621f}, -{-0.716567f, -0.681718f, 0.147621f}, -{-0.525731f, -0.850651f, 0.000000f}, -{-0.500000f, -0.809017f, 0.309017f}, -{-0.238856f, -0.864188f, 0.442863f}, -{-0.262866f, -0.951056f, 0.162460f}, -{-0.864188f, -0.442863f, 0.238856f}, -{-0.809017f, -0.309017f, 0.500000f}, -{-0.688191f, -0.587785f, 0.425325f}, -{-0.681718f, -0.147621f, 0.716567f}, -{-0.442863f, -0.238856f, 0.864188f}, -{-0.587785f, -0.425325f, 0.688191f}, -{-0.309017f, -0.500000f, 0.809017f}, -{-0.147621f, -0.716567f, 0.681718f}, -{-0.425325f, -0.688191f, 0.587785f}, -{-0.162460f, -0.262866f, 0.951056f}, -{0.442863f, -0.238856f, 0.864188f}, -{0.162460f, -0.262866f, 0.951056f}, -{0.309017f, -0.500000f, 0.809017f}, -{0.147621f, -0.716567f, 0.681718f}, -{0.000000f, -0.525731f, 0.850651f}, -{0.425325f, -0.688191f, 0.587785f}, -{0.587785f, -0.425325f, 0.688191f}, -{0.688191f, -0.587785f, 0.425325f}, -{-0.955423f, 0.295242f, 0.000000f}, -{-0.951056f, 0.162460f, 0.262866f}, -{-1.000000f, 0.000000f, 0.000000f}, -{-0.850651f, 0.000000f, 0.525731f}, -{-0.955423f, -0.295242f, 0.000000f}, -{-0.951056f, -0.162460f, 0.262866f}, -{-0.864188f, 0.442863f, -0.238856f}, -{-0.951056f, 0.162460f, -0.262866f}, -{-0.809017f, 0.309017f, -0.500000f}, -{-0.864188f, -0.442863f, -0.238856f}, -{-0.951056f, -0.162460f, -0.262866f}, -{-0.809017f, -0.309017f, -0.500000f}, -{-0.681718f, 0.147621f, -0.716567f}, -{-0.681718f, -0.147621f, -0.716567f}, -{-0.850651f, 0.000000f, -0.525731f}, -{-0.688191f, 0.587785f, -0.425325f}, -{-0.587785f, 0.425325f, -0.688191f}, -{-0.425325f, 0.688191f, -0.587785f}, -{-0.425325f, -0.688191f, -0.587785f}, -{-0.587785f, -0.425325f, -0.688191f}, -{-0.688191f, -0.587785f, -0.425325f} }; +float md2norms[162][3] = { + {-0.525731f, 0.000000f, 0.850651f}, {-0.442863f, 0.238856f, 0.864188f}, {-0.295242f, 0.000000f, 0.955423f}, + {-0.309017f, 0.500000f, 0.809017f}, {-0.162460f, 0.262866f, 0.951056f}, {0.000000f, 0.000000f, 1.000000f}, + {0.000000f, 0.850651f, 0.525731f}, {-0.147621f, 0.716567f, 0.681718f}, {0.147621f, 0.716567f, 0.681718f}, + {0.000000f, 0.525731f, 0.850651f}, {0.309017f, 0.500000f, 0.809017f}, {0.525731f, 0.000000f, 0.850651f}, + {0.295242f, 0.000000f, 0.955423f}, {0.442863f, 0.238856f, 0.864188f}, {0.162460f, 0.262866f, 0.951056f}, + {-0.681718f, 0.147621f, 0.716567f}, {-0.809017f, 0.309017f, 0.500000f}, {-0.587785f, 0.425325f, 0.688191f}, + {-0.850651f, 0.525731f, 0.000000f}, {-0.864188f, 0.442863f, 0.238856f}, {-0.716567f, 0.681718f, 0.147621f}, + {-0.688191f, 0.587785f, 0.425325f}, {-0.500000f, 0.809017f, 0.309017f}, {-0.238856f, 0.864188f, 0.442863f}, + {-0.425325f, 0.688191f, 0.587785f}, {-0.716567f, 0.681718f, -0.147621f}, {-0.500000f, 0.809017f, -0.309017f}, + {-0.525731f, 0.850651f, 0.000000f}, {0.000000f, 0.850651f, -0.525731f}, {-0.238856f, 0.864188f, -0.442863f}, + {0.000000f, 0.955423f, -0.295242f}, {-0.262866f, 0.951056f, -0.162460f}, {0.000000f, 1.000000f, 0.000000f}, + {0.000000f, 0.955423f, 0.295242f}, {-0.262866f, 0.951056f, 0.162460f}, {0.238856f, 0.864188f, 0.442863f}, + {0.262866f, 0.951056f, 0.162460f}, {0.500000f, 0.809017f, 0.309017f}, {0.238856f, 0.864188f, -0.442863f}, + {0.262866f, 0.951056f, -0.162460f}, {0.500000f, 0.809017f, -0.309017f}, {0.850651f, 0.525731f, 0.000000f}, + {0.716567f, 0.681718f, 0.147621f}, {0.716567f, 0.681718f, -0.147621f}, {0.525731f, 0.850651f, 0.000000f}, + {0.425325f, 0.688191f, 0.587785f}, {0.864188f, 0.442863f, 0.238856f}, {0.688191f, 0.587785f, 0.425325f}, + {0.809017f, 0.309017f, 0.500000f}, {0.681718f, 0.147621f, 0.716567f}, {0.587785f, 0.425325f, 0.688191f}, + {0.955423f, 0.295242f, 0.000000f}, {1.000000f, 0.000000f, 0.000000f}, {0.951056f, 0.162460f, 0.262866f}, + {0.850651f, -0.525731f, 0.000000f}, {0.955423f, -0.295242f, 0.000000f}, {0.864188f, -0.442863f, 0.238856f}, + {0.951056f, -0.162460f, 0.262866f}, {0.809017f, -0.309017f, 0.500000f}, {0.681718f, -0.147621f, 0.716567f}, + {0.850651f, 0.000000f, 0.525731f}, {0.864188f, 0.442863f, -0.238856f}, {0.809017f, 0.309017f, -0.500000f}, + {0.951056f, 0.162460f, -0.262866f}, {0.525731f, 0.000000f, -0.850651f}, {0.681718f, 0.147621f, -0.716567f}, + {0.681718f, -0.147621f, -0.716567f}, {0.850651f, 0.000000f, -0.525731f}, {0.809017f, -0.309017f, -0.500000f}, + {0.864188f, -0.442863f, -0.238856f}, {0.951056f, -0.162460f, -0.262866f}, {0.147621f, 0.716567f, -0.681718f}, + {0.309017f, 0.500000f, -0.809017f}, {0.425325f, 0.688191f, -0.587785f}, {0.442863f, 0.238856f, -0.864188f}, + {0.587785f, 0.425325f, -0.688191f}, {0.688191f, 0.587785f, -0.425325f}, {-0.147621f, 0.716567f, -0.681718f}, + {-0.309017f, 0.500000f, -0.809017f}, {0.000000f, 0.525731f, -0.850651f}, {-0.525731f, 0.000000f, -0.850651f}, + {-0.442863f, 0.238856f, -0.864188f}, {-0.295242f, 0.000000f, -0.955423f}, {-0.162460f, 0.262866f, -0.951056f}, + {0.000000f, 0.000000f, -1.000000f}, {0.295242f, 0.000000f, -0.955423f}, {0.162460f, 0.262866f, -0.951056f}, + {-0.442863f, -0.238856f, -0.864188f}, {-0.309017f, -0.500000f, -0.809017f}, {-0.162460f, -0.262866f, -0.951056f}, + {0.000000f, -0.850651f, -0.525731f}, {-0.147621f, -0.716567f, -0.681718f}, {0.147621f, -0.716567f, -0.681718f}, + {0.000000f, -0.525731f, -0.850651f}, {0.309017f, -0.500000f, -0.809017f}, {0.442863f, -0.238856f, -0.864188f}, + {0.162460f, -0.262866f, -0.951056f}, {0.238856f, -0.864188f, -0.442863f}, {0.500000f, -0.809017f, -0.309017f}, + {0.425325f, -0.688191f, -0.587785f}, {0.716567f, -0.681718f, -0.147621f}, {0.688191f, -0.587785f, -0.425325f}, + {0.587785f, -0.425325f, -0.688191f}, {0.000000f, -0.955423f, -0.295242f}, {0.000000f, -1.000000f, 0.000000f}, + {0.262866f, -0.951056f, -0.162460f}, {0.000000f, -0.850651f, 0.525731f}, {0.000000f, -0.955423f, 0.295242f}, + {0.238856f, -0.864188f, 0.442863f}, {0.262866f, -0.951056f, 0.162460f}, {0.500000f, -0.809017f, 0.309017f}, + {0.716567f, -0.681718f, 0.147621f}, {0.525731f, -0.850651f, 0.000000f}, {-0.238856f, -0.864188f, -0.442863f}, + {-0.500000f, -0.809017f, -0.309017f}, {-0.262866f, -0.951056f, -0.162460f}, {-0.850651f, -0.525731f, 0.000000f}, + {-0.716567f, -0.681718f, -0.147621f}, {-0.716567f, -0.681718f, 0.147621f}, {-0.525731f, -0.850651f, 0.000000f}, + {-0.500000f, -0.809017f, 0.309017f}, {-0.238856f, -0.864188f, 0.442863f}, {-0.262866f, -0.951056f, 0.162460f}, + {-0.864188f, -0.442863f, 0.238856f}, {-0.809017f, -0.309017f, 0.500000f}, {-0.688191f, -0.587785f, 0.425325f}, + {-0.681718f, -0.147621f, 0.716567f}, {-0.442863f, -0.238856f, 0.864188f}, {-0.587785f, -0.425325f, 0.688191f}, + {-0.309017f, -0.500000f, 0.809017f}, {-0.147621f, -0.716567f, 0.681718f}, {-0.425325f, -0.688191f, 0.587785f}, + {-0.162460f, -0.262866f, 0.951056f}, {0.442863f, -0.238856f, 0.864188f}, {0.162460f, -0.262866f, 0.951056f}, + {0.309017f, -0.500000f, 0.809017f}, {0.147621f, -0.716567f, 0.681718f}, {0.000000f, -0.525731f, 0.850651f}, + {0.425325f, -0.688191f, 0.587785f}, {0.587785f, -0.425325f, 0.688191f}, {0.688191f, -0.587785f, 0.425325f}, + {-0.955423f, 0.295242f, 0.000000f}, {-0.951056f, 0.162460f, 0.262866f}, {-1.000000f, 0.000000f, 0.000000f}, + {-0.850651f, 0.000000f, 0.525731f}, {-0.955423f, -0.295242f, 0.000000f}, {-0.951056f, -0.162460f, 0.262866f}, + {-0.864188f, 0.442863f, -0.238856f}, {-0.951056f, 0.162460f, -0.262866f}, {-0.809017f, 0.309017f, -0.500000f}, + {-0.864188f, -0.442863f, -0.238856f}, {-0.951056f, -0.162460f, -0.262866f}, {-0.809017f, -0.309017f, -0.500000f}, + {-0.681718f, 0.147621f, -0.716567f}, {-0.681718f, -0.147621f, -0.716567f}, {-0.850651f, 0.000000f, -0.525731f}, + {-0.688191f, 0.587785f, -0.425325f}, {-0.587785f, 0.425325f, -0.688191f}, {-0.425325f, 0.688191f, -0.587785f}, + {-0.425325f, -0.688191f, -0.587785f}, {-0.587785f, -0.425325f, -0.688191f}, {-0.688191f, -0.587785f, -0.425325f}}; diff --git a/Runtime/blitz3d/md2rep.cpp b/Runtime/blitz3d/md2rep.cpp index f623b58..839bde1 100644 --- a/Runtime/blitz3d/md2rep.cpp +++ b/Runtime/blitz3d/md2rep.cpp @@ -1,15 +1,15 @@ -#include "std.hpp" #include "md2rep.hpp" #include "md2norms.hpp" +#include "std.hpp" -extern gxRuntime *gx_runtime; -extern gxGraphics *gx_graphics; +extern gxRuntime* gx_runtime; +extern gxGraphics* gx_graphics; -static Vector *normals = 0; -static float tex_coords[2][2] = { {0,0},{0,0} }; +static Vector* normals = 0; +static float tex_coords[2][2] = {{0, 0}, {0, 0}}; -#pragma pack( push,1 ) +#pragma pack(push, 1) struct md2_header { int magic; @@ -43,11 +43,12 @@ struct md2_tri { unsigned short verts[3], uvs[3]; }; -#pragma pack( pop ) +#pragma pack(pop) struct t_vert { unsigned short i, uv; - bool operator<(const t_vert &t)const { + bool operator<(const t_vert& t) const + { return memcmp(&i, &t.i, 4) < 0; } }; @@ -56,18 +57,20 @@ struct t_tri { unsigned short verts[3]; }; -MD2Rep::MD2Rep(const string &f) : - mesh(0), n_verts(0), n_tris(0), n_frames(0) { - - filebuf in; +MD2Rep::MD2Rep(const string& f) : mesh(0), n_verts(0), n_tris(0), n_frames(0) +{ + filebuf in; md2_header header; - if (!in.open(f.c_str(), ios_base::in | ios_base::binary)) return; - if (in.sgetn((char*)&header, sizeof(header)) != sizeof(header)) return; - if (header.magic != '2PDI' || header.version != 8) return; + if (!in.open(f.c_str(), ios_base::in | ios_base::binary)) + return; + if (in.sgetn((char*)&header, sizeof(header)) != sizeof(header)) + return; + if (header.magic != '2PDI' || header.version != 8) + return; n_frames = header.numFrames; - n_tris = header.numTriangles; + n_tris = header.numTriangles; //read in tex coords vector md2_uvs; @@ -81,8 +84,8 @@ MD2Rep::MD2Rep(const string &f) : in.pubseekpos(header.offsetTriangles); in.sgetn((char*)(&md2_tris.begin()[0]), n_tris * sizeof(md2_tri)); - vector t_tris; - vector t_verts; + vector t_tris; + vector t_verts; map t_map; int k; @@ -90,8 +93,8 @@ MD2Rep::MD2Rep(const string &f) : t_tri tr; for (int j = 0; j < 3; ++j) { t_vert t; - t.i = md2_tris[k].verts[j]; - t.uv = md2_tris[k].uvs[j]; + t.i = md2_tris[k].verts[j]; + t.uv = md2_tris[k].uvs[j]; map::iterator it = t_map.find(t); if (it == t_map.end()) { //create new vert @@ -119,8 +122,8 @@ MD2Rep::MD2Rep(const string &f) : //read in frames for (k = 0; k < n_frames; ++k) { - char t_buff[16]; - Frame *fr = &frames[k]; + char t_buff[16]; + Frame* fr = &frames[k]; in.sgetn((char*)&fr->scale, 12); in.sgetn((char*)&fr->trans, 12); in.sgetn(t_buff, 16); @@ -133,13 +136,13 @@ MD2Rep::MD2Rep(const string &f) : fr->verts.resize(n_verts); for (int j = 0; j < n_verts; ++j) { - Vertex *v = &fr->verts[j]; - const t_vert &tv = t_verts[j]; - const md2_vert &mv = md2_verts[tv.i]; - v->x = mv.y; - v->y = mv.z; - v->z = mv.x; - v->n = mv.n; + Vertex* v = &fr->verts[j]; + const t_vert& tv = t_verts[j]; + const md2_vert& mv = md2_verts[tv.i]; + v->x = mv.y; + v->y = mv.z; + v->z = mv.x; + v->n = mv.n; box.update(Vector(v->x, v->y, v->z) * fr->scale + fr->trans); } } @@ -148,7 +151,7 @@ MD2Rep::MD2Rep(const string &f) : mesh = gx_graphics->createMesh(n_verts, n_tris, 0); mesh->lock(true); for (k = 0; k < n_tris; ++k) { - const t_tri &t = t_tris[k]; + const t_tri& t = t_tris[k]; mesh->setTriangle(k, t.verts[0], t.verts[2], t.verts[1]); } mesh->unlock(); @@ -162,8 +165,10 @@ MD2Rep::MD2Rep(const string &f) : } } -MD2Rep::~MD2Rep() { - if (mesh) gx_graphics->freeMesh(mesh); +MD2Rep::~MD2Rep() +{ + if (mesh) + gx_graphics->freeMesh(mesh); } /* @@ -180,41 +185,43 @@ void MD2Rep::render( Vert *v,int frame ){ } */ -void MD2Rep::render(Vert *v, int frame, float time) { - - const Frame &frame_b = frames[frame]; - const Vertex *v_b = (Vertex*)(&frame_b.verts.begin()[0]); - const Vector scale_b = frame_b.scale, trans_b = frame_b.trans; +void MD2Rep::render(Vert* v, int frame, float time) +{ + const Frame& frame_b = frames[frame]; + const Vertex* v_b = (Vertex*)(&frame_b.verts.begin()[0]); + const Vector scale_b = frame_b.scale, trans_b = frame_b.trans; for (int k = 0; k < n_verts; ++v, ++v_b, ++k) { + const Vector t_b(v_b->x * scale_b.x + trans_b.x, v_b->y * scale_b.y + trans_b.y, + v_b->z * scale_b.z + trans_b.z); + const Vector& n_b = normals[v_b->n]; - const Vector t_b(v_b->x*scale_b.x + trans_b.x, v_b->y*scale_b.y + trans_b.y, v_b->z*scale_b.z + trans_b.z); - const Vector &n_b = normals[v_b->n]; - - v->coords += (t_b - v->coords)*time; - v->normal += (n_b - v->normal)*time; + v->coords += (t_b - v->coords) * time; + v->normal += (n_b - v->normal) * time; } } -void MD2Rep::render(Vert *v, int render_a, int render_b, float render_t) { - const Frame &frame_a = frames[render_a]; +void MD2Rep::render(Vert* v, int render_a, int render_b, float render_t) +{ + const Frame& frame_a = frames[render_a]; const Vector scale_a = frame_a.scale, trans_a = frame_a.trans; - const Frame &frame_b = frames[render_b]; + const Frame& frame_b = frames[render_b]; const Vector scale_b = frame_b.scale, trans_b = frame_b.trans; - const Vertex *v_a = (Vertex*)(&frame_a.verts.begin()[0]); - const Vertex *v_b = (Vertex*)(&frame_b.verts.begin()[0]); + const Vertex* v_a = (Vertex*)(&frame_a.verts.begin()[0]); + const Vertex* v_b = (Vertex*)(&frame_b.verts.begin()[0]); for (int k = 0; k < n_verts; ++v, ++v_a, ++v_b, ++k) { + const Vector t_a(v_a->x * scale_a.x + trans_a.x, v_a->y * scale_a.y + trans_a.y, + v_a->z * scale_a.z + trans_a.z); + const Vector t_b(v_b->x * scale_b.x + trans_b.x, v_b->y * scale_b.y + trans_b.y, + v_b->z * scale_b.z + trans_b.z); + v->coords = (t_b - t_a) * render_t + t_a; - const Vector t_a(v_a->x*scale_a.x + trans_a.x, v_a->y*scale_a.y + trans_a.y, v_a->z*scale_a.z + trans_a.z); - const Vector t_b(v_b->x*scale_b.x + trans_b.x, v_b->y*scale_b.y + trans_b.y, v_b->z*scale_b.z + trans_b.z); - v->coords = (t_b - t_a)*render_t + t_a; - - const Vector &n_a = normals[v_a->n]; - const Vector &n_b = normals[v_b->n]; - v->normal = (n_b - n_a)*render_t + n_a; + const Vector& n_a = normals[v_a->n]; + const Vector& n_b = normals[v_b->n]; + v->normal = (n_b - n_a) * render_t + n_a; } } @@ -227,27 +234,29 @@ void MD2Rep::render( Vert *v,const Vert *v_a,const Vert *v_b,float render_t ){ } */ -void MD2Rep::render(Model *model, int render_a, int render_b, float render_t) { - const Frame &frame_a = frames[render_a]; +void MD2Rep::render(Model* model, int render_a, int render_b, float render_t) +{ + const Frame& frame_a = frames[render_a]; const Vector scale_a = frame_a.scale, trans_a = frame_a.trans; - const Frame &frame_b = frames[render_b]; + const Frame& frame_b = frames[render_b]; const Vector scale_b = frame_b.scale, trans_b = frame_b.trans; - const VertexUV *uv = (VertexUV*)&uvs.begin()[0]; - const Vertex *v_a = (Vertex*)(&frame_a.verts.begin()[0]); - const Vertex *v_b = (Vertex*)(&frame_b.verts.begin()[0]); + const VertexUV* uv = (VertexUV*)&uvs.begin()[0]; + const Vertex* v_a = (Vertex*)(&frame_a.verts.begin()[0]); + const Vertex* v_b = (Vertex*)(&frame_b.verts.begin()[0]); mesh->lock(true); for (int k = 0; k < n_verts; ++uv, ++v_a, ++v_b, ++k) { + const Vector t_a(v_a->x * scale_a.x + trans_a.x, v_a->y * scale_a.y + trans_a.y, + v_a->z * scale_a.z + trans_a.z); + const Vector t_b(v_b->x * scale_b.x + trans_b.x, v_b->y * scale_b.y + trans_b.y, + v_b->z * scale_b.z + trans_b.z); + const Vector t((t_b - t_a) * render_t + t_a); - const Vector t_a(v_a->x*scale_a.x + trans_a.x, v_a->y*scale_a.y + trans_a.y, v_a->z*scale_a.z + trans_a.z); - const Vector t_b(v_b->x*scale_b.x + trans_b.x, v_b->y*scale_b.y + trans_b.y, v_b->z*scale_b.z + trans_b.z); - const Vector t((t_b - t_a)*render_t + t_a); - - const Vector &n_a = normals[v_a->n]; - const Vector &n_b = normals[v_b->n]; - const Vector n((n_b - n_a)*render_t + n_a); + const Vector& n_a = normals[v_a->n]; + const Vector& n_b = normals[v_b->n]; + const Vector n((n_b - n_a) * render_t + n_a); tex_coords[0][0] = uv->u; tex_coords[0][1] = uv->v; @@ -280,22 +289,22 @@ void MD2Rep::render( Model *model,const Vert *v_a,const Vert *v_b,float render_t } */ -void MD2Rep::render(Model *model, const Vert *v_a, int render_b, float render_t) { - - const Frame &frame_b = frames[render_b]; +void MD2Rep::render(Model* model, const Vert* v_a, int render_b, float render_t) +{ + const Frame& frame_b = frames[render_b]; const Vector scale_b = frame_b.scale, trans_b = frame_b.trans; - const VertexUV *uv = (VertexUV*)&uvs.begin()[0]; - const Vertex *v_b = (Vertex*)&frame_b.verts.begin()[0]; + const VertexUV* uv = (VertexUV*)&uvs.begin()[0]; + const Vertex* v_b = (Vertex*)&frame_b.verts.begin()[0]; mesh->lock(true); for (int k = 0; k < n_verts; ++uv, ++v_a, ++v_b, ++k) { + const Vector t_b(v_b->x * scale_b.x + trans_b.x, v_b->y * scale_b.y + trans_b.y, + v_b->z * scale_b.z + trans_b.z); + const Vector t((t_b - v_a->coords) * render_t + v_a->coords); - const Vector t_b(v_b->x*scale_b.x + trans_b.x, v_b->y*scale_b.y + trans_b.y, v_b->z*scale_b.z + trans_b.z); - const Vector t((t_b - v_a->coords)*render_t + v_a->coords); - - const Vector &n_b = normals[v_b->n]; - const Vector n((n_b - v_a->normal)*render_t + v_a->normal); + const Vector& n_b = normals[v_b->n]; + const Vector n((n_b - v_a->normal) * render_t + v_a->normal); tex_coords[0][0] = uv->u; tex_coords[0][1] = uv->v; @@ -306,4 +315,3 @@ void MD2Rep::render(Model *model, const Vert *v_a, int render_b, float render_t) model->enqueue(mesh, 0, n_verts, 0, n_tris); } - diff --git a/Runtime/blitz3d/md2rep.hpp b/Runtime/blitz3d/md2rep.hpp index 128671f..8f20be0 100644 --- a/Runtime/blitz3d/md2rep.hpp +++ b/Runtime/blitz3d/md2rep.hpp @@ -4,47 +4,56 @@ #include "model.hpp" -class MD2Rep{ -public: - struct Vert{ - Vector coords,normal; +class MD2Rep { + public: + struct Vert { + Vector coords, normal; }; - MD2Rep( const string &f ); + MD2Rep(const string& f); virtual ~MD2Rep(); - void render( Vert *verts,int frame ); - void render( Vert *verts,int frame,float time ); - void render( Vert *verts,int frame_a,int frame_b,float time ); - void render( Vert *verts,const Vert *verts_a,const Vert *verts_b,float time ); + void render(Vert* verts, int frame); + void render(Vert* verts, int frame, float time); + void render(Vert* verts, int frame_a, int frame_b, float time); + void render(Vert* verts, const Vert* verts_a, const Vert* verts_b, float time); - void render( Model *model,int frame_a,int frame_b,float time ); - void render( Model *model,const Vert *verts_a,const Vert *verts_b,float time ); - void render( Model *model,const Vert *verts_a,int frame_b,float time ); + void render(Model* model, int frame_a, int frame_b, float time); + void render(Model* model, const Vert* verts_a, const Vert* verts_b, float time); + void render(Model* model, const Vert* verts_a, int frame_b, float time); - const Box &getBox()const{ return box; } - const int numFrames()const{ return n_frames; } - const int numVertices()const{ return n_verts; } + const Box& getBox() const + { + return box; + } + const int numFrames() const + { + return n_frames; + } + const int numVertices() const + { + return n_verts; + } -private: - struct Vertex{ - unsigned char x,y,z,n; + private: + struct Vertex { + unsigned char x, y, z, n; }; - struct VertexUV{ - float u,v; + struct VertexUV { + float u, v; }; - struct Frame{ - Vector scale,trans; + struct Frame { + Vector scale, trans; vector verts; }; - Box box; - gxMesh *mesh; - int n_frames; - int n_verts,n_tris; - vector frames; + Box box; + gxMesh* mesh; + int n_frames; + int n_verts, n_tris; + vector frames; vector uvs; }; diff --git a/Runtime/blitz3d/meshcollider.cpp b/Runtime/blitz3d/meshcollider.cpp index 431a56d..b4a201d 100644 --- a/Runtime/blitz3d/meshcollider.cpp +++ b/Runtime/blitz3d/meshcollider.cpp @@ -1,196 +1,228 @@ -#include "std.hpp" #include "meshcollider.hpp" +#include "std.hpp" -static const int MAX_COLL_TRIS=16; +static const int MAX_COLL_TRIS = 16; static vector tri_centres; extern float stats3d[10]; -extern gxRuntime *gx_runtime; +extern gxRuntime* gx_runtime; -static bool triTest( const Vector a[3],const Vector b[3] ){ - bool pb0=false,pb1=false,pb2=false; - Plane p( a[0],a[1],a[2] ),p0,p1,p2; - for( int k=0;k<3;++k ){ - Line l( b[k],b[(k+1)%3]-b[k] ); - float t=p.t_intersect( l ); - if( t<0 || t>1 ) continue; - Vector i=l*t; - if( !pb0 ){ p0=Plane( a[0]+p.n,a[1],a[0] );pb0=true; } - if( p0.distance( i )<0 ) continue; - if( !pb1 ){ p1=Plane( a[1]+p.n,a[2],a[1] );pb1=true; } - if( p1.distance( i )<0 ) continue; - if( !pb2 ){ p2=Plane( a[2]+p.n,a[0],a[2] );pb2=true; } - if( p2.distance( i )<0 ) continue; +static bool triTest(const Vector a[3], const Vector b[3]) +{ + bool pb0 = false, pb1 = false, pb2 = false; + Plane p(a[0], a[1], a[2]), p0, p1, p2; + for (int k = 0; k < 3; ++k) { + Line l(b[k], b[(k + 1) % 3] - b[k]); + float t = p.t_intersect(l); + if (t < 0 || t > 1) + continue; + Vector i = l * t; + if (!pb0) { + p0 = Plane(a[0] + p.n, a[1], a[0]); + pb0 = true; + } + if (p0.distance(i) < 0) + continue; + if (!pb1) { + p1 = Plane(a[1] + p.n, a[2], a[1]); + pb1 = true; + } + if (p1.distance(i) < 0) + continue; + if (!pb2) { + p2 = Plane(a[2] + p.n, a[0], a[2]); + pb2 = true; + } + if (p2.distance(i) < 0) + continue; return true; } return false; } -static bool trisIntersect( const Vector a[3],const Vector b[3] ){ - return triTest( a,b ) || triTest( b,a ); +static bool trisIntersect(const Vector a[3], const Vector b[3]) +{ + return triTest(a, b) || triTest(b, a); } -MeshCollider::MeshCollider( const vector &verts,const vector &tris ): -vertices(verts),triangles(tris){ +MeshCollider::MeshCollider(const vector& verts, const vector& tris) : vertices(verts), triangles(tris) +{ vector ts; tri_centres.clear(); - for( int k=0;kbox ) ){ +bool MeshCollider::collide(const Box& line_box, const Line& line, float radius, const Transform& tform, + Collision* curr_coll, MeshCollider::Node* node) +{ + if (!line_box.overlaps(node->box)) { return false; } - bool hit=false; - if( !node->triangles.size() ){ - if( node->left ) hit|=collide( line_box,line,radius,tform,curr_coll,node->left ); - if( node->right ) hit|=collide( line_box,line,radius,tform,curr_coll,node->right ); + bool hit = false; + if (!node->triangles.size()) { + if (node->left) + hit |= collide(line_box, line, radius, tform, curr_coll, node->left); + if (node->right) + hit |= collide(line_box, line, radius, tform, curr_coll, node->right); return hit; } - stats3d[0]+=node->triangles.size(); + stats3d[0] += node->triangles.size(); - for( int k=0;ktriangles.size();++k ){ - - const Triangle &tri=triangles[node->triangles[k]]; - const Vector &t_v0=vertices[ tri.verts[0] ].coords; - const Vector &t_v1=vertices[ tri.verts[1] ].coords; - const Vector &t_v2=vertices[ tri.verts[2] ].coords; + for (int k = 0; k < node->triangles.size(); ++k) { + const Triangle& tri = triangles[node->triangles[k]]; + const Vector& t_v0 = vertices[tri.verts[0]].coords; + const Vector& t_v1 = vertices[tri.verts[1]].coords; + const Vector& t_v2 = vertices[tri.verts[2]].coords; //tri box - Box tri_box( t_v0 ); - tri_box.update( t_v1 ); - tri_box.update( t_v2 ); - if( !tri_box.overlaps( line_box ) ) continue; + Box tri_box(t_v0); + tri_box.update(t_v1); + tri_box.update(t_v2); + if (!tri_box.overlaps(line_box)) + continue; - if( !curr_coll->triangleCollide( line,radius,tform*t_v0,tform*t_v1,tform*t_v2 ) ) continue; + if (!curr_coll->triangleCollide(line, radius, tform * t_v0, tform * t_v1, tform * t_v2)) + continue; - curr_coll->surface=tri.surface; - curr_coll->index=tri.index; - - hit=true; + curr_coll->surface = tri.surface; + curr_coll->index = tri.index; + + hit = true; } return hit; } -Box MeshCollider::nodeBox( const vector &tris ){ +Box MeshCollider::nodeBox(const vector& tris) +{ Box box; - for( int k=0;k &tris ){ - - Node *c=new Node; - c->box=nodeBox( tris ); - c->triangles=tris; - leaves.push_back( c ); +MeshCollider::Node* MeshCollider::createLeaf(const vector& tris) +{ + Node* c = new Node; + c->box = nodeBox(tris); + c->triangles = tris; + leaves.push_back(c); return c; } -MeshCollider::Node *MeshCollider::createNode( const vector &tris ){ +MeshCollider::Node* MeshCollider::createNode(const vector& tris) +{ + if (tris.size() <= MAX_COLL_TRIS) + return createLeaf(tris); - if( tris.size()<=MAX_COLL_TRIS ) return createLeaf( tris ); - - Node *c=new Node; - c->box=nodeBox( tris ); + Node* c = new Node; + c->box = nodeBox(tris); //find longest axis // - float max=c->box.width(); - if( c->box.height()>max ) max=c->box.height(); - if( c->box.depth()>max ) max=c->box.depth(); - int axis=0; - if( max==c->box.height() ) axis=1; - else if( max==c->box.depth() ) axis=2; + float max = c->box.width(); + if (c->box.height() > max) + max = c->box.height(); + if (c->box.depth() > max) + max = c->box.depth(); + int axis = 0; + if (max == c->box.height()) + axis = 1; + else if (max == c->box.depth()) + axis = 2; //sort by axis // - int k; - multimap axis_map; - for( k=0;k p( tri_centres[tris[k]][axis],tris[k] ); - axis_map.insert( p ); + int k; + multimap axis_map; + for (k = 0; k < tris.size(); ++k) { + pair p(tri_centres[tris[k]][axis], tris[k]); + axis_map.insert(p); } //generate left node // - vector new_tris; - multimap::iterator it=axis_map.begin(); - for( k=axis_map.size()/2;k--;++it ){ - new_tris.push_back( it->second ); + vector new_tris; + multimap::iterator it = axis_map.begin(); + for (k = axis_map.size() / 2; k--; ++it) { + new_tris.push_back(it->second); } - c->left=createNode( new_tris ); + c->left = createNode(new_tris); //generate right node // new_tris.clear(); - for( ;it!=axis_map.end();++it ){ - new_tris.push_back( it->second ); + for (; it != axis_map.end(); ++it) { + new_tris.push_back(it->second); } - c->right=createNode( new_tris ); + c->right = createNode(new_tris); return c; } -bool MeshCollider::intersects( const MeshCollider &c,const Transform &t )const{ +bool MeshCollider::intersects(const MeshCollider& c, const Transform& t) const +{ + static Vector a[MAX_COLL_TRIS][3], b[3]; - static Vector a[MAX_COLL_TRIS][3],b[3]; - - if( !(t * tree->box).overlaps( c.tree->box ) ) return false; - for( int k=0;kbox; - bool tformed=false; - for( int j=0;jbox ) ) continue; - if( !tformed ){ - for( int n=0;ntriangles.size();++n ){ - const Triangle &tri=triangles[p->triangles[n]]; - a[n][0]=t * vertices[tri.verts[0]].coords; - a[n][1]=t * vertices[tri.verts[1]].coords; - a[n][2]=t * vertices[tri.verts[2]].coords; + if (!(t * tree->box).overlaps(c.tree->box)) + return false; + for (int k = 0; k < leaves.size(); ++k) { + Node* p = leaves[k]; + Box box = t * p->box; + bool tformed = false; + for (int j = 0; j < c.leaves.size(); ++j) { + Node* q = c.leaves[j]; + if (!box.overlaps(q->box)) + continue; + if (!tformed) { + for (int n = 0; n < p->triangles.size(); ++n) { + const Triangle& tri = triangles[p->triangles[n]]; + a[n][0] = t * vertices[tri.verts[0]].coords; + a[n][1] = t * vertices[tri.verts[1]].coords; + a[n][2] = t * vertices[tri.verts[2]].coords; } - tformed=true; + tformed = true; } - for( int n=0;ntriangles.size();++n ){ - const Triangle &tri=c.triangles[q->triangles[n]]; - b[0]=c.vertices[tri.verts[0]].coords; - b[1]=c.vertices[tri.verts[1]].coords; - b[2]=c.vertices[tri.verts[2]].coords; - for( int t=0;ttriangles.size();++t ){ - if( trisIntersect( a[t],b ) ) return true; + for (int n = 0; n < q->triangles.size(); ++n) { + const Triangle& tri = c.triangles[q->triangles[n]]; + b[0] = c.vertices[tri.verts[0]].coords; + b[1] = c.vertices[tri.verts[1]].coords; + b[2] = c.vertices[tri.verts[2]].coords; + for (int t = 0; t < p->triangles.size(); ++t) { + if (trisIntersect(a[t], b)) + return true; } } } diff --git a/Runtime/blitz3d/meshcollider.hpp b/Runtime/blitz3d/meshcollider.hpp index 9cf3912..71406fc 100644 --- a/Runtime/blitz3d/meshcollider.hpp +++ b/Runtime/blitz3d/meshcollider.hpp @@ -4,42 +4,47 @@ #include "collision.hpp" -class MeshCollider{ -public: - struct Vertex{ +class MeshCollider { + public: + struct Vertex { Vector coords; }; - struct Triangle{ - void *surface; - int verts[3],index; + struct Triangle { + void* surface; + int verts[3], index; }; - MeshCollider( const vector &verts,const vector &tris ); + MeshCollider(const vector& verts, const vector& tris); ~MeshCollider(); //sphere collision - bool collide( const Line &line,float radius,Collision *curr_coll,const Transform &tform ); + bool collide(const Line& line, float radius, Collision* curr_coll, const Transform& tform); - bool intersects( const MeshCollider &c,const Transform &t )const; + bool intersects(const MeshCollider& c, const Transform& t) const; -private: - vector vertices; + private: + vector vertices; vector triangles; - struct Node{ - Box box; - Node *left,*right; + struct Node { + Box box; + Node * left, *right; vector triangles; - Node():left(0),right(0){} - ~Node(){ delete left;delete right; } + Node() : left(0), right(0) {} + ~Node() + { + delete left; + delete right; + } }; - Node *tree; + Node* tree; vector leaves; - Box nodeBox( const vector &tris ); - Node *createLeaf( const vector &tris ); - Node *createNode( const vector &tris ); - bool collide( const Box &box,const Line &line,float radius,const Transform &tform,Collision *curr_coll,Node *node ); + Box nodeBox(const vector& tris); + Node* createLeaf(const vector& tris); + Node* createNode(const vector& tris); + bool collide(const Box& box, const Line& line, float radius, const Transform& tform, Collision* curr_coll, + Node* node); }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/meshloader.cpp b/Runtime/blitz3d/meshloader.cpp index aa14f16..cc8e826 100644 --- a/Runtime/blitz3d/meshloader.cpp +++ b/Runtime/blitz3d/meshloader.cpp @@ -1,130 +1,148 @@ -#include "std.hpp" #include "meshloader.hpp" #include "meshmodel.hpp" +#include "std.hpp" -struct Tri{ +struct Tri { int verts[3]; }; -struct Surf{ +struct Surf { vector tris; }; -struct MLMesh{ - map brush_map; +struct MLMesh { + map brush_map; vector verts; - MLMesh(){ - } + MLMesh() {} - ~MLMesh(){ - map::const_iterator it; - for( it=brush_map.begin();it!=brush_map.end();++it ){ + ~MLMesh() + { + map::const_iterator it; + for (it = brush_map.begin(); it != brush_map.end(); ++it) { delete it->second; } } }; -static MLMesh *ml_mesh; +static MLMesh* ml_mesh; static vector mesh_stack; -void MeshLoader::beginMesh(){ - mesh_stack.push_back( ml_mesh ); - ml_mesh=new MLMesh(); +void MeshLoader::beginMesh() +{ + mesh_stack.push_back(ml_mesh); + ml_mesh = new MLMesh(); } -int MeshLoader::numVertices(){ +int MeshLoader::numVertices() +{ return ml_mesh->verts.size(); } -void MeshLoader::addVertex( const Surface::Vertex &v ){ - ml_mesh->verts.push_back( v ); +void MeshLoader::addVertex(const Surface::Vertex& v) +{ + ml_mesh->verts.push_back(v); } -void MeshLoader::addTriangle( const int verts[3],const Brush &b ){ - addTriangle( verts[0],verts[1],verts[2],b ); +void MeshLoader::addTriangle(const int verts[3], const Brush& b) +{ + addTriangle(verts[0], verts[1], verts[2], b); } -void MeshLoader::addBone( int n,float w,int b ){ - Surface::Vertex &v=ml_mesh->verts[n]; - int i; - for( i=0;iv.bone_weights[i] ) break; +void MeshLoader::addBone(int n, float w, int b) +{ + Surface::Vertex& v = ml_mesh->verts[n]; + int i; + for (i = 0; i < MAX_SURFACE_BONES; ++i) { + if (v.bone_bones[i] == 255 || w > v.bone_weights[i]) + break; } - if( i==MAX_SURFACE_BONES ) return; - for( int k=MAX_SURFACE_BONES-1;k>i;--k ){ - v.bone_bones[k]=v.bone_bones[k-1]; - v.bone_weights[k]=v.bone_weights[k-1]; + if (i == MAX_SURFACE_BONES) + return; + for (int k = MAX_SURFACE_BONES - 1; k > i; --k) { + v.bone_bones[k] = v.bone_bones[k - 1]; + v.bone_weights[k] = v.bone_weights[k - 1]; } - v.bone_bones[i]=b; - v.bone_weights[i]=w; + v.bone_bones[i] = b; + v.bone_weights[i] = w; } -Surface::Vertex &MeshLoader::refVertex( int n ){ +Surface::Vertex& MeshLoader::refVertex(int n) +{ return ml_mesh->verts[n]; } -void MeshLoader::addTriangle( int v0,int v1,int v2,const Brush &b ){ +void MeshLoader::addTriangle(int v0, int v1, int v2, const Brush& b) +{ //find surface - Surf *surf; - map::const_iterator it=ml_mesh->brush_map.find( b ); - if( it!=ml_mesh->brush_map.end() ) surf=it->second; - else{ - surf=new Surf; - ml_mesh->brush_map.insert( make_pair( b,surf ) ); + Surf* surf; + map::const_iterator it = ml_mesh->brush_map.find(b); + if (it != ml_mesh->brush_map.end()) + surf = it->second; + else { + surf = new Surf; + ml_mesh->brush_map.insert(make_pair(b, surf)); } Tri tri; - tri.verts[0]=v0;tri.verts[1]=v1;tri.verts[2]=v2; - surf->tris.push_back( tri ); + tri.verts[0] = v0; + tri.verts[1] = v1; + tri.verts[2] = v2; + surf->tris.push_back(tri); } -void MeshLoader::endMesh( MeshModel *mesh ){ - if( mesh ){ +void MeshLoader::endMesh(MeshModel* mesh) +{ + if (mesh) { //fix bone weights - int k,max_bones=0; - for( k=0;kverts.size();++k ){ - Surface::Vertex &v=ml_mesh->verts[k]; - if( v.bone_bones[0]==255 ) continue; - int j; - float t=0; - for( j=0;jverts.size(); ++k) { + Surface::Vertex& v = ml_mesh->verts[k]; + if (v.bone_bones[0] == 255) + continue; + int j; + float t = 0; + for (j = 0; j < MAX_SURFACE_BONES; ++j) { + if (v.bone_bones[j] == 255) + break; + t += v.bone_weights[j]; } - if( j>max_bones ) max_bones=j; - t=1.0f/t; - for( j=0;j max_bones) + max_bones = j; + t = 1.0f / t; + for (j = 0; j < MAX_SURFACE_BONES; ++j) { + v.bone_weights[j] *= t; } } - map vert_map; - map::iterator it; - for( it=ml_mesh->brush_map.begin();it!=ml_mesh->brush_map.end();++it ){ + map vert_map; + map::iterator it; + for (it = ml_mesh->brush_map.begin(); it != ml_mesh->brush_map.end(); ++it) { vert_map.clear(); - Brush b=it->first; - Surf *t=it->second; - Surface *surf=mesh->findSurface( b ); - if( !surf ) surf=mesh->createSurface( b ); - for( int k=0;ktris.size();++k ){ + Brush b = it->first; + Surf* t = it->second; + Surface* surf = mesh->findSurface(b); + if (!surf) + surf = mesh->createSurface(b); + for (int k = 0; k < t->tris.size(); ++k) { Surface::Triangle tri; - for( int j=0;j<3;++j ){ - int n=t->tris[k].verts[j],id; - map::const_iterator it=vert_map.find( n ); - if( it!=vert_map.end() ) id=it->second; - else{ - id=surf->numVertices(); - surf->addVertex( ml_mesh->verts[n] ); - vert_map.insert( make_pair( n,id ) ); + for (int j = 0; j < 3; ++j) { + int n = t->tris[k].verts[j], id; + map::const_iterator it = vert_map.find(n); + if (it != vert_map.end()) + id = it->second; + else { + id = surf->numVertices(); + surf->addVertex(ml_mesh->verts[n]); + vert_map.insert(make_pair(n, id)); } - tri.verts[j]=id; + tri.verts[j] = id; } - surf->addTriangle( tri ); + surf->addTriangle(tri); } } } delete ml_mesh; - ml_mesh=mesh_stack.back(); + ml_mesh = mesh_stack.back(); mesh_stack.pop_back(); } diff --git a/Runtime/blitz3d/meshloader.hpp b/Runtime/blitz3d/meshloader.hpp index 1a4423b..2f951c3 100644 --- a/Runtime/blitz3d/meshloader.hpp +++ b/Runtime/blitz3d/meshloader.hpp @@ -5,38 +5,35 @@ #include "model.hpp" #include "surface.hpp" -class MeshLoader{ -public: - enum{ - HINT_COLLAPSE=1, - HINT_ANIMONLY=2 - }; +class MeshLoader { + public: + enum { HINT_COLLAPSE = 1, HINT_ANIMONLY = 2 }; - virtual MeshModel *load( const string &f,const Transform &conv,int hint )=0; + virtual MeshModel* load(const string& f, const Transform& conv, int hint) = 0; //clear static void beginMesh(); //add a vertex - static void addVertex( const Surface::Vertex &v ); + static void addVertex(const Surface::Vertex& v); //add a triangle - static void addTriangle( const int verts[3],const Brush &b ); + static void addTriangle(const int verts[3], const Brush& b); //also add a triangle - static void addTriangle( int v0,int v1,int v2,const Brush &b ); + static void addTriangle(int v0, int v1, int v2, const Brush& b); //add a bone - static void addBone( int vert,float weight,int bone ); + static void addBone(int vert, float weight, int bone); //reference a vertex - static Surface::Vertex &refVertex( int vert ); + static Surface::Vertex& refVertex(int vert); //number of verts static int numVertices(); //finally, update the mesh... - static void endMesh( MeshModel *mesh ); + static void endMesh(MeshModel* mesh); }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/meshmodel.cpp b/Runtime/blitz3d/meshmodel.cpp index 89e6c92..34acfd9 100644 --- a/Runtime/blitz3d/meshmodel.cpp +++ b/Runtime/blitz3d/meshmodel.cpp @@ -1,61 +1,68 @@ -#include "std.hpp" #include "meshmodel.hpp" #include "meshcollider.hpp" +#include "std.hpp" -extern gxGraphics *gx_graphics; +extern gxGraphics* gx_graphics; struct MeshModel::Rep : public Surface::Monitor { + int ref_cnt; + mutable Box box, cullBox; + mutable MeshCollider* collider; + mutable int box_valid, coll_valid, norms_valid; - int ref_cnt; - mutable Box box, cullBox; - mutable MeshCollider *collider; - mutable int box_valid, coll_valid, norms_valid; - - SurfaceList surfaces; + SurfaceList surfaces; vector bone_tforms; - Rep() : - ref_cnt(1), collider(0), box_valid(-1), coll_valid(-1), norms_valid(-1) { + Rep() : ref_cnt(1), collider(0), box_valid(-1), coll_valid(-1), norms_valid(-1) + { geom_changes = brush_changes = 0; } - ~Rep() { + ~Rep() + { delete collider; - for (int k = 0; k < surfaces.size(); ++k) delete surfaces[k]; + for (int k = 0; k < surfaces.size(); ++k) + delete surfaces[k]; } - Surface *createSurface(const Brush &b) { - Surface *t = new Surface(this); + Surface* createSurface(const Brush& b) + { + Surface* t = new Surface(this); surfaces.push_back(t); t->setBrush(b); return t; } - Surface *findSurface(const Brush &b) { + Surface* findSurface(const Brush& b) + { for (int k = 0; k < surfaces.size(); ++k) { - Surface *s = surfaces[k]; - if (s->getBrush() < b || b < s->getBrush()) continue; + Surface* s = surfaces[k]; + if (s->getBrush() < b || b < s->getBrush()) + continue; return s; } return 0; } - void paint(const Brush &b) { + void paint(const Brush& b) + { for (int k = 0; k < surfaces.size(); ++k) { - Surface *s = surfaces[k]; + Surface* s = surfaces[k]; s->setBrush(b); } } - void add(Rep *t) { + void add(Rep* t) + { if (cullBox.empty() && !t->cullBox.empty()) { setCullBox(t->cullBox); } for (int k = 0; k < t->surfaces.size(); ++k) { - Surface *src = t->surfaces[k]; - Surface *dest = findSurface(src->getBrush()); - if (!dest) dest = createSurface(src->getBrush()); + Surface* src = t->surfaces[k]; + Surface* dest = findSurface(src->getBrush()); + if (!dest) + dest = createSurface(src->getBrush()); int j; for (j = 0; j < src->numTriangles(); ++j) { Surface::Triangle t = src->getTriangle(j); @@ -70,23 +77,25 @@ struct MeshModel::Rep : public Surface::Monitor { } } - void transform(const Transform &t) { + void transform(const Transform& t) + { Matrix co = t.m.cofactor(); for (int k = 0; k < surfaces.size(); ++k) { - Surface *s = surfaces[k]; + Surface* s = surfaces[k]; for (int j = 0; j < s->numVertices(); ++j) { - const Vector &v = s->getVertex(j).coords; - const Vector &n = s->getVertex(j).normal; - s->setCoords(j, t*v); - s->setNormal(j, co*n); + const Vector& v = s->getVertex(j).coords; + const Vector& n = s->getVertex(j).normal; + s->setCoords(j, t * v); + s->setNormal(j, co * n); } } } - void flip() { + void flip() + { for (int k = 0; k < surfaces.size(); ++k) { - Surface *s = surfaces[k]; - int j; + Surface* s = surfaces[k]; + int j; for (j = 0; j < s->numVertices(); ++j) { s->setNormal(j, -s->getVertex(j).normal); } @@ -98,25 +107,28 @@ struct MeshModel::Rep : public Surface::Monitor { } } - void setCullBox(const Box &t) { + void setCullBox(const Box& t) + { cullBox = t; } - void updateNormals() { + void updateNormals() + { if (norms_valid != geom_changes) { for (int k = 0; k < surfaces.size(); ++k) { - Surface *s = surfaces[k]; + Surface* s = surfaces[k]; s->updateNormals(); } norms_valid = geom_changes; } } - const Box &getBox()const { + const Box& getBox() const + { if (box_valid != geom_changes) { box.clear(); for (int k = 0; k < surfaces.size(); ++k) { - Surface *s = surfaces[k]; + Surface* s = surfaces[k]; for (int j = 0; j < s->numVertices(); ++j) { box.update(s->getVertex(j).coords); } @@ -126,25 +138,27 @@ struct MeshModel::Rep : public Surface::Monitor { return box; } - const Box &getCullBox()const { + const Box& getCullBox() const + { return cullBox.empty() ? getBox() : cullBox; } - MeshCollider *getCollider()const { + MeshCollider* getCollider() const + { if (coll_valid != geom_changes) { delete collider; - vector verts; + vector verts; vector tris; for (int k = 0; k < surfaces.size(); ++k) { - Surface *s = surfaces[k]; - int j; + Surface* s = surfaces[k]; + int j; for (j = 0; j < s->numTriangles(); ++j) { MeshCollider::Triangle q; q.verts[0] = s->getTriangle(j).verts[0] + verts.size(); q.verts[1] = s->getTriangle(j).verts[1] + verts.size(); q.verts[2] = s->getTriangle(j).verts[2] + verts.size(); - q.surface = s; - q.index = j; + q.surface = s; + q.index = j; tris.push_back(q); } for (j = 0; j < s->numVertices(); ++j) { @@ -153,18 +167,17 @@ struct MeshModel::Rep : public Surface::Monitor { verts.push_back(q); } } - collider = new MeshCollider(verts, tris); + collider = new MeshCollider(verts, tris); coll_valid = geom_changes; } return collider; } }; -MeshModel::MeshModel() : - rep(new Rep()), brush_changes(0) {} +MeshModel::MeshModel() : rep(new Rep()), brush_changes(0) {} -MeshModel::MeshModel(const MeshModel &t) : Model(t), -rep(t.rep), brush_changes(rep->brush_changes - 1) { +MeshModel::MeshModel(const MeshModel& t) : Model(t), rep(t.rep), brush_changes(rep->brush_changes - 1) +{ ++rep->ref_cnt; surf_bones.resize(t.surf_bones.size()); /* @@ -177,26 +190,32 @@ rep(t.rep), brush_changes(rep->brush_changes - 1) { */ } -MeshModel::~MeshModel() { - if (!--rep->ref_cnt) delete rep; +MeshModel::~MeshModel() +{ + if (!--rep->ref_cnt) + delete rep; } -void MeshModel::updateNormals() { +void MeshModel::updateNormals() +{ rep->updateNormals(); } -void MeshModel::setCullBox(const Box &box) { +void MeshModel::setCullBox(const Box& box) +{ rep->setCullBox(box); } -void MeshModel::setRenderBrush(const Brush &b) { +void MeshModel::setRenderBrush(const Brush& b) +{ --brush_changes; render_brush = b; } -void MeshModel::createBones() { +void MeshModel::createBones() +{ setRenderSpace(RENDER_SPACE_WORLD); - const vector &bones = getAnimator()->getObjects(); + const vector& bones = getAnimator()->getObjects(); surf_bones.resize(bones.size()); @@ -207,19 +226,21 @@ void MeshModel::createBones() { } } -bool MeshModel::render(const RenderContext &rc) { - - const Box &b = rep->getCullBox(); - if (b.empty()) return false; +bool MeshModel::render(const RenderContext& rc) +{ + const Box& b = rep->getCullBox(); + if (b.empty()) + return false; static Frustum model_frustum; - new(&model_frustum) Frustum(rc.getWorldFrustum(), -getRenderTform()); - if (!model_frustum.cull(b)) return false; + new (&model_frustum) Frustum(rc.getWorldFrustum(), -getRenderTform()); + if (!model_frustum.cull(b)) + return false; if (brush_changes != rep->brush_changes) { brushes.clear(); for (int k = 0; k < rep->surfaces.size(); ++k) { - Surface *s = rep->surfaces[k]; + Surface* s = rep->surfaces[k]; brushes.push_back(Brush(s->getBrush(), render_brush)); } brush_changes = rep->brush_changes; @@ -227,8 +248,8 @@ bool MeshModel::render(const RenderContext &rc) { if (!surf_bones.size()) { for (int k = 0; k < rep->surfaces.size(); ++k) { - Surface *s = rep->surfaces[k]; - if (gxMesh *mesh = s->getMesh()) { + Surface* s = rep->surfaces[k]; + if (gxMesh* mesh = s->getMesh()) { enqueue(mesh, 0, s->numVertices(), 0, s->numTriangles(), brushes[k]); } } @@ -236,21 +257,20 @@ bool MeshModel::render(const RenderContext &rc) { } //OK, its boned! - const vector &bones = getAnimator()->getObjects(); + const vector& bones = getAnimator()->getObjects(); int k; for (k = 0; k < bones.size(); ++k) { - Transform t = - bones[k]->getRenderTform() * rep->bone_tforms[k]; - surf_bones[k].coord_tform = t; + Transform t = bones[k]->getRenderTform() * rep->bone_tforms[k]; + surf_bones[k].coord_tform = t; surf_bones[k].normal_tform = t.m.cofactor(); } bool trans = false; for (k = 0; k < rep->surfaces.size(); ++k) { - Surface *s = rep->surfaces[k]; + Surface* s = rep->surfaces[k]; if (brushes[k].getBlend() == gxScene::BLEND_REPLACE) { - if (gxMesh *mesh = s->getMesh(surf_bones)) { + if (gxMesh* mesh = s->getMesh(surf_bones)) { enqueue(mesh, 0, s->numVertices(), 0, s->numTriangles(), brushes[k]); } } else { @@ -260,12 +280,13 @@ bool MeshModel::render(const RenderContext &rc) { return trans; } -void MeshModel::renderQueue(int type) { +void MeshModel::renderQueue(int type) +{ if (type == QUEUE_TRANSPARENT && surf_bones.size()) { for (int k = 0; k < rep->surfaces.size(); ++k) { - Surface *s = rep->surfaces[k]; + Surface* s = rep->surfaces[k]; if (brushes[k].getBlend() != gxScene::BLEND_REPLACE) { - if (gxMesh *mesh = s->getMesh(surf_bones)) { + if (gxMesh* mesh = s->getMesh(surf_bones)) { enqueue(mesh, 0, s->numVertices(), 0, s->numTriangles(), brushes[k]); } } @@ -274,47 +295,58 @@ void MeshModel::renderQueue(int type) { Model::renderQueue(type); } -Surface *MeshModel::createSurface(const Brush &b) { +Surface* MeshModel::createSurface(const Brush& b) +{ return rep->createSurface(b); --brush_changes; } -void MeshModel::flipTriangles() { +void MeshModel::flipTriangles() +{ rep->flip(); } -void MeshModel::transform(const Transform &t) { +void MeshModel::transform(const Transform& t) +{ rep->transform(t); } -void MeshModel::add(const MeshModel &t) { +void MeshModel::add(const MeshModel& t) +{ rep->add(t.rep); } -const MeshModel::SurfaceList &MeshModel::getSurfaces()const { +const MeshModel::SurfaceList& MeshModel::getSurfaces() const +{ return rep->surfaces; } -void MeshModel::paint(const Brush &b) { +void MeshModel::paint(const Brush& b) +{ rep->paint(b); } -const Box &MeshModel::getBox()const { +const Box& MeshModel::getBox() const +{ return rep->getBox(); } -MeshCollider *MeshModel::getCollider()const { +MeshCollider* MeshModel::getCollider() const +{ return rep->getCollider(); } -Surface *MeshModel::findSurface(const Brush &b)const { +Surface* MeshModel::findSurface(const Brush& b) const +{ return rep->findSurface(b); } -bool MeshModel::collide(const Line &line, float radius, Collision *curr_coll, const Transform &t) { +bool MeshModel::collide(const Line& line, float radius, Collision* curr_coll, const Transform& t) +{ return getCollider()->collide(line, radius, curr_coll, t); } -bool MeshModel::intersects(const MeshModel &m)const { - return getCollider()->intersects(*m.getCollider(), -m.GetWorldTransform()*GetWorldTransform()); +bool MeshModel::intersects(const MeshModel& m) const +{ + return getCollider()->intersects(*m.getCollider(), -m.GetWorldTransform() * GetWorldTransform()); } diff --git a/Runtime/blitz3d/meshmodel.hpp b/Runtime/blitz3d/meshmodel.hpp index a4ba775..8ff5644 100644 --- a/Runtime/blitz3d/meshmodel.hpp +++ b/Runtime/blitz3d/meshmodel.hpp @@ -7,57 +7,63 @@ class MeshCollider; -class MeshModel : public Model{ -public: +class MeshModel : public Model { + public: typedef vector SurfaceList; MeshModel(); - MeshModel( const MeshModel &t ); + MeshModel(const MeshModel& t); ~MeshModel(); //Entity interface - virtual MeshModel *getMeshModel(){ return this; } - virtual Entity *clone(){ return new MeshModel( *this ); } + virtual MeshModel* getMeshModel() + { + return this; + } + virtual Entity* clone() + { + return new MeshModel(*this); + } //Object interface - virtual bool collide( const Line &line,float radius,Collision *curr_coll,const Transform &t ); + virtual bool collide(const Line& line, float radius, Collision* curr_coll, const Transform& t); //Model interface - virtual void setRenderBrush( const Brush &b ); - virtual bool render( const RenderContext &rc ); - virtual void renderQueue( int type ); + virtual void setRenderBrush(const Brush& b); + virtual bool render(const RenderContext& rc); + virtual void renderQueue(int type); //boned mesh! void createBones(); //MeshModel interface - Surface *createSurface( const Brush &b ); - void setCullBox( const Box &box ); - void updateNormals(); - void flipTriangles(); - void transform( const Transform &t ); - void paint( const Brush &b ); - void add( const MeshModel &t ); - void optimize(); + Surface* createSurface(const Brush& b); + void setCullBox(const Box& box); + void updateNormals(); + void flipTriangles(); + void transform(const Transform& t); + void paint(const Brush& b); + void add(const MeshModel& t); + void optimize(); //accessors - const SurfaceList &getSurfaces()const; - Surface *findSurface( const Brush &b )const; - bool intersects( const MeshModel &m )const; - MeshCollider *getCollider()const; - const Box &getBox()const; + const SurfaceList& getSurfaces() const; + Surface* findSurface(const Brush& b) const; + bool intersects(const MeshModel& m) const; + MeshCollider* getCollider() const; + const Box& getBox() const; -private: + private: struct Rep; - Rep *rep; - int brush_changes; - Brush render_brush; + Rep* rep; + int brush_changes; + Brush render_brush; vector brushes; vector surf_bones; - MeshModel &operator=(const MeshModel &); + MeshModel& operator=(const MeshModel&); }; #endif diff --git a/Runtime/blitz3d/meshrenderer.hpp b/Runtime/blitz3d/meshrenderer.hpp index 8dbc58b..4728643 100644 --- a/Runtime/blitz3d/meshrenderer.hpp +++ b/Runtime/blitz3d/meshrenderer.hpp @@ -5,20 +5,20 @@ #include "mesh.hpp" #include "model.hpp" -class MeshRenderer{ -public: - MeshRenderer( const Mesh &t ); +class MeshRenderer { + public: + MeshRenderer(const Mesh& t); ~MeshRenderer(); - void render( Camera *c,Model *m )const; + void render(Camera* c, Model* m) const; -private: + private: Box box; struct Surface; vector surfs; - MeshRenderer( const MeshRenderer &t ); - MeshRenderer &operator=(const MeshRenderer&); + MeshRenderer(const MeshRenderer& t); + MeshRenderer& operator=(const MeshRenderer&); }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/meshutil.cpp b/Runtime/blitz3d/meshutil.cpp index c7dc4f1..b019b5f 100644 --- a/Runtime/blitz3d/meshutil.cpp +++ b/Runtime/blitz3d/meshutil.cpp @@ -1,228 +1,237 @@ -#include "std.hpp" #include "meshutil.hpp" +#include "std.hpp" -MeshModel *MeshUtil::createCube( const Brush &b ){ - static Vector norms[]={ - Vector(0,0,-1),Vector(1,0,0),Vector(0,0,1), - Vector(-1,0,0),Vector(0,1,0),Vector(0,-1,0) - }; - static Vector tex_coords[]={ - Vector(0,0,1),Vector(1,0,1),Vector(1,1,1),Vector(0,1,1) - }; - static int verts[]={ - 2,3,1,0,3,7,5,1,7,6,4,5,6,2,0,4,6,7,3,2,0,1,5,4 - }; - static Box box( Vector(-1,-1,-1),Vector(1,1,1) ); +MeshModel* MeshUtil::createCube(const Brush& b) +{ + static Vector norms[] = {Vector(0, 0, -1), Vector(1, 0, 0), Vector(0, 0, 1), + Vector(-1, 0, 0), Vector(0, 1, 0), Vector(0, -1, 0)}; + static Vector tex_coords[] = {Vector(0, 0, 1), Vector(1, 0, 1), Vector(1, 1, 1), Vector(0, 1, 1)}; + static int verts[] = {2, 3, 1, 0, 3, 7, 5, 1, 7, 6, 4, 5, 6, 2, 0, 4, 6, 7, 3, 2, 0, 1, 5, 4}; + static Box box(Vector(-1, -1, -1), Vector(1, 1, 1)); - MeshModel *m=new MeshModel(); - Surface *s=m->createSurface( b ); - Surface::Vertex v; + MeshModel* m = new MeshModel(); + Surface* s = m->createSurface(b); + Surface::Vertex v; Surface::Triangle t; - for( int k=0;k<24;k+=4 ){ - const Vector &normal=norms[k/4]; - for( int j=0;j<4;++j ){ - v.coords=box.corner( verts[k+j] ); - v.normal=normal; - v.tex_coords[0][0]=v.tex_coords[1][0]=tex_coords[j].x; - v.tex_coords[0][1]=v.tex_coords[1][1]=tex_coords[j].y; - s->addVertex( v ); + for (int k = 0; k < 24; k += 4) { + const Vector& normal = norms[k / 4]; + for (int j = 0; j < 4; ++j) { + v.coords = box.corner(verts[k + j]); + v.normal = normal; + v.tex_coords[0][0] = v.tex_coords[1][0] = tex_coords[j].x; + v.tex_coords[0][1] = v.tex_coords[1][1] = tex_coords[j].y; + s->addVertex(v); } - t.verts[0]=k;t.verts[1]=k+1;t.verts[2]=k+2;s->addTriangle(t); - t.verts[1]=k+2;t.verts[2]=k+3;s->addTriangle(t); + t.verts[0] = k; + t.verts[1] = k + 1; + t.verts[2] = k + 2; + s->addTriangle(t); + t.verts[1] = k + 2; + t.verts[2] = k + 3; + s->addTriangle(t); } return m; } -MeshModel *MeshUtil::createSphere( const Brush &b,int segs ){ +MeshModel* MeshUtil::createSphere(const Brush& b, int segs) +{ + int h_segs = segs * 2, v_segs = segs; - int h_segs=segs*2,v_segs=segs; + MeshModel* m = new MeshModel(); + Surface* s = m->createSurface(b); - MeshModel *m=new MeshModel(); - Surface *s=m->createSurface( b ); - - Surface::Vertex v; + Surface::Vertex v; Surface::Triangle t; - v.coords=v.normal=Vector(0,1,0); + v.coords = v.normal = Vector(0, 1, 0); int k; - for( k=0;kaddVertex( v ); - } - for( k=1;kaddVertex( v ); - } - } - v.coords=v.normal=Vector(0,-1,0); - for( k=0;kaddVertex( v ); - } - for( k=0;kaddTriangle( t ); - } - for( k=1;kaddTriangle( t ); - t.verts[1]=t.verts[2]; - t.verts[2]=t.verts[1]-1; - s->addTriangle( t ); - } - } - for( k=0;kaddTriangle( t ); - } - - return m; -} - -MeshModel *MeshUtil::createCylinder( const Brush &b,int segs,bool solid ){ - - MeshModel *m=new MeshModel(); - Surface::Vertex v; - Surface::Triangle t; - - Surface *s=m->createSurface( b ); - int k; - for( k=0;k<=segs;++k ){ - float yaw=(k%segs)*TWOPI/segs; - v.coords=rotationMatrix( 0,yaw,0 ).k; - v.coords.y=1; - v.normal=Vector(v.coords.x,0,v.coords.z); - v.tex_coords[0][0]=v.tex_coords[1][0]=float(k)/segs; - v.tex_coords[0][1]=v.tex_coords[1][1]=0; - s->addVertex( v ); - v.coords.y=-1; - v.tex_coords[0][0]=v.tex_coords[1][0]=float(k)/segs; - v.tex_coords[0][1]=v.tex_coords[1][1]=1; - s->addVertex( v ); - } - for( k=0;kaddTriangle( t ); - t.verts[1]=t.verts[2]; - t.verts[2]=t.verts[1]-2; - s->addTriangle( t ); - } - - if( !solid ) return m; - - s=m->createSurface( b ); - - for( k=0;kaddVertex(v); - v.coords.y=-1;v.normal=Vector( 0,-1,0 ); + for (k = 0; k < h_segs; ++k) { + v.tex_coords[0][0] = v.tex_coords[1][0] = (k + .5f) / h_segs; + v.tex_coords[0][1] = v.tex_coords[1][1] = 0; s->addVertex(v); } - for( k=2;kaddTriangle( t ); - t.verts[0]=1; - t.verts[1]=(k-1)*2+1; - t.verts[2]=k*2+1; - s->addTriangle( t ); + for (k = 1; k < v_segs; ++k) { + float pitch = k * PI / v_segs - HALFPI; + for (int j = 0; j <= h_segs; ++j) { + float yaw = (j % h_segs) * TWOPI / h_segs; + v.coords = v.normal = rotationMatrix(pitch, yaw, 0).k; + v.tex_coords[0][0] = v.tex_coords[1][0] = float(j) / float(h_segs); + v.tex_coords[0][1] = v.tex_coords[1][1] = float(k) / float(v_segs); + s->addVertex(v); + } + } + v.coords = v.normal = Vector(0, -1, 0); + for (k = 0; k < h_segs; ++k) { + v.tex_coords[0][0] = v.tex_coords[1][0] = (k + .5f) / h_segs; + v.tex_coords[0][1] = v.tex_coords[1][1] = 1; + s->addVertex(v); + } + for (k = 0; k < h_segs; ++k) { + t.verts[0] = k; + t.verts[1] = t.verts[0] + h_segs + 1; + t.verts[2] = t.verts[1] - 1; + s->addTriangle(t); + } + for (k = 1; k < v_segs - 1; ++k) { + for (int j = 0; j < h_segs; ++j) { + t.verts[0] = k * (h_segs + 1) + j - 1; + t.verts[1] = t.verts[0] + 1; + t.verts[2] = t.verts[1] + h_segs + 1; + s->addTriangle(t); + t.verts[1] = t.verts[2]; + t.verts[2] = t.verts[1] - 1; + s->addTriangle(t); + } + } + for (k = 0; k < h_segs; ++k) { + t.verts[0] = (h_segs + 1) * (v_segs - 1) + k - 1; + t.verts[1] = t.verts[0] + 1; + t.verts[2] = t.verts[1] + h_segs; + s->addTriangle(t); } return m; } -MeshModel *MeshUtil::createCone( const Brush &b,int segs,bool solid ){ - MeshModel *m=new MeshModel(); - Surface::Vertex v; +MeshModel* MeshUtil::createCylinder(const Brush& b, int segs, bool solid) +{ + MeshModel* m = new MeshModel(); + Surface::Vertex v; Surface::Triangle t; - Surface *s; - s=m->createSurface( b ); + Surface* s = m->createSurface(b); + int k; + for (k = 0; k <= segs; ++k) { + float yaw = (k % segs) * TWOPI / segs; + v.coords = rotationMatrix(0, yaw, 0).k; + v.coords.y = 1; + v.normal = Vector(v.coords.x, 0, v.coords.z); + v.tex_coords[0][0] = v.tex_coords[1][0] = float(k) / segs; + v.tex_coords[0][1] = v.tex_coords[1][1] = 0; + s->addVertex(v); + v.coords.y = -1; + v.tex_coords[0][0] = v.tex_coords[1][0] = float(k) / segs; + v.tex_coords[0][1] = v.tex_coords[1][1] = 1; + s->addVertex(v); + } + for (k = 0; k < segs; ++k) { + t.verts[0] = k * 2; + t.verts[1] = t.verts[0] + 2; + t.verts[2] = t.verts[1] + 1; + s->addTriangle(t); + t.verts[1] = t.verts[2]; + t.verts[2] = t.verts[1] - 2; + s->addTriangle(t); + } + + if (!solid) + return m; + + s = m->createSurface(b); + + for (k = 0; k < segs; ++k) { + float yaw = k * TWOPI / segs; + v.coords = rotationMatrix(0, yaw, 0).k; + v.coords.y = 1; + v.normal = Vector(0, 1, 0); + v.tex_coords[0][0] = v.tex_coords[1][0] = v.coords.x * .5f + .5f; + v.tex_coords[0][1] = v.tex_coords[1][1] = v.coords.z * .5f + .5f; + s->addVertex(v); + v.coords.y = -1; + v.normal = Vector(0, -1, 0); + s->addVertex(v); + } + for (k = 2; k < segs; ++k) { + t.verts[0] = 0; + t.verts[1] = k * 2; + t.verts[2] = (k - 1) * 2; + s->addTriangle(t); + t.verts[0] = 1; + t.verts[1] = (k - 1) * 2 + 1; + t.verts[2] = k * 2 + 1; + s->addTriangle(t); + } + + return m; +} + +MeshModel* MeshUtil::createCone(const Brush& b, int segs, bool solid) +{ + MeshModel* m = new MeshModel(); + Surface::Vertex v; + Surface::Triangle t; + + Surface* s; + s = m->createSurface(b); int k; - v.coords=v.normal=Vector(0,1,0); - for( k=0;kaddVertex( v ); + v.coords = v.normal = Vector(0, 1, 0); + for (k = 0; k < segs; ++k) { + v.tex_coords[0][0] = v.tex_coords[1][0] = (k + .5f) / segs; + v.tex_coords[0][1] = v.tex_coords[1][1] = 0; + s->addVertex(v); } - for( k=0;k<=segs;++k ){ - float yaw=(k%segs)*TWOPI/segs; - v.coords=yawMatrix( yaw ).k;v.coords.y=-1; - v.normal=Vector( v.coords.x,0,v.coords.z ); - v.tex_coords[0][0]=v.tex_coords[1][0]=float(k)/segs; - v.tex_coords[0][1]=v.tex_coords[1][1]=1; - s->addVertex( v ); + for (k = 0; k <= segs; ++k) { + float yaw = (k % segs) * TWOPI / segs; + v.coords = yawMatrix(yaw).k; + v.coords.y = -1; + v.normal = Vector(v.coords.x, 0, v.coords.z); + v.tex_coords[0][0] = v.tex_coords[1][0] = float(k) / segs; + v.tex_coords[0][1] = v.tex_coords[1][1] = 1; + s->addVertex(v); } - for( k=0;kaddTriangle( t ); + for (k = 0; k < segs; ++k) { + t.verts[0] = k; + t.verts[1] = k + segs + 1; + t.verts[2] = k + segs; + s->addTriangle(t); } - if( !solid ) return m; - s=m->createSurface( b ); - for( k=0;kaddVertex( v ); + if (!solid) + return m; + s = m->createSurface(b); + for (k = 0; k < segs; ++k) { + float yaw = k * TWOPI / segs; + v.coords = yawMatrix(yaw).k; + v.coords.y = -1; + v.normal = Vector(v.coords.x, 0, v.coords.z); + v.tex_coords[0][0] = v.tex_coords[1][0] = v.coords.x * .5f + .5f; + v.tex_coords[0][1] = v.tex_coords[1][1] = v.coords.z * .5f + .5f; + s->addVertex(v); } - t.verts[0]=0; - for( k=2;kaddTriangle( t ); + t.verts[0] = 0; + for (k = 2; k < segs; ++k) { + t.verts[1] = k - 1; + t.verts[2] = k; + s->addTriangle(t); } return m; } -void MeshUtil::lightMesh( MeshModel *m,const Vector &pos,const Vector &rgb,float range ){ - if( range ){ - float att=1.0f/range; - const MeshModel::SurfaceList &surfs=m->getSurfaces(); - for( int k=0;knumVertices();++j ){ - const Surface::Vertex &v=s->getVertex( j ); - Vector lv=pos-v.coords; - float dp=v.normal.normalized().dot( lv ); - if( dp<=0 ) continue; - float d=lv.length(); - float i=1/(d*att)*(dp/d); - s->setColor( j,s->getColor(j)+rgb*i ); +void MeshUtil::lightMesh(MeshModel* m, const Vector& pos, const Vector& rgb, float range) +{ + if (range) { + float att = 1.0f / range; + const MeshModel::SurfaceList& surfs = m->getSurfaces(); + for (int k = 0; k < surfs.size(); ++k) { + Surface* s = surfs[k]; + for (int j = 0; j < s->numVertices(); ++j) { + const Surface::Vertex& v = s->getVertex(j); + Vector lv = pos - v.coords; + float dp = v.normal.normalized().dot(lv); + if (dp <= 0) + continue; + float d = lv.length(); + float i = 1 / (d * att) * (dp / d); + s->setColor(j, s->getColor(j) + rgb * i); } } - }else{ - const MeshModel::SurfaceList &surfs=m->getSurfaces(); - for( int k=0;knumVertices();++j ){ - const Surface::Vertex &v=s->getVertex( j ); - s->setColor( j,s->getColor(j)+rgb ); + } else { + const MeshModel::SurfaceList& surfs = m->getSurfaces(); + for (int k = 0; k < surfs.size(); ++k) { + Surface* s = surfs[k]; + for (int j = 0; j < s->numVertices(); ++j) { + const Surface::Vertex& v = s->getVertex(j); + s->setColor(j, s->getColor(j) + rgb); } } } diff --git a/Runtime/blitz3d/meshutil.hpp b/Runtime/blitz3d/meshutil.hpp index 0ebda71..4f949e0 100644 --- a/Runtime/blitz3d/meshutil.hpp +++ b/Runtime/blitz3d/meshutil.hpp @@ -4,14 +4,13 @@ #include "meshmodel.hpp" -struct MeshUtil{ +struct MeshUtil { + static MeshModel* createCube(const Brush& b); + static MeshModel* createSphere(const Brush& b, int segs); + static MeshModel* createCylinder(const Brush& b, int segs, bool solid); + static MeshModel* createCone(const Brush& b, int segs, bool solid); - static MeshModel *createCube( const Brush &b ); - static MeshModel *createSphere( const Brush &b,int segs ); - static MeshModel *createCylinder( const Brush &b,int segs,bool solid ); - static MeshModel *createCone( const Brush &b,int segs,bool solid ); - - static void lightMesh( MeshModel *m,const Vector &pos,const Vector &rgb,float range ); + static void lightMesh(MeshModel* m, const Vector& pos, const Vector& rgb, float range); /* static void flipMesh( Mesh *m ); diff --git a/Runtime/blitz3d/mirror.cpp b/Runtime/blitz3d/mirror.cpp index 9aa46b0..0e25d0e 100644 --- a/Runtime/blitz3d/mirror.cpp +++ b/Runtime/blitz3d/mirror.cpp @@ -1,13 +1,9 @@ -#include "std.hpp" #include "mirror.hpp" +#include "std.hpp" -Mirror::Mirror(){ -} +Mirror::Mirror() {} -Mirror::Mirror( const Mirror &t ): -Object(t){ -} +Mirror::Mirror(const Mirror& t) : Object(t) {} -Mirror::~Mirror(){ -} \ No newline at end of file +Mirror::~Mirror() {} \ No newline at end of file diff --git a/Runtime/blitz3d/mirror.hpp b/Runtime/blitz3d/mirror.hpp index cc3f06d..e2dcb75 100644 --- a/Runtime/blitz3d/mirror.hpp +++ b/Runtime/blitz3d/mirror.hpp @@ -4,15 +4,21 @@ #include "object.hpp" -class Mirror : public Object{ -public: +class Mirror : public Object { + public: Mirror(); - Mirror( const Mirror &t ); + Mirror(const Mirror& t); ~Mirror(); //Entity interface - Entity *clone(){ return new Mirror( *this ); } - Mirror *getMirror(){ return this; } + Entity* clone() + { + return new Mirror(*this); + } + Mirror* getMirror() + { + return this; + } }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/model.cpp b/Runtime/blitz3d/model.cpp index 74fe4bd..52186ed 100644 --- a/Runtime/blitz3d/model.cpp +++ b/Runtime/blitz3d/model.cpp @@ -1,128 +1,140 @@ -#include "std.hpp" #include "model.hpp" +#include "std.hpp" -extern gxScene *gx_scene; +extern gxScene* gx_scene; -class Model::MeshQueue{ - union{ - gxMesh *mesh; - MeshQueue *next; +class Model::MeshQueue { + union { + gxMesh* mesh; + MeshQueue* next; }; - int fv,vc,ft,tc; + int fv, vc, ft, tc; Brush brush; - int q_type; -// bool opaque; + int q_type; + // bool opaque; - static MeshQueue *pool; + static MeshQueue* pool; -public: - MeshQueue(){} + public: + MeshQueue() {} - MeshQueue( gxMesh *m,int fv,int vc,int ft,int tc,const Brush &b ): - mesh(m),fv(fv),vc(vc),ft(ft),tc(tc),brush(b){ - int n=brush.getBlend(); - q_type=(n==gxScene::BLEND_REPLACE) ? QUEUE_OPAQUE : QUEUE_TRANSPARENT; + MeshQueue(gxMesh* m, int fv, int vc, int ft, int tc, const Brush& b) + : mesh(m), fv(fv), vc(vc), ft(ft), tc(tc), brush(b) + { + int n = brush.getBlend(); + q_type = (n == gxScene::BLEND_REPLACE) ? QUEUE_OPAQUE : QUEUE_TRANSPARENT; } - int getQueueType()const{ + int getQueueType() const + { return q_type; } - void render(){ - gx_scene->setRenderState( brush.getRenderState() ); - gx_scene->render( mesh,fv,vc,ft,tc ); + void render() + { + gx_scene->setRenderState(brush.getRenderState()); + gx_scene->render(mesh, fv, vc, ft, tc); } - void *operator new( size_t sz ){ - static const int GROW=256; - if( !pool ){ - pool=new MeshQueue[GROW]; - for( int k=0;knext; + MeshQueue* t = pool; + pool = t->next; return t; } - void operator delete( void *q ){ - MeshQueue *t=(MeshQueue*)q; - t->next=pool; - pool=t; + void operator delete(void* q) + { + MeshQueue* t = (MeshQueue*)q; + t->next = pool; + pool = t; } }; -Model::MeshQueue *Model::MeshQueue::pool; +Model::MeshQueue* Model::MeshQueue::pool; -Model::Model(): -space( RENDER_SPACE_LOCAL ), -auto_fade(false), -captured_alpha(1),w_brush(true){ -} +Model::Model() : space(RENDER_SPACE_LOCAL), auto_fade(false), captured_alpha(1), w_brush(true) {} -Model::Model( const Model &t ):Object(t), -space(t.space),brush(t.brush), -auto_fade(t.auto_fade),auto_fade_nr(t.auto_fade_nr),auto_fade_fr(t.auto_fade_fr), -captured_alpha(t.captured_alpha),w_brush(true){ -} +Model::Model(const Model& t) + : Object(t), space(t.space), brush(t.brush), auto_fade(t.auto_fade), auto_fade_nr(t.auto_fade_nr), + auto_fade_fr(t.auto_fade_fr), captured_alpha(t.captured_alpha), w_brush(true) +{} -void Model::capture(){ +void Model::capture() +{ Object::capture(); - captured_alpha=brush.getAlpha(); + captured_alpha = brush.getAlpha(); } -bool Model::beginRender( float t ){ - Object::beginRender( t ); - tweened_alpha=brush.getAlpha(); - if( t!=1 && tweened_alpha!=captured_alpha ){ +bool Model::beginRender(float t) +{ + Object::beginRender(t); + tweened_alpha = brush.getAlpha(); + if (t != 1 && tweened_alpha != captured_alpha) { // //render tweening of alpha // - tweened_alpha=(tweened_alpha-captured_alpha)*t+captured_alpha; + tweened_alpha = (tweened_alpha - captured_alpha) * t + captured_alpha; } - return tweened_alpha>0; + return tweened_alpha > 0; } -bool Model::doAutoFade( const Vector &eye ){ - float alpha=tweened_alpha; - if( auto_fade ){ +bool Model::doAutoFade(const Vector& eye) +{ + float alpha = tweened_alpha; + if (auto_fade) { // //autofading of alpha // - float d=eye.distance( getRenderTform().v ); - if( d>=auto_fade_fr ) return false; - if( d>=auto_fade_nr ){ - float t=1-(d-auto_fade_nr)/(auto_fade_fr-auto_fade_nr ); - alpha*=t;if( alpha<=0 ) return false; + float d = eye.distance(getRenderTform().v); + if (d >= auto_fade_fr) + return false; + if (d >= auto_fade_nr) { + float t = 1 - (d - auto_fade_nr) / (auto_fade_fr - auto_fade_nr); + alpha *= t; + if (alpha <= 0) + return false; } } - if( w_brush ) render_brush=brush; + if (w_brush) + render_brush = brush; - if( alpha!=render_brush.getAlpha() ){ - render_brush.setAlpha( alpha ); - }else if( !w_brush ){ + if (alpha != render_brush.getAlpha()) { + render_brush.setAlpha(alpha); + } else if (!w_brush) { return true; } - setRenderBrush( render_brush ); - w_brush=false; + setRenderBrush(render_brush); + w_brush = false; return true; } -void Model::enqueue( MeshQueue *q ){ - queues[q->getQueueType()].push_back( q ); +void Model::enqueue(MeshQueue* q) +{ + queues[q->getQueueType()].push_back(q); } -void Model::enqueue( gxMesh *mesh,int fv,int vc,int ft,int tc ){ - enqueue( new MeshQueue( mesh,fv,vc,ft,tc,render_brush ) ); +void Model::enqueue(gxMesh* mesh, int fv, int vc, int ft, int tc) +{ + enqueue(new MeshQueue(mesh, fv, vc, ft, tc, render_brush)); } -void Model::enqueue( gxMesh *mesh,int fv,int vc,int ft,int tc,const Brush &brush ){ - enqueue( new MeshQueue( mesh,fv,vc,ft,tc,brush ) ); +void Model::enqueue(gxMesh* mesh, int fv, int vc, int ft, int tc, const Brush& brush) +{ + enqueue(new MeshQueue(mesh, fv, vc, ft, tc, brush)); } -void Model::renderQueue( int type ){ - vector *que=&queues[type]; - for( ;que->size();que->pop_back() ){ - MeshQueue *q=que->back(); +void Model::renderQueue(int type) +{ + vector* que = &queues[type]; + for (; que->size(); que->pop_back()) { + MeshQueue* q = que->back(); q->render(); delete q; } diff --git a/Runtime/blitz3d/model.hpp b/Runtime/blitz3d/model.hpp index bff3a08..9c4209b 100644 --- a/Runtime/blitz3d/model.hpp +++ b/Runtime/blitz3d/model.hpp @@ -11,82 +11,145 @@ class Terrain; class PlaneModel; class Q3BSPModel; -class Model : public Object{ -public: - enum{ - RENDER_SPACE_LOCAL=0, - RENDER_SPACE_WORLD=1 - }; - enum{ - COLLISION_GEOMETRY_DEFAULT=0, - COLLISION_GEOMETRY_TRIS=1, - COLLISION_GEOMETRY_BOX=2, - COLLISION_GEOMETRY_SPHERE=3 - }; - enum{ - QUEUE_OPAQUE=0, - QUEUE_TRANSPARENT=1 +class Model : public Object { + public: + enum { RENDER_SPACE_LOCAL = 0, RENDER_SPACE_WORLD = 1 }; + enum { + COLLISION_GEOMETRY_DEFAULT = 0, + COLLISION_GEOMETRY_TRIS = 1, + COLLISION_GEOMETRY_BOX = 2, + COLLISION_GEOMETRY_SPHERE = 3 }; + enum { QUEUE_OPAQUE = 0, QUEUE_TRANSPARENT = 1 }; Model(); - Model( const Model &m ); + Model(const Model& m); //Entity interface - Model *getModel(){ return this; } + Model* getModel() + { + return this; + } //Object interface void capture(); - bool beginRender( float tween ); + bool beginRender(float tween); //Model interface - virtual void setRenderBrush( const Brush &b ){} - virtual bool render( const RenderContext &rc ){ return false; } - virtual void renderQueue( int type ); + virtual void setRenderBrush(const Brush& b) {} + virtual bool render(const RenderContext& rc) + { + return false; + } + virtual void renderQueue(int type); - virtual Sprite *getSprite(){ return 0; } - virtual Terrain *getTerrain(){ return 0; } - virtual PlaneModel *getPlaneModel(){ return 0; } - virtual MeshModel *getMeshModel(){ return 0; } - virtual MD2Model *getMD2Model(){ return 0; } - virtual Q3BSPModel *getBSPModel(){ return 0; } + virtual Sprite* getSprite() + { + return 0; + } + virtual Terrain* getTerrain() + { + return 0; + } + virtual PlaneModel* getPlaneModel() + { + return 0; + } + virtual MeshModel* getMeshModel() + { + return 0; + } + virtual MD2Model* getMD2Model() + { + return 0; + } + virtual Q3BSPModel* getBSPModel() + { + return 0; + } - virtual void setBrush( const Brush &b ){ brush=b;w_brush=true; } - virtual void setColor( const Vector &c ){ brush.setColor(c);w_brush=true; } - virtual void setAlpha( float a ){ brush.setAlpha(a);w_brush=true; } - virtual void setShininess( float t ){ brush.setShininess(t);w_brush=true; } - virtual void setTexture( int i,const Texture &t,int f ){ brush.setTexture(i,t,f);w_brush=true; } - virtual void setBlend( int n ){ brush.setBlend(n);w_brush=true; } - virtual void setFX( int n ){ brush.setFX(n);w_brush=true; } + virtual void setBrush(const Brush& b) + { + brush = b; + w_brush = true; + } + virtual void setColor(const Vector& c) + { + brush.setColor(c); + w_brush = true; + } + virtual void setAlpha(float a) + { + brush.setAlpha(a); + w_brush = true; + } + virtual void setShininess(float t) + { + brush.setShininess(t); + w_brush = true; + } + virtual void setTexture(int i, const Texture& t, int f) + { + brush.setTexture(i, t, f); + w_brush = true; + } + virtual void setBlend(int n) + { + brush.setBlend(n); + w_brush = true; + } + virtual void setFX(int n) + { + brush.setFX(n); + w_brush = true; + } - const Brush &getBrush()const{ return brush; } + const Brush& getBrush() const + { + return brush; + } - void setRenderSpace( int n ){ space=n; } - int getRenderSpace()const{ return space; } + void setRenderSpace(int n) + { + space = n; + } + int getRenderSpace() const + { + return space; + } - void setAutoFade( float nr,float fr ){ auto_fade_nr=nr;auto_fade_fr=fr;auto_fade=true; } + void setAutoFade(float nr, float fr) + { + auto_fade_nr = nr; + auto_fade_fr = fr; + auto_fade = true; + } - bool doAutoFade( const Vector &eye ); + bool doAutoFade(const Vector& eye); - void enqueue( gxMesh *mesh,int first_vert,int vert_cnt,int first_tri,int tri_cnt ); - void enqueue( gxMesh *mesh,int first_vert,int vert_cnt,int first_tri,int tri_cnt,const Brush &b ); + void enqueue(gxMesh* mesh, int first_vert, int vert_cnt, int first_tri, int tri_cnt); + void enqueue(gxMesh* mesh, int first_vert, int vert_cnt, int first_tri, int tri_cnt, const Brush& b); - int queueSize( int type )const{ return queues[type].size(); } + int queueSize(int type) const + { + return queues[type].size(); + } -private: + private: class MeshQueue; - int space; - Brush brush,render_brush; + int space; + Brush brush, render_brush; mutable bool w_brush; - float captured_alpha,tweened_alpha; + float captured_alpha, tweened_alpha; - bool auto_fade; - float auto_fade_nr,auto_fade_fr; + bool auto_fade; + float auto_fade_nr, auto_fade_fr; vector queues[2]; - void enqueue( MeshQueue *q ); + void enqueue(MeshQueue* q); }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/object.cpp b/Runtime/blitz3d/object.cpp index d99e8f2..8e3d715 100644 --- a/Runtime/blitz3d/object.cpp +++ b/Runtime/blitz3d/object.cpp @@ -1,162 +1,191 @@ -#include "std.hpp" #include "object.hpp" +#include "std.hpp" -extern gxRuntime *gx_runtime; +extern gxRuntime* gx_runtime; -Object::Object(): -order(0),animator(0),last_copy(0), -coll_type(0),coll_radii(Vector(1,1,1)),coll_box(Box(Vector(-1,-1,-1),Vector(1,1,1))), -pick_geom(0),obscurer(false),captured(false){ +Object::Object() + : order(0), animator(0), last_copy(0), coll_type(0), coll_radii(Vector(1, 1, 1)), + coll_box(Box(Vector(-1, -1, -1), Vector(1, 1, 1))), pick_geom(0), obscurer(false), captured(false) +{ reset(); } -Object::Object( const Object &o ): -Entity(o), -order(o.order),animator(0),last_copy(0), -coll_type(o.coll_type),coll_radii(o.coll_radii),coll_box(o.coll_box), -pick_geom(o.pick_geom),obscurer(o.obscurer),captured(false){ +Object::Object(const Object& o) + : Entity(o), order(o.order), animator(0), last_copy(0), coll_type(o.coll_type), coll_radii(o.coll_radii), + coll_box(o.coll_box), pick_geom(o.pick_geom), obscurer(o.obscurer), captured(false) +{ reset(); } -Object::~Object(){ +Object::~Object() +{ delete animator; - velocity=Vector(); + velocity = Vector(); updateSounds(); } -Object *Object::copy(){ - last_copy=clone()->getObject(); - for( Entity *e=GetChildren();e;e=e->GetSuccessor() ){ - Object *cpy=e->getObject()->copy(); - cpy->SetParent( last_copy ); +Object* Object::copy() +{ + last_copy = clone()->getObject(); + for (Entity* e = GetChildren(); e; e = e->GetSuccessor()) { + Object* cpy = e->getObject()->copy(); + cpy->SetParent(last_copy); } - if( animator ) last_copy->setAnimator( new Animator( animator ) ); + if (animator) + last_copy->setAnimator(new Animator(animator)); return last_copy; } -void Object::reset(){ +void Object::reset() +{ colls.clear(); - velocity=Vector(); - prev_tform=GetWorldTransform(); + velocity = Vector(); + prev_tform = GetWorldTransform(); } -void Object::setCollisionType( int type ){ - coll_type=type; +void Object::setCollisionType(int type) +{ + coll_type = type; } -void Object::setCollisionRadii( const Vector &radii ){ - coll_radii=radii; +void Object::setCollisionRadii(const Vector& radii) +{ + coll_radii = radii; } -void Object::setCollisionBox( const Box &box ){ - coll_box=box; +void Object::setCollisionBox(const Box& box) +{ + coll_box = box; } -void Object::setAnimator( Animator *t ){ - if( animator ) delete animator; - animator=t; +void Object::setAnimator(Animator* t) +{ + if (animator) + delete animator; + animator = t; } -void Object::beginUpdate( float e ){ - elapsed=e; +void Object::beginUpdate(float e) +{ + elapsed = e; colls.clear(); - animate( e ); + animate(e); } -void Object::animate( float e ){ - if( animator ) animator->update( e ); +void Object::animate(float e) +{ + if (animator) + animator->update(e); } -void Object::addCollision( const ObjCollision *c ){ - colls.push_back( c ); +void Object::addCollision(const ObjCollision* c) +{ + colls.push_back(c); } -void Object::endUpdate(){ - velocity=(GetWorldTransform().v-prev_tform.v)/elapsed; - prev_tform=GetWorldTransform(); +void Object::endUpdate() +{ + velocity = (GetWorldTransform().v - prev_tform.v) / elapsed; + prev_tform = GetWorldTransform(); } -void Object::capture(){ - capt_pos=GetLocalPosition(); - capt_scl=GetLocalScale(); - capt_rot=GetLocalRotation(); - captured=true; +void Object::capture() +{ + capt_pos = GetLocalPosition(); + capt_scl = GetLocalScale(); + capt_rot = GetLocalRotation(); + captured = true; } -bool Object::beginRender( float tween ){ +bool Object::beginRender(float tween) +{ updateSounds(); - if( tween==1 || !captured ){ - render_tform=GetWorldTransform(); - render_tform_valid=true; - }else{ - Vector pos=(GetLocalPosition()-capt_pos)*tween+capt_pos; - Vector scl=(GetLocalScale()-capt_scl)*tween+capt_scl; - Quat rot=capt_rot.slerpTo( GetLocalRotation(),tween ); - tween_tform.m=Matrix( rot ); - tween_tform.m.i*=scl.x; - tween_tform.m.j*=scl.y; - tween_tform.m.k*=scl.z; - tween_tform.v=pos; - render_tform_valid=false; + if (tween == 1 || !captured) { + render_tform = GetWorldTransform(); + render_tform_valid = true; + } else { + Vector pos = (GetLocalPosition() - capt_pos) * tween + capt_pos; + Vector scl = (GetLocalScale() - capt_scl) * tween + capt_scl; + Quat rot = capt_rot.slerpTo(GetLocalRotation(), tween); + tween_tform.m = Matrix(rot); + tween_tform.m.i *= scl.x; + tween_tform.m.j *= scl.y; + tween_tform.m.k *= scl.z; + tween_tform.v = pos; + render_tform_valid = false; } return true; } -void Object::endRender(){ -} +void Object::endRender() {} -int Object::getCollisionType()const{ +int Object::getCollisionType() const +{ return coll_type; } -const Vector &Object::getCollisionRadii()const{ +const Vector& Object::getCollisionRadii() const +{ return coll_radii; } -const Box &Object::getCollisionBox()const{ +const Box& Object::getCollisionBox() const +{ return coll_box; } -const Vector &Object::getVelocity()const{ +const Vector& Object::getVelocity() const +{ return velocity; } -const Object::Collisions &Object::getCollisions()const{ +const Object::Collisions& Object::getCollisions() const +{ return colls; } -const Transform &Object::getRenderTform()const{ - if( render_tform_valid ) return render_tform; +const Transform& Object::getRenderTform() const +{ + if (render_tform_valid) + return render_tform; - Object *parent=(Object*)getParent(); - render_tform=parent ? parent->getRenderTform() * tween_tform : tween_tform; - render_tform_valid=true; + Object* parent = (Object*)getParent(); + render_tform = parent ? parent->getRenderTform() * tween_tform : tween_tform; + render_tform_valid = true; return render_tform; } -const Transform &Object::getPrevWorldTform()const{ +const Transform& Object::getPrevWorldTform() const +{ return prev_tform; } -gxChannel *Object::emitSound( gxSound *sound ){ - if( !sound ) return 0; +gxChannel* Object::emitSound(gxSound* sound) +{ + if (!sound) + return 0; - gxChannel *chan=sound->play3d( &GetWorldTransform().v.x,&velocity.x ); - for( int k=0;kplay3d(&GetWorldTransform().v.x, &velocity.x); + for (int k = 0; k < channels.size(); ++k) { + if (chan == channels[k]) + return chan; + if (!channels[k]) + return channels[k] = chan; } - channels.push_back( chan ); + channels.push_back(chan); return chan; } -void Object::updateSounds(){ - for( int k=0;kisPlaying() ) chan->set3d( &GetWorldTransform().v.x,&velocity.x ); - else channels[k]=0; +void Object::updateSounds() +{ + for (int k = 0; k < channels.size(); ++k) { + if (gxChannel* chan = channels[k]) { + if (chan->isPlaying()) + chan->set3d(&GetWorldTransform().v.x, &velocity.x); + else + channels[k] = 0; } } } diff --git a/Runtime/blitz3d/object.hpp b/Runtime/blitz3d/object.hpp index 5840b55..0f1bb4e 100644 --- a/Runtime/blitz3d/object.hpp +++ b/Runtime/blitz3d/object.hpp @@ -4,96 +4,135 @@ #include -#include "entity.hpp" #include "animator.hpp" #include "collision.hpp" +#include "entity.hpp" class gxSound; -struct ObjCollision{ - Object *with; - Vector coords; +struct ObjCollision { + Object* with; + Vector coords; Collision collision; }; -class Object : public Entity{ -public: +class Object : public Entity { + public: typedef std::vector Collisions; Object(); - Object( const Object &object ); + Object(const Object& object); ~Object(); //Entity interface - Object *getObject(){ return this; } - Entity *clone(){ return new Object( *this ); } + Object* getObject() + { + return this; + } + Entity* clone() + { + return new Object(*this); + } //deep object copy! - Object *copy(); + Object* copy(); //called by user void reset(); - void setCollisionType( int type ); - void setCollisionRadii( const Vector &radii ); - void setCollisionBox( const Box &box ); - void setOrder( int n ){ order=n; } - void setPickGeometry( int n ){ pick_geom=n; } - void setObscurer( bool t ){ obscurer=t; } - void setAnimation( const Animation &t ){ anim=t; } - void setAnimator( Animator *t ); + void setCollisionType(int type); + void setCollisionRadii(const Vector& radii); + void setCollisionBox(const Box& box); + void setOrder(int n) + { + order = n; + } + void setPickGeometry(int n) + { + pick_geom = n; + } + void setObscurer(bool t) + { + obscurer = t; + } + void setAnimation(const Animation& t) + { + anim = t; + } + void setAnimator(Animator* t); - gxChannel *emitSound( gxSound *sound ); + gxChannel* emitSound(gxSound* sound); //overridables! - virtual bool collide( const Line &line,float radius,::Collision *curr_coll,const Transform &t ){ return false; } + virtual bool collide(const Line& line, float radius, ::Collision* curr_coll, const Transform& t) + { + return false; + } virtual void capture(); - virtual void animate( float e ); - virtual bool beginRender( float tween ); + virtual void animate(float e); + virtual bool beginRender(float tween); virtual void endRender(); //for use by world - void beginUpdate( float elapsed ); - void addCollision( const ObjCollision *c ); + void beginUpdate(float elapsed); + void addCollision(const ObjCollision* c); void endUpdate(); //accessors - int getCollisionType()const; - const Vector &getCollisionRadii()const; - const Box &getCollisionBox()const; - int getOrder()const{ return order; } - const Vector &getVelocity()const; - const Collisions &getCollisions()const; - const Transform &getRenderTform()const; - const Transform &getPrevWorldTform()const; - int getPickGeometry()const{ return pick_geom; } - int getObscurer()const{ return obscurer; } - Animation getAnimation()const{ return anim; } - Animator *getAnimator()const{ return animator; } - Object *getLastCopy()const{ return last_copy; } + int getCollisionType() const; + const Vector& getCollisionRadii() const; + const Box& getCollisionBox() const; + int getOrder() const + { + return order; + } + const Vector& getVelocity() const; + const Collisions& getCollisions() const; + const Transform& getRenderTform() const; + const Transform& getPrevWorldTform() const; + int getPickGeometry() const + { + return pick_geom; + } + int getObscurer() const + { + return obscurer; + } + Animation getAnimation() const + { + return anim; + } + Animator* getAnimator() const + { + return animator; + } + Object* getLastCopy() const + { + return last_copy; + } -private: - int coll_type; - int order; - Vector coll_radii; - Collisions colls; - bool captured; - Box coll_box; - int pick_geom; - bool obscurer; - float elapsed; - Vector velocity; + private: + int coll_type; + int order; + Vector coll_radii; + Collisions colls; + bool captured; + Box coll_box; + int pick_geom; + bool obscurer; + float elapsed; + Vector velocity; vector channels; - Vector capt_pos,capt_scl; - Quat capt_rot; - mutable Object *last_copy; + Vector capt_pos, capt_scl; + Quat capt_rot; + mutable Object* last_copy; - Transform prev_tform; - Transform captured_tform,tween_tform; + Transform prev_tform; + Transform captured_tform, tween_tform; mutable Transform render_tform; - mutable bool render_tform_valid; + mutable bool render_tform_valid; Animation anim; - Animator *animator; + Animator* animator; void updateSounds(); }; diff --git a/Runtime/blitz3d/pivot.cpp b/Runtime/blitz3d/pivot.cpp index 77e5497..de6a007 100644 --- a/Runtime/blitz3d/pivot.cpp +++ b/Runtime/blitz3d/pivot.cpp @@ -1,10 +1,7 @@ -#include "std.hpp" #include "pivot.hpp" +#include "std.hpp" -Pivot::Pivot(){ -} +Pivot::Pivot() {} -Pivot::Pivot( const Object &t ): -Object(t){ -} +Pivot::Pivot(const Object& t) : Object(t) {} diff --git a/Runtime/blitz3d/pivot.hpp b/Runtime/blitz3d/pivot.hpp index c464e01..37aee18 100644 --- a/Runtime/blitz3d/pivot.hpp +++ b/Runtime/blitz3d/pivot.hpp @@ -4,13 +4,16 @@ #include "object.hpp" -class Pivot : public Object{ -public: +class Pivot : public Object { + public: Pivot(); - Pivot( const Object &t ); + Pivot(const Object& t); //Entity interface - Entity *clone(){ return new Pivot( *this ); } + Entity* clone() + { + return new Pivot(*this); + } }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/planemodel.cpp b/Runtime/blitz3d/planemodel.cpp index d2fd451..b69de49 100644 --- a/Runtime/blitz3d/planemodel.cpp +++ b/Runtime/blitz3d/planemodel.cpp @@ -1,136 +1,140 @@ -#include "std.hpp" #include "planemodel.hpp" -#include "frustum.hpp" #include "camera.hpp" +#include "frustum.hpp" +#include "std.hpp" static Vector vts[17][17]; -extern gxGraphics *gx_graphics; +extern gxGraphics* gx_graphics; -struct PlaneModel::Rep{ +struct PlaneModel::Rep { + int ref_cnt; + gxMesh* mesh; + int sub_divs; - int ref_cnt; - gxMesh *mesh; - int sub_divs; - - Rep( int n ): - ref_cnt(1),sub_divs(n){ - mesh=gx_graphics->createMesh( 5*sub_divs*sub_divs,3*sub_divs*sub_divs,0 ); + Rep(int n) : ref_cnt(1), sub_divs(n) + { + mesh = gx_graphics->createMesh(5 * sub_divs * sub_divs, 3 * sub_divs * sub_divs, 0); } - ~Rep(){ - gx_graphics->freeMesh( mesh ); + ~Rep() + { + gx_graphics->freeMesh(mesh); } - void render( PlaneModel *model,const RenderContext &rc ){ - + void render(PlaneModel* model, const RenderContext& rc) + { static Frustum f; - new( &f ) Frustum( rc.getWorldFrustum(),-model->getRenderTform() ); - const Vector &eye=f.getVertex( Frustum::VERT_EYE ); + new (&f) Frustum(rc.getWorldFrustum(), -model->getRenderTform()); + const Vector& eye = f.getVertex(Frustum::VERT_EYE); - if( eye.y<=0 ) return; + if (eye.y <= 0) + return; - const Vector &tl=f.getVertex( Frustum::VERT_TLFAR ); - const Vector &tr=f.getVertex( Frustum::VERT_TRFAR ); - const Vector &br=f.getVertex( Frustum::VERT_BRFAR ); - const Vector &bl=f.getVertex( Frustum::VERT_BLFAR ); + const Vector& tl = f.getVertex(Frustum::VERT_TLFAR); + const Vector& tr = f.getVertex(Frustum::VERT_TRFAR); + const Vector& br = f.getVertex(Frustum::VERT_BRFAR); + const Vector& bl = f.getVertex(Frustum::VERT_BLFAR); - Transform tex_t( model->getRenderTform().m ); + Transform tex_t(model->getRenderTform().m); int x; - for( x=0;x<=sub_divs;++x ){ - float t=float(x)/float(sub_divs); - Vector tx=(tr-tl)*t+tl; - Vector bx=(br-bl)*t+bl; - for( int y=0;y<=sub_divs;++y ){ - float t=float(y)/float(sub_divs); - vts[x][y]=(bx-tx)*t+tx; + for (x = 0; x <= sub_divs; ++x) { + float t = float(x) / float(sub_divs); + Vector tx = (tr - tl) * t + tl; + Vector bx = (br - bl) * t + bl; + for (int y = 0; y <= sub_divs; ++y) { + float t = float(y) / float(sub_divs); + vts[x][y] = (bx - tx) * t + tx; } } - Plane plane( Vector( 0,1,0 ),0 ); + Plane plane(Vector(0, 1, 0), 0); - mesh->lock( true ); - int v_cnt=0,t_cnt=0; - for( x=0;xlock(true); + int v_cnt = 0, t_cnt = 0; + for (x = 0; x < sub_divs; ++x) { + for (int y = 0; y < sub_divs; ++y) { + Vector in_verts[4], out_verts[5]; - Vector in_verts[4],out_verts[5]; + in_verts[0] = vts[x][y]; + in_verts[1] = vts[x + 1][y]; + in_verts[2] = vts[x + 1][y + 1]; + in_verts[3] = vts[x][y + 1]; - in_verts[0]=vts[x][y]; - in_verts[1]=vts[x+1][y]; - in_verts[2]=vts[x+1][y+1]; - in_verts[3]=vts[x][y+1]; + int k, out_cnt = 0; + for (k = 0; k < 4; ++k) { + const Vector& vert = in_verts[k]; + const Vector& prev_vert = in_verts[(k - 1) & 3]; - int k,out_cnt=0; - for( k=0;k<4;++k ){ - - const Vector &vert=in_verts[k]; - const Vector &prev_vert=in_verts[(k-1)&3]; - - if( vert.y>0 ){ - if( prev_vert.y<=0 ){ - float t=prev_vert.y/(prev_vert.y-vert.y); - out_verts[out_cnt++]=(vert-prev_vert)*t+prev_vert; + if (vert.y > 0) { + if (prev_vert.y <= 0) { + float t = prev_vert.y / (prev_vert.y - vert.y); + out_verts[out_cnt++] = (vert - prev_vert) * t + prev_vert; } - }else{ - if( prev_vert.y>0 ){ - float t=prev_vert.y/(prev_vert.y-vert.y); - out_verts[out_cnt++]=(vert-prev_vert)*t+prev_vert; + } else { + if (prev_vert.y > 0) { + float t = prev_vert.y / (prev_vert.y - vert.y); + out_verts[out_cnt++] = (vert - prev_vert) * t + prev_vert; } - out_verts[out_cnt++]=plane.intersect( Line( eye,vert-eye ) ); + out_verts[out_cnt++] = plane.intersect(Line(eye, vert - eye)); } } - if( out_cnt<3 || out_cnt>5 ) continue; + if (out_cnt < 3 || out_cnt > 5) + continue; - for( k=0;ksetVertex( v_cnt+k,&v.x,&plane.n.x,tex_coords ); + for (k = 0; k < out_cnt; ++k) { + const Vector& v = out_verts[k]; + float tex_coords[2][2] = {{v.x, v.z}, {v.x, v.z}}; + mesh->setVertex(v_cnt + k, &v.x, &plane.n.x, tex_coords); } - for( k=2;ksetTriangle( t_cnt++,v_cnt,v_cnt+k-1,v_cnt+k ); + for (k = 2; k < out_cnt; ++k) { + mesh->setTriangle(t_cnt++, v_cnt, v_cnt + k - 1, v_cnt + k); } - v_cnt+=out_cnt; + v_cnt += out_cnt; } } mesh->unlock(); - if( v_cnt<3 ) return; - model->enqueue( mesh,0,v_cnt,0,t_cnt ); + if (v_cnt < 3) + return; + model->enqueue(mesh, 0, v_cnt, 0, t_cnt); } }; -PlaneModel::PlaneModel( int sub_divs ): -rep( new Rep(sub_divs) ){ -} +PlaneModel::PlaneModel(int sub_divs) : rep(new Rep(sub_divs)) {} -PlaneModel::PlaneModel( const PlaneModel &t ): -Model( t ),rep( t.rep ){ +PlaneModel::PlaneModel(const PlaneModel& t) : Model(t), rep(t.rep) +{ ++rep->ref_cnt; } -PlaneModel::~PlaneModel(){ - if( !--rep->ref_cnt ) delete rep; +PlaneModel::~PlaneModel() +{ + if (!--rep->ref_cnt) + delete rep; } -Plane PlaneModel::getRenderPlane()const{ - return Plane( getRenderTform().v,getRenderTform().m.j.normalized() ); +Plane PlaneModel::getRenderPlane() const +{ + return Plane(getRenderTform().v, getRenderTform().m.j.normalized()); } -bool PlaneModel::render( const RenderContext &rc ){ - rep->render( this,rc ); +bool PlaneModel::render(const RenderContext& rc) +{ + rep->render(this, rc); return false; } -bool PlaneModel::collide( const Line &l,float radius,Collision *curr_coll,const Transform &tf ){ +bool PlaneModel::collide(const Line& l, float radius, Collision* curr_coll, const Transform& tf) +{ + Line line(-tf * l); - Line line( -tf * l ); + Plane p(Vector(0, 1, 0), 0); + p.d -= radius; + float t = p.t_intersect(line); + if (t >= curr_coll->time) + return false; - Plane p( Vector( 0,1,0 ),0 ); - p.d-=radius; - float t=p.t_intersect( line ); - if( t>=curr_coll->time ) return false; + Vector n = (tf.m.cofactor() * p.n).normalized(); - Vector n=(tf.m.cofactor() * p.n).normalized(); - - return curr_coll->update( l,t,n ); + return curr_coll->update(l, t, n); } diff --git a/Runtime/blitz3d/planemodel.hpp b/Runtime/blitz3d/planemodel.hpp index 3e37843..b1e7a1a 100644 --- a/Runtime/blitz3d/planemodel.hpp +++ b/Runtime/blitz3d/planemodel.hpp @@ -2,30 +2,36 @@ #ifndef PLANEMODEL_H #define PLANEMODEL_H -#include "model.hpp" #include "brush.hpp" +#include "model.hpp" -class PlaneModel : public Model{ -public: - PlaneModel( int sub_divs ); - PlaneModel( const PlaneModel &t ); +class PlaneModel : public Model { + public: + PlaneModel(int sub_divs); + PlaneModel(const PlaneModel& t); ~PlaneModel(); - Entity *clone(){ return new PlaneModel( *this ); } + Entity* clone() + { + return new PlaneModel(*this); + } //model interface - bool render( const RenderContext &rc ); + bool render(const RenderContext& rc); //object interface - bool collide( const Line &line,float radius,Collision *curr_coll,const Transform &tf ); + bool collide(const Line& line, float radius, Collision* curr_coll, const Transform& tf); - Plane getRenderPlane()const; + Plane getRenderPlane() const; -private: + private: struct Rep; - Rep *rep; + Rep* rep; - virtual PlaneModel *getPlaneModel(){ return this; } + virtual PlaneModel* getPlaneModel() + { + return this; + } }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/q3bspmodel.cpp b/Runtime/blitz3d/q3bspmodel.cpp index 3e6fe77..08d187a 100644 --- a/Runtime/blitz3d/q3bspmodel.cpp +++ b/Runtime/blitz3d/q3bspmodel.cpp @@ -1,46 +1,49 @@ -#include "std.hpp" #include "q3bspmodel.hpp" #include "q3bsprep.hpp" +#include "std.hpp" -struct Q3BSPModel::Rep : public Q3BSPRep{ +struct Q3BSPModel::Rep : public Q3BSPRep { int ref_cnt; - Rep( const string &f,float gam ):Q3BSPRep( f,gam ), - ref_cnt(1){ - } + Rep(const string& f, float gam) : Q3BSPRep(f, gam), ref_cnt(1) {} }; -Q3BSPModel::Q3BSPModel( const string &f,float gam ): -rep( new Rep( f,gam ) ){ -} +Q3BSPModel::Q3BSPModel(const string& f, float gam) : rep(new Rep(f, gam)) {} -Q3BSPModel::Q3BSPModel( const Q3BSPModel &t ):Model(t), -rep( t.rep ){ +Q3BSPModel::Q3BSPModel(const Q3BSPModel& t) : Model(t), rep(t.rep) +{ ++rep->ref_cnt; } -Q3BSPModel::~Q3BSPModel(){ - if( !--rep->ref_cnt ) delete rep; +Q3BSPModel::~Q3BSPModel() +{ + if (!--rep->ref_cnt) + delete rep; } -bool Q3BSPModel::collide( const Line &line,float radius,Collision *curr_coll,const Transform &t ){ - return rep->collide( line,radius,curr_coll,t ); +bool Q3BSPModel::collide(const Line& line, float radius, Collision* curr_coll, const Transform& t) +{ + return rep->collide(line, radius, curr_coll, t); } -bool Q3BSPModel::render( const RenderContext &rc ){ - rep->render( this,rc ); +bool Q3BSPModel::render(const RenderContext& rc) +{ + rep->render(this, rc); return false; } -void Q3BSPModel::setAmbient( const Vector &t ){ - rep->setAmbient( t ); +void Q3BSPModel::setAmbient(const Vector& t) +{ + rep->setAmbient(t); } -void Q3BSPModel::setLighting( bool l ){ - rep->setLighting( l ); +void Q3BSPModel::setLighting(bool l) +{ + rep->setLighting(l); } -bool Q3BSPModel::isValid()const{ +bool Q3BSPModel::isValid() const +{ return rep->isValid(); } \ No newline at end of file diff --git a/Runtime/blitz3d/q3bspmodel.hpp b/Runtime/blitz3d/q3bspmodel.hpp index d77e1cf..bef5a9a 100644 --- a/Runtime/blitz3d/q3bspmodel.hpp +++ b/Runtime/blitz3d/q3bspmodel.hpp @@ -4,31 +4,37 @@ #include "model.hpp" -class Q3BSPModel : public Model{ -public: - Q3BSPModel( const string &f,float gamma_adj ); - Q3BSPModel( const Q3BSPModel &m ); +class Q3BSPModel : public Model { + public: + Q3BSPModel(const string& f, float gamma_adj); + Q3BSPModel(const Q3BSPModel& m); ~Q3BSPModel(); //Entity interface - Entity *clone(){ return new Q3BSPModel( *this ); } + Entity* clone() + { + return new Q3BSPModel(*this); + } //Object interface - virtual bool collide( const Line &line,float radius,Collision *curr_coll,const Transform &t ); + virtual bool collide(const Line& line, float radius, Collision* curr_coll, const Transform& t); //Model interface - Q3BSPModel *getBSPModel(){ return this; } + Q3BSPModel* getBSPModel() + { + return this; + } - bool render( const RenderContext &rc ); + bool render(const RenderContext& rc); - void setAmbient( const Vector &t ); - void setLighting( bool use_lmap ); + void setAmbient(const Vector& t); + void setLighting(bool use_lmap); - bool isValid()const; + bool isValid() const; -private: + private: struct Rep; - Rep *rep; + Rep* rep; }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/q3bsprep.cpp b/Runtime/blitz3d/q3bsprep.cpp index becc207..ed32f37 100644 --- a/Runtime/blitz3d/q3bsprep.cpp +++ b/Runtime/blitz3d/q3bsprep.cpp @@ -1,25 +1,25 @@ -#include "std.hpp" #include "q3bsprep.hpp" +#include "std.hpp" /* Quake3 File format types */ -#pragma pack(push,1) +#pragma pack(push, 1) struct q3_plane { Vector normal; - float distance; + float distance; }; struct q3_tex { char name[64]; - int flags, contents; + int flags, contents; }; struct q3_vertex { - Vector coords; - float tex_coords[4]; - Vector normal; + Vector coords; + float tex_coords[4]; + Vector normal; unsigned char color[4]; }; @@ -31,20 +31,20 @@ struct q3_node { }; struct q3_face { - int texture; - int effect; - int type; - int vertex; - int n_verts; - int meshvert; - int n_meshverts; - int lm_index; - int lm_start[2]; - int lm_size[2]; + int texture; + int effect; + int type; + int vertex; + int n_verts; + int meshvert; + int n_meshverts; + int lm_index; + int lm_start[2]; + int lm_size[2]; float lm_origin[3]; float lm_vecs[2][3]; float normal[3]; - int patch_size[2]; + int patch_size[2]; }; struct q3_leaf { @@ -71,15 +71,15 @@ struct q3_brushside { struct q3_direntry { union { - int offset; - void *lump; + int offset; + void* lump; }; int length; }; struct q3_header { - unsigned magic; - int version; + unsigned magic; + int version; q3_direntry dir[17]; }; @@ -87,16 +87,20 @@ struct q3_header { /* Loading reps */ struct Surf { - Q3BSPSurf *surf; - int texture, lm_index; + Q3BSPSurf* surf; + int texture, lm_index; vector verts, tris; }; struct FaceCmp { - bool operator()(const q3_face *a, const q3_face *b)const { - if (a->texture < b->texture) return true; - if (b->texture < a->texture) return false; - if (a->lm_index < b->lm_index) return true; + bool operator()(const q3_face* a, const q3_face* b) const + { + if (a->texture < b->texture) + return true; + if (b->texture < a->texture) + return false; + if (a->lm_index < b->lm_index) + return true; return false; } }; @@ -108,16 +112,16 @@ typedef map FaceMap; struct Q3BSPFace; struct Q3BSPSurf { - Brush brush; - gxMesh *mesh; + Brush brush; + gxMesh* mesh; vector r_faces; - int texture, lm_index; + int texture, lm_index; }; struct Q3BSPFace { union { - Surf *t_surf; - Q3BSPSurf *surf; + Surf* t_surf; + Q3BSPSurf* surf; }; int vert, n_verts, tri, n_tris; }; @@ -127,69 +131,80 @@ struct Q3BSPBrush { }; struct Q3BSPLeaf { - int cluster; - Box box; + int cluster; + Box box; vector faces; }; struct Q3BSPNode { - Box box; - Plane plane; - Q3BSPNode *nodes[2]; - Q3BSPLeaf *leafs[2]; + Box box; + Plane plane; + Q3BSPNode* nodes[2]; + Q3BSPLeaf* leafs[2]; - ~Q3BSPNode() { delete nodes[0]; delete nodes[1]; delete leafs[0]; delete leafs[1]; } + ~Q3BSPNode() + { + delete nodes[0]; + delete nodes[1]; + delete leafs[0]; + delete leafs[1]; + } }; -static q3_header header; -static FaceMap face_map; -static vector t_surfs; -static vector p_verts; //patch vertices -static vector p_coll_verts; +static q3_header header; +static FaceMap face_map; +static vector t_surfs; +static vector p_verts; //patch vertices +static vector p_coll_verts; static vector coll_tris; static float gamma_adj; -static Vector r_eye; -static int r_cluster; -static Frustum r_frustum; -static Vector r_frustedges[12]; +static Vector r_eye; +static int r_cluster; +static Frustum r_frustum; +static Vector r_frustedges[12]; static map q3face_map; -extern gxScene *gx_scene; -extern gxRuntime *gx_runtime; -extern gxGraphics *gx_graphics; +extern gxScene* gx_scene; +extern gxRuntime* gx_runtime; +extern gxGraphics* gx_graphics; //#define SWAPTRIS -Vector static tf(const Vector &v) { +Vector static tf(const Vector& v) +{ return Vector(-v.y, v.z, v.x); } #ifdef BETA -static void debuglog(const string &t) { +static void debuglog(const string& t) +{ gx_runtime->debugLog(t.c_str()); } #else -static void debuglog(const string &t) {} +static void debuglog(const string& t) {} #endif -static Surf *findSurf(q3_face *f) { +static Surf* findSurf(q3_face* f) +{ FaceMap::const_iterator it = face_map.find(f); - if (it != face_map.end()) return it->second; - Surf *s = new Surf; - s->texture = f->texture; + if (it != face_map.end()) + return it->second; + Surf* s = new Surf; + s->texture = f->texture; s->lm_index = f->lm_index; face_map.insert(make_pair(f, s)); t_surfs.push_back(s); return s; } -void Q3BSPRep::createTextures() { - int n_texs = header.dir[1].length / sizeof(q3_tex); - q3_tex *q3tex = (q3_tex*)header.dir[1].lump; +void Q3BSPRep::createTextures() +{ + int n_texs = header.dir[1].length / sizeof(q3_tex); + q3_tex* q3tex = (q3_tex*)header.dir[1].lump; for (int k = 0; k < n_texs; ++k) { string t = string(q3tex->name); - char fl[32], co[32]; + char fl[32], co[32]; _itoa(q3tex->flags, fl, 16); _itoa(q3tex->contents, co, 16); debuglog(t + ", flags=0x" + fl + ", contents=0x" + co); @@ -200,7 +215,8 @@ void Q3BSPRep::createTextures() { tex = Texture(t + ".png", 1); if (!tex.getCanvas(0)) { tex = Texture(t + ".dds", 1); - if (!tex.getCanvas(0)) debuglog("Failed!"); + if (!tex.getCanvas(0)) + debuglog("Failed!"); } } } @@ -210,17 +226,19 @@ void Q3BSPRep::createTextures() { } } -void Q3BSPRep::createLightMaps() { - int n_lmaps = header.dir[14].length / (128 * 128 * 3); - unsigned char *rgb = (unsigned char*)header.dir[14].lump; - unsigned char adj[256]; - int k; - for (k = 0; k < 256; ++k) adj[k] = pow(k / 255.0f, gamma_adj)*255.0f; +void Q3BSPRep::createLightMaps() +{ + int n_lmaps = header.dir[14].length / (128 * 128 * 3); + unsigned char* rgb = (unsigned char*)header.dir[14].lump; + unsigned char adj[256]; + int k; + for (k = 0; k < 256; ++k) + adj[k] = pow(k / 255.0f, gamma_adj) * 255.0f; for (k = 0; k < n_lmaps; ++k) { Texture tex(128, 128, 1 + 8 + 16 + 32, 1); tex.setBlend(gxScene::BLEND_ADD); - gxCanvas *c = tex.getCanvas(0); + gxCanvas* c = tex.getCanvas(0); c->lock(); for (int y = 0; y < 128; ++y) { for (int x = 0; x < 128; ++x) { @@ -234,21 +252,23 @@ void Q3BSPRep::createLightMaps() { } } -void Q3BSPRep::createVis() { - int *vis = (int*)header.dir[16].lump; - int n_vecs = *vis++; - vis_sz = *vis++; +void Q3BSPRep::createVis() +{ + int* vis = (int*)header.dir[16].lump; + int n_vecs = *vis++; + vis_sz = *vis++; debuglog("vis: " + itoa(n_vecs) + "," + itoa(vis_sz)); - vis_data = new char[n_vecs*vis_sz]; - memcpy(vis_data, vis, n_vecs*vis_sz); + vis_data = new char[n_vecs * vis_sz]; + memcpy(vis_data, vis, n_vecs * vis_sz); } -void Q3BSPRep::createCollider() { +void Q3BSPRep::createCollider() +{ vector coll_verts; - int n_verts = header.dir[10].length / sizeof(q3_vertex); - q3_vertex *t = (q3_vertex*)header.dir[10].lump; - MeshCollider::Vertex cv; - int k; + int n_verts = header.dir[10].length / sizeof(q3_vertex); + q3_vertex* t = (q3_vertex*)header.dir[10].lump; + MeshCollider::Vertex cv; + int k; for (k = 0; k < n_verts; ++k) { cv.coords = tf(t->coords); coll_verts.push_back(cv); @@ -269,24 +289,25 @@ void Q3BSPRep::createCollider() { coll_tris.clear(); } -void Q3BSPRep::createSurfs() { +void Q3BSPRep::createSurfs() +{ int k; for (k = 0; k < t_surfs.size(); ++k) { - Surf *s = t_surfs[k]; - gxMesh *mesh = gx_graphics->createMesh(s->verts.size(), s->tris.size() / 3, 0); + Surf* s = t_surfs[k]; + gxMesh* mesh = gx_graphics->createMesh(s->verts.size(), s->tris.size() / 3, 0); mesh->lock(true); int j; for (j = 0; j < s->verts.size(); ++j) { - q3_vertex *t; - int n = s->verts[j]; + q3_vertex* t; + int n = s->verts[j]; if (n >= 0) { t = (q3_vertex*)header.dir[10].lump + n; } else { t = &p_verts[-n - 1]; } - float tex_coords[2][2] = { {t->tex_coords[2],t->tex_coords[3]},{t->tex_coords[0],t->tex_coords[1]} }; - unsigned argb = 0xff000000 | (t->color[0] << 16) | (t->color[1] << 8) | t->color[2]; + float tex_coords[2][2] = {{t->tex_coords[2], t->tex_coords[3]}, {t->tex_coords[0], t->tex_coords[1]}}; + unsigned argb = 0xff000000 | (t->color[0] << 16) | (t->color[1] << 8) | t->color[2]; mesh->setVertex(j, tf(t->coords), tf(t->normal), argb, tex_coords); } for (j = 0; j < s->tris.size(); j += 3) { @@ -298,17 +319,18 @@ void Q3BSPRep::createSurfs() { } mesh->unlock(); - Q3BSPSurf *surf = new Q3BSPSurf; - surf->texture = s->texture; - surf->lm_index = s->lm_index; - surf->mesh = mesh; + Q3BSPSurf* surf = new Q3BSPSurf; + surf->texture = s->texture; + surf->lm_index = s->lm_index; + surf->mesh = mesh; surfs.push_back(surf); s->surf = surf; } for (k = 0; k < faces.size(); ++k) { - Q3BSPFace *f = faces[k]; - f->surf = f->t_surf->surf; - f->tri /= 3; f->n_tris /= 3; + Q3BSPFace* f = faces[k]; + f->surf = f->t_surf->surf; + f->tri /= 3; + f->n_tris /= 3; } for (k = 0; k < t_surfs.size(); ++k) { delete t_surfs[k]; @@ -318,16 +340,18 @@ void Q3BSPRep::createSurfs() { p_verts.clear(); } -static void average(const q3_vertex &a, const q3_vertex &b, q3_vertex *c) { - c->coords = (a.coords + b.coords)*.5f; - c->normal = (a.normal + b.normal)*.5f; +static void average(const q3_vertex& a, const q3_vertex& b, q3_vertex* c) +{ + c->coords = (a.coords + b.coords) * .5f; + c->normal = (a.normal + b.normal) * .5f; for (int k = 0; k < 4; ++k) { - c->color[k] = (a.color[k] + b.color[k] + 1) / 2; - c->tex_coords[k] = (a.tex_coords[k] + b.tex_coords[k])*.5f; + c->color[k] = (a.color[k] + b.color[k] + 1) / 2; + c->tex_coords[k] = (a.tex_coords[k] + b.tex_coords[k]) * .5f; } } -static void subdivide(vector &verts, int level, int index, int step) { +static void subdivide(vector& verts, int level, int index, int step) +{ if (!level) { q3_vertex t1, t2; average(verts[index], verts[index + step], &t1); @@ -342,49 +366,49 @@ static void subdivide(vector &verts, int level, int index, int step) subdivide(verts, level - 1, index + step, step / 2); } -static void patchFace(Q3BSPFace *face, q3_face *q3face, bool draw, bool solid, int level) { - - int k, x, y; +static void patchFace(Q3BSPFace* face, q3_face* q3face, bool draw, bool solid, int level) +{ + int k, x, y; vector verts; if (draw) { - int step = 1 << level; - int size_x = (q3face->patch_size[0] - 1)*step + 1; - int size_y = (q3face->patch_size[1] - 1)*step + 1; - verts.resize(size_x*size_y); + int step = 1 << level; + int size_x = (q3face->patch_size[0] - 1) * step + 1; + int size_y = (q3face->patch_size[1] - 1) * step + 1; + verts.resize(size_x * size_y); //seed initial verts - q3_vertex *t = (q3_vertex*)header.dir[10].lump + q3face->vertex; + q3_vertex* t = (q3_vertex*)header.dir[10].lump + q3face->vertex; for (y = 0; y < size_y; y += step) { for (x = 0; x < size_x; x += step) { - verts[y*size_x + x] = *t++; + verts[y * size_x + x] = *t++; } } //subdivide! for (y = 0; y < size_y; y += step) { for (x = 0; x < size_x - 1; x += step * 2) { - subdivide(verts, level, y*size_x + x, step); + subdivide(verts, level, y * size_x + x, step); } } for (x = 0; x < size_x; ++x) { for (y = 0; y < size_y - 1; y += step * 2) { - subdivide(verts, level, y*size_x + x, size_x*step); + subdivide(verts, level, y * size_x + x, size_x * step); } } - Surf *surf = face->t_surf; - int vert = surf->verts.size() - face->vert; + Surf* surf = face->t_surf; + int vert = surf->verts.size() - face->vert; //generate patch verts - for (k = 0; k < size_x*size_y; ++k) { + for (k = 0; k < size_x * size_y; ++k) { p_verts.push_back(verts[k]); surf->verts.push_back(p_verts.size()); // Why was there a - here } - face->n_verts += size_x*size_y; + face->n_verts += size_x * size_y; //generate tris... for (y = 0; y < size_y - 1; ++y) { - int n = y*size_x + vert; + int n = y * size_x + vert; for (x = 0; x < size_x - 1; ++n, ++x) { surf->tris.push_back(n); surf->tris.push_back(n + size_x); @@ -394,41 +418,44 @@ static void patchFace(Q3BSPFace *face, q3_face *q3face, bool draw, bool solid, i surf->tris.push_back(n + size_x); } } - face->n_tris += (size_x - 1)*(size_y - 1) * 6; + face->n_tris += (size_x - 1) * (size_y - 1) * 6; } if (solid) { vector verts; - int step = 1; - int size_x = q3face->patch_size[0]; - int size_y = q3face->patch_size[1]; - verts.resize(size_x*size_y); + int step = 1; + int size_x = q3face->patch_size[0]; + int size_y = q3face->patch_size[1]; + verts.resize(size_x * size_y); //seed initial verts - q3_vertex *t = (q3_vertex*)header.dir[10].lump + q3face->vertex; - for (k = 0; k < size_x*size_y; ++k) verts[k] = *t++; + q3_vertex* t = (q3_vertex*)header.dir[10].lump + q3face->vertex; + for (k = 0; k < size_x * size_y; ++k) + verts[k] = *t++; //subdivide! for (y = 0; y < size_y; y += step) { for (x = 0; x < size_x - 1; x += step * 2) { - subdivide(verts, 0, y*size_x + x, step); + subdivide(verts, 0, y * size_x + x, step); } } for (x = 0; x < size_x; ++x) { for (y = 0; y < size_y - 1; y += step * 2) { - subdivide(verts, 0, y*size_x + x, size_x*step); + subdivide(verts, 0, y * size_x + x, size_x * step); } } int vert = header.dir[10].length / sizeof(q3_vertex) + p_coll_verts.size(); //generate patch verts - for (k = 0; k < size_x*size_y; ++k) p_coll_verts.push_back(tf(verts[k].coords)); + for (k = 0; k < size_x * size_y; ++k) + p_coll_verts.push_back(tf(verts[k].coords)); MeshCollider::Triangle ct; - ct.surface = 0; ct.index = 0; + ct.surface = 0; + ct.index = 0; //generate tris... for (y = 0; y < size_y - 1; ++y) { - int n = y*size_x + vert; + int n = y * size_x + vert; for (x = 0; x < size_x - 1; ++n, ++x) { ct.verts[0] = n; ct.verts[1] = n + size_x; @@ -443,12 +470,14 @@ static void patchFace(Q3BSPFace *face, q3_face *q3face, bool draw, bool solid, i } } -static void meshFace(Q3BSPFace *face, q3_face *q3face, bool draw, bool solid) { +static void meshFace(Q3BSPFace* face, q3_face* q3face, bool draw, bool solid) +{ static map vert_map; vert_map.clear(); - int *meshverts = (int*)header.dir[11].lump + q3face->meshvert; + int* meshverts = (int*)header.dir[11].lump + q3face->meshvert; MeshCollider::Triangle ct; - ct.surface = 0; ct.index = 0; + ct.surface = 0; + ct.index = 0; for (int j = 0; j < q3face->n_meshverts; j += 3) { for (int q = 0; q < 3; ++q) { int n = meshverts[j + q] + q3face->vertex; @@ -463,28 +492,31 @@ static void meshFace(Q3BSPFace *face, q3_face *q3face, bool draw, bool solid) { } ct.verts[q] = n; } - if (solid) coll_tris.push_back(ct); + if (solid) + coll_tris.push_back(ct); } } -static Q3BSPBrush *createBrush(int n) { - Q3BSPBrush *brush = new Q3BSPBrush; - q3_brush *q3brush = (q3_brush*)header.dir[8].lump + n; - q3_brushside *q3brushside = (q3_brushside*)header.dir[9].lump + q3brush->brushside; - Plane p; +static Q3BSPBrush* createBrush(int n) +{ + Q3BSPBrush* brush = new Q3BSPBrush; + q3_brush* q3brush = (q3_brush*)header.dir[8].lump + n; + q3_brushside* q3brushside = (q3_brushside*)header.dir[9].lump + q3brush->brushside; + Plane p; for (int j = 0; j < q3brush->n_brushsides; ++j) { - q3_plane *q3plane = (q3_plane*)header.dir[2].lump + q3brushside[j].plane; - p.n = tf(q3plane->normal); - p.d = -q3plane->distance; + q3_plane* q3plane = (q3_plane*)header.dir[2].lump + q3brushside[j].plane; + p.n = tf(q3plane->normal); + p.d = -q3plane->distance; brush->planes.push_back(p); } return brush; } -Q3BSPLeaf *Q3BSPRep::createLeaf(int n) { - q3_leaf *q3leaf = (q3_leaf*)header.dir[4].lump + n; +Q3BSPLeaf* Q3BSPRep::createLeaf(int n) +{ + q3_leaf* q3leaf = (q3_leaf*)header.dir[4].lump + n; - Q3BSPLeaf *leaf = new Q3BSPLeaf; + Q3BSPLeaf* leaf = new Q3BSPLeaf; leaf->cluster = q3leaf->cluster; @@ -492,41 +524,46 @@ Q3BSPLeaf *Q3BSPRep::createLeaf(int n) { Vector maxs(q3leaf->maxs[0], q3leaf->maxs[1], q3leaf->maxs[2]); leaf->box = Box(tf(mins)); leaf->box.update(tf(maxs)); - int *leaffaces = (int*)header.dir[5].lump + q3leaf->leafface; + int* leaffaces = (int*)header.dir[5].lump + q3leaf->leafface; for (int k = 0; k < q3leaf->n_leaffaces; ++k) { - int face_n = leaffaces[k]; map::const_iterator it = q3face_map.find(face_n); if (it != q3face_map.end()) { - if (it->second) leaf->faces.push_back(it->second); + if (it->second) + leaf->faces.push_back(it->second); continue; } - q3_face *q3face = (q3_face*)header.dir[13].lump + leaffaces[k]; + q3_face* q3face = (q3_face*)header.dir[13].lump + leaffaces[k]; if (q3face->type == 1 || q3face->type == 3) { - if (!q3face->n_meshverts || (q3face->n_meshverts % 3)) continue; - } else if (q3face->type != 2) continue; + if (!q3face->n_meshverts || (q3face->n_meshverts % 3)) + continue; + } else if (q3face->type != 2) + continue; bool draw = true, solid = true; if (q3face->texture >= 0) { - q3_tex *q3tex = (q3_tex*)header.dir[1].lump + q3face->texture; - if (!(q3tex->contents & 1)) continue; - if (q3tex->flags & 0x84) draw = false; + q3_tex* q3tex = (q3_tex*)header.dir[1].lump + q3face->texture; + if (!(q3tex->contents & 1)) + continue; + if (q3tex->flags & 0x84) + draw = false; } - if (!draw && !solid) continue; + if (!draw && !solid) + continue; - Q3BSPFace *face = 0; + Q3BSPFace* face = 0; if (draw) { - Surf *surf = findSurf(q3face); - face = new Q3BSPFace; - face->t_surf = surf; - face->vert = surf->verts.size(); - face->tri = surf->tris.size(); + Surf* surf = findSurf(q3face); + face = new Q3BSPFace; + face->t_surf = surf; + face->vert = surf->verts.size(); + face->tri = surf->tris.size(); face->n_verts = face->n_tris = 0; leaf->faces.push_back(face); faces.push_back(face); @@ -543,11 +580,12 @@ Q3BSPLeaf *Q3BSPRep::createLeaf(int n) { return leaf; } -Q3BSPNode *Q3BSPRep::createNode(int n) { - q3_node *q3node = (q3_node*)header.dir[3].lump + n; - q3_plane *q3plane = (q3_plane*)header.dir[2].lump + q3node->plane; +Q3BSPNode* Q3BSPRep::createNode(int n) +{ + q3_node* q3node = (q3_node*)header.dir[3].lump + n; + q3_plane* q3plane = (q3_plane*)header.dir[2].lump + q3node->plane; - Q3BSPNode *node = new Q3BSPNode; + Q3BSPNode* node = new Q3BSPNode; Vector mins(q3node->mins[0], q3node->mins[1], q3node->mins[2]); Vector maxs(q3node->maxs[0], q3node->maxs[1], q3node->maxs[2]); @@ -570,15 +608,18 @@ Q3BSPNode *Q3BSPRep::createNode(int n) { return node; } -Q3BSPRep::Q3BSPRep(const string &f, float gam) :root_node(0), vis_sz(0), vis_data(0), use_lmap(true) { - +Q3BSPRep::Q3BSPRep(const string& f, float gam) : root_node(0), vis_sz(0), vis_data(0), use_lmap(true) +{ gamma_adj = 1 - gam; - FILE *buf = fopen(f.c_str(), "rb"); if (!buf) return; + FILE* buf = fopen(f.c_str(), "rb"); + if (!buf) + return; fread(&header, sizeof(header), 1, buf); if (header.magic != 'PSBI' || header.version != 0x2e) { - fclose(buf); return; + fclose(buf); + return; } debuglog("Header OK"); @@ -621,7 +662,8 @@ Q3BSPRep::Q3BSPRep(const string &f, float gam) :root_node(0), vis_sz(0), vis_dat q3face_map.clear(); } -Q3BSPRep::~Q3BSPRep() { +Q3BSPRep::~Q3BSPRep() +{ delete root_node; delete[] vis_data; int k; @@ -634,80 +676,99 @@ Q3BSPRep::~Q3BSPRep() { } } -void Q3BSPRep::vis(Q3BSPNode *n) { +void Q3BSPRep::vis(Q3BSPNode* n) +{ int i = n->plane.distance(r_eye) < 0; - if (n->nodes[i]) vis(n->nodes[i]); - else r_cluster = n->leafs[i]->cluster; + if (n->nodes[i]) + vis(n->nodes[i]); + else + r_cluster = n->leafs[i]->cluster; } -static bool cull(const Box &b, int *clip) { +static bool cull(const Box& b, int* clip) +{ for (int n = 0; n < 6; ++n) { int mask = 1 << n; - if (!(*clip & mask)) continue; - const Plane &p = r_frustum.getPlane(n); - int q = - (p.distance(b.corner(0)) >= 0) + (p.distance(b.corner(1)) >= 0) + - (p.distance(b.corner(2)) >= 0) + (p.distance(b.corner(3)) >= 0) + - (p.distance(b.corner(4)) >= 0) + (p.distance(b.corner(5)) >= 0) + - (p.distance(b.corner(6)) >= 0) + (p.distance(b.corner(7)) >= 0); - if (!q) return false; - if (q == 8) *clip &= ~mask; + if (!(*clip & mask)) + continue; + const Plane& p = r_frustum.getPlane(n); + int q = (p.distance(b.corner(0)) >= 0) + (p.distance(b.corner(1)) >= 0) + (p.distance(b.corner(2)) >= 0) + + (p.distance(b.corner(3)) >= 0) + (p.distance(b.corner(4)) >= 0) + (p.distance(b.corner(5)) >= 0) + + (p.distance(b.corner(6)) >= 0) + (p.distance(b.corner(7)) >= 0); + if (!q) + return false; + if (q == 8) + *clip &= ~mask; } return true; } -void Q3BSPRep::render(Q3BSPLeaf *l, int clip) { +void Q3BSPRep::render(Q3BSPLeaf* l, int clip) +{ int cluster = l->cluster; - if (cluster < 0) return; + if (cluster < 0) + return; if (r_cluster >= 0) { - if (!(vis_data[cluster*vis_sz + r_cluster / 8] & (1 << (r_cluster & 7)))) return; + if (!(vis_data[cluster * vis_sz + r_cluster / 8] & (1 << (r_cluster & 7)))) + return; } - if (clip && !cull(l->box, &clip)) return; + if (clip && !cull(l->box, &clip)) + return; for (int k = 0; k < l->faces.size(); ++k) { - Q3BSPFace *f = l->faces[k]; - if (Q3BSPSurf *s = f->surf) { - if (!s->r_faces.size()) r_surfs.push_back(s); + Q3BSPFace* f = l->faces[k]; + if (Q3BSPSurf* s = f->surf) { + if (!s->r_faces.size()) + r_surfs.push_back(s); s->r_faces.push_back(f); f->surf = 0; } } } -void Q3BSPRep::render(Q3BSPNode *n, int clip) { - if (clip && !cull(n->box, &clip)) return; +void Q3BSPRep::render(Q3BSPNode* n, int clip) +{ + if (clip && !cull(n->box, &clip)) + return; //draw front to back... int i = n->plane.distance(r_eye) < 0; - if (n->nodes[i]) render(n->nodes[i], clip); - else render(n->leafs[i], clip); + if (n->nodes[i]) + render(n->nodes[i], clip); + else + render(n->leafs[i], clip); i ^= 1; - if (n->nodes[i]) render(n->nodes[i], clip); - else render(n->leafs[i], clip); + if (n->nodes[i]) + render(n->nodes[i], clip); + else + render(n->leafs[i], clip); } -void Q3BSPRep::render(Model *model, const RenderContext &rc) { +void Q3BSPRep::render(Model* model, const RenderContext& rc) +{ r_eye = -model->getRenderTform() * rc.getCameraTform().v; - new(&r_frustum) Frustum(rc.getWorldFrustum(), -model->getRenderTform()); + new (&r_frustum) Frustum(rc.getWorldFrustum(), -model->getRenderTform()); vis(root_node); - if (r_cluster == -1) debuglog("No cluster!"); + if (r_cluster == -1) + debuglog("No cluster!"); render(root_node, 0x3f); - if (!r_surfs.size()) return; + if (!r_surfs.size()) + return; gx_scene->setAmbient2(&ambient.x); gx_scene->setWorldMatrix((gxScene::Matrix*)&model->getRenderTform()); int k; for (k = 0; k < r_surfs.size(); ++k) { - Q3BSPSurf *s = r_surfs[k]; + Q3BSPSurf* s = r_surfs[k]; gx_scene->setRenderState(s->brush.getRenderState()); int j; for (j = 0; j < s->r_faces.size(); ++j) { - Q3BSPFace *f = s->r_faces[j]; + Q3BSPFace* f = s->r_faces[j]; gx_scene->render(s->mesh, f->vert, f->n_verts, f->tri, f->n_tris); f->surf = s; } @@ -716,21 +777,25 @@ void Q3BSPRep::render(Model *model, const RenderContext &rc) { r_surfs.clear(); } -bool Q3BSPRep::collide(const Line &line, float radius, Collision *curr_coll, const Transform &t) { +bool Q3BSPRep::collide(const Line& line, float radius, Collision* curr_coll, const Transform& t) +{ return collider->collide(line, radius, curr_coll, t); } -void Q3BSPRep::setAmbient(const Vector &t) { +void Q3BSPRep::setAmbient(const Vector& t) +{ ambient = t; } -void Q3BSPRep::setLighting(bool lmap) { - if (lmap == use_lmap) return; +void Q3BSPRep::setLighting(bool lmap) +{ + if (lmap == use_lmap) + return; int fx = gxScene::FX_CONDLIGHT; if (use_lmap = lmap) { int k; for (k = 0; k < surfs.size(); ++k) { - Q3BSPSurf *s = surfs[k]; + Q3BSPSurf* s = surfs[k]; if (s->lm_index >= 0) { //has a lightmap... s->brush.setFX(fx); @@ -746,10 +811,10 @@ void Q3BSPRep::setLighting(bool lmap) { } } } else { - int k; + int k; Texture tex; for (k = 0; k < surfs.size(); ++k) { - Q3BSPSurf *s = surfs[k]; + Q3BSPSurf* s = surfs[k]; s->brush.setFX(fx | gxScene::FX_EMISSIVE); if (s->texture >= 0) { s->brush.setTexture(0, textures[s->texture], 0); diff --git a/Runtime/blitz3d/q3bsprep.hpp b/Runtime/blitz3d/q3bsprep.hpp index dbf06b3..3837bd7 100644 --- a/Runtime/blitz3d/q3bsprep.hpp +++ b/Runtime/blitz3d/q3bsprep.hpp @@ -2,55 +2,58 @@ #ifndef Q3BSPREP_H #define Q3BSPREP_H -#include "model.hpp" #include "meshcollider.hpp" +#include "model.hpp" struct Q3BSPSurf; struct Q3BSPFace; struct Q3BSPLeaf; struct Q3BSPNode; -class Q3BSPRep{ -public: +class Q3BSPRep { + public: //constructor - Q3BSPRep( const string &f,float gamma_adj ); + Q3BSPRep(const string& f, float gamma_adj); ~Q3BSPRep(); - void render( Model *model,const RenderContext &rc ); - bool collide( const Line &line,float radius,Collision *curr_coll,const Transform &t ); + void render(Model* model, const RenderContext& rc); + bool collide(const Line& line, float radius, Collision* curr_coll, const Transform& t); - void setAmbient( const Vector &t ); - void setLighting( bool use_lmap ); + void setAmbient(const Vector& t); + void setLighting(bool use_lmap); - bool isValid()const{ return root_node!=0; } + bool isValid() const + { + return root_node != 0; + } -private: - Q3BSPNode *root_node; + private: + Q3BSPNode* root_node; Vector ambient; vector faces; - vector surfs,r_surfs; - vector textures,light_maps; + vector surfs, r_surfs; + vector textures, light_maps; - int vis_sz; - char *vis_data; - bool use_lmap; + int vis_sz; + char* vis_data; + bool use_lmap; - MeshCollider *collider; + MeshCollider* collider; void createVis(); void createSurfs(); void createCollider(); - void createTextures(); - void createLightMaps(); - Q3BSPLeaf *createLeaf( int n ); - Q3BSPNode *createNode( int n ); + void createTextures(); + void createLightMaps(); + Q3BSPLeaf* createLeaf(int n); + Q3BSPNode* createNode(int n); - void vis( Q3BSPNode *node ); - void render( Q3BSPLeaf *l,int clip ); - void render( Q3BSPNode *n,int clip ); + void vis(Q3BSPNode* node); + void render(Q3BSPLeaf* l, int clip); + void render(Q3BSPNode* n, int clip); }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/rendercontext.hpp b/Runtime/blitz3d/rendercontext.hpp index 080ee64..4e9d1ec 100644 --- a/Runtime/blitz3d/rendercontext.hpp +++ b/Runtime/blitz3d/rendercontext.hpp @@ -4,22 +4,34 @@ #include "frustum.hpp" -class RenderContext{ -public: - RenderContext( const Transform &t,const Frustum &f,bool r ): - camera_tform( t ),camera_frustum(f),ref(r){ - new( &world_frustum ) Frustum( f,t ); +class RenderContext { + public: + RenderContext(const Transform& t, const Frustum& f, bool r) : camera_tform(t), camera_frustum(f), ref(r) + { + new (&world_frustum) Frustum(f, t); } - bool isReflected()const{ return ref; } - const Transform &getCameraTform()const{ return camera_tform; } - const Frustum &getWorldFrustum()const{ return world_frustum; } - const Frustum &getCameraFrustum()const{ return camera_frustum; } + bool isReflected() const + { + return ref; + } + const Transform& getCameraTform() const + { + return camera_tform; + } + const Frustum& getWorldFrustum() const + { + return world_frustum; + } + const Frustum& getCameraFrustum() const + { + return camera_frustum; + } -private: + private: Transform camera_tform; - Frustum world_frustum,camera_frustum; - bool ref; + Frustum world_frustum, camera_frustum; + bool ref; }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/skinmodel.cpp b/Runtime/blitz3d/skinmodel.cpp index 9f980e0..aec49bc 100644 --- a/Runtime/blitz3d/skinmodel.cpp +++ b/Runtime/blitz3d/skinmodel.cpp @@ -1,31 +1,33 @@ -#include "std.hpp" #include "skinmodel.hpp" +#include "std.hpp" -SkinModel::SkinModel(){ - setRenderSpace( RENDER_SPACE_WORLD ); +SkinModel::SkinModel() +{ + setRenderSpace(RENDER_SPACE_WORLD); } -void SkinModel::setBones( const vector &bones ){ - _bones=bones; - _surf_bones.resize( _bones.size() ); +void SkinModel::setBones(const vector& bones) +{ + _bones = bones; + _surf_bones.resize(_bones.size()); } -void SkinModel::render( const RenderContext &rc ){ - +void SkinModel::render(const RenderContext& rc) +{ int k; - for( k=0;k<_bones.size();++k ){ - Object *obj=_bones[k]; - _surf_bones[k].coord_tform=obj->getRenderTform(); - _surf_bones[k].normal_tform=_surf_bones[k].coord_tform.m.cofactor(); + for (k = 0; k < _bones.size(); ++k) { + Object* obj = _bones[k]; + _surf_bones[k].coord_tform = obj->getRenderTform(); + _surf_bones[k].normal_tform = _surf_bones[k].coord_tform.m.cofactor(); } - const MeshModel::SurfaceList &_surfs=getSurfaces(); + const MeshModel::SurfaceList& _surfs = getSurfaces(); - for( k=0;k<_surfs.size();++k ){ - Surface *surf=_surfs[k]; - if( gxMesh *mesh=surf->getMesh( _surf_bones ) ){ - enqueue( mesh,0,surf->numVertices(),0,surf->numTriangles(),surf->getBrush() ); + for (k = 0; k < _surfs.size(); ++k) { + Surface* surf = _surfs[k]; + if (gxMesh* mesh = surf->getMesh(_surf_bones)) { + enqueue(mesh, 0, surf->numVertices(), 0, surf->numTriangles(), surf->getBrush()); } } } diff --git a/Runtime/blitz3d/skinmodel.hpp b/Runtime/blitz3d/skinmodel.hpp index 4f4c915..59e732e 100644 --- a/Runtime/blitz3d/skinmodel.hpp +++ b/Runtime/blitz3d/skinmodel.hpp @@ -4,16 +4,17 @@ #include "meshmodel.hpp" -class SkinModel : public MeshModel{ - vector _bones; +class SkinModel : public MeshModel { + vector _bones; vector _surf_bones; -public: + + public: SkinModel(); - void setBones( const vector &bones ); + void setBones(const vector& bones); //Model interface - virtual void render( const RenderContext &rc ); + virtual void render(const RenderContext& rc); }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/sprite.cpp b/Runtime/blitz3d/sprite.cpp index 51738c8..cf73670 100644 --- a/Runtime/blitz3d/sprite.cpp +++ b/Runtime/blitz3d/sprite.cpp @@ -1,139 +1,151 @@ -#include "std.hpp" #include "sprite.hpp" +#include "std.hpp" extern float stats3d[]; -static float null[]={0,0,0}; +static float null[] = {0, 0, 0}; -static float tex_coords0[2][2]={ {0,0},{0,0} }; -static float tex_coords1[2][2]={ {1,0},{1,0} }; -static float tex_coords2[2][2]={ {1,1},{1,1} }; -static float tex_coords3[2][2]={ {0,1},{0,1} }; +static float tex_coords0[2][2] = {{0, 0}, {0, 0}}; +static float tex_coords1[2][2] = {{1, 0}, {1, 0}}; +static float tex_coords2[2][2] = {{1, 1}, {1, 1}}; +static float tex_coords3[2][2] = {{0, 1}, {0, 1}}; -extern gxRuntime *gx_runtime; -extern gxGraphics *gx_graphics; +extern gxRuntime* gx_runtime; +extern gxGraphics* gx_graphics; -static gxMesh *mesh; -static int mesh_size; +static gxMesh* mesh; +static int mesh_size; static vector mesh_indices; -static int allocIndex(){ - if( !mesh_indices.size() ){ - if( mesh_size ) gx_graphics->freeMesh( mesh ); - for( int k=0;k<256;++k ){ - mesh_indices.push_back( mesh_size++ ); +static int allocIndex() +{ + if (!mesh_indices.size()) { + if (mesh_size) + gx_graphics->freeMesh(mesh); + for (int k = 0; k < 256; ++k) { + mesh_indices.push_back(mesh_size++); } - mesh=gx_graphics->createMesh( mesh_size*4,mesh_size*2,0 ); + mesh = gx_graphics->createMesh(mesh_size * 4, mesh_size * 2, 0); } - int n=mesh_indices.back(); + int n = mesh_indices.back(); mesh_indices.pop_back(); return n; } -static void freeIndex( int n ){ - mesh_indices.push_back( n ); - if( mesh_indices.size()!=mesh_size ) return; - gx_graphics->freeMesh( mesh ); +static void freeIndex(int n) +{ + mesh_indices.push_back(n); + if (mesh_indices.size() != mesh_size) + return; + gx_graphics->freeMesh(mesh); mesh_indices.clear(); - mesh_size=0; + mesh_size = 0; } -Sprite::Sprite(): -view_mode(VIEW_MODE_FREE), -xhandle(0),yhandle(0), -rot(0),xscale(1),yscale(1),captured(false){ - setRenderSpace( RENDER_SPACE_WORLD ); - mesh_index=allocIndex(); +Sprite::Sprite() : view_mode(VIEW_MODE_FREE), xhandle(0), yhandle(0), rot(0), xscale(1), yscale(1), captured(false) +{ + setRenderSpace(RENDER_SPACE_WORLD); + mesh_index = allocIndex(); } -Sprite::Sprite( const Sprite &t ): -Model(t), -view_mode(t.view_mode), -xhandle(t.xhandle),yhandle(t.yhandle), -rot(t.rot),xscale(t.xscale),yscale(t.yscale),captured(false){ - mesh_index=allocIndex(); +Sprite::Sprite(const Sprite& t) + : Model(t), view_mode(t.view_mode), xhandle(t.xhandle), yhandle(t.yhandle), rot(t.rot), xscale(t.xscale), + yscale(t.yscale), captured(false) +{ + mesh_index = allocIndex(); } -Sprite::~Sprite(){ - freeIndex( mesh_index ); +Sprite::~Sprite() +{ + freeIndex(mesh_index); } -void Sprite::setRotation( float angle ){ - rot=angle; +void Sprite::setRotation(float angle) +{ + rot = angle; } -void Sprite::setScale( float x,float y ){ - xscale=x;yscale=y; +void Sprite::setScale(float x, float y) +{ + xscale = x; + yscale = y; } -void Sprite::setHandle( float x,float y ){ - xhandle=x;yhandle=y; +void Sprite::setHandle(float x, float y) +{ + xhandle = x; + yhandle = y; } -void Sprite::setViewmode( int mode ){ - view_mode=mode; +void Sprite::setViewmode(int mode) +{ + view_mode = mode; } -void Sprite::capture(){ +void Sprite::capture() +{ Model::capture(); - r_rot=rot; - r_xscale=xscale; - r_yscale=yscale; - captured=true; + r_rot = rot; + r_xscale = xscale; + r_yscale = yscale; + captured = true; } -bool Sprite::beginRender( float tween ){ - Model::beginRender( tween ); - if( tween==1 || !captured ){ - r_rot=rot; - r_xscale=xscale; - r_yscale=yscale; - }else{ - r_rot=(rot-r_rot)*tween+r_rot; - r_xscale=(xscale-r_xscale)*tween+r_xscale; - r_yscale=(yscale-r_yscale)*tween+r_yscale; +bool Sprite::beginRender(float tween) +{ + Model::beginRender(tween); + if (tween == 1 || !captured) { + r_rot = rot; + r_xscale = xscale; + r_yscale = yscale; + } else { + r_rot = (rot - r_rot) * tween + r_rot; + r_xscale = (xscale - r_xscale) * tween + r_xscale; + r_yscale = (yscale - r_yscale) * tween + r_yscale; } return true; } -bool Sprite::render( const RenderContext &rc ){ +bool Sprite::render(const RenderContext& rc) +{ + Transform t = getRenderTform(); - Transform t=getRenderTform(); - - if( view_mode==VIEW_MODE_FREE ){ - t.m=rc.getCameraTform().m; - }else if( view_mode==VIEW_MODE_UPRIGHT ){ - t.m.k=rc.getCameraTform().m.k;t.m.orthogonalize(); - }else if( view_mode==VIEW_MODE_UPRIGHT2 ){ - t.m=yawMatrix( matrixYaw( rc.getCameraTform().m ) ) * t.m; + if (view_mode == VIEW_MODE_FREE) { + t.m = rc.getCameraTform().m; + } else if (view_mode == VIEW_MODE_UPRIGHT) { + t.m.k = rc.getCameraTform().m.k; + t.m.orthogonalize(); + } else if (view_mode == VIEW_MODE_UPRIGHT2) { + t.m = yawMatrix(matrixYaw(rc.getCameraTform().m)) * t.m; } - t.m=t.m * rollMatrix( r_rot ) * scaleMatrix( r_xscale,r_yscale,1 ); + t.m = t.m * rollMatrix(r_rot) * scaleMatrix(r_xscale, r_yscale, 1); static Vector verts[4]; - verts[0]=t * Vector( -1-xhandle, 1-yhandle,0 ); - verts[1]=t * Vector( 1-xhandle, 1-yhandle,0 ); - verts[2]=t * Vector( 1-xhandle,-1-yhandle,0 ); - verts[3]=t * Vector( -1-xhandle,-1-yhandle,0 ); + verts[0] = t * Vector(-1 - xhandle, 1 - yhandle, 0); + verts[1] = t * Vector(1 - xhandle, 1 - yhandle, 0); + verts[2] = t * Vector(1 - xhandle, -1 - yhandle, 0); + verts[3] = t * Vector(-1 - xhandle, -1 - yhandle, 0); - if( !rc.getWorldFrustum().cull( verts,4 ) ) return false; + if (!rc.getWorldFrustum().cull(verts, 4)) + return false; - mesh->lock( false ); - int fv=mesh_index*4,ft=mesh_index*2; - mesh->setVertex( fv+0,&verts[0].x,null,tex_coords0 ); - mesh->setVertex( fv+1,&verts[1].x,null,tex_coords1 ); - mesh->setVertex( fv+2,&verts[2].x,null,tex_coords2 ); - mesh->setVertex( fv+3,&verts[3].x,null,tex_coords3 ); - if( rc.isReflected() ){ - mesh->setTriangle( ft+0,0,2,1 ); - mesh->setTriangle( ft+1,0,3,2 ); - }else{ - mesh->setTriangle( ft+0,0,1,2 ); - mesh->setTriangle( ft+1,0,2,3 ); + mesh->lock(false); + int fv = mesh_index * 4, ft = mesh_index * 2; + mesh->setVertex(fv + 0, &verts[0].x, null, tex_coords0); + mesh->setVertex(fv + 1, &verts[1].x, null, tex_coords1); + mesh->setVertex(fv + 2, &verts[2].x, null, tex_coords2); + mesh->setVertex(fv + 3, &verts[3].x, null, tex_coords3); + if (rc.isReflected()) { + mesh->setTriangle(ft + 0, 0, 2, 1); + mesh->setTriangle(ft + 1, 0, 3, 2); + } else { + mesh->setTriangle(ft + 0, 0, 1, 2); + mesh->setTriangle(ft + 1, 0, 2, 3); } mesh->unlock(); - enqueue( mesh,fv,4,ft,2 ); + enqueue(mesh, fv, 4, ft, 2); return false; } diff --git a/Runtime/blitz3d/sprite.hpp b/Runtime/blitz3d/sprite.hpp index a424df5..da26997 100644 --- a/Runtime/blitz3d/sprite.hpp +++ b/Runtime/blitz3d/sprite.hpp @@ -2,43 +2,49 @@ #ifndef SPRITE_H #define SPRITE_H -#include "model.hpp" #include "brush.hpp" -#include "../gxruntime/gxmesh.hpp" +#include "gxmesh.hpp" +#include "model.hpp" -class Sprite : public Model{ -public: - enum{ - VIEW_MODE_FREE=1, //visible from any angle - VIEW_MODE_FIXED=2, //visible only from front - VIEW_MODE_UPRIGHT=3, //upright tree-style - VIEW_MODE_UPRIGHT2=4 //better upright tree-style +class Sprite : public Model { + public: + enum { + VIEW_MODE_FREE = 1, //visible from any angle + VIEW_MODE_FIXED = 2, //visible only from front + VIEW_MODE_UPRIGHT = 3, //upright tree-style + VIEW_MODE_UPRIGHT2 = 4 //better upright tree-style }; Sprite(); - Sprite( const Sprite &t ); + Sprite(const Sprite& t); ~Sprite(); - Sprite *getSprite(){ return this; } + Sprite* getSprite() + { + return this; + } - Entity *clone(){ return new Sprite( *this ); } + Entity* clone() + { + return new Sprite(*this); + } void capture(); - bool beginRender( float tween ); + bool beginRender(float tween); - void setRotation( float angle ); - void setScale( float x_scale,float y_scale ); - void setHandle( float x,float y ); - void setViewmode( int mode ); + void setRotation(float angle); + void setScale(float x_scale, float y_scale); + void setHandle(float x, float y); + void setViewmode(int mode); - bool render( const RenderContext &rc ); + bool render(const RenderContext& rc); -private: - float xhandle,yhandle; - float rot,xscale,yscale; - float r_rot,r_xscale,r_yscale; - int view_mode,mesh_index; - bool captured; + private: + float xhandle, yhandle; + float rot, xscale, yscale; + float r_rot, r_xscale, r_yscale; + int view_mode, mesh_index; + bool captured; }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/std.hpp b/Runtime/blitz3d/std.hpp index bf52761..1fa9af2 100644 --- a/Runtime/blitz3d/std.hpp +++ b/Runtime/blitz3d/std.hpp @@ -2,18 +2,18 @@ #ifndef STD_H #define STD_H -#pragma warning( disable:4786 ) +#pragma warning(disable : 4786) #include "config.hpp" -#include "stdutil.hpp" #include "gxruntime.hpp" +#include "stdutil.hpp" -#include +#include +#include #include +#include #include #include -#include -#include using namespace std; diff --git a/Runtime/blitz3d/surface.cpp b/Runtime/blitz3d/surface.cpp index 0a3434b..2ca6465 100644 --- a/Runtime/blitz3d/surface.cpp +++ b/Runtime/blitz3d/surface.cpp @@ -1,155 +1,186 @@ -#include "std.hpp" #include "surface.hpp" +#include "std.hpp" -extern gxGraphics *gx_graphics; +extern gxGraphics* gx_graphics; static Surface::Monitor nop_mon; -Surface::Surface(): -mesh(0),mesh_vs(0),mesh_ts(0),valid_vs(0),valid_ts(0),mon( &nop_mon ){ +Surface::Surface() : mesh(0), mesh_vs(0), mesh_ts(0), valid_vs(0), valid_ts(0), mon(&nop_mon) {} + +Surface::Surface(Monitor* m) : mesh(0), mesh_vs(0), mesh_ts(0), valid_vs(0), valid_ts(0), mon(m) {} + +Surface::~Surface() +{ + if (mesh) + gx_graphics->freeMesh(mesh); } -Surface::Surface( Monitor *m ): -mesh(0),mesh_vs(0),mesh_ts(0),valid_vs(0),valid_ts(0),mon(m){ -} - -Surface::~Surface(){ - if( mesh ) gx_graphics->freeMesh( mesh ); -} - -void Surface::setBrush( const Brush &b ){ - brush=b; +void Surface::setBrush(const Brush& b) +{ + brush = b; ++mon->brush_changes; } -void Surface::setName( const string &n ){ - name=n; +void Surface::setName(const string& n) +{ + name = n; } -void Surface::clear( bool verts,bool tris ){ - if( verts ){ vertices.clear();valid_vs=0; } - if( tris ){ triangles.clear();valid_ts=0; } +void Surface::clear(bool verts, bool tris) +{ + if (verts) { + vertices.clear(); + valid_vs = 0; + } + if (tris) { + triangles.clear(); + valid_ts = 0; + } ++mon->geom_changes; } -void Surface::addVertices( const vector &verts ){ - vertices.insert( vertices.end(),verts.begin(),verts.end() ); +void Surface::addVertices(const vector& verts) +{ + vertices.insert(vertices.end(), verts.begin(), verts.end()); ++mon->geom_changes; } -void Surface::setColor( int n,const Vector &v ){ - int r=floor(v.x*255);if(r<0)r=0;else if(r>255)r=255; - int g=floor(v.y*255);if(g<0)g=0;else if(g>255)g=255; - int b=floor(v.z*255);if(b<0)b=0;else if(b>255)b=255; +void Surface::setColor(int n, const Vector& v) +{ + int r = floor(v.x * 255); + if (r < 0) + r = 0; + else if (r > 255) + r = 255; + int g = floor(v.y * 255); + if (g < 0) + g = 0; + else if (g > 255) + g = 255; + int b = floor(v.z * 255); + if (b < 0) + b = 0; + else if (b > 255) + b = 255; - unsigned argb=0xff000000|(r<<16)|(g<<8)|b; + unsigned argb = 0xff000000 | (r << 16) | (g << 8) | b; - vertices[n].color=argb; - if( n>16; - float g=(vertices[n].color&0x0000ff00)>>8; - float b= vertices[n].color&0x000000ff; - return Vector( r/255.0f,g/255.0f,b/255.0f ); +Vector Surface::getColor(int n) const +{ + float r = (vertices[n].color & 0x00ff0000) >> 16; + float g = (vertices[n].color & 0x0000ff00) >> 8; + float b = vertices[n].color & 0x000000ff; + return Vector(r / 255.0f, g / 255.0f, b / 255.0f); } -void Surface::addTriangles( const vector &tris ){ - triangles.insert( triangles.end(),tris.begin(),tris.end() ); +void Surface::addTriangles(const vector& tris) +{ + triangles.insert(triangles.end(), tris.begin(), tris.end()); } -void Surface::updateNormals(){ - int k; - map norm_map; - for( k=0;k norm_map; + for (k = 0; k < triangles.size(); ++k) { + const Triangle& t = triangles[k]; + const Vector& v0 = vertices[t.verts[0]].coords; + const Vector& v1 = vertices[t.verts[1]].coords; + const Vector& v2 = vertices[t.verts[2]].coords; + Vector n = (v1 - v0).cross(v2 - v0); + if (n.length() <= FLT_EPSILON) + continue; n.normalize(); - norm_map[v0]+=n; - norm_map[v1]+=n; - norm_map[v2]+=n; + norm_map[v0] += n; + norm_map[v1] += n; + norm_map[v2] += n; } - for( k=0;knormal=norm_map[v->coords].normalized(); + for (k = 0; k < vertices.size(); ++k) { + Vertex* v = &vertices[k]; + v->normal = norm_map[v->coords].normalized(); } } -gxMesh *Surface::getMesh(){ - if( mesh && mesh->dirty() ) valid_vs=0; +gxMesh* Surface::getMesh() +{ + if (mesh && mesh->dirty()) + valid_vs = 0; - if( valid_vs==vertices.size() && valid_ts==triangles.size() ) return mesh; + if (valid_vs == vertices.size() && valid_ts == triangles.size()) + return mesh; - valid_vs=valid_ts=0; + valid_vs = valid_ts = 0; - if( mesh_vsfreeMesh( mesh ); - mesh_vs=vertices.size()+mesh_vs/2; - mesh_ts=triangles.size()+mesh_ts/2; - }else{ - mesh_vs=vertices.size(); - mesh_ts=triangles.size(); + if (mesh_vs < vertices.size() || mesh_ts < triangles.size()) { + if (mesh) { + gx_graphics->freeMesh(mesh); + mesh_vs = vertices.size() + mesh_vs / 2; + mesh_ts = triangles.size() + mesh_ts / 2; + } else { + mesh_vs = vertices.size(); + mesh_ts = triangles.size(); } - mesh=gx_graphics->createMesh( mesh_vs,mesh_ts,0 ); + mesh = gx_graphics->createMesh(mesh_vs, mesh_ts, 0); } - mesh->lock( true ); - for( ;valid_vssetVertex( valid_vs,&vertices[valid_vs] ); + mesh->lock(true); + for (; valid_vs < vertices.size(); ++valid_vs) { + mesh->setVertex(valid_vs, &vertices[valid_vs]); } - for( ;valid_tssetTriangle( valid_ts,t.verts[0],t.verts[1],t.verts[2] ); + for (; valid_ts < triangles.size(); ++valid_ts) { + const Triangle& t = triangles[valid_ts]; + mesh->setTriangle(valid_ts, t.verts[0], t.verts[1], t.verts[2]); } mesh->unlock(); return mesh; } -gxMesh *Surface::getMesh( const vector &bones ){ +gxMesh* Surface::getMesh(const vector& bones) +{ + valid_vs = valid_ts = 0; - valid_vs=valid_ts=0; - - if( mesh_vsfreeMesh( mesh ); - mesh_vs=vertices.size(); - mesh_ts=triangles.size(); - mesh=gx_graphics->createMesh( mesh_vs,mesh_ts,0 ); + if (mesh_vs < vertices.size() || mesh_ts < triangles.size()) { + if (mesh) + gx_graphics->freeMesh(mesh); + mesh_vs = vertices.size(); + mesh_ts = triangles.size(); + mesh = gx_graphics->createMesh(mesh_vs, mesh_ts, 0); } - mesh->lock( true ); - for( ;valid_vslock(true); + for (; valid_vs < vertices.size(); ++valid_vs) { + const Vertex& v = vertices[valid_vs]; + if (v.bone_bones[0] == 255) { //no bone! - const Bone &bone=bones[0]; - mesh->setVertex( valid_vs,bone.coord_tform * v.coords,bone.normal_tform * v.normal,v.color,v.tex_coords ); - }else if( v.bone_bones[1]==255 ){ + const Bone& bone = bones[0]; + mesh->setVertex(valid_vs, bone.coord_tform * v.coords, bone.normal_tform * v.normal, v.color, v.tex_coords); + } else if (v.bone_bones[1] == 255) { //one bone only - const Bone &bone=bones[v.bone_bones[0]]; - mesh->setVertex( valid_vs,bone.coord_tform * v.coords,bone.normal_tform * v.normal,v.color,v.tex_coords ); - }else{ - const Vertex &v=vertices[valid_vs]; + const Bone& bone = bones[v.bone_bones[0]]; + mesh->setVertex(valid_vs, bone.coord_tform * v.coords, bone.normal_tform * v.normal, v.color, v.tex_coords); + } else { + const Vertex& v = vertices[valid_vs]; //two or more bones - Vector tv,tn; - for( int n=0;nsetVertex( valid_vs,tv,tn.normalized(),v.color,v.tex_coords ); + mesh->setVertex(valid_vs, tv, tn.normalized(), v.color, v.tex_coords); } } - for( ;valid_tssetTriangle( valid_ts,t.verts[0],t.verts[1],t.verts[2] ); + for (; valid_ts < triangles.size(); ++valid_ts) { + const Triangle& t = triangles[valid_ts]; + mesh->setTriangle(valid_ts, t.verts[0], t.verts[1], t.verts[2]); } mesh->unlock(); return mesh; @@ -235,4 +266,3 @@ gxMesh *Surface::getMesh( const vector &bones,gxMesh *mesh ){ } */ - diff --git a/Runtime/blitz3d/surface.hpp b/Runtime/blitz3d/surface.hpp index 43a5804..d3bb5bc 100644 --- a/Runtime/blitz3d/surface.hpp +++ b/Runtime/blitz3d/surface.hpp @@ -6,111 +6,145 @@ #define MAX_SURFACE_BONES 4 -class Surface{ -public: - struct Vertex{ - Vector coords; - Vector normal; - unsigned color; - float tex_coords[2][2]; +class Surface { + public: + struct Vertex { + Vector coords; + Vector normal; + unsigned color; + float tex_coords[2][2]; unsigned char bone_bones[MAX_SURFACE_BONES]; - float bone_weights[MAX_SURFACE_BONES]; + float bone_weights[MAX_SURFACE_BONES]; - Vertex():color(~0){ - bone_bones[0]=255; - memset(tex_coords,0,sizeof(tex_coords)); + Vertex() : color(~0) + { + bone_bones[0] = 255; + memset(tex_coords, 0, sizeof(tex_coords)); } - - bool operator<( const Vertex &t )const{ - return memcmp( this,&t,sizeof(*this) )==-1; + + bool operator<(const Vertex& t) const + { + return memcmp(this, &t, sizeof(*this)) == -1; } }; - struct Triangle{ + struct Triangle { unsigned short verts[3]; }; - struct Bone{ + struct Bone { Transform coord_tform; - Matrix normal_tform; + Matrix normal_tform; }; - struct Monitor{ - int brush_changes,geom_changes; + struct Monitor { + int brush_changes, geom_changes; }; Surface(); - Surface( Monitor *mon ); + Surface(Monitor* mon); ~Surface(); - void setName( const string &t ); - void setBrush( const Brush &b ); + void setName(const string& t); + void setBrush(const Brush& b); - void clear( bool verts,bool tris ); + void clear(bool verts, bool tris); - void addVertex( const Vertex &v ){ + void addVertex(const Vertex& v) + { vertices.push_back(v); ++mon->geom_changes; } - void setVertex( int n,const Vertex &v ){ - vertices[n]=v; - if( ngeom_changes; } - void setCoords( int n,const Vector &v ){ - vertices[n].coords=v; - if( ngeom_changes; } - void setNormal( int n,const Vector &v ){ - vertices[n].normal=v; - if( ngeom_changes; } - void setTriangle( int n,const Triangle &t ){ - triangles[n]=t; - if( ngeom_changes; } - Vector getColor( int index )const; - void setColor( int index,const Vector &v ); - void addVertices( const vector &verts ); - void addTriangles( const vector &tris ); + Vector getColor(int index) const; + void setColor(int index, const Vector& v); + void addVertices(const vector& verts); + void addTriangles(const vector& tris); void updateNormals(); - gxMesh *getMesh(); - gxMesh *getMesh( const vector &bones ); + gxMesh* getMesh(); + gxMesh* getMesh(const vector& bones); - string getName()const{ return name; } - const Brush &getBrush()const{ return brush; } - int numVertices()const{ return vertices.size(); } - int numTriangles()const{ return triangles.size(); } - const Vertex &getVertex( int n )const{ return vertices[n]; } - const Triangle &getTriangle( int n )const{ return triangles[n]; } + string getName() const + { + return name; + } + const Brush& getBrush() const + { + return brush; + } + int numVertices() const + { + return vertices.size(); + } + int numTriangles() const + { + return triangles.size(); + } + const Vertex& getVertex(int n) const + { + return vertices[n]; + } + const Triangle& getTriangle(int n) const + { + return triangles[n]; + } -private: - Brush brush; - string name; - gxMesh *mesh; - vector vertices; + private: + Brush brush; + string name; + gxMesh* mesh; + vector vertices; vector triangles; - int mesh_vs,mesh_ts; - int valid_vs,valid_ts; - Monitor *mon; + int mesh_vs, mesh_ts; + int valid_vs, valid_ts; + Monitor* mon; }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/terrain.cpp b/Runtime/blitz3d/terrain.cpp index 23b7285..2fd3eec 100644 --- a/Runtime/blitz3d/terrain.cpp +++ b/Runtime/blitz3d/terrain.cpp @@ -1,41 +1,48 @@ -#include "std.hpp" #include "terrain.hpp" +#include "std.hpp" #include "terrainrep.hpp" -Terrain::Terrain( int size_shift ): -rep( new TerrainRep( size_shift ) ){ -} +Terrain::Terrain(int size_shift) : rep(new TerrainRep(size_shift)) {} -Terrain::~Terrain(){ +Terrain::~Terrain() +{ delete rep; } -void Terrain::setDetail( int n,bool m ){ - rep->setDetail( n,m ); +void Terrain::setDetail(int n, bool m) +{ + rep->setDetail(n, m); } -void Terrain::setShading( bool t ){ - rep->setShading( t ); +void Terrain::setShading(bool t) +{ + rep->setShading(t); } -void Terrain::setHeight( int x,int z,float h,bool realtime ){ - if( x>=0 && z>=0 && x<=rep->getSize() && z<=rep->getSize() ) rep->setHeight( x,z,h,realtime ); +void Terrain::setHeight(int x, int z, float h, bool realtime) +{ + if (x >= 0 && z >= 0 && x <= rep->getSize() && z <= rep->getSize()) + rep->setHeight(x, z, h, realtime); } -int Terrain::getSize()const{ +int Terrain::getSize() const +{ return rep->getSize(); } -float Terrain::getHeight( int x,int z )const{ - return (x>=0 && z>=0 && x<=rep->getSize() && z<=rep->getSize() ) ? rep->getHeight( x,z ) : 0; +float Terrain::getHeight(int x, int z) const +{ + return (x >= 0 && z >= 0 && x <= rep->getSize() && z <= rep->getSize()) ? rep->getHeight(x, z) : 0; } -bool Terrain::render( const RenderContext &rc ){ - rep->render( this,rc ); +bool Terrain::render(const RenderContext& rc) +{ + rep->render(this, rc); return false; } -bool Terrain::collide( const Line &line,float radius,Collision *curr_coll,const Transform &tf ){ - return rep->collide( line,radius,curr_coll,tf ); +bool Terrain::collide(const Line& line, float radius, Collision* curr_coll, const Transform& tf) +{ + return rep->collide(line, radius, curr_coll, tf); } diff --git a/Runtime/blitz3d/terrain.hpp b/Runtime/blitz3d/terrain.hpp index 7838b70..509cee9 100644 --- a/Runtime/blitz3d/terrain.hpp +++ b/Runtime/blitz3d/terrain.hpp @@ -6,28 +6,31 @@ struct TerrainRep; -class Terrain : public Model{ -public: - Terrain( int size_shift ); +class Terrain : public Model { + public: + Terrain(int size_shift); ~Terrain(); - Terrain *getTerrain(){ return this; } + Terrain* getTerrain() + { + return this; + } - void setDetail( int n,bool morph ); - void setHeight( int x,int z,float h,bool realtime ); - void setShading( bool shading ); + void setDetail(int n, bool morph); + void setHeight(int x, int z, float h, bool realtime); + void setShading(bool shading); - int getSize()const; - float getHeight( int x,int z )const; + int getSize() const; + float getHeight(int x, int z) const; //model interface - bool render( const RenderContext &rc ); + bool render(const RenderContext& rc); //object interface - bool collide( const Line &line,float radius,Collision *curr_coll,const Transform &tf ); - -private: - TerrainRep *rep; + bool collide(const Line& line, float radius, Collision* curr_coll, const Transform& tf); + + private: + TerrainRep* rep; }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/terrainrep.cpp b/Runtime/blitz3d/terrainrep.cpp index 7603414..9afb3cd 100644 --- a/Runtime/blitz3d/terrainrep.cpp +++ b/Runtime/blitz3d/terrainrep.cpp @@ -1,220 +1,240 @@ -#include "std.hpp" #include "terrainrep.hpp" #include +#include "std.hpp" -extern gxRuntime *gx_runtime; -extern gxGraphics *gx_graphics; -extern float stats3d[10]; +extern gxRuntime* gx_runtime; +extern gxGraphics* gx_graphics; +extern float stats3d[10]; -static Vector eye_vec; -static Plane eye_plane; -static const Vector up_normal( 0,1,0 ); -static TerrainRep::Tri *tri_pool; -static const TerrainRep *curr; -static Frustum frustum; -static int out_cnt,proc_cnt,clip_cnt; +static Vector eye_vec; +static Plane eye_plane; +static const Vector up_normal(0, 1, 0); +static TerrainRep::Tri* tri_pool; +static const TerrainRep* curr; +static Frustum frustum; +static int out_cnt, proc_cnt, clip_cnt; -static float proj_epsilon= FLT_EPSILON; //.01f; +static float proj_epsilon = FLT_EPSILON; //.01f; -struct TerrainRep::Cell{ +struct TerrainRep::Cell { unsigned char height; }; -struct TerrainRep::Error{ - unsigned char error,bound; +struct TerrainRep::Error { + unsigned char error, bound; }; -int TerrainRep::getSize()const{ +int TerrainRep::getSize() const +{ return cell_size; } -float TerrainRep::getHeight( int x,int z )const{ - return cells[((z&cell_mask)<getHeight(x,z),(float)z){ - src_y=v.y; - } - Vert(int x, int z, float sy) :x(x), z(z), v((float)x, curr->getHeight(x, z), (float)z), src_y(sy) { + Vert() {} + Vert(int x, int z) : x(x), z(z), v((float)x, curr->getHeight(x, z), (float)z) + { + src_y = v.y; } + Vert(int x, int z, float sy) : x(x), z(z), v((float)x, curr->getHeight(x, z), (float)z), src_y(sy) {} }; -static int vert_cnt,max_verts; -static TerrainRep::Vert *verts,*next_vert; +static int vert_cnt, max_verts; +static TerrainRep::Vert *verts, *next_vert; -struct TerrainRep::Tri{ - int id; - short clip,v0,v1,v2; - Tri *e0,*e1,*e2; +struct TerrainRep::Tri { + int id; + short clip, v0, v1, v2; + Tri * e0, *e1, *e2; float proj_err; - Tri(){ - } - Tri( int id,int clip,int v0,int v1,int v2,Tri *e0=0,Tri *e1=0,Tri *e2=0 ): - id(id),clip(clip), - v0(v0),v1(v1),v2(v2), - e0(e0),e1(e1),e2(e2),proj_err(0){ - } + Tri() {} + Tri(int id, int clip, int v0, int v1, int v2, Tri* e0 = 0, Tri* e1 = 0, Tri* e2 = 0) + : id(id), clip(clip), v0(v0), v1(v1), v2(v2), e0(e0), e1(e1), e2(e2), proj_err(0) + {} - void *operator new( size_t sz ){ - static const int GROW=64; - if( !tri_pool ){ - tri_pool=new Tri[GROW]; - for( int k=0;ke0; + Tri* t = tri_pool; + tri_pool = t->e0; return t; } - void operator delete( void *q ){ - Tri *t=(Tri*)q; - t->e0=tri_pool; - tri_pool=t; + void operator delete(void* q) + { + Tri* t = (Tri*)q; + t->e0 = tri_pool; + tri_pool = t; } - void unlink(){ - if( e0 ){ - if( e0->e0==this ) e0->e0=0; - else if( e0->e1==this ) e0->e1=0; - else e0->e2=0; + void unlink() + { + if (e0) { + if (e0->e0 == this) + e0->e0 = 0; + else if (e0->e1 == this) + e0->e1 = 0; + else + e0->e2 = 0; } - if( e1 ){ - if( e1->e0==this ) e1->e0=0; - else if( e1->e1==this ) e1->e1=0; - else e1->e2=0; + if (e1) { + if (e1->e0 == this) + e1->e0 = 0; + else if (e1->e1 == this) + e1->e1 = 0; + else + e1->e2 = 0; } - if( e2 ){ - if( e2->e0==this ) e2->e0=0; - else if( e2->e1==this ) e2->e1=0; - else e2->e2=0; + if (e2) { + if (e2->e0 == this) + e2->e0 = 0; + else if (e2->e1 == this) + e2->e1 = 0; + else + e2->e2 = 0; } } }; -struct TriComp{ - bool operator()( TerrainRep::Tri *a,TerrainRep::Tri *b )const{ return a->proj_errproj_err; } +struct TriComp { + bool operator()(TerrainRep::Tri* a, TerrainRep::Tri* b) const + { + return a->proj_err < b->proj_err; + } }; -struct TriQue : public priority_queue,TriComp>{ - vector &getVector(){ return c; } - const vector &getVector()const{ return c; } +struct TriQue : public priority_queue, TriComp> { + vector& getVector() + { + return c; + } + const vector& getVector() const + { + return c; + } }; -static TriQue tri_que; +static TriQue tri_que; static vector tris; -static bool clip( const Line &l,const Box &box ){ - static const Vector normals[]={ - Vector( 1,0,0 ), - Vector( 0,0,1 ), - Vector( 0,-1,0 ), - Vector( -1,0,0 ), - Vector( 0,0,-1 ), - Vector( 0,1,0 ) - }; - Vector v0=l.o,v1=l.o+l.d; - for( int k=0;k<6;++k ){ - Vector t=box.corner(k); - const Vector &n=normals[k]; - float d0=n.dot( v0-t ),d1=n.dot( v1-t ); - if( d0<0 ){ - if( d1<0 ) return false; - v0+=(v1-v0)*( d0/(d0-d1) ); - }else if( d1<0 ){ - v1+=(v0-v1)*( d1/(d1-d0) ); +static bool clip(const Line& l, const Box& box) +{ + static const Vector normals[] = {Vector(1, 0, 0), Vector(0, 0, 1), Vector(0, -1, 0), + Vector(-1, 0, 0), Vector(0, 0, -1), Vector(0, 1, 0)}; + Vector v0 = l.o, v1 = l.o + l.d; + for (int k = 0; k < 6; ++k) { + Vector t = box.corner(k); + const Vector& n = normals[k]; + float d0 = n.dot(v0 - t), d1 = n.dot(v1 - t); + if (d0 < 0) { + if (d1 < 0) + return false; + v0 += (v1 - v0) * (d0 / (d0 - d1)); + } else if (d1 < 0) { + v1 += (v0 - v1) * (d1 / (d1 - d0)); } } return true; } -TerrainRep::TerrainRep( int n ): -cell_shift(n),cell_size(1<freeMesh( mesh ); +TerrainRep::~TerrainRep() +{ + if (mesh) + gx_graphics->freeMesh(mesh); delete[] errors; delete[] cells; } -void TerrainRep::clear(){ - memset( cells,0,cell_size*cell_size*sizeof(Cell) ); - memset( errors,0,end_tri_id*sizeof(Error) ); - errs_valid=true; +void TerrainRep::clear() +{ + memset(cells, 0, cell_size * cell_size * sizeof(Cell)); + memset(errors, 0, end_tri_id * sizeof(Error)); + errs_valid = true; } -void TerrainRep::setDetail( int n,bool m ){ - morph=m; - if( n==detail ) return; - detail=n; +void TerrainRep::setDetail(int n, bool m) +{ + morph = m; + if (n == detail) + return; + detail = n; - n+=32; - if( n>max_verts ){ + n += 32; + if (n > max_verts) { delete[] verts; - max_verts=n; - verts=new Vert[max_verts]; + max_verts = n; + verts = new Vert[max_verts]; } - if( mesh ) gx_graphics->freeMesh( mesh ); - mesh_verts=mesh_tris=n; - mesh=gx_graphics->createMesh( mesh_verts,mesh_tris,0 ); + if (mesh) + gx_graphics->freeMesh(mesh); + mesh_verts = mesh_tris = n; + mesh = gx_graphics->createMesh(mesh_verts, mesh_tris, 0); } -void TerrainRep::setShading( bool t ){ - shading=t; +void TerrainRep::setShading(bool t) +{ + shading = t; } -void TerrainRep::setHeight( int x,int z,float h,bool realtime ){ - cells[((z&cell_mask)<id>=end_tri_id || !errors[t->id].error ){ - if( t->clip & 63 ){ + if (t->id >= end_tri_id || !errors[t->id].error) { + if (t->clip & 63) { ++clip_cnt; - Vector e0( verts[t->v0].v ),e1( verts[t->v1].v ),e2( verts[t->v2].v ); - for( int n=0;n<6;++n ){ - if( !( t->clip & (1<v0].v), e1(verts[t->v1].v), e2(verts[t->v2].v); + for (int n = 0; n < 6; ++n) { + if (!(t->clip & (1 << n))) + continue; + const Plane& p = frustum.getPlane(n); + if (p.distance(e0) < 0 && p.distance(e1) < 0 && p.distance(e2) < 0) { t->unlink(); delete t; ++out_cnt; @@ -222,361 +242,412 @@ void TerrainRep::insert( Tri *t ){ } } } - t->clip|=128; - tris.push_back( t ); + t->clip |= 128; + tris.push_back(t); ++out_cnt; return; } //clip? - if( t->idclip & 63) ){ + if (t->id < end_tri_id / 2 && (t->clip & 63)) { ++clip_cnt; - Vector e0( verts[t->v0].v ),e1( verts[t->v1].v ),e2( verts[t->v2].v ); - Vector e3(e0),e4(e1),e5(e2); - e0.y=e1.y=e2.y=0; - e3.y=e4.y=e5.y=errors[t->id].bound/255.0f; - for( int n=0;n<6;++n ){ - int mask=1<clip & mask) ) continue; - const Plane &p=frustum.getPlane( n ); - int q= - (p.distance( e0 )>=0)+(p.distance( e1 )>=0)+(p.distance( e2 )>=0)+ - (p.distance( e3 )>=0)+(p.distance( e4 )>=0)+(p.distance( e5 )>=0); - if( !q ){ + Vector e0(verts[t->v0].v), e1(verts[t->v1].v), e2(verts[t->v2].v); + Vector e3(e0), e4(e1), e5(e2); + e0.y = e1.y = e2.y = 0; + e3.y = e4.y = e5.y = errors[t->id].bound / 255.0f; + for (int n = 0; n < 6; ++n) { + int mask = 1 << n; + if (!(t->clip & mask)) + continue; + const Plane& p = frustum.getPlane(n); + int q = (p.distance(e0) >= 0) + (p.distance(e1) >= 0) + (p.distance(e2) >= 0) + (p.distance(e3) >= 0) + + (p.distance(e4) >= 0) + (p.distance(e5) >= 0); + if (!q) { t->unlink(); delete t; ++out_cnt; return; } - if( q==6 ) t->clip&=~mask; + if (q == 6) + t->clip &= ~mask; } } - if( t->clip & 128 ){ - t->clip|=128; - tris.push_back( t ); - }else{ - Vector v=Vector( verts[t->v1].v+verts[t->v2].v )/2; -// float d=eye_plane.distance( v ); - float d=eye_vec.distance( v ); - if( dproj_err=errors[t->id].error/d; - if( t->proj_err>proj_epsilon ){ - tri_que.push( t ); - }else{ - t->clip|=128; - tris.push_back( t ); + if (t->clip & 128) { + t->clip |= 128; + tris.push_back(t); + } else { + Vector v = Vector(verts[t->v1].v + verts[t->v2].v) / 2; + // float d=eye_plane.distance( v ); + float d = eye_vec.distance(v); + if (d < FLT_EPSILON) + d = FLT_EPSILON; + t->proj_err = errors[t->id].error / d; + if (t->proj_err > proj_epsilon) { + tri_que.push(t); + } else { + t->clip |= 128; + tris.push_back(t); } } ++out_cnt; } -void TerrainRep::split( Tri *t ){ +void TerrainRep::split(Tri* t) +{ + if (t->e2 && t->e2->e2 != t) + split(t->e2); - if( t->e2 && t->e2->e2!=t ) split( t->e2 ); - - int tv=vert_cnt++; - if( tv>=max_verts ){ - max_verts+=max_verts/2+32; - Vert *t=verts; - verts=new Vert[max_verts]; - memcpy( verts,t,sizeof(Vert)*tv ); - next_vert=verts+tv; + int tv = vert_cnt++; + if (tv >= max_verts) { + max_verts += max_verts / 2 + 32; + Vert* t = verts; + verts = new Vert[max_verts]; + memcpy(verts, t, sizeof(Vert) * tv); + next_vert = verts + tv; } - Vert *vert=next_vert++; - vert->v.x=vert->x=(verts[t->v1].x+verts[t->v2].x)/2; - vert->v.z=vert->z=(verts[t->v1].z+verts[t->v2].z)/2; - vert->src_y=(verts[t->v1].v.y+verts[t->v2].v.y)/2; - vert->v.y=getHeight( vert->x,vert->z ); + Vert* vert = next_vert++; + vert->v.x = vert->x = (verts[t->v1].x + verts[t->v2].x) / 2; + vert->v.z = vert->z = (verts[t->v1].z + verts[t->v2].z) / 2; + vert->src_y = (verts[t->v1].v.y + verts[t->v2].v.y) / 2; + vert->v.y = getHeight(vert->x, vert->z); - Tri *tl=new Tri( t->id*2,t->clip,tv,t->v2,t->v0,0,0,t->e0 ); - if( Tri *p=tl->e2 ){ - if( p->e0==t ) p->e0=tl; - else if( p->e1==t ) p->e1=tl; - else p->e2=tl; + Tri* tl = new Tri(t->id * 2, t->clip, tv, t->v2, t->v0, 0, 0, t->e0); + if (Tri* p = tl->e2) { + if (p->e0 == t) + p->e0 = tl; + else if (p->e1 == t) + p->e1 = tl; + else + p->e2 = tl; } - Tri *tr=new Tri( t->id*2+1,t->clip,tv,t->v0,t->v1,0,tl,t->e1 ); - tl->e0=tr; - if( Tri *p=tr->e2 ){ - if( p->e0==t ) p->e0=tr; - else if( p->e1==t ) p->e1=tr; - else p->e2=tr; + Tri* tr = new Tri(t->id * 2 + 1, t->clip, tv, t->v0, t->v1, 0, tl, t->e1); + tl->e0 = tr; + if (Tri* p = tr->e2) { + if (p->e0 == t) + p->e0 = tr; + else if (p->e1 == t) + p->e1 = tr; + else + p->e2 = tr; } - if( Tri *b=t->e2 ){ - Tri *br=new Tri( b->id*2,b->clip,tv,b->v2,b->v0,0,tr,b->e0 ); - tr->e0=br; - if( Tri *p=br->e2 ){ - if( p->e0==b ) p->e0=br; - else if( p->e1==b ) p->e1=br; - else p->e2=br; + if (Tri* b = t->e2) { + Tri* br = new Tri(b->id * 2, b->clip, tv, b->v2, b->v0, 0, tr, b->e0); + tr->e0 = br; + if (Tri* p = br->e2) { + if (p->e0 == b) + p->e0 = br; + else if (p->e1 == b) + p->e1 = br; + else + p->e2 = br; } - Tri *bl=new Tri( b->id*2+1,b->clip,tv,b->v0,b->v1,tl,br,b->e1 ); - tl->e1=br->e0=bl; - if( Tri *p=bl->e2 ){ - if( p->e0==b ) p->e0=bl; - else if( p->e1==b ) p->e1=bl; - else p->e2=bl; + Tri* bl = new Tri(b->id * 2 + 1, b->clip, tv, b->v0, b->v1, tl, br, b->e1); + tl->e1 = br->e0 = bl; + if (Tri* p = bl->e2) { + if (p->e0 == b) + p->e0 = bl; + else if (p->e1 == b) + p->e1 = bl; + else + p->e2 = bl; } - b->id=0; + b->id = 0; --out_cnt; - insert( br ); - insert( bl ); + insert(br); + insert(bl); } - t->id=0; + t->id = 0; --out_cnt; - insert( tl ); - insert( tr ); + insert(tl); + insert(tr); } -TerrainRep::Error TerrainRep::calcErr( int id,const Vert &v0,const Vert &v1,const Vert &v2 )const{ - +TerrainRep::Error TerrainRep::calcErr(int id, const Vert& v0, const Vert& v1, const Vert& v2) const +{ Error et; - float y=v0.v.y; - if( v1.v.y>y ) y=v1.v.y; - if( v2.v.y>y ) y=v2.v.y; + float y = v0.v.y; + if (v1.v.y > y) + y = v1.v.y; + if (v2.v.y > y) + y = v2.v.y; et.error = 0; - et.bound = y>=1 ? 255 : ceil(y*255.0f); + et.bound = y >= 1 ? 255 : ceil(y * 255.0f); - if( id>=end_tri_id ) return et; + if (id >= end_tri_id) + return et; - Vert tv( (v1.x+v2.x)/2,(v1.z+v2.z)/2 ); - float e=fabs(tv.v.y-(v1.v.y+v2.v.y)/2); - et.error= e>=1 ? 255 : ceil( (e- FLT_EPSILON)*255.0f ); + Vert tv((v1.x + v2.x) / 2, (v1.z + v2.z) / 2); + float e = fabs(tv.v.y - (v1.v.y + v2.v.y) / 2); + et.error = e >= 1 ? 255 : ceil((e - FLT_EPSILON) * 255.0f); - Error el=calcErr( id*2,tv,v2,v0 ); - Error er=calcErr( id*2+1,tv,v0,v1 ); + Error el = calcErr(id * 2, tv, v2, v0); + Error er = calcErr(id * 2 + 1, tv, v0, v1); - if( el.error>et.error ) et.error=el.error; - if( er.error>et.error ) et.error=er.error; + if (el.error > et.error) + et.error = el.error; + if (er.error > et.error) + et.error = er.error; - if( el.bound>et.bound ) et.bound=el.bound; - if( er.bound>et.bound ) et.bound=er.bound; + if (el.bound > et.bound) + et.bound = el.bound; + if (er.bound > et.bound) + et.bound = er.bound; - return errors[id]=et; + return errors[id] = et; } -TerrainRep::Error TerrainRep::calcErr( int id,int x,int z,const Vert &v0,const Vert &v1,const Vert &v2 )const{ - +TerrainRep::Error TerrainRep::calcErr(int id, int x, int z, const Vert& v0, const Vert& v1, const Vert& v2) const +{ Error et; - float y=v0.v.y; - if( v1.v.y>y ) y=v1.v.y; - if( v2.v.y>y ) y=v2.v.y; + float y = v0.v.y; + if (v1.v.y > y) + y = v1.v.y; + if (v2.v.y > y) + y = v2.v.y; et.error = 0; - et.bound = y>=1 ? 255 : ceil(y*255.0f); + et.bound = y >= 1 ? 255 : ceil(y * 255.0f); - if( id>=end_tri_id ) return et; + if (id >= end_tri_id) + return et; //is x/z inside this triangle? - int dx,dz; - dx=-(v1.z-v0.z);dz=(v1.x-v0.x); - if( (x-v0.x)*dx+(z-v0.z)*dz<0 ) return errors[id]; - dx=-(v2.z-v1.z);dz=(v2.x-v1.x); - if( (x-v1.x)*dx+(z-v1.z)*dz<0 ) return errors[id]; - dx=-(v0.z-v2.z);dz=(v0.x-v2.x); - if( (x-v2.x)*dx+(z-v2.z)*dz<0 ) return errors[id]; + int dx, dz; + dx = -(v1.z - v0.z); + dz = (v1.x - v0.x); + if ((x - v0.x) * dx + (z - v0.z) * dz < 0) + return errors[id]; + dx = -(v2.z - v1.z); + dz = (v2.x - v1.x); + if ((x - v1.x) * dx + (z - v1.z) * dz < 0) + return errors[id]; + dx = -(v0.z - v2.z); + dz = (v0.x - v2.x); + if ((x - v2.x) * dx + (z - v2.z) * dz < 0) + return errors[id]; - Vert tv( (v1.x+v2.x)/2,(v1.z+v2.z)/2 ); - float e=fabs(tv.v.y-(v1.v.y+v2.v.y)/2); - et.error= e>=1 ? 255 : ceil( (e- FLT_EPSILON)*255.0f ); + Vert tv((v1.x + v2.x) / 2, (v1.z + v2.z) / 2); + float e = fabs(tv.v.y - (v1.v.y + v2.v.y) / 2); + et.error = e >= 1 ? 255 : ceil((e - FLT_EPSILON) * 255.0f); - Error el=calcErr( id*2,x,z,tv,v2,v0 ); - Error er=calcErr( id*2+1,x,z,tv,v0,v1 ); + Error el = calcErr(id * 2, x, z, tv, v2, v0); + Error er = calcErr(id * 2 + 1, x, z, tv, v0, v1); - if( el.error>et.error ) et.error=el.error; - if( er.error>et.error ) et.error=er.error; + if (el.error > et.error) + et.error = el.error; + if (er.error > et.error) + et.error = er.error; - if( el.bound>et.bound ) et.bound=el.bound; - if( er.bound>et.bound ) et.bound=er.bound; + if (el.bound > et.bound) + et.bound = el.bound; + if (er.bound > et.bound) + et.bound = er.bound; - return errors[id]=et; + return errors[id] = et; } -void TerrainRep::validateErrs()const{ - if( errs_valid ) return; - Vert v0(0,0),v1(cell_size,0),v2(cell_size,cell_size),v3(0,cell_size); - calcErr( 2,v1,v2,v0 ); - calcErr( 3,v3,v0,v2 ); - errs_valid=true; +void TerrainRep::validateErrs() const +{ + if (errs_valid) + return; + Vert v0(0, 0), v1(cell_size, 0), v2(cell_size, cell_size), v3(0, cell_size); + calcErr(2, v1, v2, v0); + calcErr(3, v3, v0, v2); + errs_valid = true; } -void TerrainRep::render( Model *model,const RenderContext &rc ){ - - curr=this; +void TerrainRep::render(Model* model, const RenderContext& rc) +{ + curr = this; validateErrs(); - new( &frustum ) Frustum( rc.getWorldFrustum(),-model->getRenderTform() ); - eye_plane=frustum.getPlane( Frustum::PLANE_NEAR ); - eye_vec=frustum.getVertex( Frustum::VERT_EYE ); + new (&frustum) Frustum(rc.getWorldFrustum(), -model->getRenderTform()); + eye_plane = frustum.getPlane(Frustum::PLANE_NEAR); + eye_vec = frustum.getVertex(Frustum::VERT_EYE); - vert_cnt=4;next_vert=verts; - out_cnt=proc_cnt=clip_cnt=0; + vert_cnt = 4; + next_vert = verts; + out_cnt = proc_cnt = clip_cnt = 0; tri_que.getVector().clear(); tris.clear(); - new(next_vert++) Vert(0,0); - new(next_vert++) Vert(cell_size,0); - new(next_vert++) Vert(cell_size,cell_size); - new(next_vert++) Vert(0,cell_size); + new (next_vert++) Vert(0, 0); + new (next_vert++) Vert(cell_size, 0); + new (next_vert++) Vert(cell_size, cell_size); + new (next_vert++) Vert(0, cell_size); - Tri *t0=new Tri( 2,0x3f,1,2,0 ); - Tri *t1=new Tri( 3,0x3f,3,0,2 ); - t0->e2=t1;t1->e2=t0; + Tri* t0 = new Tri(2, 0x3f, 1, 2, 0); + Tri* t1 = new Tri(3, 0x3f, 3, 0, 2); + t0->e2 = t1; + t1->e2 = t0; - insert( t0 ); - insert( t1 ); + insert(t0); + insert(t1); - while( tri_que.size() && out_cntid ) split( t ); + if (t->id) + split(t); delete t; } - int k; - const vector &q_tris=tri_que.getVector(); + int k; + const vector& q_tris = tri_que.getVector(); - if( !mesh ) out_cnt=0; + if (!mesh) + out_cnt = 0; - if( !out_cnt ){ - for( k=0;kid ){ tris.push_back( t );++err_cnt; } - else delete t; + int err_cnt = 0; + for (k = 0; k < q_tris.size(); ++k) { + Tri* t = q_tris[k]; + if (t->id) { + tris.push_back(t); + ++err_cnt; + } else + delete t; } - if( morph ){ - if( int morph_cnt=err_cnt/4 ){ - if( morph_cnt>vert_cnt ) morph_cnt=vert_cnt; - float t=0,morph_step=1.0f/morph_cnt; - for( int vn=vert_cnt-morph_cnt;vn vert_cnt) + morph_cnt = vert_cnt; + float t = 0, morph_step = 1.0f / morph_cnt; + for (int vn = vert_cnt - morph_cnt; vn < vert_cnt; ++vn) { + verts[vn].v.y += (verts[vn].src_y - verts[vn].v.y) * t; + t += morph_step; } } } - int tri_cnt=tris.size(); + int tri_cnt = tris.size(); - if( vert_cnt>mesh_verts || tri_cnt>mesh_tris ){ - int vc=vert_cnt+32;if( vc>mesh_verts ) mesh_verts=vc; - int tc=tri_cnt+32;if( tc>mesh_tris ) mesh_tris=tc; - if( mesh ) gx_graphics->freeMesh( mesh ); - mesh=gx_graphics->createMesh( mesh_verts,mesh_tris,0 ); + if (vert_cnt > mesh_verts || tri_cnt > mesh_tris) { + int vc = vert_cnt + 32; + if (vc > mesh_verts) + mesh_verts = vc; + int tc = tri_cnt + 32; + if (tc > mesh_tris) + mesh_tris = tc; + if (mesh) + gx_graphics->freeMesh(mesh); + mesh = gx_graphics->createMesh(mesh_verts, mesh_tris, 0); } - mesh->lock( true ); - int tc=0,vc=0; - if( !shading ){ - for( k=0;ksetVertex( vc++,&v.x,&up_normal.x,tex_coords ); + mesh->lock(true); + int tc = 0, vc = 0; + if (!shading) { + for (k = 0; k < vert_cnt; ++k) { + const Vert& t = verts[k]; + const Vector& v = t.v; + float tex_coords[2][2] = {{v.x, cell_size - v.z}, {v.x, cell_size - v.z}}; + mesh->setVertex(vc++, &v.x, &up_normal.x, tex_coords); } - }else{ - for( k=0;ksetVertex( vc++,&v.x,&normal.x,tex_coords ); + } else { + for (k = 0; k < vert_cnt; ++k) { + const Vert& t = verts[k]; + const Vector& v = t.v; + float tex_coords[2][2] = {{v.x, cell_size - v.z}, {v.x, cell_size - v.z}}; + Vector normal = getNormal(v.x, v.z); + mesh->setVertex(vc++, &v.x, &normal.x, tex_coords); } } - for( k=0;kid ) mesh->setTriangle( tc++,t->v0,t->v2,t->v1 ); + for (k = 0; k < tri_cnt; ++k) { + Tri* t = tris[k]; + if (t->id) + mesh->setTriangle(tc++, t->v0, t->v2, t->v1); delete t; } mesh->unlock(); - static int mvc,mtc; - if( vc>mvc ) mvc=vc; - if( tc>mtc ) mtc=tc; - stats3d[1]=mvc; - stats3d[2]=mtc; + static int mvc, mtc; + if (vc > mvc) + mvc = vc; + if (tc > mtc) + mtc = tc; + stats3d[1] = mvc; + stats3d[2] = mtc; - model->enqueue( mesh,0,vc,0,tc ); + model->enqueue(mesh, 0, vc, 0, tc); } -bool TerrainRep::collide( const Line &line,Collision *curr_coll,const Transform &tform,int id,const Vert &v0,const Vert &v1,const Vert &v2,const Line &l )const{ - Box b( v0.v ); - b.update( v1.v ); - b.update( v2.v ); +bool TerrainRep::collide(const Line& line, Collision* curr_coll, const Transform& tform, int id, const Vert& v0, + const Vert& v1, const Vert& v2, const Line& l) const +{ + Box b(v0.v); + b.update(v1.v); + b.update(v2.v); - if( id>=end_tri_id || !errors[id].error ){ - return ::clip( l,b ) ? - curr_coll->triangleCollide( line,0,tform*v0.v,tform*v2.v,tform*v1.v ) - : false; + if (id >= end_tri_id || !errors[id].error) { + return ::clip(l, b) ? curr_coll->triangleCollide(line, 0, tform * v0.v, tform * v2.v, tform * v1.v) : false; } - b.a.y=0; - b.b.y=errors[id].bound/255.0f; - if( !::clip( l,b ) ) return false; + b.a.y = 0; + b.b.y = errors[id].bound / 255.0f; + if (!::clip(l, b)) + return false; - Vert tv( (v1.x+v2.x)/2,(v1.z+v2.z)/2 ); + Vert tv((v1.x + v2.x) / 2, (v1.z + v2.z) / 2); - return - collide( line,curr_coll,tform,id*2,tv,v2,v0,l )| - collide( line,curr_coll,tform,id*2+1,tv,v0,v1,l ); + return collide(line, curr_coll, tform, id * 2, tv, v2, v0, l) + | collide(line, curr_coll, tform, id * 2 + 1, tv, v0, v1, l); } -bool TerrainRep::collide( const Line &line,float radius,Collision *curr_coll,const Transform &tform,int id,const Vert &v0,const Vert &v1,const Vert &v2,const Box &box )const{ - Box b( v0.v ); - b.update( v1.v ); - b.update( v2.v ); +bool TerrainRep::collide(const Line& line, float radius, Collision* curr_coll, const Transform& tform, int id, + const Vert& v0, const Vert& v1, const Vert& v2, const Box& box) const +{ + Box b(v0.v); + b.update(v1.v); + b.update(v2.v); - if( id>=end_tri_id || !errors[id].error ){ - if( v0.v==v1.v || v0.v==v2.v || v1.v==v2.v ){ - gx_runtime->debugLog( "OUCH!" ); + if (id >= end_tri_id || !errors[id].error) { + if (v0.v == v1.v || v0.v == v2.v || v1.v == v2.v) { + gx_runtime->debugLog("OUCH!"); } - return b.overlaps(box) ? - curr_coll->triangleCollide( line,radius,tform*v0.v,tform*v2.v,tform*v1.v ) - : false; + return b.overlaps(box) ? curr_coll->triangleCollide(line, radius, tform * v0.v, tform * v2.v, tform * v1.v) + : false; } - b.a.y=0; - b.b.y=errors[id].bound/255.0f; - if( !b.overlaps( box ) ) return false; + b.a.y = 0; + b.b.y = errors[id].bound / 255.0f; + if (!b.overlaps(box)) + return false; - Vert tv( (v1.x+v2.x)/2,(v1.z+v2.z)/2 ); - return - collide( line,radius,curr_coll,tform,id*2,tv,v2,v0,box )| - collide( line,radius,curr_coll,tform,id*2+1,tv,v0,v1,box ); + Vert tv((v1.x + v2.x) / 2, (v1.z + v2.z) / 2); + return collide(line, radius, curr_coll, tform, id * 2, tv, v2, v0, box) + | collide(line, radius, curr_coll, tform, id * 2 + 1, tv, v0, v1, box); } -bool TerrainRep::collide( const Line &line,float radius,Collision *curr_coll,const Transform &tform )const{ - - curr=this; +bool TerrainRep::collide(const Line& line, float radius, Collision* curr_coll, const Transform& tform) const +{ + curr = this; validateErrs(); - Vert v0(0,0),v1(cell_size,0),v2(cell_size,cell_size),v3(0,cell_size); + Vert v0(0, 0), v1(cell_size, 0), v2(cell_size, cell_size), v3(0, cell_size); - if( !radius ){ - Line l=-tform * line; - return - collide( line,curr_coll,tform,2,v1,v2,v0,l )| - collide( line,curr_coll,tform,3,v3,v0,v2,l ); + if (!radius) { + Line l = -tform * line; + return collide(line, curr_coll, tform, 2, v1, v2, v0, l) | collide(line, curr_coll, tform, 3, v3, v0, v2, l); } //create local box - Box b( line ); - b.expand( radius ); - Box box=-tform * b; + Box b(line); + b.expand(radius); + Box box = -tform * b; - return - collide( line,radius,curr_coll,tform,2,v1,v2,v0,box )| - collide( line,radius,curr_coll,tform,3,v3,v0,v2,box ); + return collide(line, radius, curr_coll, tform, 2, v1, v2, v0, box) + | collide(line, radius, curr_coll, tform, 3, v3, v0, v2, box); } - diff --git a/Runtime/blitz3d/terrainrep.hpp b/Runtime/blitz3d/terrainrep.hpp index 2e89777..4ab7a29 100644 --- a/Runtime/blitz3d/terrainrep.hpp +++ b/Runtime/blitz3d/terrainrep.hpp @@ -6,50 +6,52 @@ #include "model.hpp" -struct TerrainRep{ -public: - TerrainRep( int cell_shift ); +struct TerrainRep { + public: + TerrainRep(int cell_shift); ~TerrainRep(); - - void clear(); - void setShading( bool shading ); - void setDetail( int n,bool morph ); - void setHeight( int x,int z,float h,bool realtime ); - void setTile( int x,int z,const Brush &brush ); - void render( Model *model,const RenderContext &rc ); - int getSize()const; - float getHeight( int x,int z )const; - bool collide( const Line &line,float radius,Collision *curr_coll,const Transform &tform )const; + void clear(); + void setShading(bool shading); + void setDetail(int n, bool morph); + void setHeight(int x, int z, float h, bool realtime); + void setTile(int x, int z, const Brush& brush); + void render(Model* model, const RenderContext& rc); + + int getSize() const; + float getHeight(int x, int z) const; + bool collide(const Line& line, float radius, Collision* curr_coll, const Transform& tform) const; struct Tri; struct Vert; -private: + private: struct Cell; struct Error; friend struct Tri; friend struct Vert; - Cell *cells; - Error *errors; - gxMesh *mesh; + Cell* cells; + Error* errors; + gxMesh* mesh; - int cell_size,cell_shift,cell_mask; - int end_tri_id,detail,mesh_verts,mesh_tris; - bool morph,shading; + int cell_size, cell_shift, cell_mask; + int end_tri_id, detail, mesh_verts, mesh_tris; + bool morph, shading; mutable bool errs_valid; - void insert( Tri *t ); - void split( Tri *t ); + void insert(Tri* t); + void split(Tri* t); - void validateErrs()const; - Vector getNormal( int x,int z )const; - Error calcErr( int id,const Vert &v0,const Vert &v1,const Vert &v2 )const; - Error calcErr( int id,int x,int z,const Vert &v0,const Vert &v1,const Vert &v2 )const; - bool collide( const Line &line,Collision *curr_coll,const Transform &tform,int id,const Vert &v0,const Vert &v1,const Vert &v2,const Line &l )const; - bool collide( const Line &line,float radius,Collision *curr_coll,const Transform &tform,int id,const Vert &v0,const Vert &v1,const Vert &v2,const Box &box )const; + void validateErrs() const; + Vector getNormal(int x, int z) const; + Error calcErr(int id, const Vert& v0, const Vert& v1, const Vert& v2) const; + Error calcErr(int id, int x, int z, const Vert& v0, const Vert& v1, const Vert& v2) const; + bool collide(const Line& line, Collision* curr_coll, const Transform& tform, int id, const Vert& v0, const Vert& v1, + const Vert& v2, const Line& l) const; + bool collide(const Line& line, float radius, Collision* curr_coll, const Transform& tform, int id, const Vert& v0, + const Vert& v1, const Vert& v2, const Box& box) const; }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/texture.cpp b/Runtime/blitz3d/texture.cpp index 46b1eac..9cb76b3 100644 --- a/Runtime/blitz3d/texture.cpp +++ b/Runtime/blitz3d/texture.cpp @@ -1,24 +1,24 @@ -#include "std.hpp" -#include "geom.hpp" #include "texture.hpp" #include "cachedtexture.hpp" +#include "geom.hpp" +#include "std.hpp" #include "../gxruntime/gxgraphics.hpp" -extern gxScene *gx_scene; -extern gxGraphics *gx_graphics; +extern gxScene* gx_scene; +extern gxGraphics* gx_graphics; struct Filter { string t; - int flags; - Filter(const string &t, int flags) :t(t), flags(flags) { - } + int flags; + Filter(const string& t, int flags) : t(t), flags(flags) {} }; static vector filters; -static int filterFile(const string &t, int flags) { +static int filterFile(const string& t, int flags) +{ //check filters... string l = tolower(t); for (size_t k = 0; k < filters.size(); ++k) { @@ -30,166 +30,193 @@ static int filterFile(const string &t, int flags) { } struct Texture::Rep { - - int ref_cnt; + int ref_cnt; CachedTextureFactory cached_tex; - vector tex_frames; + vector tex_frames; - int tex_blend, tex_flags; + int tex_blend, tex_flags; bool transparent; - float sx, sy, tx, ty, rot; - bool mat_used, mat_valid; + float sx, sy, tx, ty, rot; + bool mat_used, mat_valid; gxScene::Matrix matrix; - Rep(int w, int h, int flags, int cnt) : - ref_cnt(1), cached_tex(w, h, flags, cnt), - tex_blend(gxScene::BLEND_MULTIPLY), tex_flags(0), - sx(1), sy(1), tx(0), ty(0), rot(0), mat_used(false) { - tex_frames = cached_tex.getFrames(); - transparent = - (flags & gxCanvas::CANVAS_TEX_ALPHA) && - !(flags & gxCanvas::CANVAS_TEX_MASK); + Rep(int w, int h, int flags, int cnt) + : ref_cnt(1), cached_tex(w, h, flags, cnt), tex_blend(gxScene::BLEND_MULTIPLY), tex_flags(0), sx(1), sy(1), + tx(0), ty(0), rot(0), mat_used(false) + { + tex_frames = cached_tex.getFrames(); + transparent = (flags & gxCanvas::CANVAS_TEX_ALPHA) && !(flags & gxCanvas::CANVAS_TEX_MASK); memset(&matrix, 0, sizeof(matrix)); } - Rep(const string &f, int flags, int w, int h, int first, int cnt) : - ref_cnt(1), cached_tex(f, flags, w, h, first, cnt), - tex_blend(gxScene::BLEND_MULTIPLY), tex_flags(0), - sx(1), sy(1), tx(0), ty(0), rot(0), mat_used(false) { - tex_frames = cached_tex.getFrames(); - transparent = - (flags & gxCanvas::CANVAS_TEX_ALPHA) && - !(flags & gxCanvas::CANVAS_TEX_MASK); + Rep(const string& f, int flags, int w, int h, int first, int cnt) + : ref_cnt(1), cached_tex(f, flags, w, h, first, cnt), tex_blend(gxScene::BLEND_MULTIPLY), tex_flags(0), sx(1), + sy(1), tx(0), ty(0), rot(0), mat_used(false) + { + tex_frames = cached_tex.getFrames(); + transparent = (flags & gxCanvas::CANVAS_TEX_ALPHA) && !(flags & gxCanvas::CANVAS_TEX_MASK); memset(&matrix, 0, sizeof(matrix)); } - Rep(const Rep &t) : - ref_cnt(1), cached_tex(t.cached_tex), tex_frames(t.tex_frames), - tex_blend(t.tex_blend), tex_flags(t.tex_flags), - sx(t.sx), sy(t.sy), tx(t.tx), ty(t.ty), rot(t.rot), - mat_used(t.mat_used), mat_valid(t.mat_valid), matrix(t.matrix), - transparent(t.transparent) { - } + Rep(const Rep& t) + : ref_cnt(1), cached_tex(t.cached_tex), tex_frames(t.tex_frames), tex_blend(t.tex_blend), + tex_flags(t.tex_flags), sx(t.sx), sy(t.sy), tx(t.tx), ty(t.ty), rot(t.rot), mat_used(t.mat_used), + mat_valid(t.mat_valid), matrix(t.matrix), transparent(t.transparent) + {} }; -Texture::Texture() :rep(0) { -} +Texture::Texture() : rep(0) {} -Texture::Texture(const string &f, int flags) { +Texture::Texture(const string& f, int flags) +{ flags = filterFile(f, flags) | gxCanvas::CANVAS_TEXTURE; - if (flags & gxCanvas::CANVAS_TEX_MASK) flags |= gxCanvas::CANVAS_TEX_RGB | gxCanvas::CANVAS_TEX_ALPHA; + if (flags & gxCanvas::CANVAS_TEX_MASK) + flags |= gxCanvas::CANVAS_TEX_RGB | gxCanvas::CANVAS_TEX_ALPHA; rep = new Rep(f, flags, 0, 0, 0, 1); } -Texture::Texture(const string &f, int flags, int w, int h, int first, int cnt) { +Texture::Texture(const string& f, int flags, int w, int h, int first, int cnt) +{ flags = filterFile(f, flags) | gxCanvas::CANVAS_TEXTURE; - if (flags & gxCanvas::CANVAS_TEX_MASK) flags |= gxCanvas::CANVAS_TEX_RGB | gxCanvas::CANVAS_TEX_ALPHA; + if (flags & gxCanvas::CANVAS_TEX_MASK) + flags |= gxCanvas::CANVAS_TEX_RGB | gxCanvas::CANVAS_TEX_ALPHA; rep = new Rep(f, flags, w, h, first, cnt); } -Texture::Texture(int w, int h, int flags, int cnt) { +Texture::Texture(int w, int h, int flags, int cnt) +{ flags |= gxCanvas::CANVAS_TEXTURE; - if (flags & gxCanvas::CANVAS_TEX_MASK) flags |= gxCanvas::CANVAS_TEX_RGB | gxCanvas::CANVAS_TEX_ALPHA; + if (flags & gxCanvas::CANVAS_TEX_MASK) + flags |= gxCanvas::CANVAS_TEX_RGB | gxCanvas::CANVAS_TEX_ALPHA; rep = new Rep(w, h, flags, cnt); } -Texture::Texture(const Texture &t) : - rep(t.rep) { - if (rep) ++rep->ref_cnt; +Texture::Texture(const Texture& t) : rep(t.rep) +{ + if (rep) + ++rep->ref_cnt; } -Texture::~Texture() { - if (rep && !--rep->ref_cnt) delete rep; +Texture::~Texture() +{ + if (rep && !--rep->ref_cnt) + delete rep; } -Texture &Texture::operator=(const Texture &t) { - if (t.rep) ++t.rep->ref_cnt; - if (rep && !--rep->ref_cnt) delete rep; +Texture& Texture::operator=(const Texture& t) +{ + if (t.rep) + ++t.rep->ref_cnt; + if (rep && !--rep->ref_cnt) + delete rep; rep = t.rep; return *this; } -void Texture::setScale(float u_scale, float v_scale) { - if (!rep) return; - rep->sx = u_scale; rep->sy = v_scale; +void Texture::setScale(float u_scale, float v_scale) +{ + if (!rep) + return; + rep->sx = u_scale; + rep->sy = v_scale; rep->mat_valid = false; - rep->mat_used = true; + rep->mat_used = true; } -void Texture::setRotation(float angle) { - if (!rep) return; - rep->rot = angle; +void Texture::setRotation(float angle) +{ + if (!rep) + return; + rep->rot = angle; rep->mat_valid = false; - rep->mat_used = true; + rep->mat_used = true; } -void Texture::setPosition(float u_pos, float v_pos) { - if (!rep) return; - rep->tx = u_pos; - rep->ty = v_pos; +void Texture::setPosition(float u_pos, float v_pos) +{ + if (!rep) + return; + rep->tx = u_pos; + rep->ty = v_pos; rep->mat_valid = false; - rep->mat_used = true; + rep->mat_used = true; } -void Texture::setBlend(int blend) { - if (!rep) return; +void Texture::setBlend(int blend) +{ + if (!rep) + return; rep->tex_blend = blend; } -void Texture::setFlags(int flags) { - if (!rep) return; +void Texture::setFlags(int flags) +{ + if (!rep) + return; rep->tex_flags = flags; } -bool Texture::isTransparent()const { +bool Texture::isTransparent() const +{ return rep ? rep->transparent : false; } -gxCanvas *Texture::getCanvas(int n)const { +gxCanvas* Texture::getCanvas(int n) const +{ return rep && n >= 0 && n < rep->tex_frames.size() ? rep->tex_frames[n] : 0; } -int Texture::getCanvasFlags()const { +int Texture::getCanvasFlags() const +{ return rep && rep->tex_frames.size() ? rep->tex_frames[0]->getFlags() : 0; } -CachedTextureFactory *Texture::getCachedTexture()const { +CachedTextureFactory* Texture::getCachedTexture() const +{ return rep ? &rep->cached_tex : 0; } -int Texture::getBlend()const { +int Texture::getBlend() const +{ return rep ? rep->tex_blend : 0; } -int Texture::getFlags()const { +int Texture::getFlags() const +{ return rep ? rep->tex_flags : 0; } -const gxScene::Matrix *Texture::getMatrix()const { - if (!rep || !rep->mat_used) return 0; +const gxScene::Matrix* Texture::getMatrix() const +{ + if (!rep || !rep->mat_used) + return 0; if (!rep->mat_valid) { float c = cos(rep->rot), s = sin(rep->rot); - rep->matrix.elements[0][0] = c*rep->sx; - rep->matrix.elements[1][0] = s*rep->sx; - rep->matrix.elements[0][1] = -s*rep->sy; - rep->matrix.elements[1][1] = c*rep->sy; + rep->matrix.elements[0][0] = c * rep->sx; + rep->matrix.elements[1][0] = s * rep->sx; + rep->matrix.elements[0][1] = -s * rep->sy; + rep->matrix.elements[1][1] = c * rep->sy; rep->matrix.elements[2][0] = rep->tx; rep->matrix.elements[2][1] = rep->ty; - rep->mat_valid = true; + rep->mat_valid = true; } return &rep->matrix; } -bool Texture::operator<(const Texture &t)const { - if (rep && t.rep) return rep->cached_tex < t.rep->cached_tex; +bool Texture::operator<(const Texture& t) const +{ + if (rep && t.rep) + return rep->cached_tex < t.rep->cached_tex; return rep < t.rep; } -void Texture::clearFilters() { +void Texture::clearFilters() +{ filters.clear(); } -void Texture::addFilter(const string &t, int flags) { +void Texture::addFilter(const string& t, int flags) +{ filters.push_back(Filter(tolower(t), flags)); } diff --git a/Runtime/blitz3d/texture.hpp b/Runtime/blitz3d/texture.hpp index 2c67941..a6f09f6 100644 --- a/Runtime/blitz3d/texture.hpp +++ b/Runtime/blitz3d/texture.hpp @@ -6,41 +6,41 @@ #include "cachedtexture.hpp" -#include "../gxruntime/gxcanvas.hpp" +#include "gxcanvas.hpp" -class Texture{ -public: +class Texture { + public: Texture(); - Texture( const std::string &file,int flags ); - Texture( const std::string &file,int flags,int w,int h,int first,int cnt ); - Texture( int width,int height,int flags,int cnt ); - Texture( const Texture &texture ); + Texture(const std::string& file, int flags); + Texture(const std::string& file, int flags, int w, int h, int first, int cnt); + Texture(int width, int height, int flags, int cnt); + Texture(const Texture& texture); ~Texture(); - Texture &operator=( const Texture &texture ); + Texture& operator=(const Texture& texture); - void setScale( float u_scale,float v_scale ); - void setRotation( float rot ); - void setPosition( float u_pos,float v_pos ); - void setBlend( int blend ); - void setFlags( int flags ); + void setScale(float u_scale, float v_scale); + void setRotation(float rot); + void setPosition(float u_pos, float v_pos); + void setBlend(int blend); + void setFlags(int flags); - int getCanvasFlags()const; - gxCanvas *getCanvas( int frame )const; - const gxScene::Matrix *getMatrix()const; - int getBlend()const; - int getFlags()const; - CachedTextureFactory *getCachedTexture()const; + int getCanvasFlags() const; + gxCanvas* getCanvas(int frame) const; + const gxScene::Matrix* getMatrix() const; + int getBlend() const; + int getFlags() const; + CachedTextureFactory* getCachedTexture() const; - bool isTransparent()const; - bool operator<( const Texture &t )const; + bool isTransparent() const; + bool operator<(const Texture& t) const; static void clearFilters(); - static void addFilter( const std::string &filter,int flags ); + static void addFilter(const std::string& filter, int flags); -private: + private: struct Rep; - Rep *rep; + Rep* rep; }; #endif \ No newline at end of file diff --git a/Runtime/blitz3d/world.cpp b/Runtime/blitz3d/world.cpp index 194a297..474f6a8 100644 --- a/Runtime/blitz3d/world.cpp +++ b/Runtime/blitz3d/world.cpp @@ -1,27 +1,29 @@ -#include "std.hpp" -#include #include "world.hpp" +#include +#include "std.hpp" //0=tris compared for collision //1=max proj err of terrain float stats3d[10]; -extern gxScene *gx_scene; -extern gxRuntime *gx_runtime; +extern gxScene* gx_scene; +extern gxRuntime* gx_runtime; static std::list s_objectsEnabled, s_objectsVisible; -static void StaticEnumerateEnabled() { +static void StaticEnumerateEnabled() +{ s_objectsEnabled.clear(); - for (Entity *e = Entity::GetEntityOrphans(); e; e = e->GetSuccessor()) { + for (Entity* e = Entity::GetEntityOrphans(); e; e = e->GetSuccessor()) { e->EnumerateEnabled(s_objectsEnabled); } } -static void StaticEnumerateVisible() { +static void StaticEnumerateVisible() +{ s_objectsVisible.clear(); - for (Entity *e = Entity::GetEntityOrphans(); e; e = e->GetSuccessor()) { + for (Entity* e = Entity::GetEntityOrphans(); e; e = e->GetSuccessor()) { e->EnumerateVisible(s_objectsVisible); } } @@ -32,8 +34,9 @@ static vector _objsByType[1000]; static vector free_colls, used_colls; -static ObjCollision *allocObjColl(Object *with, const Vector &coords, const Collision &coll) { - ObjCollision *c; +static ObjCollision* allocObjColl(Object* with, const Vector& coords, const Collision& coll) +{ + ObjCollision* c; if (free_colls.size()) { c = free_colls.back(); free_colls.pop_back(); @@ -41,16 +44,16 @@ static ObjCollision *allocObjColl(Object *with, const Vector &coords, const Coll c = new ObjCollision(); } used_colls.push_back(c); - c->with = with; - c->coords = coords; + c->with = with; + c->coords = coords; c->collision = coll; return c; } -static void collided(Object *src, Object *dest, const Line &line, const Collision &coll, float y_scale) { - - ObjCollision *c; - const Vector &coords = line*coll.time - coll.normal*src->getCollisionRadii().x; +static void collided(Object* src, Object* dest, const Line& line, const Collision& coll, float y_scale) +{ + ObjCollision* c; + const Vector& coords = line * coll.time - coll.normal * src->getCollisionRadii().x; c = allocObjColl(dest, coords, coll); c->coords.y *= y_scale; @@ -61,52 +64,58 @@ static void collided(Object *src, Object *dest, const Line &line, const Collisio dest->addCollision(c); } -void World::clearCollisions() { +void World::clearCollisions() +{ for (int k = 0; k < 1000; ++k) { _collInfo[k].clear(); } } -void World::addCollision(int src_type, int dst_type, int method, int response) { - - vector &info = _collInfo[src_type]; +void World::addCollision(int src_type, int dst_type, int method, int response) +{ + vector& info = _collInfo[src_type]; for (size_t k = 0; k < info.size(); ++k) { - const CollInfo &t = info[k]; - if (dst_type == t.dst_type) return; + const CollInfo& t = info[k]; + if (dst_type == t.dst_type) + return; } - CollInfo co = { dst_type,method,response }; + CollInfo co = {dst_type, method, response}; _collInfo[src_type].push_back(co); } -bool World::hitTest(const Line &line, float radius, Object *obj, const Transform &tf, int method, Collision *curr_coll) { +bool World::hitTest(const Line& line, float radius, Object* obj, const Transform& tf, int method, Collision* curr_coll) +{ switch (method) { - case COLLISION_METHOD_SPHERE: - return curr_coll->sphereCollide(line, radius, tf.v, obj->getCollisionRadii().x); - case COLLISION_METHOD_POLYGON: - return obj->collide(line, radius, curr_coll, tf); - case COLLISION_METHOD_BOX: - Transform t = tf; - t.m.i.normalize(); t.m.j.normalize(); t.m.k.normalize(); - if (curr_coll->boxCollide(~t*line, radius, obj->getCollisionBox())) { - curr_coll->normal = t.m*curr_coll->normal; - return true; - } + case COLLISION_METHOD_SPHERE: + return curr_coll->sphereCollide(line, radius, tf.v, obj->getCollisionRadii().x); + case COLLISION_METHOD_POLYGON: + return obj->collide(line, radius, curr_coll, tf); + case COLLISION_METHOD_BOX: + Transform t = tf; + t.m.i.normalize(); + t.m.j.normalize(); + t.m.k.normalize(); + if (curr_coll->boxCollide(~t * line, radius, obj->getCollisionBox())) { + curr_coll->normal = t.m * curr_coll->normal; + return true; + } } return false; } -bool World::CheckLineOfSight(Object *src, Object *dest) { - +bool World::CheckLineOfSight(Object* src, Object* dest) +{ StaticEnumerateEnabled(); - Object *coll_obj = 0; + Object* coll_obj = 0; Collision curr_coll; Line line(src->GetWorldPosition(), dest->GetWorldPosition() - src->GetWorldPosition()); for (Object* obj : s_objectsEnabled) { - if (obj == src || obj == dest || !obj->getPickGeometry() || !obj->getObscurer()) continue; + if (obj == src || obj == dest || !obj->getPickGeometry() || !obj->getObscurer()) + continue; if (hitTest(line, 0, obj, obj->GetWorldTransform(), obj->getPickGeometry(), &curr_coll)) { return false; @@ -115,22 +124,22 @@ bool World::CheckLineOfSight(Object *src, Object *dest) { return true; } -Object *World::traceRay(const Line &line, float radius, ObjCollision *curr_coll) { - +Object* World::traceRay(const Line& line, float radius, ObjCollision* curr_coll) +{ StaticEnumerateEnabled(); - Object *coll_obj = 0; + Object* coll_obj = 0; for (Object* obj : s_objectsEnabled) { - - if (!obj->getPickGeometry()) continue; + if (!obj->getPickGeometry()) + continue; if (hitTest(line, radius, obj, obj->GetWorldTransform(), obj->getPickGeometry(), &curr_coll->collision)) { coll_obj = obj; } } if (curr_coll->with = coll_obj) { - curr_coll->coords = line*curr_coll->collision.time - curr_coll->collision.normal*radius; + curr_coll->coords = line * curr_coll->collision.time - curr_coll->collision.normal * radius; } return coll_obj; } @@ -138,7 +147,8 @@ Object *World::traceRay(const Line &line, float radius, ObjCollision *curr_coll) // // NEW VERSION // -void World::collide(Object *src) { +void World::collide(Object* src) +{ Vector dv = src->GetWorldTransform().v; Vector sv = src->getPrevWorldTform().v; @@ -153,77 +163,74 @@ void World::collide(Object *src) { static Transform y_tform; - const Vector &radii = src->getCollisionRadii(); + const Vector& radii = src->getCollisionRadii(); - float radius = radii.x, inv_y_scale; + float radius = radii.x, inv_y_scale; float y_scale = inv_y_scale = y_tform.m.j.y = 1; if (radii.x != radii.y) { y_scale = y_tform.m.j.y = radius / radii.y; - inv_y_scale = 1 / y_scale; + inv_y_scale = 1 / y_scale; sv.y *= y_scale; dv.y *= y_scale; } - int n_hit = 0; - Plane planes[2]; - Line coll_line(sv, dv - sv); + int n_hit = 0; + Plane planes[2]; + Line coll_line(sv, dv - sv); Vector dir = coll_line.d; - float td = coll_line.d.length(); + float td = coll_line.d.length(); float td_xz = Vector(coll_line.d.x, 0, coll_line.d.z).length(); - const vector &collinfos = _collInfo[src->getCollisionType()]; + const vector& collinfos = _collInfo[src->getCollisionType()]; int hits = 0; for (;;) { - Collision coll; - Object *coll_obj = 0; + Collision coll; + Object* coll_obj = 0; vector::const_iterator coll_it, coll_info; for (coll_it = collinfos.begin(); coll_it != collinfos.end(); ++coll_it) { -// const std::list &dst_objs = _objsByType[coll_it->dst_type]; - for (Object* dst : /*dst_objs*/_objsByType[coll_it->dst_type]) { + // const std::list &dst_objs = _objsByType[coll_it->dst_type]; + for (Object* dst : /*dst_objs*/ _objsByType[coll_it->dst_type]) { + if (src == dst) + continue; - if (src == dst) continue; - - const Transform &dst_tform = dst->getPrevWorldTform(); + const Transform& dst_tform = dst->getPrevWorldTform(); if (y_scale == 1) { - if (hitTest( - coll_line, radius, dst, dst_tform, - coll_it->method, &coll)) { - coll_obj = dst; + if (hitTest(coll_line, radius, dst, dst_tform, coll_it->method, &coll)) { + coll_obj = dst; coll_info = coll_it; } } else { - if (hitTest( - coll_line, radius, dst, y_tform * dst_tform, - coll_it->method, &coll)) { - coll_obj = dst; + if (hitTest(coll_line, radius, dst, y_tform * dst_tform, coll_it->method, &coll)) { + coll_obj = dst; coll_info = coll_it; } } } } - if (!coll_obj) break; + if (!coll_obj) + break; //register collision if (++hits == WORLD_COLLISION_HITS) { -// exit(0); + // exit(0); break; } collided(src, coll_obj, coll_line, coll, inv_y_scale); - Plane coll_plane(coll_line*coll.time, coll.normal); + Plane coll_plane(coll_line * coll.time, coll.normal); coll_plane.d -= COLLISION_FLT_EPSILON; coll.time = coll_plane.t_intersect(coll_line); - if (coll.time > 0) {// && fabs(coll.normal.dot( coll_line.d ))>FLT_EPSILON ){ + if (coll.time > 0) { // && fabs(coll.normal.dot( coll_line.d ))>FLT_EPSILON ){ //update source position - ONLY IF AHEAD! - sv = coll_line*coll.time; + sv = coll_line * coll.time; td *= 1 - coll.time; td_xz *= 1 - coll.time; } @@ -240,37 +247,53 @@ void World::collide(Object *src) { dv = nv; } else if (n_hit == 1) { if (planes[0].distance(nv) >= 0) { - dv = nv; n_hit = 0; + dv = nv; + n_hit = 0; } else if (fabs(planes[0].n.dot(coll_plane.n)) < 1 - FLT_EPSILON) { dv = coll_plane.intersect(planes[0]).nearest(dv); } else { //SQUISHED! //exit(0); - hits = WORLD_COLLISION_HITS; break; + hits = WORLD_COLLISION_HITS; + break; } } else if (planes[0].distance(nv) >= 0 && planes[1].distance(nv) >= 0) { - dv = nv; n_hit = 0; + dv = nv; + n_hit = 0; } else { - dv = sv; break; + dv = sv; + break; } Vector dd(dv - sv); //going behind initial direction? really necessary? - if (dd.dot(dir) <= 0) { dv = sv; break; } + if (dd.dot(dir) <= 0) { + dv = sv; + break; + } if (coll_info->response == COLLISION_RESPONSE_SLIDE) { float d = dd.length(); - if (d <= FLT_EPSILON) { dv = sv; break; } - if (d > td) dd *= td / d; + if (d <= FLT_EPSILON) { + dv = sv; + break; + } + if (d > td) + dd *= td / d; } else if (coll_info->response == COLLISION_RESPONSE_SLIDEXZ) { float d = Vector(dd.x, 0, dd.z).length(); - if (d <= FLT_EPSILON) { dv = sv; break; } - if (d > td_xz) dd *= td_xz / d; + if (d <= FLT_EPSILON) { + dv = sv; + break; + } + if (d > td_xz) + dd *= td_xz / d; } - coll_line.o = sv; - coll_line.d = dd; dv = sv + dd; + coll_line.o = sv; + coll_line.d = dd; + dv = sv + dd; planes[n_hit++] = coll_plane; } @@ -447,8 +470,8 @@ void World::collide( Object *src ){ } */ -void World::update(float elapsed) { - +void World::update(float elapsed) +{ stats3d[0] = 0; for (; used_colls.size(); used_colls.pop_back()) { @@ -463,7 +486,8 @@ void World::update(float elapsed) { } o->beginUpdate(elapsed); - if (o->getCollisionType()) collide(o); + if (o->getCollisionType()) + collide(o); o->endUpdate(); } @@ -475,23 +499,23 @@ void World::update(float elapsed) { /****************************** Render *********************************/ -static Transform cam_tform; //current camera transform +static Transform cam_tform; //current camera transform -static vector _lights; -static vector _mirrors; +static vector _lights; +static vector _mirrors; static vector _listeners; struct OrderComp { - bool operator()(Object *a, Object *b) { + bool operator()(Object* a, Object* b) + { return a->getOrder() < b->getOrder(); } }; struct TransComp { - bool operator()(Model *a, Model *b)const { - return - cam_tform.v.distance(a->getRenderTform().v) < - cam_tform.v.distance(b->getRenderTform().v); + bool operator()(Model* a, Model* b) const + { + return cam_tform.v.distance(a->getRenderTform().v) < cam_tform.v.distance(b->getRenderTform().v); } }; @@ -503,8 +527,8 @@ static priority_queue, OrderComp> cam_que; static priority_queue, TransComp> transparents; -void World::capture() { - +void World::capture() +{ StaticEnumerateVisible(); for (Object* o : s_objectsVisible) { @@ -512,8 +536,8 @@ void World::capture() { } } -void World::render(float tween) { - +void World::render(float tween) +{ //set render tweens, and build ordered and unordered model lists... ord_mods.clear(); unord_mods.clear(); @@ -526,29 +550,38 @@ void World::render(float tween) { StaticEnumerateVisible(); for (Object* o : s_objectsVisible) { - if (!o->beginRender(tween)) continue; + if (!o->beginRender(tween)) + continue; - if (Light *t = o->getLight()) _lights.push_back(t->getGxLight()); - else if (Camera *t = o->getCamera()) cam_que.push(t); - else if (Mirror *t = o->getMirror()) _mirrors.push_back(t); - else if (Listener *t = o->getListener()) _listeners.push_back(t); - else if (Model *t = o->getModel()) { - if (t->getOrder()) ord_que.push(t); - else unord_mods.push_back(t); + if (Light* t = o->getLight()) + _lights.push_back(t->getGxLight()); + else if (Camera* t = o->getCamera()) + cam_que.push(t); + else if (Mirror* t = o->getMirror()) + _mirrors.push_back(t); + else if (Listener* t = o->getListener()) + _listeners.push_back(t); + else if (Model* t = o->getModel()) { + if (t->getOrder()) + ord_que.push(t); + else + unord_mods.push_back(t); } } - for (; ord_que.size(); ord_que.pop()) ord_mods.push_back(ord_que.top()); + for (; ord_que.size(); ord_que.pop()) + ord_mods.push_back(ord_que.top()); -// gx_runtime->debugLog( "RenderWorld" ); + // gx_runtime->debugLog( "RenderWorld" ); - if (!gx_scene->begin(_lights)) return; + if (!gx_scene->begin(_lights)) + return; for (; cam_que.size(); cam_que.pop()) { + Camera* cam = cam_que.top(); - Camera *cam = cam_que.top(); - - if (!cam->beginRenderFrame()) continue; + if (!cam->beginRenderFrame()) + continue; vector::const_iterator mir_it; for (mir_it = _mirrors.begin(); mir_it != _mirrors.end(); ++mir_it) { @@ -560,7 +593,7 @@ void World::render(float tween) { gx_scene->end(); -// gx_runtime->debugLog( "End RenderWorld" ); + // gx_runtime->debugLog( "End RenderWorld" ); vector::const_iterator lis_it; for (lis_it = _listeners.begin(); lis_it != _listeners.end(); ++lis_it) { @@ -568,11 +601,11 @@ void World::render(float tween) { } } -void World::render(Camera *cam, Mirror *mirror) { - +void World::render(Camera* cam, Mirror* mirror) +{ if (mirror) { - const Transform &t = mirror->getRenderTform(); - cam_tform = t * Transform(scaleMatrix(1, -1, 1)) * -t * cam->getRenderTform(); + const Transform& t = mirror->getRenderTform(); + cam_tform = t * Transform(scaleMatrix(1, -1, 1)) * -t * cam->getRenderTform(); gx_scene->setFlippedTris(true); } else { cam_tform = cam->getRenderTform(); @@ -588,17 +621,19 @@ void World::render(Camera *cam, Mirror *mirror) { //draw everything in order size_t ord = 0; gx_scene->setZMode(gxScene::ZMODE_DISABLE); - while (ord < ord_mods.size() && ord_mods[ord]->getOrder()>0) { - Model *mod = ord_mods[ord++]; - if (!mod->doAutoFade(cam_tform.v)) continue; + while (ord < ord_mods.size() && ord_mods[ord]->getOrder() > 0) { + Model* mod = ord_mods[ord++]; + if (!mod->doAutoFade(cam_tform.v)) + continue; render(mod, rc); flushTransparent(); } gx_scene->setZMode(gxScene::ZMODE_NORMAL); for (size_t k = 0; k < unord_mods.size(); ++k) { - Model *mod = unord_mods[k]; - if (!mod->doAutoFade(cam_tform.v)) continue; + Model* mod = unord_mods[k]; + if (!mod->doAutoFade(cam_tform.v)) + continue; render(mod, rc); } gx_scene->setZMode(gxScene::ZMODE_CMPONLY); @@ -606,15 +641,16 @@ void World::render(Camera *cam, Mirror *mirror) { gx_scene->setZMode(gxScene::ZMODE_DISABLE); while (ord < ord_mods.size()) { - Model *mod = ord_mods[ord++]; - if (!mod->doAutoFade(cam_tform.v)) continue; + Model* mod = ord_mods[ord++]; + if (!mod->doAutoFade(cam_tform.v)) + continue; render(mod, rc); flushTransparent(); } } -void World::render(Model *mod, const RenderContext &rc) { - +void World::render(Model* mod, const RenderContext& rc) +{ bool trans = mod->render(rc); if (mod->queueSize(Model::QUEUE_OPAQUE)) { @@ -631,12 +667,12 @@ void World::render(Model *mod, const RenderContext &rc) { } } -void World::flushTransparent() { - +void World::flushTransparent() +{ bool local = true; for (; transparents.size(); transparents.pop()) { - Model *mod = transparents.top(); + Model* mod = transparents.top(); if (mod->getRenderSpace() == Model::RENDER_SPACE_LOCAL) { gx_scene->setWorldMatrix((gxScene::Matrix*)&mod->getRenderTform()); local = true; diff --git a/Runtime/blitz3d/world.hpp b/Runtime/blitz3d/world.hpp index 996a9a9..ffed910 100644 --- a/Runtime/blitz3d/world.hpp +++ b/Runtime/blitz3d/world.hpp @@ -4,57 +4,52 @@ #include -#include "model.hpp" #include "camera.hpp" #include "light.hpp" -#include "mirror.hpp" #include "listener.hpp" +#include "mirror.hpp" +#include "model.hpp" -#define WORLD_COLLISION_TYPES 16 -#define WORLD_COLLISION_HITS 4 +#define WORLD_COLLISION_TYPES 16 +#define WORLD_COLLISION_HITS 4 -class World{ -public: +class World { + public: //collision methods - enum{ - COLLISION_METHOD_SPHERE=1, - COLLISION_METHOD_POLYGON=2, - COLLISION_METHOD_BOX=3 - }; + enum { COLLISION_METHOD_SPHERE = 1, COLLISION_METHOD_POLYGON = 2, COLLISION_METHOD_BOX = 3 }; //collision actions - enum{ - COLLISION_RESPONSE_NONE=0, - COLLISION_RESPONSE_STOP=1, - COLLISION_RESPONSE_SLIDE=2, - COLLISION_RESPONSE_SLIDEXZ=3, + enum { + COLLISION_RESPONSE_NONE = 0, + COLLISION_RESPONSE_STOP = 1, + COLLISION_RESPONSE_SLIDE = 2, + COLLISION_RESPONSE_SLIDEXZ = 3, }; void clearCollisions(); - void addCollision( int src_type,int dest_type,int method,int response ); + void addCollision(int src_type, int dest_type, int method, int response); - void update( float elapsed ); + void update(float elapsed); void capture(); - void render( float tween ); + void render(float tween); - bool CheckLineOfSight( Object *src,Object *dest ); - bool hitTest( const Line &line,float radius,Object *obj,const Transform &tf,int method,Collision *curr_coll ); - Object *traceRay( const Line &line,float radius,ObjCollision *curr_coll ); + bool CheckLineOfSight(Object* src, Object* dest); + bool hitTest(const Line& line, float radius, Object* obj, const Transform& tf, int method, Collision* curr_coll); + Object* traceRay(const Line& line, float radius, ObjCollision* curr_coll); -private: - struct CollInfo{ - int dst_type,method,response; + private: + struct CollInfo { + int dst_type, method, response; }; - vector _collInfo[WORLD_COLLISION_TYPES]; + vector _collInfo[WORLD_COLLISION_TYPES]; std::list _objsByType[WORLD_COLLISION_TYPES]; std::list _objsByTypeSwappable[WORLD_COLLISION_TYPES]; - void collide( Object *src ); - void render( Camera *c,Mirror *m ); - void render( Model *m,const RenderContext &rc ); + void collide(Object* src); + void render(Camera* c, Mirror* m); + void render(Model* m, const RenderContext& rc); void flushTransparent(); - }; #endif diff --git a/Runtime/lib/CMakeLists.txt b/Runtime/lib/CMakeLists.txt index 49d4175..802d9de 100644 --- a/Runtime/lib/CMakeLists.txt +++ b/Runtime/lib/CMakeLists.txt @@ -20,6 +20,10 @@ add_library(${PROJECT_NAME} STATIC "bbmath.hpp" "bbruntime.cpp" "bbruntime.hpp" + "bbsockets.cpp" + "bbsockets.hpp" + "bbstream.cpp" + "bbstream.hpp" "bbstring.cpp" "bbstring.hpp" "bbsys.cpp" @@ -33,8 +37,8 @@ add_library(${PROJECT_NAME} STATIC "runtime.cpp" "std.cpp" "std.hpp" - "userlib.cpp" - "userlib.hpp" +# "userlib.cpp" +# "userlib.hpp" "userlibs.cpp" "userlibs.hpp" ) @@ -42,10 +46,11 @@ add_library(${PROJECT_NAME} STATIC target_link_libraries(${PROJECT_NAME} PUBLIC stdutil - gxruntime + runtime_gfx ddraw dsound dxguid + mswsock ) target_include_directories(${PROJECT_NAME} @@ -61,7 +66,7 @@ if (WIN32) WIN32_LEAN_AND_MEAN NOGPICAPMASKS NOVIRTUALKEYCODES - #NOWINMESSAGES + NOWINMESSAGES NOWINSTYLES NOSYSMETRICS NOMENUS @@ -78,7 +83,7 @@ if (WIN32) #NOGDI NOKERNEL #NOUSER - #NONLS + NONLS NOMB NOMEMMGR NOMETAFILE @@ -88,7 +93,7 @@ if (WIN32) NOSCROLL NOSERVICE NOSOUND - #NOTEXTMETRIC + NOTEXTMETRIC NOWH NOWINOFFSETS NOCOMM diff --git a/Runtime/lib/basic.cpp b/Runtime/lib/basic.cpp index 270f566..fdf3516 100644 --- a/Runtime/lib/basic.cpp +++ b/Runtime/lib/basic.cpp @@ -1,5 +1,5 @@ -#include "std.hpp" #include "bbsys.hpp" +#include "std.hpp" //how many strings allocated static int stringCnt; @@ -17,7 +17,7 @@ static const int OBJ_NEW_INC = 512; static const int STR_NEW_INC = 512; //current data ptr -static BBData *dataPtr; +static BBData* dataPtr; //chunks of mem - WHAT THE FUCK WAS I ON?!?!?!? //static list memBlks; @@ -37,7 +37,8 @@ static BBType _bbFltType(BBTYPE_FLT); static BBType _bbStrType(BBTYPE_STR); static BBType _bbCStrType(BBTYPE_CSTR); -static void *bbMalloc(int size) { +static void* bbMalloc(int size) +{ return malloc(size); /* char *c=new char[ size ]; @@ -46,7 +47,8 @@ static void *bbMalloc(int size) { */ } -static void bbFree(void *q) { +static void bbFree(void* q) +{ free(q); /* if( !q ) return; @@ -56,151 +58,196 @@ static void bbFree(void *q) { */ } -static void removeStr(BBStr *str) { +static void removeStr(BBStr* str) +{ str->next->prev = str->prev; str->prev->next = str->next; } -static void insertStr(BBStr *str, BBStr *next) { - str->next = next; - str->prev = next->prev; +static void insertStr(BBStr* str, BBStr* next) +{ + str->next = next; + str->prev = next->prev; str->prev->next = str; - next->prev = str; + next->prev = str; } -void *BBStr::operator new(size_t size) { +void* BBStr::operator new(size_t size) +{ if (freeStrs.next == &freeStrs) { - BBStr *t = (BBStr*)bbMalloc(sizeof(BBStr)*STR_NEW_INC); - for (int k = 0; k < STR_NEW_INC; ++k) insertStr(t++, &freeStrs); + BBStr* t = (BBStr*)bbMalloc(sizeof(BBStr) * STR_NEW_INC); + for (int k = 0; k < STR_NEW_INC; ++k) + insertStr(t++, &freeStrs); } - BBStr *t = freeStrs.next; - removeStr(t); insertStr(t, &usedStrs); + BBStr* t = freeStrs.next; + removeStr(t); + insertStr(t, &usedStrs); return t; } -void BBStr::operator delete(void *q) { - if (!q) return; - BBStr *t = (BBStr*)q; - removeStr(t); insertStr(t, &freeStrs); +void BBStr::operator delete(void* q) +{ + if (!q) + return; + BBStr* t = (BBStr*)q; + removeStr(t); + insertStr(t, &freeStrs); } -BBStr::BBStr() { +BBStr::BBStr() +{ ++stringCnt; } -BBStr::BBStr(const char *s) :string(s) { +BBStr::BBStr(const char* s) : string(s) +{ ++stringCnt; } -BBStr::BBStr(const char *s, int n) : string(s, n) { +BBStr::BBStr(const char* s, int n) : string(s, n) +{ ++stringCnt; } -BBStr::BBStr(const BBStr &s) : string(s) { +BBStr::BBStr(const BBStr& s) : string(s) +{ ++stringCnt; } -BBStr::BBStr(const string &s) : string(s) { +BBStr::BBStr(const string& s) : string(s) +{ ++stringCnt; } -BBStr &BBStr::operator=(const char *s) { - string::operator=(s); return *this; +BBStr& BBStr::operator=(const char* s) +{ + string::operator=(s); + return *this; } -BBStr &BBStr::operator=(const BBStr &s) { - string::operator=(s); return *this; +BBStr& BBStr::operator=(const BBStr& s) +{ + string::operator=(s); + return *this; } -BBStr &BBStr::operator=(const string &s) { - string::operator=(s); return *this; +BBStr& BBStr::operator=(const string& s) +{ + string::operator=(s); + return *this; } -BBStr::~BBStr() { +BBStr::~BBStr() +{ --stringCnt; } -BBStr *_bbStrLoad(BBStr **var) { +BBStr* _bbStrLoad(BBStr** var) +{ return *var ? new BBStr(**var) : new BBStr(); } -void _bbStrRelease(BBStr *str) { +void _bbStrRelease(BBStr* str) +{ delete str; } -void _bbStrStore(BBStr **var, BBStr *str) { - _bbStrRelease(*var); *var = str; +void _bbStrStore(BBStr** var, BBStr* str) +{ + _bbStrRelease(*var); + *var = str; } -BBStr *_bbStrConcat(BBStr *s1, BBStr *s2) { - *s1 += *s2; delete s2; return s1; +BBStr* _bbStrConcat(BBStr* s1, BBStr* s2) +{ + *s1 += *s2; + delete s2; + return s1; } -int _bbStrCompare(BBStr *lhs, BBStr *rhs) { +int _bbStrCompare(BBStr* lhs, BBStr* rhs) +{ int n = lhs->compare(*rhs); - delete lhs; delete rhs; return n; + delete lhs; + delete rhs; + return n; } -int _bbStrToInt(BBStr *s) { +int _bbStrToInt(BBStr* s) +{ int n = atoi(*s); - delete s; return n; + delete s; + return n; } -BBStr *_bbStrFromInt(int n) { +BBStr* _bbStrFromInt(int n) +{ return new BBStr(itoa(n)); } -float _bbStrToFloat(BBStr *s) { +float _bbStrToFloat(BBStr* s) +{ float n = (float)atof(*s); - delete s; return n; + delete s; + return n; } -BBStr *_bbStrFromFloat(float n) { +BBStr* _bbStrFromFloat(float n) +{ return new BBStr(ftoa(n)); } -BBStr *_bbStrConst(const char *s) { +BBStr* _bbStrConst(const char* s) +{ return new BBStr(s); } -void * _bbVecAlloc(BBVecType *type) { - void *vec = bbMalloc(type->size * 4); +void* _bbVecAlloc(BBVecType* type) +{ + void* vec = bbMalloc(type->size * 4); memset(vec, 0, type->size * 4); return vec; } -void _bbVecFree(void *vec, BBVecType *type) { +void _bbVecFree(void* vec, BBVecType* type) +{ if (type->elementType->type == BBTYPE_STR) { - BBStr **p = (BBStr**)vec; + BBStr** p = (BBStr**)vec; for (int k = 0; k < type->size; ++p, ++k) { - if (*p) _bbStrRelease(*p); + if (*p) + _bbStrRelease(*p); } } else if (type->elementType->type == BBTYPE_OBJ) { - BBObj **p = (BBObj**)vec; + BBObj** p = (BBObj**)vec; for (int k = 0; k < type->size; ++p, ++k) { - if (*p) _bbObjRelease(*p); + if (*p) + _bbObjRelease(*p); } } bbFree(vec); } -void _bbVecBoundsEx() { +void _bbVecBoundsEx() +{ ThrowRuntimeException("Blitz array index out of bounds"); } -void _bbUndimArray(BBArray *array) { - if (void *t = array->data) { +void _bbUndimArray(BBArray* array) +{ + if (void* t = array->data) { if (array->elementType == BBTYPE_STR) { - BBStr **p = (BBStr**)t; - int size = array->scales[array->dims - 1]; + BBStr** p = (BBStr**)t; + int size = array->scales[array->dims - 1]; for (int k = 0; k < size; ++p, ++k) { - if (*p) _bbStrRelease(*p); + if (*p) + _bbStrRelease(*p); } } else if (array->elementType == BBTYPE_OBJ) { - BBObj **p = (BBObj**)t; - int size = array->scales[array->dims - 1]; + BBObj** p = (BBObj**)t; + int size = array->scales[array->dims - 1]; for (int k = 0; k < size; ++p, ++k) { - if (*p) _bbObjRelease(*p); + if (*p) + _bbObjRelease(*p); } } bbFree(t); @@ -208,47 +255,53 @@ void _bbUndimArray(BBArray *array) { } } -void _bbDimArray(BBArray *array) { +void _bbDimArray(BBArray* array) +{ int k; - for (k = 0; k < array->dims; ++k) ++array->scales[k]; + for (k = 0; k < array->dims; ++k) + ++array->scales[k]; for (k = 1; k < array->dims; ++k) { array->scales[k] *= array->scales[k - 1]; } - int size = array->scales[array->dims - 1]; + int size = array->scales[array->dims - 1]; array->data = bbMalloc(size * 4); memset(array->data, 0, size * 4); } -void _bbArrayBoundsEx() { +void _bbArrayBoundsEx() +{ ThrowRuntimeException("Array index out of bounds"); } -static void unlinkObj(BBObj *obj) { +static void unlinkObj(BBObj* obj) +{ obj->next->prev = obj->prev; obj->prev->next = obj->next; } -static void insertObj(BBObj *obj, BBObj *next) { - obj->next = next; - obj->prev = next->prev; +static void insertObj(BBObj* obj, BBObj* next) +{ + obj->next = next; + obj->prev = next->prev; next->prev->next = obj; - next->prev = obj; + next->prev = obj; } -BBObj *_bbObjNew(BBObjType *type) { +BBObj* _bbObjNew(BBObjType* type) +{ if (type->free.next == &type->free) { - int obj_size = sizeof(BBObj) + type->fieldCnt * 4; - BBObj *o = (BBObj*)bbMalloc(obj_size*OBJ_NEW_INC); + int obj_size = sizeof(BBObj) + type->fieldCnt * 4; + BBObj* o = (BBObj*)bbMalloc(obj_size * OBJ_NEW_INC); for (int k = 0; k < OBJ_NEW_INC; ++k) { insertObj(o, &type->free); o = (BBObj*)((char*)o + obj_size); } } - BBObj *o = type->free.next; + BBObj* o = type->free.next; unlinkObj(o); - o->type = type; + o->type = type; o->ref_cnt = 1; - o->fields = (BBField*)(o + 1); + o->fields = (BBField*)(o + 1); for (int k = 0; k < type->fieldCnt; ++k) { switch (type->fieldTypes[k]->type) { case BBTYPE_VEC: @@ -264,11 +317,14 @@ BBObj *_bbObjNew(BBObjType *type) { return o; } -void _bbObjDelete(BBObj *obj) { - if (!obj) return; - BBField *fields = obj->fields; - if (!fields) return; - BBObjType *type = obj->type; +void _bbObjDelete(BBObj* obj) +{ + if (!obj) + return; + BBField* fields = obj->fields; + if (!fields) + return; + BBObjType* type = obj->type; for (int k = 0; k < type->fieldCnt; ++k) { switch (type->fieldTypes[k]->type) { case BBTYPE_STR: @@ -292,122 +348,157 @@ void _bbObjDelete(BBObj *obj) { --objCnt; } -void _bbObjDeleteEach(BBObjType *type) { - BBObj *obj = type->used.next; +void _bbObjDeleteEach(BBObjType* type) +{ + BBObj* obj = type->used.next; while (obj->type) { - BBObj *next = obj->next; - if (obj->fields) _bbObjDelete(obj); + BBObj* next = obj->next; + if (obj->fields) + _bbObjDelete(obj); obj = next; } } -extern void bbDebugLog(BBStr *t); +extern void bbDebugLog(BBStr* t); extern void bbStop(); -void _bbObjRelease(BBObj *obj) { - if (!obj || --obj->ref_cnt) return; +void _bbObjRelease(BBObj* obj) +{ + if (!obj || --obj->ref_cnt) + return; unlinkObj(obj); insertObj(obj, &obj->type->free); --unrelObjCnt; } -void _bbObjStore(BBObj **var, BBObj *obj) { - if (obj) ++obj->ref_cnt; //do this first incase of self-assignment +void _bbObjStore(BBObj** var, BBObj* obj) +{ + if (obj) + ++obj->ref_cnt; //do this first incase of self-assignment _bbObjRelease(*var); *var = obj; } -int _bbObjCompare(BBObj *o1, BBObj *o2) { +int _bbObjCompare(BBObj* o1, BBObj* o2) +{ return (o1 ? o1->fields : 0) != (o2 ? o2->fields : 0); } -BBObj *_bbObjNext(BBObj *obj) { +BBObj* _bbObjNext(BBObj* obj) +{ do { obj = obj->next; - if (!obj->type) return 0; + if (!obj->type) + return 0; } while (!obj->fields); return obj; } -BBObj *_bbObjPrev(BBObj *obj) { +BBObj* _bbObjPrev(BBObj* obj) +{ do { obj = obj->prev; - if (!obj->type) return 0; + if (!obj->type) + return 0; } while (!obj->fields); return obj; } -BBObj *_bbObjFirst(BBObjType *type) { +BBObj* _bbObjFirst(BBObjType* type) +{ return _bbObjNext(&type->used); } -BBObj *_bbObjLast(BBObjType *type) { +BBObj* _bbObjLast(BBObjType* type) +{ return _bbObjPrev(&type->used); } -void _bbObjInsBefore(BBObj *o1, BBObj *o2) { - if (o1 == o2) return; +void _bbObjInsBefore(BBObj* o1, BBObj* o2) +{ + if (o1 == o2) + return; unlinkObj(o1); insertObj(o1, o2); } -void _bbObjInsAfter(BBObj *o1, BBObj *o2) { - if (o1 == o2) return; +void _bbObjInsAfter(BBObj* o1, BBObj* o2) +{ + if (o1 == o2) + return; unlinkObj(o1); insertObj(o1, o2->next); } -int _bbObjEachFirst(BBObj **var, BBObjType *type) { +int _bbObjEachFirst(BBObj** var, BBObjType* type) +{ _bbObjStore(var, _bbObjFirst(type)); return *var != 0; } -int _bbObjEachNext(BBObj **var) { +int _bbObjEachNext(BBObj** var) +{ _bbObjStore(var, _bbObjNext(*var)); return *var != 0; } -int _bbObjEachFirst2(BBObj **var, BBObjType *type) { +int _bbObjEachFirst2(BBObj** var, BBObjType* type) +{ *var = _bbObjFirst(type); return *var != 0; } -int _bbObjEachNext2(BBObj **var) { +int _bbObjEachNext2(BBObj** var) +{ *var = _bbObjNext(*var); return *var != 0; } -BBStr *_bbObjToStr(BBObj *obj) { - if (!obj || !obj->fields) return new BBStr("[NULL]"); +BBStr* _bbObjToStr(BBObj* obj) +{ + if (!obj || !obj->fields) + return new BBStr("[NULL]"); - static BBObj *root; - static int recurs_cnt; + static BBObj* root; + static int recurs_cnt; - if (obj == root) return new BBStr("[ROOT]"); - if (recurs_cnt == 8) return new BBStr("...."); + if (obj == root) + return new BBStr("[ROOT]"); + if (recurs_cnt == 8) + return new BBStr("...."); ++recurs_cnt; - BBObj *oldRoot = root; - if (!root) root = obj; + BBObj* oldRoot = root; + if (!root) + root = obj; - BBObjType *type = obj->type; - BBField *fields = obj->fields; - BBStr *s = new BBStr("["), *t; + BBObjType* type = obj->type; + BBField* fields = obj->fields; + BBStr * s = new BBStr("["), *t; for (int k = 0; k < type->fieldCnt; ++k) { - if (k) *s += ','; + if (k) + *s += ','; switch (type->fieldTypes[k]->type) { case BBTYPE_INT: - t = _bbStrFromInt(fields[k].INT); *s += *t; delete t; + t = _bbStrFromInt(fields[k].INT); + *s += *t; + delete t; break; case BBTYPE_FLT: - t = _bbStrFromFloat(fields[k].FLT); *s += *t; delete t; + t = _bbStrFromFloat(fields[k].FLT); + *s += *t; + delete t; break; case BBTYPE_STR: - if (fields[k].STR) *s += '\"' + *fields[k].STR + '\"'; - else *s += "\"\""; + if (fields[k].STR) + *s += '\"' + *fields[k].STR + '\"'; + else + *s += "\"\""; break; case BBTYPE_OBJ: - t = _bbObjToStr(fields[k].OBJ); *s += *t; delete t; + t = _bbObjToStr(fields[k].OBJ); + *s += *t; + delete t; break; default: *s += "???"; @@ -419,90 +510,129 @@ BBStr *_bbObjToStr(BBObj *obj) { return s; } -int _bbObjToHandle(BBObj *obj) { - if (!obj || !obj->fields) return 0; +int _bbObjToHandle(BBObj* obj) +{ + if (!obj || !obj->fields) + return 0; map::const_iterator it = object_map.find(obj); - if (it != object_map.end()) return it->second; + if (it != object_map.end()) + return it->second; ++next_handle; - object_map[obj] = next_handle; + object_map[obj] = next_handle; handle_map[next_handle] = obj; return next_handle; } -BBObj *_bbObjFromHandle(int handle, BBObjType *type) { +BBObj* _bbObjFromHandle(int handle, BBObjType* type) +{ map::const_iterator it = handle_map.find(handle); - if (it == handle_map.end()) return 0; - BBObj *obj = it->second; + if (it == handle_map.end()) + return 0; + BBObj* obj = it->second; return obj->type == type ? obj : 0; } -void _bbNullObjEx() { +void _bbNullObjEx() +{ ThrowRuntimeException("Object does not exist"); } -void _bbRestore(BBData *data) { +void _bbRestore(BBData* data) +{ dataPtr = data; } -int _bbReadInt() { +int _bbReadInt() +{ switch (dataPtr->fieldType) { - case BBTYPE_END:ThrowRuntimeException("Out of data"); return 0; - case BBTYPE_INT:return dataPtr++->field.INT; - case BBTYPE_FLT:return dataPtr++->field.FLT; - case BBTYPE_CSTR:return atoi(dataPtr++->field.CSTR); - default:ThrowRuntimeException("Bad data type"); return 0; + case BBTYPE_END: + ThrowRuntimeException("Out of data"); + return 0; + case BBTYPE_INT: + return dataPtr++->field.INT; + case BBTYPE_FLT: + return dataPtr++->field.FLT; + case BBTYPE_CSTR: + return atoi(dataPtr++->field.CSTR); + default: + ThrowRuntimeException("Bad data type"); + return 0; } } -float _bbReadFloat() { +float _bbReadFloat() +{ switch (dataPtr->fieldType) { - case BBTYPE_END:ThrowRuntimeException("Out of data"); return 0; - case BBTYPE_INT:return dataPtr++->field.INT; - case BBTYPE_FLT:return dataPtr++->field.FLT; - case BBTYPE_CSTR:return atof(dataPtr++->field.CSTR); - default:ThrowRuntimeException("Bad data type"); return 0; + case BBTYPE_END: + ThrowRuntimeException("Out of data"); + return 0; + case BBTYPE_INT: + return dataPtr++->field.INT; + case BBTYPE_FLT: + return dataPtr++->field.FLT; + case BBTYPE_CSTR: + return atof(dataPtr++->field.CSTR); + default: + ThrowRuntimeException("Bad data type"); + return 0; } } -BBStr *_bbReadStr() { +BBStr* _bbReadStr() +{ switch (dataPtr->fieldType) { - case BBTYPE_END:ThrowRuntimeException("Out of data"); return 0; - case BBTYPE_INT:return new BBStr(itoa(dataPtr++->field.INT)); - case BBTYPE_FLT:return new BBStr(ftoa(dataPtr++->field.FLT)); - case BBTYPE_CSTR:return new BBStr(dataPtr++->field.CSTR); - default:ThrowRuntimeException("Bad data type"); return 0; + case BBTYPE_END: + ThrowRuntimeException("Out of data"); + return 0; + case BBTYPE_INT: + return new BBStr(itoa(dataPtr++->field.INT)); + case BBTYPE_FLT: + return new BBStr(ftoa(dataPtr++->field.FLT)); + case BBTYPE_CSTR: + return new BBStr(dataPtr++->field.CSTR); + default: + ThrowRuntimeException("Bad data type"); + return 0; } } -int _bbAbs(int n) { +int _bbAbs(int n) +{ return n >= 0 ? n : -n; } -int _bbSgn(int n) { +int _bbSgn(int n) +{ return n > 0 ? 1 : (n < 0 ? -1 : 0); } -int _bbMod(int x, int y) { - return x%y; +int _bbMod(int x, int y) +{ + return x % y; } -float _bbFAbs(float n) { +float _bbFAbs(float n) +{ return n >= 0 ? n : -n; } -float _bbFSgn(float n) { +float _bbFSgn(float n) +{ return n > 0 ? 1 : (n < 0 ? -1 : 0); } -float _bbFMod(float x, float y) { +float _bbFMod(float x, float y) +{ return (float)fmod(x, y); } -float _bbFPow(float x, float y) { +float _bbFPow(float x, float y) +{ return (float)pow(x, y); } -void bbRuntimeStats() { +void bbRuntimeStats() +{ gx_runtime->debugLog(("Active strings :" + itoa(stringCnt)).c_str()); gx_runtime->debugLog(("Active objects :" + itoa(objCnt)).c_str()); gx_runtime->debugLog(("Unreleased objs:" + itoa(unrelObjCnt)).c_str()); @@ -516,7 +646,8 @@ void bbRuntimeStats() { */ } -bool basic_create() { +bool basic_create() +{ next_handle = 0; // memBlks.clear(); handle_map.clear(); @@ -527,16 +658,18 @@ bool basic_create() { return true; } -bool basic_destroy() { - while (usedStrs.next != &usedStrs) delete usedStrs.next; +bool basic_destroy() +{ + while (usedStrs.next != &usedStrs) + delete usedStrs.next; // while( memBlks.size() ) bbFree( memBlks.back() ); handle_map.clear(); object_map.clear(); return true; } -void basic_link(void(*rtSym)(const char *sym, void *pc)) { - +void basic_link(void (*rtSym)(const char* sym, void* pc)) +{ rtSym("_bbIntType", &_bbIntType); rtSym("_bbFltType", &_bbFltType); rtSym("_bbStrType", &_bbStrType); diff --git a/Runtime/lib/basic.hpp b/Runtime/lib/basic.hpp index a44f135..484400e 100644 --- a/Runtime/lib/basic.hpp +++ b/Runtime/lib/basic.hpp @@ -2,89 +2,98 @@ #ifndef BASIC_H #define BASIC_H -#include #include +#include enum { - BBTYPE_END = 0, - BBTYPE_INT = 1, BBTYPE_FLT = 2, - BBTYPE_STR = 3, BBTYPE_CSTR = 4, - BBTYPE_OBJ = 5, BBTYPE_VEC = 6 + BBTYPE_END = 0, + BBTYPE_INT = 1, + BBTYPE_FLT = 2, + BBTYPE_STR = 3, + BBTYPE_CSTR = 4, + BBTYPE_OBJ = 5, + BBTYPE_VEC = 6 }; -#pragma pack( push,1 ) +#pragma pack(push, 1) struct BBObj; struct BBStr; struct BBType; struct BBObjType; struct BBVecType; -union BBField; +union BBField; struct BBArray; struct BBObj { - BBField *fields; - BBObj *next, *prev; - BBObjType *type; - int ref_cnt; + BBField* fields; + BBObj * next, *prev; + BBObjType* type; + int ref_cnt; }; struct BBType { int type; - BBType(int n) :type(n) {} + BBType(int n) : type(n) {} }; struct BBObjType : public BBType { - BBObj used, free; - int fieldCnt; - BBType *fieldTypes[1]; + BBObj used, free; + int fieldCnt; + BBType* fieldTypes[1]; }; struct BBVecType : public BBType { - int size; - BBType *elementType; + int size; + BBType* elementType; }; union BBField { - int INT; - float FLT; - BBStr *STR; - char *CSTR; - BBObj *OBJ; - void *VEC; + int INT; + float FLT; + BBStr* STR; + char* CSTR; + BBObj* OBJ; + void* VEC; }; struct BBArray { - void *data; - int elementType, dims, scales[1]; + void* data; + int elementType, dims, scales[1]; }; struct BBStr : public std::string { BBStr *next, *prev; BBStr(); - BBStr(const char *s); - BBStr(const char *s, int n); - BBStr(const BBStr &s); - BBStr(const std::string &s); - BBStr &operator=(const char *s); - BBStr &operator=(const BBStr &s); - BBStr &operator=(const std::string &s); + BBStr(const char* s); + BBStr(const char* s, int n); + BBStr(const BBStr& s); + BBStr(const std::string& s); + BBStr& operator=(const char* s); + BBStr& operator=(const BBStr& s); + BBStr& operator=(const std::string& s); ~BBStr(); - void *operator new(size_t size); - void operator delete(void *q); + void* operator new(size_t size); + void operator delete(void* q); - void *operator new(size_t size, const char *file, int line) { return operator new(size); } - void operator delete(void *q, const char *file, int line) { operator delete(q); } + void* operator new(size_t size, const char* file, int line) + { + return operator new(size); + } + void operator delete(void* q, const char* file, int line) + { + operator delete(q); + } }; struct BBData { - int fieldType; + int fieldType; BBField field; }; -#pragma pack( pop ) +#pragma pack(pop) void basic_link(); @@ -93,58 +102,58 @@ extern BBType _bbFltType; extern BBType _bbStrType; extern BBType _bbCStrType; -BBStr * _bbStrLoad(BBStr **var); -void _bbStrRelease(BBStr *str); -void _bbStrStore(BBStr **var, BBStr *str); -int _bbStrCompare(BBStr *lhs, BBStr *rhs); +BBStr* _bbStrLoad(BBStr** var); +void _bbStrRelease(BBStr* str); +void _bbStrStore(BBStr** var, BBStr* str); +int _bbStrCompare(BBStr* lhs, BBStr* rhs); -BBStr * _bbStrConcat(BBStr *s1, BBStr *s2); -int _bbStrToInt(BBStr *s); -BBStr * _bbStrFromInt(int n); -float _bbStrToFloat(BBStr *s); -BBStr * _bbStrFromFloat(float n); -BBStr * _bbStrConst(const char *s); +BBStr* _bbStrConcat(BBStr* s1, BBStr* s2); +int _bbStrToInt(BBStr* s); +BBStr* _bbStrFromInt(int n); +float _bbStrToFloat(BBStr* s); +BBStr* _bbStrFromFloat(float n); +BBStr* _bbStrConst(const char* s); -void _bbDimArray(BBArray *array); -void _bbUndimArray(BBArray *array); -void _bbArrayBoundsEx(); +void _bbDimArray(BBArray* array); +void _bbUndimArray(BBArray* array); +void _bbArrayBoundsEx(); -void * _bbVecAlloc(BBVecType *type); -void _bbVecFree(void *vec, BBVecType *type); -void _bbVecBoundsEx(); +void* _bbVecAlloc(BBVecType* type); +void _bbVecFree(void* vec, BBVecType* type); +void _bbVecBoundsEx(); -BBObj * _bbObjNew(BBObjType *t); -void _bbObjDelete(BBObj *obj); -void _bbObjDeleteEach(BBObjType *type); -void _bbObjRelease(BBObj *obj); -void _bbObjStore(BBObj **var, BBObj *obj); -BBObj * _bbObjNext(BBObj *obj); -BBObj * _bbObjPrev(BBObj *obj); -BBObj * _bbObjFirst(BBObjType *t); -BBObj * _bbObjLast(BBObjType *t); -void _bbObjInsBefore(BBObj *o1, BBObj *o2); -void _bbObjInsAfter(BBObj *o1, BBObj *o2); -int _bbObjEachFirst(BBObj **var, BBObjType *type); -int _bbObjEachNext(BBObj **var); -int _bbObjCompare(BBObj *o1, BBObj *o2); -BBStr * _bbObjToStr(BBObj *obj); -int _bbObjToHandle(BBObj *obj); -BBObj * _bbObjFromHandle(int handle, BBObjType *type); -void _bbNullObjEx(); +BBObj* _bbObjNew(BBObjType* t); +void _bbObjDelete(BBObj* obj); +void _bbObjDeleteEach(BBObjType* type); +void _bbObjRelease(BBObj* obj); +void _bbObjStore(BBObj** var, BBObj* obj); +BBObj* _bbObjNext(BBObj* obj); +BBObj* _bbObjPrev(BBObj* obj); +BBObj* _bbObjFirst(BBObjType* t); +BBObj* _bbObjLast(BBObjType* t); +void _bbObjInsBefore(BBObj* o1, BBObj* o2); +void _bbObjInsAfter(BBObj* o1, BBObj* o2); +int _bbObjEachFirst(BBObj** var, BBObjType* type); +int _bbObjEachNext(BBObj** var); +int _bbObjCompare(BBObj* o1, BBObj* o2); +BBStr* _bbObjToStr(BBObj* obj); +int _bbObjToHandle(BBObj* obj); +BBObj* _bbObjFromHandle(int handle, BBObjType* type); +void _bbNullObjEx(); -void _bbRestore(BBData *data); -int _bbReadInt(); -float _bbReadFloat(); -BBStr * _bbReadStr(); +void _bbRestore(BBData* data); +int _bbReadInt(); +float _bbReadFloat(); +BBStr* _bbReadStr(); -int _bbAbs(int n); -int _bbSgn(int n); -int _bbMod(int x, int y); -float _bbFAbs(float n); -float _bbFSgn(float n); -float _bbFMod(float x, float y); -float _bbFPow(float x, float y); +int _bbAbs(int n); +int _bbSgn(int n); +int _bbMod(int x, int y); +float _bbFAbs(float n); +float _bbFSgn(float n); +float _bbFMod(float x, float y); +float _bbFPow(float x, float y); -void bbRuntimeStats(); +void bbRuntimeStats(); #endif \ No newline at end of file diff --git a/Runtime/lib/bb_basic.hpp b/Runtime/lib/bb_basic.hpp index 61caad2..1d7f312 100644 --- a/Runtime/lib/bb_basic.hpp +++ b/Runtime/lib/bb_basic.hpp @@ -1,82 +1,83 @@ -enum{ - BBTYPE_END=0, - BBTYPE_INT=1,BBTYPE_FLOAT=2, - BBTYPE_STRING=3,BBTYPE_CSTR=4, - BBTYPE_OBJECT=5,BBTYPE_VECTOR=6 +enum { + BBTYPE_END = 0, + BBTYPE_INT = 1, + BBTYPE_FLOAT = 2, + BBTYPE_STRING = 3, + BBTYPE_CSTR = 4, + BBTYPE_OBJECT = 5, + BBTYPE_VECTOR = 6 }; -typedef int bbInt; -typedef float bbFloat; -typedef bbStringhandle *bbString; -typedef bbObjectHandle *bbObject; -typedef bbVectorHandle *bbVector; -typedef const char * bbCStr; +typedef int bbInt; +typedef float bbFloat; +typedef bbStringhandle* bbString; +typedef bbObjectHandle* bbObject; +typedef bbVectorHandle* bbVector; +typedef const char* bbCStr; -union bbValue{ - bbInt INT; - bbFloat FLOAT; +union bbValue { + bbInt INT; + bbFloat FLOAT; bbString STRING; bbObject OBJECT; bbVector VECTOR; - bbCStr CSTR; + bbCStr CSTR; }; -struct bbType{ +struct bbType { int id; bbType( int n ):id(n(){} }; -struct bbInstance{ +struct bbInstance { bbValue value; }; -struct bbHandle{ - bbInstance *instance; - int ref_cnt; - bbType *type; +struct bbHandle { + bbInstance* instance; + int ref_cnt; + bbType* type; }; -struct bbEnviron{ - bbVector *variables; +struct bbEnviron { + bbVector* variables; }; -struct bbIntType : public bbType{ - bbInt():bbType( BBTYPE_INT ){} +struct bbIntType : public bbType { + bbInt() : bbType(BBTYPE_INT) {} }; -struct bbFloatType : public bbType{ - bbFloat():bbType( BBTYPE_FLOAT ){} +struct bbFloatType : public bbType { + bbFloat() : bbType(BBTYPE_FLOAT) {} }; -struct bbCStrType : public bbType{ - bbCStrType():bbType( BBTYPE_CSTR ){} +struct bbCStrType : public bbType { + bbCStrType() : bbType(BBTYPE_CSTR) {} }; -struct bbStringType : public bbType{ - bbStringType():bbType( BBTYPE_STRING ){} +struct bbStringType : public bbType { + bbStringType() : bbType(BBTYPE_STRING) {} }; -struct bbVectorType : public bbType{ - bbType *element_type; - bbVectorType( bbType *e ):bbType( BBTYPE_VECTOR ),element_type( e ){} +struct bbVectorType : public bbType { + bbType* element_type; + bbVectorType(bbType* e) : bbType(BBTYPE_VECTOR), element_type(e) {} } -struct bbObjectType : public bbType{ - bbEnviron *environ; - bbObject *first_used,*last_used; - bbObject *first_free,*last_free; - bbObjectType( bbEnviron *e ):bbType( BBTYPE_OBJECT ),environ( e ){} +struct bbObjectType : public bbType { + bbEnviron* environ; + bbObject * first_used, *last_used; + bbObject * first_free, *last_free; + bbObjectType(bbEnviron* e) : bbType(BBTYPE_OBJECT), environ(e) {} }; -struct bbStringHandle : public bbHandle{ +struct bbStringHandle : public bbHandle {}; + +struct bbObjectHandle : public bbHandle { + bbObject *next, *prev; }; -struct bbObjectHandle : public bbHandle{ - bbObject *next,*prev; -}; +struct bbVectorHandle : public bbHandle {}; -struct bbVectorHandle : public bbHandle{ -}; - -void assign( bbHandleVariable dest,bbHandle src ); +void assign(bbHandleVariable dest, bbHandle src); diff --git a/Runtime/lib/bbaudio.cpp b/Runtime/lib/bbaudio.cpp index 0df7256..d7baf0f 100644 --- a/Runtime/lib/bbaudio.cpp +++ b/Runtime/lib/bbaudio.cpp @@ -1,138 +1,177 @@ -#include "std.hpp" #include "bbaudio.hpp" +#include "std.hpp" -gxAudio *gx_audio; +gxAudio* gx_audio; -static inline void debugSound( gxSound *s ){ - if( debug ){ - if( !gx_audio->verifySound( s ) ) ThrowRuntimeException( "Sound does not exist" ); +static inline void debugSound(gxSound* s) +{ + if (debug) { + if (!gx_audio->verifySound(s)) + ThrowRuntimeException("Sound does not exist"); } } -static gxSound *loadSound( BBStr *f,bool use_3d ){ - string t=*f;delete f; - return gx_audio ? gx_audio->loadSound( t,use_3d ) : 0; +static gxSound* loadSound(BBStr* f, bool use_3d) +{ + string t = *f; + delete f; + return gx_audio ? gx_audio->loadSound(t, use_3d) : 0; } -static gxChannel *playMusic( BBStr *f,bool use_3d ){ - string t=*f;delete f; - return gx_audio ? gx_audio->playFile( t,use_3d ) : 0; +static gxChannel* playMusic(BBStr* f, bool use_3d) +{ + string t = *f; + delete f; + return gx_audio ? gx_audio->playFile(t, use_3d) : 0; } -gxSound *bbLoadSound( BBStr *f ){ - return loadSound( f,false ); +gxSound* bbLoadSound(BBStr* f) +{ + return loadSound(f, false); } -void bbFreeSound( gxSound *sound ){ - if( !sound ) return; - debugSound( sound ); - gx_audio->freeSound( sound ); +void bbFreeSound(gxSound* sound) +{ + if (!sound) + return; + debugSound(sound); + gx_audio->freeSound(sound); } -void bbLoopSound( gxSound *sound ){ - if( !sound ) return; - debugSound( sound ); - sound->setLoop( true ); +void bbLoopSound(gxSound* sound) +{ + if (!sound) + return; + debugSound(sound); + sound->setLoop(true); } -void bbSoundPitch( gxSound *sound,int pitch ){ - if( !sound ) return; - debugSound( sound ); - sound->setPitch( pitch ); +void bbSoundPitch(gxSound* sound, int pitch) +{ + if (!sound) + return; + debugSound(sound); + sound->setPitch(pitch); } -void bbSoundVolume( gxSound *sound,float volume ){ - if( !sound ) return; - debugSound( sound ); - sound->setVolume( volume ); +void bbSoundVolume(gxSound* sound, float volume) +{ + if (!sound) + return; + debugSound(sound); + sound->setVolume(volume); } -void bbSoundPan( gxSound *sound,float pan ){ - if( !sound ) return; - debugSound( sound ); - sound->setPan( pan ); +void bbSoundPan(gxSound* sound, float pan) +{ + if (!sound) + return; + debugSound(sound); + sound->setPan(pan); } -gxChannel *bbPlaySound( gxSound *sound ){ - if( !sound ) return 0; - debugSound( sound ); +gxChannel* bbPlaySound(gxSound* sound) +{ + if (!sound) + return 0; + debugSound(sound); return sound->play(); } -gxChannel *bbPlayMusic( BBStr *f ){ - return playMusic( f,false ); +gxChannel* bbPlayMusic(BBStr* f) +{ + return playMusic(f, false); } -gxChannel *bbPlayCDTrack( int track,int mode ){ - return gx_audio ? gx_audio->playCDTrack( track,mode ) : 0; +gxChannel* bbPlayCDTrack(int track, int mode) +{ + return gx_audio ? gx_audio->playCDTrack(track, mode) : 0; } -void bbStopChannel( gxChannel *channel ){ - if( !channel ) return; +void bbStopChannel(gxChannel* channel) +{ + if (!channel) + return; channel->stop(); } -void bbPauseChannel( gxChannel *channel ){ - if( !channel ) return; - channel->setPaused( true ); +void bbPauseChannel(gxChannel* channel) +{ + if (!channel) + return; + channel->setPaused(true); } -void bbResumeChannel( gxChannel *channel ){ - if( !channel ) return; - channel->setPaused( false ); +void bbResumeChannel(gxChannel* channel) +{ + if (!channel) + return; + channel->setPaused(false); } -void bbChannelPitch( gxChannel *channel,int pitch ){ - if( !channel ) return; - channel->setPitch( pitch ); +void bbChannelPitch(gxChannel* channel, int pitch) +{ + if (!channel) + return; + channel->setPitch(pitch); } -void bbChannelVolume( gxChannel *channel,float volume ){ - if( !channel ) return; - channel->setVolume( volume ); +void bbChannelVolume(gxChannel* channel, float volume) +{ + if (!channel) + return; + channel->setVolume(volume); } -void bbChannelPan( gxChannel *channel,float pan ){ - if( !channel ) return; - channel->setPan( pan ); +void bbChannelPan(gxChannel* channel, float pan) +{ + if (!channel) + return; + channel->setPan(pan); } -int bbChannelPlaying( gxChannel *channel ){ +int bbChannelPlaying(gxChannel* channel) +{ return channel ? channel->isPlaying() : 0; } -gxSound *bbLoad3DSound( BBStr *f ){ - return loadSound( f,true ); +gxSound* bbLoad3DSound(BBStr* f) +{ + return loadSound(f, true); } -bool audio_create(){ - gx_audio=gx_runtime->openAudio( 0 ); +bool audio_create() +{ + gx_audio = gx_runtime->openAudio(0); return true; } -bool audio_destroy(){ - if( gx_audio ) gx_runtime->closeAudio( gx_audio ); - gx_audio=0; +bool audio_destroy() +{ + if (gx_audio) + gx_runtime->closeAudio(gx_audio); + gx_audio = 0; return true; } -void audio_link( void(*rtSym)(const char*,void*) ){ - rtSym( "%LoadSound$filename",bbLoadSound ); - rtSym( "FreeSound%sound",bbFreeSound ); - rtSym( "LoopSound%sound",bbLoopSound ); - rtSym( "SoundPitch%sound%pitch",bbSoundPitch ); - rtSym( "SoundVolume%sound#volume",bbSoundVolume ); - rtSym( "SoundPan%sound#pan",bbSoundPan ); - rtSym( "%PlaySound%sound",bbPlaySound ); - rtSym( "%PlayMusic$midifile",bbPlayMusic ); - rtSym( "%PlayCDTrack%track%mode=1",bbPlayCDTrack ); - rtSym( "StopChannel%channel",bbStopChannel ); - rtSym( "PauseChannel%channel",bbPauseChannel ); - rtSym( "ResumeChannel%channel",bbResumeChannel ); - rtSym( "ChannelPitch%channel%pitch",bbChannelPitch ); - rtSym( "ChannelVolume%channel#volume",bbChannelVolume ); - rtSym( "ChannelPan%channel#pan",bbChannelPan ); - rtSym( "%ChannelPlaying%channel",bbChannelPlaying ); - rtSym( "%Load3DSound$filename",bbLoad3DSound ); +void audio_link(void (*rtSym)(const char*, void*)) +{ + rtSym("%LoadSound$filename", bbLoadSound); + rtSym("FreeSound%sound", bbFreeSound); + rtSym("LoopSound%sound", bbLoopSound); + rtSym("SoundPitch%sound%pitch", bbSoundPitch); + rtSym("SoundVolume%sound#volume", bbSoundVolume); + rtSym("SoundPan%sound#pan", bbSoundPan); + rtSym("%PlaySound%sound", bbPlaySound); + rtSym("%PlayMusic$midifile", bbPlayMusic); + rtSym("%PlayCDTrack%track%mode=1", bbPlayCDTrack); + rtSym("StopChannel%channel", bbStopChannel); + rtSym("PauseChannel%channel", bbPauseChannel); + rtSym("ResumeChannel%channel", bbResumeChannel); + rtSym("ChannelPitch%channel%pitch", bbChannelPitch); + rtSym("ChannelVolume%channel#volume", bbChannelVolume); + rtSym("ChannelPan%channel#pan", bbChannelPan); + rtSym("%ChannelPlaying%channel", bbChannelPlaying); + rtSym("%Load3DSound$filename", bbLoad3DSound); } diff --git a/Runtime/lib/bbaudio.hpp b/Runtime/lib/bbaudio.hpp index fe1f993..998e1f0 100644 --- a/Runtime/lib/bbaudio.hpp +++ b/Runtime/lib/bbaudio.hpp @@ -3,26 +3,25 @@ #define BBAUDIO_H #include "bbsys.hpp" -#include "../gxruntime/gxaudio.hpp" +#include "gxaudio.hpp" -extern gxAudio *gx_audio; +extern gxAudio* gx_audio; -gxSound * bbLoadSound( BBStr *file ); -void bbFreeSound( gxSound *sound ); -gxChannel * bbPlaySound( gxSound *sound ); -void bbLoopSound( gxSound *sound ); -void bbSoundPitch( gxSound *sound,int pitch ); -void bbSoundVolume( gxSound *sound,float volume ); -void bbSoundPan( gxSound *sound,float pan ); -gxChannel * bbPlayMusic( BBStr *s ); -gxChannel * bbPlayCDTrack( int track,int mode ); -void bbStopChannel( gxChannel *channel ); -void bbPauseChannel( gxChannel *channel ); -void bbResumeChannel( gxChannel *channel ); -void bbChannelPitch( gxChannel *channel,int pitch ); -void bbChannelVolume( gxChannel *channel,float volume ); -void bbChannelPan( gxChannel *channel,float pan ); -int bbChannelPlaying( gxChannel *channel ); +gxSound* bbLoadSound(BBStr* file); +void bbFreeSound(gxSound* sound); +gxChannel* bbPlaySound(gxSound* sound); +void bbLoopSound(gxSound* sound); +void bbSoundPitch(gxSound* sound, int pitch); +void bbSoundVolume(gxSound* sound, float volume); +void bbSoundPan(gxSound* sound, float pan); +gxChannel* bbPlayMusic(BBStr* s); +gxChannel* bbPlayCDTrack(int track, int mode); +void bbStopChannel(gxChannel* channel); +void bbPauseChannel(gxChannel* channel); +void bbResumeChannel(gxChannel* channel); +void bbChannelPitch(gxChannel* channel, int pitch); +void bbChannelVolume(gxChannel* channel, float volume); +void bbChannelPan(gxChannel* channel, float pan); +int bbChannelPlaying(gxChannel* channel); #endif - diff --git a/Runtime/lib/bbbank.cpp b/Runtime/lib/bbbank.cpp index 83a909b..ed366d1 100644 --- a/Runtime/lib/bbbank.cpp +++ b/Runtime/lib/bbbank.cpp @@ -1,170 +1,203 @@ -#include "std.hpp" #include "bbbank.hpp" #include "bbstream.hpp" +#include "std.hpp" -struct bbBank{ - char *data; - int size,capacity; +struct bbBank { + char* data; + int size, capacity; - bbBank( int sz ):size(sz){ - capacity=(size+15)&~15; - data=new char[capacity]; - memset( data,0,size ); + bbBank(int sz) : size(sz) + { + capacity = (size + 15) & ~15; + data = new char[capacity]; + memset(data, 0, size); } - virtual ~bbBank(){ + virtual ~bbBank() + { delete[] data; } - void resize( int n ){ - if( n>size ){ - if( n>capacity ){ - capacity=capacity*3/2; - if( n>capacity ) capacity=n; - capacity=(capacity+15)&~15; - char *p=new char[capacity]; - memcpy( p,data,size ); + void resize(int n) + { + if (n > size) { + if (n > capacity) { + capacity = capacity * 3 / 2; + if (n > capacity) + capacity = n; + capacity = (capacity + 15) & ~15; + char* p = new char[capacity]; + memcpy(p, data, size); delete[] data; - data=p; - }else memset( data+size,0,n-size ); + data = p; + } else + memset(data + size, 0, n - size); } - size=n; + size = n; } }; static set bank_set; -static inline void debugBank( bbBank *b ){ - if( debug ){ - if( !bank_set.count( b ) ) ThrowRuntimeException( "bbBank does not exist" ); +static inline void debugBank(bbBank* b) +{ + if (debug) { + if (!bank_set.count(b)) + ThrowRuntimeException("bbBank does not exist"); } } -static inline void debugBank( bbBank *b,int offset ){ - if( debug ){ - debugBank( b ); - if( offset>=b->size ) ThrowRuntimeException( "Offset out of range" ); +static inline void debugBank(bbBank* b, int offset) +{ + if (debug) { + debugBank(b); + if (offset >= b->size) + ThrowRuntimeException("Offset out of range"); } } -bbBank *bbCreateBank( int size ){ - bbBank *b=new bbBank( size ); - bank_set.insert( b ); +bbBank* bbCreateBank(int size) +{ + bbBank* b = new bbBank(size); + bank_set.insert(b); return b; } -void bbFreeBank( bbBank *b ){ - if( bank_set.erase( b ) ) delete b; +void bbFreeBank(bbBank* b) +{ + if (bank_set.erase(b)) + delete b; } -int bbBankSize( bbBank *b ){ - debugBank( b ); +int bbBankSize(bbBank* b) +{ + debugBank(b); return b->size; } -void bbResizeBank( bbBank *b,int size ){ - debugBank( b ); - b->resize( size ); +void bbResizeBank(bbBank* b, int size) +{ + debugBank(b); + b->resize(size); } -void bbCopyBank( bbBank *src,int src_p,bbBank *dest,int dest_p,int count ){ - if( debug ){ debugBank( src,src_p+count-1 );debugBank( dest,dest_p+count-1 ); } - memmove( dest->data+dest_p,src->data+src_p,count ); -} - -int bbPeekByte( bbBank *b,int offset ){ - debugBank( b,offset ); - return *(unsigned char*)(b->data+offset); -} - -int bbPeekShort( bbBank *b,int offset ){ - debugBank( b,offset+1 ); - return *(unsigned short*)(b->data+offset); -} - -int bbPeekInt( bbBank *b,int offset ){ - debugBank( b,offset+3 ); - return *(int*)(b->data+offset); -} - -float bbPeekFloat( bbBank *b,int offset ){ - debugBank( b,offset+3 ); - return *(float*)(b->data+offset); -} - -void bbPokeByte( bbBank *b,int offset,int value ){ - debugBank( b,offset ); - *(char*)(b->data+offset)=value; -} - -void bbPokeShort( bbBank *b,int offset,int value ){ - debugBank( b,offset ); - *(unsigned short*)(b->data+offset)=value; -} - -void bbPokeInt( bbBank *b,int offset,int value ){ - debugBank( b,offset ); - *(int*)(b->data+offset)=value; -} - -void bbPokeFloat( bbBank *b,int offset,float value ){ - debugBank( b,offset ); - *(float*)(b->data+offset)=value; -} - -int bbReadBytes( bbBank *b,bbStream *s,int offset,int count ){ - if( debug ){ - debugBank( b,offset+count-1 ); - debugStream( s ); +void bbCopyBank(bbBank* src, int src_p, bbBank* dest, int dest_p, int count) +{ + if (debug) { + debugBank(src, src_p + count - 1); + debugBank(dest, dest_p + count - 1); } - return s->read( b->data+offset,count ); + memmove(dest->data + dest_p, src->data + src_p, count); } -int bbWriteBytes( bbBank *b,bbStream *s,int offset,int count ){ - if( debug ){ - debugBank( b,offset+count-1 ); - debugStream( s ); - } - return s->write( b->data+offset,count ); +int bbPeekByte(bbBank* b, int offset) +{ + debugBank(b, offset); + return *(unsigned char*)(b->data + offset); } -int bbCallDLL( BBStr *dll,BBStr *fun,bbBank *in,bbBank *out ){ - if( debug ){ - if( in ) debugBank( in ); - if( out ) debugBank( out ); +int bbPeekShort(bbBank* b, int offset) +{ + debugBank(b, offset + 1); + return *(unsigned short*)(b->data + offset); +} + +int bbPeekInt(bbBank* b, int offset) +{ + debugBank(b, offset + 3); + return *(int*)(b->data + offset); +} + +float bbPeekFloat(bbBank* b, int offset) +{ + debugBank(b, offset + 3); + return *(float*)(b->data + offset); +} + +void bbPokeByte(bbBank* b, int offset, int value) +{ + debugBank(b, offset); + *(char*)(b->data + offset) = value; +} + +void bbPokeShort(bbBank* b, int offset, int value) +{ + debugBank(b, offset); + *(unsigned short*)(b->data + offset) = value; +} + +void bbPokeInt(bbBank* b, int offset, int value) +{ + debugBank(b, offset); + *(int*)(b->data + offset) = value; +} + +void bbPokeFloat(bbBank* b, int offset, float value) +{ + debugBank(b, offset); + *(float*)(b->data + offset) = value; +} + +int bbReadBytes(bbBank* b, bbStream* s, int offset, int count) +{ + if (debug) { + debugBank(b, offset + count - 1); + debugStream(s); } - int t=gx_runtime->callDll( *dll,*fun, - in ? in->data : 0,in ? in->size : 0, - out ? out->data : 0,out ? out->size : 0 ); - delete dll;delete fun; + return s->read(b->data + offset, count); +} + +int bbWriteBytes(bbBank* b, bbStream* s, int offset, int count) +{ + if (debug) { + debugBank(b, offset + count - 1); + debugStream(s); + } + return s->write(b->data + offset, count); +} + +int bbCallDLL(BBStr* dll, BBStr* fun, bbBank* in, bbBank* out) +{ + if (debug) { + if (in) + debugBank(in); + if (out) + debugBank(out); + } + int t = + gx_runtime->callDll(*dll, *fun, in ? in->data : 0, in ? in->size : 0, out ? out->data : 0, out ? out->size : 0); + delete dll; + delete fun; return t; } -bool bank_create(){ +bool bank_create() +{ return true; } -bool bank_destroy(){ - while( bank_set.size() ) bbFreeBank( *bank_set.begin() ); +bool bank_destroy() +{ + while (bank_set.size()) + bbFreeBank(*bank_set.begin()); return true; } -void bank_link( void(*rtSym)(const char*,void*) ){ - rtSym( "%CreateBank%size=0",bbCreateBank ); - rtSym( "FreeBank%bank",bbFreeBank ); - rtSym( "%BankSize%bank",bbBankSize ); - rtSym( "ResizeBank%bank%size",bbResizeBank ); - rtSym( "CopyBank%src_bank%src_offset%dest_bank%dest_offset%count",bbCopyBank ); - rtSym( "%PeekByte%bank%offset",bbPeekByte ); - rtSym( "%PeekShort%bank%offset",bbPeekShort ); - rtSym( "%PeekInt%bank%offset",bbPeekInt ); - rtSym( "#PeekFloat%bank%offset",bbPeekFloat ); - rtSym( "PokeByte%bank%offset%value",bbPokeByte ); - rtSym( "PokeShort%bank%offset%value",bbPokeShort ); - rtSym( "PokeInt%bank%offset%value",bbPokeInt ); - rtSym( "PokeFloat%bank%offset#value",bbPokeFloat ); - rtSym( "%ReadBytes%bank%file%offset%count",bbReadBytes ); - rtSym( "%WriteBytes%bank%file%offset%count",bbWriteBytes ); - rtSym( "%CallDLL$dll_name$func_name%in_bank=0%out_bank=0",bbCallDLL ); +void bank_link(void (*rtSym)(const char*, void*)) +{ + rtSym("%CreateBank%size=0", bbCreateBank); + rtSym("FreeBank%bank", bbFreeBank); + rtSym("%BankSize%bank", bbBankSize); + rtSym("ResizeBank%bank%size", bbResizeBank); + rtSym("CopyBank%src_bank%src_offset%dest_bank%dest_offset%count", bbCopyBank); + rtSym("%PeekByte%bank%offset", bbPeekByte); + rtSym("%PeekShort%bank%offset", bbPeekShort); + rtSym("%PeekInt%bank%offset", bbPeekInt); + rtSym("#PeekFloat%bank%offset", bbPeekFloat); + rtSym("PokeByte%bank%offset%value", bbPokeByte); + rtSym("PokeShort%bank%offset%value", bbPokeShort); + rtSym("PokeInt%bank%offset%value", bbPokeInt); + rtSym("PokeFloat%bank%offset#value", bbPokeFloat); + rtSym("%ReadBytes%bank%file%offset%count", bbReadBytes); + rtSym("%WriteBytes%bank%file%offset%count", bbWriteBytes); + rtSym("%CallDLL$dll_name$func_name%in_bank=0%out_bank=0", bbCallDLL); } - - diff --git a/Runtime/lib/bbblitz3d.cpp b/Runtime/lib/bbblitz3d.cpp index f11f283..aab384d 100644 --- a/Runtime/lib/bbblitz3d.cpp +++ b/Runtime/lib/bbblitz3d.cpp @@ -1,38 +1,38 @@ #include "std.hpp" -#include "bbblitz3d.hpp" -#include "bbgraphics.hpp" #include "../blitz3d/blitz3d.hpp" -#include "../blitz3d/world.hpp" -#include "../blitz3d/texture.hpp" #include "../blitz3d/brush.hpp" #include "../blitz3d/camera.hpp" -#include "../blitz3d/sprite.hpp" #include "../blitz3d/meshmodel.hpp" +#include "../blitz3d/sprite.hpp" +#include "../blitz3d/texture.hpp" +#include "../blitz3d/world.hpp" +#include "bbblitz3d.hpp" +#include "bbgraphics.hpp" //#include "../blitz3d/loader_x.hpp" +#include "../blitz3d/cachedtexture.hpp" +#include "../blitz3d/listener.hpp" #include "../blitz3d/loader_3ds.hpp" #include "../blitz3d/loader_b3d.hpp" #include "../blitz3d/md2model.hpp" -#include "../blitz3d/q3bspmodel.hpp" #include "../blitz3d/meshutil.hpp" #include "../blitz3d/pivot.hpp" #include "../blitz3d/planemodel.hpp" +#include "../blitz3d/q3bspmodel.hpp" #include "../blitz3d/terrain.hpp" -#include "../blitz3d/listener.hpp" -#include "../blitz3d/cachedtexture.hpp" -gxScene *gx_scene; -extern gxFileSystem *gx_filesys; +gxScene* gx_scene; +extern gxFileSystem* gx_filesys; -static int tri_count; -static World *world; +static int tri_count; +static World* world; -static set brush_set; +static set brush_set; static set texture_set; -static set entity_set; +static set entity_set; -static Listener *listener; +static Listener* listener; static bool stats_mode; @@ -56,101 +56,150 @@ static Loader_B3D loader_b3d; static map loader_mat_map; #ifdef _DEBUG -static inline void debug3d() { - if (debug && !gx_scene) ThrowRuntimeException("3D Graphics mode not set"); +static inline void debug3d() +{ + if (debug && !gx_scene) + ThrowRuntimeException("3D Graphics mode not set"); } -static inline void debugTexture(Texture *t) { - if (debug && !texture_set.count(t)) ThrowRuntimeException("Texture does not exist"); +static inline void debugTexture(Texture* t) +{ + if (debug && !texture_set.count(t)) + ThrowRuntimeException("Texture does not exist"); } -static inline void debugBrush(Brush *b) { - if (debug && !brush_set.count(b)) ThrowRuntimeException("Brush does not exist"); +static inline void debugBrush(Brush* b) +{ + if (debug && !brush_set.count(b)) + ThrowRuntimeException("Brush does not exist"); } -static inline void debugEntity(Entity *e) { - if (debug && !entity_set.count(e)) ThrowRuntimeException("Entity does not exist"); +static inline void debugEntity(Entity* e) +{ + if (debug && !entity_set.count(e)) + ThrowRuntimeException("Entity does not exist"); } -static inline void debugParent(Entity *e) { +static inline void debugParent(Entity* e) +{ if (debug) { debug3d(); - if (e && !entity_set.count(e)) ThrowRuntimeException("Parent entity does not exist"); + if (e && !entity_set.count(e)) + ThrowRuntimeException("Parent entity does not exist"); } } -static inline void debugMesh(MeshModel *m) { +static inline void debugMesh(MeshModel* m) +{ if (debug) { - debugEntity(m); if (!m->getMeshModel()) ThrowRuntimeException("Entity is not a mesh"); + debugEntity(m); + if (!m->getMeshModel()) + ThrowRuntimeException("Entity is not a mesh"); } } -static inline void debugObject(Object *o) { +static inline void debugObject(Object* o) +{ if (debug) { - debugEntity(o); if (!o->getObject()) ThrowRuntimeException("Entity is not an object"); + debugEntity(o); + if (!o->getObject()) + ThrowRuntimeException("Entity is not an object"); } } -static inline void debugColl(Object *o, int index) { +static inline void debugColl(Object* o, int index) +{ if (debug) { debugObject(o); - if (index<1 || index>o->getCollisions().size()) ThrowRuntimeException("Collision index out of range"); + if (index < 1 || index > o->getCollisions().size()) + ThrowRuntimeException("Collision index out of range"); } } -static inline void debugCamera(Camera *c) { +static inline void debugCamera(Camera* c) +{ if (debug) { - debugEntity(c); if (!c->getCamera()) ThrowRuntimeException("Entity is not a camera"); + debugEntity(c); + if (!c->getCamera()) + ThrowRuntimeException("Entity is not a camera"); } } -static inline void debugLight(Light *l) { +static inline void debugLight(Light* l) +{ if (debug) { - debugEntity(l); if (!l->getLight()) ThrowRuntimeException("Entity is not a light"); + debugEntity(l); + if (!l->getLight()) + ThrowRuntimeException("Entity is not a light"); } } -static inline void debugModel(Model *m) { +static inline void debugModel(Model* m) +{ if (debug) { - debugEntity(m); if (!m->getModel()) ThrowRuntimeException("Entity is not a model"); + debugEntity(m); + if (!m->getModel()) + ThrowRuntimeException("Entity is not a model"); } } -static inline void debugSprite(Sprite *s) { +static inline void debugSprite(Sprite* s) +{ if (debug) { - debugModel(s); if (!s->getSprite()) ThrowRuntimeException("Entity is not a sprite"); + debugModel(s); + if (!s->getSprite()) + ThrowRuntimeException("Entity is not a sprite"); } } -static inline void debugMD2(MD2Model *m) { +static inline void debugMD2(MD2Model* m) +{ if (debug) { - debugModel(m); if (!m->getMD2Model()) ThrowRuntimeException("Entity is not an MD2 Model"); + debugModel(m); + if (!m->getMD2Model()) + ThrowRuntimeException("Entity is not an MD2 Model"); } } -static inline void debugBSP(Q3BSPModel *m) { +static inline void debugBSP(Q3BSPModel* m) +{ if (debug) { - debugModel(m); if (!m->getBSPModel()) ThrowRuntimeException("Entity is not a BSP Model"); + debugModel(m); + if (!m->getBSPModel()) + ThrowRuntimeException("Entity is not a BSP Model"); } } -static inline void debugTerrain(Terrain *t) { +static inline void debugTerrain(Terrain* t) +{ if (debug) { - debugModel(t); if (!t->getTerrain()) ThrowRuntimeException("Entity is not a terrain"); + debugModel(t); + if (!t->getTerrain()) + ThrowRuntimeException("Entity is not a terrain"); } } -static inline void debugSegs(int n) { +static inline void debugSegs(int n) +{ if (debug) { debug3d(); - if (n < 3 || n>50) ThrowRuntimeException("Illegal number of segments"); + if (n < 3 || n > 50) + ThrowRuntimeException("Illegal number of segments"); } } -static inline void debugVertex(Surface *s, int n) { +static inline void debugVertex(Surface* s, int n) +{ if (debug) { debug3d(); - if (n < 0 || n >= s->numVertices()) ThrowRuntimeException("Vertex index out of range"); + if (n < 0 || n >= s->numVertices()) + ThrowRuntimeException("Vertex index out of range"); } } -static inline void debugVertex(Surface *s, int n, int t) { +static inline void debugVertex(Surface* s, int n, int t) +{ if (debug) { debug3d(); - if (n < 0 || n >= s->numVertices()) ThrowRuntimeException("Vertex index out of range"); - if (t < 0 || t>1) ThrowRuntimeException("Texture coordinate set out of range"); + if (n < 0 || n >= s->numVertices()) + ThrowRuntimeException("Vertex index out of range"); + if (t < 0 || t > 1) + ThrowRuntimeException("Texture coordinate set out of range"); } } #endif -static Entity *loadEntity(string t, int hint) { - t = tolower(t); - int n = t.rfind("."); if (n == string::npos) return 0; - string ext = t.substr(n + 1); - MeshLoader *l; +static Entity* loadEntity(string t, int hint) +{ + t = tolower(t); + int n = t.rfind("."); + if (n == string::npos) + return 0; + string ext = t.substr(n + 1); + MeshLoader* l; if (ext == "x") { //l = &Loader_X; @@ -162,20 +211,21 @@ static Entity *loadEntity(string t, int hint) { return 0; } - const Transform &conv = loader_mat_map[ext]; + const Transform& conv = loader_mat_map[ext]; CachedTextureFactory::setPath(filenamepath(t)); - Entity *e = l->load(t, conv, hint); + Entity* e = l->load(t, conv, hint); CachedTextureFactory::setPath(""); return e; } -static void collapseMesh(MeshModel *mesh, Entity *e) { +static void collapseMesh(MeshModel* mesh, Entity* e) +{ while (e->GetChildren()) { collapseMesh(mesh, e->GetChildren()); } - if (Model *p = e->getModel()) { - if (MeshModel *t = p->getMeshModel()) { + if (Model* p = e->getModel()) { + if (MeshModel* t = p->getMeshModel()) { t->transform(e->GetWorldTransform()); mesh->add(*t); } @@ -183,38 +233,47 @@ static void collapseMesh(MeshModel *mesh, Entity *e) { delete e; } -static void insert(Entity *e) { +static void insert(Entity* e) +{ #ifdef _DEBUG - if (debug) entity_set.insert(e); + if (debug) + entity_set.insert(e); #endif e->SetVisible(true); e->SetEnabled(true); e->getObject()->reset(); - for (Entity *p = e->GetChildren(); p; p = p->GetSuccessor()) { + for (Entity* p = e->GetChildren(); p; p = p->GetSuccessor()) { insert(p); } } -static Entity *insertEntity(Entity *e, Entity *p) { +static Entity* insertEntity(Entity* e, Entity* p) +{ e->SetParent(p); insert(e); return e; } -static void erase(Entity *e) { - for (Entity *p = e->GetChildren(); p; p = p->GetSuccessor()) { +static void erase(Entity* e) +{ + for (Entity* p = e->GetChildren(); p; p = p->GetSuccessor()) { erase(p); } - if (e->getListener()) listener = 0; + if (e->getListener()) + listener = 0; #ifdef _DEBUG - if (debug) entity_set.erase(e); + if (debug) + entity_set.erase(e); #endif } -static Entity *findChild(Entity *e, const string &t) { - if (e->getName() == t) return e; - for (Entity *p = e->GetChildren(); p; p = p->GetSuccessor()) { - if (Entity *q = findChild(p, t)) return q; +static Entity* findChild(Entity* e, const string& t) +{ + if (e->getName() == t) + return e; + for (Entity* p = e->GetChildren(); p; p = p->GetSuccessor()) { + if (Entity* q = findChild(p, t)) + return q; } return 0; } @@ -222,77 +281,89 @@ static Entity *findChild(Entity *e, const string &t) { /////////////////////////// // GLOBAL WORLD COMMANDS // /////////////////////////// -void bbLoaderMatrix(BBStr *ext, float xx, float xy, float xz, float yx, float yy, float yz, float zx, float zy, float zz) { +void bbLoaderMatrix(BBStr* ext, float xx, float xy, float xz, float yx, float yy, float yz, float zx, float zy, + float zz) +{ loader_mat_map.erase(*ext); loader_mat_map[*ext] = Transform(Matrix(Vector(xx, xy, xz), Vector(yx, yy, yz), Vector(zx, zy, zz))); delete ext; } -int bbHWTexUnits() { +int bbHWTexUnits() +{ #ifdef _DEBUG debug3d(); #endif return gx_scene->hwTexUnits(); } -int bbGfxDriverCaps3D() { +int bbGfxDriverCaps3D() +{ #ifdef _DEBUG debug3d(); #endif return gx_scene->gfxDriverCaps3D(); } -void bbHWMultiTex(int enable) { +void bbHWMultiTex(int enable) +{ #ifdef _DEBUG debug3d(); #endif gx_scene->setHWMultiTex(!!enable); } -void bbWBuffer(int enable) { +void bbWBuffer(int enable) +{ #ifdef _DEBUG debug3d(); #endif gx_scene->setWBuffer(!!enable); } -void bbDither(int enable) { +void bbDither(int enable) +{ #ifdef _DEBUG debug3d(); #endif gx_scene->setDither(!!enable); } -void bbAntiAlias(int enable) { +void bbAntiAlias(int enable) +{ #ifdef _DEBUG debug3d(); #endif gx_scene->setAntialias(!!enable); } -void bbWireFrame(int enable) { +void bbWireFrame(int enable) +{ #ifdef _DEBUG debug3d(); #endif gx_scene->setWireframe(!!enable); } -void bbAmbientLight(float r, float g, float b) { +void bbAmbientLight(float r, float g, float b) +{ #ifdef _DEBUG debug3d(); #endif - Vector t(r*s_colorToFloat, g*s_colorToFloat, b*s_colorToFloat); + Vector t(r * s_colorToFloat, g * s_colorToFloat, b * s_colorToFloat); gx_scene->setAmbient(&(t.x)); } -void bbClearCollisions() { +void bbClearCollisions() +{ #ifdef _DEBUG debug3d(); #endif world->clearCollisions(); } -void bbCollisions(int src_type, int dest_type, int method, int response) { +void bbCollisions(int src_type, int dest_type, int method, int response) +{ #ifdef _DEBUG debug3d(); #endif @@ -301,45 +372,48 @@ void bbCollisions(int src_type, int dest_type, int method, int response) { static int update_ms; -void bbUpdateWorld(float elapsed) { +void bbUpdateWorld(float elapsed) +{ #ifdef _DEBUG debug3d(); #endif - #ifndef BETA +#ifndef BETA world->update(elapsed); return; - #endif +#endif update_ms = gx_runtime->getMilliSecs(); world->update(elapsed); update_ms = gx_runtime->getMilliSecs() - update_ms; } -void bbCaptureWorld() { +void bbCaptureWorld() +{ #ifdef _DEBUG debug3d(); #endif world->capture(); } -void bbRenderWorld(float tween) { +void bbRenderWorld(float tween) +{ #ifdef _DEBUG debug3d(); #endif - #ifndef BETA +#ifndef BETA tri_count = gx_scene->getTrianglesDrawn(); world->render(tween); tri_count = gx_scene->getTrianglesDrawn() - tri_count; return; - #else - int tris = gx_scene->getTrianglesDrawn(); +#else + int tris = gx_scene->getTrianglesDrawn(); int render_ms = gx_runtime->getMilliSecs(); world->render(tween); render_ms = gx_runtime->getMilliSecs() - render_ms; - extern int bbKeyHit(int); + extern int bbKeyHit(int); extern void bbDelay(int); bbDelay(0); if (bbKeyHit(0x57)) { @@ -347,38 +421,45 @@ void bbRenderWorld(float tween) { } if (bbKeyHit(0x58)) { static int n; - string t = "screenshot" + itoa(++n) + ".bmp"; + string t = "screenshot" + itoa(++n) + ".bmp"; bbSaveBuffer(bbBackBuffer(), new BBStr(t)); } - if (!stats_mode) return; + if (!stats_mode) + return; tris = gx_scene->getTrianglesDrawn() - tris; static int time; - int frame_ms = gx_runtime->getMilliSecs() - time; + int frame_ms = gx_runtime->getMilliSecs() - time; time += frame_ms; int fps = frame_ms ? 1000 / frame_ms : 1000; int ups = update_ms ? 1000 / update_ms : 1000; int rps = render_ms ? 1000 / render_ms : 1000; - string t_fps = "000" + itoa(fps); t_fps = t_fps.substr(t_fps.size() - 4); - string t_ups = "000" + itoa(ups); t_ups = t_ups.substr(t_ups.size() - 4); - string t_rps = "000" + itoa(rps); t_rps = t_rps.substr(t_rps.size() - 4); - string t_tris = "00000" + itoa(tris); t_tris = t_tris.substr(t_tris.size() - 6); + string t_fps = "000" + itoa(fps); + t_fps = t_fps.substr(t_fps.size() - 4); + string t_ups = "000" + itoa(ups); + t_ups = t_ups.substr(t_ups.size() - 4); + string t_rps = "000" + itoa(rps); + t_rps = t_rps.substr(t_rps.size() - 4); + string t_tris = "00000" + itoa(tris); + t_tris = t_tris.substr(t_tris.size() - 6); string t = "FPS:" + t_fps + " UPS:" + t_ups + " RPS:" + t_rps + " TRIS:" + t_tris; bbText(0, bbGraphicsHeight() - bbFontHeight(), new BBStr(t), 0, 0); - #endif +#endif } -int bbTrisRendered() { +int bbTrisRendered() +{ return tri_count; } -float bbStats3D(int n) { +float bbStats3D(int n) +{ return stats3d[n]; } @@ -388,21 +469,12 @@ float bbStats3D(int n) { //Note: modify canvas->backup() to NOT release backup image! // -Texture * bbLoadTexture(BBStr *file, int flags) { +Texture* bbLoadTexture(BBStr* file, int flags) +{ #ifdef _DEBUG debug3d(); #endif - Texture *t = new Texture(*file, flags); delete file; - if (!t->getCanvas(0)) { delete t; return 0; } - texture_set.insert(t); - return t; -} - -Texture * bbLoadAnimTexture(BBStr *file, int flags, int w, int h, int first, int cnt) { -#ifdef _DEBUG - debug3d(); -#endif - Texture *t = new Texture(*file, flags, w, h, first, cnt); + Texture* t = new Texture(*file, flags); delete file; if (!t->getCanvas(0)) { delete t; @@ -412,7 +484,23 @@ Texture * bbLoadAnimTexture(BBStr *file, int flags, int w, int h, int first, in return t; } -Texture * bbCreateTexture(int w, int h, int flags, int frames) { +Texture* bbLoadAnimTexture(BBStr* file, int flags, int w, int h, int first, int cnt) +{ +#ifdef _DEBUG + debug3d(); +#endif + Texture* t = new Texture(*file, flags, w, h, first, cnt); + delete file; + if (!t->getCanvas(0)) { + delete t; + return 0; + } + texture_set.insert(t); + return t; +} + +Texture* bbCreateTexture(int w, int h, int flags, int frames) +{ #ifdef _DEBUG if (debug) { debug3d(); @@ -421,101 +509,116 @@ Texture * bbCreateTexture(int w, int h, int flags, int frames) { } } #endif - Texture *t = new Texture(w, h, flags, frames); + Texture* t = new Texture(w, h, flags, frames); texture_set.insert(t); return t; } -void bbFreeTexture(Texture *t) { - if (!t) return; +void bbFreeTexture(Texture* t) +{ + if (!t) + return; #ifdef _DEBUG debugTexture(t); #endif - if (texture_set.erase(t)) delete t; + if (texture_set.erase(t)) + delete t; } -void bbTextureBlend(Texture *t, int blend) { +void bbTextureBlend(Texture* t, int blend) +{ #ifdef _DEBUG debugTexture(t); #endif t->setBlend(blend); } -void bbTextureCoords(Texture *t, int flags) { +void bbTextureCoords(Texture* t, int flags) +{ #ifdef _DEBUG debugTexture(t); #endif t->setFlags(flags); } -void bbScaleTexture(Texture *t, float u_scale, float v_scale) { +void bbScaleTexture(Texture* t, float u_scale, float v_scale) +{ #ifdef _DEBUG debugTexture(t); #endif t->setScale(1 / u_scale, 1 / v_scale); } -void bbRotateTexture(Texture *t, float angle) { +void bbRotateTexture(Texture* t, float angle) +{ #ifdef _DEBUG debugTexture(t); #endif - t->setRotation(-angle*s_degreesToRadians); + t->setRotation(-angle * s_degreesToRadians); } -void bbPositionTexture(Texture *t, float u_pos, float v_pos) { +void bbPositionTexture(Texture* t, float u_pos, float v_pos) +{ #ifdef _DEBUG debugTexture(t); #endif t->setPosition(-u_pos, -v_pos); } -int bbTextureWidth(Texture *t) { +int bbTextureWidth(Texture* t) +{ #ifdef _DEBUG debugTexture(t); #endif return t->getCanvas(0)->getWidth(); } -int bbTextureHeight(Texture *t) { +int bbTextureHeight(Texture* t) +{ #ifdef _DEBUG debugTexture(t); #endif return t->getCanvas(0)->getHeight(); } -BBStr *bbTextureName(Texture *t) { +BBStr* bbTextureName(Texture* t) +{ #ifdef _DEBUG debugTexture(t); #endif - CachedTextureFactory *c = t->getCachedTexture(); + CachedTextureFactory* c = t->getCachedTexture(); return c ? new BBStr(c->getName().c_str()) : new BBStr(""); } -void bbSetCubeFace(Texture *t, int face) { +void bbSetCubeFace(Texture* t, int face) +{ #ifdef _DEBUG debugTexture(t); #endif - if (gxCanvas *c = t->getCanvas(0)) { + if (gxCanvas* c = t->getCanvas(0)) { c->setCubeFace(face); } } -void bbSetCubeMode(Texture *t, int mode) { +void bbSetCubeMode(Texture* t, int mode) +{ #ifdef _DEBUG debugTexture(t); #endif - if (gxCanvas *c = t->getCanvas(0)) { + if (gxCanvas* c = t->getCanvas(0)) { c->setCubeMode(mode); } } -gxCanvas *bbTextureBuffer(Texture *t, int frame) { +gxCanvas* bbTextureBuffer(Texture* t, int frame) +{ //v1.04 #ifdef _DEBUG debugTexture(t); #endif - if (gxCanvas *c = t->getCanvas(frame)) { - if (c->getDepth()) return c; + if (gxCanvas* c = t->getCanvas(frame)) { + if (c->getDepth()) + return c; } return 0; /* @@ -527,14 +630,16 @@ gxCanvas *bbTextureBuffer(Texture *t, int frame) { */ } -void bbClearTextureFilters() { +void bbClearTextureFilters() +{ #ifdef _DEBUG debug3d(); #endif Texture::clearFilters(); } -void bbTextureFilter(BBStr *t, int flags) { +void bbTextureFilter(BBStr* t, int flags) +{ #ifdef _DEBUG debug3d(); #endif @@ -545,59 +650,71 @@ void bbTextureFilter(BBStr *t, int flags) { //////////////////// // BRUSH COMMANDS // //////////////////// -Brush * bbCreateBrush(float r, float g, float b) { +Brush* bbCreateBrush(float r, float g, float b) +{ #ifdef _DEBUG debug3d(); #endif - Brush *br = new Brush(); - br->setColor(Vector(r*s_colorToFloat, g*s_colorToFloat, b*s_colorToFloat)); + Brush* br = new Brush(); + br->setColor(Vector(r * s_colorToFloat, g * s_colorToFloat, b * s_colorToFloat)); brush_set.insert(br); return br; } -Brush * bbLoadBrush(BBStr *file, int flags, float u_scale, float v_scale) { +Brush* bbLoadBrush(BBStr* file, int flags, float u_scale, float v_scale) +{ #ifdef _DEBUG debug3d(); #endif Texture t(*file, flags); - delete file; if (!t.getCanvas(0)) return 0; - if (u_scale != 1 || v_scale != 1) t.setScale(1 / u_scale, 1 / v_scale); - Brush *br = bbCreateBrush(255, 255, 255); + delete file; + if (!t.getCanvas(0)) + return 0; + if (u_scale != 1 || v_scale != 1) + t.setScale(1 / u_scale, 1 / v_scale); + Brush* br = bbCreateBrush(255, 255, 255); br->setTexture(0, t, 0); delete file; return br; } -void bbFreeBrush(Brush *b) { - if (!b) return; +void bbFreeBrush(Brush* b) +{ + if (!b) + return; #ifdef _DEBUG debugBrush(b); #endif - if (brush_set.erase(b)) delete b; + if (brush_set.erase(b)) + delete b; } -void bbBrushColor(Brush *br, float r, float g, float b) { +void bbBrushColor(Brush* br, float r, float g, float b) +{ #ifdef _DEBUG debugBrush(br); #endif - br->setColor(Vector(r*s_colorToFloat, g*s_colorToFloat, b*s_colorToFloat)); + br->setColor(Vector(r * s_colorToFloat, g * s_colorToFloat, b * s_colorToFloat)); } -void bbBrushAlpha(Brush *b, float alpha) { +void bbBrushAlpha(Brush* b, float alpha) +{ #ifdef _DEBUG debugBrush(b); #endif b->setAlpha(alpha); } -void bbBrushShininess(Brush *b, float n) { +void bbBrushShininess(Brush* b, float n) +{ #ifdef _DEBUG debugBrush(b); #endif b->setShininess(n); } -void bbBrushTexture(Brush *b, Texture *t, int frame, int index) { +void bbBrushTexture(Brush* b, Texture* t, int frame, int index) +{ #ifdef _DEBUG debugBrush(b); debugTexture(t); @@ -605,23 +722,26 @@ void bbBrushTexture(Brush *b, Texture *t, int frame, int index) { b->setTexture(index, *t, frame); } -Texture *bbGetBrushTexture(Brush *b, int index) { +Texture* bbGetBrushTexture(Brush* b, int index) +{ #ifdef _DEBUG debugBrush(b); #endif - Texture *tex = new Texture(b->getTexture(index)); + Texture* tex = new Texture(b->getTexture(index)); texture_set.insert(tex); return tex; } -void bbBrushBlend(Brush *b, int blend) { +void bbBrushBlend(Brush* b, int blend) +{ #ifdef _DEBUG debugBrush(b); #endif b->setBlend(blend); } -void bbBrushFX(Brush *b, int fx) { +void bbBrushFX(Brush* b, int fx) +{ #ifdef _DEBUG debugBrush(b); #endif @@ -631,116 +751,142 @@ void bbBrushFX(Brush *b, int fx) { /////////////////// // MESH COMMANDS // /////////////////// -Entity * bbCreateMesh(Entity *p) { +Entity* bbCreateMesh(Entity* p) +{ #ifdef _DEBUG debugParent(p); #endif - MeshModel *m = new MeshModel(); + MeshModel* m = new MeshModel(); return insertEntity(m, p); } -Entity * bbLoadMesh(BBStr *f, Entity *p) { +Entity* bbLoadMesh(BBStr* f, Entity* p) +{ #ifdef _DEBUG debugParent(p); #endif - Entity *e = loadEntity(f->c_str(), MeshLoader::HINT_COLLAPSE); + Entity* e = loadEntity(f->c_str(), MeshLoader::HINT_COLLAPSE); delete f; - if (!e) return 0; - MeshModel *m = new MeshModel(); + if (!e) + return 0; + MeshModel* m = new MeshModel(); collapseMesh(m, e); return insertEntity(m, p); } -Entity * bbLoadAnimMesh(BBStr *f, Entity *p) { +Entity* bbLoadAnimMesh(BBStr* f, Entity* p) +{ #ifdef _DEBUG debugParent(p); #endif - Entity *e = loadEntity(f->c_str(), 0); + Entity* e = loadEntity(f->c_str(), 0); delete f; - if (!e) return 0; - if (Animator *anim = e->getObject()->getAnimator()) { + if (!e) + return 0; + if (Animator* anim = e->getObject()->getAnimator()) { anim->animate(1, 0, 0, 0); } return insertEntity(e, p); } -Entity * bbCreateCube(Entity *p) { +Entity* bbCreateCube(Entity* p) +{ #ifdef _DEBUG debugParent(p); #endif - Entity *e = MeshUtil::createCube(Brush()); + Entity* e = MeshUtil::createCube(Brush()); return insertEntity(e, p); } -Entity * bbCreateSphere(int segs, Entity *p) { +Entity* bbCreateSphere(int segs, Entity* p) +{ #ifdef _DEBUG - if (debug) { debugParent(p); if (segs < 2 || segs>100) ThrowRuntimeException("Illegal number of segments"); } + if (debug) { + debugParent(p); + if (segs < 2 || segs > 100) + ThrowRuntimeException("Illegal number of segments"); + } #endif - Entity *e = MeshUtil::createSphere(Brush(), segs); + Entity* e = MeshUtil::createSphere(Brush(), segs); return insertEntity(e, p); } -Entity * bbCreateCylinder(int segs, int solid, Entity *p) { +Entity* bbCreateCylinder(int segs, int solid, Entity* p) +{ #ifdef _DEBUG - if (debug) { debugParent(p); if (segs < 3 || segs>100) ThrowRuntimeException("Illegal number of segments"); } + if (debug) { + debugParent(p); + if (segs < 3 || segs > 100) + ThrowRuntimeException("Illegal number of segments"); + } #endif - Entity *e = MeshUtil::createCylinder(Brush(), segs, !!solid); + Entity* e = MeshUtil::createCylinder(Brush(), segs, !!solid); return insertEntity(e, p); } -Entity * bbCreateCone(int segs, int solid, Entity *p) { +Entity* bbCreateCone(int segs, int solid, Entity* p) +{ #ifdef _DEBUG - if (debug) { debugParent(p); if (segs < 3 || segs>100) ThrowRuntimeException("Illegal number of segments"); } + if (debug) { + debugParent(p); + if (segs < 3 || segs > 100) + ThrowRuntimeException("Illegal number of segments"); + } #endif - Entity *e = MeshUtil::createCone(Brush(), segs, !!solid); + Entity* e = MeshUtil::createCone(Brush(), segs, !!solid); return insertEntity(e, p); } -Entity * bbCopyMesh(MeshModel *m, Entity *p) { +Entity* bbCopyMesh(MeshModel* m, Entity* p) +{ #ifdef _DEBUG debugMesh(m); debugParent(p); #endif - MeshModel *t = new MeshModel(); + MeshModel* t = new MeshModel(); t->add(*m); return insertEntity(t, p); } -void bbScaleMesh(MeshModel *m, float x, float y, float z) { +void bbScaleMesh(MeshModel* m, float x, float y, float z) +{ #ifdef _DEBUG debugMesh(m); #endif m->transform(scaleMatrix(x, y, z)); } -void bbRotateMesh(MeshModel *m, float x, float y, float z) { +void bbRotateMesh(MeshModel* m, float x, float y, float z) +{ #ifdef _DEBUG debugMesh(m); #endif - m->transform(rotationMatrix(x*s_degreesToRadians, y*s_degreesToRadians, z*s_degreesToRadians)); + m->transform(rotationMatrix(x * s_degreesToRadians, y * s_degreesToRadians, z * s_degreesToRadians)); } -void bbPositionMesh(MeshModel *m, float x, float y, float z) { +void bbPositionMesh(MeshModel* m, float x, float y, float z) +{ #ifdef _DEBUG debugMesh(m); #endif m->transform(Vector(x, y, z)); } -void bbFitMesh(MeshModel *m, float x, float y, float z, float w, float h, float d, int uniform) { +void bbFitMesh(MeshModel* m, float x, float y, float z, float w, float h, float d, int uniform) +{ #ifdef _DEBUG debugMesh(m); #endif Box box(Vector(x, y, z)); box.update(Vector(x + w, y + h, z + d)); - const Box &curr_box = m->getBox(); - float x_scale = box.width() / curr_box.width(); - float y_scale = box.height() / curr_box.height(); - float z_scale = box.depth() / curr_box.depth(); - Transform t; + const Box& curr_box = m->getBox(); + float x_scale = box.width() / curr_box.width(); + float y_scale = box.height() / curr_box.height(); + float z_scale = box.depth() / curr_box.depth(); + Transform t; if (uniform) { if (x_scale < y_scale && x_scale < z_scale) { y_scale = z_scale = x_scale; @@ -753,365 +899,445 @@ void bbFitMesh(MeshModel *m, float x, float y, float z, float w, float h, float t.m.i.x = x_scale; t.m.j.y = y_scale; t.m.k.z = z_scale; - t.v = box.centre() - t.m * curr_box.centre(); + t.v = box.centre() - t.m * curr_box.centre(); m->transform(t); } -void bbFlipMesh(MeshModel *m) { +void bbFlipMesh(MeshModel* m) +{ #ifdef _DEBUG debugMesh(m); #endif m->flipTriangles(); } -void bbPaintMesh(MeshModel *m, Brush *b) { +void bbPaintMesh(MeshModel* m, Brush* b) +{ #ifdef _DEBUG - if (debug) { debugMesh(m); debugBrush(b); } + if (debug) { + debugMesh(m); + debugBrush(b); + } #endif m->paint(*b); } -void bbAddMesh(MeshModel *src, MeshModel *dest) { +void bbAddMesh(MeshModel* src, MeshModel* dest) +{ #ifdef _DEBUG if (debug) { - debugMesh(src); debugMesh(dest); - if (src == dest) ThrowRuntimeException("A mesh cannot be added to itself"); + debugMesh(src); + debugMesh(dest); + if (src == dest) + ThrowRuntimeException("A mesh cannot be added to itself"); } #endif dest->add(*src); } -void bbUpdateNormals(MeshModel *m) { +void bbUpdateNormals(MeshModel* m) +{ #ifdef _DEBUG debugMesh(m); #endif m->updateNormals(); } -void bbLightMesh(MeshModel *m, float r, float g, float b, float range, float x, float y, float z) { +void bbLightMesh(MeshModel* m, float r, float g, float b, float range, float x, float y, float z) +{ #ifdef _DEBUG debugMesh(m); #endif - MeshUtil::lightMesh(m, Vector(x, y, z), Vector(r*s_colorToFloat, g*s_colorToFloat, b*s_colorToFloat), range); + MeshUtil::lightMesh(m, Vector(x, y, z), Vector(r * s_colorToFloat, g * s_colorToFloat, b * s_colorToFloat), range); } -float bbMeshWidth(MeshModel *m) { +float bbMeshWidth(MeshModel* m) +{ #ifdef _DEBUG debugMesh(m); #endif return m->getBox().width(); } -float bbMeshHeight(MeshModel *m) { +float bbMeshHeight(MeshModel* m) +{ #ifdef _DEBUG debugMesh(m); #endif return m->getBox().height(); } -float bbMeshDepth(MeshModel *m) { +float bbMeshDepth(MeshModel* m) +{ #ifdef _DEBUG debugMesh(m); #endif return m->getBox().depth(); } -int bbMeshesIntersect(MeshModel *a, MeshModel *b) { +int bbMeshesIntersect(MeshModel* a, MeshModel* b) +{ #ifdef _DEBUG - if (debug) { debugMesh(a); debugMesh(b); } + if (debug) { + debugMesh(a); + debugMesh(b); + } #endif return a->intersects(*b); } -int bbCountSurfaces(MeshModel *m) { +int bbCountSurfaces(MeshModel* m) +{ #ifdef _DEBUG debugMesh(m); #endif return m->getSurfaces().size(); } -Surface * bbGetSurface(MeshModel *m, int index) { - #ifdef _DEBUG +Surface* bbGetSurface(MeshModel* m, int index) +{ +#ifdef _DEBUG if (debug) { debugMesh(m); - if ((size_t)index<1 || index>m->getSurfaces().size()) { + if ((size_t)index < 1 || index > m->getSurfaces().size()) { ThrowRuntimeException("Surface Index out of range"); } } - #endif +#endif return m->getSurfaces()[index - 1]; } -void bbMeshCullBox(MeshModel *m, float x, float y, float z, float width, float height, float depth) { +void bbMeshCullBox(MeshModel* m, float x, float y, float z, float width, float height, float depth) +{ #ifdef _DEBUG debugMesh(m); #endif m->setCullBox(Box(Vector(x, y, z), Vector(x + width, y + height, z + depth))); } - ////////////////////// // SURFACE COMMANDS // ////////////////////// -Surface * bbFindSurface(MeshModel *m, Brush *b) { +Surface* bbFindSurface(MeshModel* m, Brush* b) +{ #ifdef _DEBUG - if (debug) { debugMesh(m); debugBrush(b); } + if (debug) { + debugMesh(m); + debugBrush(b); + } #endif return m->findSurface(*b); } -Surface * bbCreateSurface(MeshModel *m, Brush *b) { +Surface* bbCreateSurface(MeshModel* m, Brush* b) +{ #ifdef _DEBUG - if (debug) { debugMesh(m); if (b) debugBrush(b); } + if (debug) { + debugMesh(m); + if (b) + debugBrush(b); + } #endif - Surface *s = b ? m->createSurface(*b) : m->createSurface(Brush()); + Surface* s = b ? m->createSurface(*b) : m->createSurface(Brush()); return s; } -Brush *bbGetSurfaceBrush(Surface *s) { - Brush *br = new Brush(s->getBrush()); +Brush* bbGetSurfaceBrush(Surface* s) +{ + Brush* br = new Brush(s->getBrush()); brush_set.insert(br); return br; } -Brush *bbGetEntityBrush(Model *m) { +Brush* bbGetEntityBrush(Model* m) +{ #ifdef _DEBUG debugModel(m); #endif - Brush *br = new Brush(m->getBrush()); + Brush* br = new Brush(m->getBrush()); brush_set.insert(br); return br; } -void bbClearSurface(Surface *s, int verts, int tris) { +void bbClearSurface(Surface* s, int verts, int tris) +{ s->clear(!!verts, !!tris); } -void bbPaintSurface(Surface *s, Brush *b) { +void bbPaintSurface(Surface* s, Brush* b) +{ #ifdef _DEBUG debugBrush(b); #endif s->setBrush(*b); } -int bbAddVertex(Surface *s, float x, float y, float z, float tu, float tv, float tw) { +int bbAddVertex(Surface* s, float x, float y, float z, float tu, float tv, float tw) +{ Surface::Vertex v; - v.coords = Vector(x, y, z); - v.color = 0xffffffff; + v.coords = Vector(x, y, z); + v.color = 0xffffffff; v.tex_coords[0][0] = v.tex_coords[1][0] = tu; v.tex_coords[0][1] = v.tex_coords[1][1] = tv; s->addVertex(v); return s->numVertices() - 1; } -int bbAddTriangle(Surface *s, int v0, int v1, int v2) { +int bbAddTriangle(Surface* s, int v0, int v1, int v2) +{ Surface::Triangle t; - t.verts[0] = v0; t.verts[1] = v1; t.verts[2] = v2; + t.verts[0] = v0; + t.verts[1] = v1; + t.verts[2] = v2; s->addTriangle(t); return s->numTriangles() - 1; } -void bbVertexCoords(Surface *s, int n, float x, float y, float z) { +void bbVertexCoords(Surface* s, int n, float x, float y, float z) +{ s->setCoords(n, Vector(x, y, z)); } -void bbVertexNormal(Surface *s, int n, float x, float y, float z) { +void bbVertexNormal(Surface* s, int n, float x, float y, float z) +{ s->setNormal(n, Vector(x, y, z)); } -void bbVertexColor(Surface *s, int n, float r, float g, float b, float a) { - if (r < 0)r = 0; else if (r > 255)r = 255; - if (g < 0)g = 0; else if (g > 255)g = 255; - if (b < 0)b = 0; else if (b > 255)b = 255; - a *= 255; if (a < 0)a = 0; else if (a > 255)a = 255; +void bbVertexColor(Surface* s, int n, float r, float g, float b, float a) +{ + if (r < 0) + r = 0; + else if (r > 255) + r = 255; + if (g < 0) + g = 0; + else if (g > 255) + g = 255; + if (b < 0) + b = 0; + else if (b > 255) + b = 255; + a *= 255; + if (a < 0) + a = 0; + else if (a > 255) + a = 255; s->setColor(n, (int(a) << 24) | (int(r) << 16) | (int(g) << 8) | int(b)); } -void bbVertexTexCoords(Surface *s, int n, float u, float v, float w, int set) { +void bbVertexTexCoords(Surface* s, int n, float u, float v, float w, int set) +{ s->setTexCoords(n, Vector(u, v, w), set); } -int bbCountVertices(Surface *s) { +int bbCountVertices(Surface* s) +{ return s->numVertices(); } -int bbCountTriangles(Surface *s) { +int bbCountTriangles(Surface* s) +{ return s->numTriangles(); } -float bbVertexX(Surface *s, int n) { +float bbVertexX(Surface* s, int n) +{ #ifdef _DEBUG debugVertex(s, n); #endif return s->getVertex(n).coords.x; } -float bbVertexY(Surface *s, int n) { +float bbVertexY(Surface* s, int n) +{ #ifdef _DEBUG debugVertex(s, n); #endif return s->getVertex(n).coords.y; } -float bbVertexZ(Surface *s, int n) { +float bbVertexZ(Surface* s, int n) +{ #ifdef _DEBUG debugVertex(s, n); #endif return s->getVertex(n).coords.z; } -float bbVertexNX(Surface *s, int n) { +float bbVertexNX(Surface* s, int n) +{ #ifdef _DEBUG debugVertex(s, n); #endif return s->getVertex(n).normal.x; } -float bbVertexNY(Surface *s, int n) { +float bbVertexNY(Surface* s, int n) +{ #ifdef _DEBUG debugVertex(s, n); #endif return s->getVertex(n).normal.y; } -float bbVertexNZ(Surface *s, int n) { +float bbVertexNZ(Surface* s, int n) +{ #ifdef _DEBUG debugVertex(s, n); #endif return s->getVertex(n).normal.z; } -float bbVertexRed(Surface *s, int n) { +float bbVertexRed(Surface* s, int n) +{ #ifdef _DEBUG debugVertex(s, n); #endif return (float)((s->getVertex(n).color & 0xff0000) >> 16); } -float bbVertexGreen(Surface *s, int n) { +float bbVertexGreen(Surface* s, int n) +{ #ifdef _DEBUG debugVertex(s, n); #endif return (float)((s->getVertex(n).color & 0xff00) >> 8); } -float bbVertexBlue(Surface *s, int n) { +float bbVertexBlue(Surface* s, int n) +{ #ifdef _DEBUG debugVertex(s, n); #endif return (float)(s->getVertex(n).color & 0xff); } -float bbVertexAlpha(Surface *s, int n) { +float bbVertexAlpha(Surface* s, int n) +{ #ifdef _DEBUG debugVertex(s, n); #endif return (float)(((s->getVertex(n).color & 0xff000000) >> 24) / 255.0f); } -float bbVertexU(Surface *s, int n, int t) { +float bbVertexU(Surface* s, int n, int t) +{ #ifdef _DEBUG debugVertex(s, n, t); #endif return s->getVertex(n).tex_coords[t][0]; } -float bbVertexV(Surface *s, int n, int t) { +float bbVertexV(Surface* s, int n, int t) +{ #ifdef _DEBUG debugVertex(s, n, t); #endif return s->getVertex(n).tex_coords[t][1]; } -float bbVertexW(Surface *s, int n, int t) { +float bbVertexW(Surface* s, int n, int t) +{ #ifdef _DEBUG debugVertex(s, n, t); #endif return 1; } -int bbTriangleVertex(Surface *s, int n, int v) { +int bbTriangleVertex(Surface* s, int n, int v) +{ return s->getTriangle(n).verts[v]; } ///////////////////// // CAMERA COMMANDS // ///////////////////// -Entity * bbCreateCamera(Entity *p) { +Entity* bbCreateCamera(Entity* p) +{ #ifdef _DEBUG debugParent(p); #endif int x, y, w, h; gx_canvas->getViewport(&x, &y, &w, &h); - Camera *c = new Camera(); + Camera* c = new Camera(); c->setViewport(x, y, w, h); return insertEntity(c, p); } -void bbCameraZoom(Camera *c, float zoom) { +void bbCameraZoom(Camera* c, float zoom) +{ #ifdef _DEBUG debugCamera(c); #endif c->setZoom(zoom); } -void bbCameraRange(Camera *c, float nr, float fr) { +void bbCameraRange(Camera* c, float nr, float fr) +{ #ifdef _DEBUG debugCamera(c); #endif c->setRange(nr, fr); } -void bbCameraClsColor(Camera *c, float r, float g, float b) { +void bbCameraClsColor(Camera* c, float r, float g, float b) +{ #ifdef _DEBUG debugCamera(c); #endif - c->setClsColor(Vector(r*s_colorToFloat, g*s_colorToFloat, b*s_colorToFloat)); + c->setClsColor(Vector(r * s_colorToFloat, g * s_colorToFloat, b * s_colorToFloat)); } -void bbCameraClsMode(Camera *c, int cls_color, int cls_zbuffer) { +void bbCameraClsMode(Camera* c, int cls_color, int cls_zbuffer) +{ #ifdef _DEBUG debugCamera(c); #endif c->setClsMode(cls_color ? true : false, cls_zbuffer ? true : false); } -void bbCameraProjMode(Camera *c, int mode) { +void bbCameraProjMode(Camera* c, int mode) +{ #ifdef _DEBUG debugCamera(c); #endif c->setProjMode(mode); } -void bbCameraViewport(Camera *c, int x, int y, int w, int h) { +void bbCameraViewport(Camera* c, int x, int y, int w, int h) +{ #ifdef _DEBUG debugCamera(c); #endif c->setViewport(x, y, w, h); } -void bbCameraFogRange(Camera *c, float nr, float fr) { +void bbCameraFogRange(Camera* c, float nr, float fr) +{ #ifdef _DEBUG debugCamera(c); #endif c->setFogRange(nr, fr); } -void bbCameraFogColor(Camera *c, float r, float g, float b) { +void bbCameraFogColor(Camera* c, float r, float g, float b) +{ #ifdef _DEBUG debugCamera(c); #endif - c->setFogColor(Vector(r*s_colorToFloat, g*s_colorToFloat, b*s_colorToFloat)); + c->setFogColor(Vector(r * s_colorToFloat, g * s_colorToFloat, b * s_colorToFloat)); } -void bbCameraFogMode(Camera *c, int mode) { +void bbCameraFogMode(Camera* c, int mode) +{ #ifdef _DEBUG debugCamera(c); #endif c->setFogMode(mode); } -int bbCameraProject(Camera *c, float x, float y, float z) { +int bbCameraProject(Camera* c, float x, float y, float z) +{ #ifdef _DEBUG debugCamera(c); #endif - Vector v = -c->GetWorldTransform()*Vector(x, y, z); - const Frustum &f = c->getFrustum(); + Vector v = -c->GetWorldTransform() * Vector(x, y, z); + const Frustum& f = c->getFrustum(); if (c->getProjMode() == Camera::PROJ_ORTHO) { int vp_x, vp_y, vp_w, vp_h; c->getViewport(&vp_x, &vp_y, &vp_w, &vp_h); - float nr = c->getFrustumNear(); - float fr = c->getFrustumFar(); + float nr = c->getFrustumNear(); + float fr = c->getFrustumFar(); float nr_w = c->getFrustumWidth(); float nr_h = c->getFrustumHeight(); - projected = Vector((v.x / nr_w + .5f)*vp_w, (.5f - v.y / nr_h)*vp_h, nr); + projected = Vector((v.x / nr_w + .5f) * vp_w, (.5f - v.y / nr_h) * vp_h, nr); return 1; } if (v.z > 0) { @@ -1119,13 +1345,11 @@ int bbCameraProject(Camera *c, float x, float y, float z) { if (v.z <= fr) { int vp_x, vp_y, vp_w, vp_h; c->getViewport(&vp_x, &vp_y, &vp_w, &vp_h); - float nr = c->getFrustumNear(); - float fr = c->getFrustumFar(); + float nr = c->getFrustumNear(); + float fr = c->getFrustumFar(); float nr_w = c->getFrustumWidth(); float nr_h = c->getFrustumHeight(); - projected = Vector( - (v.x*nr / v.z / nr_w + .5f)*vp_w, - (.5f - v.y*nr / v.z / nr_h)*vp_h, nr); + projected = Vector((v.x * nr / v.z / nr_w + .5f) * vp_w, (.5f - v.y * nr / v.z / nr_h) * vp_h, nr); return 1; } } @@ -1133,50 +1357,57 @@ int bbCameraProject(Camera *c, float x, float y, float z) { return 0; } -float bbProjectedX() { +float bbProjectedX() +{ return projected.x; } -float bbProjectedY() { +float bbProjectedY() +{ return projected.y; } -float bbProjectedZ() { +float bbProjectedZ() +{ return projected.z; } -static Object *doPick(const Line &l, float radius) { +static Object* doPick(const Line& l, float radius) +{ picked.collision.time = 1; return world->traceRay(l, radius, &picked); } -Entity * bbCameraPick(Camera *c, float x, float y) { +Entity* bbCameraPick(Camera* c, float x, float y) +{ #ifdef _DEBUG debugCamera(c); #endif int vp_x, vp_y, vp_w, vp_h; c->getViewport(&vp_x, &vp_y, &vp_w, &vp_h); - float nr = c->getFrustumNear(); - float fr = c->getFrustumFar(); + float nr = c->getFrustumNear(); + float fr = c->getFrustumFar(); float nr_w = c->getFrustumWidth(); float nr_h = c->getFrustumHeight(); - x = ((x / vp_w) - .5f)*nr_w; - y = (.5f - (y / vp_h))*nr_h; + x = ((x / vp_w) - .5f) * nr_w; + y = (.5f - (y / vp_h)) * nr_h; Line l; if (c->getProjMode() == Camera::PROJ_ORTHO) { - l = c->GetWorldTransform() * Line(Vector(x, y, 0), Vector(0, 0, fr)); //x,y,fr) ); + l = c->GetWorldTransform() * Line(Vector(x, y, 0), Vector(0, 0, fr)); //x,y,fr) ); } else { - x /= nr; y /= nr; - l = c->GetWorldTransform() * Line(Vector(), Vector(x*fr, y*fr, fr)); + x /= nr; + y /= nr; + l = c->GetWorldTransform() * Line(Vector(), Vector(x * fr, y * fr, fr)); } return doPick(l, 0); } -Entity * bbLinePick(float x, float y, float z, float dx, float dy, float dz, float radius) { +Entity* bbLinePick(float x, float y, float z, float dx, float dy, float dz, float radius) +{ #ifdef _DEBUG debug3d(); #endif @@ -1186,7 +1417,8 @@ Entity * bbLinePick(float x, float y, float z, float dx, float dy, float dz, fl return doPick(l, radius); } -Entity * bbEntityPick(Object *src, float range) { +Entity* bbEntityPick(Object* src, float range) +{ #ifdef _DEBUG debugEntity(src); #endif @@ -1196,173 +1428,209 @@ Entity * bbEntityPick(Object *src, float range) { return doPick(l, 0); } -int bbEntityVisible(Object *src, Object *dest) { +int bbEntityVisible(Object* src, Object* dest) +{ #ifdef _DEBUG - if (debug) { debugObject(src); debugObject(dest); } + if (debug) { + debugObject(src); + debugObject(dest); + } #endif return world->CheckLineOfSight(src, dest) ? 1 : 0; } -int bbEntityInView(Entity *e, Camera *c) { +int bbEntityInView(Entity* e, Camera* c) +{ #ifdef _DEBUG - if (debug) { debugEntity(e); debugCamera(c); } + if (debug) { + debugEntity(e); + debugCamera(c); + } #endif - if (Model *p = e->getModel()) { - if (MeshModel *m = p->getMeshModel()) { - const Box &b = m->getBox(); - Transform t = -c->GetWorldTransform() * e->GetWorldTransform(); - Vector p[] = { - t*b.corner(0),t*b.corner(1),t*b.corner(2),t*b.corner(3), - t*b.corner(4),t*b.corner(5),t*b.corner(6),t*b.corner(7) - }; + if (Model* p = e->getModel()) { + if (MeshModel* m = p->getMeshModel()) { + const Box& b = m->getBox(); + Transform t = -c->GetWorldTransform() * e->GetWorldTransform(); + Vector p[] = {t * b.corner(0), t * b.corner(1), t * b.corner(2), t * b.corner(3), + t * b.corner(4), t * b.corner(5), t * b.corner(6), t * b.corner(7)}; return c->getFrustum().cull(p, 8); } } - Vector p[] = { -c->GetWorldTransform() * e->GetWorldPosition() }; + Vector p[] = {-c->GetWorldTransform() * e->GetWorldPosition()}; return c->getFrustum().cull(p, 1); } -float bbPickedX() { +float bbPickedX() +{ return picked.coords.x; } -float bbPickedY() { +float bbPickedY() +{ return picked.coords.y; } -float bbPickedZ() { +float bbPickedZ() +{ return picked.coords.z; } -float bbPickedNX() { +float bbPickedNX() +{ return picked.collision.normal.x; } -float bbPickedNY() { +float bbPickedNY() +{ return picked.collision.normal.y; } -float bbPickedNZ() { +float bbPickedNZ() +{ return picked.collision.normal.z; } -float bbPickedTime() { +float bbPickedTime() +{ return picked.collision.time; } -Object * bbPickedEntity() { +Object* bbPickedEntity() +{ return picked.with; } -void * bbPickedSurface() { +void* bbPickedSurface() +{ return picked.collision.surface; } -int bbPickedTriangle() { +int bbPickedTriangle() +{ return picked.collision.index; } //////////////////// // LIGHT COMMANDS // //////////////////// -Entity * bbCreateLight(int type, Entity *p) { +Entity* bbCreateLight(int type, Entity* p) +{ #ifdef _DEBUG debugParent(p); #endif - Light *t = new Light(type); + Light* t = new Light(type); return insertEntity(t, p); } -void bbLightColor(Light *light, float r, float g, float b) { +void bbLightColor(Light* light, float r, float g, float b) +{ #ifdef _DEBUG debugLight(light); #endif - light->setColor(Vector(r*s_colorToFloat, g*s_colorToFloat, b*s_colorToFloat)); + light->setColor(Vector(r * s_colorToFloat, g * s_colorToFloat, b * s_colorToFloat)); } -void bbLightRange(Light *light, float range) { +void bbLightRange(Light* light, float range) +{ #ifdef _DEBUG debugLight(light); #endif light->setRange(range); } -void bbLightConeAngles(Light *light, float inner, float outer) { +void bbLightConeAngles(Light* light, float inner, float outer) +{ #ifdef _DEBUG debugLight(light); #endif inner *= s_degreesToRadians; outer *= s_degreesToRadians; - if (inner < 0) inner = 0; - else if (inner > PI) inner = PI; - if (outer < inner) outer = inner; - else if (outer > PI) outer = PI; + if (inner < 0) + inner = 0; + else if (inner > PI) + inner = PI; + if (outer < inner) + outer = inner; + else if (outer > PI) + outer = PI; light->setConeAngles(inner, outer); } //////////////////// // PIVOT COMMANDS // //////////////////// -Entity * bbCreatePivot(Entity *p) { +Entity* bbCreatePivot(Entity* p) +{ #ifdef _DEBUG debugParent(p); #endif - Pivot *t = new Pivot(); + Pivot* t = new Pivot(); return insertEntity(t, p); } ///////////////////// // SPRITE COMMANDS // ///////////////////// -Entity * bbCreateSprite(Entity *p) { +Entity* bbCreateSprite(Entity* p) +{ #ifdef _DEBUG debugParent(p); #endif - Sprite *s = new Sprite(); + Sprite* s = new Sprite(); s->setFX(gxScene::FX_FULLBRIGHT); return insertEntity(s, p); } -Entity * bbLoadSprite(BBStr *file, int flags, Entity *p) { +Entity* bbLoadSprite(BBStr* file, int flags, Entity* p) +{ #ifdef _DEBUG debugParent(p); #endif Texture t(*file, flags); - delete file; if (!t.getCanvas(0)) return 0; - Sprite *s = new Sprite(); + delete file; + if (!t.getCanvas(0)) + return 0; + Sprite* s = new Sprite(); s->setTexture(0, t, 0); s->setFX(gxScene::FX_FULLBRIGHT); - if (flags & gxCanvas::CANVAS_TEX_MASK) s->setBlend(gxScene::BLEND_REPLACE); - else if (flags & gxCanvas::CANVAS_TEX_ALPHA) s->setBlend(gxScene::BLEND_ALPHA); - else s->setBlend(gxScene::BLEND_ADD); + if (flags & gxCanvas::CANVAS_TEX_MASK) + s->setBlend(gxScene::BLEND_REPLACE); + else if (flags & gxCanvas::CANVAS_TEX_ALPHA) + s->setBlend(gxScene::BLEND_ALPHA); + else + s->setBlend(gxScene::BLEND_ADD); return insertEntity(s, p); } -void bbRotateSprite(Sprite *s, float angle) { +void bbRotateSprite(Sprite* s, float angle) +{ #ifdef _DEBUG debugSprite(s); #endif - s->setRotation(angle*s_degreesToRadians); + s->setRotation(angle * s_degreesToRadians); } -void bbScaleSprite(Sprite *s, float x, float y) { +void bbScaleSprite(Sprite* s, float x, float y) +{ #ifdef _DEBUG debugSprite(s); #endif s->setScale(x, y); } -void bbHandleSprite(Sprite *s, float x, float y) { +void bbHandleSprite(Sprite* s, float x, float y) +{ #ifdef _DEBUG debugSprite(s); #endif s->setHandle(x, y); } -void bbSpriteViewMode(Sprite *s, int mode) { +void bbSpriteViewMode(Sprite* s, int mode) +{ #ifdef _DEBUG debugSprite(s); #endif @@ -1372,62 +1640,74 @@ void bbSpriteViewMode(Sprite *s, int mode) { ///////////////////// // MIRROR COMMANDS // ///////////////////// -Entity * bbCreateMirror(Entity *p) { +Entity* bbCreateMirror(Entity* p) +{ #ifdef _DEBUG debugParent(p); #endif - Mirror *t = new Mirror(); + Mirror* t = new Mirror(); return insertEntity(t, p); } //////////////////// // PLANE COMMANDS // //////////////////// -Entity * bbCreatePlane(int segs, Entity *p) { +Entity* bbCreatePlane(int segs, Entity* p) +{ #ifdef _DEBUG if (debug) { debugParent(p); - if (segs < 1 || segs>20) ThrowRuntimeException("Illegal number of segments"); + if (segs < 1 || segs > 20) + ThrowRuntimeException("Illegal number of segments"); } #endif - PlaneModel *t = new PlaneModel(segs); + PlaneModel* t = new PlaneModel(segs); return insertEntity(t, p); } ////////////////// // MD2 COMMANDS // ////////////////// -Entity * bbLoadMD2(BBStr *file, Entity *p) { +Entity* bbLoadMD2(BBStr* file, Entity* p) +{ #ifdef _DEBUG debugParent(p); #endif - MD2Model *t = new MD2Model(*file); delete file; - if (!t->getValid()) { delete t; return 0; } + MD2Model* t = new MD2Model(*file); + delete file; + if (!t->getValid()) { + delete t; + return 0; + } return insertEntity(t, p); } -void bbAnimateMD2(MD2Model *m, int mode, float speed, int first, int last, float trans) { +void bbAnimateMD2(MD2Model* m, int mode, float speed, int first, int last, float trans) +{ #ifdef _DEBUG debugMD2(m); #endif m->startMD2Anim(first, last, mode, speed, trans); } -float bbMD2AnimTime(MD2Model *m) { +float bbMD2AnimTime(MD2Model* m) +{ #ifdef _DEBUG debugMD2(m); #endif return m->getMD2AnimTime(); } -int bbMD2AnimLength(MD2Model *m) { +int bbMD2AnimLength(MD2Model* m) +{ #ifdef _DEBUG debugMD2(m); #endif return m->getMD2AnimLength(); } -int bbMD2Animating(MD2Model *m) { +int bbMD2Animating(MD2Model* m) +{ #ifdef _DEBUG debugMD2(m); #endif @@ -1437,27 +1717,34 @@ int bbMD2Animating(MD2Model *m) { ////////////////// // BSP Commands // ////////////////// -Entity * bbLoadBSP(BBStr *file, float gam, Entity *p) { +Entity* bbLoadBSP(BBStr* file, float gam, Entity* p) +{ #ifdef _DEBUG debugParent(p); #endif CachedTextureFactory::setPath(filenamepath(*file)); - Q3BSPModel *t = new Q3BSPModel(*file, gam); delete file; + Q3BSPModel* t = new Q3BSPModel(*file, gam); + delete file; CachedTextureFactory::setPath(""); - if (!t->isValid()) { delete t; return 0; } + if (!t->isValid()) { + delete t; + return 0; + } return insertEntity(t, p); } -void bbBSPAmbientLight(Q3BSPModel *t, float r, float g, float b) { +void bbBSPAmbientLight(Q3BSPModel* t, float r, float g, float b) +{ #ifdef _DEBUG debugBSP(t); #endif - t->setAmbient(Vector(r*s_colorToFloat, g*s_colorToFloat, b*s_colorToFloat)); + t->setAmbient(Vector(r * s_colorToFloat, g * s_colorToFloat, b * s_colorToFloat)); } -void bbBSPLighting(Q3BSPModel *t, int lmap) { +void bbBSPLighting(Q3BSPModel* t, int lmap) +{ #ifdef _DEBUG debugBSP(t); #endif @@ -1467,52 +1754,62 @@ void bbBSPLighting(Q3BSPModel *t, int lmap) { ////////////////////// // TERRAIN COMMANDS // ////////////////////// -static float terrainHeight(Terrain *t, float x, float z) { - int ix = (int)floor(x); - int iz = (int)floor(z); +static float terrainHeight(Terrain* t, float x, float z) +{ + int ix = (int)floor(x); + int iz = (int)floor(z); float tx = x - ix, tz = z - iz; float h0 = t->getHeight(ix, iz); float h1 = t->getHeight(ix + 1, iz); float h2 = t->getHeight(ix, iz + 1); float h3 = t->getHeight(ix + 1, iz + 1); - float ha = (h1 - h0)*tx + h0, hb = (h3 - h2)*tx + h2; - float h = (hb - ha)*tz + ha; + float ha = (h1 - h0) * tx + h0, hb = (h3 - h2) * tx + h2; + float h = (hb - ha) * tz + ha; return h; } -static Vector terrainVector(Terrain *t, float x, float y, float z) { +static Vector terrainVector(Terrain* t, float x, float y, float z) +{ Vector v = -t->GetWorldTransform() * Vector(x, y, z); return t->GetWorldTransform() * Vector(v.x, terrainHeight(t, v.x, v.z), v.z); } -Entity * bbCreateTerrain(int n, Entity *p) { +Entity* bbCreateTerrain(int n, Entity* p) +{ #ifdef _DEBUG debugParent(p); #endif int shift = 0; - while ((1 << shift) < n) ++shift; - if ((1 << shift) != n) ThrowRuntimeException("Illegal terrain size"); - Terrain *t = new Terrain(shift); + while ((1 << shift) < n) + ++shift; + if ((1 << shift) != n) + ThrowRuntimeException("Illegal terrain size"); + Terrain* t = new Terrain(shift); return insertEntity(t, p); } -Entity * bbLoadTerrain(BBStr *file, Entity *p) { +Entity* bbLoadTerrain(BBStr* file, Entity* p) +{ #ifdef _DEBUG debugParent(p); #endif - gxCanvas *c = gx_graphics->loadCanvas(*file, gxCanvas::CANVAS_HIGHCOLOR); - if (!c) ThrowRuntimeException("Unable to load heightmap image"); + gxCanvas* c = gx_graphics->loadCanvas(*file, gxCanvas::CANVAS_HIGHCOLOR); + if (!c) + ThrowRuntimeException("Unable to load heightmap image"); int w = c->getWidth(), h = c->getHeight(); - if (w != h) ThrowRuntimeException("Terrain must be square"); + if (w != h) + ThrowRuntimeException("Terrain must be square"); int shift = 0; - while ((1 << shift) < w) ++shift; - if ((1 << shift) != w) ThrowRuntimeException("Illegal terrain size"); - Terrain *t = new Terrain(shift); + while ((1 << shift) < w) + ++shift; + if ((1 << shift) != w) + ThrowRuntimeException("Illegal terrain size"); + Terrain* t = new Terrain(shift); c->lock(); for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { - int rgb = c->getPixelFast(x, y); - int r = (rgb >> 16) & 0xff, g = (rgb >> 8) & 0xff, b = rgb & 0xff; + int rgb = c->getPixelFast(x, y); + int r = (rgb >> 16) & 0xff, g = (rgb >> 8) & 0xff, b = rgb & 0xff; float p = (r > g ? (r > b ? r : b) : (g > b ? g : b)) / 255.0f; t->setHeight(x, h - 1 - y, p, false); } @@ -1522,56 +1819,64 @@ Entity * bbLoadTerrain(BBStr *file, Entity *p) { return insertEntity(t, p); } -void bbTerrainDetail(Terrain *t, int n, int m) { +void bbTerrainDetail(Terrain* t, int n, int m) +{ #ifdef _DEBUG debugTerrain(t); #endif t->setDetail(n, !!m); } -void bbTerrainShading(Terrain *t, int enable) { +void bbTerrainShading(Terrain* t, int enable) +{ #ifdef _DEBUG debugTerrain(t); #endif t->setShading(!!enable); } -float bbTerrainX(Terrain *t, float x, float y, float z) { +float bbTerrainX(Terrain* t, float x, float y, float z) +{ #ifdef _DEBUG debugTerrain(t); #endif return terrainVector(t, x, y, z).x; } -float bbTerrainY(Terrain *t, float x, float y, float z) { +float bbTerrainY(Terrain* t, float x, float y, float z) +{ #ifdef _DEBUG debugTerrain(t); #endif return terrainVector(t, x, y, z).y; } -float bbTerrainZ(Terrain *t, float x, float y, float z) { +float bbTerrainZ(Terrain* t, float x, float y, float z) +{ #ifdef _DEBUG debugTerrain(t); #endif return terrainVector(t, x, y, z).z; } -int bbTerrainSize(Terrain *t) { +int bbTerrainSize(Terrain* t) +{ #ifdef _DEBUG debugTerrain(t); #endif return t->getSize(); } -float bbTerrainHeight(Terrain *t, int x, int z) { +float bbTerrainHeight(Terrain* t, int x, int z) +{ #ifdef _DEBUG debugTerrain(t); #endif return t->getHeight(x, z); } -void bbModifyTerrain(Terrain *t, int x, int z, float h, int realtime) { +void bbModifyTerrain(Terrain* t, int x, int z, float h, int realtime) +{ #ifdef _DEBUG debugTerrain(t); #endif @@ -1581,22 +1886,26 @@ void bbModifyTerrain(Terrain *t, int x, int z, float h, int realtime) { //////////////////// // AUDIO COMMANDS // //////////////////// -Entity * bbCreateListener(Entity *p, float roll, float dopp, float dist) { +Entity* bbCreateListener(Entity* p, float roll, float dopp, float dist) +{ #ifdef _DEBUG if (debug) { debugParent(p); - if (listener) ThrowRuntimeException("Listener already created"); + if (listener) + ThrowRuntimeException("Listener already created"); } #endif listener = new Listener(roll, dopp, dist); return insertEntity(listener, p); } -gxChannel * bbEmitSound(gxSound *sound, Object *o) { +gxChannel* bbEmitSound(gxSound* sound, Object* o) +{ #ifdef _DEBUG if (debug) { debugObject(o); - if (!listener) ThrowRuntimeException("No Listener created"); + if (!listener) + ThrowRuntimeException("No Listener created"); } #endif return o->emitSound(sound); @@ -1605,20 +1914,24 @@ gxChannel * bbEmitSound(gxSound *sound, Object *o) { ///////////////////// // ENTITY COMMANDS // ///////////////////// -Entity * bbCopyEntity(Entity *e, Entity *p) { +Entity* bbCopyEntity(Entity* e, Entity* p) +{ #ifdef _DEBUG if (debug) { debugEntity(e); debugParent(p); } #endif - Entity *t = e->getObject()->copy(); - if (!t) return 0; + Entity* t = e->getObject()->copy(); + if (!t) + return 0; return insertEntity(t, p); } -void bbFreeEntity(Entity *e) { - if (!e) return; +void bbFreeEntity(Entity* e) +{ + if (!e) + return; #ifdef _DEBUG if (debug) { debugEntity(e); @@ -1628,7 +1941,8 @@ void bbFreeEntity(Entity *e) { delete e; } -void bbHideEntity(Entity *e) { +void bbHideEntity(Entity* e) +{ #ifdef _DEBUG debugEntity(e); #endif @@ -1636,7 +1950,8 @@ void bbHideEntity(Entity *e) { e->SetVisible(false); } -void bbShowEntity(Entity *e) { +void bbShowEntity(Entity* e) +{ #ifdef _DEBUG debugEntity(e); #endif @@ -1645,12 +1960,13 @@ void bbShowEntity(Entity *e) { e->getObject()->reset(); } -void bbEntityParent(Entity *e, Entity *p, int global) { +void bbEntityParent(Entity* e, Entity* p, int global) +{ #ifdef _DEBUG if (debug) { debugEntity(e); debugParent(p); - Entity *t = p; + Entity* t = p; while (t) { if (t == e) { ThrowRuntimeException("Entity cannot be parented to itself!"); @@ -1660,7 +1976,8 @@ void bbEntityParent(Entity *e, Entity *p, int global) { } #endif - if (e->getParent() == p) return; + if (e->getParent() == p) + return; if (global) { Transform t = e->GetWorldTransform(); @@ -1672,25 +1989,30 @@ void bbEntityParent(Entity *e, Entity *p, int global) { } } -int bbCountChildren(Entity *e) { +int bbCountChildren(Entity* e) +{ #ifdef _DEBUG debugEntity(e); #endif int n = 0; - for (Entity *p = e->GetChildren(); p; p = p->GetSuccessor()) ++n; + for (Entity* p = e->GetChildren(); p; p = p->GetSuccessor()) + ++n; return n; } -Entity * bbGetChild(Entity *e, int index) { +Entity* bbGetChild(Entity* e, int index) +{ #ifdef _DEBUG debugEntity(e); #endif - Entity *p = e->GetChildren(); - while (--index && p) p = p->GetSuccessor(); + Entity* p = e->GetChildren(); + while (--index && p) + p = p->GetSuccessor(); return p; } -Entity * bbFindChild(Entity *e, BBStr *t) { +Entity* bbFindChild(Entity* e, BBStr* t) +{ #ifdef _DEBUG debugEntity(e); #endif @@ -1702,15 +2024,16 @@ Entity * bbFindChild(Entity *e, BBStr *t) { //////////////////////// // ANIMATION COMMANDS // //////////////////////// -int bbLoadAnimSeq(Object *o, BBStr *f) { +int bbLoadAnimSeq(Object* o, BBStr* f) +{ #ifdef _DEBUG debugObject(o); #endif - if (Animator *anim = o->getAnimator()) { - Entity *t = loadEntity(f->c_str(), MeshLoader::HINT_ANIMONLY); + if (Animator* anim = o->getAnimator()) { + Entity* t = loadEntity(f->c_str(), MeshLoader::HINT_ANIMONLY); delete f; if (t) { - if (Animator *p = t->getObject()->getAnimator()) { + if (Animator* p = t->getObject()->getAnimator()) { anim->addSeqs(p); } delete t; @@ -1722,55 +2045,63 @@ int bbLoadAnimSeq(Object *o, BBStr *f) { return -1; } -void bbSetAnimTime(Object *o, float time, int seq) { +void bbSetAnimTime(Object* o, float time, int seq) +{ #ifdef _DEBUG debugObject(o); #endif - if (Animator *anim = o->getAnimator()) { + if (Animator* anim = o->getAnimator()) { anim->setAnimTime(time, seq); } else { ThrowRuntimeException("Entity has not animation"); } } -void bbAnimate(Object *o, int mode, float speed, int seq, float trans) { +void bbAnimate(Object* o, int mode, float speed, int seq, float trans) +{ #ifdef _DEBUG debugObject(o); #endif - if (Animator *anim = o->getAnimator()) { + if (Animator* anim = o->getAnimator()) { anim->animate(mode, speed, seq, trans); } else { ThrowRuntimeException("Entity has no animation"); } } -void bbSetAnimKey(Object *o, int frame, int pos_key, int rot_key, int scl_key) { +void bbSetAnimKey(Object* o, int frame, int pos_key, int rot_key, int scl_key) +{ #ifdef _DEBUG debugObject(o); #endif Animation anim = o->getAnimation(); - if (pos_key) anim.setPositionKey(frame, o->GetLocalPosition()); - if (rot_key) anim.setRotationKey(frame, o->GetLocalRotation()); - if (scl_key) anim.setScaleKey(frame, o->GetLocalScale()); + if (pos_key) + anim.setPositionKey(frame, o->GetLocalPosition()); + if (rot_key) + anim.setRotationKey(frame, o->GetLocalRotation()); + if (scl_key) + anim.setScaleKey(frame, o->GetLocalScale()); o->setAnimation(anim); } -int bbExtractAnimSeq(Object *o, int first, int last, int seq) { +int bbExtractAnimSeq(Object* o, int first, int last, int seq) +{ #ifdef _DEBUG debugObject(o); #endif - if (Animator *anim = o->getAnimator()) { + if (Animator* anim = o->getAnimator()) { anim->extractSeq(first, last, seq); return anim->numSeqs() - 1; } return -1; } -int bbAddAnimSeq(Object *o, int length) { +int bbAddAnimSeq(Object* o, int length) +{ #ifdef _DEBUG debugObject(o); #endif - Animator *anim = o->getAnimator(); + Animator* anim = o->getAnimator(); if (anim) { anim->addSeq(length); } else { @@ -1780,42 +2111,51 @@ int bbAddAnimSeq(Object *o, int length) { return anim->numSeqs() - 1; } -int bbAnimSeq(Object *o) { +int bbAnimSeq(Object* o) +{ #ifdef _DEBUG debugObject(o); #endif - if (Animator *anim = o->getAnimator()) return anim->animSeq(); + if (Animator* anim = o->getAnimator()) + return anim->animSeq(); return -1; } -float bbAnimTime(Object *o) { +float bbAnimTime(Object* o) +{ #ifdef _DEBUG debugObject(o); #endif - if (Animator *anim = o->getAnimator()) return anim->animTime(); + if (Animator* anim = o->getAnimator()) + return anim->animTime(); return -1; } -int bbAnimLength(Object *o) { +int bbAnimLength(Object* o) +{ #ifdef _DEBUG debugObject(o); #endif - if (Animator *anim = o->getAnimator()) return anim->animLen(); + if (Animator* anim = o->getAnimator()) + return anim->animLen(); return -1; } -int bbAnimating(Object *o) { +int bbAnimating(Object* o) +{ #ifdef _DEBUG debugObject(o); #endif - if (Animator *anim = o->getAnimator()) return anim->animating(); + if (Animator* anim = o->getAnimator()) + return anim->animating(); return 0; } //////////////////////////////// // ENTITY SPECIAL FX COMMANDS // //////////////////////////////// -void bbPaintEntity(Model *m, Brush *b) { +void bbPaintEntity(Model* m, Brush* b) +{ #ifdef _DEBUG if (debug) { debugModel(m); @@ -1825,28 +2165,32 @@ void bbPaintEntity(Model *m, Brush *b) { m->setBrush(*b); } -void bbEntityColor(Model *m, float r, float g, float b) { +void bbEntityColor(Model* m, float r, float g, float b) +{ #ifdef _DEBUG debugModel(m); #endif - m->setColor(Vector(r*s_colorToFloat, g*s_colorToFloat, b*s_colorToFloat)); + m->setColor(Vector(r * s_colorToFloat, g * s_colorToFloat, b * s_colorToFloat)); } -void bbEntityAlpha(Model *m, float alpha) { +void bbEntityAlpha(Model* m, float alpha) +{ #ifdef _DEBUG debugModel(m); #endif m->setAlpha(alpha); } -void bbEntityShininess(Model *m, float shininess) { +void bbEntityShininess(Model* m, float shininess) +{ #ifdef _DEBUG debugModel(m); #endif m->setShininess(shininess); } -void bbEntityTexture(Model *m, Texture *t, int frame, int index) { +void bbEntityTexture(Model* m, Texture* t, int frame, int index) +{ #ifdef _DEBUG debugModel(m); debugTexture(t); @@ -1854,28 +2198,32 @@ void bbEntityTexture(Model *m, Texture *t, int frame, int index) { m->setTexture(index, *t, frame); } -void bbEntityBlend(Model *m, int blend) { +void bbEntityBlend(Model* m, int blend) +{ #ifdef _DEBUG debugModel(m); #endif m->setBlend(blend); } -void bbEntityFX(Model *m, int fx) { +void bbEntityFX(Model* m, int fx) +{ #ifdef _DEBUG debugModel(m); #endif m->setFX(fx); } -void bbEntityAutoFade(Model *m, float nr, float fr) { +void bbEntityAutoFade(Model* m, float nr, float fr) +{ #ifdef _DEBUG debugModel(m); #endif m->setAutoFade(nr, fr); } -void bbEntityOrder(Object *o, int n) { +void bbEntityOrder(Object* o, int n) +{ #ifdef _DEBUG if (debug) { debugEntity(o); @@ -1890,163 +2238,202 @@ void bbEntityOrder(Object *o, int n) { ////////////////////////////// // ENTITY PROPERTY COMMANDS // ////////////////////////////// -float bbEntityX(Entity *e, int global) { +float bbEntityX(Entity* e, int global) +{ #ifdef _DEBUG debugEntity(e); #endif return global ? e->GetWorldPosition().x : e->GetLocalPosition().x; } -float bbEntityY(Entity *e, int global) { +float bbEntityY(Entity* e, int global) +{ #ifdef _DEBUG debugEntity(e); #endif return global ? e->GetWorldPosition().y : e->GetLocalPosition().y; } -float bbEntityZ(Entity *e, int global) { +float bbEntityZ(Entity* e, int global) +{ #ifdef _DEBUG debugEntity(e); #endif return global ? e->GetWorldPosition().z : e->GetLocalPosition().z; } -float bbEntityPitch(Entity *e, int global) { +float bbEntityPitch(Entity* e, int global) +{ #ifdef _DEBUG debugEntity(e); #endif return quatPitch(global ? e->GetWorldRotation() : e->GetLocalRotation()) * s_radiansToDegrees; } -float bbEntityYaw(Entity *e, int global) { +float bbEntityYaw(Entity* e, int global) +{ #ifdef _DEBUG debugEntity(e); #endif return quatYaw(global ? e->GetWorldRotation() : e->GetLocalRotation()) * s_radiansToDegrees; } -float bbEntityRoll(Entity *e, int global) { +float bbEntityRoll(Entity* e, int global) +{ #ifdef _DEBUG debugEntity(e); #endif return quatRoll(global ? e->GetWorldRotation() : e->GetLocalRotation()) * s_radiansToDegrees; } -float bbGetMatElement(Entity *e, int row, int col) { +float bbGetMatElement(Entity* e, int row, int col) +{ #ifdef _DEBUG debugEntity(e); #endif return row < 3 ? e->GetWorldTransform().m[row][col] : e->GetWorldTransform().v[col]; } -void bbTFormPoint(float x, float y, float z, Entity *src, Entity *dest) { +void bbTFormPoint(float x, float y, float z, Entity* src, Entity* dest) +{ #ifdef _DEBUG if (debug) { - if (src) debugEntity(src); - if (dest) debugEntity(dest); + if (src) + debugEntity(src); + if (dest) + debugEntity(dest); } #endif tformed = Vector(x, y, z); - if (src) tformed = src->GetWorldTransform() * tformed; - if (dest) tformed = -dest->GetWorldTransform() * tformed; + if (src) + tformed = src->GetWorldTransform() * tformed; + if (dest) + tformed = -dest->GetWorldTransform() * tformed; } -void bbTFormVector(float x, float y, float z, Entity *src, Entity *dest) { +void bbTFormVector(float x, float y, float z, Entity* src, Entity* dest) +{ #ifdef _DEBUG if (debug) { - if (src) debugEntity(src); - if (dest) debugEntity(dest); + if (src) + debugEntity(src); + if (dest) + debugEntity(dest); } #endif tformed = Vector(x, y, z); - if (src) tformed = src->GetWorldTransform().m * tformed; - if (dest) tformed = -dest->GetWorldTransform().m * tformed; + if (src) + tformed = src->GetWorldTransform().m * tformed; + if (dest) + tformed = -dest->GetWorldTransform().m * tformed; } -void bbTFormNormal(float x, float y, float z, Entity *src, Entity *dest) { +void bbTFormNormal(float x, float y, float z, Entity* src, Entity* dest) +{ #ifdef _DEBUG if (debug) { - if (src) debugEntity(src); - if (dest) debugEntity(dest); + if (src) + debugEntity(src); + if (dest) + debugEntity(dest); } #endif tformed = Vector(x, y, z); - if (src) tformed = (src->GetWorldTransform().m).cofactor() * tformed; - if (dest) tformed = (-dest->GetWorldTransform().m).cofactor() * tformed; + if (src) + tformed = (src->GetWorldTransform().m).cofactor() * tformed; + if (dest) + tformed = (-dest->GetWorldTransform().m).cofactor() * tformed; tformed.normalize(); } -float bbTFormedX() { +float bbTFormedX() +{ return tformed.x; } -float bbTFormedY() { +float bbTFormedY() +{ return tformed.y; } -float bbTFormedZ() { +float bbTFormedZ() +{ return tformed.z; } -float bbVectorYaw(float x, float y, float z) { +float bbVectorYaw(float x, float y, float z) +{ return Vector(x, y, z).yaw() * s_radiansToDegrees; } -float bbVectorPitch(float x, float y, float z) { +float bbVectorPitch(float x, float y, float z) +{ return Vector(x, y, z).pitch() * s_radiansToDegrees; } -float bbDeltaYaw(Entity *src, Entity *dest) { +float bbDeltaYaw(Entity* src, Entity* dest) +{ float x = src->GetWorldTransform().m.k.yaw(); float y = (dest->GetWorldTransform().v - src->GetWorldTransform().v).yaw(); float d = y - x; - if (d < -PI) d += TWOPI; - else if (d >= PI) d -= TWOPI; - return d*s_radiansToDegrees; + if (d < -PI) + d += TWOPI; + else if (d >= PI) + d -= TWOPI; + return d * s_radiansToDegrees; } -float bbDeltaPitch(Entity *src, Entity *dest) { +float bbDeltaPitch(Entity* src, Entity* dest) +{ float x = src->GetWorldTransform().m.k.pitch(); float y = (dest->GetWorldTransform().v - src->GetWorldTransform().v).pitch(); float d = y - x; - if (d < -PI) d += TWOPI; - else if (d >= PI) d -= TWOPI; - return d*s_radiansToDegrees; + if (d < -PI) + d += TWOPI; + else if (d >= PI) + d -= TWOPI; + return d * s_radiansToDegrees; } /////////////////////////////// // ENTITY COLLISION COMMANDS // /////////////////////////////// -void bbResetEntity(Object *o) { +void bbResetEntity(Object* o) +{ #ifdef _DEBUG debugObject(o); #endif o->reset(); } -static void entityType(Entity *e, int type) { +static void entityType(Entity* e, int type) +{ e->getObject()->setCollisionType(type); e->getObject()->reset(); - for (Entity *p = e->GetChildren(); p; p = p->GetSuccessor()) { + for (Entity* p = e->GetChildren(); p; p = p->GetSuccessor()) { entityType(p, type); } } -void bbEntityType(Object *o, int type, int recurs) { +void bbEntityType(Object* o, int type, int recurs) +{ #ifdef _DEBUG if (debug) { debugObject(o); - if (type < 0 || type>999) ThrowRuntimeException("EntityType ID must be in the range 0...999"); + if (type < 0 || type > 999) + ThrowRuntimeException("EntityType ID must be in the range 0...999"); } #endif - if (recurs) entityType(o, type); + if (recurs) + entityType(o, type); else { o->setCollisionType(type); o->reset(); } } -void bbEntityPickMode(Object *o, int mode, int obs) { +void bbEntityPickMode(Object* o, int mode, int obs) +{ #ifdef _DEBUG debugObject(o); #endif @@ -2054,21 +2441,24 @@ void bbEntityPickMode(Object *o, int mode, int obs) { o->setObscurer(!!obs); } -Entity * bbGetParent(Entity *e) { +Entity* bbGetParent(Entity* e) +{ #ifdef _DEBUG debugEntity(e); #endif return e->getParent(); } -int bbGetEntityType(Object *o) { +int bbGetEntityType(Object* o) +{ #ifdef _DEBUG debugObject(o); #endif return o->getCollisionType(); } -void bbEntityRadius(Object *o, float x_radius, float y_radius) { +void bbEntityRadius(Object* o, float x_radius, float y_radius) +{ #ifdef _DEBUG debugObject(o); #endif @@ -2076,7 +2466,8 @@ void bbEntityRadius(Object *o, float x_radius, float y_radius) { o->setCollisionRadii(radii); } -void bbEntityBox(Object *o, float x, float y, float z, float w, float h, float d) { +void bbEntityBox(Object* o, float x, float y, float z, float w, float h, float d) +{ #ifdef _DEBUG debugObject(o); #endif @@ -2085,97 +2476,111 @@ void bbEntityBox(Object *o, float x, float y, float z, float w, float h, float o->setCollisionBox(b); } -Object * bbEntityCollided(Object *o, int type) { +Object* bbEntityCollided(Object* o, int type) +{ #ifdef _DEBUG debugObject(o); #endif Object::Collisions::const_iterator it; - const Object::Collisions &c = o->getCollisions(); + const Object::Collisions& c = o->getCollisions(); for (it = c.begin(); it != c.end(); ++it) { - const ObjCollision *c = *it; - if (c->with->getCollisionType() == type) return c->with; + const ObjCollision* c = *it; + if (c->with->getCollisionType() == type) + return c->with; } return 0; } -int bbCountCollisions(Object *o) { +int bbCountCollisions(Object* o) +{ #ifdef _DEBUG debugObject(o); #endif return o->getCollisions().size(); } -float bbCollisionX(Object *o, int index) { +float bbCollisionX(Object* o, int index) +{ #ifdef _DEBUG debugColl(o, index); #endif return o->getCollisions()[index - 1]->coords.x; } -float bbCollisionY(Object *o, int index) { +float bbCollisionY(Object* o, int index) +{ #ifdef _DEBUG debugColl(o, index); #endif return o->getCollisions()[index - 1]->coords.y; } -float bbCollisionZ(Object *o, int index) { +float bbCollisionZ(Object* o, int index) +{ #ifdef _DEBUG debugColl(o, index); #endif return o->getCollisions()[index - 1]->coords.z; } -float bbCollisionNX(Object *o, int index) { +float bbCollisionNX(Object* o, int index) +{ #ifdef _DEBUG debugColl(o, index); #endif return o->getCollisions()[index - 1]->collision.normal.x; } -float bbCollisionNY(Object *o, int index) { +float bbCollisionNY(Object* o, int index) +{ #ifdef _DEBUG debugColl(o, index); #endif return o->getCollisions()[index - 1]->collision.normal.y; } -float bbCollisionNZ(Object *o, int index) { +float bbCollisionNZ(Object* o, int index) +{ #ifdef _DEBUG debugColl(o, index); #endif return o->getCollisions()[index - 1]->collision.normal.z; } -float bbCollisionTime(Object *o, int index) { +float bbCollisionTime(Object* o, int index) +{ #ifdef _DEBUG debugColl(o, index); #endif return o->getCollisions()[index - 1]->collision.time; } -Object * bbCollisionEntity(Object *o, int index) { +Object* bbCollisionEntity(Object* o, int index) +{ #ifdef _DEBUG debugColl(o, index); #endif return o->getCollisions()[index - 1]->with; } -void * bbCollisionSurface(Object *o, int index) { +void* bbCollisionSurface(Object* o, int index) +{ #ifdef _DEBUG debugColl(o, index); #endif return o->getCollisions()[index - 1]->collision.surface; } -int bbCollisionTriangle(Object *o, int index) { +int bbCollisionTriangle(Object* o, int index) +{ #ifdef _DEBUG debugColl(o, index); #endif return o->getCollisions()[index - 1]->collision.index; } -float bbEntityDistance(Entity *src, Entity *dest) { +float bbEntityDistance(Entity* src, Entity* dest) +{ #ifdef _DEBUG debugEntity(src); debugEntity(dest); @@ -2186,95 +2591,105 @@ float bbEntityDistance(Entity *src, Entity *dest) { //////////////////////////////////// // ENTITY TRANSFORMATION COMMANDS // //////////////////////////////////// -void bbMoveEntity(Entity *e, float x, float y, float z) { +void bbMoveEntity(Entity* e, float x, float y, float z) +{ #ifdef _DEBUG debugEntity(e); #endif - e->SetLocalPosition(e->GetLocalPosition() + e->GetLocalRotation()*Vector(x, y, z)); + e->SetLocalPosition(e->GetLocalPosition() + e->GetLocalRotation() * Vector(x, y, z)); } -void bbTurnEntity(Entity *e, float p, float y, float r, int global) { +void bbTurnEntity(Entity* e, float p, float y, float r, int global) +{ #ifdef _DEBUG debugEntity(e); #endif - global ? - e->SetWorldRotation(rotationQuat(p*s_degreesToRadians, y*s_degreesToRadians, r*s_degreesToRadians)*e->GetWorldRotation()) : - e->SetLocalRotation(e->GetLocalRotation()*rotationQuat(p*s_degreesToRadians, y*s_degreesToRadians, r*s_degreesToRadians)); + global + ? e->SetWorldRotation(rotationQuat(p * s_degreesToRadians, y * s_degreesToRadians, r * s_degreesToRadians) + * e->GetWorldRotation()) + : e->SetLocalRotation(e->GetLocalRotation() + * rotationQuat(p * s_degreesToRadians, y * s_degreesToRadians, r * s_degreesToRadians)); } -void bbTranslateEntity(Entity *e, float x, float y, float z, int global) { +void bbTranslateEntity(Entity* e, float x, float y, float z, int global) +{ #ifdef _DEBUG debugEntity(e); #endif - global ? - e->SetWorldPosition(e->GetWorldPosition() + Vector(x, y, z)) : - e->SetLocalPosition(e->GetLocalPosition() + Vector(x, y, z)); + global ? e->SetWorldPosition(e->GetWorldPosition() + Vector(x, y, z)) + : e->SetLocalPosition(e->GetLocalPosition() + Vector(x, y, z)); } -void bbPositionEntity(Entity *e, float x, float y, float z, int global) { +void bbPositionEntity(Entity* e, float x, float y, float z, int global) +{ #ifdef _DEBUG debugEntity(e); #endif - global ? - e->SetWorldPosition(Vector(x, y, z)) : - e->SetLocalPosition(Vector(x, y, z)); + global ? e->SetWorldPosition(Vector(x, y, z)) : e->SetLocalPosition(Vector(x, y, z)); } -void bbScaleEntity(Entity *e, float x, float y, float z, int global) { +void bbScaleEntity(Entity* e, float x, float y, float z, int global) +{ #ifdef _DEBUG debugEntity(e); #endif - global ? - e->SetWorldScale(Vector(x, y, z)) : - e->SetLocalScale(Vector(x, y, z)); + global ? e->SetWorldScale(Vector(x, y, z)) : e->SetLocalScale(Vector(x, y, z)); } -void bbRotateEntity(Entity *e, float p, float y, float r, int global) { +void bbRotateEntity(Entity* e, float p, float y, float r, int global) +{ #ifdef _DEBUG debugEntity(e); #endif - global ? - e->SetWorldRotation(rotationQuat(p*s_degreesToRadians, y*s_degreesToRadians, r*s_degreesToRadians)) : - e->SetLocalRotation(rotationQuat(p*s_degreesToRadians, y*s_degreesToRadians, r*s_degreesToRadians)); + global ? e->SetWorldRotation(rotationQuat(p * s_degreesToRadians, y * s_degreesToRadians, r * s_degreesToRadians)) + : e->SetLocalRotation(rotationQuat(p * s_degreesToRadians, y * s_degreesToRadians, r * s_degreesToRadians)); } -void bbPointEntity(Entity *e, Entity *t, float roll) { +void bbPointEntity(Entity* e, Entity* t, float roll) +{ #ifdef _DEBUG - if (debug) { debugEntity(e); debugEntity(t); } + if (debug) { + debugEntity(e); + debugEntity(t); + } #endif Vector v = t->GetWorldTransform().v - e->GetWorldTransform().v; - e->SetWorldRotation(rotationQuat(v.pitch(), v.yaw(), roll*s_degreesToRadians)); + e->SetWorldRotation(rotationQuat(v.pitch(), v.yaw(), roll * s_degreesToRadians)); } -void bbAlignToVector(Entity *e, float nx, float ny, float nz, int axis, float rate) { +void bbAlignToVector(Entity* e, float nx, float ny, float nz, int axis, float rate) +{ Vector ax(nx, ny, nz); - float l = ax.length(); - if (l <= FLT_EPSILON) return; + float l = ax.length(); + if (l <= FLT_EPSILON) + return; ax /= l; - Quat q = e->GetWorldRotation(); + Quat q = e->GetWorldRotation(); Vector tv = (axis == 1) ? q.i() : (axis == 2 ? q.j() : q.k()); float dp = ax.dot(tv); - if (dp >= 1 - FLT_EPSILON) return; + if (dp >= 1 - FLT_EPSILON) + return; if (dp <= -1 + FLT_EPSILON) { - float an = PI*rate / 2; + float an = PI * rate / 2; Vector cp = (axis == 1) ? q.j() : (axis == 2 ? q.k() : q.i()); - e->SetWorldRotation(Quat(cosf(an), cp*sinf(an)) * q); + e->SetWorldRotation(Quat(cosf(an), cp * sinf(an)) * q); return; } - float an = acosf(dp)*rate / 2; + float an = acosf(dp) * rate / 2; Vector cp = ax.cross(tv).normalized(); - e->SetWorldRotation(Quat(cosf(an), cp*sinf(an)) * q); + e->SetWorldRotation(Quat(cosf(an), cp * sinf(an)) * q); } ////////////////////////// // ENTITY MISC COMMANDS // ////////////////////////// -void bbNameEntity(Entity *e, BBStr *t) { +void bbNameEntity(Entity* e, BBStr* t) +{ #ifdef _DEBUG debugEntity(e); #endif @@ -2282,69 +2697,91 @@ void bbNameEntity(Entity *e, BBStr *t) { delete t; } -BBStr * bbEntityName(Entity *e) { +BBStr* bbEntityName(Entity* e) +{ #ifdef _DEBUG debugEntity(e); #endif return new BBStr(e->getName()); } -BBStr *bbEntityClass(Entity *e) { +BBStr* bbEntityClass(Entity* e) +{ #ifdef _DEBUG debugEntity(e); #endif - const char *p = "Pivot"; - if (e->getLight()) p = "Light"; - else if (e->getCamera()) p = "Camera"; - else if (e->getMirror()) p = "Mirror"; - else if (e->getListener()) p = "Listener"; - else if (Model *t = e->getModel()) { - if (t->getSprite()) p = "Sprite"; - else if (t->getTerrain()) p = "Terrain"; - else if (t->getPlaneModel()) p = "Plane"; - else if (t->getMeshModel()) p = "Mesh"; - else if (t->getMD2Model()) p = "MD2"; - else if (t->getBSPModel()) p = "BSP"; + const char* p = "Pivot"; + if (e->getLight()) + p = "Light"; + else if (e->getCamera()) + p = "Camera"; + else if (e->getMirror()) + p = "Mirror"; + else if (e->getListener()) + p = "Listener"; + else if (Model* t = e->getModel()) { + if (t->getSprite()) + p = "Sprite"; + else if (t->getTerrain()) + p = "Terrain"; + else if (t->getPlaneModel()) + p = "Plane"; + else if (t->getMeshModel()) + p = "Mesh"; + else if (t->getMD2Model()) + p = "MD2"; + else if (t->getBSPModel()) + p = "BSP"; } return new BBStr(p); } -void bbClearWorld(int e, int b, int t) { +void bbClearWorld(int e, int b, int t) +{ if (e) { - while (Entity::GetEntityOrphans()) bbFreeEntity(Entity::GetEntityOrphans()); + while (Entity::GetEntityOrphans()) + bbFreeEntity(Entity::GetEntityOrphans()); } if (b) { - while (brush_set.size()) bbFreeBrush(*brush_set.begin()); + while (brush_set.size()) + bbFreeBrush(*brush_set.begin()); } if (t) { - while (texture_set.size()) bbFreeTexture(*texture_set.begin()); + while (texture_set.size()) + bbFreeTexture(*texture_set.begin()); } } extern int active_texs; -int bbActiveTextures() { +int bbActiveTextures() +{ return active_texs; } -void blitz3d_open() { +void blitz3d_open() +{ gx_scene = gx_graphics->createScene(0); - if (!gx_scene) ThrowRuntimeException("Unable to create 3D Scene"); - world = new World(); - projected = Vector(); + if (!gx_scene) + ThrowRuntimeException("Unable to create 3D Scene"); + world = new World(); + projected = Vector(); picked.collision = Collision(); - picked.with = 0; picked.coords = Vector(); + picked.with = 0; + picked.coords = Vector(); Texture::clearFilters(); Texture::addFilter("", gxCanvas::CANVAS_TEX_RGB | gxCanvas::CANVAS_TEX_MIPMAP); loader_mat_map.clear(); - loader_mat_map["x"] = Transform(); + loader_mat_map["x"] = Transform(); loader_mat_map["3ds"] = Transform(Matrix(Vector(1, 0, 0), Vector(0, 0, 1), Vector(0, 1, 0))); - listener = 0; - stats_mode = false; + listener = 0; + stats_mode = false; } -void blitz3d_close() { - if (!gx_scene) return; +void blitz3d_close() +{ + if (!gx_scene) + return; bbClearWorld(1, 1, 1); Texture::clearFilters(); loader_mat_map.clear(); @@ -2353,18 +2790,22 @@ void blitz3d_close() { gx_scene = 0; } -bool blitz3d_create() { +bool blitz3d_create() +{ tri_count = 0; - gx_scene = 0; world = 0; + gx_scene = 0; + world = 0; return true; } -bool blitz3d_destroy() { +bool blitz3d_destroy() +{ blitz3d_close(); return true; } -void blitz3d_link(void(*rtSym)(const char *sym, void *pc)) { +void blitz3d_link(void (*rtSym)(const char* sym, void* pc)) +{ rtSym("LoaderMatrix$file_ext#xx#xy#xz#yx#yy#yz#zx#zy#zz", bbLoaderMatrix); rtSym("HWMultiTex%enable", bbHWMultiTex); rtSym("%HWTexUnits", bbHWTexUnits); diff --git a/Runtime/lib/bbblitz3d.hpp b/Runtime/lib/bbblitz3d.hpp index 259c34e..5706d21 100644 --- a/Runtime/lib/bbblitz3d.hpp +++ b/Runtime/lib/bbblitz3d.hpp @@ -3,9 +3,8 @@ #define BBBLITZ3D_H #include "bbsys.hpp" -#include "../gxruntime/gxscene.hpp" +#include "gxscene.hpp" -extern gxScene *gx_scene; +extern gxScene* gx_scene; #endif - diff --git a/Runtime/lib/bbfilesystem.cpp b/Runtime/lib/bbfilesystem.cpp index ad308d8..9ce4057 100644 --- a/Runtime/lib/bbfilesystem.cpp +++ b/Runtime/lib/bbfilesystem.cpp @@ -1,170 +1,204 @@ -#include "std.hpp" #include "bbfilesystem.hpp" -#include "bbstream.hpp" #include +#include "bbstream.hpp" +#include "std.hpp" -gxFileSystem *gx_filesys; +gxFileSystem* gx_filesys; -struct bbFile : public bbStream{ - filebuf *buf; - bbFile( filebuf *f ):buf(f){ - } - ~bbFile(){ +struct bbFile : public bbStream { + filebuf* buf; + bbFile(filebuf* f) : buf(f) {} + ~bbFile() + { delete buf; } - int read( char *buff,int size ){ - return buf->sgetn( (char*)buff,size ); + int read(char* buff, int size) + { + return buf->sgetn((char*)buff, size); } - int write( const char *buff,int size ){ - return buf->sputn( (char*)buff,size ); + int write(const char* buff, int size) + { + return buf->sputn((char*)buff, size); } - int avail(){ + int avail() + { return buf->in_avail(); } - int eof(){ - return buf->sgetc()==EOF; + int eof() + { + return buf->sgetc() == EOF; } }; static set file_set; -static inline void debugFile( bbFile *f ){ - if( debug ){ - if( !file_set.count( f ) ) ThrowRuntimeException( "File does not exist" ); +static inline void debugFile(bbFile* f) +{ + if (debug) { + if (!file_set.count(f)) + ThrowRuntimeException("File does not exist"); } } -static inline void debugDir( gxDir *d ){ - if( debug ){ - if( !gx_filesys->verifyDir( d ) ) ThrowRuntimeException( "Directory does not exist" ); +static inline void debugDir(gxDir* d) +{ + if (debug) { + if (!gx_filesys->verifyDir(d)) + ThrowRuntimeException("Directory does not exist"); } } -static bbFile *open( BBStr *f,int n ){ - string t=*f; - filebuf *buf=new filebuf(); - if( buf->open( t.c_str(),n|ios_base::binary ) ){ - bbFile *f=new bbFile( buf ); - file_set.insert( f ); - return f; +static bbFile* open(BBStr* file_path, int flags) +{ + string t = *file_path; + filebuf* buf = new filebuf(); + if (buf->open(t.c_str(), flags | ios_base::binary)) { + bbFile* file = new bbFile(buf); + file_set.insert(file); + return file; } delete buf; return 0; } -bbFile *bbReadFile( BBStr *f ){ - return open( f,ios_base::in ); +bbFile* bbReadFile(BBStr* f) +{ + return open(f, ios_base::in); } -bbFile *bbWriteFile( BBStr *f ){ - return open( f,ios_base::out|ios_base::trunc ); +bbFile* bbWriteFile(BBStr* f) +{ + return open(f, ios_base::out | ios_base::trunc); } -bbFile *bbOpenFile( BBStr *f ){ - return open( f,ios_base::in|ios_base::out ); +bbFile* bbOpenFile(BBStr* f) +{ + return open(f, ios_base::in | ios_base::out); } -void bbCloseFile( bbFile *f ){ - debugFile( f ); - file_set.erase( f ); +void bbCloseFile(bbFile* f) +{ + debugFile(f); + file_set.erase(f); delete f; } -int bbFilePos( bbFile *f ){ - return f->buf->pubseekoff( 0,ios_base::cur ); +int bbFilePos(bbFile* f) +{ + return f->buf->pubseekoff(0, ios_base::cur); } -int bbSeekFile( bbFile *f,int pos ){ - return f->buf->pubseekoff( pos,ios_base::beg ); +int bbSeekFile(bbFile* f, int pos) +{ + return f->buf->pubseekoff(pos, ios_base::beg); } -gxDir *bbReadDir( BBStr *d ){ - string t=*d;delete d; - return gx_filesys->openDir( t,0 ); +gxDir* bbReadDir(BBStr* d) +{ + string t = *d; + delete d; + return gx_filesys->openDir(t, 0); } -void bbCloseDir( gxDir *d ){ - gx_filesys->closeDir( d ); +void bbCloseDir(gxDir* d) +{ + gx_filesys->closeDir(d); } -BBStr *bbNextFile( gxDir *d ){ - debugDir( d ); - return new BBStr( d->getNextFile() ); +BBStr* bbNextFile(gxDir* d) +{ + debugDir(d); + return new BBStr(d->getNextFile()); } -BBStr *bbCurrentDir(){ - return new BBStr( gx_filesys->getCurrentDir() ); +BBStr* bbCurrentDir() +{ + return new BBStr(gx_filesys->getCurrentDir()); } -void bbChangeDir( BBStr *d ){ - gx_filesys->setCurrentDir( *d ); +void bbChangeDir(BBStr* d) +{ + gx_filesys->setCurrentDir(*d); delete d; } -void bbCreateDir( BBStr *d ){ - gx_filesys->createDir( *d ); +void bbCreateDir(BBStr* d) +{ + gx_filesys->createDir(*d); delete d; } -void bbDeleteDir( BBStr *d ){ - gx_filesys->deleteDir( *d ); +void bbDeleteDir(BBStr* d) +{ + gx_filesys->deleteDir(*d); delete d; } -int bbFileType( BBStr *f ){ - string t=*f;delete f; - int n=gx_filesys->getFileType( t ); - return n==gxFileSystem::FILE_TYPE_FILE ? 1 : (n==gxFileSystem::FILE_TYPE_DIR ? 2 : 0); +int bbFileType(BBStr* f) +{ + string t = *f; + delete f; + int n = gx_filesys->getFileType(t); + return n == gxFileSystem::FILE_TYPE_FILE ? 1 : (n == gxFileSystem::FILE_TYPE_DIR ? 2 : 0); } -int bbFileSize( BBStr *f ){ - string t=*f;delete f; - return gx_filesys->getFileSize( t ); +int bbFileSize(BBStr* f) +{ + string t = *f; + delete f; + return gx_filesys->getFileSize(t); } -void bbCopyFile( BBStr *f,BBStr *to ){ - string src=*f,dest=*to; - delete f;delete to; - gx_filesys->copyFile( src,dest ); +void bbCopyFile(BBStr* f, BBStr* to) +{ + string src = *f, dest = *to; + delete f; + delete to; + gx_filesys->copyFile(src, dest); } -void bbDeleteFile( BBStr *f ){ - gx_filesys->deleteFile( *f ); +void bbDeleteFile(BBStr* f) +{ + gx_filesys->deleteFile(*f); delete f; } -bool filesystem_create(){ - if( gx_filesys=gx_runtime->openFileSystem( 0 ) ){ +bool filesystem_create() +{ + if (gx_filesys = gx_runtime->openFileSystem(0)) { return true; } return false; } -bool filesystem_destroy(){ - while( file_set.size() ) bbCloseFile( *file_set.begin() ); - gx_runtime->closeFileSystem( gx_filesys ); +bool filesystem_destroy() +{ + while (file_set.size()) + bbCloseFile(*file_set.begin()); + gx_runtime->closeFileSystem(gx_filesys); return true; } -void filesystem_link( void(*rtSym)(const char*,void*) ){ - rtSym( "%OpenFile$filename",bbOpenFile ); - rtSym( "%ReadFile$filename",bbReadFile ); - rtSym( "%WriteFile$filename",bbWriteFile ); - rtSym( "CloseFile%file_stream",bbCloseFile ); - rtSym( "%FilePos%file_stream",bbFilePos ); - rtSym( "%SeekFile%file_stream%pos",bbSeekFile ); +void filesystem_link(void (*rtSym)(const char*, void*)) +{ + rtSym("%OpenFile$filename", bbOpenFile); + rtSym("%ReadFile$filename", bbReadFile); + rtSym("%WriteFile$filename", bbWriteFile); + rtSym("CloseFile%file_stream", bbCloseFile); + rtSym("%FilePos%file_stream", bbFilePos); + rtSym("%SeekFile%file_stream%pos", bbSeekFile); - rtSym( "%ReadDir$dirname",bbReadDir ); - rtSym( "CloseDir%dir",bbCloseDir ); - rtSym( "$NextFile%dir",bbNextFile ); - rtSym( "$CurrentDir",bbCurrentDir ); - rtSym( "ChangeDir$dir",bbChangeDir ); - rtSym( "CreateDir$dir",bbCreateDir ); - rtSym( "DeleteDir$dir",bbDeleteDir ); + rtSym("%ReadDir$dirname", bbReadDir); + rtSym("CloseDir%dir", bbCloseDir); + rtSym("$NextFile%dir", bbNextFile); + rtSym("$CurrentDir", bbCurrentDir); + rtSym("ChangeDir$dir", bbChangeDir); + rtSym("CreateDir$dir", bbCreateDir); + rtSym("DeleteDir$dir", bbDeleteDir); - rtSym( "%FileSize$file",bbFileSize ); - rtSym( "%FileType$file",bbFileType ); - rtSym( "CopyFile$file$to",bbCopyFile ); - rtSym( "DeleteFile$file",bbDeleteFile ); + rtSym("%FileSize$file", bbFileSize); + rtSym("%FileType$file", bbFileType); + rtSym("CopyFile$file$to", bbCopyFile); + rtSym("DeleteFile$file", bbDeleteFile); } diff --git a/Runtime/lib/bbfilesystem.hpp b/Runtime/lib/bbfilesystem.hpp index 3bf31f4..d197abb 100644 --- a/Runtime/lib/bbfilesystem.hpp +++ b/Runtime/lib/bbfilesystem.hpp @@ -3,8 +3,8 @@ #define BBFILESYSTEM_H #include "bbsys.hpp" -#include "../gxruntime/gxfilesystem.hpp" +#include "gxfilesystem.hpp" -extern gxFileSystem *gx_filesys; +extern gxFileSystem* gx_filesys; #endif \ No newline at end of file diff --git a/Runtime/lib/bbgraphics.cpp b/Runtime/lib/bbgraphics.cpp index dedce9a..8ac421b 100644 --- a/Runtime/lib/bbgraphics.cpp +++ b/Runtime/lib/bbgraphics.cpp @@ -1,84 +1,96 @@ -#include "std.hpp" #include "bbgraphics.hpp" #include "bbinput.hpp" +#include "std.hpp" -gxGraphics *gx_graphics; -gxCanvas *gx_canvas; +gxGraphics* gx_graphics; +gxCanvas* gx_canvas; struct GfxMode { int w, h, d, caps; }; class bbImage { -public: - bbImage(const vector &f) :frames(f) { - } - ~bbImage() { + public: + bbImage(const vector& f) : frames(f) {} + ~bbImage() + { for (int k = 0; k < frames.size(); ++k) { gx_graphics->freeCanvas(frames[k]); } } - const vector &getFrames()const { + const vector& getFrames() const + { return frames; } - void replaceFrame(int n, gxCanvas *c) { + void replaceFrame(int n, gxCanvas* c) + { gx_graphics->freeCanvas(frames[n]); frames[n] = c; } -private: + + private: vector frames; }; //degrees to radians static const float s_degreesToRadians = 0.0174532925199432957692369076848861f; -static int gx_driver; //current graphics driver +static int gx_driver; //current graphics driver -static bool filter; -static bool auto_dirty; -static bool auto_midhandle; +static bool filter; +static bool auto_dirty; +static bool auto_midhandle; static set image_set; -static int curs_x, curs_y; -static gxCanvas *p_canvas; +static int curs_x, curs_y; +static gxCanvas* p_canvas; -static gxFont *curr_font; +static gxFont* curr_font; static unsigned curr_color; static unsigned curr_clsColor; static vector gfx_modes; #ifdef _DEBUG -static inline void debugImage(bbImage *i, int frame = 0) { +static inline void debugImage(bbImage* i, int frame = 0) +{ if (debug) { - if (!image_set.count(i)) ThrowRuntimeException("Image does not exist"); - if (frame >= i->getFrames().size()) ThrowRuntimeException("Image frame out of range"); + if (!image_set.count(i)) + ThrowRuntimeException("Image does not exist"); + if (frame >= i->getFrames().size()) + ThrowRuntimeException("Image frame out of range"); } } -static inline void debugFont(gxFont *f) { +static inline void debugFont(gxFont* f) +{ if (debug) { - if (!gx_graphics->verifyFont(f)) ThrowRuntimeException("Font does not exist"); + if (!gx_graphics->verifyFont(f)) + ThrowRuntimeException("Font does not exist"); } } -static inline void debugCanvas(gxCanvas *c) { +static inline void debugCanvas(gxCanvas* c) +{ if (debug) { - if (!gx_graphics->verifyCanvas(c)) ThrowRuntimeException("Buffer does not exist"); + if (!gx_graphics->verifyCanvas(c)) + ThrowRuntimeException("Buffer does not exist"); } } -static inline void debugDriver(int n) { +static inline void debugDriver(int n) +{ if (debug) { - if (n<1 || n>gx_runtime->enumerateGraphicsDrivers()) { + if (n < 1 || n > gx_runtime->enumerateGraphicsDrivers()) { ThrowRuntimeException("Illegal graphics driver index"); } } } -static inline void debugMode(int n) { +static inline void debugMode(int n) +{ if (debug) { - if (n<1 || n>gfx_modes.size()) { + if (n < 1 || n > gfx_modes.size()) { ThrowRuntimeException("Illegal graphics mode index"); } } @@ -88,82 +100,118 @@ static inline void debugMode(int n) { #define debugFont #define debugCanvas #define debugDriver -#define debugMode +#define debugMode #endif -void bbFreeImage(bbImage *i); +void bbFreeImage(bbImage* i); -static void freeGraphics() { +static void freeGraphics() +{ extern void blitz3d_close(); blitz3d_close(); - while (image_set.size()) bbFreeImage(*image_set.begin()); + while (image_set.size()) + bbFreeImage(*image_set.begin()); if (p_canvas) { gx_graphics->freeCanvas(p_canvas); p_canvas = 0; } } -#define RED(_X_) ( ((_X_)>>16) & 0xff ) -#define GRN(_X_) ( ((_X_)>>8) & 0xff ) -#define BLU(_X_) ( (_X_) & 0xff ) +#define RED(_X_) (((_X_) >> 16) & 0xff) +#define GRN(_X_) (((_X_) >> 8) & 0xff) +#define BLU(_X_) ((_X_)&0xff) -static int getPixel(gxCanvas *c, float x, float y) { +static int getPixel(gxCanvas* c, float x, float y) +{ debugCanvas(c); - x -= .5f; y -= .5f; + x -= .5f; + y -= .5f; float fx = floor(x), fy = floor(y); - int ix = fx, iy = fy; fx = x - fx; fy = y - fy; + int ix = fx, iy = fy; + fx = x - fx; + fy = y - fy; int tl = c->getPixel(ix, iy); int tr = c->getPixel(ix + 1, iy); int br = c->getPixel(ix + 1, iy + 1); int bl = c->getPixel(ix, iy + 1); - float w1 = (1 - fx)*(1 - fy), w2 = fx*(1 - fy), w3 = (1 - fx)*fy, w4 = fx*fy; + float w1 = (1 - fx) * (1 - fy), w2 = fx * (1 - fy), w3 = (1 - fx) * fy, w4 = fx * fy; - float r = RED(tl)*w1 + RED(tr)*w2 + RED(bl)*w3 + RED(br)*w4; - float g = GRN(tl)*w1 + GRN(tr)*w2 + GRN(bl)*w3 + GRN(br)*w4; - float b = BLU(tl)*w1 + BLU(tr)*w2 + BLU(bl)*w3 + BLU(br)*w4; + float r = RED(tl) * w1 + RED(tr) * w2 + RED(bl) * w3 + RED(br) * w4; + float g = GRN(tl) * w1 + GRN(tr) * w2 + GRN(bl) * w3 + GRN(br) * w4; + float b = BLU(tl) * w1 + BLU(tr) * w2 + BLU(bl) * w3 + BLU(br) * w4; return (int(r + .5f) << 16) | (int(g + .5f) << 8) | int(b + .5f); } -struct vec2 { float x, y; }; +struct vec2 { + float x, y; +}; -static vec2 vrot(float m[2][2], const vec2 &v) { - vec2 t; t.x = m[0][0] * v.x + m[0][1] * v.y; t.y = m[1][0] * v.x + m[1][1] * v.y; +static vec2 vrot(float m[2][2], const vec2& v) +{ + vec2 t; + t.x = m[0][0] * v.x + m[0][1] * v.y; + t.y = m[1][0] * v.x + m[1][1] * v.y; return t; } -static float vmin(float a, float b, float c, float d) { - float t = a; if (b < t) t = b; if (c < t) t = c; if (d < t) t = d; return t; +static float vmin(float a, float b, float c, float d) +{ + float t = a; + if (b < t) + t = b; + if (c < t) + t = c; + if (d < t) + t = d; + return t; } -static float vmax(float a, float b, float c, float d) { - float t = a; if (b > t) t = b; if (c > t) t = c; if (d > t) t = d; return t; +static float vmax(float a, float b, float c, float d) +{ + float t = a; + if (b > t) + t = b; + if (c > t) + t = c; + if (d > t) + t = d; + return t; } -static gxCanvas *tformCanvas(gxCanvas *c, float m[2][2], int x_handle, int y_handle) { - - vec2 v, v0, v1, v2, v3; +static gxCanvas* tformCanvas(gxCanvas* c, float m[2][2], int x_handle, int y_handle) +{ + vec2 v, v0, v1, v2, v3; float i[2][2]; float dt = 1.0f / (m[0][0] * m[1][1] - m[1][0] * m[0][1]); - i[0][0] = dt*m[1][1]; i[1][0] = -dt*m[1][0]; - i[0][1] = -dt*m[0][1]; i[1][1] = dt*m[0][0]; + i[0][0] = dt * m[1][1]; + i[1][0] = -dt * m[1][0]; + i[0][1] = -dt * m[0][1]; + i[1][1] = dt * m[0][0]; float ox = x_handle, oy = y_handle; - v0.x = -ox; v0.y = -oy; //tl - v1.x = c->getWidth() - ox; v1.y = -oy; //tr - v2.x = c->getWidth() - ox; v2.y = c->getHeight() - oy; //br - v3.x = -ox; v3.y = c->getHeight() - oy; //bl - v0 = vrot(m, v0); v1 = vrot(m, v1); v2 = vrot(m, v2); v3 = vrot(m, v3); + v0.x = -ox; + v0.y = -oy; //tl + v1.x = c->getWidth() - ox; + v1.y = -oy; //tr + v2.x = c->getWidth() - ox; + v2.y = c->getHeight() - oy; //br + v3.x = -ox; + v3.y = c->getHeight() - oy; //bl + v0 = vrot(m, v0); + v1 = vrot(m, v1); + v2 = vrot(m, v2); + v3 = vrot(m, v3); float minx = floor(vmin(v0.x, v1.x, v2.x, v3.x)); float miny = floor(vmin(v0.y, v1.y, v2.y, v3.y)); float maxx = ceil(vmax(v0.x, v1.x, v2.x, v3.x)); float maxy = ceil(vmax(v0.y, v1.y, v2.y, v3.y)); - int iw = maxx - minx, ih = maxy - miny; + int iw = maxx - minx, ih = maxy - miny; - gxCanvas *t = gx_graphics->createCanvas(iw, ih, 0); + gxCanvas* t = gx_graphics->createCanvas(iw, ih, 0); t->setHandle(-minx, -miny); t->setMask(c->getMask()); @@ -174,7 +222,7 @@ static gxCanvas *tformCanvas(gxCanvas *c, float m[2][2], int x_handle, int y_han for (int y = 0; y < ih; ++v.y, ++y) { v.x = minx + .5f; for (int x = 0; x < iw; ++v.x, ++x) { - vec2 q = vrot(i, v); + vec2 q = vrot(i, v); unsigned rgb = filter ? getPixel(c, q.x + ox, q.y + oy) : c->getPixel(floor(q.x + ox), floor(q.y + oy)); t->setPixel(x, y, rgb); } @@ -186,38 +234,40 @@ static gxCanvas *tformCanvas(gxCanvas *c, float m[2][2], int x_handle, int y_han return t; } -static bool saveCanvas(gxCanvas *c, const string &f) { - +static bool saveCanvas(gxCanvas* c, const string& f) +{ ofstream out(f.c_str(), ios::binary); - if (!out.good()) return false; + if (!out.good()) + return false; - int tempsize = (c->getWidth() * 3 + 3)&~3; + int tempsize = (c->getWidth() * 3 + 3) & ~3; BITMAPFILEHEADER bf; memset(&bf, 0, sizeof(bf)); - bf.bfType = 'MB'; - bf.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + tempsize*c->getHeight(); + bf.bfType = 'MB'; + bf.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + tempsize * c->getHeight(); bf.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); - BITMAPINFOHEADER bi; memset(&bi, 0, sizeof(bi)); - bi.biSize = sizeof(bi); - bi.biWidth = c->getWidth(); - bi.biHeight = c->getHeight(); - bi.biPlanes = 1; + BITMAPINFOHEADER bi; + memset(&bi, 0, sizeof(bi)); + bi.biSize = sizeof(bi); + bi.biWidth = c->getWidth(); + bi.biHeight = c->getHeight(); + bi.biPlanes = 1; bi.biBitCount = 24; out.write((char*)&bf, sizeof(bf)); out.write((char*)&bi, sizeof(bi)); - unsigned char *temp = new unsigned char[tempsize]; + unsigned char* temp = new unsigned char[tempsize]; memset(temp, 0, tempsize); c->lock(); for (int y = c->getHeight() - 1; y >= 0; --y) { - unsigned char *dest = temp; + unsigned char* dest = temp; for (int x = 0; x < c->getWidth(); ++x) { unsigned rgb = c->getPixelFast(x, y); - *dest++ = rgb & 0xff; - *dest++ = (rgb >> 8) & 0xff; - *dest++ = (rgb >> 16) & 0xff; + *dest++ = rgb & 0xff; + *dest++ = (rgb >> 8) & 0xff; + *dest++ = (rgb >> 16) & 0xff; } out.write((char*)temp, tempsize); } @@ -228,24 +278,29 @@ static bool saveCanvas(gxCanvas *c, const string &f) { return out.good(); } -int bbCountGfxDrivers() { +int bbCountGfxDrivers() +{ return gx_runtime->enumerateGraphicsDrivers(); } -BBStr * bbGfxDriverName(int n) { +BBStr* bbGfxDriverName(int n) +{ debugDriver(n); - string t; int caps; + string t; + int caps; gx_runtime->graphicsDriverInfo(n - 1, &t, &caps); return new BBStr(t); } -void bbSetGfxDriver(int n) { +void bbSetGfxDriver(int n) +{ debugDriver(n); gfx_modes.clear(); gx_driver = n - 1; } -int bbCountGfxModes() { +int bbCountGfxModes() +{ gfx_modes.clear(); int n = gx_runtime->countGraphicsModes(gx_driver); for (int k = 0; k < n; ++k) { @@ -256,78 +311,95 @@ int bbCountGfxModes() { return gfx_modes.size(); } -int bbGfxModeWidth(int n) { +int bbGfxModeWidth(int n) +{ debugMode(n); return gfx_modes[n - 1].w; } -int bbGfxModeHeight(int n) { +int bbGfxModeHeight(int n) +{ debugMode(n); return gfx_modes[n - 1].h; } -int bbGfxModeDepth(int n) { +int bbGfxModeDepth(int n) +{ debugMode(n); return gfx_modes[n - 1].d; } -static int modeExists(int w, int h, int d, bool bb3d) { +static int modeExists(int w, int h, int d, bool bb3d) +{ int cnt = gx_runtime->countGraphicsModes(gx_driver); for (int k = 0; k < cnt; ++k) { int tw, th, td, tc; gx_runtime->graphicsModeInfo(gx_driver, k, &tw, &th, &td, &tc); - if (bb3d && !(tc&gxRuntime::GFXMODECAPS_3D)) continue; - if (w == tw && h == th && d == td) return 1; + if (bb3d && !(tc & gxRuntime::GFXMODECAPS_3D)) + continue; + if (w == tw && h == th && d == td) + return 1; } return 0; } -int bbGfxModeExists(int w, int h, int d) { +int bbGfxModeExists(int w, int h, int d) +{ return modeExists(w, h, d, false); } -int bbGfxDriver3D(int n) { +int bbGfxDriver3D(int n) +{ debugDriver(n); - string t; int caps; + string t; + int caps; gx_runtime->graphicsDriverInfo(n - 1, &t, &caps); return (caps & gxRuntime::GFXMODECAPS_3D) ? 1 : 0; } -int bbCountGfxModes3D() { +int bbCountGfxModes3D() +{ gfx_modes.clear(); int n = gx_runtime->countGraphicsModes(gx_driver); for (int k = 0; k < n; ++k) { GfxMode m; gx_runtime->graphicsModeInfo(gx_driver, k, &m.w, &m.h, &m.d, &m.caps); - if (m.caps & gxRuntime::GFXMODECAPS_3D) gfx_modes.push_back(m); + if (m.caps & gxRuntime::GFXMODECAPS_3D) + gfx_modes.push_back(m); } return gfx_modes.size(); } -int bbGfxMode3DExists(int w, int h, int d) { +int bbGfxMode3DExists(int w, int h, int d) +{ return modeExists(w, h, d, true); } -int bbGfxMode3D(int n) { +int bbGfxMode3D(int n) +{ debugMode(n); return gfx_modes[n - 1].caps & gxRuntime::GFXMODECAPS_3D ? 1 : 0; } -int bbWindowed3D() { +int bbWindowed3D() +{ int tc; gx_runtime->windowedModeInfo(&tc); return (tc & gxRuntime::GFXMODECAPS_3D) ? 1 : 0; } -int bbTotalVidMem() { +int bbTotalVidMem() +{ return gx_graphics->getTotalVidmem(); } -int bbAvailVidMem() { +int bbAvailVidMem() +{ return gx_graphics->getAvailVidmem(); } -void bbSetBuffer(gxCanvas *buff) { +void bbSetBuffer(gxCanvas* buff) +{ debugCanvas(buff); gx_canvas = buff; curs_x = curs_y = 0; @@ -361,367 +433,485 @@ void bbSetBuffer(gxCanvas *buff) { //} } -gxCanvas *bbGraphicsBuffer() { +gxCanvas* bbGraphicsBuffer() +{ return gx_canvas; } -int bbLoadBuffer(gxCanvas *c, BBStr *str) { +int bbLoadBuffer(gxCanvas* c, BBStr* str) +{ debugCanvas(c); - string s = *str; delete str; - gxCanvas *t = gx_graphics->loadCanvas(s, 0); - if (!t) return 0; + string s = *str; + delete str; + gxCanvas* t = gx_graphics->loadCanvas(s, 0); + if (!t) + return 0; float m[2][2]; m[0][0] = (float)c->getWidth() / (float)t->getWidth(); m[1][1] = (float)c->getHeight() / (float)t->getHeight(); m[1][0] = m[0][1] = 0; - gxCanvas *p = tformCanvas(t, m, 0, 0); + gxCanvas* p = tformCanvas(t, m, 0, 0); gx_graphics->freeCanvas(t); int ox, oy; - c->getOrigin(&ox, &oy); c->setOrigin(0, 0); + c->getOrigin(&ox, &oy); + c->setOrigin(0, 0); c->blit(0, 0, p, 0, 0, p->getWidth(), p->getHeight(), true); gx_graphics->freeCanvas(p); return 1; } -int bbSaveBuffer(gxCanvas *c, BBStr *str) { +int bbSaveBuffer(gxCanvas* c, BBStr* str) +{ debugCanvas(c); - string t = *str; delete str; + string t = *str; + delete str; return saveCanvas(c, t) ? 1 : 0; } -void bbBufferDirty(gxCanvas *c) { +void bbBufferDirty(gxCanvas* c) +{ debugCanvas(c); c->backup(); } -static void graphics(int w, int h, int d, int flags) { +static void graphics(int w, int h, int d, int flags) +{ freeGraphics(); gx_runtime->closeGraphics(gx_graphics); gx_graphics = gx_runtime->openGraphics(w, h, d, gx_driver, flags); - if (!gx_runtime->idle()) ThrowRuntimeException(0); + if (!gx_runtime->idle()) + ThrowRuntimeException(0); if (!gx_graphics) { ThrowRuntimeException("Unable to set graphics mode"); } - curr_clsColor = 0; - curr_color = 0xffffffff; - curr_font = gx_graphics->getDefaultFont(); - gxCanvas *buff = (flags & gxGraphics::GRAPHICS_3D) ? - gx_graphics->getBackCanvas() : gx_graphics->getFrontCanvas(); + curr_clsColor = 0; + curr_color = 0xffffffff; + curr_font = gx_graphics->getDefaultFont(); + gxCanvas* buff = (flags & gxGraphics::GRAPHICS_3D) ? gx_graphics->getBackCanvas() : gx_graphics->getFrontCanvas(); bbSetBuffer(buff); } -void bbGraphics(int w, int h, int d, int mode) { +void bbGraphics(int w, int h, int d, int mode) +{ int flags = 0; switch (mode) { - case 0:flags |= debug ? gxGraphics::GRAPHICS_WINDOWED : 0; break; - case 1:break; - case 2:flags |= gxGraphics::GRAPHICS_WINDOWED; break; - case 3:flags |= gxGraphics::GRAPHICS_WINDOWED | gxGraphics::GRAPHICS_SCALED; break; - case 6:flags |= gxGraphics::GRAPHICS_WINDOWED | gxGraphics::GRAPHICS_AUTOSUSPEND; break; - case 7:flags |= gxGraphics::GRAPHICS_WINDOWED | gxGraphics::GRAPHICS_SCALED | gxGraphics::GRAPHICS_AUTOSUSPEND; break; - default:ThrowRuntimeException("Illegal Graphics mode"); + case 0: + flags |= debug ? gxGraphics::GRAPHICS_WINDOWED : 0; + break; + case 1: + break; + case 2: + flags |= gxGraphics::GRAPHICS_WINDOWED; + break; + case 3: + flags |= gxGraphics::GRAPHICS_WINDOWED | gxGraphics::GRAPHICS_SCALED; + break; + case 6: + flags |= gxGraphics::GRAPHICS_WINDOWED | gxGraphics::GRAPHICS_AUTOSUSPEND; + break; + case 7: + flags |= gxGraphics::GRAPHICS_WINDOWED | gxGraphics::GRAPHICS_SCALED | gxGraphics::GRAPHICS_AUTOSUSPEND; + break; + default: + ThrowRuntimeException("Illegal Graphics mode"); } graphics(w, h, d, flags); } -void bbGraphics3D(int w, int h, int d, int mode) { +void bbGraphics3D(int w, int h, int d, int mode) +{ int flags = gxGraphics::GRAPHICS_3D; switch (mode) { - case 0:flags |= (debug && bbWindowed3D()) ? gxGraphics::GRAPHICS_WINDOWED : 0; break; - case 1:break; - case 2:flags |= gxGraphics::GRAPHICS_WINDOWED; break; - case 3:flags |= gxGraphics::GRAPHICS_WINDOWED | gxGraphics::GRAPHICS_SCALED; break; - case 6:flags |= gxGraphics::GRAPHICS_WINDOWED | gxGraphics::GRAPHICS_AUTOSUSPEND; break; - case 7:flags |= gxGraphics::GRAPHICS_WINDOWED | gxGraphics::GRAPHICS_SCALED | gxGraphics::GRAPHICS_AUTOSUSPEND; break; - default:ThrowRuntimeException("Illegal Graphics3D mode"); + case 0: + flags |= (debug && bbWindowed3D()) ? gxGraphics::GRAPHICS_WINDOWED : 0; + break; + case 1: + break; + case 2: + flags |= gxGraphics::GRAPHICS_WINDOWED; + break; + case 3: + flags |= gxGraphics::GRAPHICS_WINDOWED | gxGraphics::GRAPHICS_SCALED; + break; + case 6: + flags |= gxGraphics::GRAPHICS_WINDOWED | gxGraphics::GRAPHICS_AUTOSUSPEND; + break; + case 7: + flags |= gxGraphics::GRAPHICS_WINDOWED | gxGraphics::GRAPHICS_SCALED | gxGraphics::GRAPHICS_AUTOSUSPEND; + break; + default: + ThrowRuntimeException("Illegal Graphics3D mode"); } graphics(w, h, d, flags); extern void blitz3d_open(); blitz3d_open(); } -void bbEndGraphics() { +void bbEndGraphics() +{ freeGraphics(); gx_runtime->closeGraphics(gx_graphics); gx_graphics = gx_runtime->openGraphics(400, 300, 0, 0, gxGraphics::GRAPHICS_WINDOWED); - if (!gx_runtime->idle()) ThrowRuntimeException(0); + if (!gx_runtime->idle()) + ThrowRuntimeException(0); if (gx_graphics) { curr_clsColor = 0; - curr_color = 0xffffffff; - curr_font = gx_graphics->getDefaultFont(); + curr_color = 0xffffffff; + curr_font = gx_graphics->getDefaultFont(); bbSetBuffer(gx_graphics->getFrontCanvas()); return; } ThrowRuntimeException("Unable to set graphics mode"); } -int bbGraphicsLost() { +int bbGraphicsLost() +{ return gx_runtime->graphicsLost(); } -void bbSetGamma(int r, int g, int b, float dr, float dg, float db) { - if (dr < 0) dr = 0; - else if (dr > 255.0f) dr = 255.0f; - if (dg < 0) dg = 0; - else if (dg > 255.0f) dg = 255.0f; - if (db < 0) db = 0; - else if (db > 255.0f) db = 255.0f; +void bbSetGamma(int r, int g, int b, float dr, float dg, float db) +{ + if (dr < 0) + dr = 0; + else if (dr > 255.0f) + dr = 255.0f; + if (dg < 0) + dg = 0; + else if (dg > 255.0f) + dg = 255.0f; + if (db < 0) + db = 0; + else if (db > 255.0f) + db = 255.0f; gx_graphics->setGamma(r, g, b, dr, dg, db); } -void bbUpdateGamma(int calibrate) { +void bbUpdateGamma(int calibrate) +{ gx_graphics->updateGamma(!!calibrate); } -float bbGammaRed(int n) { +float bbGammaRed(int n) +{ float dr, dg, db; gx_graphics->getGamma(n, n, n, &dr, &dg, &db); return dr; } -float bbGammaGreen(int n) { +float bbGammaGreen(int n) +{ float dr, dg, db; gx_graphics->getGamma(n, n, n, &dr, &dg, &db); return dg; } -float bbGammaBlue(int n) { +float bbGammaBlue(int n) +{ float dr, dg, db; gx_graphics->getGamma(n, n, n, &dr, &dg, &db); return db; } -gxCanvas *bbFrontBuffer() { +gxCanvas* bbFrontBuffer() +{ return gx_graphics->getFrontCanvas(); } -gxCanvas *bbBackBuffer() { +gxCanvas* bbBackBuffer() +{ return gx_graphics->getBackCanvas(); } -void bbLockBuffer(gxCanvas *buff) { - if (buff) debugCanvas(buff); +void bbLockBuffer(gxCanvas* buff) +{ + if (buff) + debugCanvas(buff); (buff ? buff : gx_canvas)->lock(); } -void bbUnlockBuffer(gxCanvas *buff) { - if (buff) debugCanvas(buff); +void bbUnlockBuffer(gxCanvas* buff) +{ + if (buff) + debugCanvas(buff); (buff ? buff : gx_canvas)->unlock(); } -int bbReadPixel(int x, int y, gxCanvas *buff) { - if (buff) debugCanvas(buff); +int bbReadPixel(int x, int y, gxCanvas* buff) +{ + if (buff) + debugCanvas(buff); return (buff ? buff : gx_canvas)->getPixel(x, y); } -void bbWritePixel(int x, int y, int argb, gxCanvas *buff) { - if (buff) debugCanvas(buff); +void bbWritePixel(int x, int y, int argb, gxCanvas* buff) +{ + if (buff) + debugCanvas(buff); (buff ? buff : gx_canvas)->setPixel(x, y, argb); } -int bbReadPixelFast(int x, int y, gxCanvas *buff) { +int bbReadPixelFast(int x, int y, gxCanvas* buff) +{ return (buff ? buff : gx_canvas)->getPixelFast(x, y); } -void bbWritePixelFast(int x, int y, int argb, gxCanvas *buff) { +void bbWritePixelFast(int x, int y, int argb, gxCanvas* buff) +{ (buff ? buff : gx_canvas)->setPixelFast(x, y, argb); } -void bbCopyPixel(int src_x, int src_y, gxCanvas *src, int dest_x, int dest_y, gxCanvas *buff) { +void bbCopyPixel(int src_x, int src_y, gxCanvas* src, int dest_x, int dest_y, gxCanvas* buff) +{ (buff ? buff : gx_canvas)->copyPixel(dest_x, dest_y, src ? src : gx_canvas, src_x, src_y); } -void bbCopyPixelFast(int src_x, int src_y, gxCanvas *src, int dest_x, int dest_y, gxCanvas *buff) { +void bbCopyPixelFast(int src_x, int src_y, gxCanvas* src, int dest_x, int dest_y, gxCanvas* buff) +{ (buff ? buff : gx_canvas)->copyPixelFast(dest_x, dest_y, src ? src : gx_canvas, src_x, src_y); } -int bbScanLine() { +int bbScanLine() +{ return gx_graphics->getScanLine(); } -void bbVWait(int n) { +void bbVWait(int n) +{ gx_graphics->vwait(); - if (!gx_runtime->idle()) ThrowRuntimeException(0); + if (!gx_runtime->idle()) + ThrowRuntimeException(0); } -void bbFlip(int vwait) { +void bbFlip(int vwait) +{ gx_graphics->flip(vwait ? true : false); - if (!gx_runtime->idle()) ThrowRuntimeException(0); + if (!gx_runtime->idle()) + ThrowRuntimeException(0); } -int bbGraphicsWidth() { +int bbGraphicsWidth() +{ return gx_graphics->getWidth(); } -int bbGraphicsHeight() { +int bbGraphicsHeight() +{ return gx_graphics->getHeight(); } -int bbGraphicsDepth() { +int bbGraphicsDepth() +{ return gx_graphics->getDepth(); } -void bbOrigin(int x, int y) { +void bbOrigin(int x, int y) +{ gx_canvas->setOrigin(x, y); } -void bbViewport(int x, int y, int w, int h) { +void bbViewport(int x, int y, int w, int h) +{ gx_canvas->setViewport(x, y, w, h); } -void bbColor(int r, int g, int b) { +void bbColor(int r, int g, int b) +{ gx_canvas->setColor(curr_color = (r << 16) | (g << 8) | b); } -void bbGetColor(int x, int y) { +void bbGetColor(int x, int y) +{ gx_canvas->setColor(curr_color = gx_canvas->getPixel(x, y)); } -int bbColorRed() { +int bbColorRed() +{ return (gx_canvas->getColor() >> 16) & 0xff; } -int bbColorGreen() { +int bbColorGreen() +{ return (gx_canvas->getColor() >> 8) & 0xff; } -int bbColorBlue() { +int bbColorBlue() +{ return gx_canvas->getColor() & 0xff; } -void bbClsColor(int r, int g, int b) { +void bbClsColor(int r, int g, int b) +{ gx_canvas->setClsColor(curr_clsColor = (r << 16) | (g << 8) | b); } -void bbSetFont(gxFont *f) { +void bbSetFont(gxFont* f) +{ debugFont(f); gx_canvas->setFont(curr_font = f); } -void bbCls() { +void bbCls() +{ gx_canvas->cls(); } -void bbPlot(int x, int y) { +void bbPlot(int x, int y) +{ gx_canvas->plot(x, y); } -void bbLine(int x1, int y1, int x2, int y2) { +void bbLine(int x1, int y1, int x2, int y2) +{ gx_canvas->line(x1, y1, x2, y2); } -void bbRect(int x, int y, int w, int h, int solid) { +void bbRect(int x, int y, int w, int h, int solid) +{ gx_canvas->rect(x, y, w, h, solid ? true : false); } -void bbOval(int x, int y, int w, int h, int solid) { +void bbOval(int x, int y, int w, int h, int solid) +{ gx_canvas->oval(x, y, w, h, solid ? true : false); } -void bbText(int x, int y, BBStr *str, int centre_x, int centre_y) { - if (centre_x) x -= curr_font->getWidth(*str) / 2; - if (centre_y) y -= curr_font->getHeight() / 2; +void bbText(int x, int y, BBStr* str, int centre_x, int centre_y) +{ + if (centre_x) + x -= curr_font->getWidth(*str) / 2; + if (centre_y) + y -= curr_font->getHeight() / 2; gx_canvas->text(x, y, *str); delete str; } -void bbCopyRect(int sx, int sy, int w, int h, int dx, int dy, gxCanvas *src, gxCanvas *dest) { - if (src) debugCanvas(src); - else src = gx_canvas; - if (dest) debugCanvas(dest); - else dest = gx_canvas; +void bbCopyRect(int sx, int sy, int w, int h, int dx, int dy, gxCanvas* src, gxCanvas* dest) +{ + if (src) + debugCanvas(src); + else + src = gx_canvas; + if (dest) + debugCanvas(dest); + else + dest = gx_canvas; dest->blit(dx, dy, src, sx, sy, w, h, true); } -gxFont *bbLoadFont(BBStr *name, int height, int bold, int italic, int underline) { +gxFont* bbLoadFont(BBStr* name, int height, int bold, int italic, int underline) +{ int flags = - (bold ? gxFont::FONT_BOLD : 0) | - (italic ? gxFont::FONT_ITALIC : 0) | - (underline ? gxFont::FONT_UNDERLINE : 0); - gxFont *font = gx_graphics->loadFont(*name, height, flags); + (bold ? gxFont::FONT_BOLD : 0) | (italic ? gxFont::FONT_ITALIC : 0) | (underline ? gxFont::FONT_UNDERLINE : 0); + gxFont* font = gx_graphics->loadFont(*name, height, flags); delete name; return font; } -void bbFreeFont(gxFont *f) { +void bbFreeFont(gxFont* f) +{ debugFont(f); - if (f == curr_font) bbSetFont(gx_graphics->getDefaultFont()); + if (f == curr_font) + bbSetFont(gx_graphics->getDefaultFont()); gx_graphics->freeFont(f); } -int bbFontWidth() { +int bbFontWidth() +{ return curr_font->getWidth(); } -int bbFontHeight() { +int bbFontHeight() +{ return curr_font->getHeight(); } -int bbStringWidth(BBStr *str) { - string t = *str; delete str; +int bbStringWidth(BBStr* str) +{ + string t = *str; + delete str; return curr_font->getWidth(t); } -int bbStringHeight(BBStr *str) { +int bbStringHeight(BBStr* str) +{ delete str; return curr_font->getHeight(); } -gxMovie *bbOpenMovie(BBStr *s) { - gxMovie *movie = gx_graphics->openMovie(*s, 0); delete s; +gxMovie* bbOpenMovie(BBStr* s) +{ + gxMovie* movie = gx_graphics->openMovie(*s, 0); + delete s; return movie; } -int bbDrawMovie(gxMovie *movie, int x, int y, int w, int h) { - if (w < 0) w = movie->getWidth(); - if (h < 0) h = movie->getHeight(); +int bbDrawMovie(gxMovie* movie, int x, int y, int w, int h) +{ + if (w < 0) + w = movie->getWidth(); + if (h < 0) + h = movie->getHeight(); int playing = movie->draw(gx_canvas, x, y, w, h); - if (!gx_runtime->idle()) ThrowRuntimeException(0); + if (!gx_runtime->idle()) + ThrowRuntimeException(0); return playing; } -int bbMovieWidth(gxMovie *movie) { +int bbMovieWidth(gxMovie* movie) +{ return movie->getWidth(); } -int bbMovieHeight(gxMovie *movie) { +int bbMovieHeight(gxMovie* movie) +{ return movie->getHeight(); } -int bbMoviePlaying(gxMovie *movie) { +int bbMoviePlaying(gxMovie* movie) +{ return movie->isPlaying(); } -void bbCloseMovie(gxMovie *movie) { +void bbCloseMovie(gxMovie* movie) +{ gx_graphics->closeMovie(movie); } -bbImage *bbLoadImage(BBStr *s) { - string t = *s; delete s; - gxCanvas *c = gx_graphics->loadCanvas(t, 0); - if (!c) return 0; - if (auto_dirty) c->backup(); - if (auto_midhandle) c->setHandle(c->getWidth() / 2, c->getHeight() / 2); +bbImage* bbLoadImage(BBStr* s) +{ + string t = *s; + delete s; + gxCanvas* c = gx_graphics->loadCanvas(t, 0); + if (!c) + return 0; + if (auto_dirty) + c->backup(); + if (auto_midhandle) + c->setHandle(c->getWidth() / 2, c->getHeight() / 2); vector frames; frames.push_back(c); - bbImage *i = new bbImage(frames); + bbImage* i = new bbImage(frames); image_set.insert(i); return i; } -bbImage *bbLoadAnimImage(BBStr *s, int w, int h, int first, int cnt) { +bbImage* bbLoadAnimImage(BBStr* s, int w, int h, int first, int cnt) +{ + string t = *s; + delete s; - string t = *s; delete s; + if (cnt < 1) + ThrowRuntimeException("Illegal frame count"); + if (first < 0) + ThrowRuntimeException("Illegal first frame"); - if (cnt < 1) ThrowRuntimeException("Illegal frame count"); - if (first < 0) ThrowRuntimeException("Illegal first frame"); - - gxCanvas *pic = gx_graphics->loadCanvas(t, gxCanvas::CANVAS_NONDISPLAY); - if (!pic) return 0; + gxCanvas* pic = gx_graphics->loadCanvas(t, gxCanvas::CANVAS_NONDISPLAY); + if (!pic) + return 0; //frames per row, per picture int fpr = pic->getWidth() / w; - int fpp = pic->getHeight() / h*fpr; + int fpp = pic->getHeight() / h * fpr; if (first + cnt > fpp) { gx_graphics->freeCanvas(pic); ThrowRuntimeException("Not enough frames in bitmap"); @@ -729,72 +919,89 @@ bbImage *bbLoadAnimImage(BBStr *s, int w, int h, int first, int cnt) { //x,y of first frame... vector frames; - int src_x = first%fpr*w, src_y = first / fpr*h; + int src_x = first % fpr * w, src_y = first / fpr * h; for (int k = 0; k < cnt; ++k) { - gxCanvas *c = gx_graphics->createCanvas(w, h, 0); + gxCanvas* c = gx_graphics->createCanvas(w, h, 0); if (!c) { - for (--k; k >= 0; --k) gx_graphics->freeCanvas(frames[k]); - gx_graphics->freeCanvas(pic); return 0; + for (--k; k >= 0; --k) + gx_graphics->freeCanvas(frames[k]); + gx_graphics->freeCanvas(pic); + return 0; } c->blit(0, 0, pic, src_x, src_y, w, h, true); - if (auto_dirty) c->backup(); - if (auto_midhandle) c->setHandle(c->getWidth() / 2, c->getHeight() / 2); + if (auto_dirty) + c->backup(); + if (auto_midhandle) + c->setHandle(c->getWidth() / 2, c->getHeight() / 2); frames.push_back(c); - src_x += w; if (src_x + w > pic->getWidth()) { src_x = 0; src_y += h; } + src_x += w; + if (src_x + w > pic->getWidth()) { + src_x = 0; + src_y += h; + } } gx_graphics->freeCanvas(pic); - bbImage *i = new bbImage(frames); + bbImage* i = new bbImage(frames); image_set.insert(i); return i; } -bbImage *bbCopyImage(bbImage *i) { +bbImage* bbCopyImage(bbImage* i) +{ debugImage(i); - vector frames; - const vector &f = i->getFrames(); + vector frames; + const vector& f = i->getFrames(); for (int k = 0; k < f.size(); ++k) { - gxCanvas *t = f[k]; - gxCanvas *c = gx_graphics->createCanvas(t->getWidth(), t->getHeight(), 0); + gxCanvas* t = f[k]; + gxCanvas* c = gx_graphics->createCanvas(t->getWidth(), t->getHeight(), 0); if (!c) { - for (--k; k >= 0; --k) gx_graphics->freeCanvas(frames[k]); + for (--k; k >= 0; --k) + gx_graphics->freeCanvas(frames[k]); return 0; } int x, y; t->getHandle(&x, &y); t->setHandle(0, 0); c->blit(0, 0, t, 0, 0, t->getWidth(), t->getHeight(), true); - if (auto_dirty) c->backup(); + if (auto_dirty) + c->backup(); t->setHandle(x, y); c->setHandle(x, y); c->setMask(t->getMask()); frames.push_back(c); } - bbImage *t = new bbImage(frames); + bbImage* t = new bbImage(frames); image_set.insert(t); return t; } -bbImage *bbCreateImage(int w, int h, int n) { +bbImage* bbCreateImage(int w, int h, int n) +{ vector frames; for (int k = 0; k < n; ++k) { - gxCanvas *c = gx_graphics->createCanvas(w, h, 0); + gxCanvas* c = gx_graphics->createCanvas(w, h, 0); if (!c) { - for (--k; k >= 0; --k) gx_graphics->freeCanvas(frames[k]); + for (--k; k >= 0; --k) + gx_graphics->freeCanvas(frames[k]); return 0; } - if (auto_dirty) c->backup(); - if (auto_midhandle) c->setHandle(c->getWidth() / 2, c->getHeight() / 2); + if (auto_dirty) + c->backup(); + if (auto_midhandle) + c->setHandle(c->getWidth() / 2, c->getHeight() / 2); frames.push_back(c); } - bbImage *i = new bbImage(frames); + bbImage* i = new bbImage(frames); image_set.insert(i); return i; } -void bbFreeImage(bbImage *i) { - if (!image_set.erase(i)) return; - const vector &f = i->getFrames(); +void bbFreeImage(bbImage* i) +{ + if (!image_set.erase(i)) + return; + const vector& f = i->getFrames(); for (int k = 0; k < f.size(); ++k) { if (f[k] == gx_canvas) { bbSetBuffer(gx_graphics->getFrontCanvas()); @@ -804,44 +1011,53 @@ void bbFreeImage(bbImage *i) { delete i; } -int bbSaveImage(bbImage *i, BBStr *str, int n) { +int bbSaveImage(bbImage* i, BBStr* str, int n) +{ debugImage(i, n); - string t = *str; delete str; - gxCanvas *c = i->getFrames()[n]; + string t = *str; + delete str; + gxCanvas* c = i->getFrames()[n]; return saveCanvas(c, t) ? 1 : 0; } -void bbGrabImage(bbImage *i, int x, int y, int n) { +void bbGrabImage(bbImage* i, int x, int y, int n) +{ debugImage(i, n); - gxCanvas *c = i->getFrames()[n]; - int src_ox, src_oy, dst_hx, dst_hy; + gxCanvas* c = i->getFrames()[n]; + int src_ox, src_oy, dst_hx, dst_hy; gx_canvas->getOrigin(&src_ox, &src_oy); c->getHandle(&dst_hx, &dst_hy); - x += src_ox - dst_hx; y += src_oy - dst_hy; + x += src_ox - dst_hx; + y += src_oy - dst_hy; c->setViewport(0, 0, c->getWidth(), c->getHeight()); c->blit(0, 0, gx_canvas, x, y, c->getWidth(), c->getHeight(), true); - if (auto_dirty) c->backup(); + if (auto_dirty) + c->backup(); } -gxCanvas *bbImageBuffer(bbImage *i, int n) { +gxCanvas* bbImageBuffer(bbImage* i, int n) +{ debugImage(i, n); return i->getFrames()[n]; } -void bbDrawImage(bbImage *i, int x, int y, int frame) { +void bbDrawImage(bbImage* i, int x, int y, int frame) +{ debugImage(i, frame); - gxCanvas *c = i->getFrames()[frame]; + gxCanvas* c = i->getFrames()[frame]; gx_canvas->blit(x, y, c, 0, 0, c->getWidth(), c->getHeight(), false); } -void bbDrawBlock(bbImage *i, int x, int y, int frame) { +void bbDrawBlock(bbImage* i, int x, int y, int frame) +{ debugImage(i, frame); - gxCanvas *c = i->getFrames()[frame]; + gxCanvas* c = i->getFrames()[frame]; gx_canvas->blit(x, y, c, 0, 0, c->getWidth(), c->getHeight(), true); } -static void tile(bbImage *i, int x, int y, int frame, bool solid) { - gxCanvas *c = i->getFrames()[frame]; +static void tile(bbImage* i, int x, int y, int frame, bool solid) +{ + gxCanvas* c = i->getFrames()[frame]; int hx, hy; c->getHandle(&hx, &hy); @@ -854,8 +1070,8 @@ static void tile(bbImage *i, int x, int y, int frame, bool solid) { int dy = vp_y - oy + hy; x -= dx; y -= dy; - dx += (x >= 0 ? x%w : w - (-x%w)); - dy += (y >= 0 ? y%h : h - (-y%h)); + dx += (x >= 0 ? x % w : w - (-x % w)); + dy += (y >= 0 ? y % h : h - (-y % h)); for (y = -h; y < vp_h; y += h) { for (x = -w; x < vp_w; x += w) { @@ -864,112 +1080,134 @@ static void tile(bbImage *i, int x, int y, int frame, bool solid) { } } -void bbTileImage(bbImage *i, int x, int y, int frame) { +void bbTileImage(bbImage* i, int x, int y, int frame) +{ debugImage(i, frame); tile(i, x, y, frame, false); } -void bbTileBlock(bbImage *i, int x, int y, int frame) { +void bbTileBlock(bbImage* i, int x, int y, int frame) +{ debugImage(i, frame); tile(i, x, y, frame, true); } -void bbDrawImageRect(bbImage *i, int x, int y, int r_x, int r_y, int r_w, int r_h, int frame) { +void bbDrawImageRect(bbImage* i, int x, int y, int r_x, int r_y, int r_w, int r_h, int frame) +{ debugImage(i, frame); - gxCanvas *c = i->getFrames()[frame]; + gxCanvas* c = i->getFrames()[frame]; gx_canvas->blit(x, y, c, r_x, r_y, r_w, r_h, false); } -void bbDrawBlockRect(bbImage *i, int x, int y, int r_x, int r_y, int r_w, int r_h, int frame) { +void bbDrawBlockRect(bbImage* i, int x, int y, int r_x, int r_y, int r_w, int r_h, int frame) +{ debugImage(i, frame); - gxCanvas *c = i->getFrames()[frame]; + gxCanvas* c = i->getFrames()[frame]; gx_canvas->blit(x, y, c, r_x, r_y, r_w, r_h, true); } -void bbMaskImage(bbImage *i, int r, int g, int b) { +void bbMaskImage(bbImage* i, int r, int g, int b) +{ debugImage(i); - unsigned argb = (r << 16) | (g << 8) | b; - const vector &f = i->getFrames(); - for (int k = 0; k < f.size(); ++k) f[k]->setMask(argb); + unsigned argb = (r << 16) | (g << 8) | b; + const vector& f = i->getFrames(); + for (int k = 0; k < f.size(); ++k) + f[k]->setMask(argb); } -void bbHandleImage(bbImage *i, int x, int y) { +void bbHandleImage(bbImage* i, int x, int y) +{ debugImage(i); - const vector &f = i->getFrames(); - for (int k = 0; k < f.size(); ++k) f[k]->setHandle(x, y); + const vector& f = i->getFrames(); + for (int k = 0; k < f.size(); ++k) + f[k]->setHandle(x, y); } -void bbMidHandle(bbImage *i) { +void bbMidHandle(bbImage* i) +{ debugImage(i); - const vector &f = i->getFrames(); - for (int k = 0; k < f.size(); ++k) f[k]->setHandle(f[k]->getWidth() / 2, f[k]->getHeight() / 2); + const vector& f = i->getFrames(); + for (int k = 0; k < f.size(); ++k) + f[k]->setHandle(f[k]->getWidth() / 2, f[k]->getHeight() / 2); } -void bbAutoMidHandle(int enable) { +void bbAutoMidHandle(int enable) +{ auto_midhandle = enable ? true : false; } -int bbImageWidth(bbImage *i) { +int bbImageWidth(bbImage* i) +{ debugImage(i); return i->getFrames()[0]->getWidth(); } -int bbImageHeight(bbImage *i) { +int bbImageHeight(bbImage* i) +{ debugImage(i); return i->getFrames()[0]->getHeight(); } -int bbImageXHandle(bbImage *i) { +int bbImageXHandle(bbImage* i) +{ debugImage(i); int x, y; i->getFrames()[0]->getHandle(&x, &y); return x; } -int bbImageYHandle(bbImage *i) { +int bbImageYHandle(bbImage* i) +{ debugImage(i); int x, y; i->getFrames()[0]->getHandle(&x, &y); return y; } -int bbImagesOverlap(bbImage *i1, int x1, int y1, bbImage *i2, int x2, int y2) { +int bbImagesOverlap(bbImage* i1, int x1, int y1, bbImage* i2, int x2, int y2) +{ debugImage(i1); debugImage(i2); - gxCanvas *c1 = i1->getFrames()[0]; - gxCanvas *c2 = i2->getFrames()[0]; + gxCanvas* c1 = i1->getFrames()[0]; + gxCanvas* c2 = i2->getFrames()[0]; return c1->collide(x1, y1, c2, x2, y2, true); } -int bbImagesCollide(bbImage *i1, int x1, int y1, int f1, bbImage *i2, int x2, int y2, int f2) { +int bbImagesCollide(bbImage* i1, int x1, int y1, int f1, bbImage* i2, int x2, int y2, int f2) +{ debugImage(i1, f1); debugImage(i2, f2); - gxCanvas *c1 = i1->getFrames()[f1]; - gxCanvas *c2 = i2->getFrames()[f2]; + gxCanvas* c1 = i1->getFrames()[f1]; + gxCanvas* c2 = i2->getFrames()[f2]; return c1->collide(x1, y1, c2, x2, y2, false); } -int bbRectsOverlap(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) { - if (x1 + w1 <= x2 || x1 >= x2 + w2 || y1 + h1 <= y2 || y1 >= y2 + h2) return 0; +int bbRectsOverlap(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) +{ + if (x1 + w1 <= x2 || x1 >= x2 + w2 || y1 + h1 <= y2 || y1 >= y2 + h2) + return 0; return 1; } -int bbImageRectOverlap(bbImage *i, int x, int y, int x2, int y2, int w2, int h2) { +int bbImageRectOverlap(bbImage* i, int x, int y, int x2, int y2, int w2, int h2) +{ debugImage(i); - gxCanvas *c = i->getFrames()[0]; + gxCanvas* c = i->getFrames()[0]; return c->rect_collide(x, y, x2, y2, w2, h2, true); } -int bbImageRectCollide(bbImage *i, int x, int y, int f, int x2, int y2, int w2, int h2) { +int bbImageRectCollide(bbImage* i, int x, int y, int f, int x2, int y2, int w2, int h2) +{ debugImage(i, f); - gxCanvas *c = i->getFrames()[f]; + gxCanvas* c = i->getFrames()[f]; return c->rect_collide(x, y, x2, y2, w2, h2, false); } -void bbTFormImage(bbImage *i, float a, float b, float c, float d) { +void bbTFormImage(bbImage* i, float a, float b, float c, float d) +{ debugImage(i); - const vector &f = i->getFrames(); - int k; + const vector& f = i->getFrames(); + int k; for (k = 0; k < f.size(); ++k) { if (f[k] == gx_canvas) { bbSetBuffer(gx_graphics->getFrontCanvas()); @@ -977,42 +1215,50 @@ void bbTFormImage(bbImage *i, float a, float b, float c, float d) { } } float m[2][2]; - m[0][0] = a; m[1][0] = b; m[0][1] = c; m[1][1] = d; + m[0][0] = a; + m[1][0] = b; + m[0][1] = c; + m[1][1] = d; for (k = 0; k < f.size(); ++k) { - gxCanvas *c = f[k]; - int hx, hy; c->getHandle(&hx, &hy); - gxCanvas *t = tformCanvas(c, m, hx, hy); + gxCanvas* c = f[k]; + int hx, hy; + c->getHandle(&hx, &hy); + gxCanvas* t = tformCanvas(c, m, hx, hy); i->replaceFrame(k, t); t->backup(); } } -void bbScaleImage(bbImage *i, float w, float h) { +void bbScaleImage(bbImage* i, float w, float h) +{ debugImage(i); bbTFormImage(i, w, 0, 0, h); } -void bbResizeImage(bbImage *i, float w, float h) { +void bbResizeImage(bbImage* i, float w, float h) +{ debugImage(i); - gxCanvas *c = i->getFrames()[0]; + gxCanvas* c = i->getFrames()[0]; bbTFormImage(i, w / (float)c->getWidth(), 0, 0, h / (float)c->getHeight()); } -void bbRotateImage(bbImage *i, float d) { +void bbRotateImage(bbImage* i, float d) +{ debugImage(i); d *= -s_degreesToRadians; bbTFormImage(i, cos(d), -sin(d), sin(d), cos(d)); } -void bbTFormFilter(int enable) { +void bbTFormFilter(int enable) +{ filter = enable ? true : false; } static int p_ox, p_oy, p_hx, p_hy, p_vpx, p_vpy, p_vpw, p_vph; -static gxCanvas *startPrinting() { - - gxCanvas *c = gx_graphics->getFrontCanvas(); +static gxCanvas* startPrinting() +{ + gxCanvas* c = gx_graphics->getFrontCanvas(); c->lock(); c->unlock(); @@ -1040,24 +1286,29 @@ static gxCanvas *startPrinting() { return c; } -static void endPrinting(gxCanvas *c) { +static void endPrinting(gxCanvas* c) +{ c->setViewport(p_vpx, p_vpy, p_vpw, p_vph); c->setHandle(p_hx, p_hy); c->setOrigin(p_ox, p_oy); - if (c == gx_canvas) c->setColor(curr_color); - if (!gx_runtime->idle()) ThrowRuntimeException(0); + if (c == gx_canvas) + c->setColor(curr_color); + if (!gx_runtime->idle()) + ThrowRuntimeException(0); } -void bbWrite(BBStr *str) { - gxCanvas *c = startPrinting(); +void bbWrite(BBStr* str) +{ + gxCanvas* c = startPrinting(); c->text(curs_x, curs_y, *str); curs_x += curr_font->getWidth(*str); endPrinting(c); delete str; } -void bbPrint(BBStr *str) { - gxCanvas *c = startPrinting(); +void bbPrint(BBStr* str) +{ + gxCanvas* c = startPrinting(); c->text(curs_x, curs_y, *str); curs_x = 0; curs_y += curr_font->getHeight(); @@ -1065,13 +1316,16 @@ void bbPrint(BBStr *str) { delete str; } -BBStr *bbInput(BBStr *prompt) { - gxCanvas *c = startPrinting(); - string t = *prompt; delete prompt; +BBStr* bbInput(BBStr* prompt) +{ + gxCanvas* c = startPrinting(); + string t = *prompt; + delete prompt; //get temp canvas if (!p_canvas || p_canvas->getWidth() < c->getWidth() || p_canvas->getHeight() < curr_font->getHeight() * 2) { - if (p_canvas) gx_graphics->freeCanvas(p_canvas); + if (p_canvas) + gx_graphics->freeCanvas(p_canvas); p_canvas = gx_graphics->createCanvas(c->getWidth(), curr_font->getHeight() * 2, 0); if (!p_canvas) { endPrinting(c); @@ -1087,11 +1341,10 @@ BBStr *bbInput(BBStr *prompt) { p_canvas->blit(0, 0, c, 0, curs_y, c->getWidth(), curr_font->getHeight(), true); string str; - bool go = true; - int curs = 0, last_key = 0, last_time, rep_delay; + bool go = true; + int curs = 0, last_key = 0, last_time, rep_delay; while (go) { - //render all text //calc curs x and width int cx = curs_x + curr_font->getWidth(str.substr(0, curs)); @@ -1105,11 +1358,11 @@ BBStr *bbInput(BBStr *prompt) { int n = (t - st) / 320; if (n != tc) { tc = n; - if (!(tc & 1)) { //cursor ON + if (!(tc & 1)) { //cursor ON c->setColor(curr_clsColor ^ 0xffffff); c->rect(cx, curs_y, cw, curr_font->getHeight(), true); c->setColor(curr_clsColor); - } else { //cursor OFF + } else { //cursor OFF c->blit(cx, curs_y, p_canvas, cx, 0, cw, curr_font->getHeight(), true); c->setColor(curr_color); } @@ -1118,9 +1371,9 @@ BBStr *bbInput(BBStr *prompt) { if (key = gx_keyboard->getKey()) { if (int asc = gx_input->toAscii(key)) { rep_delay = 280; - last_key = key; + last_key = key; last_time = t; - key = asc; + key = asc; break; } } @@ -1132,48 +1385,53 @@ BBStr *bbInput(BBStr *prompt) { break; } } - } else last_key = 0; + } else + last_key = 0; gx_runtime->delay(20); } //check the key switch (key) { - case 0: - go = false; - str = ""; - break; - case 8: - if (curs) { - str = str.substr(0, curs - 1) + str.substr(curs); - --curs; - } - break; - case 27: - curs = 0; str = ""; - break; - case gxInput::ASC_DELETE: - if (curs < str.size()) str = str.substr(0, curs) + str.substr(curs + 1); - break; - case gxInput::ASC_HOME: - curs = 0; - break; - case gxInput::ASC_END: - curs = str.size(); - break; - case gxInput::ASC_LEFT: - if (curs) --curs; - break; - case gxInput::ASC_RIGHT: - if (curs < str.size()) ++curs; - break; - case '\r': - go = false; - break; - default: - if (curr_font->isPrintable(key)) { - str = str.substr(0, curs) + char(key) + str.substr(curs); - ++curs; - } + case 0: + go = false; + str = ""; + break; + case 8: + if (curs) { + str = str.substr(0, curs - 1) + str.substr(curs); + --curs; + } + break; + case 27: + curs = 0; + str = ""; + break; + case gxInput::ASC_DELETE: + if (curs < str.size()) + str = str.substr(0, curs) + str.substr(curs + 1); + break; + case gxInput::ASC_HOME: + curs = 0; + break; + case gxInput::ASC_END: + curs = str.size(); + break; + case gxInput::ASC_LEFT: + if (curs) + --curs; + break; + case gxInput::ASC_RIGHT: + if (curs < str.size()) + ++curs; + break; + case '\r': + go = false; + break; + default: + if (curr_font->isPrintable(key)) { + str = str.substr(0, curs) + char(key) + str.substr(curs); + ++curs; + } } //render text @@ -1188,39 +1446,44 @@ BBStr *bbInput(BBStr *prompt) { return new BBStr(str); } -void bbLocate(int x, int y) { - gxCanvas *c = gx_graphics->getFrontCanvas(); - curs_x = x < 0 ? 0 : (x > c->getWidth() ? c->getWidth() : x); - curs_y = y < 0 ? 0 : (y > c->getHeight() ? c->getHeight() : y); +void bbLocate(int x, int y) +{ + gxCanvas* c = gx_graphics->getFrontCanvas(); + curs_x = x < 0 ? 0 : (x > c->getWidth() ? c->getWidth() : x); + curs_y = y < 0 ? 0 : (y > c->getHeight() ? c->getHeight() : y); } -void bbShowPointer() { +void bbShowPointer() +{ gx_runtime->setPointerVisible(true); } -void bbHidePointer() { +void bbHidePointer() +{ gx_runtime->setPointerVisible(false); } -bool graphics_create() { - p_canvas = 0; - filter = true; +bool graphics_create() +{ + p_canvas = 0; + filter = true; gx_driver = 0; freeGraphics(); - auto_dirty = true; + auto_dirty = true; auto_midhandle = false; - gx_graphics = gx_runtime->openGraphics(400, 300, 0, 0, gxGraphics::GRAPHICS_WINDOWED); + gx_graphics = gx_runtime->openGraphics(400, 300, 0, 0, gxGraphics::GRAPHICS_WINDOWED); if (gx_graphics) { curr_clsColor = 0; - curr_color = 0xffffffff; - curr_font = gx_graphics->getDefaultFont(); + curr_color = 0xffffffff; + curr_font = gx_graphics->getDefaultFont(); bbSetBuffer(bbFrontBuffer()); return true; } return false; } -bool graphics_destroy() { +bool graphics_destroy() +{ freeGraphics(); gfx_modes.clear(); if (gx_graphics) { @@ -1230,8 +1493,8 @@ bool graphics_destroy() { return true; } -void graphics_link(void(*rtSym)(const char *sym, void *pc)) { - +void graphics_link(void (*rtSym)(const char* sym, void* pc)) +{ //gfx driver info rtSym("%CountGfxDrivers", bbCountGfxDrivers); rtSym("$GfxDriverName%driver", bbGfxDriverName); @@ -1254,7 +1517,7 @@ void graphics_link(void(*rtSym)(const char *sym, void *pc)) { rtSym("%Windowed3D", bbWindowed3D); //display mode -// rtSym( "Graphics%width%height%depth=0%mode=0",bbGraphics ); + // rtSym( "Graphics%width%height%depth=0%mode=0",bbGraphics ); rtSym("Graphics3D%width%height%depth=0%mode=0", bbGraphics3D); rtSym("EndGraphics", bbEndGraphics); rtSym("%GraphicsLost", bbGraphicsLost); @@ -1372,5 +1635,5 @@ void graphics_link(void(*rtSym)(const char *sym, void *pc)) { // NVidia Optimus extern "C" { - _declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001; +_declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001; } \ No newline at end of file diff --git a/Runtime/lib/bbgraphics.hpp b/Runtime/lib/bbgraphics.hpp index f393686..eacaf88 100644 --- a/Runtime/lib/bbgraphics.hpp +++ b/Runtime/lib/bbgraphics.hpp @@ -3,118 +3,117 @@ #define BBGRAPHICS_H #include "bbsys.hpp" -#include "../gxruntime/gxgraphics.hpp" +#include "gxgraphics.hpp" -extern gxGraphics *gx_graphics; -extern gxCanvas *gx_canvas; -extern gxScene *gx_scene; +extern gxGraphics* gx_graphics; +extern gxCanvas* gx_canvas; +extern gxScene* gx_scene; class bbImage; //general graphics functions -int bbCountGfxDrivers(); -BBStr * bbGfxDriverName( int n ); -BBStr * bbGfxDriverDesc( int n ); -void bbSetGfxDriver( int n ); -int bbGfxModeExists( int w,int h,int d ); -int bbCountGfxModes(); -int bbGfxModeWidth( int n ); -int bbGfxModeHeight( int n ); -int bbGfxModeDepth( int n ); -int bbGraphicsWidth(); -int bbGraphicsHeight(); -int bbGraphicsDepth(); -int bbAvailVidMem(); -int bbTotalVidMem(); +int bbCountGfxDrivers(); +BBStr* bbGfxDriverName(int n); +BBStr* bbGfxDriverDesc(int n); +void bbSetGfxDriver(int n); +int bbGfxModeExists(int w, int h, int d); +int bbCountGfxModes(); +int bbGfxModeWidth(int n); +int bbGfxModeHeight(int n); +int bbGfxModeDepth(int n); +int bbGraphicsWidth(); +int bbGraphicsHeight(); +int bbGraphicsDepth(); +int bbAvailVidMem(); +int bbTotalVidMem(); //mode functions -void bbGraphics( int w,int h,int d,int mode ); -gxCanvas * bbFrontBuffer(); -gxCanvas * bbBackBuffer(); -void bbEndGraphics(); -int bbGraphicsLost(); -int bbScanLine(); -void bbVWait( int n ); -void bbFlip( int vwait ); +void bbGraphics(int w, int h, int d, int mode); +gxCanvas* bbFrontBuffer(); +gxCanvas* bbBackBuffer(); +void bbEndGraphics(); +int bbGraphicsLost(); +int bbScanLine(); +void bbVWait(int n); +void bbFlip(int vwait); //graphics buffer functions -void bbSetBuffer( gxCanvas *buff ); -gxCanvas * bbGraphicsBuffer(); -int bbLoadBuffer( gxCanvas *surf,BBStr *str ); -int bbSaveBuffer( gxCanvas *surf,BBStr *str ); +void bbSetBuffer(gxCanvas* buff); +gxCanvas* bbGraphicsBuffer(); +int bbLoadBuffer(gxCanvas* surf, BBStr* str); +int bbSaveBuffer(gxCanvas* surf, BBStr* str); //fast read/write operations... -void bbLockBuffer( gxCanvas *buff ); -void bbUnlockBuffer( gxCanvas *buff ); -int bbReadPixel( int x,int y,gxCanvas *buff ); -void bbWritePixel( int x,int y,int argb,gxCanvas *buff ); -int bbReadPixelFast( int x,int y,gxCanvas *buff ); -void bbWritePixelFast( int x,int y,int argb,gxCanvas *buff ); - +void bbLockBuffer(gxCanvas* buff); +void bbUnlockBuffer(gxCanvas* buff); +int bbReadPixel(int x, int y, gxCanvas* buff); +void bbWritePixel(int x, int y, int argb, gxCanvas* buff); +int bbReadPixelFast(int x, int y, gxCanvas* buff); +void bbWritePixelFast(int x, int y, int argb, gxCanvas* buff); //2d rendering functions -void bbOrigin( int x,int y ); -void bbViewport( int x,int y,int w,int h ); -void bbColor( int r,int g,int b ); -void bbClsColor( int r,int g,int b ); -void bbCls(); -void bbPlot( int x,int y ); -void bbLine( int x1,int y1,int x2,int y2 ); -void bbRect( int x,int y,int w,int h,int solid ); -void bbOval( int x,int y,int w,int h,int solid ); -void bbText( int x,int y,BBStr *str,int centre_x,int centre_y ); -void bbGetColor( int x,int y ); -int bbColorRed(); -int bbColorGreen(); -int bbColorBlue(); +void bbOrigin(int x, int y); +void bbViewport(int x, int y, int w, int h); +void bbColor(int r, int g, int b); +void bbClsColor(int r, int g, int b); +void bbCls(); +void bbPlot(int x, int y); +void bbLine(int x1, int y1, int x2, int y2); +void bbRect(int x, int y, int w, int h, int solid); +void bbOval(int x, int y, int w, int h, int solid); +void bbText(int x, int y, BBStr* str, int centre_x, int centre_y); +void bbGetColor(int x, int y); +int bbColorRed(); +int bbColorGreen(); +int bbColorBlue(); //font functions -gxFont * bbLoadFont( BBStr *name,int height,int bold,int italic,int underline ); -void bbFreeFont( gxFont *f ); -void bbSetFont( gxFont *f ); -int bbFontWidth(); -int bbFontHeight(); -int bbStringWidth( BBStr *str ); -int bbStringHeight( BBStr *str ); +gxFont* bbLoadFont(BBStr* name, int height, int bold, int italic, int underline); +void bbFreeFont(gxFont* f); +void bbSetFont(gxFont* f); +int bbFontWidth(); +int bbFontHeight(); +int bbStringWidth(BBStr* str); +int bbStringHeight(BBStr* str); //image functions -bbImage* bbLoadImage( BBStr *s ); -bbImage* bbCopyImage( bbImage *i ); -bbImage* bbCreateImage( int w,int h,int n ); -bbImage* bbLoadAnimImage( BBStr *s,int w,int h,int first,int cnt ); -void bbFreeImage( bbImage *i ); -int bbSaveImage( bbImage *i,BBStr *filename,int frame ); -void bbGrabImage( bbImage *i,int x,int y,int n ); -gxCanvas * bbImageBuffer( bbImage *i,int n ); -void bbDrawImage( bbImage *i,int x,int y,int frame ); -void bbDrawBlock( bbImage *i,int x,int y,int frame ); -void bbTileImage( bbImage *i,int x,int y,int frame ); -void bbTileBlock( bbImage *i,int x,int y,int frame ); -void bbDrawImageRect( bbImage *i,int x,int y,int r_x,int r_y,int r_w,int r_h,int frame ); -void bbDrawBlockRect( bbImage *i,int x,int y,int r_x,int r_y,int r_w,int r_h,int frame ); -void bbMaskImage( bbImage *i,int r,int g,int b ); -void bbHandleImage( bbImage *i,int x,int y ); -void bbScaleImage( bbImage *i,float w,float h ); -void bbResizeImage( bbImage *i,float w,float h ); -void bbRotateImage( bbImage *i,float angle ); -void bbTFormImage( bbImage *i,float a,float b,float c,float d ); -void bbTFormFilter( int enable ); -void bbAutoMidHandle( int enable ); -void bbMidHandle( bbImage *i ); -int bbImageWidth( bbImage *i ); -int bbImageHeight( bbImage *i ); -int bbImageXHandle( bbImage *i ); -int bbImageYHandle( bbImage *i ); -int bbImagesOverlap( bbImage *i1,int x1,int y1,bbImage *i2,int x2,int y2 ); -int bbImagesCollide( bbImage *i1,int x1,int y1,int f1,bbImage *i2,int x2,int y2,int f2 ); -int bbRectsOverlap( int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2 ); -int bbImageRectOverlap( bbImage *i,int x,int y,int r_x,int r_y,int r_w,int r_h ); -int bbImageRectCollide( bbImage *i,int x,int y,int f,int r_x,int r_y,int r_w,int r_h ); +bbImage* bbLoadImage(BBStr* s); +bbImage* bbCopyImage(bbImage* i); +bbImage* bbCreateImage(int w, int h, int n); +bbImage* bbLoadAnimImage(BBStr* s, int w, int h, int first, int cnt); +void bbFreeImage(bbImage* i); +int bbSaveImage(bbImage* i, BBStr* filename, int frame); +void bbGrabImage(bbImage* i, int x, int y, int n); +gxCanvas* bbImageBuffer(bbImage* i, int n); +void bbDrawImage(bbImage* i, int x, int y, int frame); +void bbDrawBlock(bbImage* i, int x, int y, int frame); +void bbTileImage(bbImage* i, int x, int y, int frame); +void bbTileBlock(bbImage* i, int x, int y, int frame); +void bbDrawImageRect(bbImage* i, int x, int y, int r_x, int r_y, int r_w, int r_h, int frame); +void bbDrawBlockRect(bbImage* i, int x, int y, int r_x, int r_y, int r_w, int r_h, int frame); +void bbMaskImage(bbImage* i, int r, int g, int b); +void bbHandleImage(bbImage* i, int x, int y); +void bbScaleImage(bbImage* i, float w, float h); +void bbResizeImage(bbImage* i, float w, float h); +void bbRotateImage(bbImage* i, float angle); +void bbTFormImage(bbImage* i, float a, float b, float c, float d); +void bbTFormFilter(int enable); +void bbAutoMidHandle(int enable); +void bbMidHandle(bbImage* i); +int bbImageWidth(bbImage* i); +int bbImageHeight(bbImage* i); +int bbImageXHandle(bbImage* i); +int bbImageYHandle(bbImage* i); +int bbImagesOverlap(bbImage* i1, int x1, int y1, bbImage* i2, int x2, int y2); +int bbImagesCollide(bbImage* i1, int x1, int y1, int f1, bbImage* i2, int x2, int y2, int f2); +int bbRectsOverlap(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2); +int bbImageRectOverlap(bbImage* i, int x, int y, int r_x, int r_y, int r_w, int r_h); +int bbImageRectCollide(bbImage* i, int x, int y, int f, int r_x, int r_y, int r_w, int r_h); //simple print functions -void bbWrite( BBStr *str ); -void bbPrint( BBStr *str ); -BBStr * bbInput( BBStr *prompt ); -void bbLocate( int x,int y ); +void bbWrite(BBStr* str); +void bbPrint(BBStr* str); +BBStr* bbInput(BBStr* prompt); +void bbLocate(int x, int y); #endif \ No newline at end of file diff --git a/Runtime/lib/bbinput.cpp b/Runtime/lib/bbinput.cpp index 7896bac..e838030 100644 --- a/Runtime/lib/bbinput.cpp +++ b/Runtime/lib/bbinput.cpp @@ -1,287 +1,355 @@ -#include "std.hpp" #include "bbsys.hpp" +#include "std.hpp" -gxInput *gx_input; -gxDevice *gx_mouse; -gxDevice *gx_keyboard; +gxInput* gx_input; +gxDevice* gx_mouse; +gxDevice* gx_keyboard; vector gx_joysticks; -static int mouse_x,mouse_y,mouse_z; -static const float JLT=-1.0f/3.0f; -static const float JHT=1.0f/3.0f; +static int mouse_x, mouse_y, mouse_z; +static const float JLT = -1.0f / 3.0f; +static const float JHT = 1.0f / 3.0f; -bool input_create(){ - if( gx_input=gx_runtime->openInput( 0 ) ){ - if( gx_keyboard=gx_input->getKeyboard() ){ - if( gx_mouse=gx_input->getMouse() ){ +bool input_create() +{ + if (gx_input = gx_runtime->openInput(0)) { + if (gx_keyboard = gx_input->getKeyboard()) { + if (gx_mouse = gx_input->getMouse()) { gx_joysticks.clear(); - for( int k=0;knumJoysticks();++k ){ - gx_joysticks.push_back( gx_input->getJoystick(k) ); + for (int k = 0; k < gx_input->numJoysticks(); ++k) { + gx_joysticks.push_back(gx_input->getJoystick(k)); } - mouse_x=mouse_y=mouse_z=0; + mouse_x = mouse_y = mouse_z = 0; return true; } } - gx_runtime->closeInput( gx_input ); - gx_input=0; + gx_runtime->closeInput(gx_input); + gx_input = 0; } return false; } -bool input_destroy(){ +bool input_destroy() +{ gx_joysticks.clear(); - gx_runtime->closeInput( gx_input ); - gx_input=0; + gx_runtime->closeInput(gx_input); + gx_input = 0; return true; } -int bbKeyDown( int n ){ - return gx_keyboard->keyDown( n ); +int bbKeyDown(int n) +{ + return gx_keyboard->keyDown(n); } -int bbKeyHit( int n ){ - return gx_keyboard->keyHit( n ); +int bbKeyHit(int n) +{ + return gx_keyboard->keyHit(n); } -int bbGetKey(){ - return gx_input->toAscii( gx_keyboard->getKey() ); +int bbGetKey() +{ + return gx_input->toAscii(gx_keyboard->getKey()); } -int bbWaitKey(){ - for(;;){ - if( !gx_runtime->idle() ) ThrowRuntimeException( 0 ); - if( int key=gx_keyboard->getKey( ) ){ - if( key=gx_input->toAscii( key ) ) return key; +int bbWaitKey() +{ + for (;;) { + if (!gx_runtime->idle()) + ThrowRuntimeException(0); + if (int key = gx_keyboard->getKey()) { + if (key = gx_input->toAscii(key)) + return key; } - gx_runtime->delay( 20 ); + gx_runtime->delay(20); } } -void bbFlushKeys(){ +void bbFlushKeys() +{ gx_keyboard->flush(); } -int bbMouseDown( int n ){ - return gx_mouse->keyDown( n ); +int bbMouseDown(int n) +{ + return gx_mouse->keyDown(n); } -int bbMouseHit( int n ){ - return gx_mouse->keyHit( n ); +int bbMouseHit(int n) +{ + return gx_mouse->keyHit(n); } -int bbGetMouse(){ +int bbGetMouse() +{ return gx_mouse->getKey(); } -int bbWaitMouse(){ - for(;;){ - if( !gx_runtime->idle() ) ThrowRuntimeException( 0 ); - if( int key=gx_mouse->getKey() ) return key; - gx_runtime->delay( 20 ); +int bbWaitMouse() +{ + for (;;) { + if (!gx_runtime->idle()) + ThrowRuntimeException(0); + if (int key = gx_mouse->getKey()) + return key; + gx_runtime->delay(20); } } -int bbMouseWait(){ +int bbMouseWait() +{ return bbWaitMouse(); } -int bbMouseX(){ - return gx_mouse->getAxisState( 0 ); +int bbMouseX() +{ + return gx_mouse->getAxisState(0); } -int bbMouseY(){ - return gx_mouse->getAxisState( 1 ); +int bbMouseY() +{ + return gx_mouse->getAxisState(1); } -int bbMouseZ(){ - return gx_mouse->getAxisState( 2 )/120; +int bbMouseZ() +{ + return gx_mouse->getAxisState(2) / 120; } -int bbMouseXSpeed(){ - int dx=bbMouseX()-mouse_x; - mouse_x+=dx; +int bbMouseXSpeed() +{ + int dx = bbMouseX() - mouse_x; + mouse_x += dx; return dx; } -int bbMouseYSpeed(){ - int dy=bbMouseY()-mouse_y; - mouse_y+=dy; +int bbMouseYSpeed() +{ + int dy = bbMouseY() - mouse_y; + mouse_y += dy; return dy; } -int bbMouseZSpeed(){ - int dz=bbMouseZ()-mouse_z; - mouse_z+=dz; +int bbMouseZSpeed() +{ + int dz = bbMouseZ() - mouse_z; + mouse_z += dz; return dz; } -void bbFlushMouse(){ +void bbFlushMouse() +{ gx_mouse->flush(); } -void bbMoveMouse( int x,int y ){ - gx_input->moveMouse( mouse_x=x,mouse_y=y ); +void bbMoveMouse(int x, int y) +{ + gx_input->moveMouse(mouse_x = x, mouse_y = y); } -int bbJoyType( int port ){ - return gx_input->getJoystickType( port ); +int bbJoyType(int port) +{ + return gx_input->getJoystickType(port); } -int bbJoyDown( int n,int port ){ - if( port<0 || port>=gx_joysticks.size() ) return 0; - return gx_joysticks[port]->keyDown( n ); +int bbJoyDown(int n, int port) +{ + if (port < 0 || port >= gx_joysticks.size()) + return 0; + return gx_joysticks[port]->keyDown(n); } -int bbJoyHit( int n,int port ){ - if( port<0 || port>=gx_joysticks.size() ) return 0; - return gx_joysticks[port]->keyHit( n ); +int bbJoyHit(int n, int port) +{ + if (port < 0 || port >= gx_joysticks.size()) + return 0; + return gx_joysticks[port]->keyHit(n); } -int bbGetJoy( int port ){ - if( port<0 || port>=gx_joysticks.size() ) return 0; +int bbGetJoy(int port) +{ + if (port < 0 || port >= gx_joysticks.size()) + return 0; return gx_joysticks[port]->getKey(); } -int bbWaitJoy( int port ){ - if( port<0 || port>=gx_joysticks.size() ) return 0; - for(;;){ - if( !gx_runtime->idle() ) ThrowRuntimeException( 0 ); - if( int key=gx_joysticks[port]->getKey() ) return key; - gx_runtime->delay( 20 ); +int bbWaitJoy(int port) +{ + if (port < 0 || port >= gx_joysticks.size()) + return 0; + for (;;) { + if (!gx_runtime->idle()) + ThrowRuntimeException(0); + if (int key = gx_joysticks[port]->getKey()) + return key; + gx_runtime->delay(20); } } -float bbJoyX( int port ){ - if( port<0 || port>=gx_joysticks.size() ) return 0; +float bbJoyX(int port) +{ + if (port < 0 || port >= gx_joysticks.size()) + return 0; return gx_joysticks[port]->getAxisState(0); } -float bbJoyY( int port ){ - if( port<0 || port>=gx_joysticks.size() ) return 0; +float bbJoyY(int port) +{ + if (port < 0 || port >= gx_joysticks.size()) + return 0; return gx_joysticks[port]->getAxisState(1); } -float bbJoyZ( int port ){ - if( port<0 || port>=gx_joysticks.size() ) return 0; +float bbJoyZ(int port) +{ + if (port < 0 || port >= gx_joysticks.size()) + return 0; return gx_joysticks[port]->getAxisState(2); } -float bbJoyU( int port ){ - if( port<0 || port>=gx_joysticks.size() ) return 0; +float bbJoyU(int port) +{ + if (port < 0 || port >= gx_joysticks.size()) + return 0; return gx_joysticks[port]->getAxisState(3); } -float bbJoyV( int port ){ - if( port<0 || port>=gx_joysticks.size() ) return 0; +float bbJoyV(int port) +{ + if (port < 0 || port >= gx_joysticks.size()) + return 0; return gx_joysticks[port]->getAxisState(4); } -float bbJoyPitch( int port ){ - if( port<0 || port>=gx_joysticks.size() ) return 0; - return gx_joysticks[port]->getAxisState(5)*180; +float bbJoyPitch(int port) +{ + if (port < 0 || port >= gx_joysticks.size()) + return 0; + return gx_joysticks[port]->getAxisState(5) * 180; } -float bbJoyYaw( int port ){ - if( port<0 || port>=gx_joysticks.size() ) return 0; - return gx_joysticks[port]->getAxisState(6)*180; +float bbJoyYaw(int port) +{ + if (port < 0 || port >= gx_joysticks.size()) + return 0; + return gx_joysticks[port]->getAxisState(6) * 180; } -float bbJoyRoll( int port ){ - if( port<0 || port>=gx_joysticks.size() ) return 0; - return gx_joysticks[port]->getAxisState(7)*180; +float bbJoyRoll(int port) +{ + if (port < 0 || port >= gx_joysticks.size()) + return 0; + return gx_joysticks[port]->getAxisState(7) * 180; } -int bbJoyHat( int port ){ - if( port<0 || port>=gx_joysticks.size() ) return 0; +int bbJoyHat(int port) +{ + if (port < 0 || port >= gx_joysticks.size()) + return 0; return gx_joysticks[port]->getAxisState(8); } -int bbJoyXDir( int port ){ - if( port<0 || port>=gx_joysticks.size() ) return 0; - float t=gx_joysticks[port]->getAxisState(0); - return tJHT ? 1 : 0 ); +int bbJoyXDir(int port) +{ + if (port < 0 || port >= gx_joysticks.size()) + return 0; + float t = gx_joysticks[port]->getAxisState(0); + return t < JLT ? -1 : (t > JHT ? 1 : 0); } -int bbJoyYDir( int port ){ - if( port<0 || port>=gx_joysticks.size() ) return 0; - float t=gx_joysticks[port]->getAxisState(1); - return tJHT ? 1 : 0 ); +int bbJoyYDir(int port) +{ + if (port < 0 || port >= gx_joysticks.size()) + return 0; + float t = gx_joysticks[port]->getAxisState(1); + return t < JLT ? -1 : (t > JHT ? 1 : 0); } -int bbJoyZDir( int port ){ - if( port<0 || port>=gx_joysticks.size() ) return 0; - float t=gx_joysticks[port]->getAxisState(2); - return tJHT ? 1 : 0 ); +int bbJoyZDir(int port) +{ + if (port < 0 || port >= gx_joysticks.size()) + return 0; + float t = gx_joysticks[port]->getAxisState(2); + return t < JLT ? -1 : (t > JHT ? 1 : 0); } -int bbJoyUDir( int port ){ - if( port<0 || port>=gx_joysticks.size() ) return 0; - float t=gx_joysticks[port]->getAxisState(3); - return tJHT ? 1 : 0 ); +int bbJoyUDir(int port) +{ + if (port < 0 || port >= gx_joysticks.size()) + return 0; + float t = gx_joysticks[port]->getAxisState(3); + return t < JLT ? -1 : (t > JHT ? 1 : 0); } -int bbJoyVDir( int port ){ - if( port<0 || port>=gx_joysticks.size() ) return 0; - float t=gx_joysticks[port]->getAxisState(4); - return tJHT ? 1 : 0 ); +int bbJoyVDir(int port) +{ + if (port < 0 || port >= gx_joysticks.size()) + return 0; + float t = gx_joysticks[port]->getAxisState(4); + return t < JLT ? -1 : (t > JHT ? 1 : 0); } -void bbFlushJoy(){ - for( int k=0;kflush(); +void bbFlushJoy() +{ + for (int k = 0; k < gx_joysticks.size(); ++k) + gx_joysticks[k]->flush(); } -void bbEnableDirectInput( int enable ){ - gx_runtime->enableDirectInput( !!enable ); +void bbEnableDirectInput(int enable) +{ + gx_runtime->enableDirectInput(!!enable); } -int bbDirectInputEnabled(){ +int bbDirectInputEnabled() +{ return gx_runtime->directInputEnabled(); } -void input_link( void (*rtSym)( const char *sym,void *pc ) ){ - rtSym( "%KeyDown%key",bbKeyDown ); - rtSym( "%KeyHit%key",bbKeyHit ); - rtSym( "%GetKey",bbGetKey ); - rtSym( "%WaitKey",bbWaitKey ); - rtSym( "FlushKeys",bbFlushKeys ); +void input_link(void (*rtSym)(const char* sym, void* pc)) +{ + rtSym("%KeyDown%key", bbKeyDown); + rtSym("%KeyHit%key", bbKeyHit); + rtSym("%GetKey", bbGetKey); + rtSym("%WaitKey", bbWaitKey); + rtSym("FlushKeys", bbFlushKeys); - rtSym( "%MouseDown%button",bbMouseDown ); - rtSym( "%MouseHit%button",bbMouseHit ); - rtSym( "%GetMouse",bbGetMouse ); - rtSym( "%WaitMouse",bbWaitMouse ); - rtSym( "%MouseWait",bbWaitMouse ); - rtSym( "%MouseX",bbMouseX ); - rtSym( "%MouseY",bbMouseY ); - rtSym( "%MouseZ",bbMouseZ ); - rtSym( "%MouseXSpeed",bbMouseXSpeed ); - rtSym( "%MouseYSpeed",bbMouseYSpeed ); - rtSym( "%MouseZSpeed",bbMouseZSpeed ); - rtSym( "FlushMouse",bbFlushMouse ); - rtSym( "MoveMouse%x%y",bbMoveMouse ); + rtSym("%MouseDown%button", bbMouseDown); + rtSym("%MouseHit%button", bbMouseHit); + rtSym("%GetMouse", bbGetMouse); + rtSym("%WaitMouse", bbWaitMouse); + rtSym("%MouseWait", bbWaitMouse); + rtSym("%MouseX", bbMouseX); + rtSym("%MouseY", bbMouseY); + rtSym("%MouseZ", bbMouseZ); + rtSym("%MouseXSpeed", bbMouseXSpeed); + rtSym("%MouseYSpeed", bbMouseYSpeed); + rtSym("%MouseZSpeed", bbMouseZSpeed); + rtSym("FlushMouse", bbFlushMouse); + rtSym("MoveMouse%x%y", bbMoveMouse); - rtSym( "%JoyType%port=0",bbJoyType ); - rtSym( "%JoyDown%button%port=0",bbJoyDown ); - rtSym( "%JoyHit%button%port=0",bbJoyHit ); - rtSym( "%GetJoy%port=0",bbGetJoy ); - rtSym( "%WaitJoy%port=0",bbWaitJoy ); - rtSym( "%JoyWait%port=0",bbWaitJoy ); - rtSym( "#JoyX%port=0",bbJoyX ); - rtSym( "#JoyY%port=0",bbJoyY ); - rtSym( "#JoyZ%port=0",bbJoyZ ); - rtSym( "#JoyU%port=0",bbJoyU ); - rtSym( "#JoyV%port=0",bbJoyV ); - rtSym( "#JoyPitch%port=0",bbJoyPitch ); - rtSym( "#JoyYaw%port=0",bbJoyYaw ); - rtSym( "#JoyRoll%port=0",bbJoyRoll ); - rtSym( "%JoyHat%port=0",bbJoyHat ); - rtSym( "%JoyXDir%port=0",bbJoyXDir ); - rtSym( "%JoyYDir%port=0",bbJoyYDir ); - rtSym( "%JoyZDir%port=0",bbJoyZDir ); - rtSym( "%JoyUDir%port=0",bbJoyUDir ); - rtSym( "%JoyVDir%port=0",bbJoyVDir ); - rtSym( "FlushJoy",bbFlushJoy ); + rtSym("%JoyType%port=0", bbJoyType); + rtSym("%JoyDown%button%port=0", bbJoyDown); + rtSym("%JoyHit%button%port=0", bbJoyHit); + rtSym("%GetJoy%port=0", bbGetJoy); + rtSym("%WaitJoy%port=0", bbWaitJoy); + rtSym("%JoyWait%port=0", bbWaitJoy); + rtSym("#JoyX%port=0", bbJoyX); + rtSym("#JoyY%port=0", bbJoyY); + rtSym("#JoyZ%port=0", bbJoyZ); + rtSym("#JoyU%port=0", bbJoyU); + rtSym("#JoyV%port=0", bbJoyV); + rtSym("#JoyPitch%port=0", bbJoyPitch); + rtSym("#JoyYaw%port=0", bbJoyYaw); + rtSym("#JoyRoll%port=0", bbJoyRoll); + rtSym("%JoyHat%port=0", bbJoyHat); + rtSym("%JoyXDir%port=0", bbJoyXDir); + rtSym("%JoyYDir%port=0", bbJoyYDir); + rtSym("%JoyZDir%port=0", bbJoyZDir); + rtSym("%JoyUDir%port=0", bbJoyUDir); + rtSym("%JoyVDir%port=0", bbJoyVDir); + rtSym("FlushJoy", bbFlushJoy); - rtSym( "EnableDirectInput%enable",bbEnableDirectInput ); - rtSym( "%DirectInputEnabled",bbDirectInputEnabled ); + rtSym("EnableDirectInput%enable", bbEnableDirectInput); + rtSym("%DirectInputEnabled", bbDirectInputEnabled); } diff --git a/Runtime/lib/bbinput.hpp b/Runtime/lib/bbinput.hpp index 4bd986d..e8841db 100644 --- a/Runtime/lib/bbinput.hpp +++ b/Runtime/lib/bbinput.hpp @@ -5,51 +5,51 @@ #include #include "bbsys.hpp" -#include "../gxruntime/gxinput.hpp" +#include "gxinput.hpp" -extern gxInput *gx_input; -extern gxDevice *gx_mouse; -extern gxDevice *gx_keyboard; +extern gxInput* gx_input; +extern gxDevice* gx_mouse; +extern gxDevice* gx_keyboard; extern std::vector gx_joysticks; //keyboard -int bbKeyDown( int n ); -int bbKeyHit( int n ); -int bbGetKey(); -int bbWaitKey(); -void bbFlushKeys(); +int bbKeyDown(int n); +int bbKeyHit(int n); +int bbGetKey(); +int bbWaitKey(); +void bbFlushKeys(); //mouse -int bbMouseDown( int n ); -int bbMouseHit( int n ); -int bbGetMouse(); -int bbWaitMouse(); -int bbMouseX(); -int bbMouseY(); -int bbMouseXSpeed(); -int bbMouseYSpeed(); -void bbMoveMouse( int x,int y ); -void bbFlushMouse(); +int bbMouseDown(int n); +int bbMouseHit(int n); +int bbGetMouse(); +int bbWaitMouse(); +int bbMouseX(); +int bbMouseY(); +int bbMouseXSpeed(); +int bbMouseYSpeed(); +void bbMoveMouse(int x, int y); +void bbFlushMouse(); //joysticks -int bbJoyType( int port ); -int bbJoyDown( int n,int port ); -int bbJoyHit( int n,int port ); -int bbGetJoy( int port ); -int bbWaitJoy( int port ); -float bbJoyX( int port ); -float bbJoyY( int port ); -float bbJoyZ( int port ); -float bbJoyU( int port ); -float bbJoyV( int port ); -float bbJoyPitch( int port ); -float bbJoyYaw( int port ); -float bbJoyRoll( int port ); -int bbJoyXDir( int port ); -int bbJoyYDir( int port ); -int bbJoyZDir( int port ); -int bbJoyUDir( int port ); -int bbJoyVDir( int port ); -void bbFlushJoy(); +int bbJoyType(int port); +int bbJoyDown(int n, int port); +int bbJoyHit(int n, int port); +int bbGetJoy(int port); +int bbWaitJoy(int port); +float bbJoyX(int port); +float bbJoyY(int port); +float bbJoyZ(int port); +float bbJoyU(int port); +float bbJoyV(int port); +float bbJoyPitch(int port); +float bbJoyYaw(int port); +float bbJoyRoll(int port); +int bbJoyXDir(int port); +int bbJoyYDir(int port); +int bbJoyZDir(int port); +int bbJoyUDir(int port); +int bbJoyVDir(int port); +void bbFlushJoy(); #endif \ No newline at end of file diff --git a/Runtime/lib/bbmath.cpp b/Runtime/lib/bbmath.cpp index 1124d69..9268451 100644 --- a/Runtime/lib/bbmath.cpp +++ b/Runtime/lib/bbmath.cpp @@ -1,80 +1,129 @@ -#include "std.hpp" #include "bbmath.hpp" +#include "std.hpp" -static int rnd_state; -static const int RND_A=48271; -static const int RND_M=2147483647; -static const int RND_Q=44488; -static const int RND_R=3399; +static int rnd_state; +static const int RND_A = 48271; +static const int RND_M = 2147483647; +static const int RND_Q = 44488; +static const int RND_R = 3399; -static const float s_degreesToRadians=0.0174532925199432957692369076848861f; -static const float s_radiansToDegrees=57.2957795130823208767981548141052f; +static const float s_degreesToRadians = 0.0174532925199432957692369076848861f; +static const float s_radiansToDegrees = 57.2957795130823208767981548141052f; -float bbSin( float n ){ return (float)sin(n*s_degreesToRadians); } -float bbCos( float n ){ return (float)cos(n*s_degreesToRadians); } -float bbTan( float n ){ return (float)tan(n*s_degreesToRadians); } -float bbASin( float n ){ return (float)asin(n)*s_radiansToDegrees; } -float bbACos( float n ){ return (float)acos(n)*s_radiansToDegrees; } -float bbATan( float n ){ return (float)atan(n)*s_radiansToDegrees; } -float bbATan2( float n,float t ){ return (float)atan2(n,t)*s_radiansToDegrees; } -float bbSqr( float n ){ return (float)sqrt(n); } -float bbFloor( float n ){ return (float)floor(n); } -float bbCeil( float n ){ return (float)ceil(n); } -float bbExp( float n ){ return (float)exp(n); } -float bbLog( float n ){ return (float)log(n); } -float bbLog10( float n ){ return (float)log10(n); } +float bbSin(float n) +{ + return (float)sin(n * s_degreesToRadians); +} +float bbCos(float n) +{ + return (float)cos(n * s_degreesToRadians); +} +float bbTan(float n) +{ + return (float)tan(n * s_degreesToRadians); +} +float bbASin(float n) +{ + return (float)asin(n) * s_radiansToDegrees; +} +float bbACos(float n) +{ + return (float)acos(n) * s_radiansToDegrees; +} +float bbATan(float n) +{ + return (float)atan(n) * s_radiansToDegrees; +} +float bbATan2(float n, float t) +{ + return (float)atan2(n, t) * s_radiansToDegrees; +} +float bbSqr(float n) +{ + return (float)sqrt(n); +} +float bbFloor(float n) +{ + return (float)floor(n); +} +float bbCeil(float n) +{ + return (float)ceil(n); +} +float bbExp(float n) +{ + return (float)exp(n); +} +float bbLog(float n) +{ + return (float)log(n); +} +float bbLog10(float n) +{ + return (float)log10(n); +} //return rand float from 0...1 -static inline float rnd(){ - rnd_state=RND_A*(rnd_state%RND_Q)-RND_R*(rnd_state/RND_Q); - if( rnd_state<0 ) rnd_state+=RND_M; - return (rnd_state&65535)/65536.0f+(.5f/65536.0f); +static inline float rnd() +{ + rnd_state = RND_A * (rnd_state % RND_Q) - RND_R * (rnd_state / RND_Q); + if (rnd_state < 0) + rnd_state += RND_M; + return (rnd_state & 65535) / 65536.0f + (.5f / 65536.0f); } -float bbRnd( float from,float to ){ - return rnd()*(to-from)+from; +float bbRnd(float from, float to) +{ + return rnd() * (to - from) + from; } -int bbRand( int from,int to ){ - if( todebugStop(); - if (!gx_runtime->idle()) ThrowRuntimeException(0); + if (!gx_runtime->idle()) + ThrowRuntimeException(0); } -void bbAppTitle(BBStr *ti, BBStr *cp) { +void bbAppTitle(BBStr* ti, BBStr* cp) +{ gx_runtime->setTitle(*ti, *cp); - delete ti; delete cp; + delete ti; + delete cp; } -void bbRuntimeError(BBStr *str) { - string t = *str; delete str; - if (t.size() > 255) t[255] = 0; +void bbRuntimeError(BBStr* str) +{ + string t = *str; + delete str; + if (t.size() > 255) + t[255] = 0; static char err[256]; strcpy(err, t.c_str()); ThrowRuntimeException(err); } -int bbExecFile(BBStr *f) { - string t = *f; delete f; +int bbExecFile(BBStr* f) +{ + string t = *f; + delete f; int n = gx_runtime->execute(t); - if (!gx_runtime->idle()) ThrowRuntimeException(0); + if (!gx_runtime->idle()) + ThrowRuntimeException(0); return n; } -void bbDelay(int ms) { - if (!gx_runtime->delay(ms)) ThrowRuntimeException(0); +void bbDelay(int ms) +{ + if (!gx_runtime->delay(ms)) + ThrowRuntimeException(0); } -int bbMilliSecs() { +int bbMilliSecs() +{ return gx_runtime->getMilliSecs(); } -BBStr * bbCommandLine() { +BBStr* bbCommandLine() +{ return new BBStr(gx_runtime->commandLine()); } -BBStr * bbSystemProperty(BBStr *p) { +BBStr* bbSystemProperty(BBStr* p) +{ string t = gx_runtime->systemProperty(*p); - delete p; return new BBStr(t); + delete p; + return new BBStr(t); } -BBStr * bbGetEnv(BBStr *env_var) { - char *p = getenv(env_var->c_str()); - BBStr *val = new BBStr(p ? p : ""); +BBStr* bbGetEnv(BBStr* env_var) +{ + char* p = getenv(env_var->c_str()); + BBStr* val = new BBStr(p ? p : ""); delete env_var; return val; } -void bbSetEnv(BBStr *env_var, BBStr *val) { +void bbSetEnv(BBStr* env_var, BBStr* val) +{ string t = *env_var + "=" + *val; _putenv(t.c_str()); delete env_var; delete val; } -gxTimer * bbCreateTimer(int hertz) { - gxTimer *t = gx_runtime->createTimer(hertz); +gxTimer* bbCreateTimer(int hertz) +{ + gxTimer* t = gx_runtime->createTimer(hertz); return t; } -int bbWaitTimer(gxTimer *t) { +int bbWaitTimer(gxTimer* t) +{ int n = t->wait(); - if (!gx_runtime->idle()) ThrowRuntimeException(0); + if (!gx_runtime->idle()) + ThrowRuntimeException(0); return n; } -void bbFreeTimer(gxTimer *t) { +void bbFreeTimer(gxTimer* t) +{ gx_runtime->freeTimer(t); } -void bbDebugLog(BBStr *t) { +void bbDebugLog(BBStr* t) +{ gx_runtime->debugLog(t->c_str()); delete t; } -void _bbDebugStmt(int pos, const char *file) { +void _bbDebugStmt(int pos, const char* file) +{ gx_runtime->debugStmt(pos, file); - if (!gx_runtime->idle()) ThrowRuntimeException(0); + if (!gx_runtime->idle()) + ThrowRuntimeException(0); } -void _bbDebugEnter(void *frame, void *env, const char *func) { +void _bbDebugEnter(void* frame, void* env, const char* func) +{ gx_runtime->debugEnter(frame, env, func); } -void _bbDebugLeave() { +void _bbDebugLeave() +{ gx_runtime->debugLeave(); } bool basic_create(); bool basic_destroy(); -void basic_link(void(*rtSym)(const char *sym, void *pc)); +void basic_link(void (*rtSym)(const char* sym, void* pc)); bool math_create(); bool math_destroy(); -void math_link(void(*rtSym)(const char *sym, void *pc)); +void math_link(void (*rtSym)(const char* sym, void* pc)); bool string_create(); bool string_destroy(); -void string_link(void(*rtSym)(const char *sym, void *pc)); +void string_link(void (*rtSym)(const char* sym, void* pc)); bool stream_create(); bool stream_destroy(); -void stream_link(void(*rtSym)(const char *sym, void *pc)); +void stream_link(void (*rtSym)(const char* sym, void* pc)); bool sockets_create(); bool sockets_destroy(); -void sockets_link(void(*rtSym)(const char *sym, void *pc)); +void sockets_link(void (*rtSym)(const char* sym, void* pc)); bool filesystem_create(); bool filesystem_destroy(); -void filesystem_link(void(*rtSym)(const char *sym, void *pc)); +void filesystem_link(void (*rtSym)(const char* sym, void* pc)); bool bank_create(); bool bank_destroy(); -void bank_link(void(*rtSym)(const char *sym, void *pc)); +void bank_link(void (*rtSym)(const char* sym, void* pc)); bool graphics_create(); bool graphics_destroy(); -void graphics_link(void(*rtSym)(const char *sym, void *pc)); +void graphics_link(void (*rtSym)(const char* sym, void* pc)); bool input_create(); bool input_destroy(); -void input_link(void(*rtSym)(const char *sym, void *pc)); +void input_link(void (*rtSym)(const char* sym, void* pc)); bool audio_create(); bool audio_destroy(); -void audio_link(void(*rtSym)(const char *sym, void *pc)); +void audio_link(void (*rtSym)(const char* sym, void* pc)); //bool multiplay_create(); //bool multiplay_destroy(); //void multiplay_link( void (*rtSym)( const char *sym,void *pc ) ); bool userlibs_create(); void userlibs_destroy(); -void userlibs_link(void(*rtSym)(const char *sym, void *pc)); +void userlibs_link(void (*rtSym)(const char* sym, void* pc)); bool blitz3d_create(); bool blitz3d_destroy(); -void blitz3d_link(void(*rtSym)(const char *sym, void *pc)); - -void bbruntime_link(void(*rtSym)(const char *sym, void *pc)) { +void blitz3d_link(void (*rtSym)(const char* sym, void* pc)); +void bbruntime_link(void (*rtSym)(const char* sym, void* pc)) +{ rtSym("End", bbEnd); rtSym("Stop", bbStop); rtSym("AppTitle$title$close_prompt=\"\"", bbAppTitle); @@ -174,12 +202,14 @@ void bbruntime_link(void(*rtSym)(const char *sym, void *pc)) { } //start up error -static void sue(const char *t) { +static void sue(const char* t) +{ string p = string("Startup Error: ") + t; gx_runtime->debugInfo(p.c_str()); } -bool bbruntime_create() { +bool bbruntime_create() +{ if (basic_create()) { if (math_create()) { if (string_create()) { @@ -195,33 +225,45 @@ bool bbruntime_create() { if (userlibs_create()) { return true; } - } else sue("blitz3d_create failed"); + } else + sue("blitz3d_create failed"); // multiplay_destroy(); //}else sue( "multiplay_create failed" ); audio_destroy(); - } else sue("audio_create failed"); + } else + sue("audio_create failed"); input_destroy(); - } else sue("input_create failed"); + } else + sue("input_create failed"); graphics_destroy(); - } else sue("graphics_create failed"); + } else + sue("graphics_create failed"); bank_destroy(); - } else sue("bank_create failed"); + } else + sue("bank_create failed"); filesystem_destroy(); - } else sue("filesystem_create failed"); + } else + sue("filesystem_create failed"); sockets_destroy(); - } else sue("sockets_create failed"); + } else + sue("sockets_create failed"); stream_destroy(); - } else sue("stream_create failed"); + } else + sue("stream_create failed"); string_destroy(); - } else sue("string_create failed"); + } else + sue("string_create failed"); math_destroy(); - } else sue("math_create failed"); + } else + sue("math_create failed"); basic_destroy(); - } else sue("basic_create failed"); + } else + sue("basic_create failed"); return false; } -bool bbruntime_destroy() { +bool bbruntime_destroy() +{ userlibs_destroy(); blitz3d_destroy(); //multiplay_destroy(); @@ -238,14 +280,17 @@ bool bbruntime_destroy() { return true; } -const char *bbruntime_run(gxRuntime *rt, void(*pc)(), bool dbg) { - debug = dbg; +const char* bbruntime_run(gxRuntime* rt, void (*pc)(), bool dbg) +{ + debug = dbg; gx_runtime = rt; - if (!bbruntime_create()) return "Unable to start program"; - const char *t = 0; + if (!bbruntime_create()) + return "Unable to start program"; + const char* t = 0; try { - if (!gx_runtime->idle()) ThrowRuntimeException(0); + if (!gx_runtime->idle()) + ThrowRuntimeException(0); pc(); gx_runtime->debugInfo("Program has ended"); } catch (bbEx x) { @@ -255,6 +300,7 @@ const char *bbruntime_run(gxRuntime *rt, void(*pc)(), bool dbg) { return t; } -void bbruntime_panic(const char *err) { +void bbruntime_panic(const char* err) +{ ThrowRuntimeException(err); } diff --git a/Runtime/lib/bbruntime.hpp b/Runtime/lib/bbruntime.hpp index 8bff663..fe4dce2 100644 --- a/Runtime/lib/bbruntime.hpp +++ b/Runtime/lib/bbruntime.hpp @@ -12,10 +12,10 @@ To be statically linked with an appropriate gxruntime driver. #include "gxruntime.hpp" -void bbruntime_link( void (*rtSym)( const char *sym,void *pc ) ); +void bbruntime_link(void (*rtSym)(const char* sym, void* pc)); -const char *bbruntime_run( gxRuntime *runtime,void (*pc)(),bool debug ); +const char* bbruntime_run(gxRuntime* runtime, void (*pc)(), bool debug); -void bbruntime_panic( const char *err ); +void bbruntime_panic(const char* err); #endif diff --git a/Runtime/lib/bbsockets.cpp b/Runtime/lib/bbsockets.cpp index a9d5e33..842fe6a 100644 --- a/Runtime/lib/bbsockets.cpp +++ b/Runtime/lib/bbsockets.cpp @@ -1,14 +1,16 @@ -#include "std.h" -#include "bbsockets.h" +#include "bbsockets.hpp" +#include +#include "std.hpp" -static bool socks_ok; +static bool socks_ok; static WSADATA wsadata; -static int recv_timeout; -static int read_timeout; -static int accept_timeout; +static int recv_timeout; +static int read_timeout; +static int accept_timeout; -static void close(SOCKET sock, int e) { +static void close(SOCKET sock, int e) +{ if (e < 0) { int opt = 1; setsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char*)&opt, sizeof(opt)); @@ -25,12 +27,12 @@ static set tcp_set; static set server_set; class UDPStream : public bbStream { -public: + public: UDPStream(SOCKET s); ~UDPStream(); - int read(char *buff, int size); - int write(const char *buff, int size); + int read(char* buff, int size); + int write(const char* buff, int size); int avail(); int eof(); @@ -41,69 +43,92 @@ public: int getMsgIP(); int getMsgPort(); -private: - SOCKET sock; + private: + SOCKET sock; vector in_buf, out_buf; - sockaddr_in addr, in_addr, out_addr; - int in_get, e; + sockaddr_in addr, in_addr, out_addr; + int in_get, e; }; -UDPStream::UDPStream(SOCKET s) :sock(s), in_get(0), e(0) { +UDPStream::UDPStream(SOCKET s) : sock(s), in_get(0), e(0) +{ int len = sizeof(addr); getsockname(s, (sockaddr*)&addr, &len); in_addr = out_addr = addr; } -UDPStream::~UDPStream() { +UDPStream::~UDPStream() +{ close(sock, e); } -int UDPStream::read(char *buff, int size) { - if (e) return 0; +int UDPStream::read(char* buff, int size) +{ + if (e) + return 0; int n = in_buf.size() - in_get; - if (n < size) size = n; + if (n < size) + size = n; memcpy(buff, &in_buf[in_get], size); in_get += size; return size; } -int UDPStream::write(const char *buff, int size) { - if (e) return 0; +int UDPStream::write(const char* buff, int size) +{ + if (e) + return 0; out_buf.insert(out_buf.end(), buff, buff + size); return size; } -int UDPStream::avail() { - if (e) return 0; +int UDPStream::avail() +{ + if (e) + return 0; return in_buf.size() - in_get; } -int UDPStream::eof() { +int UDPStream::eof() +{ return e ? e : in_get == in_buf.size(); } //fill buffer, return sender -int UDPStream::recv() { - if (e) return 0; +int UDPStream::recv() +{ + if (e) + return 0; int tout; - if (recv_timeout) tout = gx_runtime->getMilliSecs() + recv_timeout; + if (recv_timeout) + tout = gx_runtime->getMilliSecs() + recv_timeout; for (;;) { int dt = 0; if (recv_timeout) { dt = tout - gx_runtime->getMilliSecs(); - if (dt < 0) dt = 0; + if (dt < 0) + dt = 0; + } + fd_set fd = {1, sock}; + timeval tv = {dt / 1000, (dt % 1000) * 1000}; + int n = ::select(0, &fd, 0, 0, &tv); + if (!n) + return 0; + if (n != 1) { + e = -1; + return 0; } - fd_set fd = { 1,sock }; - timeval tv = { dt / 1000,(dt % 1000) * 1000 }; - int n = ::select(0, &fd, 0, 0, &tv); - if (!n) return 0; - if (n != 1) { e = -1; return 0; } unsigned long sz = -1; - if (ioctlsocket(sock, FIONREAD, &sz)) { e = -1; return 0; } - in_buf.resize(sz); in_get = 0; + if (ioctlsocket(sock, FIONREAD, &sz)) { + e = -1; + return 0; + } + in_buf.resize(sz); + in_get = 0; int len = sizeof(in_addr); - n = ::recvfrom(sock, &(in_buf.begin())[0], sz, 0, (sockaddr*)&in_addr, &len); - if (n == SOCKET_ERROR) continue; //{ e=-1;return 0; } + n = ::recvfrom(sock, &(in_buf.begin())[0], sz, 0, (sockaddr*)&in_addr, &len); + if (n == SOCKET_ERROR) + continue; //{ e=-1;return 0; } in_buf.resize(n); return getMsgIP(); } @@ -111,179 +136,234 @@ int UDPStream::recv() { } //send, empty buffer -int UDPStream::send(int ip, int port) { - if (e) return 0; - int sz = out_buf.size(); +int UDPStream::send(int ip, int port) +{ + if (e) + return 0; + int sz = out_buf.size(); out_addr.sin_addr.S_un.S_addr = htonl(ip); - out_addr.sin_port = htons(port ? port : addr.sin_port); + out_addr.sin_port = htons(port ? port : addr.sin_port); int n = ::sendto(sock, &(out_buf.begin())[0], sz, 0, (sockaddr*)&out_addr, sizeof(out_addr)); - if (n != sz) return e = -1; + if (n != sz) + return e = -1; out_buf.clear(); return sz; } -int UDPStream::getIP() { +int UDPStream::getIP() +{ return ntohl(addr.sin_addr.S_un.S_addr); } -int UDPStream::getPort() { +int UDPStream::getPort() +{ return ntohs(addr.sin_port); } -int UDPStream::getMsgIP() { +int UDPStream::getMsgIP() +{ return ntohl(in_addr.sin_addr.S_un.S_addr); } -int UDPStream::getMsgPort() { +int UDPStream::getMsgPort() +{ return ntohs(in_addr.sin_port); } class TCPStream : public bbStream { -public: - TCPStream(SOCKET s, TCPServer *t); + public: + TCPStream(SOCKET s, TCPServer* t); ~TCPStream(); - int read(char *buff, int size); - int write(const char *buff, int size); + int read(char* buff, int size); + int write(const char* buff, int size); int avail(); int eof(); int getIP(); int getPort(); -private: - SOCKET sock; - TCPServer *server; - int e, ip, port; + private: + SOCKET sock; + TCPServer* server; + int e, ip, port; }; class TCPServer { -public: + public: TCPServer(SOCKET S); ~TCPServer(); - TCPStream *accept(); + TCPStream* accept(); - void remove(TCPStream *s); + void remove(TCPStream* s); -private: - int e; - SOCKET sock; + private: + int e; + SOCKET sock; set accepted_set; }; -TCPStream::TCPStream(SOCKET s, TCPServer *t) :sock(s), server(t), e(0) { +TCPStream::TCPStream(SOCKET s, TCPServer* t) : sock(s), server(t), e(0) +{ sockaddr_in addr; - int len = sizeof(addr); + int len = sizeof(addr); if (getpeername(s, (sockaddr*)&addr, &len)) { ip = port = 0; return; } - ip = ntohl(addr.sin_addr.S_un.S_addr); + ip = ntohl(addr.sin_addr.S_un.S_addr); port = ntohs(addr.sin_port); } -TCPStream::~TCPStream() { - if (server) server->remove(this); +TCPStream::~TCPStream() +{ + if (server) + server->remove(this); close(sock, e); } -int TCPStream::read(char *buff, int size) { - if (e) return 0; +int TCPStream::read(char* buff, int size) +{ + if (e) + return 0; char *b = buff, *l = buff + size; - int tout; - if (read_timeout) tout = gx_runtime->getMilliSecs() + read_timeout; + int tout; + if (read_timeout) + tout = gx_runtime->getMilliSecs() + read_timeout; while (b < l) { int dt = 0; if (read_timeout) { dt = tout - gx_runtime->getMilliSecs(); - if (dt < 0) dt = 0; + if (dt < 0) + dt = 0; + } + fd_set fd = {1, sock}; + timeval tv = {dt / 1000, (dt % 1000) * 1000}; + int n = ::select(0, &fd, 0, 0, &tv); + if (n != 1) { + e = -1; + break; } - fd_set fd = { 1,sock }; - timeval tv = { dt / 1000,(dt % 1000) * 1000 }; - int n = ::select(0, &fd, 0, 0, &tv); - if (n != 1) { e = -1; break; } n = ::recv(sock, b, l - b, 0); - if (n == 0) { e = 1; break; } - if (n == SOCKET_ERROR) { e = -1; break; } + if (n == 0) { + e = 1; + break; + } + if (n == SOCKET_ERROR) { + e = -1; + break; + } b += n; } return b - buff; } -int TCPStream::write(const char *buff, int size) { - if (e) return 0; +int TCPStream::write(const char* buff, int size) +{ + if (e) + return 0; int n = ::send(sock, buff, size, 0); - if (n == SOCKET_ERROR) { e = -1; return 0; } + if (n == SOCKET_ERROR) { + e = -1; + return 0; + } return n; } -int TCPStream::avail() { +int TCPStream::avail() +{ unsigned long t; - int n = ::ioctlsocket(sock, FIONREAD, &t); - if (n == SOCKET_ERROR) { e = -1; return 0; } + int n = ::ioctlsocket(sock, FIONREAD, &t); + if (n == SOCKET_ERROR) { + e = -1; + return 0; + } return t; } -int TCPStream::eof() { - if (e) return e; - fd_set fd = { 1,sock }; - timeval tv = { 0,0 }; +int TCPStream::eof() +{ + if (e) + return e; + fd_set fd = {1, sock}; + timeval tv = {0, 0}; switch (::select(0, &fd, 0, 0, &tv)) { - case 0:break; - case 1:if (!avail()) e = 1; break; - default:e = -1; + case 0: + break; + case 1: + if (!avail()) + e = 1; + break; + default: + e = -1; } return e; } -int TCPStream::getIP() { +int TCPStream::getIP() +{ return ip; } -int TCPStream::getPort() { +int TCPStream::getPort() +{ return port; } -TCPServer::TCPServer(SOCKET s) :sock(s), e(0) { -} +TCPServer::TCPServer(SOCKET s) : sock(s), e(0) {} -TCPServer::~TCPServer() { - while (accepted_set.size()) delete *accepted_set.begin(); +TCPServer::~TCPServer() +{ + while (accepted_set.size()) + delete *accepted_set.begin(); close(sock, e); } -TCPStream *TCPServer::accept() { - if (e) return 0; - fd_set fd = { 1,sock }; - timeval tv = { accept_timeout / 1000,(accept_timeout % 1000) * 1000 }; - int n = ::select(0, &fd, 0, 0, &tv); - if (n == 0) return 0; - if (n != 1) { e = -1; return 0; } +TCPStream* TCPServer::accept() +{ + if (e) + return 0; + fd_set fd = {1, sock}; + timeval tv = {accept_timeout / 1000, (accept_timeout % 1000) * 1000}; + int n = ::select(0, &fd, 0, 0, &tv); + if (n == 0) + return 0; + if (n != 1) { + e = -1; + return 0; + } SOCKET t = ::accept(sock, 0, 0); - if (t == INVALID_SOCKET) { e = -1; return 0; } - TCPStream *s = new TCPStream(t, this); + if (t == INVALID_SOCKET) { + e = -1; + return 0; + } + TCPStream* s = new TCPStream(t, this); accepted_set.insert(s); return s; } -void TCPServer::remove(TCPStream *s) { +void TCPServer::remove(TCPStream* s) +{ accepted_set.erase(s); } -static inline void debugUDPStream(UDPStream *p) { +static inline void debugUDPStream(UDPStream* p) +{ if (debug && !udp_set.count(p)) { ThrowRuntimeException("UDP Stream does not exist"); } } -static inline void debugTCPStream(TCPStream *p) { +static inline void debugTCPStream(TCPStream* p) +{ if (debug && !tcp_set.count(p)) { ThrowRuntimeException("TCP Stream does not exist"); } } -static inline void debugTCPServer(TCPServer *p) { +static inline void debugTCPServer(TCPServer* p) +{ if (debug && !server_set.count(p)) { ThrowRuntimeException("TCP Server does not exist"); } @@ -291,19 +371,24 @@ static inline void debugTCPServer(TCPServer *p) { static vector host_ips; -int bbCountHostIPs(BBStr *host) { +int bbCountHostIPs(BBStr* host) +{ host_ips.clear(); - HOSTENT *h = gethostbyname(host->c_str()); - delete host; if (!h) return 0; - char **p = h->h_addr_list; - while (char *t = *p++) host_ips.push_back(ntohl(*(int*)t)); + HOSTENT* h = gethostbyname(host->c_str()); + delete host; + if (!h) + return 0; + char** p = h->h_addr_list; + while (char* t = *p++) + host_ips.push_back(ntohl(*(int*)t)); return host_ips.size(); } -int bbHostIP(int index) { +int bbHostIP(int index) +{ #ifdef _DEBUG if (debug) { - if (index<1 || index>host_ips.size()) { + if (index < 1 || index > host_ips.size()) { ThrowRuntimeException("Host index out of range"); } } @@ -311,13 +396,15 @@ int bbHostIP(int index) { return host_ips[index - 1]; } -UDPStream *bbCreateUDPStream(int port) { - if (!socks_ok) return 0; +UDPStream* bbCreateUDPStream(int port) +{ + if (!socks_ok) + return 0; SOCKET s = ::socket(AF_INET, SOCK_DGRAM, 0); if (s != INVALID_SOCKET) { - sockaddr_in addr = { AF_INET,htons(port) }; + sockaddr_in addr = {AF_INET, htons(port)}; if (!::bind(s, (sockaddr*)&addr, sizeof(addr))) { - UDPStream *p = new UDPStream(s); + UDPStream* p = new UDPStream(s); udp_set.insert(p); return p; } @@ -326,84 +413,98 @@ UDPStream *bbCreateUDPStream(int port) { return 0; } -void bbCloseUDPStream(UDPStream *p) { +void bbCloseUDPStream(UDPStream* p) +{ debugUDPStream(p); udp_set.erase(p); delete p; } -int bbRecvUDPMsg(UDPStream *p) { +int bbRecvUDPMsg(UDPStream* p) +{ debugUDPStream(p); return p->recv(); } -void bbSendUDPMsg(UDPStream *p, int ip, int port) { +void bbSendUDPMsg(UDPStream* p, int ip, int port) +{ debugUDPStream(p); p->send(ip, port); } -int bbUDPStreamIP(UDPStream *p) { +int bbUDPStreamIP(UDPStream* p) +{ debugUDPStream(p); return p->getIP(); } -int bbUDPStreamPort(UDPStream *p) { +int bbUDPStreamPort(UDPStream* p) +{ debugUDPStream(p); return p->getPort(); } -int bbUDPMsgIP(UDPStream *p) { +int bbUDPMsgIP(UDPStream* p) +{ debugUDPStream(p); return p->getMsgIP(); } -int bbUDPMsgPort(UDPStream *p) { +int bbUDPMsgPort(UDPStream* p) +{ debugUDPStream(p); return p->getMsgPort(); } -void bbUDPTimeouts(int rt) { +void bbUDPTimeouts(int rt) +{ recv_timeout = rt; } -BBStr *bbDottedIP(int ip) { - return new BBStr( - itoa((ip >> 24) & 255) + "." + itoa((ip >> 16) & 255) + "." + - itoa((ip >> 8) & 255) + "." + itoa(ip & 255)); +BBStr* bbDottedIP(int ip) +{ + return new BBStr(itoa((ip >> 24) & 255) + "." + itoa((ip >> 16) & 255) + "." + itoa((ip >> 8) & 255) + "." + + itoa(ip & 255)); } -static int findHostIP(const string &t) { +static int findHostIP(const string& t) +{ int ip = inet_addr(t.c_str()); - if (ip != INADDR_NONE) return ip; - HOSTENT *h = gethostbyname(t.c_str()); - if (!h) return -1; - char *p; - for (char **list = h->h_addr_list; p = *list; ++list) { + if (ip != INADDR_NONE) + return ip; + HOSTENT* h = gethostbyname(t.c_str()); + if (!h) + return -1; + char* p; + for (char** list = h->h_addr_list; p = *list; ++list) { return *(int*)p; } return 0; } -TCPStream *bbOpenTCPStream(BBStr *server, int port, int local_port) { +TCPStream* bbOpenTCPStream(BBStr* server, int port, int local_port) +{ if (!socks_ok) { delete server; return 0; } - int ip = findHostIP(*server); delete server; - if (ip == -1) return 0; + int ip = findHostIP(*server); + delete server; + if (ip == -1) + return 0; SOCKET s = ::socket(AF_INET, SOCK_STREAM, 0); if (s != INVALID_SOCKET) { if (local_port) { - sockaddr_in addr = { AF_INET,htons(local_port) }; + sockaddr_in addr = {AF_INET, htons(local_port)}; if (::bind(s, (sockaddr*)&addr, sizeof(addr))) { ::closesocket(s); return 0; } } - sockaddr_in addr = { AF_INET,htons(port) }; + sockaddr_in addr = {AF_INET, htons(port)}; addr.sin_addr.S_un.S_addr = ip; if (!::connect(s, (sockaddr*)&addr, sizeof(addr))) { - TCPStream *p = new TCPStream(s, 0); + TCPStream* p = new TCPStream(s, 0); tcp_set.insert(p); return p; } @@ -412,19 +513,21 @@ TCPStream *bbOpenTCPStream(BBStr *server, int port, int local_port) { return 0; } -void bbCloseTCPStream(TCPStream *p) { +void bbCloseTCPStream(TCPStream* p) +{ debugTCPStream(p); tcp_set.erase(p); delete p; } -TCPServer * bbCreateTCPServer(int port) { +TCPServer* bbCreateTCPServer(int port) +{ SOCKET s = ::socket(AF_INET, SOCK_STREAM, 0); if (s != INVALID_SOCKET) { - sockaddr_in addr = { AF_INET,htons(port) }; + sockaddr_in addr = {AF_INET, htons(port)}; if (!::bind(s, (sockaddr*)&addr, sizeof(addr))) { if (!::listen(s, SOMAXCONN)) { - TCPServer *p = new TCPServer(s); + TCPServer* p = new TCPServer(s); server_set.insert(p); return p; } @@ -434,54 +537,67 @@ TCPServer * bbCreateTCPServer(int port) { return 0; } -void bbCloseTCPServer(TCPServer *p) { +void bbCloseTCPServer(TCPServer* p) +{ debugTCPServer(p); server_set.erase(p); delete p; } -TCPStream * bbAcceptTCPStream(TCPServer *server) { +TCPStream* bbAcceptTCPStream(TCPServer* server) +{ debugTCPServer(server); - if (!gx_runtime->idle()) ThrowRuntimeException(0); - if (TCPStream *tcp = server->accept()) { + if (!gx_runtime->idle()) + ThrowRuntimeException(0); + if (TCPStream* tcp = server->accept()) { tcp_set.insert(tcp); return tcp; } return 0; } -int bbTCPStreamIP(TCPStream *p) { +int bbTCPStreamIP(TCPStream* p) +{ debugTCPStream(p); return p->getIP(); } -int bbTCPStreamPort(TCPStream *p) { +int bbTCPStreamPort(TCPStream* p) +{ debugTCPStream(p); return p->getPort(); } -void bbTCPTimeouts(int rt, int at) { - read_timeout = rt; +void bbTCPTimeouts(int rt, int at) +{ + read_timeout = rt; accept_timeout = at; } -bool sockets_create() { - socks_ok = WSAStartup(0x0101, &wsadata) == 0; - recv_timeout = 0; - read_timeout = 10000; +bool sockets_create() +{ + socks_ok = WSAStartup(0x0101, &wsadata) == 0; + recv_timeout = 0; + read_timeout = 10000; accept_timeout = 0; return true; } -bool sockets_destroy() { - while (udp_set.size()) bbCloseUDPStream(*udp_set.begin()); - while (tcp_set.size()) bbCloseTCPStream(*tcp_set.begin()); - while (server_set.size()) bbCloseTCPServer(*server_set.begin()); - if (socks_ok) WSACleanup(); +bool sockets_destroy() +{ + while (udp_set.size()) + bbCloseUDPStream(*udp_set.begin()); + while (tcp_set.size()) + bbCloseTCPStream(*tcp_set.begin()); + while (server_set.size()) + bbCloseTCPServer(*server_set.begin()); + if (socks_ok) + WSACleanup(); return true; } -void sockets_link(void(*rtSym)(const char*, void*)) { +void sockets_link(void (*rtSym)(const char*, void*)) +{ rtSym("$DottedIP%IP", bbDottedIP); rtSym("%CountHostIPs$host_name", bbCountHostIPs); rtSym("%HostIP%host_index", bbHostIP); diff --git a/Runtime/lib/bbsockets.hpp b/Runtime/lib/bbsockets.hpp index 21d79e9..83ca54a 100644 --- a/Runtime/lib/bbsockets.hpp +++ b/Runtime/lib/bbsockets.hpp @@ -2,7 +2,6 @@ #ifndef BBSOCKETS_H #define BBSOCKETS_H -#include "bbstream.h" +#include "bbstream.hpp" #endif - diff --git a/Runtime/lib/bbstream.cpp b/Runtime/lib/bbstream.cpp index d35e66d..04ad25a 100644 --- a/Runtime/lib/bbstream.cpp +++ b/Runtime/lib/bbstream.cpp @@ -1,160 +1,199 @@ -#include "std.h" -#include "bbstream.h" +#include "bbstream.hpp" +#include "std.hpp" static set stream_set; -void debugStream( bbStream *s ){ - if( stream_set.count(s) ) return; - ThrowRuntimeException( "Stream does not exist" ); +void debugStream(bbStream* s) +{ + if (stream_set.count(s)) + return; + ThrowRuntimeException("Stream does not exist"); } -bbStream::bbStream(){ - stream_set.insert( this ); +bbStream::bbStream() +{ + stream_set.insert(this); } -bbStream::~bbStream(){ - stream_set.erase( this ); +bbStream::~bbStream() +{ + stream_set.erase(this); } -int bbEof( bbStream *s ){ - if( debug ) debugStream( s ); +int bbEof(bbStream* s) +{ + if (debug) + debugStream(s); return s->eof(); } -int bbReadAvail( bbStream *s ){ - if( debug ) debugStream( s ); +int bbReadAvail(bbStream* s) +{ + if (debug) + debugStream(s); return s->avail(); } -int bbReadByte( bbStream *s ){ - if( debug ) debugStream( s ); - int n=0; - s->read( (char*)&n,1 ); +int bbReadByte(bbStream* s) +{ + if (debug) + debugStream(s); + int n = 0; + s->read((char*)&n, 1); return n; } -int bbReadShort( bbStream *s ){ - if( debug ) debugStream( s ); - int n=0; - s->read( (char*)&n,2 ); +int bbReadShort(bbStream* s) +{ + if (debug) + debugStream(s); + int n = 0; + s->read((char*)&n, 2); return n; } -int bbReadInt( bbStream *s ){ - if( debug ) debugStream( s ); - int n=0; - s->read( (char*)&n,4 ); +int bbReadInt(bbStream* s) +{ + if (debug) + debugStream(s); + int n = 0; + s->read((char*)&n, 4); return n; } -float bbReadFloat( bbStream *s ){ - if( debug ) debugStream( s ); - float n=0; - s->read( (char*)&n,4 ); +float bbReadFloat(bbStream* s) +{ + if (debug) + debugStream(s); + float n = 0; + s->read((char*)&n, 4); return n; } -BBStr *bbReadString( bbStream *s ){ - if( debug ) debugStream( s ); - int len; - BBStr *str=new BBStr(); - if( s->read( (char*)&len,4 ) ){ - char *buff=new char[len]; - if( s->read( buff,len ) ){ - *str=string( buff,len ); +BBStr* bbReadString(bbStream* s) +{ + if (debug) + debugStream(s); + int len; + BBStr* str = new BBStr(); + if (s->read((char*)&len, 4)) { + char* buff = new char[len]; + if (s->read(buff, len)) { + *str = string(buff, len); } delete[] buff; } return str; } -BBStr *bbReadLine( bbStream *s ){ - if( debug ) debugStream( s ); +BBStr* bbReadLine(bbStream* s) +{ + if (debug) + debugStream(s); unsigned char c; - BBStr *str=new BBStr(); - for(;;){ - if( s->read( (char*)&c,1 )!=1 ) break; - if( c=='\n' ) break; - if( c!='\r' ) *str+=c; + BBStr* str = new BBStr(); + for (;;) { + if (s->read((char*)&c, 1) != 1) + break; + if (c == '\n') + break; + if (c != '\r') + *str += c; } return str; } -void bbWriteByte( bbStream *s,int n ){ - if( debug ) debugStream( s ); - s->write( (char*)&n,1 ); +void bbWriteByte(bbStream* s, int n) +{ + if (debug) + debugStream(s); + s->write((char*)&n, 1); } -void bbWriteShort( bbStream *s,int n ){ - if( debug ) debugStream( s ); - s->write( (char*)&n,2 ); +void bbWriteShort(bbStream* s, int n) +{ + if (debug) + debugStream(s); + s->write((char*)&n, 2); } -void bbWriteInt( bbStream *s,int n ){ - if( debug ) debugStream( s ); - s->write( (char*)&n,4 ); +void bbWriteInt(bbStream* s, int n) +{ + if (debug) + debugStream(s); + s->write((char*)&n, 4); } -void bbWriteFloat( bbStream *s,float n ){ - if( debug ) debugStream( s ); - s->write( (char*)&n,4 ); +void bbWriteFloat(bbStream* s, float n) +{ + if (debug) + debugStream(s); + s->write((char*)&n, 4); } -void bbWriteString( bbStream *s,BBStr *t ){ - if( debug ) debugStream( s ); - int n=t->size(); - s->write( (char*)&n,4 ); - s->write( t->data(),t->size() ); +void bbWriteString(bbStream* s, BBStr* t) +{ + if (debug) + debugStream(s); + int n = t->size(); + s->write((char*)&n, 4); + s->write(t->data(), t->size()); delete t; } -void bbWriteLine( bbStream *s,BBStr *t ){ - if( debug ) debugStream( s ); - s->write( t->data(),t->size() ); - s->write( "\r\n",2 ); +void bbWriteLine(bbStream* s, BBStr* t) +{ + if (debug) + debugStream(s); + s->write(t->data(), t->size()); + s->write("\r\n", 2); delete t; } -void bbCopyStream( bbStream *s,bbStream *d,int buff_size ){ - if( debug ){ - debugStream( s );debugStream( d ); - if( buff_size<1 || buff_size>1024*1024 ) ThrowRuntimeException( "Illegal buffer size" ); +void bbCopyStream(bbStream* s, bbStream* d, int buff_size) +{ + if (debug) { + debugStream(s); + debugStream(d); + if (buff_size < 1 || buff_size > 1024 * 1024) + ThrowRuntimeException("Illegal buffer size"); } - char *buff=new char[buff_size]; - while( s->eof()==0 && d->eof()==0 ){ - int n=s->read( buff,buff_size ); - d->write( buff,n ); - if( neof() == 0 && d->eof() == 0) { + int n = s->read(buff, buff_size); + d->write(buff, n); + if (n < buff_size) + break; } delete buff; } -bool stream_create(){ +bool stream_create() +{ return true; } -bool stream_destroy(){ +bool stream_destroy() +{ return true; } -void stream_link( void(*rtSym)(const char*,void*) ){ - rtSym( "%Eof%stream",bbEof ); - rtSym( "%ReadAvail%stream",bbReadAvail ); - rtSym( "%ReadByte%stream",bbReadByte ); - rtSym( "%ReadShort%stream",bbReadShort ); - rtSym( "%ReadInt%stream",bbReadInt ); - rtSym( "#ReadFloat%stream",bbReadFloat ); - rtSym( "$ReadString%stream",bbReadString ); - rtSym( "$ReadLine%stream",bbReadLine ); - rtSym( "WriteByte%stream%byte",bbWriteByte ); - rtSym( "WriteShort%stream%short",bbWriteShort ); - rtSym( "WriteInt%stream%int",bbWriteInt ); - rtSym( "WriteFloat%stream#float",bbWriteFloat ); - rtSym( "WriteString%stream$string",bbWriteString ); - rtSym( "WriteLine%stream$string",bbWriteLine ); - rtSym( "CopyStream%src_stream%dest_stream%buffer_size=16384",bbCopyStream ); +void stream_link(void (*rtSym)(const char*, void*)) +{ + rtSym("%Eof%stream", bbEof); + rtSym("%ReadAvail%stream", bbReadAvail); + rtSym("%ReadByte%stream", bbReadByte); + rtSym("%ReadShort%stream", bbReadShort); + rtSym("%ReadInt%stream", bbReadInt); + rtSym("#ReadFloat%stream", bbReadFloat); + rtSym("$ReadString%stream", bbReadString); + rtSym("$ReadLine%stream", bbReadLine); + rtSym("WriteByte%stream%byte", bbWriteByte); + rtSym("WriteShort%stream%short", bbWriteShort); + rtSym("WriteInt%stream%int", bbWriteInt); + rtSym("WriteFloat%stream#float", bbWriteFloat); + rtSym("WriteString%stream$string", bbWriteString); + rtSym("WriteLine%stream$string", bbWriteLine); + rtSym("CopyStream%src_stream%dest_stream%buffer_size=16384", bbCopyStream); } - - - diff --git a/Runtime/lib/bbstream.hpp b/Runtime/lib/bbstream.hpp index 6867b2d..617a944 100644 --- a/Runtime/lib/bbstream.hpp +++ b/Runtime/lib/bbstream.hpp @@ -4,28 +4,26 @@ #include "bbsys.hpp" -class bbStream{ -public: - enum{ - EOF_ERROR=-1,EOF_NOT=0,EOF_OK=1 - }; +class bbStream { + public: + enum { EOF_ERROR = -1, EOF_NOT = 0, EOF_OK = 1 }; bbStream(); virtual ~bbStream(); //returns chars read - virtual int read( char *buff,int size )=0; + virtual int read(char* buff, int size) = 0; //returns chars written - virtual int write( const char *buff,int size )=0; + virtual int write(const char* buff, int size) = 0; //returns chars avilable for reading - virtual int avail()=0; + virtual int avail() = 0; //returns EOF status - virtual int eof()=0; + virtual int eof() = 0; }; -void debugStream( bbStream *s ); +void debugStream(bbStream* s); #endif \ No newline at end of file diff --git a/Runtime/lib/bbstring.cpp b/Runtime/lib/bbstring.cpp index 3bd33bd..9ec8079 100644 --- a/Runtime/lib/bbstring.cpp +++ b/Runtime/lib/bbstring.cpp @@ -1,162 +1,213 @@ -#include "bbsys.hpp" #include +#include "bbsys.hpp" -#define CHKPOS(x) if( (x)<0 ) ThrowRuntimeException( "parameter must be positive" ); -#define CHKOFF(x) if( (x)<=0 ) ThrowRuntimeException( "parameter must be greater than 0" ); +#define CHKPOS(x) \ + if ((x) < 0) \ + ThrowRuntimeException("parameter must be positive"); +#define CHKOFF(x) \ + if ((x) <= 0) \ + ThrowRuntimeException("parameter must be greater than 0"); -BBStr *bbString( BBStr *s,int n ){ - BBStr *t=new BBStr(); - while( n-->0 ) *t+=*s; - delete s;return t; +BBStr* bbString(BBStr* s, int n) +{ + BBStr* t = new BBStr(); + while (n-- > 0) + *t += *s; + delete s; + return t; } -BBStr *bbLeft( BBStr *s,int n ){ - CHKPOS( n ); - *s=s->substr( 0,n );return s; -} - -BBStr *bbRight( BBStr *s,int n ){ - CHKPOS( n ); - n=s->size()-n;if( n<0 ) n=0; - *s=s->substr( n );return s; -} - -BBStr *bbReplace( BBStr *s,BBStr *from,BBStr *to ){ - int n=0,from_sz=from->size(),to_sz=to->size(); - while( nsize() && (n=s->find( *from,n ))!=string::npos ){ - s->replace( n,from_sz,*to ); - n+=to_sz; - } - delete from;delete to;return s; -} - -int bbInstr( BBStr *s,BBStr *t,int from ){ - CHKOFF( from );--from; - int n=s->find( *t,from ); - delete s;delete t; - return n==string::npos ? 0 : n+1; -} - -BBStr *bbMid( BBStr *s,int o,int n ){ - CHKOFF( o );--o; - if( o>s->size() ) o=s->size(); - if( n>=0 ) *s=s->substr( o,n ); - else *s=s->substr( o ); - return s; -} - -BBStr *bbUpper( BBStr *s ){ - for( int k=0;ksize();++k ) (*s)[k]=toupper( (*s)[k] ); - return s; -} - -BBStr *bbLower( BBStr *s ){ - for( int k=0;ksize();++k ) (*s)[k]=tolower( (*s)[k] ); - return s; -} - -BBStr *bbTrim( BBStr *s ){ - int n=0,p=s->size(); - while( nsize() && !isgraph( (*s)[n] ) ) ++n; - while( p>n && !isgraph( (*s)[p-1] ) ) --p; - *s=s->substr( n,p-n );return s; -} - -BBStr *bbLSet( BBStr *s,int n ){ +BBStr* bbLeft(BBStr* s, int n) +{ CHKPOS(n); - if( s->size()>n ) *s=s->substr( 0,n ); - else{ - while( s->size()substr(0, n); return s; } -BBStr *bbRSet( BBStr *s,int n ){ +BBStr* bbRight(BBStr* s, int n) +{ CHKPOS(n); - if( s->size()>n ) *s=s->substr( s->size()-n ); - else{ - while( s->size()size() - n; + if (n < 0) + n = 0; + *s = s->substr(n); + return s; +} + +BBStr* bbReplace(BBStr* s, BBStr* from, BBStr* to) +{ + int n = 0, from_sz = from->size(), to_sz = to->size(); + while (n < s->size() && (n = s->find(*from, n)) != string::npos) { + s->replace(n, from_sz, *to); + n += to_sz; + } + delete from; + delete to; + return s; +} + +int bbInstr(BBStr* s, BBStr* t, int from) +{ + CHKOFF(from); + --from; + int n = s->find(*t, from); + delete s; + delete t; + return n == string::npos ? 0 : n + 1; +} + +BBStr* bbMid(BBStr* s, int o, int n) +{ + CHKOFF(o); + --o; + if (o > s->size()) + o = s->size(); + if (n >= 0) + *s = s->substr(o, n); + else + *s = s->substr(o); + return s; +} + +BBStr* bbUpper(BBStr* s) +{ + for (int k = 0; k < s->size(); ++k) + (*s)[k] = toupper((*s)[k]); + return s; +} + +BBStr* bbLower(BBStr* s) +{ + for (int k = 0; k < s->size(); ++k) + (*s)[k] = tolower((*s)[k]); + return s; +} + +BBStr* bbTrim(BBStr* s) +{ + int n = 0, p = s->size(); + while (n < s->size() && !isgraph((*s)[n])) + ++n; + while (p > n && !isgraph((*s)[p - 1])) + --p; + *s = s->substr(n, p - n); + return s; +} + +BBStr* bbLSet(BBStr* s, int n) +{ + CHKPOS(n); + if (s->size() > n) + *s = s->substr(0, n); + else { + while (s->size() < n) + *s += ' '; } return s; } -BBStr *bbChr( int n ){ - BBStr *t=new BBStr(); - *t+=(char)n;return t; +BBStr* bbRSet(BBStr* s, int n) +{ + CHKPOS(n); + if (s->size() > n) + *s = s->substr(s->size() - n); + else { + while (s->size() < n) + *s = ' ' + *s; + } + return s; } -BBStr *bbHex( int n ){ +BBStr* bbChr(int n) +{ + BBStr* t = new BBStr(); + *t += (char)n; + return t; +} + +BBStr* bbHex(int n) +{ char buff[12]; - for( int k=7;k>=0;n>>=4,--k ){ - int t=(n&15)+'0'; - buff[k]=t>'9' ? t+='A'-'9'-1 : t; + for (int k = 7; k >= 0; n >>= 4, --k) { + int t = (n & 15) + '0'; + buff[k] = t > '9' ? t += 'A' - '9' - 1 : t; } - buff[8]=0; - return new BBStr( buff ); + buff[8] = 0; + return new BBStr(buff); } -BBStr *bbBin( int n ){ +BBStr* bbBin(int n) +{ char buff[36]; - for( int k=31;k>=0;n>>=1,--k ){ - buff[k]=n&1 ? '1' : '0'; + for (int k = 31; k >= 0; n >>= 1, --k) { + buff[k] = n & 1 ? '1' : '0'; } - buff[32]=0; - return new BBStr( buff ); + buff[32] = 0; + return new BBStr(buff); } -int bbAsc( BBStr *s ){ - int n=s->size() ? (*s)[0] & 255 : -1; - delete s;return n; +int bbAsc(BBStr* s) +{ + int n = s->size() ? (*s)[0] & 255 : -1; + delete s; + return n; } -int bbLen( BBStr *s ){ - int n=s->size(); - delete s;return n; +int bbLen(BBStr* s) +{ + int n = s->size(); + delete s; + return n; } -BBStr *bbCurrentDate(){ +BBStr* bbCurrentDate() +{ time_t t; - time( &t ); + time(&t); char buff[256]; - strftime( buff,256,"%d %b %Y",localtime( &t ) ); - return new BBStr( buff ); + strftime(buff, 256, "%d %b %Y", localtime(&t)); + return new BBStr(buff); } -BBStr *bbCurrentTime(){ +BBStr* bbCurrentTime() +{ time_t t; - time( &t ); + time(&t); char buff[256]; - strftime( buff,256,"%H:%M:%S",localtime( &t ) ); - return new BBStr( buff ); + strftime(buff, 256, "%H:%M:%S", localtime(&t)); + return new BBStr(buff); } -bool string_create(){ +bool string_create() +{ return true; } -bool string_destroy(){ +bool string_destroy() +{ return true; } -void string_link( void(*rtSym)(const char*,void*) ){ - rtSym( "$String$string%repeat",bbString ); - rtSym( "$Left$string%count",bbLeft ); - rtSym( "$Right$string%count",bbRight ); - rtSym( "$Replace$string$from$to",bbReplace ); - rtSym( "%Instr$string$find%from=1",bbInstr ); - rtSym( "$Mid$string%start%count=-1",bbMid ); - rtSym( "$Upper$string",bbUpper ); - rtSym( "$Lower$string",bbLower ); - rtSym( "$Trim$string",bbTrim ); - rtSym( "$LSet$string%size",bbLSet ); - rtSym( "$RSet$string%size",bbRSet ); - rtSym( "$Chr%ascii",bbChr ); - rtSym( "%Asc$string",bbAsc ); - rtSym( "%Len$string",bbLen ); - rtSym( "$Hex%value",bbHex ); - rtSym( "$Bin%value",bbBin ); - rtSym( "$CurrentDate",bbCurrentDate ); - rtSym( "$CurrentTime",bbCurrentTime ); +void string_link(void (*rtSym)(const char*, void*)) +{ + rtSym("$String$string%repeat", bbString); + rtSym("$Left$string%count", bbLeft); + rtSym("$Right$string%count", bbRight); + rtSym("$Replace$string$from$to", bbReplace); + rtSym("%Instr$string$find%from=1", bbInstr); + rtSym("$Mid$string%start%count=-1", bbMid); + rtSym("$Upper$string", bbUpper); + rtSym("$Lower$string", bbLower); + rtSym("$Trim$string", bbTrim); + rtSym("$LSet$string%size", bbLSet); + rtSym("$RSet$string%size", bbRSet); + rtSym("$Chr%ascii", bbChr); + rtSym("%Asc$string", bbAsc); + rtSym("%Len$string", bbLen); + rtSym("$Hex%value", bbHex); + rtSym("$Bin%value", bbBin); + rtSym("$CurrentDate", bbCurrentDate); + rtSym("$CurrentTime", bbCurrentTime); } diff --git a/Runtime/lib/bbstring.hpp b/Runtime/lib/bbstring.hpp index 0133c25..bb9a01a 100644 --- a/Runtime/lib/bbstring.hpp +++ b/Runtime/lib/bbstring.hpp @@ -4,23 +4,23 @@ #include "basic.h" -BBStr * bbString( BBStr *s,int n ); -BBStr * bbLeft( BBStr *s,int n ); -BBStr * bbRight( BBStr *s,int n ); -BBStr * bbReplace( BBStr *s,BBStr *from,BBStr *to ); -int bbInstr( BBStr *s,BBStr *t,int from ); -BBStr * bbMid( BBStr *s,int o,int n ); -BBStr * bbUpper( BBStr *s ); -BBStr * bbLower( BBStr *s ); -BBStr * bbTrim( BBStr *s ); -BBStr * bbLSet( BBStr *s,int n ); -BBStr * bbRSet( BBStr *s,int n ); -BBStr * bbChr( int n ); -int bbAsc( BBStr *s ); -int bbLen( BBStr *s ); -BBStr * bbHex( int n ); -BBStr * bbBin( int n ); -BBStr * bbCurrentDate(); -BBStr * bbCurrentTime(); +BBStr* bbString(BBStr* s, int n); +BBStr* bbLeft(BBStr* s, int n); +BBStr* bbRight(BBStr* s, int n); +BBStr* bbReplace(BBStr* s, BBStr* from, BBStr* to); +int bbInstr(BBStr* s, BBStr* t, int from); +BBStr* bbMid(BBStr* s, int o, int n); +BBStr* bbUpper(BBStr* s); +BBStr* bbLower(BBStr* s); +BBStr* bbTrim(BBStr* s); +BBStr* bbLSet(BBStr* s, int n); +BBStr* bbRSet(BBStr* s, int n); +BBStr* bbChr(int n); +int bbAsc(BBStr* s); +int bbLen(BBStr* s); +BBStr* bbHex(int n); +BBStr* bbBin(int n); +BBStr* bbCurrentDate(); +BBStr* bbCurrentTime(); #endif \ No newline at end of file diff --git a/Runtime/lib/bbsys.cpp b/Runtime/lib/bbsys.cpp index 6567b41..519323b 100644 --- a/Runtime/lib/bbsys.cpp +++ b/Runtime/lib/bbsys.cpp @@ -1,6 +1,6 @@ -#include "std.hpp" #include "bbsys.hpp" +#include "std.hpp" -bool debug; -gxRuntime *gx_runtime; +bool debug; +gxRuntime* gx_runtime; diff --git a/Runtime/lib/bbsys.hpp b/Runtime/lib/bbsys.hpp index bedf253..454459c 100644 --- a/Runtime/lib/bbsys.hpp +++ b/Runtime/lib/bbsys.hpp @@ -3,18 +3,20 @@ #define BBSYS_H #include "basic.hpp" -#include "../gxruntime/gxruntime.hpp" +#include "gxruntime.hpp" -extern bool debug; -extern gxRuntime *gx_runtime; +extern bool debug; +extern gxRuntime* gx_runtime; -struct bbEx{ - const char *err; - bbEx( const char *e ):err(e){ - if( e ) gx_runtime->debugError( e ); +struct bbEx { + const char* err; + bbEx(const char* e) : err(e) + { + if (e) + gx_runtime->debugError(e); } }; -#define ThrowRuntimeException( _X_ ) throw bbEx( _X_ ); +#define ThrowRuntimeException(_X_) throw bbEx(_X_); #endif \ No newline at end of file diff --git a/Runtime/lib/multiplay.cpp b/Runtime/lib/multiplay.cpp index 0d78755..7f495f7 100644 --- a/Runtime/lib/multiplay.cpp +++ b/Runtime/lib/multiplay.cpp @@ -5,9 +5,9 @@ */ -#include "std.hpp" #include "multiplay.hpp" #include "multiplay_setup.hpp" +#include "std.hpp" //struct Player; // diff --git a/Runtime/lib/multiplay_setup.cpp b/Runtime/lib/multiplay_setup.cpp index 2079f2a..fb6985a 100644 --- a/Runtime/lib/multiplay_setup.cpp +++ b/Runtime/lib/multiplay_setup.cpp @@ -1,8 +1,8 @@ -#include "std.hpp" +#include "multiplay_setup.hpp" #include "bbsys.hpp" #include "resource.hpp" -#include "multiplay_setup.hpp" +#include "std.hpp" //IDirectPlay4 *dirPlay; // diff --git a/Runtime/lib/resource.hpp b/Runtime/lib/resource.hpp index 81c886c..841efd4 100644 --- a/Runtime/lib/resource.hpp +++ b/Runtime/lib/resource.hpp @@ -2,20 +2,20 @@ // Microsoft Developer Studio generated include file. // Used by runtime.rc // -#define IDD_MULTIPLAYER 101 -#define IDC_CANCEL 1011 -#define IDC_HOSTGAME 1012 -#define IDC_GAMENAME 1013 -#define IDC_GAMELIST 1014 -#define IDC_CONNECTIONS 1015 +#define IDD_MULTIPLAYER 101 +#define IDC_CANCEL 1011 +#define IDC_HOSTGAME 1012 +#define IDC_GAMENAME 1013 +#define IDC_GAMELIST 1014 +#define IDC_CONNECTIONS 1015 // Next default values for new objects -// +// #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_NEXT_RESOURCE_VALUE 105 -#define _APS_NEXT_COMMAND_VALUE 40001 -#define _APS_NEXT_CONTROL_VALUE 1021 -#define _APS_NEXT_SYMED_VALUE 101 +#define _APS_NEXT_RESOURCE_VALUE 105 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1021 +#define _APS_NEXT_SYMED_VALUE 101 #endif #endif diff --git a/Runtime/lib/std.hpp b/Runtime/lib/std.hpp index ae192be..1af16a9 100644 --- a/Runtime/lib/std.hpp +++ b/Runtime/lib/std.hpp @@ -3,21 +3,20 @@ #define STD_H //#ifndef _WINSOCKAPI_ -//#define _WINSOCKAPI_ /* Prevent inclusion of winsock.h in windows.h */ +//#define _WINSOCKAPI_ /* Prevent inclusion of winsock.h in windows.h */ //#endif #include //#include - #include "../stdutil/stdutil.hpp" -#include -#include +#include +#include #include +#include +#include #include #include -#include -#include #include diff --git a/Runtime/lib/userlib.cpp b/Runtime/lib/userlib.cpp index e5711e2..62890ee 100644 --- a/Runtime/lib/userlib.cpp +++ b/Runtime/lib/userlib.cpp @@ -1,34 +1,39 @@ -#include "std.hpp" #include "bbsys.hpp" +#include "std.hpp" #include static vector _mods; -static void procNotFound(){ - ThrowRuntimeException( "User lib function not found" ); +static void procNotFound() +{ + ThrowRuntimeException("User lib function not found"); } -void _bbLoadLibs( char *p ){ - while( *p ){ - HMODULE mod=LoadLibrary( p ); - if( !mod ){ +void _bbLoadLibs(char* p) +{ + while (*p) { + HMODULE mod = LoadLibrary(p); + if (!mod) { continue; } _mods.push_back(mod); - p+=strlen(p)+1; - while( *p ){ - void *proc=GetProcAddress( mod,p ); - p+=strlen(p)+1; - void *ptr=*(void**)p; - p+=4; - if( !proc ) proc=procNotFound; - *(void**)ptr=proc; + p += strlen(p) + 1; + while (*p) { + void* proc = GetProcAddress(mod, p); + p += strlen(p) + 1; + void* ptr = *(void**)p; + p += 4; + if (!proc) + proc = procNotFound; + *(void**)ptr = proc; } } } -void _bbUnloadLibs(){ - for( ;_mods.size();_mods.pop_back() ) FreeLibrary( _mods.back() ); +void _bbUnloadLibs() +{ + for (; _mods.size(); _mods.pop_back()) + FreeLibrary(_mods.back()); } diff --git a/Runtime/lib/userlib.hpp b/Runtime/lib/userlib.hpp index a5a7df3..e89b8c2 100644 --- a/Runtime/lib/userlib.hpp +++ b/Runtime/lib/userlib.hpp @@ -2,7 +2,7 @@ #ifndef USERLIB_H #define USERLIB_H -void _bbLoadLibs( char *table ); +void _bbLoadLibs(char* table); void _bbUnloadLibs(); #endif \ No newline at end of file diff --git a/Runtime/lib/userlibs.cpp b/Runtime/lib/userlibs.cpp index b3aa42e..8c27559 100644 --- a/Runtime/lib/userlibs.cpp +++ b/Runtime/lib/userlibs.cpp @@ -1,95 +1,101 @@ -#include "std.hpp" -#include "bbsys.hpp" #include "userlibs.hpp" +#include "bbsys.hpp" +#include "std.hpp" #include static vector _mods; -struct Str{ - char *p; - int size; +struct Str { + char* p; + int size; }; static Str _strs[256]; static int _nextStr; -static void libNotFound(){ - ThrowRuntimeException( "User lib not found" ); +static void libNotFound() +{ + ThrowRuntimeException("User lib not found"); } -static void procNotFound(){ - ThrowRuntimeException( "User lib function not found" ); +static void procNotFound() +{ + ThrowRuntimeException("User lib function not found"); } -void _bbLoadLibs( char *p ){ - +void _bbLoadLibs(char* p) +{ string home; - if( const char *t=getenv( "blitzpath" ) ) home=t; + if (const char* t = getenv("blitzpath")) + home = t; - while( *p ){ - HMODULE mod=LoadLibrary( p ); - if( !mod && home.size() ){ - mod=LoadLibrary( (home+"/userlibs/"+p).c_str() ); + while (*p) { + HMODULE mod = LoadLibrary(p); + if (!mod && home.size()) { + mod = LoadLibrary((home + "/userlibs/" + p).c_str()); } - p+=strlen(p)+1; - if( mod ){ - _mods.push_back( mod ); - while( *p ){ - void *proc=GetProcAddress( mod,p ); - p+=strlen(p)+1; - void *ptr=*(void**)p; - p+=4; - *(void**)ptr=proc ? proc : procNotFound; + p += strlen(p) + 1; + if (mod) { + _mods.push_back(mod); + while (*p) { + void* proc = GetProcAddress(mod, p); + p += strlen(p) + 1; + void* ptr = *(void**)p; + p += 4; + *(void**)ptr = proc ? proc : procNotFound; } - }else{ - while( *p ){ - p+=strlen(p)+1; - void *ptr=*(void**)p; - p+=4; - *(void**)ptr=libNotFound; + } else { + while (*p) { + p += strlen(p) + 1; + void* ptr = *(void**)p; + p += 4; + *(void**)ptr = libNotFound; } } ++p; } } -const char* _bbStrToCStr( BBStr *str ){ +const char* _bbStrToCStr(BBStr* str) +{ + Str& t = _strs[_nextStr++ & 255]; - Str &t=_strs[_nextStr++ & 255]; + int size = str->size(); - int size=str->size(); - - if( !t.p || t.sizedata(),size ); - t.p[size]=0; + memcpy(t.p, str->data(), size); + t.p[size] = 0; delete str; return t.p; } -BBStr* _bbCStrToStr( const char *str ){ - return new BBStr( str ); +BBStr* _bbCStrToStr(const char* str) +{ + return new BBStr(str); } -bool userlibs_create(){ +bool userlibs_create() +{ return true; } -void userlibs_destroy(){ - for( ;_mods.size();_mods.pop_back() ) FreeLibrary( _mods.back() ); +void userlibs_destroy() +{ + for (; _mods.size(); _mods.pop_back()) + FreeLibrary(_mods.back()); } -void userlibs_link( void(*rtSym)(const char*,void*) ){ - rtSym( "_bbLoadLibs",_bbLoadLibs ); - rtSym( "_bbStrToCStr",_bbStrToCStr ); - rtSym( "_bbCStrToStr",_bbCStrToStr ); +void userlibs_link(void (*rtSym)(const char*, void*)) +{ + rtSym("_bbLoadLibs", _bbLoadLibs); + rtSym("_bbStrToCStr", _bbStrToCStr); + rtSym("_bbCStrToStr", _bbCStrToStr); } - - diff --git a/Runtime/lib/userlibs.hpp b/Runtime/lib/userlibs.hpp index 7a23b89..61ce2f7 100644 --- a/Runtime/lib/userlibs.hpp +++ b/Runtime/lib/userlibs.hpp @@ -4,9 +4,9 @@ #include "basic.hpp" -void _bbLoadLibs( char *p ); +void _bbLoadLibs(char* p); -const char* _bbStrToCStr( BBStr *str ); -BBStr* _bbCStrToStr( const char *str ); +const char* _bbStrToCStr(BBStr* str); +BBStr* _bbCStrToStr(const char* str); #endif diff --git a/Runtime/resource.hpp b/Runtime/resource.hpp index e81f931..3d97c50 100644 --- a/Runtime/resource.hpp +++ b/Runtime/resource.hpp @@ -2,16 +2,16 @@ // Microsoft Developer Studio generated include file. // Used by runtime_dll.rc // -#define IDI_ICON1 107 -#define IDR_BBMODULE 1111 +#define IDI_ICON1 107 +#define IDR_BBMODULE 1111 // Next default values for new objects -// +// #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_NEXT_RESOURCE_VALUE 108 -#define _APS_NEXT_COMMAND_VALUE 40001 -#define _APS_NEXT_CONTROL_VALUE 1000 -#define _APS_NEXT_SYMED_VALUE 103 +#define _APS_NEXT_RESOURCE_VALUE 108 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1000 +#define _APS_NEXT_SYMED_VALUE 103 #endif #endif