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

Revision 1401, 11.7 KB checked in by giegl, 18 years ago (diff)

GTPD - Jungle Rumble - fixed all warnings (except performance)

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 = int( 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_LOADGAME, L"Start Level", 0, 0, 120, 35, L'8' );
225        //this->GUI->AddButton( IDC_EXIT, L"Exit Game...", 30, 430, 120, 35, L'X' );
226        this->GUI->AddButton( IDC_EXIT, L"Exit Game", 30, 430, 120, 35, L'X' );
227
228        // List box
229    this->GUI->AddListBox( IDC_CHALLENGELIST, 30, 200, 200, 150, 2 );
230        for( UINT i = 0; i < this->challengeNameList.size(); ++i )
231    {
232                std::wstring temp2(this->challengeNameList.at(i).begin(), this->challengeNameList.at(i).end());
233                this->GUI->GetListBox( IDC_CHALLENGELIST )->AddItem( temp2.c_str(), (LPVOID)(size_t)i );
234    }
235
236        float factorX = ((float)this->manager->screenWidth)/((float)100);
237        float factorY = ((float)this->manager->screenHeight)/((float)100);
238       
239        this->GUI->GetListBox(IDC_CHALLENGELIST)->SetLocation((int)(81*factorX), (int)(5*factorY));
240        this->GUI->GetListBox(IDC_CHALLENGELIST)->SetSize((int)(18*factorX), (int)(52*factorY));
241        this->GUI->GetListBox(IDC_CHALLENGELIST)->GetElement(0)->iFont = 1;
242        this->GUI->GetListBox(IDC_CHALLENGELIST)->GetElement(1)->iFont = 1;
243
244        this->GUI->GetButton(IDC_LOADGAME)->SetLocation((int)(81*factorX), (int)(65*factorY));
245        this->GUI->GetButton(IDC_LOADGAME)->SetSize((int)(18*factorX), (int)(10*factorY));
246        this->GUI->GetButton(IDC_LOADGAME)->GetElement(0)->iFont = 1;
247        this->GUI->GetButton(IDC_LOADGAME)->GetElement(1)->iFont = 1;
248       
249        this->GUI->GetButton(IDC_EXIT)->SetLocation((int)(81*factorX), (int)(81*factorY));
250        this->GUI->GetButton(IDC_EXIT)->SetSize((int)(18*factorX), (int)(10*factorY));
251        this->GUI->GetButton(IDC_EXIT)->GetElement(0)->iFont = 1;
252        this->GUI->GetButton(IDC_EXIT)->GetElement(1)->iFont = 1;
253}
254
255void MenueScene::renderGUI(float fElapsedTime) {
256        this->device->BeginScene();
257        this->GUI->OnRender(fElapsedTime);
258        this->device->EndScene();
259}
260
261void MenueScene::renderBackground() {
262        this->device->BeginScene();
263        this->sprite->Begin(D3DXSPRITE_ALPHABLEND);
264        if(this->backgroundIndex == -1) {
265                this->sprite->Draw(this->Textures[0], NULL, NULL, NULL, 0xFFFFFFFF);
266        } else {
267                this->sprite->Draw(this->LoadingTextures[this->backgroundIndex], NULL, NULL, NULL, 0xFFFFFFFF);
268        }
269        this->sprite->End();
270        this->device->EndScene();
271}
272
273void MenueScene::setDialogResourceManager(CDXUTDialogResourceManager * DialogResourceManager) {
274        this->DialogResourceManager = DialogResourceManager;
275}
276
277void MenueScene::setGUI(CDXUTDialog * GUI) {
278        this->GUI = GUI;
279}
280
281void MenueScene::OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext )
282{
283    switch( nControlID )
284    {
285        case IDC_LOADGAME:
286                        this->prepareLoading(this->selectedChallengeNumber);
287                        break;
288                case IDC_CHALLENGELIST:
289                        switch( nEvent )
290            {
291                case EVENT_LISTBOX_SELECTION:
292                                        this->selectedChallengeNumber = ((CDXUTListBox *)pControl)->GetSelectedIndex();
293                                        break;
294                                default:
295                                        break;
296                        }
297                        break;
298                case IDC_EXIT:
299                        SendMessage( DXUTGetHWND(), WM_CLOSE, 0, 0 );
300                        break;
301    }
302}
303
304std::vector<std::string>* MenueScene::getChallengeList() {
305        return &this->challengeList;
306}
307
308std::vector<std::string>* MenueScene::getChallengeNameList() {
309        return &this->challengeNameList;
310}
311
312std::vector<std::string>* MenueScene::getChallengeScreenList() {
313        return &this->challengeScreenList;
314}
315
316LPD3DXSPRITE* MenueScene::getSprite() {
317        return &this->sprite;
318}
319
320void MenueScene::OnLostDevice( void* pUserContext )
321{
322        this->sprite->OnLostDevice();
323        Scene::OnLostDevice(pUserContext);
324}
325
326void MenueScene::OnDestroyDevice( void* pUserContext )
327{
328        SAFE_RELEASE(this->sprite);
329        this->sprite = NULL;
330        this->Textures.clear();
331        Scene::OnDestroyDevice(pUserContext);
332}
333
334HRESULT MenueScene::OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
335{
336        Scene::OnResetDevice(pd3dDevice, pBackBufferSurfaceDesc, pUserContext);
337        D3DXCreateSprite(this->device,&sprite);
338       
339        //load background texture:
340        Textures.push_back(this->manager->resManager.loadTexture("./media/textures/MenuBackground.JPG"));
341       
342        //draw crosshair:
343        Textures[0]->GetLevelDesc(0, &desc);    //get texture size             
344        //calculate transfomation:
345        D3DXVECTOR2 scaling((float)this->manager->screenWidth/desc.Width,(float)this->manager->screenHeight/desc.Height);
346        //set matrix:
347        D3DXMatrixTransformation2D(&spriteMatrix,NULL,0.0,&scaling,NULL,0,NULL);
348        this->sprite->SetTransform(&spriteMatrix);
349        return S_OK;
350}
351
352CDXUTDialog* MenueScene::getGUI() {
353        return this->GUI;
354}
355
356//void MenueScene::setBackgroundSound(std::string filename) {
357//      this->bgMusicStream = FSOUND_Stream_Open(filename.c_str(), FSOUND_LOOP_NORMAL, 0, 0);
358//}
359//
360//void MenueScene::playBackgroundSound() {
361//      if(this->bgMusicStream!=NULL) {
362//              this->bgSoundChannel = FSOUND_Stream_Play(FSOUND_FREE, this->bgMusicStream);
363//              FSOUND_SetVolume(this->bgSoundChannel, (int)(255*this->bgSoundVolume));
364//      }
365//}
366//
367void MenueScene::updateSoundNodeVolume()
368{
369        Scene::updateSoundNodeVolume();
370        if(this->bgSoundChannel!=-1 && !this->muteMusic) {
371                if(this->sceneAlpha!=0) {
372                        if(this->soundStopped) {
373                                this->soundStopped = false;
374                                this->bgSoundChannel = FSOUND_Stream_Play(FSOUND_FREE, this->bgMusicStream);
375                        }
376                        FSOUND_SetVolume(this->bgSoundChannel, (int)(255*this->bgSoundVolume*this->sceneAlpha));
377                } else {
378                        FSOUND_Stream_Stop(this->bgMusicStream);
379                        this->soundStopped = true;
380                }
381        }
382}
383
384/*void MenueScene::setBackgroundSoundMute(bool _muteMusic)
385{       
386        this->muteMusic = _muteMusic;
387        if(!this->muteMusic) {
388                if(this->bgSoundChannel!=-1) {
389                        if(this->soundStopped) {
390                                this->soundStopped = false;
391                                this->bgSoundChannel = FSOUND_Stream_Play(FSOUND_FREE, this->bgMusicStream);
392                        }
393                        FSOUND_SetVolume(this->bgSoundChannel, (int)(255*this->bgSoundVolume*this->sceneAlpha));
394                }
395        } else {
396                if(this->bgSoundChannel!=-1) {
397                        FSOUND_SetVolume(this->bgSoundChannel, 0);
398                }
399        }
400}*/
Note: See TracBrowser for help on using the repository browser.