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

Revision 1378, 10.9 KB checked in by giegl, 18 years ago (diff)

GTPD - Jungle Rumble - integrate into GTP SVN structure

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)this->manager->screenWidth/desc.Width,(float)this->manager->screenHeight/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)this->manager->screenWidth/desc.Width,(float)this->manager->screenHeight/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        //FONTs
191        this->GUI->SetFont( 1, L"Comic Sans MS", 18, FW_BOLD );
192
193        // Buttons
194        this->GUI->AddButton( IDC_LOADGAME, L"Load Challenge...", 0, 0, 120, 35, L'8' );
195        this->GUI->AddButton( IDC_EXIT, L"Exit Game...", 30, 430, 120, 35, L'X' );
196
197        // List box
198    this->GUI->AddListBox( IDC_CHALLENGELIST, 30, 200, 200, 150, 2 );
199        for( UINT i = 0; i < this->challengeNameList.size(); ++i )
200    {
201                std::wstring temp2(this->challengeNameList.at(i).begin(), this->challengeNameList.at(i).end());
202                this->GUI->GetListBox( IDC_CHALLENGELIST )->AddItem( temp2.c_str(), (LPVOID)(size_t)i );
203    }
204
205        this->setBackgroundSound("./media/music/soundpark/menu.mp3");
206        this->playBackgroundSound();
207       
208        this->GUI->SetVisible(true);
209}
210
211void MenueScene::setupGUI() {
212        //diese funktion ist leider naotwendig da ich die größe noch nicht in initGUI festlegen kann da der manager da noch NULL ist...
213        float factorX = (float)this->manager->screenWidth/100;
214        float factorY = (float)this->manager->screenHeight/100;
215       
216        this->GUI->GetListBox(IDC_CHALLENGELIST)->SetLocation((int)(81*factorX), (int)(5*factorY));
217        this->GUI->GetListBox(IDC_CHALLENGELIST)->SetSize((int)(18*factorX), (int)(52*factorY));
218        this->GUI->GetListBox(IDC_CHALLENGELIST)->GetElement(0)->iFont = 1;
219        this->GUI->GetListBox(IDC_CHALLENGELIST)->GetElement(1)->iFont = 1;
220
221        this->GUI->GetButton(IDC_LOADGAME)->SetLocation((int)(81*factorX), (int)(65*factorY));
222        this->GUI->GetButton(IDC_LOADGAME)->SetSize((int)(18*factorX), (int)(10*factorY));
223        this->GUI->GetButton(IDC_LOADGAME)->GetElement(0)->iFont = 1;
224        this->GUI->GetButton(IDC_LOADGAME)->GetElement(1)->iFont = 1;
225       
226        this->GUI->GetButton(IDC_EXIT)->SetLocation((int)(81*factorX), (int)(81*factorY));
227        this->GUI->GetButton(IDC_EXIT)->SetSize((int)(18*factorX), (int)(10*factorY));
228        this->GUI->GetButton(IDC_EXIT)->GetElement(0)->iFont = 1;
229        this->GUI->GetButton(IDC_EXIT)->GetElement(1)->iFont = 1;
230}
231
232void MenueScene::renderGUI(float fElapsedTime) {
233        this->device->BeginScene();
234        this->GUI->OnRender(fElapsedTime);
235        this->device->EndScene();
236}
237
238void MenueScene::renderBackground() {
239        this->device->BeginScene();
240        this->sprite->Begin(D3DXSPRITE_ALPHABLEND);
241        if(this->backgroundIndex == -1) {
242                this->sprite->Draw(this->Textures[0], NULL, NULL, NULL, 0xFFFFFFFF);
243        } else {
244                this->sprite->Draw(this->LoadingTextures[this->backgroundIndex], NULL, NULL, NULL, 0xFFFFFFFF);
245        }
246        this->sprite->End();
247        this->device->EndScene();
248}
249
250void MenueScene::setDialogResourceManager(CDXUTDialogResourceManager * DialogResourceManager) {
251        this->DialogResourceManager = DialogResourceManager;
252}
253
254void MenueScene::setGUI(CDXUTDialog * GUI) {
255        this->GUI = GUI;
256}
257
258void MenueScene::OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext )
259{
260    switch( nControlID )
261    {
262        case IDC_LOADGAME:
263                        this->prepareLoading(this->selectedChallengeNumber);
264                        break;
265                case IDC_CHALLENGELIST:
266                        switch( nEvent )
267            {
268                case EVENT_LISTBOX_SELECTION:
269                                        this->selectedChallengeNumber = ((CDXUTListBox *)pControl)->GetSelectedIndex();
270                                        break;
271                                default:
272                                        break;
273                        }
274                        break;
275                case IDC_EXIT:
276                        SendMessage( DXUTGetHWND(), WM_CLOSE, 0, 0 );
277                        break;
278    }
279}
280
281std::vector<std::string>* MenueScene::getChallengeList() {
282        return &this->challengeList;
283}
284
285std::vector<std::string>* MenueScene::getChallengeNameList() {
286        return &this->challengeNameList;
287}
288
289std::vector<std::string>* MenueScene::getChallengeScreenList() {
290        return &this->challengeScreenList;
291}
292
293LPD3DXSPRITE* MenueScene::getSprite() {
294        return &this->sprite;
295}
296
297void MenueScene::OnLostDevice( void* pUserContext )
298{
299        this->sprite->OnLostDevice();
300        Scene::OnLostDevice(pUserContext);
301}
302
303void MenueScene::OnDestroyDevice( void* pUserContext )
304{
305        SAFE_RELEASE(this->sprite);
306        this->sprite = NULL;
307        this->Textures.clear();
308        Scene::OnDestroyDevice(pUserContext);
309}
310
311HRESULT MenueScene::OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
312{
313        Scene::OnResetDevice(pd3dDevice, pBackBufferSurfaceDesc, pUserContext);
314        D3DXCreateSprite(this->device,&sprite);
315       
316        //load background texture:
317        Textures.push_back(this->manager->resManager.loadTexture("./media/textures/MenuBackground.JPG"));
318       
319        //draw crosshair:
320        Textures[0]->GetLevelDesc(0, &desc);    //get texture size             
321        //calculate transfomation:
322        D3DXVECTOR2 scaling((float)this->manager->screenWidth/desc.Width,(float)this->manager->screenHeight/desc.Height);
323        //set matrix:
324        D3DXMatrixTransformation2D(&spriteMatrix,NULL,0.0,&scaling,NULL,0,NULL);
325        this->sprite->SetTransform(&spriteMatrix);
326        return S_OK;
327}
328
329CDXUTDialog* MenueScene::getGUI() {
330        return this->GUI;
331}
332
333//void MenueScene::setBackgroundSound(std::string filename) {
334//      this->bgMusicStream = FSOUND_Stream_Open(filename.c_str(), FSOUND_LOOP_NORMAL, 0, 0);
335//}
336//
337//void MenueScene::playBackgroundSound() {
338//      if(this->bgMusicStream!=NULL) {
339//              this->bgSoundChannel = FSOUND_Stream_Play(FSOUND_FREE, this->bgMusicStream);
340//              FSOUND_SetVolume(this->bgSoundChannel, (int)(255*this->bgSoundVolume));
341//      }
342//}
343//
344void MenueScene::updateSoundNodeVolume()
345{
346        Scene::updateSoundNodeVolume();
347        if(this->bgSoundChannel!=-1 && !this->muteMusic) {
348                if(this->sceneAlpha!=0) {
349                        if(this->soundStopped) {
350                                this->soundStopped = false;
351                                this->bgSoundChannel = FSOUND_Stream_Play(FSOUND_FREE, this->bgMusicStream);
352                        }
353                        FSOUND_SetVolume(this->bgSoundChannel, (int)(255*this->bgSoundVolume*this->sceneAlpha));
354                } else {
355                        FSOUND_Stream_Stop(this->bgMusicStream);
356                        this->soundStopped = true;
357                }
358        }
359}
360
361/*void MenueScene::setBackgroundSoundMute(bool _muteMusic)
362{       
363        this->muteMusic = _muteMusic;
364        if(!this->muteMusic) {
365                if(this->bgSoundChannel!=-1) {
366                        if(this->soundStopped) {
367                                this->soundStopped = false;
368                                this->bgSoundChannel = FSOUND_Stream_Play(FSOUND_FREE, this->bgMusicStream);
369                        }
370                        FSOUND_SetVolume(this->bgSoundChannel, (int)(255*this->bgSoundVolume*this->sceneAlpha));
371                }
372        } else {
373                if(this->bgSoundChannel!=-1) {
374                        FSOUND_SetVolume(this->bgSoundChannel, 0);
375                }
376        }
377}*/
Note: See TracBrowser for help on using the repository browser.