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 "Framework/System/SimpleFramework.h"
00027 #include "Graphics/System/LampGraphics.h"
00028 #include "Graphics/System/GraphicsDevice.h"
00029 #include "Input/System/LampInput.h"
00030 #include "Sound/System/LampSound.h"
00031 #include "Core/Utility/FPSController.h"
00032
00033 namespace Lamp{
00034
00035
00036 SimpleFramework* SimpleFramework::instance_ = NULL;
00037
00038
00039
00040
00041
00042 SimpleFramework::SimpleFramework(const String& name) : name_(name){
00043
00044 windowHandle_ = NULL;
00045 minimumWindowSize_.set(320, 240);
00046 backGroundColor_.set(0x80, 0x80, 0x80, 0);
00047 fpsController_ = new FPSController();
00048 keyboard_ = NULL;
00049 mouse_ = NULL;
00050
00051 startFullscreen_ = false;
00052
00053 LampGraphics::initialize();
00054 }
00055
00056
00057 SimpleFramework::~SimpleFramework(){
00058 SafeDelete(fpsController_);
00059 }
00060
00061
00062 int SimpleFramework::execute(HINSTANCE instance){
00063 int result = 1;
00064
00065 if(frameworkInitialize(instance)){
00066
00067 if(initialize()){
00068
00069 result = frameworkExecute();
00070 }
00071
00072 finalize();
00073 }
00074
00075 frameworkFinalize();
00076 return result;
00077 }
00078
00079
00080
00081
00082 bool SimpleFramework::frameworkInitialize(HINSTANCE instanceHandle){
00083
00084 if(windowHandle_ == NULL){
00085 createWindowParameter_.windowName_ = name_;
00086 createWindowParameter_.instanceHandle_ = instanceHandle;
00087
00088 if(createWindowParameter_.windowProcedure_ == NULL){
00089 createWindowParameter_.windowProcedure_ =
00090 registrationWindowProcedure;
00091 }
00092 windowHandle_ = WindowCreator::create(createWindowParameter_);
00093 }
00094
00095 if(windowHandle_ == NULL){ return false; }
00096
00097
00098 if(!LampGraphics::initializeDevice(windowHandle_, startFullscreen_)){
00099 return false;
00100 }
00101
00102 GraphicsDevice::getInstance()->clear(backGroundColor_);
00103
00104
00105 if(!LampInput::initialize(
00106 instanceHandle, windowHandle_, LampInput::modeBuffering)){
00107 return false;
00108 }
00109 keyboard_ = LampInput::getKeyboard();
00110 mouse_ = LampInput::getMouse();
00111
00112
00113 if(!LampSound::initialize(windowHandle_)){ return false; }
00114
00115
00116 Assert(instance_ == NULL);
00117 instance_ = this;
00118 return true;
00119 }
00120
00121
00122 void SimpleFramework::frameworkFinalize(){
00123
00124 instance_ = NULL;
00125
00126 LampSound::finalize();
00127
00128 LampInput::finalize();
00129
00130 LampGraphics::finalize();
00131
00132 WindowCreator::destroy(windowHandle_);
00133
00134 LampCore::finalize();
00135 }
00136
00137
00138 int SimpleFramework::frameworkExecute(){
00139
00140
00141 MSG message;
00142 bool gotMessage;
00143 message.message = WM_NULL;
00144 ::PeekMessage(&message, NULL, 0, 0, PM_NOREMOVE);
00145
00146 if(LampInput::getInputMode() == LampInput::modeBuffering){
00147 LampInput::bufferClear();
00148 }
00149
00150 while(message.message != WM_QUIT){
00151
00152 gotMessage = (PeekMessage(&message, NULL, 0, 0, PM_REMOVE) != 0);
00153 if(gotMessage){
00154 TranslateMessage(&message);
00155 DispatchMessage(&message);
00156 }else{
00157
00158 mainLoop();
00159 }
00160 }
00161 return 0;
00162 }
00163
00164
00165
00166
00167 void SimpleFramework::mainLoop(){
00168
00169
00170
00171 int inputCount = 0;
00172 LampInput::waitForInput();
00173 while(LampInput::hasMoreInput()){
00174 LampInput::nextInput();
00175 frameworkRun();
00176 inputCount++;
00177 if(inputCount == 60){
00178 LampInput::bufferClear();
00179 break;
00180 }
00181 }
00182
00183 frameworkRenderSetup();
00184
00185 frameworkPresentation();
00186
00187 frameworkRender();
00188 }
00189
00190
00191 void SimpleFramework::frameworkRun(){
00192 run();
00193
00194 if(keyboard_->down(Keyboard::keyEscape)){ PostQuitMessage(0); }
00195 }
00196
00197
00198 void SimpleFramework::frameworkRenderSetup(){
00199 renderSetup();
00200 }
00201
00202
00203 bool SimpleFramework::frameworkPresentation(){
00204
00205 LampSound::presentation();
00206
00207
00208 fpsController_->sleep();
00209
00210
00211 return GraphicsDevice::getInstance()->presentation();
00212 }
00213
00214
00215 void SimpleFramework::frameworkRender(){
00216
00217 GraphicsDevice::getInstance()->clear(backGroundColor_);
00218 render();
00219 }
00220
00221
00222
00223
00224 LRESULT CALLBACK SimpleFramework::registrationWindowProcedure(
00225 HWND windowHandle, u_int message, WPARAM wParam, LPARAM lParam){
00226
00227 if(instance_ == NULL){
00228 return DefWindowProc(windowHandle, message, wParam, lParam);
00229 }
00230
00231
00232 LRESULT result;
00233 result = instance_->windowProcedure(windowHandle, message, wParam, lParam);
00234 if(result != 0){ return result; }
00235
00236
00237 result = LampCore::windowProcedure(windowHandle, message, wParam, lParam);
00238 if(result != 0){ return result; }
00239
00240
00241 return instance_->frameworkWindowProcedure(
00242 windowHandle, message, wParam, lParam);
00243 }
00244
00245
00246 LRESULT SimpleFramework::frameworkWindowProcedure(
00247 HWND windowHandle, u_int message, WPARAM wParam, LPARAM lParam){
00248 switch(message){
00249
00250 case WM_GETMINMAXINFO:
00251 ((MINMAXINFO*)lParam)->ptMinTrackSize.x = minimumWindowSize_.width;
00252 ((MINMAXINFO*)lParam)->ptMinTrackSize.y = minimumWindowSize_.height;
00253 break;
00254 }
00255
00256 return DefWindowProc(windowHandle, message, wParam, lParam);
00257 }
00258
00259 }
00260