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 "SexyAppFramework/WidgetManager.h" |
||
| 5 | |||
| 6 | // We will be accessing the resource manager in this demo, so include it's header |
||
| 7 | #include "SexyAppFramework/ResourceManager.h" |
||
| 8 | |||
| 9 | // Required for playing music |
||
| 10 | #include "SexyAppFramework/BassMusicInterface.h" |
||
| 11 | |||
| 12 | // Contains all the resources from the resources.xml file in our |
||
| 13 | // properties directory. See that file for more information. |
||
| 14 | #include "Res.h" |
||
| 15 | |||
| 16 | // The SexyAppFramework resides in the "Sexy" namespace. As a convenience, |
||
| 17 | // you'll see in all the .cpp files "using namespace Sexy" to avoid |
||
| 18 | // having to prefix everything with Sexy:: |
||
| 19 | using namespace Sexy; |
||
| 20 | |||
| 21 | |||
| 22 | ////////////////////////////////////////////////////////////////////////// |
||
| 23 | ////////////////////////////////////////////////////////////////////////// |
||
| 24 | GameApp::GameApp() |
||
| 25 | { |
||
| 26 | // mProdName is used for internal purposes to indicate the game that we're working on |
||
| 27 | mProdName = "Demo 4"; |
||
| 28 | |||
| 29 | // For internal uses, indicates the current product version |
||
| 30 | mProductVersion = "1.0"; |
||
| 31 | |||
| 32 | // This is the text that appears in the title bar of the application window |
||
| 33 | mTitle = StringToSexyStringFast("SexyAppFramework: " + mProdName + " - " + mProductVersion); |
||
| 34 | |||
| 35 | // Indicates the registry location where all registry keys will be read from |
||
| 36 | // and written to. This is stored under the HKEY_CURRENT_USER tree on |
||
| 37 | // Windows systems. |
||
| 38 | mRegKey = "PopCap\\SexyAppFramework\\Demo4"; |
||
| 39 | |||
| 40 | // Set the application width/height in terms of pixels here. Let's |
||
| 41 | // use a different resolution from Demo 1 just for fun. |
||
| 42 | mWidth = 800; |
||
| 43 | mHeight = 600; |
||
| 44 | |||
| 45 | // By setting this to true, the framework will automatically check to see |
||
| 46 | // if hardware acceleration can be turned on. This doesn't guarantee that it |
||
| 47 | // WILL be turned on, however. Some cards just aren't compatible or have |
||
| 48 | // known issues. Also, cards with less than 8MB of video RAM aren't supported. |
||
| 49 | // There are ways to override the 3D enabled settings, which we will discuss |
||
| 50 | // in a later demo. As a side note, if you want to see if you app is |
||
| 51 | // running with 3D acceleration, first enable debug keys by pressing |
||
| 52 | // CTRL-ALT-D and then press F9. To toggle 3D on/off, press F8. That is just |
||
| 53 | // for testing purposes. |
||
| 54 | mAutoEnable3D = true; |
||
| 55 | |||
| 56 | mBoard = NULL; |
||
| 57 | mTitleScreen = NULL; |
||
| 58 | |||
| 59 | // See Board::UpdateF for a very lengthy explanation of this and smooth motion |
||
| 60 | mVSyncUpdates = true; |
||
| 61 | } |
||
| 62 | |||
| 63 | ////////////////////////////////////////////////////////////////////////// |
||
| 64 | ////////////////////////////////////////////////////////////////////////// |
||
| 65 | GameApp::~GameApp() |
||
| 66 | { |
||
| 67 | // Remove our "Board" class which was, in this particular demo, |
||
| 68 | // responsible for all our game drawing and updating. |
||
| 69 | // All widgets MUST be removed from the widget manager before deletion. |
||
| 70 | // More information on the basics of widgets can be found in the Board |
||
| 71 | // class file. If you tried to delete the Board widget before removing |
||
| 72 | // it, you will get an assert. Because our board might not have been |
||
| 73 | // added (if you shut down the app before closing the loading screen), |
||
| 74 | // only remove it if it isn't null. |
||
| 75 | if (mBoard != NULL) |
||
| 76 | mWidgetManager->RemoveWidget(mBoard); |
||
| 77 | |||
| 78 | // Take a look at TitleScreen::ButtonDepress if you haven't already. |
||
| 79 | // It explains a function called SafeDeleteWidget. Notice that we're |
||
| 80 | // directly deleting the widget here: that is because when our app's |
||
| 81 | // destructor is called, it's at the very end of the shutdown sequence |
||
| 82 | // and the safe delete widget list will NOT be processed. Thus we |
||
| 83 | // have to delete the memory manually. |
||
| 84 | delete mBoard; |
||
| 85 | |||
| 86 | |||
| 87 | // If you shut down the app before closing the loading screen, then |
||
| 88 | // it will need to be removed here. The rational for the next two |
||
| 89 | // steps is the same as for Board: |
||
| 90 | if (mTitleScreen != NULL) |
||
| 91 | mWidgetManager->RemoveWidget(mTitleScreen); |
||
| 92 | delete mTitleScreen; |
||
| 93 | |||
| 94 | // We should also free up all the resources that we loaded |
||
| 95 | // for ALL the resource groups. Deleting a group that was |
||
| 96 | // already deleted doesn't do anything, it's ignored. |
||
| 97 | mResourceManager->DeleteResources("Init"); |
||
| 98 | mResourceManager->DeleteResources("TitleScreen"); |
||
| 99 | mResourceManager->DeleteResources("Game"); |
||
| 100 | |||
| 101 | } |
||
| 102 | |||
| 103 | ////////////////////////////////////////////////////////////////////////// |
||
| 104 | ////////////////////////////////////////////////////////////////////////// |
||
| 105 | void GameApp::Init() |
||
| 106 | { |
||
| 107 | // Let the parent class perform any needed initializations first. |
||
| 108 | // This should always be done. |
||
| 109 | SexyAppBase::Init(); |
||
| 110 | |||
| 111 | // We need to tell the resource manager to read in all the groups |
||
| 112 | // and information from that main group we made, called ResourceManifest, |
||
| 113 | // in the file "properties/resources.xml". The path/filename are |
||
| 114 | // by default set up to load that file, so you must name it exactly as such. |
||
| 115 | // This doesn't load any resources: it just parses the data and sets |
||
| 116 | // things up for loading. |
||
| 117 | LoadResourceManifest(); |
||
| 118 | |||
| 119 | // Next, we want to load our absolutely necessary files that have to |
||
| 120 | // be loaded before anything else can run. You'll notice in the resources.xml |
||
| 121 | // file that we created a group called Init that contains these resources. |
||
| 122 | // You may call it whatever you like. Let's load those resources now. |
||
| 123 | // We do that by calling the LoadResources method of our mResourceManager |
||
| 124 | // variable and specifying in quotes the name of the resource group to |
||
| 125 | // load. This string is case sensitive. |
||
| 126 | if (!mResourceManager->LoadResources("Init")) |
||
| 127 | { |
||
| 128 | mLoadingFailed = true; |
||
| 129 | // This will display an informative error message indicating exactly |
||
| 130 | // what went wrong in the resource loading process. |
||
| 131 | ShowResourceError(true); |
||
| 132 | return; |
||
| 133 | } |
||
| 134 | |||
| 135 | // Now we've loaded the resources, but we need to extract them. |
||
| 136 | // Extraction is the phase that converts sound files to raw WAV |
||
| 137 | // files, and sets up and initializes fonts and palletizes images. |
||
| 138 | // The ResourceGen.exe program, when it generates C++ code for our |
||
| 139 | // resources, also creates a function for EVERY resource group of the |
||
| 140 | // form: Extract<GROUP>Resources, where <GROUP> is the exact name |
||
| 141 | // of the resource group you made. In our case, we made an "Init" |
||
| 142 | // group, so we have an ExtractInitResources method. You pass to it |
||
| 143 | // the pointer to the resource manager. Because an error can occur |
||
| 144 | // during this step, you should make sure to check for it. |
||
| 145 | if (!ExtractInitResources(mResourceManager)) |
||
| 146 | { |
||
| 147 | mLoadingFailed = true; |
||
| 148 | ShowResourceError(true); |
||
| 149 | return; |
||
| 150 | } |
||
| 151 | |||
| 152 | // We also need to load our title screen graphics in, since you can't |
||
| 153 | // display the title screen without any graphics. For an explanation of why |
||
| 154 | // we placed this in a separate group from Init, see properties/resources.xml. |
||
| 155 | // This code works exactly like the above did for the Init group. |
||
| 156 | if (!mResourceManager->LoadResources("TitleScreen")) |
||
| 157 | { |
||
| 158 | mLoadingFailed = true; |
||
| 159 | ShowResourceError(true); |
||
| 160 | return; |
||
| 161 | } |
||
| 162 | |||
| 163 | if (!ExtractTitleScreenResources(mResourceManager)) |
||
| 164 | { |
||
| 165 | mLoadingFailed = true; |
||
| 166 | ShowResourceError(true); |
||
| 167 | return; |
||
| 168 | } |
||
| 169 | |||
| 170 | // Now let's create and add our title screen to the widget manager |
||
| 171 | mTitleScreen = new TitleScreen(this); |
||
| 172 | mTitleScreen->Resize(0, 0, mWidth, mHeight); |
||
| 173 | |||
| 174 | // Let's let the title screen initialize it's widgets and data |
||
| 175 | // before adding it to the widget manager: |
||
| 176 | mTitleScreen->Init(); |
||
| 177 | |||
| 178 | mWidgetManager->AddWidget(mTitleScreen); |
||
| 179 | |||
| 180 | // Let's also load in some music to play. We use the mMusicInterface |
||
| 181 | // member for all our music needs, which requires the BassMusicInterface.h |
||
| 182 | // header to be loaded, since we use the library BASS to play our music. |
||
| 183 | // We can load in WAV, OGG, or MP3 files. BASS also supports a number |
||
| 184 | // of tracker formats, such as .it, .xm, .mod, etc. It also supports |
||
| 185 | // a format called MO3, which is a compressed version of a tracker |
||
| 186 | // file. For this example, we will use the MO3 from AstroPop. |
||
| 187 | // Why? Cause it's ours and we won't get sued for using it. |
||
| 188 | // We load our file manually, we do not use the resource manager for this. |
||
| 189 | // The first parameter is the ID to associate the song with. Just as sounds |
||
| 190 | // have IDs, so do music tracks. |
||
| 191 | mMusicInterface->LoadMusic(0, "music/music.mo3"); |
||
| 192 | |||
| 193 | // Let's load another copy of the file. Why? In order to fade from one |
||
| 194 | // track to another, we need two instances of the track on different |
||
| 195 | // channels. Let's load it again and give it a different ID, 1. |
||
| 196 | mMusicInterface->LoadMusic(1, "music/music.mo3"); |
||
| 197 | |||
| 198 | // Now we need to start playing a track. Because we are using an MO3 |
||
| 199 | // and because the original format was a .it (Impulse Tracker) file, |
||
| 200 | // there are actually multiple songs inside of it, differentiated |
||
| 201 | // by various offsets. If you were just playing a single MP3 or OGG |
||
| 202 | // or WAV file instead of a tracker file, you would ignore this |
||
| 203 | // and use the default offset of 0 for the start of the song. |
||
| 204 | // Because the person that made the song file was nice and |
||
| 205 | // told us which offsets equated to which song pieces, I already |
||
| 206 | // know the magic offset numbers. In this particular case, the |
||
| 207 | // song for the intro screen is at offset 0, and the song |
||
| 208 | // for the main game music is at offset 9. Our music artist |
||
| 209 | // also was kind enough to put in tracker looping commands, |
||
| 210 | // so you'll notice that the songs play over and over. A discussion |
||
| 211 | // of tracker file formats is beyond the scope of this. Again, |
||
| 212 | // if you are just playing a WAV/OGG/MP3, you use offset 0 (the default) |
||
| 213 | // to indicate that you want to start playing from the start of the song. |
||
| 214 | // |
||
| 215 | // You can use PlayMusic to instantly play the track, or, like below, |
||
| 216 | // you can use FadeIn to smoothly fade the song in. The first parameter |
||
| 217 | // for both methods is the channel or song id that was used when the |
||
| 218 | // track was first loaded (In our case, either 0 or 1 works). For both, |
||
| 219 | // the second parameter is the offset to start playing at. Again, I just |
||
| 220 | // happen to know that the intro song is at offset 0. For FadeIn, the |
||
| 221 | // third parameter is how quickly to fade in, out of 1.0. The last parameter |
||
| 222 | // for both indicates whether or not you want to loop. This is kind of weird, |
||
| 223 | // but specify "false" to loop and "true" to not loop. |
||
| 224 | mMusicInterface->FadeIn(0, 0, 0.002, false); |
||
| 225 | |||
| 226 | // We'll cover changing the music and sound volumes in a later demo. |
||
| 227 | |||
| 228 | // Next, we need to know how many resources there are to load. |
||
| 229 | // This is necessary so we can display our progress bar on the title screen |
||
| 230 | // and make it be the appropriate length. There's a variable in SexyAppBase |
||
| 231 | // called mNumLoadingThreadTasks which holds the number of resources to |
||
| 232 | // load in the LoadingThreadProc function. You get the number of resources |
||
| 233 | // in a given group with a call to the resource manager's GetNumResources function |
||
| 234 | // for each of your groups that you are going to load: |
||
| 235 | mNumLoadingThreadTasks = mResourceManager->GetNumResources("Game"); |
||
| 236 | } |
||
| 237 | |||
| 238 | ////////////////////////////////////////////////////////////////////////// |
||
| 239 | ////////////////////////////////////////////////////////////////////////// |
||
| 240 | void GameApp::LoadingThreadProc() |
||
| 241 | { |
||
| 242 | // This time, things are different. We aren't manually loading |
||
| 243 | // our fonts, sounds, and images. The resource manager is doing |
||
| 244 | // it for us. 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 false, |
||
| 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 | |||
| 294 | return; |
||
| 295 | } |
||
| 296 | |||
| 297 | } |
||
| 298 | |||
| 299 | ////////////////////////////////////////////////////////////////////////// |
||
| 300 | ////////////////////////////////////////////////////////////////////////// |
||
| 301 | void GameApp::LoadingThreadCompleted() |
||
| 302 | { |
||
| 303 | // Let the base app class also know that we have completed |
||
| 304 | SexyAppBase::LoadingThreadCompleted(); |
||
| 305 | |||
| 306 | // When we're actually loading resources, we'll set the |
||
| 307 | // mLoadingFailed variable to "true" if there were any problems |
||
| 308 | // encountered along the way. If that is the case, just return |
||
| 309 | // because we won't want the user to get to the main menu or any |
||
| 310 | // other part of the game. We will want them to exit out. |
||
| 311 | if (mLoadingFailed) |
||
| 312 | return; |
||
| 313 | |||
| 314 | |||
| 315 | // We aren't going to make and add the Board class here like we |
||
| 316 | // did in the previous demos. Instead, since we are done loading |
||
| 317 | // everything, we're going to tell the title screen that |
||
| 318 | // we're done and that it should unhide the continue link and let |
||
| 319 | // the user enter the game. |
||
| 320 | mTitleScreen->LoadingComplete(); |
||
| 321 | |||
| 322 | // Remember: since we didn't give our title screen an Update method, |
||
| 323 | // this class is responsible for telling it when to repaint. If we |
||
| 324 | // don't mark it dirty, you won't see the hyperlink widget |
||
| 325 | // appear. So mark it dirty now: |
||
| 326 | mTitleScreen->MarkDirty(); |
||
| 327 | } |
||
| 328 | |||
| 329 | ////////////////////////////////////////////////////////////////////////// |
||
| 330 | ////////////////////////////////////////////////////////////////////////// |
||
| 331 | void GameApp::TitleScreenIsFinished() |
||
| 332 | { |
||
| 333 | // This function is called by the title screen when the user clicks |
||
| 334 | // on the hyperlink widget to continue. At this point, the title screen |
||
| 335 | // has already removed itself and its widgets and we should set up our |
||
| 336 | // Board class and begin the game. Let's also set our title screen |
||
| 337 | // pointer to NULL, since it will be safely deleted automatically at a |
||
| 338 | // later point, and we don't want to delete it twice. |
||
| 339 | mTitleScreen = NULL; |
||
| 340 | mBoard = new Board(this); |
||
| 341 | |||
| 342 | // Now that the title screen is done, we don't need its resources |
||
| 343 | // wasting memory. Let's delete all of its resources. We do that |
||
| 344 | // by calling DeleteResources and specifying the exact name of the |
||
| 345 | // resource group we want to free up: |
||
| 346 | mResourceManager->DeleteResources("TitleScreen"); |
||
| 347 | |||
| 348 | // This is a very important step: Because the Board class is a widget |
||
| 349 | // (see Board.h/.cpp for more details) we need to tell it what |
||
| 350 | // dimensions it has and where to place it. |
||
| 351 | // By default a widget is invisible because its |
||
| 352 | // width/height are 0, 0. Since the Board class is our main |
||
| 353 | // drawing area and game logic class, we want to make it the |
||
| 354 | // same size as the application. For this particular demo, that means |
||
| 355 | // 800x600. We will use mWidth and mHeight though, as those were |
||
| 356 | // already set to the proper resolution in GameApp::Init(). |
||
| 357 | mBoard->Resize(0, 0, mWidth, mHeight); |
||
| 358 | |||
| 359 | // Also an important step is to add the newly created Board widget to |
||
| 360 | // the widget manager so that it will automatically have its update, draw, |
||
| 361 | // and input processing methods called. |
||
| 362 | mWidgetManager->AddWidget(mBoard); |
||
| 363 | |||
| 364 | // Let's fade out the intro song and fade in the main game music. |
||
| 365 | // FadeOut works just like FadeIn did in Init() but with some |
||
| 366 | // slightly different parameters. The first, is like with FadeIn and |
||
| 367 | // PlayMusic, the channel or song id that you want to mess with. |
||
| 368 | // The second indicates that the song fading out should stop when |
||
| 369 | // done, if it is true. The final parameter indicates how fast |
||
| 370 | // to fade out, and is from 0 to 1. |
||
| 371 | mMusicInterface->FadeOut(0, true, 0.004); |
||
| 372 | |||
| 373 | // Let's fade in the main game music. This is the same as in Init. |
||
| 374 | // The only difference is we're using 1 instead of 0 for our song id. |
||
| 375 | // Why? Well, channel/song id 0 is being used to fade out the |
||
| 376 | // previously playing track, we can't use it to also fade in. |
||
| 377 | // That's why we loaded another copy of the song into channel 1. |
||
| 378 | // Again, as explained in Init, I happen to know that offset 9 |
||
| 379 | // is the start of the main game music. |
||
| 380 | mMusicInterface->FadeIn(1, 9, 0.002, false); |
||
| 381 | |||
| 382 | // We'll cover changing the music and sound volumes in a later demo. |
||
| 383 | } |
||
| 384 | |||
| 385 | ////////////////////////////////////////////////////////////////////////// |
||
| 386 | ////////////////////////////////////////////////////////////////////////// |
||
| 387 | void GameApp::HandleCmdLineParam(const std::string& theParamName, const std::string& theParamValue) |
||
| 388 | { |
||
| 389 | // If you wanted to, in here you could examine command line parameters and their values. |
||
| 390 | // We actually don't care to, in this. The purpose was to show you how you'd do it, |
||
| 391 | // and this function is the one you use to read those values. We'll just print the |
||
| 392 | // parameters out for now: |
||
| 393 | OutputDebugString(StrFormat("theParamName = \"%s\", theParamValue = \"%s\"", |
||
| 394 | theParamName.c_str(), theParamValue.c_str()).c_str()); |
||
| 395 | } |