home *** CD-ROM | disk | FTP | other *** search
/ NEXT Generation 27 / NEXT27.iso / pc / demos / emperor / dx3.exe / SDK / SAMPLES / IKLOWNS / CGACTION.CPP next >
C/C++ Source or Header  |  1996-08-28  |  9KB  |  353 lines

  1. /*===========================================================================*\
  2. |
  3. |  File:        cgaction.cpp
  4. |
  5. |  Description: 
  6. |       
  7. |-----------------------------------------------------------------------------
  8. |
  9. |  Copyright (C) 1995-1996 Microsoft Corporation.  All Rights Reserved.
  10. |
  11. |  Written by Moss Bay Engineering, Inc. under contract to Microsoft Corporation
  12. |
  13. \*===========================================================================*/
  14.  
  15. /**************************************************************************
  16.  
  17.     (C) Copyright 1995-1996 Microsoft Corp.  All rights reserved.
  18.  
  19.     You have a royalty-free right to use, modify, reproduce and 
  20.     distribute the Sample Files (and/or any modified version) in 
  21.     any way you find useful, provided that you agree that 
  22.     Microsoft has no warranty obligations or liability for any 
  23.     Sample Application Files which are modified. 
  24.  
  25.     we do not recomend you base your game on IKlowns, start with one of
  26.     the other simpler sample apps in the GDK
  27.  
  28.  **************************************************************************/
  29.  
  30.  
  31. #include <ctype.h>
  32. #include <windows.h>
  33. #ifdef __WATCOMC__
  34. #include <stdlib.h>
  35. #endif
  36. #include "cgglobl.h"
  37. #include "cgdebug.h"
  38. #include "cgsprite.h"
  39. #include "strrec.h"
  40. #include "cgaction.h"
  41. #include "cgimage.h"
  42.  
  43. /*---------------------------------------------------------------------------*\
  44. |
  45. |       Class CGameAction
  46. |
  47. |  DESCRIPTION:
  48. |       
  49. |
  50. |
  51. \*---------------------------------------------------------------------------*/
  52. CGameAction::CGameAction(
  53.     char* pFileName,
  54.     char* pActionName,
  55.     DWORD objID
  56.     ) : mpSequence( NULL )
  57. {
  58.     // get the sequence info
  59.     char sequenceBuf[256];
  60.     char fileBuf[256];
  61.     char defSequence[] = "";    // no default sequence names
  62.     int frameRate;
  63.  
  64.     GetPrivateProfileString(
  65.         pActionName,
  66.         "SequenceName",
  67.         defSequence,
  68.         sequenceBuf,
  69.         sizeof( sequenceBuf ),
  70.         pFileName
  71.         );
  72.  
  73.     GetPrivateProfileString(
  74.         pActionName,
  75.         "SequenceFile",
  76.         defSequence,
  77.         fileBuf,
  78.         sizeof( fileBuf ),
  79.         pFileName
  80.         );
  81.  
  82.     frameRate = GetPrivateProfileInt(
  83.         pActionName,
  84.         "Rate",
  85.         30,
  86.         pFileName
  87.         );
  88.  
  89.     // should we keep a mirror?
  90.     BOOL mirrorHorz = (int) GetPrivateProfileInt(
  91.                 pActionName,
  92.                 "MirrorHorizontal",
  93.                 FALSE,
  94.                 pFileName
  95.                 );
  96.  
  97.     BOOL mirrorVert = (int) GetPrivateProfileInt(
  98.                 pActionName,
  99.                 "MirrorVertical",
  100.                 FALSE,
  101.                 pFileName
  102.  
  103.                 );
  104.  
  105.     char dirBuf[256];
  106.     mpSequence = new CGameSpriteSequence( fileBuf, sequenceBuf, frameRate, mirrorHorz, mirrorVert );
  107.  
  108.     // See if there is a sound associated with this action
  109.     GetPrivateProfileString ( 
  110.         pActionName,
  111.         "Sound",
  112.         "",
  113.         dirBuf,
  114.         sizeof( dirBuf ),
  115.         pFileName
  116.     );
  117.  
  118.     if (dirBuf[0] != '\0')
  119.     {
  120.         CStringRecord fields( dirBuf, "," );
  121.         BOOL fLoop=FALSE;
  122.  
  123.         // See if the sound is to be looped when it is played
  124.         if ((fields.GetNumFields() > 2) && (toupper(*fields[2]) == 'L'))
  125.         {
  126.             fLoop = TRUE;
  127.         }
  128.  
  129.         // Create sound effect object based on WAV file
  130.         pSoundEffect = new CSoundEffect(fields[0], objID, fLoop, gSoundMode);
  131.  
  132.         // If a volume was specified and sound got created ok,
  133.         // set the defaul volume
  134.         if ((pSoundEffect != NULL) && (fields.GetNumFields() > 1))
  135.         {
  136.             defaultVolume = atoi(fields[1]);
  137.             pSoundEffect->SetVolume(defaultVolume);
  138.         }
  139.      } else {
  140.         pSoundEffect = NULL;
  141.      }
  142.     // See if there is an ending sound associated with this action
  143.     GetPrivateProfileString ( 
  144.         pActionName,
  145.         "SoundEnd",
  146.         "",
  147.         dirBuf,
  148.         sizeof( dirBuf ),
  149.         pFileName
  150.     );
  151.  
  152.     if (dirBuf[0] != '\0')
  153.     {
  154.         CStringRecord fields( dirBuf, "," );
  155.  
  156.         // Create sound effect object based on WAV file
  157.         pEndSoundEffect = new CSoundEffect(fields[0], objID, FALSE, gSoundMode);
  158.  
  159.         // If a volume was specified and sound got created ok,
  160.         // set the defaul volume
  161.         if ((pEndSoundEffect != NULL) && (fields.GetNumFields() > 1))
  162.         {
  163.             defaultVolume = atoi(fields[1]);
  164.             pEndSoundEffect->SetVolume(defaultVolume);
  165.         }
  166.      } else {
  167.         pEndSoundEffect = NULL;
  168.      }
  169. }
  170.  
  171. CGameAction::CGameAction(
  172.     char* pFileName, 
  173.     CGameCharSequenceInfo *pSequence,
  174.     DWORD objID
  175.     ) : mpSequence( NULL )
  176. {
  177.     // get the sequence info
  178.     char sequenceBuf[256];
  179.     char fileBuf[256];
  180.     char defSequence[] = "";    // no default sequence names
  181.     int frameRate;
  182.     lstrcpy(sequenceBuf, pSequence->SequenceName);
  183.  
  184.     lstrcpy(fileBuf, pSequence->SequenceFile);
  185.  
  186.     frameRate = pSequence->Rate;
  187.  
  188.     // should we keep a mirror?
  189.     BOOL mirrorHorz = FALSE;
  190.     BOOL mirrorVert = FALSE;
  191.  
  192.     mpSequence = new CGameSpriteSequence( fileBuf, sequenceBuf, frameRate, mirrorHorz, mirrorVert );
  193.  
  194.     if (pSequence->Sound.Sound != NULL)
  195.     {
  196.         BOOL fLoop=FALSE;
  197.  
  198.         fLoop = pSequence->Sound.Loop;
  199.  
  200.         // Create sound effect object based on WAV file
  201.         pSoundEffect = new CSoundEffect(pSequence->Sound.Sound
  202.         , objID, fLoop, gSoundMode);
  203.  
  204.         // If a volume was specified and sound got created ok,
  205.         // set the defaul volume
  206.         if ((pSoundEffect != NULL))
  207.         {
  208.             defaultVolume = pSequence->Sound.Rate; //atoi(fields[1]);
  209.             pSoundEffect->SetVolume(defaultVolume);
  210.         }
  211.      } else {
  212.         pSoundEffect = NULL;
  213.      }
  214.  
  215.     if (pSequence->Sound.SoundEnd != NULL)
  216.     {
  217.  
  218.         // Create sound effect object based on WAV file
  219.         pEndSoundEffect = new CSoundEffect(pSequence->Sound.SoundEnd
  220.         , objID, FALSE, gSoundMode);
  221.  
  222.         // If a volume was specified and sound got created ok,
  223.         // set the defaul volume
  224.         if (pEndSoundEffect != NULL)
  225.         {
  226.             defaultVolume = pSequence->Sound.Rate;
  227.             pEndSoundEffect->SetVolume(defaultVolume);
  228.         }
  229.      } else {
  230.         pEndSoundEffect = NULL;
  231.      }
  232. }
  233.  
  234.  
  235. CGameAction::~CGameAction()
  236. {
  237.     if (pSoundEffect != NULL)
  238.         delete pSoundEffect;
  239.     if (pEndSoundEffect != NULL)
  240.         delete pEndSoundEffect;
  241.     delete mpSequence;
  242. }
  243.  
  244. BOOL    // return TRUE if more sprites to go after this one
  245. CGameAction::Activate()
  246. {
  247.     if (pSoundEffect != NULL)
  248.         pSoundEffect->Play();
  249.     return(TRUE);
  250. }
  251.  
  252. BOOL    // return TRUE if more sprites to go after this one
  253. CGameAction::DeActivate()
  254. {
  255.     // reset the current sequence to 0
  256.     mpSequence->SetCurSprite( 0 );
  257.     if (pSoundEffect != NULL)
  258.         pSoundEffect->Stop();
  259.     if (pEndSoundEffect != NULL)
  260.         pEndSoundEffect->Play();
  261.     return TRUE;
  262. }
  263.  
  264. BOOL
  265. GetStereoValues(
  266.     int screenX,
  267.     int &vol,
  268.     int &pan
  269. )
  270. {
  271.     BOOL    fOffScreen = FALSE;
  272.  
  273.     if (vol == 0)
  274.         return FALSE;
  275.  
  276.     // Determine if object making sound is off screen to the
  277.     // left...
  278.     if (screenX < 0)
  279.     {
  280.         fOffScreen = TRUE;
  281.         pan = 0;    // place off left speaker
  282.     }
  283.  
  284.     // or to the right...
  285.     else if (screenX > SCREEN_WIDTH)
  286.     {
  287.         fOffScreen = TRUE;
  288.         pan = 127;      // place off right speaker
  289.  
  290.     // or somewhere on screen
  291.     } else {
  292.         pan = (screenX * 127) / SCREEN_WIDTH;
  293.     }
  294.  
  295.  
  296.     // If it is off screen, lower the volume accordingly
  297.     if (fOffScreen)
  298.     {
  299.         int howFarX;
  300.  
  301.         // Figure out how far to the left or right the object is
  302.         // off screen.
  303.         howFarX = (screenX > 0) ? screenX-SCREEN_WIDTH : -screenX;
  304.  
  305.         // Adjust the volume proportionally so that it is full
  306.         // volume when it reaches the screen and zero volume when
  307.         // it is a full screen widths away.
  308.         vol = vol - (howFarX * vol / SCREEN_WIDTH);
  309.  
  310.         if (vol < 0)
  311.             vol = 0;
  312.     }
  313.  
  314.     // return TRUE if a sound should be made
  315.     return (vol != 0);
  316. }
  317.  
  318.  
  319. BOOL    // return TRUE if more sprites to go after this one
  320. CGameAction::Update(int screenX)
  321. {
  322.     int PanVal;
  323.     int curVolume = defaultVolume;
  324.  
  325.     if (pSoundEffect != NULL)
  326.     {
  327.         // Place sound in accordance with objects position in the world!
  328.         if (GetStereoValues(screenX, curVolume, PanVal))
  329.         {
  330.             pSoundEffect->SetVolume(curVolume);
  331.             pSoundEffect->SetPan(PanVal);
  332.             pSoundEffect->SetMute(FALSE);       // make sure it is playing
  333.         } else {
  334.             pSoundEffect->SetMute(TRUE);        // make sure it is stopped
  335.         }
  336.     }
  337.  
  338.     return mpSequence->Frame();
  339. }
  340.  
  341. void
  342. CGameAction::Render(
  343.     CGameScreen* pScreen,
  344.     int x,
  345.     int y,
  346.     BOOL revX,
  347.     BOOL revY,
  348.     LPRECT pDirty
  349.     )
  350. {
  351.     mpSequence->Render( pScreen, x, y, revX, revY, pDirty );
  352. }   
  353.