Example01_BasicCode.cpp

logotipo.png


/*
  How to use CRM32Pro SDK. Example 1: basic code.
  -----------------------------------------------
  
  >MegaStorm Systems (c) 2009
  >http://www.megastormsystems.com
  >Roberto Prieto
  
  - Initialize SDL and CRM32Pro.
  - Printing help on a window.
  - Use of log system.
  - Initialize graphics: window icon, video mode and cursor.
  - Initialize sound system.
  - Use of time system.
  - Blit a surface, play a sound with mouse click and exit with ESC key
  - Exiting using right way ;)   
 */

// ---Includes---
#include "CRM32Pro.h"
#ifdef _WINDOWS
 #include <windows.h>
#endif

// ---Prototypes---
void PrintHelp();

// ---Global vars---
SDL_Surface*sBg;         // Background surface

// ---Defines---
#define EXAMPLE_VERSION "Example 1: basic code"
#define GFX_RESOURCE "data/gfx.dpf"     // DPF with graphic resources
#define AUDIO_RESOURCE "data/audio.dpf" // DPF with sound resources

// -------------MAIN FUNCTION----------------
int main(int argc,char *argv[])
{
   Uint8       done = 0;
   SDL_Event   event;
   Uint32      cCursor;     // Cursor ID 
   int         hndClick;    // Click sound

   // -Print help on a window-
   PrintHelp();
   
   // -Log system initialize-  
   ILogSystem.Init("BasicCode.log",LOG_FILE,LOG_NORMAL,EXAMPLE_VERSION);
   
   // -CRM32Pro and SDL initialize- 
   if(CRM32Pro.Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) < 0)
   {
      ILogSystem.Msg(LOG_NORMAL," · [LOG] - Couldnīt initialize CRM32Pro: %s\n",SDL_GetError());
      return 1;
   }

   // -Desired config-
   CRM32Pro.Config.Title = EXAMPLE_VERSION;
   CRM32Pro.Config.Icon = IImage->Load(GFX_RESOURCE,"icono");
   CRM32Pro.Config.bMTFriendly = 1;   
   //CRM32Pro.Config.VideoRenderer = RENDER_OPENGL;
   //CRM32Pro.Config.VideoAccel = ACCEL_HARDSMOOTH;

   // -Graphics system initialize-  
   if(!CRM32Pro.SetVideoMode())
   {
      CRM32Pro.Quit();
      return 1;
   }

   ICursor->Init();
   cCursor = ICursor->Load(GFX_RESOURCE,"cursor");
   ICursor->Select(cCursor);

   // -Sound system initialize-  
   if(!ISoundFX->Init(44100, AUDIO_S16, 2, 4096))
   {
      ILogSystem.Msg(LOG_NORMAL," · [LOG Warning] - Couldnīt init sound system: %s\n",SDL_GetError());  
   }

   // -Time system initialize-
   ITimeSystem->Init();
   ITimeSystem->SetRate(0,20);  // Optional: desired Rendering and Logic Frame Rate

   // -Your code...-
   // Load resources
   hndClick = ISoundFX->SoundLoad(AUDIO_RESOURCE,"click");
   sBg = IImage->Load(GFX_RESOURCE,"background");
   SDL_BlitSurface(sBg,NULL,CRM32Pro.screen,NULL);

   // -Main loop-
   done = 0;
   while(!done)
   {   
           // 1.Logic frame stuff
           // ...

       // Event loop: update all systems: our graphics using render callback function,events,timing...
       while(CRM32Pro.Update(&event))
       {
          switch(event.type)
          {
             case SDL_MOUSEBUTTONDOWN:
                ISoundFX->SoundPlay(1,hndClick,0);
                break;
             case SDL_QUIT:
                done = 1;
                break;
             case SDL_KEYDOWN:
                            if(event.key.keysym.sym == SDLK_ESCAPE) done = 1;                   
                            break;
            case EVENT_LOGICWAIT:
                // While CRM32Pro.Update() wait for fulfill the logic rate this event is raised 
                            // You could call to the Rendering Graphics here instead of to use the CRM32Pro callback.
                           
                                // With doublebuffer screen we must blit the gfx every frame to avoid flickering
                                if(CRM32Pro.screen->flags & SDL_DOUBLEBUF) SDL_BlitSurface(sBg,NULL,CRM32Pro.screen,NULL);
                break;
                         default:
               break;
          }
       }
   }

   // -Print useful information-
   ITimeSystem->Info();
   ICursor->Info();
   CRM32Pro.VideoInfo();
   CRM32Pro.AudioInfo();

   // -Free resources and exit-
   ISoundFX->SoundFree(hndClick);
   CRM32Pro.FreeSurface(sBg);
   ICursor->Delete(cCursor);
   CRM32Pro.Quit();
   return 0;
}

           
// --------------------Help STUFF------------------------
// -Print help-
void PrintHelp()
{
        char *sMsg;

        // Create the message
        sMsg = new char[1024];
    strcpy(sMsg,"-----------------------------------------------------------\n");
        strcat(sMsg,"                     ");
        strcat(sMsg,EXAMPLE_VERSION);
        strcat(sMsg,"\n-----------------------------------------------------------\n\n");
        strcat(sMsg," Press [ESCAPE] key to exit.\n");
        strcat(sMsg,"\n To see further information, open 'BasicCode.log'.\n");
        strcat(sMsg,"\n");

        // Display the message
    #ifdef _WINDOWS
          MessageBox(NULL,sMsg,"CRM32Pro SDK - HelpScreen",MB_OK);
    #endif 
    #ifdef _LINUX 
          printf("CRM32Pro SDK - HelpScreen\n%s",sMsg);
    #endif

        // Free the message
        delete sMsg;
}

Generated on Mon Dec 21 17:04:39 2009 for CRM32Pro SDK v5.00 by  doxygen 1.6.1