1 #include "SDL.h"
2 #include <stdlib.h>
3 #include "../engine/kyra.h"
4 
5 #include "tutorial1.h"
6 
7 using namespace grinliz;
8 
9 #define SDL_TIMER_EVENT ( SDL_USEREVENT + 0 )
10 const int TIMER_INTERVAL = 80;
11 
12 const int SCREENX = 640;
13 const int SCREENY = 480;
14 
15 
TimerCallback(Uint32 interval)16 Uint32 TimerCallback(Uint32 interval)
17 {
18 	SDL_Event event;
19 	event.type = SDL_TIMER_EVENT;
20 
21 	SDL_PeepEvents( &event, 1, SDL_ADDEVENT, 0 );
22 	return TIMER_INTERVAL;
23 }
24 
25 
main(int argc,char * argv[])26 int main( int argc, char *argv[] )
27 {
28 	// code_d A random number generator.
29 	Random random;
30 
31 	SDL_Surface* screen;
32 
33 	const SDL_version* sdlVersion = SDL_Linked_Version();
34 	if ( sdlVersion->minor < 2 )
35 	{
36 		printf( "SDL version must be at least 1.2.0" );
37 		exit( 254 );
38 	}
39 
40 	/* Initialize the SDL library */
41 	if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_NOPARACHUTE) < 0 ) {
42 		printf( "Couldn't initialize SDL: %s\n",SDL_GetError());
43 		exit(255);
44 	}
45 
46 	/* Create a display for the image */
47 	screen = SDL_SetVideoMode( SCREENX, SCREENY, 0, SDL_SWSURFACE );
48 	if ( screen == NULL ) {
49 		exit(3);
50 	}
51 
52 	KrEngine* engine = new KrEngine( screen );
53 	engine->Draw();
54 
55 	// Load the dat file.
56 	// The dat file was carefully created in the sprite
57 	// editor. Loading allows us access to the
58 	// MAGE, PARTICLE, and CARPET.
59 	if ( !engine->Vault()->LoadDatFile( "tutorial1.dat" ) )
60 	{
61 		printf( "Error loading the tutorial dat file\n" );
62 		exit( 255 );
63 	}
64 
65 	// Get the CARPET resource
66 	KrSpriteResource* carpetRes = engine->Vault()->GetSpriteResource( TUT1_CARPET );
67 	GLASSERT( carpetRes );
68 
69 	// Create the carpet sprite and add it to the tree
70 	KrSprite* carpet = new KrSprite( carpetRes );
71 	carpet->SetPos( SCREENX, SCREENY / 2 );
72 	engine->Tree()->AddNode( 0, carpet );
73 	// Get the MAGE resource, create the mage.
74 	KrSpriteResource* mageRes = engine->Vault()->GetSpriteResource( TUT1_MAGE );
75 	GLASSERT( mageRes );
76 	KrSprite* mage = new KrSprite( mageRes );
77 
78 	// Add the Mage as a child of the carpet.
79 	engine->Tree()->AddNode( carpet, mage );
80 	SDL_Event event;
81 	bool done = false;
82 
83 	// Start timing!
84 	SDL_SetTimer( TIMER_INTERVAL, TimerCallback );
85 
86 	while( !done && SDL_WaitEvent(&event) )
87 	{
88 		if ( event.type == SDL_QUIT )
89 			break;
90 
91 		switch(event.type)
92 		{
93 			case SDL_KEYDOWN:
94 			{
95 				done = true;
96 			}
97 			break;
98 
99 			case SDL_TIMER_EVENT:
100 			{
101 				// code_d Walk the list and move the particles
102 				// Move the carpet.
103 				carpet->DoStep();
104 				if ( carpet->X() < 0 )
105 				{
106 					carpet->SetPos( SCREENX, carpet->Y() );
107 				}
108 				engine->Draw();
109 			}
110 			break;
111 
112 			default:
113 				break;
114 		}
115 
116 	}
117 
118 	delete engine;
119 
120 	SDL_Quit();
121 	return 0;
122 }
123