Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
244 chris 1
#include "GameApp.h"
2
#include "TitleScreen.h"
3
#include "Board.h"
4
#include "DemoDialog.h"
5
#include "SexyAppFramework/WidgetManager.h"
6
 
7
#include "SexyAppFramework/Checkbox.h"
8
 
9
// We will be accessing the resource manager in this demo, so include it's header
10
#include "SexyAppFramework/ResourceManager.h"
11
 
12
// Required for playing music
13
#include "SexyAppFramework/BassMusicInterface.h"
14
 
15
// Contains all the resources from the resources.xml file in our
16
// properties directory. See that file for more information.
17
#include "Res.h"
18
 
19
#include "SexyAppFramework/Dialog.h"
20
 
21
// The SexyAppFramework resides in the "Sexy" namespace. As a convenience,
22
// you'll see in all the .cpp files "using namespace Sexy" to avoid
23
// having to prefix everything with Sexy::
24
using namespace Sexy;
25
 
26
 
27
//////////////////////////////////////////////////////////////////////////
28
//////////////////////////////////////////////////////////////////////////
29
GameApp::GameApp()
30
{
31
        // mProdName is used for internal purposes to indicate the game that we're working on
32
        mProdName = "Demo 5";
33
 
34
        // For internal uses, indicates the current product version
35
        mProductVersion = "1.0";
36
 
37
        // This is the text that appears in the title bar of the application window
38
        mTitle = StringToSexyStringFast("SexyAppFramework: " + mProdName + " - " + mProductVersion);
39
 
40
        // Indicates the registry location where all registry keys will be read from
41
        // and written to. This is stored under the HKEY_CURRENT_USER tree on 
42
        // Windows systems.
43
        mRegKey = "PopCap\\SexyAppFramework\\Demo5";
44
 
45
        // Set the application width/height in terms of pixels here.
46
        mWidth = 640;
47
        mHeight = 480;
48
 
49
        // By setting this to true, the framework will automatically check to see
50
        // if hardware acceleration can be turned on. This doesn't guarantee that it
51
        // WILL be turned on, however. Some cards just aren't compatible or have
52
        // known issues. Also, cards with less than 8MB of video RAM aren't supported.
53
        // There are ways to override the 3D enabled settings, which we will discuss
54
        // in a later demo. As a side note, if you want to see if you app is
55
        // running with 3D acceleration, first enable debug keys by pressing
56
        // CTRL-ALT-D and then press F8. To toggle 3D on/off, press shift-F8. That is just
57
        // for testing purposes.
58
        mAutoEnable3D = true;
59
 
60
        mBoard = NULL;
61
        mTitleScreen = NULL;
62
 
63
}
64
 
65
//////////////////////////////////////////////////////////////////////////
66
//////////////////////////////////////////////////////////////////////////
67
GameApp::~GameApp()
68
{
69
        // Remove our "Board" class which was, in this particular demo,
70
        // responsible for all our game drawing and updating.
71
        // All widgets MUST be removed from the widget manager before deletion.
72
        // More information on the basics of widgets can be found in the Board
73
        // class file. If you tried to delete the Board widget before removing
74
        // it, you will get an assert. Because our board might not have been
75
        // added (if you shut down the app before closing the loading screen),
76
        // only remove it if it isn't null.
77
        if (mBoard != NULL)
78
                mWidgetManager->RemoveWidget(mBoard);
79
 
80
        // Take a look at TitleScreen::ButtonDepress if you haven't already.
81
        // It explains a function called SafeDeleteWidget. Notice that we're
82
        // directly deleting the widget here: that is because when our app's
83
        // destructor is called, it's at the very end of the shutdown sequence
84
        // and the safe delete widget list will NOT be processed. Thus we
85
        // have to delete the memory manually.
86
        delete mBoard;
87
 
88
 
89
        // If you shut down the app before closing the loading screen, then
90
        // it will need to be removed here. The rational for the next two
91
        // steps is the same as for Board:
92
        if (mTitleScreen != NULL)
93
                mWidgetManager->RemoveWidget(mTitleScreen);
94
        delete mTitleScreen;
95
 
96
        // We should also free up all the resources that we loaded
97
        // for ALL the resource groups. Deleting a group that was
98
        // already deleted doesn't do anything, it's ignored.
99
        mResourceManager->DeleteResources("Init");
100
        mResourceManager->DeleteResources("TitleScreen");
101
        mResourceManager->DeleteResources("Game");
102
 
103
}
104
 
