CRM32Pro SDK  v5.22
Example09_LogicRenderRates.cpp


/*--------------------------------------------
Examples - CRM32Pro SDK - Roberto Prieto
Copyright (C) 2001-2011 MegaStorm Systems
--------------------------------------------
Example 9: Logic and Render Rates
---------------------------------
- Initialize SDL and CRM32Pro.
- Printing help on a window.
- Use of log system.
- Initialize graphics: window icon, video mode and cursor.
- Load resources from DPF: images(IPF) and sprites.
- Using ITimeSystem to set a Rendering rate(unlimited) and a Logic Rate to 20.
- Using CRM32Pro.SetRenderCallback() to draw our objects at Rendering Rate.
- Clone sprites. Sprites movement using a smooth and normal way.
- Information panel with desired Logic Rate and current Render Rate.
- With F1 key you can enable or disable the smooth movement.
- With <- and -> cursors keys you can modify the desired Logic Rate.
- 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
contact@megastormsystems.com
http://www.megastormsystems.com
*/
// ---Includes---
#include "CRM32Pro.h"
// ---Defines---
#define EXAMPLE_VERSION "Example 9: Logic and Render Rates"
#define GFX_RESOURCE "data/gfx.dpf" // DPF with graphic resources
// ---Prototypes---
void RenderGraphics(int);
void RenderGraphicsGL(int);
void PrintHelp();
// ---Global vars---
SDL_Surface *bg, *info, *backinfo, *surfCursor;
CRM32Pro_CSprite *spr1, *spr2, *spr3, *spr4;
SDL_Rect inf;
Uint32 lastinfo = 0;
int bSmooth = -1;
// -------------MAIN FUNCTION----------------
int main(int argc,char *argv[])
{
Uint8 done = 0;
SDL_Event event;
int tmp = 0;
int iPos1, iDir1, iPos2, iDir2, iPos3, iDir3, iPos4, iDir4;
// -Print help on a window-
PrintHelp();
// -Log system initialize-
ILogSystem.Init("LogicRenderRates.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;
}
// -Time system initialize-
ITimeSystem->Init();
ITimeSystem->SetRate(0,20); // Desired Rendering and Logic Frame Rate
// -Set our render graphics function to fullfil desired Rendering Frame Rate-
if(!CRM32Pro.IsGL()) CRM32Pro.SetRenderCallback(RenderGraphics);
else CRM32Pro.SetRenderCallback(RenderGraphicsGL);
// -Load resources: background, info panel, info font, sprites
bg = IImage->Load(GFX_RESOURCE,"background2");
info = IImage->Load(GFX_RESOURCE,"info2");
inf.x = 6;inf.y = 6;inf.w = info->w;inf.h = info->h;
backinfo = IImage->Load(GFX_RESOURCE,"info2");
fInfo = new CRM32Pro_CFont();
fInfo->Load(GFX_RESOURCE,"InfoFont");
spr1 = new CRM32Pro_CSprite();
spr1->Load(GFX_RESOURCE,"rabbit");
spr1->AutoRestore(0);
spr1->Info();
// as all our sprites have the same picture, we clone them!
spr2 = new CRM32Pro_CSprite();
spr2->Clone(spr1);
spr3 = new CRM32Pro_CSprite();
spr3->Clone(spr1);
spr4 = new CRM32Pro_CSprite();
spr4->Clone(spr1);
// -Original positions and directions-
iPos1 = 0;
iDir1 = 1;
spr2->GetOffset(&tmp,NULL);
iPos2 = CRM32Pro.screen->w - tmp;
iDir2 = -1;
spr3->GetOffset(NULL,&tmp);
iPos3 = CRM32Pro.screen->h - tmp;
iDir3 = -1;
iPos4 = 1;
iDir4 = 1;
// -Main loop-
done = 1;
int test =0;
while(done)
{
// 1.Logic frame stuff
// Move the sprites across the screen limits
// 1.1.Sprite 1 stuff: movement of 1 pixel each logic frame
iPos1 = iPos1 + (iDir1);
spr1->SetPosition(CRM32Pro.screen->w / 2, iPos1, bSmooth);
if((iPos1 < 1) || (iPos1 > CRM32Pro.screen->h)) iDir1 = iDir1 * (-1);
// 1.2.Sprite 2 stuff: movement of 2 pixels each logic frame
iPos2 = iPos2 + (2 * iDir2);
spr2->SetPosition(iPos2,CRM32Pro.screen->h / 2, bSmooth);
if((iPos2 < 1) || (iPos2 > CRM32Pro.screen->w)) iDir2 = iDir2 * (-1);
// 1.3.Sprite 3 stuff: movement of 3 pixels each logic frame
iPos3 = iPos3 + (3 * iDir3);
spr3->SetPosition(CRM32Pro.screen->w / 3, iPos3, bSmooth);
if((iPos3 < 1) || (iPos3 > CRM32Pro.screen->h)) iDir3 = iDir3 * (-1);
// 1.4.Sprite 4 stuff: movement of 4 pixels each logic frame
iPos4 = iPos4 + (4 * iDir4);
spr4->SetPosition(iPos4,CRM32Pro.screen->h / 3, bSmooth);
if((iPos4 < 1) || (iPos4 > CRM32Pro.screen->w)) iDir4 = iDir4 * (-1);
// 2.Event loop: update all systems: our graphics using render callback function,events,timing...
while(CRM32Pro.Update(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
// Exit
if(event.key.keysym.sym == SDLK_ESCAPE) done = 0;
// Enable or disable smooth movement
if(event.key.keysym.sym == SDLK_F1)
{
bSmooth = bSmooth * (-1);
// In order to get the smooth effect we need to update all the gfx on each iteration
if(bSmooth == 1) CRM32Pro.RenderNeeded(UPDATEFRAME_ALWAYS);
}
// Inc or dec the Logic Rate
if(event.key.keysym.sym == SDLK_RIGHT) ITimeSystem->SetRate(ITimeSystem->GetDesiredRFR(),ITimeSystem->GetDesiredLFR()+1);
if(event.key.keysym.sym == SDLK_LEFT) ITimeSystem->SetRate(ITimeSystem->GetDesiredRFR(),ITimeSystem->GetDesiredLFR()-1);
break;
case SDL_QUIT:
done = 1;
break;
// 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;
}
}
}
// -Free resources, show information and exit-
delete fInfo;
delete spr4;
delete spr3;
delete spr2;
delete spr1;
CRM32Pro.FreeSurface(bg);
CRM32Pro.FreeSurface(info);
CRM32Pro.FreeSurface(backinfo);
ITimeSystem->Info();
CRM32Pro.VideoInfo();
CRM32Pro.Quit();
return 0;
}
// ---Our render graphics function---
// We render our graphics if a logic frame occurred(bLogicUpdate = 1) or if CRM32Pro.IsRenderNeeded() return 1
void RenderGraphics(int bLogicUpdate)
{
char sTmp[16];
// Check if we need to render everything
if(!CRM32Pro.IsRenderNeeded() && (!bLogicUpdate)) return;
// -Blit background-
SDL_BlitSurface(bg,NULL,CRM32Pro.screen,NULL);
// Sprites blitting
spr4->Draw();
spr3->Draw();
spr2->Draw();
spr1->Draw();
// Blit information panel: updated twice per second
SDL_BlitSurface(info,NULL,CRM32Pro.screen,&inf);
if((lastinfo + 500) < ITimeSystem->GetTime())
{
lastinfo = ITimeSystem->GetTime();
// Restore original info panel
SDL_BlitSurface(backinfo,NULL,info,NULL);
// Userīs desired logic rate
sprintf(sTmp,"%d",(Uint32)ITimeSystem->GetDesiredLFR());
fInfo->PutString(info,80,6,sTmp);
// Frames per second (real time)
sprintf(sTmp,"%d",(Uint32)ITimeSystem->GetCurrentRFR());
fInfo->PutString(info,80,23,sTmp);
// Using smooth way or not?
if(bSmooth == 1) fInfo->PutString(info,65,40,"ON");
else fInfo->PutString(info,65,40,"OFF");
}
}
// ---Our render graphics function for OpenGL mode---
// In OpenGL modes, blitting from a surface to another surface requires some conversions
// so the best workaround is just blit everything to CRM32Pro.screen surface.
// CRM32Pro.IsRenderNeeded() knows we are on an OpenGL mode so it will force to render everything
void RenderGraphicsGL(int bLogicUpdate)
{
char sTmp[16];
// Check if we need to render everything
if(!CRM32Pro.IsRenderNeeded() && (!bLogicUpdate)) return;
// -Blit background-
SDL_BlitSurface(bg,NULL,CRM32Pro.screen,NULL);
// Sprites blitting
spr4->Draw();
spr3->Draw();
spr2->Draw();
spr1->Draw();
// Blit information panel each frame update
lastinfo = ITimeSystem->GetTime();
// Restore original info panel
SDL_BlitSurface(backinfo,NULL,CRM32Pro.screen,NULL);
// Userīs desired logic rate
sprintf(sTmp,"%d",(Uint32)ITimeSystem->GetDesiredLFR());
fInfo->PutString(CRM32Pro.screen,80,6,sTmp);
// Frames per second (real time)
sprintf(sTmp,"%d",(Uint32)ITimeSystem->GetCurrentRFR());
fInfo->PutString(CRM32Pro.screen,80,23,sTmp);
// Using smooth way or not?
if(bSmooth == 1) fInfo->PutString(CRM32Pro.screen,65,40,"ON");
else fInfo->PutString(CRM32Pro.screen,65,40,"OFF");
}
// --------------------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 [F1] key to enable/disable smooth animation.\n");
strcat(sMsg," Press [RIGHT] key to increase the Logic Frame Rate.\n");
strcat(sMsg," Press [LEFT] key to decrease the Logic Frame Rate.\n");
strcat(sMsg,"\n Runtime information is logged into 'LogicRenderRates.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;
}