source: GTP/trunk/App/Games/Jungle_Rumble/src/MenueScene.cpp @ 1397

Revision 1397, 11.6 KB checked in by giegl, 18 years ago (diff)

GTPD - Jungle Rumble - sceneFader.fx, .obj "1024x768"- fix (Michael) working

Line 
1#include "dxstdafx.h"
2#include ".\menuescene.h"
3#include ".\GameManager.h"
4
5MenueScene::MenueScene(void) : Scene()
6{
7        this->lightSetted = false;
8        this->usePhysXDebugger = false;
9        this->bgSoundChannel = -1;
10        this->bgSoundVolume = 1.0f;
11        this->menuestatus = MENU_IDLE;
12        this->soundStopped = false;
13}
14
15MenueScene::~MenueScene(void)
16{
17        this->sprite->Release();
18        this->clearScene();
19}
20
21void MenueScene::clearScene()
22{
23        Scene::clearScene();
24}
25
26void MenueScene::initScene(GameManager &_manager) {
27       
28        Scene::initScene(_manager);
29       
30        this->activeChallengeNumber = -1;
31        this->selectedChallengeNumber = -1;
32       
33        //create sprite:
34        D3DXCreateSprite(DXUTGetD3DDevice(),&sprite);
35
36        //load background texture:
37        Textures.push_back(this->manager->resManager.loadTexture("./media/textures/MenuBackgroundGTP.JPG", true));
38       
39        for (UINT i = 0; i < this->challengeScreenList.size(); i++) {
40                LoadingTextures.push_back(this->manager->resManager.loadTexture(this->challengeScreenList[i], true));
41        }
42
43        this->activateStandardBackground();
44
45        SoundNode* bgSound = (SoundNode*) this->createNode(Scene::NODE_SOUND);
46        //bgSound->loadFile("./media/music
47}
48
49void MenueScene::activateStandardBackground() {
50        //draw crosshair:
51        Textures[0]->GetLevelDesc(0, &desc);    //get texture size             
52        //calculate transfomation:
53        D3DXVECTOR2 scaling((float)((float)this->manager->screenWidth/(float)desc.Width),(float)((float)this->manager->screenHeight/(float)desc.Height));
54        //set matrix:
55        D3DXMatrixTransformation2D(&spriteMatrix,NULL,0.0,&scaling,NULL,0,NULL);
56        this->sprite->SetTransform(&spriteMatrix);
57
58        this->backgroundIndex = -1;
59}
60
61void MenueScene::activateLoadingBackground(int index) {
62        //draw crosshair:
63        this->LoadingTextures[index]->GetLevelDesc(0, &desc);   //get texture size             
64        //calculate transfomation:
65        D3DXVECTOR2 scaling((float)((float)this->manager->screenWidth/(float)desc.Width),(float)((float)this->manager->screenHeight/(float)desc.Height));
66        //set matrix:
67        D3DXMatrixTransformation2D(&spriteMatrix,NULL,0.0,&scaling,NULL,0,NULL);
68        this->sprite->SetTransform(&spriteMatrix);
69
70        this->backgroundIndex = index;
71}
72
73void MenueScene::addChallenge(std::string challengeName)
74{
75        this->challengeList.push_back(challengeName);
76}
77
78void MenueScene::renderScene(float fElapsedTime)
79{
80        if(this->isVisible()) {
81                if(this->device==0) {
82                        this->device = DXUTGetD3DDevice();
83                        this->physxRenderer.setDevice(*this->device);
84                }
85                this->device->SetRenderTarget(0, this->finalImageSurface);
86
87                if(this->firstFrame) {
88                        this->setupGUI();
89                        //this->playBackgroundSound();
90                }
91
92                this->takeTime();
93                //ClearScreen
94                //this->clearRenderTargetAndZBuffer();
95                //Clear SharedResources
96                this->clearSharedResources();
97                //Fetch Physic Results
98                this->fetchPhysicResults();
99                //Fading Stuff
100                this->doFade();
101                //update game first
102                this->updateMenueFirst();
103                //Trigger updates
104                this->updateTrigger();
105                //Update Nodes
106                this->updateNodes();
107                //Calculate Transformations
108                this->calculateTransformations();
109               
110                //startRenderPasses
111                this->startRenderPasses();
112
113                this->device->SetRenderTarget(0, this->finalImageSurface);
114
115                //Render Background:
116                this->renderBackground();
117                //Render GUI:
118                this->renderGUI(fElapsedTime);
119
120                //Cleanup Memory
121                this->cleanUpScene();
122                //Start Physic
123                this->startPhysic();
124
125                this->firstFrame = false;
126        }
127}
128
129
130
131void MenueScene::updateMenueFirst() {
132        if(this->keyDown[VK_ESCAPE]) {
133            this->keyDown[VK_ESCAPE] = false;
134                manager->switchToGameScene();           
135        }
136        if (this->menuestatus == MENU_LOAD) {
137                this->menuestatus = MENU_IDLE;
138                this->loadGameScene();
139        }
140        if (this->menuestatus == MENU_PREPARE) {
141                this->menuestatus = MENU_LOAD;
142                this->GUI->SetVisible(false);
143        }
144}
145
146void MenueScene::prepareLoading(int index) {
147        if (this->selectedChallengeNumber >= 0) {
148                //change the number of the current challenge to index
149                this->activeChallengeNumber = index;
150
151                this->manager->printToConsole("preparing to load gamescene");
152               
153                this->activeGameScene = &this->manager->gs;//&GameScene();
154               
155                this->activeGameScene->setWidth(1000);
156                this->activeGameScene->setHeight(1000);
157               
158                this->activateLoadingBackground(index);
159
160                this->menuestatus = MENU_PREPARE;
161        }
162}
163
164void MenueScene::loadGameScene() {
165        //clear scene for re-use:
166        if(!this->activeGameScene->alreadyUsed) {
167                this->activeGameScene->alreadyUsed = true;
168        } else {
169                this->activeGameScene->clearScene();
170        }
171       
172        //clean up resources
173        this->manager->resManager.cleanUpResources(false);
174
175        //init scene:
176        this->activeGameScene->initScene(*this->manager);
177        //load the challenge:
178        this->activeGameScene->loadGame(this->challengeList[this->activeChallengeNumber]);
179        //add some light
180        if(!this->lightSetted) {
181                this->lightSetted = true;
182                this->activeGameScene->setLight((*this->activeGameScene).getSunDirection());
183        }
184       
185        manager->switchToGameScene();
186}
187
188void MenueScene::initGUI() {
189       
190        this->setBackgroundSound("./media/music/soundpark/menu.mp3");
191        this->playBackgroundSound();
192       
193        this->GUI->SetVisible(true);
194}
195
196void MenueScene::setupGUI() {
197        //diese funktion ist leider naotwendig da ich die größe noch nicht in initGUI festlegen kann da der manager da noch NULL ist...
198               
199        #if(0)
200                int fontsize = 0;
201                if(manager->screenWidth <= 640) {
202                        fontsize = 14;
203                } else if (manager->screenWidth <= 800) {
204                        fontsize = 18;
205                } else if (manager->screenWidth <= 1024) {
206                        fontsize = 24;
207                } else if (manager->screenWidth <= 1200) {
208                        fontsize = 30;
209                } else if (manager->screenWidth <= 1600) {
210                        fontsize = 36;
211                } else {
212                        fontsize = 42;
213                }
214        #elif(1) // MG
215                int fontsize = 20.0 * (manager->screenWidth / 1024.0);
216        #endif
217       
218        //this->GUI->SetFont( 1, L"Arial", fontsize, FW_BOLD );
219        this->GUI->SetFont( 1, L"Verdana", fontsize, FW_BOLD );
220        //this->GUI->SetFont( 1, L"Tahoma", fontsize, FW_BOLD );
221
222        // Buttons
223        this->GUI->AddButton( IDC_LOADGAME, L"Load Challenge...", 0, 0, 120, 35, L'8' );
224        this->GUI->AddButton( IDC_EXIT, L"Exit Game...", 30, 430, 120, 35, L'X' );
225
226        // List box
227    this->GUI->AddListBox( IDC_CHALLENGELIST, 30, 200, 200, 150, 2 );
228        for( UINT i = 0; i < this->challengeNameList.size(); ++i )
229    {
230                std::wstring temp2(this->challengeNameList.at(i).begin(), this->challengeNameList.at(i).end());
231                this->GUI->GetListBox( IDC_CHALLENGELIST )->AddItem( temp2.c_str(), (LPVOID)(size_t)i );
232    }
233
234        float factorX = ((float)this->manager->screenWidth)/((float)100);
235        float factorY = ((float)this->manager->screenHeight)/((float)100);
236       
237        this->GUI->GetListBox(IDC_CHALLENGELIST)->SetLocation((int)(81*factorX), (int)(5*factorY));
238        this->GUI->GetListBox(IDC_CHALLENGELIST)->SetSize((int)(18*factorX), (int)(52*factorY));
239        this->GUI->GetListBox(IDC_CHALLENGELIST)->GetElement(0)->iFont = 1;
240        this->GUI->GetListBox(IDC_CHALLENGELIST)->GetElement(1)->iFont = 1;
241
242        this->GUI->GetButton(IDC_LOADGAME)->SetLocation((int)(81*factorX), (int)(65*factorY));
243        this->GUI->GetButton(IDC_LOADGAME)->SetSize((int)(18*factorX), (int)(10*factorY));
244        this->GUI->GetButton(IDC_LOADGAME)->GetElement(0)->iFont = 1;
245        this->GUI->GetButton(IDC_LOADGAME)->GetElement(1)->iFont = 1;
246       
247        this->GUI->GetButton(IDC_EXIT)->SetLocation((int)(81*factorX), (int)(81*factorY));
248        this->GUI->GetButton(IDC_EXIT)->SetSize((int)(18*factorX), (int)(10*factorY));
249        this->GUI->GetButton(IDC_EXIT)->GetElement(0)->iFont = 1;
250        this->GUI->GetButton(IDC_EXIT)->GetElement(1)->iFont = 1;
251}
252
253void MenueScene::renderGUI(float fElapsedTime) {
254        this->device->BeginScene();
255        this->GUI->OnRender(fElapsedTime);
256        this->device->EndScene();
257}
258
259void MenueScene::renderBackground() {
260        this->device->BeginScene();
261        this->sprite->Begin(D3DXSPRITE_ALPHABLEND);
262        if(this->backgroundIndex == -1) {
263                this->sprite->Draw(this->Textures[0], NULL, NULL, NULL, 0xFFFFFFFF);
264        } else {
265                this->sprite->Draw(this->LoadingTextures[this->backgroundIndex], NULL, NULL, NULL, 0xFFFFFFFF);
266        }
267        this->sprite->End();
268        this->device->EndScene();
269}
270
271void MenueScene::setDialogResourceManager(CDXUTDialogResourceManager * DialogResourceManager) {
272        this->DialogResourceManager = DialogResourceManager;
273}
274
275void MenueScene::setGUI(CDXUTDialog * GUI) {
276        this->GUI = GUI;
277}
278
279void MenueScene::OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext )
280{
281    switch( nControlID )
282    {
283        case IDC_LOADGAME:
284                        this->prepareLoading(this->selectedChallengeNumber);
285                        break;
286                case IDC_CHALLENGELIST:
287                        switch( nEvent )
288            {
289                case EVENT_LISTBOX_SELECTION:
290                                        this->selectedChallengeNumber = ((CDXUTListBox *)pControl)->GetSelectedIndex();
291                                        break;
292                                default:
293                                        break;
294                        }
295                        break;
296                case IDC_EXIT:
297                        SendMessage( DXUTGetHWND(), WM_CLOSE, 0, 0 );
298                        break;
299    }
300}
301
302std::vector<std::string>* MenueScene::getChallengeList() {
303        return &this->challengeList;
304}
305
306std::vector<std::string>* MenueScene::getChallengeNameList() {
307        return &this->challengeNameList;
308}
309
310std::vector<std::string>* MenueScene::getChallengeScreenList() {
311        return &this->challengeScreenList;
312}
313
314LPD3DXSPRITE* MenueScene::getSprite() {
315        return &this->sprite;
316}
317
318void MenueScene::OnLostDevice( void* pUserContext )
319{
320        this->sprite->OnLostDevice();
321        Scene::OnLostDevice(pUserContext);
322}
323
324void MenueScene::OnDestroyDevice( void* pUserContext )
325{
326        SAFE_RELEASE(this->sprite);
327        this->sprite = NULL;
328        this->Textures.clear();
329        Scene::OnDestroyDevice(pUserContext);
330}
331
332HRESULT MenueScene::OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
333{
334        Scene::OnResetDevice(pd3dDevice, pBackBufferSurfaceDesc, pUserContext);
335        D3DXCreateSprite(this->device,&sprite);
336       
337        //load background texture:
338        Textures.push_back(this->manager->resManager.loadTexture("./media/textures/MenuBackground.JPG"));
339       
340        //draw crosshair:
341        Textures[0]->GetLevelDesc(0, &desc);    //get texture size             
342        //calculate transfomation:
343        D3DXVECTOR2 scaling((float)this->manager->screenWidth/desc.Width,(float)this->manager->screenHeight/desc.Height);
344        //set matrix:
345        D3DXMatrixTransformation2D(&spriteMatrix,NULL,0.0,&scaling,NULL,0,NULL);
346        this->sprite->SetTransform(&spriteMatrix);
347        return S_OK;
348}
349
350CDXUTDialog* MenueScene::getGUI() {
351        return this->GUI;
352}
353
354//void MenueScene::setBackgroundSound(std::string filename) {
355//      this->bgMusicStream = FSOUND_Stream_Open(filename.c_str(), FSOUND_LOOP_NORMAL, 0, 0);
356//}
357//
358//void MenueScene::playBackgroundSound() {
359//      if(this->bgMusicStream!=NULL) {
360//              this->bgSoundChannel = FSOUND_Stream_Play(FSOUND_FREE, this->bgMusicStream);
361//              FSOUND_SetVolume(this->bgSoundChannel, (int)(255*this->bgSoundVolume));
362//      }
363//}
364//
365void MenueScene::updateSoundNodeVolume()
366{
367        Scene::updateSoundNodeVolume();
368        if(this->bgSoundChannel!=-1 && !this->muteMusic) {
369                if(this->sceneAlpha!=0) {
370                        if(this->soundStopped) {
371                                this->soundStopped = false;
372                                this->bgSoundChannel = FSOUND_Stream_Play(FSOUND_FREE, this->bgMusicStream);
373                        }
374                        FSOUND_SetVolume(this->bgSoundChannel, (int)(255*this->bgSoundVolume*this->sceneAlpha));
375                } else {
376                        FSOUND_Stream_Stop(this->bgMusicStream);
377                        this->soundStopped = true;
378                }
379        }
380}
381
382/*void MenueScene::setBackgroundSoundMute(bool _muteMusic)
383{       
384        this->muteMusic = _muteMusic;
385        if(!this->muteMusic) {
386                if(this->bgSoundChannel!=-1) {
387                        if(this->soundStopped) {
388                                this->soundStopped = false;
389                                this->bgSoundChannel = FSOUND_Stream_Play(FSOUND_FREE, this->bgMusicStream);
390                        }
391                        FSOUND_SetVolume(this->bgSoundChannel, (int)(255*this->bgSoundVolume*this->sceneAlpha));
392                }
393        } else {
394                if(this->bgSoundChannel!=-1) {
395                        FSOUND_SetVolume(this->bgSoundChannel, 0);
396                }
397        }
398}*/
Note: See TracBrowser for help on using the repository browser.