105
//////////////////////////////////////////////////////////////////////////
106
//////////////////////////////////////////////////////////////////////////
107
void GameApp::Init()
108
{
109
        // Let the parent class perform any needed initializations first.
110
        // This should always be done.
111
        SexyAppBase::Init();
112
 
113
        // We need to tell the resource manager to read in all the groups
114
        // and information from that main group we made, called ResourceManifest,
115
        // in the file "properties/resources.xml". The path/filename are
116
        // by default set up to load that file, so you must name it exactly as such.
117
        // This doesn't load any resources: it just parses the data and sets
118
        // things up for loading.
119
        LoadResourceManifest();
120
 
121
        // Next, we want to load our absolutely necessary files that have to
122
        // be loaded before anything else can run. You'll notice in the resources.xml
123
        // file that we created a group called Init that contains these resources.
124
        // You may call it whatever you like. Let's load those resources now.
125
        // We do that by calling the LoadResources method of our mResourceManager
126
        // variable and specifying in quotes the name of the resource group to 
127
        // load. This string is case sensitive.
128
        if (!mResourceManager->LoadResources("Init"))
129
        {
130
                mLoadingFailed = true;
131
                // This will display an informative error message indicating exactly
132
                // what went wrong in the resource loading process.
133
                ShowResourceError(true);
134
                return;
135
        }
136
 
137
        // Now we've loaded the resources, but we need to extract them.
138
        // Extraction is the phase that converts sound files to raw WAV
139
        // files, and sets up and initializes fonts and palletizes images.
140
        // The ResourceGen.exe program, when it generates C++ code for our
141
        // resources, also creates a function for EVERY resource group of the
142
        // form: Extract<GROUP>Resources, where <GROUP> is the exact name
143
        // of the resource group you made. In our case, we made an "Init"
144
        // group, so we have an ExtractInitResources method. You pass to it
145
        // the pointer to the resource manager. Because an error can occur
146
        // during this step, you should make sure to check for it.
147
        if (!ExtractInitResources(mResourceManager))
148
        {
149
                mLoadingFailed = true;
150
                ShowResourceError(true);
151
                return;
152
        }
153
 
154
        // We also need to load our title screen graphics in, since you can't 
155
        // display the title screen without any graphics. For an explanation of why
156
        // we placed this in a separate group from Init, see properties/resources.xml.
157
        // This code works exactly like the above did for the Init group.
158
        if (!mResourceManager->LoadResources("TitleScreen"))
159
        {
160
                mLoadingFailed = true;
161
                ShowResourceError(true);
162
                return;
163
        }
164
 
165
        if (!ExtractTitleScreenResources(mResourceManager))
166
        {
167
                mLoadingFailed = true;
168
                ShowResourceError(true);
169
                return;
170
        }
171
 
172
        // Now let's create and add our title screen to the widget manager
173
        mTitleScreen = new TitleScreen(this);
174
        mTitleScreen->Resize(0, 0, mWidth, mHeight);
175
 
176
        // Let's let the title screen initialize it's widgets and data
177
        // before adding it to the widget manager:
178
        mTitleScreen->Init();
179
 
180
        mWidgetManager->AddWidget(mTitleScreen);
181
 
182
        // Let's also load in some music to play. We use the mMusicInterface
183
        // member for all our music needs, which requires the BassMusicInterface.h
184
        // header to be loaded, since we use the library BASS to play our music.
185
        // We can load in WAV, OGG, or MP3 files. BASS also supports a number
186
        // of tracker formats, such as .it, .xm, .mod, etc. It also supports
187
        // a format called MO3, which is a compressed version of a tracker
188
        // file. For this example, we will use the MO3 from AstroPop.
189
        // Why? Cause it's ours and we won't get sued for using it.
190
        // We load our file manually, we do not use the resource manager for this.
191
        // The first parameter is the ID to associate the song with. Just as sounds
192
        // have IDs, so do music tracks.
193
        mMusicInterface->LoadMusic(0, "music/music.mo3");
194
 
195
        // Let's load another copy of the file. Why? In order to fade from one
196
        // track to another, we need two instances of the track on different
197
        // channels. Let's load it again and give it a different ID, 1.
198
        mMusicInterface->LoadMusic(1, "music/music.mo3");
199
 
200
        // Now we need to start playing a track. Because we are using an MO3
201
        // and because the original format was a .it (Impulse Tracker) file,
202
        // there are actually multiple songs inside of it, differentiated
203
        // by various offsets. If you were just playing a single MP3 or OGG
204
        // or WAV file instead of a tracker file, you would ignore this
205
        // and use the default offset of 0 for the start of the song.
206
        // Because the person that made the song file was nice and
207
        // told us which offsets equated to which song pieces, I already
208
        // know the magic offset numbers. In this particular case, the
209
        // song for the intro screen is at offset 0, and the song
210
        // for the main game music is at offset 9. Our music artist
211
        // also was kind enough to put in tracker looping commands,
212
        // so you'll notice that the songs play over and over. A discussion
213
        // of tracker file formats is beyond the scope of this. Again,
214
        // if you are just playing a WAV/OGG/MP3, you use offset 0 (the default)
215
        // to indicate that you want to start playing from the start of the song.
216
        //
217
        // You can use PlayMusic to instantly play the track, or, like below,
218
        // you can use FadeIn to smoothly fade the song in. The first parameter
219
        // for both methods is the channel or song id that was used when the
220
        // track was first loaded (In our case, either 0 or 1 works). For both,
221
        // the second parameter is the offset to start playing at. Again, I just
222
        // happen to know that the intro song is at offset 0. For FadeIn, the
223
        // third parameter is how quickly to fade in, out of 1.0. The last parameter
224
        // for both indicates whether or not you want to loop. This is kind of weird,
225
        // but specify "false" to loop and "true" to not loop.
226
        mMusicInterface->FadeIn(0, 0, 0.002, false);
227
 
228
        // We'll cover changing the music and sound volumes in a later demo.
229
 
230
        // Next, we need to know how many resources there are to load.
231
        // This is necessary so we can display our progress bar on the title screen
232
        // and make it be the appropriate length. There's a variable in SexyAppBase
233
        // called mNumLoadingThreadTasks which holds the number of resources to
234
        // load in the LoadingThreadProc function. You get the number of resources
235
        // in a given group with a call to the resource manager's GetNumResources function
236
        // for each of your groups that you are going to load:
237
        mNumLoadingThreadTasks = mResourceManager->GetNumResources("Game");
238
}
239
 
