Example12_DRTS.cpp


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

  Example 12: integrated console and debug window
  -----------------------------------------------
  - 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
  - Using the integrated console and debug window
  - 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"

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

// ---Defines---
#define EXAMPLE_VERSION "Example 12: integrated console"
#define GFX_RESOURCE "data/gfx.dpf"     // DPF with graphic resources

// ---Our custom handler---
int cmdMy(vector<string> *tokens)
{
           int iRet = 0;

           if(tokens->size() == 1) 
           {
                      CRM32Pro.ConPrint(" usage: my <parameter> [value]");
                      CRM32Pro.ConPrint("  <version> - Print the application version");
                      CRM32Pro.ConPrint("  <quit> - Close the application");
                      iRet = 1;
           }
           // Version subcommand
           else if(!strcmp(tokens->at(1).c_str(),"version"))
           {
                      CRM32Pro.ConPrint("My version is 1.0");
                      iRet = 1;
           }
           // Quit subcommand
           else if(!strcmp(tokens->at(1).c_str(),"quit"))
           {
                      CRM32Pro.ConPrint("Exiting...(no logic for handling it :) )");
                      iRet = 1;
           }
           
           return iRet;
}

// ---Our render graphics callback---
void RenderGraphics(int);
void PrintHelp();

// -------------MAIN FUNCTION----------------
int main(int argc,char *argv[])
{
   Uint8       done = 0;
   SDL_Event   event;
   Uint32      cCursor;     // Cursor ID 
  
   // -Print help on a window-
   PrintHelp();

   // -Log system initialize-  
   ILogSystem.Init("DRTS.log",LOG_FILE | LOG_DRTS ,LOG_NORMAL,EXAMPLE_VERSION);
   
   // -CRM32Pro and SDL initialize- 
   if(CRM32Pro.Init(SDL_INIT_VIDEO | 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;
   }

   // -Console & debugwo init-   
   CRM32Pro.ConTitle(EXAMPLE_VERSION);
   CRM32Pro.ConEnable();
   CRM32Pro.ConAddCmdHandler("my","Example of custom commands",cmdMy);

   CRM32Pro.DwEnable();
   CRM32Pro.DwTitle("Mouse vars");
   CRM32Pro.DwAddWatch("mousex",&CRM32Pro.mouse_x);
   CRM32Pro.DwAddWatch("mousey",&CRM32Pro.mouse_y);

   // -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
   CRM32Pro.SetRenderCallback(RenderGraphics);
   
   // -Your code...-
   // Load resources
   sBg = IImage->Load(GFX_RESOURCE,"background2");
   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_QUIT:
                done = 1;
                break;
             case SDL_KEYDOWN:                                    
                                     if(event.key.keysym.sym == SDLK_ESCAPE) done = 1;             
                                            if(event.key.keysym.sym == SDLK_f) { CRM32Pro.ConGetAlpha(); }
                                     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.
                break;
                                  default:
               break;
          }
       }
   }

   // -Print useful information-
   ITimeSystem->Info();

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

// ---Our render graphics function---
void RenderGraphics(int bLogicUpdate)
{          
           // Check if we need to render everything
           if(!CRM32Pro.IsRenderNeeded()) return;

           CRM32Pro.Blit(sBg,NULL,CRM32Pro.screen,NULL);
}

// --------------------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 [`] to set visible/non-visible the console\n");
           strcat(sMsg,"\n Runtime information is logged into 'DRTS.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