Example03_GUI.cpp


/*--------------------------------------------
   Examples - CRM32Pro SDK - Roberto Prieto
   Copyright (C) 2001-2011 MegaStorm Systems
  --------------------------------------------

  Example 3: GUI interface
  ------------------------
  - 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 the right way ;)

  This example is totally free and can be used without 
any restriction but there is not any warranty for its usage.
Please, do not forget to include a copy of all the library licenses
located at /licenses directory if you distribute or share it.

Roberto Prieto
megastorm@ono.com
http://www.megastormsystems.com
*/

// ---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 *);
void PrintHelp();

// ---Global vars---
SDL_Surface    *sBg = NULL;          // Background surface
CRM32Pro_CFont *fFont;
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; 

   // -Print help on a window-
   PrintHelp();

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

      // 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 do some stuff here.
                                            // Is possible to call here to your Rendering Graphics after checking the user.code returned instead of using the CRM32Pro callback,
                                            // but it will have some extra overdraw.
                                            
                                            // As this is an example, we dont mind about overdraw
                                            if(event.user.code == EVENT_CODE_RENDER) 
                                            {
                                                       CRM32Pro.RenderNeeded(); // Request a next frame update
                                                       CRM32Pro.Blit(sBg,NULL,CRM32Pro.screen,NULL);
                                                       fFont->PutString(CRM32Pro.screen,330,350,sFont);                                                   
                                                       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;

    // Modify the progress bar
    int current = IButton->GetValue(*id);
    current = current + *op;
    IButton->SetValue(*id, current);
}

// --------------------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," Press [1] to set the CTYPE_CENTER cursor pointer.\n");
           strcat(sMsg," Press [2] to set the CTYPE_NORMAL cursor pointer.\n");
           strcat(sMsg,"\n Runtime information is logged into 'GUI.log' file.\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;
}
 All Classes Functions Variables