CRM32Pro SDK  v5.22
Example01_BasicCode.cpp


/*--------------------------------------------
Examples - CRM32Pro SDK - Roberto Prieto
Copyright (C) 2001-2011 MegaStorm Systems
--------------------------------------------
Example 1: basic code
------------------------
- 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 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"
// ---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;
// 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);
}
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 Runtime information is logged into 'BasicCode.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;
}