librw/src/engine.cpp

272 lines
5.8 KiB
C++
Raw Normal View History

2016-06-16 14:08:09 +02:00
#include <cstdio>
#include <cstdlib>
#include <cassert>
2016-06-16 14:08:09 +02:00
#include "rwbase.h"
2017-08-09 10:57:32 +02:00
#include "rwerror.h"
2016-06-16 14:08:09 +02:00
#include "rwplg.h"
#include "rwpipeline.h"
#include "rwobjects.h"
#include "rwengine.h"
#include "ps2/rwps2.h"
#include "d3d/rwxbox.h"
#include "d3d/rwd3d.h"
#include "d3d/rwd3d8.h"
#include "d3d/rwd3d9.h"
#include "gl/rwgl3.h"
#include "gl/rwwdgl.h"
2016-06-16 14:08:09 +02:00
2017-08-09 10:57:32 +02:00
#define PLUGIN_ID 0
2017-08-25 14:06:53 +02:00
// on windows
//#ifdef DEBUG
//#include <crtdbg.h>
//#define free(p) _free_dbg(p, _NORMAL_BLOCK);
//#define malloc(sz) _malloc_dbg(sz, _NORMAL_BLOCK, __FILE__, __LINE__)
//#define realloc(p, sz) _realloc_dbg(p, sz, _NORMAL_BLOCK, __FILE__, __LINE__)
//#endif
2016-06-16 14:08:09 +02:00
namespace rw {
Engine *engine;
PluginList Engine::s_plglist;
2017-08-09 10:57:32 +02:00
Engine::State Engine::state = Dead;
2017-08-24 15:10:34 +02:00
MemoryFunctions Engine::memfuncs;
PluginList Driver::s_plglist[NUM_PLATFORMS];
2017-08-24 15:10:34 +02:00
2017-08-25 14:06:53 +02:00
void *malloc_h(size_t sz, uint32 hint) { if(sz == 0) return nil; return malloc(sz); }
2017-08-24 15:10:34 +02:00
void *realloc_h(void *p, size_t sz, uint32 hint) { return realloc(p, sz); }
2017-08-25 14:06:53 +02:00
// TODO: make the debug out configurable
void *mustmalloc_h(size_t sz, uint32 hint)
{
void *ret;
ret = rwMalloc(sz, hint);
if(ret || sz == 0)
2017-08-25 14:06:53 +02:00
return ret;
fprintf(stderr, "Error: out of memory\n");
exit(1);
return nil;
}
void *mustrealloc_h(void *p, size_t sz, uint32 hint)
{
void *ret;
ret = rwRealloc(p, sz, hint);
if(ret || sz == 0)
2017-08-25 14:06:53 +02:00
return ret;
fprintf(stderr, "Error: out of memory\n");
exit(1);
return nil;
}
2017-08-09 10:57:32 +02:00
// This function mainly registers engine plugins
bool32
Engine::init(void)
{
2017-08-09 10:57:32 +02:00
if(engine || Engine::state != Dead){
RWERROR((ERR_ENGINEINIT));
return 0;
}
// TODO: make this an argument
2017-08-25 14:06:53 +02:00
memfuncs.rwmalloc = malloc_h;
memfuncs.rwrealloc = realloc_h;
memfuncs.rwfree = free;
memfuncs.rwmustmalloc = mustmalloc_h;
memfuncs.rwmustrealloc = mustrealloc_h;
2017-08-24 15:10:34 +02:00
2017-08-09 10:57:32 +02:00
PluginList init = { sizeof(Driver), sizeof(Driver), nil, nil };
for(uint i = 0; i < NUM_PLATFORMS; i++)
2017-07-12 10:10:57 +02:00
Driver::s_plglist[i] = init;
Engine::s_plglist.size = sizeof(Engine);
Engine::s_plglist.defaultSize = sizeof(Engine);
Engine::s_plglist.first = nil;
Engine::s_plglist.last = nil;
// core plugin attach here
Frame::registerModule();
Texture::registerModule();
// driver plugin attach
ps2::registerPlatformPlugins();
xbox::registerPlatformPlugins();
d3d8::registerPlatformPlugins();
d3d9::registerPlatformPlugins();
wdgl::registerPlatformPlugins();
gl3::registerPlatformPlugins();
2017-08-09 10:57:32 +02:00
Engine::state = Initialized;
return 1;
}
// This is where RW allocates the engine and e.g. opens d3d
// TODO: this will probably take an argument with device specific data
2017-08-09 10:57:32 +02:00
bool32
Engine::open(void)
{
if(engine || Engine::state != Initialized){
RWERROR((ERR_ENGINEOPEN));
return 0;
}
2017-08-09 10:57:32 +02:00
// Allocate engine
engine = (Engine*)rwNew(Engine::s_plglist.size, MEMDUR_GLOBAL);
2016-07-21 08:59:06 +02:00
engine->currentCamera = nil;
engine->currentWorld = nil;
engine->imtexture = nil;
2016-07-21 08:59:06 +02:00
2017-08-09 10:57:32 +02:00
// Initialize device
// Device and possibly OS specific!
2017-08-12 10:25:25 +02:00
#ifdef RW_PS2
engine->device = ps2::renderdevice;
#elif RW_GL3
2017-08-09 10:57:32 +02:00
engine->device = gl3::renderdevice;
#elif RW_D3D9
engine->device = d3d::renderdevice;
#else
engine->device = null::renderdevice;
#endif
2016-07-06 11:44:59 +02:00
2017-08-09 10:57:32 +02:00
// TODO: open device; create d3d object/get video mode
2017-08-09 10:57:32 +02:00
// TODO: init driver functions
ObjPipeline *defpipe = new ObjPipeline(PLATFORM_NULL);
for(uint i = 0; i < NUM_PLATFORMS; i++){
2017-08-25 14:06:53 +02:00
rw::engine->driver[i] = (Driver*)rwNew(Driver::s_plglist[i].size,
MEMDUR_GLOBAL);
2017-08-09 10:57:32 +02:00
engine->driver[i]->defaultPipeline = defpipe;
2017-08-09 10:57:32 +02:00
engine->driver[i]->rasterCreate = null::rasterCreate;
engine->driver[i]->rasterLock = null::rasterLock;
engine->driver[i]->rasterUnlock = null::rasterUnlock;
engine->driver[i]->rasterNumLevels = null::rasterNumLevels;
engine->driver[i]->rasterFromImage = null::rasterFromImage;
engine->driver[i]->rasterToImage = null::rasterToImage;
}
2017-08-09 10:57:32 +02:00
Engine::state = Opened;
return 1;
}
2017-08-09 10:57:32 +02:00
// This is where RW creates the actual rendering device
// ans calls the engine plugin ctors
bool32
Engine::start(EngineStartParams *p)
{
if(engine == nil || Engine::state != Opened){
RWERROR((ERR_ENGINESTART));
return 0;
}
2017-08-09 10:57:32 +02:00
engine->device.system(DEVICESTART, (void*)p);
engine->device.system(DEVICEINIT, nil);
Engine::s_plglist.construct(engine);
2017-08-09 10:57:32 +02:00
for(uint i = 0; i < NUM_PLATFORMS; i++)
Driver::s_plglist[i].construct(rw::engine->driver[i]);
engine->device.system(DEVICEFINALIZE, nil);
2017-08-09 10:57:32 +02:00
Engine::state = Started;
return 1;
}
2017-08-10 00:42:33 +02:00
void
Engine::term(void)
{
2017-08-24 15:10:34 +02:00
// TODO
Engine::state = Dead;
2017-08-10 00:42:33 +02:00
}
void
Engine::close(void)
{
2017-08-24 15:10:34 +02:00
// TODO
for(uint i = 0; i < NUM_PLATFORMS; i++)
rwFree(rw::engine->driver[i]);
2017-08-24 15:10:34 +02:00
rwFree(engine);
Engine::state = Initialized;
2017-08-10 00:42:33 +02:00
}
2017-08-09 10:57:32 +02:00
void
Engine::stop(void)
{
engine->device.system(DEVICESTOP, nil);
for(uint i = 0; i < NUM_PLATFORMS; i++)
Driver::s_plglist[i].destruct(rw::engine->driver[i]);
Engine::s_plglist.destruct(engine);
Engine::state = Opened;
}
2016-06-16 14:08:09 +02:00
namespace null {
void beginUpdate(Camera*) { }
void endUpdate(Camera*) { }
2016-07-05 18:22:22 +02:00
void clearCamera(Camera*,RGBA*,uint32) { }
2017-08-09 10:57:32 +02:00
void showRaster(Raster*) { }
2016-07-05 11:36:43 +02:00
void setRenderState(int32, uint32) { }
uint32 getRenderState(int32) { return 0; }
void im2DRenderIndexedPrimitive(PrimitiveType, void*, int32, void*, int32) { }
2016-06-16 14:08:09 +02:00
void
rasterCreate(Raster*)
{
assert(0 && "rasterCreate not implemented");
}
uint8*
rasterLock(Raster*, int32)
{
assert(0 && "lockRaster not implemented");
return nil;
2016-06-16 14:08:09 +02:00
}
void
rasterUnlock(Raster*, int32)
{
assert(0 && "unlockRaster not implemented");
}
int32
2016-06-16 15:35:45 +02:00
rasterNumLevels(Raster*)
2016-06-16 14:08:09 +02:00
{
assert(0 && "rasterNumLevels not implemented");
return 0;
}
void
rasterFromImage(Raster*, Image*)
{
assert(0 && "rasterFromImage not implemented");
}
2016-07-15 11:55:52 +02:00
Image*
rasterToImage(Raster*)
{
assert(0 && "rasterToImage not implemented");
return nil;
}
2017-08-09 10:57:32 +02:00
int
2017-08-12 10:25:25 +02:00
deviceSystem(DeviceReq req, void *arg0)
2017-08-09 10:57:32 +02:00
{
return 1;
}
Device renderdevice = {
0.0f, 1.0f,
null::beginUpdate,
null::endUpdate,
null::clearCamera,
null::showRaster,
null::setRenderState,
null::getRenderState,
null::im2DRenderIndexedPrimitive,
2017-08-12 10:25:25 +02:00
null::deviceSystem
2017-08-09 10:57:32 +02:00
};
2016-06-16 14:08:09 +02:00
}
}