240
//////////////////////////////////////////////////////////////////////////
241
//////////////////////////////////////////////////////////////////////////
242
void GameApp::LoadingThreadProc()
243
{
244
        // For each of the groups that we want to load,
245
        // we first have to instruct the resource manager to begin the
246
        // loading phase and initialize its internal variables. 
247
        // We do that with the StartLoadResources method and pass in the 
248
        // exact string name of the group to begin loading:
249
        mResourceManager->StartLoadResources("Game");
250
 
251
        // Now we need to load each individual resource. We will loop,
252
        // calling LoadNextResource at the start. When it returns true,
253
        // there are no more resources to load for the current group.
254
        // LoadNextResource knows what group to load from because 
255
        // of the call to StartLoadResources above:
256
        while (mResourceManager->LoadNextResource())
257
        {
258
                // The SexyAppBase variable, mCompletedLoadingThreadTasks, indicates the
259
                // total number of resources that have so far been loaded. This is used
260
                // to tell our loading screen the % progress we've made. See TitleScreen::Draw
261
                // for an example of how this is used. We need to increment this value
262
                // ourselves everytime we load a resource:
263
                mCompletedLoadingThreadTasks++;
264
 
265
                // If there was an error loading our resource, the resource manager
266
                // will tell us to shut down by setting mShutdown to true. If that
267
                // happened, immediately abort and return:
268
                if (mShutdown)
269
                        return;
270
 
271
                // Remember in demos 1-3 how we had the Board class call MarkDirty
272
                // every update? Well, the title screen doesn't need to be such a hog.
273
                // The title screen only needs to repaint when its progress bar changes
274
                // size. The progress bar only changes size when a resource gets loaded.
275
                // Because the game app is the only one that knows when this happens,
276
                // the game app will be the one to tell the title screen that it's a
277
                // dirty, dirty widget and that it needs a good and proper repainting.
278
                // You COULD make an update method for the title screen and mark dirty
279
                // every frame. But because this consumes more CPU time, it will take
280
                // longer to load our resources. And since you want the loading time
281
                // to be as quick as possible, you should only repaint when you need to.
282
                mTitleScreen->MarkDirty();
283
        }
284
 
285
        // Just like in our Init function, after loading resources we
286
        // need to extract them. Let's do that. Let's also ask the resource
287
        // manager if an error occurred in the above loop that we
288
        // didn't yet catch. We do that with the HadError method:
289
        if (mResourceManager->HadError() || !ExtractGameResources(mResourceManager))
290
        {              
291
                ShowResourceError(false);
292
                mLoadingFailed = true;
293
                return;
294
        }
295
 
296
}
297
 
