Example03_GUI.cpp

logotipo.png


/*
  How to use CRM32Pro library. Example 3: GUI interface.
  -----------------------------------------------------
  
  >MegaStorm Systems (c) 2009
  >http://www.megastormsystems.com
  >Roberto Prieto
  
  - Initialize SDL and CRM32Pro.
  - Use of log system.
  - Initialize graphics: window icon, video mode and cursor.
  - Load buttons from DPF
  - Blit a surface, show interface with buttons and show
    the use of fonts and fades.
  - Exiting using right way ;)
  
 */

// ---Includes---
#include "CRM32Pro.h"      

// ---Defines---
#define EXAMPLE_VERSION "Example 3: GUI"
#define GFX_RESOURCE "data/gfx.dpf"     // DPF with graphic resources

// ---Prototypes---
void ModifyProgress(void *,void *);

// ---Global vars---
SDL_Surface    *sBg = NULL;          // Background surface
CRM32Pro_CFont *fFont;
Uint8           bRedrawAll = 1;      // Flag to draw all gfx
char            sFont[20];           // Text to render using font 
   
// -------------MAIN FUNCTION----------------
int main(int argc,char *argv[])
{
   
   SDL_Event      event;
   Uint32         cCursor;      // Cursor ID   
   CRM32Pro_CFont *fInput;      // Font object   
   const int      iAdd = 1, iSub = -1; // Constant used by the ModifyProgress() function
   Uint8          done = 0;

   // Buttons' ID
   Uint32  bExitID, bID, bCheckID, bSliderID, bInputID;      
   Uint32  bProgressID, bAddID, bSubtractID; 

   // -Log system initialize-  
   ILogSystem.Init("GUI.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;
   }

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

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

   // -Load resources-
   sBg = IImage->Load(GFX_RESOURCE,"background"); 
   IButton->SetLayer(0);
   bExitID = IButton->Load(GFX_RESOURCE,"MyComplex");
   bCheckID = IButton->Load(GFX_RESOURCE,"MyCheck");
   bSliderID = IButton->Load(GFX_RESOURCE,"MySlider");
   bInputID = IButton->Load(GFX_RESOURCE,"MyInput");
   bProgressID = IButton->Load(GFX_RESOURCE,"MyProgress");
   bAddID = IButton->Load(GFX_RESOURCE,"MyAdd");
   bSubtractID = IButton->Load(GFX_RESOURCE,"MySubtract");

   // -Setting a function for Add and Subtract buttons-
   IButton->SetFunction(bAddID,ModifyProgress,(void*)&bProgressID,(void*)&iAdd);
   IButton->SetFunction(bSubtractID,ModifyProgress,(void*)&bProgressID,(void*)&iSub);

   // -Setting the font for the inputbox-
   fFont = new CRM32Pro_CFont;
   fFont->Load(GFX_RESOURCE,"MyFont");
   fInput = new CRM32Pro_CFont;
   fInput->Load(GFX_RESOURCE,"InputFont");
   IButton->SetTextFont(bInputID,fInput);
   IButton->SetText(bInputID,"CRM32Pro Example");

   // -Main loop-  
   strcpy(sFont,"");
   
   while(!done)
   {    
          // 1.Logic frame stuff
          // Button check       
      bID = IButton->Check();

      if(bID == bExitID) done = 1;
      else if(bID == bSliderID)
      {
         if(IButton->GetValue(bSliderID) == 0)
         {
            strcpy(sFont,"0123456789");
         }
         else if(IButton->GetValue(bSliderID) == 1)
         {
            strcpy(sFont,"green font");
         }
         else
            strcpy(sFont,"ANOTHER FONT");
         // In this case, we need to redraw everything to restore the background of the previous text
                 bRedrawAll = 1;
          }

      // 2.Event loop: update all systems: our graphics using render callback function,events,timing...
      while(CRM32Pro.Update(&event))
      {
         switch(event.type)
         {              
            case SDL_QUIT:
               done = 1;
               break;
            case SDL_KEYDOWN:
               if(event.key.keysym.sym == SDLK_ESCAPE) done = 1;
               if(event.key.keysym.sym == SDLK_1)
               {
                  ICursor->ChangeType(cCursor,CTYPE_CENTER);
                  ILogSystem.Msg(LOG_NORMAL,"Cursor Center\n");
               }
               if(event.key.keysym.sym == SDLK_2)
               {
                  ICursor->ChangeType(cCursor,CTYPE_NORMAL);
                  ILogSystem.Msg(LOG_NORMAL,"Cursor normal\n");                
                           }
               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
                           // or if bRedrawAll is set
                           if((CRM32Pro.screen->flags & SDL_DOUBLEBUF) || (bRedrawAll))
               {
                                   SDL_BlitSurface(sBg,NULL,CRM32Pro.screen,NULL);
                                   fFont->PutString(CRM32Pro.screen,330,350,sFont);
                                   IButton->ForceDraw(); // As we have blitted the background, all buttons need to be immediately drawn.
                                   IButton->Draw();
                                   bRedrawAll = 0;
                           }
                           // Otherwise, we call the IButton->Draw() member. It will draw the buttons if needed
                           else IButton->Draw();
                           break;
                         default:
               break;
         }
      } 
   }
   // Timer information
   ITimeSystem->Info();

   // Fade to white
   if(IButton->GetState(bCheckID) == B_PRESS)
   {
      IScreenFX->FadeToColor(255,255,255,1000);
   }

   // Free resources and exit
   IButton->Info(1);
   fFont->Info();
   delete fFont;
   delete fInput;
   IButton->RemoveAll();
   CRM32Pro.FreeSurface(sBg);
   ICursor->Delete(cCursor);

   // -Exiting..-
   CRM32Pro.VideoInfo();
   CRM32Pro.Quit();
   return 0;
}

// Assigned function to two buttons: modify (add or subtract) the progress bar
void ModifyProgress(void *iParam1, void *iParam2)
{
        // Explicit cast to our right data type
        const int *id = (const int*)iParam1;   
        const int *op = (const int*)iParam2;

        // Static var to control a delay
        static int delay = 0;

        // Delay control
        if((delay + 250) < SDL_GetTicks())
        {
                delay = SDL_GetTicks();        
        // Modify the progress bar
            int current = IButton->GetValue(*id);
            current = current + *op;
            IButton->SetValue(*id, current);
        }
}


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