00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "LampBasic.h"
00026 #include "Graphics/System/LampGraphics.h"
00027 #include "Graphics/Enumeration/GraphicsDeviceEnumeration.h"
00028 #include "Graphics/DeviceSelector/GraphicsDeviceSelector.h"
00029 #include "Graphics/System/GraphicsDeviceSettings.h"
00030 #include "Graphics/DeviceSelector/DesktopGraphicsDeviceSelector.h"
00031 #include "Graphics/System/GraphicsDevice.h"
00032 #include "Graphics/System/GraphicsDeviceCapacity.h"
00033 #include "Graphics/Renderer/RenderingDevice.h"
00034 #include "Graphics/Shader/ShaderManager.h"
00035 #include "Graphics/System/GraphicsDeviceObjectHolder.h"
00036
00037 namespace Lamp{
00038
00039
00040 HWND LampGraphics::windowHandle_ = NULL;
00041
00042 GraphicsDeviceEnumeration* LampGraphics::enumeration_ = NULL;
00043
00044 GraphicsDeviceSelector* LampGraphics::selector_ = NULL;
00045
00046 GraphicsDeviceSettings* LampGraphics::settings_ = NULL;
00047
00048 GraphicsDevice* LampGraphics::device_ = NULL;
00049
00050 Direct3DDevice* LampGraphics::direct3DDevice_ = NULL;
00051
00052 GraphicsDeviceCapacity* LampGraphics::deviceCapacity_ = NULL;
00053
00054 RenderingDevice* LampGraphics::renderingDevice_ = NULL;
00055
00056 ShaderManager* LampGraphics::shaderManager_ = NULL;
00057
00058 Direct3D* LampGraphics::direct3D_ = NULL;
00059
00060 ArrayList<GraphicsDeviceObjectHolder*> LampGraphics::deviceObjectHolders_;
00061
00062
00063 ArrayList<Scene*> LampGraphics::sceneArray_(scenesCapacity_);
00064
00065 HashMap<String, Scene*> LampGraphics::sceneDatabase_(scenesCapacity_, 1.f);
00066
00067
00068 bool LampGraphics::isInitialized_ = false;
00069
00070 bool LampGraphics::deviceInitialized_ = false;
00071
00072
00073
00074 void LampGraphics::initialize(){
00075 if(isInitialized_){ return; }
00076 LampCore::initialize();
00077 enumeration_ = new GraphicsDeviceEnumeration();
00078 selector_ = new DesktopGraphicsDeviceSelector();
00079 settings_ = new GraphicsDeviceSettings();
00080 device_ = new GraphicsDevice();
00081 deviceCapacity_ = new GraphicsDeviceCapacity();
00082 renderingDevice_ = new RenderingDevice(deviceCapacity_);
00083 shaderManager_ = new ShaderManager();
00084
00085 isInitialized_ = true;
00086 return;
00087 }
00088
00089
00090 bool LampGraphics::initializeDevice(
00091 HWND windowHandle, bool startFullscreen){
00092 Assert(isInitialized_);
00093 if(deviceInitialized_){ return true; }
00094 windowHandle_ = windowHandle;
00095
00096 direct3D_ = Direct3DCreate(D3D_SDK_VERSION);
00097 if(direct3D_ == NULL){
00098 ErrorOut("LampGraphics::initialize() Direct3DCreate()");
00099 return false;
00100 }
00101
00102 if(!enumeration_->enumerate()){
00103 ErrorOut("LampGraphics::initialize() Enumerate()");
00104 return false;
00105 }
00106
00107 if(!selector_->chooseDeviceSettings(windowHandle_, startFullscreen)){
00108 ErrorOut("LampGraphics::initialize() chooseDeviceSettings()");
00109 return false;
00110 }
00111
00112 device_->initializeWindowHandle(windowHandle_);
00113 if(!device_->initialize()){
00114 ErrorOut("LampGraphics::initialize() initializeDevice()");
00115 return false;
00116 }
00117
00118 deviceInitialized_ = true;
00119 return true;
00120 }
00121
00122
00123 void LampGraphics::finalize(){
00124
00125 device_->cleanup();
00126
00127 SafeRelease(direct3D_);
00128 windowHandle_ = NULL;
00129
00130 deviceInitialized_ = false;
00131
00132
00133 SafeDelete(shaderManager_);
00134 SafeDelete(renderingDevice_);
00135 SafeDelete(deviceCapacity_);
00136 SafeDelete(device_);
00137 SafeDelete(selector_);
00138 SafeDelete(settings_);
00139 SafeDelete(enumeration_);
00140
00141 isInitialized_ = false;
00142
00143
00144 Assert(getSceneCount() == 0);
00145 if(sceneArray_.getCapacity() != scenesCapacity_){
00146 sceneArray_.setCapacity(scenesCapacity_);
00147 sceneDatabase_.setCapacity(scenesCapacity_);
00148 }
00149 }
00150
00151
00152 LRESULT LampGraphics::windowProcedure(
00153 HWND windowHandle, u_int message, WPARAM wParam, LPARAM lParam){
00154
00155 return device_->windowProcedure(windowHandle, message, wParam, lParam);
00156 }
00157
00158
00159 void LampGraphics::setDeviceSelector(GraphicsDeviceSelector* selector){
00160 SafeDelete(selector_);
00161 selector_ = selector;
00162 }
00163
00164
00165
00166
00167 void LampGraphics::deviceReset(){
00168 direct3DDevice_ = device_->getDirect3DDevice();
00169
00170 deviceCapacity_->deviceReset(direct3DDevice_);
00171
00172 renderingDevice_->restoreDefaultStateBlock(direct3DDevice_);
00173 }
00174
00175
00176 bool LampGraphics::initializeDeviceObjects(){
00177 bool result = true;
00178 int objectHolderCount = getDeviceObjectHolderCount();
00179 for(int i = 0; i < objectHolderCount; i++){
00180 GraphicsDeviceObjectHolder* holder = getDeviceObjectHolder(i);
00181 if(!holder->initializeGraphicsDeviceObjects()){ result = false; }
00182 }
00183 return result;
00184 }
00185
00186
00187 void LampGraphics::deleteDeviceObjects(){
00188 int objectHolderCount = getDeviceObjectHolderCount();
00189 for(int i = 0; i < objectHolderCount; i++){
00190 getDeviceObjectHolder(i)->deleteGraphicsDeviceObjects();
00191 }
00192 }
00193
00194
00195 bool LampGraphics::restoreDeviceObjects(){
00196 bool result = true;
00197
00198 int objectHolderCount = getDeviceObjectHolderCount();
00199 for(int i = 0; i < objectHolderCount; i++){
00200 GraphicsDeviceObjectHolder* holder = getDeviceObjectHolder(i);
00201 if(!holder->restoreGraphicsDeviceObjects()){ result = false; }
00202 }
00203 return result;
00204 }
00205
00206
00207 void LampGraphics::invalidateDeviceObjects(){
00208
00209 int objectHolderCount = getDeviceObjectHolderCount();
00210 for(int i = 0; i < objectHolderCount; i++){
00211 getDeviceObjectHolder(i)->invalidateGraphicsDeviceObjects();
00212 }
00213
00214 renderingDevice_->invalidateDefaultStateBlock();
00215 }
00216
00217
00218
00219
00220 Scene* LampGraphics::createScene(const String& name){
00221
00222 if(name.getSize() == 0){
00223 ErrorOut("LampGraphics::createScene() name.getSize() == 0");
00224 return NULL;
00225 }
00226
00227 if(search(name) != NULL){
00228 ErrorOut("LampGraphics::createScene() repetition name %s",
00229 name.getBytes());
00230 return NULL;
00231 }
00232 Scene* scene = new Scene(name);
00233 sceneDatabase_.put(name, scene);
00234 sceneArray_.add(scene);
00235 addDeviceObjectHolder(scene);
00236 return scene;
00237 }
00238
00239
00240 void LampGraphics::destroyScene(Scene* scene){
00241 removeDeviceObjectHolder(scene);
00242 sceneArray_.removeByValue(scene);
00243 sceneDatabase_.remove(scene->getName());
00244 delete scene;
00245 }
00246
00247 }
00248