298
//////////////////////////////////////////////////////////////////////////
299
//////////////////////////////////////////////////////////////////////////
300
void GameApp::LoadingThreadCompleted()
301
{
302
        // Let the base app class also know that we have completed
303
        SexyAppBase::LoadingThreadCompleted();
304
 
305
        // When we're actually loading resources, we'll set the
306
        // mLoadingFailed variable to "true" if there were any problems
307
        // encountered along the way. If that is the case, just return
308
        // because we won't want the user to get to the main menu or any
309
        // other part of the game. We will want them to exit out.
310
        if (mLoadingFailed)
311
                return;
312
 
313
 
314
        // We aren't going to make and add the Board class here like we
315
        // did in the previous demos. Instead, since we are done loading
316
        // everything, we're going to tell the title screen that 
317
        // we're done and that it should unhide the continue link and let
318
        // the user enter the game.
319
        mTitleScreen->LoadingComplete();
320
 
321
        // Remember: since we didn't give our title screen an Update method,
322
        // this class is responsible for telling it when to repaint. If we
323
        // don't mark it dirty, you won't see the hyperlink widget
324
        // appear. So mark it dirty now:
325
        mTitleScreen->MarkDirty();
326
}
327
 
328
//////////////////////////////////////////////////////////////////////////
329
//////////////////////////////////////////////////////////////////////////
330
void GameApp::TitleScreenIsFinished()
331
{
332
        mTitleScreen = NULL;
333
        mBoard = new Board(this);
334
 
335
        // Now that the title screen is done, we don't need its resources
336
        // wasting memory. Let's delete all of its resources. We do that
337
        // by calling DeleteResources and specifying the exact name of the
338
        // resource group we want to free up:
339
        mResourceManager->DeleteResources("TitleScreen");
340
 
341
        mBoard->Resize(0, 0, mWidth, mHeight);
342
        mWidgetManager->AddWidget(mBoard);
343
 
344
        // This is a new step: We're going to tell the WidgetManager
345
        // that keyboard input and mouse wheel notifications should go to
346
        // the board object. This way, we'll be able to respond to keypresses:
347
        mWidgetManager->SetFocus(mBoard);
348
 
349
 
350
 
351
        // Let's fade out the intro song and fade in the main game music.
352
        // FadeOut works just like FadeIn did in Init() but with some
353
        // slightly different parameters. The first, is like with FadeIn and
354
        // PlayMusic, the channel or song id that you want to mess with.
355
        // The second indicates that the song fading out should stop when
356
        // done, if it is true. The final parameter indicates how fast
357
        // to fade out, and is from 0 to 1.
358
        mMusicInterface->FadeOut(0, true, 0.004);
359
 
360
        // Let's fade in the main game music. This is the same as in Init.
361
        // The only difference is we're using 1 instead of 0 for our song id.
362
        // Why? Well, channel/song id 0 is being used to fade out the 
363
        // previously playing track, we can't use it to also fade in.
364
        // That's why we loaded another copy of the song into channel 1.
365
        // Again, as explained in Init, I happen to know that offset 9
366
        // is the start of the main game music.
367
        mMusicInterface->FadeIn(1, 9, 0.002, false);
368
 
369
        // We'll cover changing the music and sound volumes in our options dialog.
370
}
371
 
372
//////////////////////////////////////////////////////////////////////////
373
//////////////////////////////////////////////////////////////////////////
374
Dialog* GameApp::NewDialog(int theDialogId, bool isModal, const std::string& theDialogHeader,
375
                                                   const std::string& theDialogLines, const std::string& theDialogFooter, int theButtonMode)
376
{
377
        // Rather than dupliate a lengthy explanation, check out the top of DemoDialog.cpp for a complete description
378
        // of what all the parameters and functions are.
379
        Dialog* d = new Dialog(IMAGE_DIALOG_BOX, IMAGE_DIALOG_BUTTON, theDialogId, isModal,
380
                                                        StringToSexyStringFast(theDialogHeader), StringToSexyStringFast(theDialogLines), StringToSexyStringFast(theDialogFooter), theButtonMode);
381
 
382
        d->SetButtonFont(FONT_DEFAULT);
383
        d->SetLinesFont(FONT_DEFAULT);
384
        d->SetHeaderFont(FONT_DEFAULT);
385
 
386
        d->SetColor(Dialog::COLOR_HEADER, Color::Black);
387
        d->SetColor(Dialog::COLOR_LINES, Color::Black);
388
 
389
        d->mSpaceAfterHeader = 30;
390
        d->Resize(100, 100, 300, 250);
391
 
392
        return d;
393
}
394
 
395
//////////////////////////////////////////////////////////////////////////
396
//////////////////////////////////////////////////////////////////////////
397
void GameApp::SwitchScreenMode(bool wantWindowed, bool is3d)
398
{
399
        // Let the app handle the actual details of this call...
400
        SexyAppBase::SwitchScreenMode(wantWindowed, is3d);
401
 
402
        // We can see if the options dialog is up with a call to
403
        // GetDialog. You pass GetDialog the unique ID of the dialog box,
404
        // and if it exists it is returned to you, otherwise NULL is returned.
405
        DemoDialog* d = (DemoDialog*) GetDialog(DemoDialog::DIALOG_ID);
406
 
407
        // Set the checkbox state to our windowed state
408
        if ((d != NULL) && (d->mFSCheckbox != NULL))
409
                d->mFSCheckbox->SetChecked(!wantWindowed);
410
 
411
}
412
 
413
//////////////////////////////////////////////////////////////////////////
414
//////////////////////////////////////////////////////////////////////////
415
void GameApp::ButtonPress(int theId)
416
{
417
        if (theId == DemoDialog::MESSAGE_BOX_ID + 2000)
418
                KillDialog(theId - 2000);
419
}
420
 
421
//////////////////////////////////////////////////////////////////////////
422
//////////////////////////////////////////////////////////////////////////
423
void GameApp::SetFocusToBoard()
424
{
425
        if (mBoard != NULL)
426
                mWidgetManager->SetFocus(mBoard);
427
}