1 /*--License:
2 	Kyra Sprite Engine
3 	Copyright Lee Thomason (Grinning Lizard Software) 2001-2005
4 	www.grinninglizard.com/kyra
5 	www.sourceforge.net/projects/kyra
6 
7 	Kyra is provided under the LGPL.
8 
9 	I kindly request you display a splash screen (provided in the HTML documentation)
10 	to promote Kyra and acknowledge the software and everyone who has contributed to it,
11 	but it is not required by the license.
12 
13 --- LGPL License --
14 
15     This library is free software; you can redistribute it and/or
16     modify it under the terms of the GNU Lesser General Public
17     License as published by the Free Software Foundation; either
18     version 2.1 of the License, or (at your option) any later version.
19 
20     This library is distributed in the hope that it will be useful,
21     but WITHOUT ANY WARRANTY; without even the implied warranty of
22     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23     Lesser General Public License for more details.
24 
25     You should have received a copy of the GNU Lesser General Public
26     License along with this library; if not, write to the Free Software
27     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 
29 	The full text of the license can be found in lgpl.txt
30 */
31 
32 
33 #include "SDL.h"
34 #include <stdlib.h>
35 #include "../engine/kyra.h"
36 
37 // code_b Include the generated header
38 #include "tutorial1.h"
39 
40 #define SDL_TIMER_EVENT ( SDL_USEREVENT + 0 )
41 
42 using namespace grinliz;
43 const int TIMER_INTERVAL = 80;
44 
45 const int SCREENX = 640;
46 const int SCREENY = 480;
47 
48 
TimerCallback(Uint32 interval)49 Uint32 TimerCallback(Uint32 interval)
50 {
51 	SDL_Event event;
52 	event.type = SDL_TIMER_EVENT;
53 
54 	SDL_PeepEvents( &event, 1, SDL_ADDEVENT, 0 );
55 	return TIMER_INTERVAL;
56 }
57 
58 
main(int argc,char * argv[])59 int main( int argc, char *argv[] )
60 {
61 	SDL_Surface* screen;
62 
63 	const SDL_version* sdlVersion = SDL_Linked_Version();
64 	if ( sdlVersion->minor < 2 )
65 	{
66 		printf( "SDL version must be at least 1.2.0" );
67 		exit( 254 );
68 	}
69 
70 	/* Initialize the SDL library */
71 	if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_NOPARACHUTE) < 0 ) {
72 		printf( "Couldn't initialize SDL: %s\n",SDL_GetError());
73 		exit(255);
74 	}
75 
76 	/* Create a display for the image */
77 	screen = SDL_SetVideoMode( SCREENX, SCREENY, 0, SDL_SWSURFACE );
78 	if ( screen == NULL ) {
79 		exit(3);
80 	}
81 
82 	KrEngine* engine = new KrEngine( screen );
83 	engine->Draw();
84 
85 	// code_b Load the dat file.
86 	// The dat file was carefully created in the sprite
87 	// editor. Loading allows us access to the
88 	// MAGE, PARTICLE, and CARPET.
89 	if ( !engine->Vault()->LoadDatFile( "tutorial1.dat" ) )
90 	{
91 		printf( "Error loading the tutorial dat file\n" );
92 		exit( 255 );
93 	}
94 
95 	// code_b Get the CARPET resource
96 	KrSpriteResource* carpetRes = engine->Vault()->GetSpriteResource( TUT1_CARPET );
97 	GLASSERT( carpetRes );
98 
99 	// code_b Create the carpet sprite and add it to the tree
100 	KrSprite* carpet = new KrSprite( carpetRes );
101 	carpet->SetPos( SCREENX, SCREENY / 2 );
102 	engine->Tree()->AddNode( 0, carpet );
103 
104 	// code_c Get the MAGE resource, create the mage.
105 	KrSpriteResource* mageRes = engine->Vault()->GetSpriteResource( TUT1_MAGE );
106 	GLASSERT( mageRes );
107 	KrSprite* mage = new KrSprite( mageRes );
108 
109 	// code_c Add the Mage as a child of the carpet.
110 	engine->Tree()->AddNode( carpet, mage );
111 
112 	SDL_Event event;
113 	bool done = false;
114 
115 	// Start timing!
116 	SDL_SetTimer( TIMER_INTERVAL, TimerCallback );
117 
118 	while( !done && SDL_WaitEvent(&event) )
119 	{
120 		if ( event.type == SDL_QUIT )
121 			break;
122 
123 		switch(event.type)
124 		{
125 			case SDL_KEYDOWN:
126 			{
127 				done = true;
128 			}
129 			break;
130 
131 			case SDL_TIMER_EVENT:
132 			{
133 				// code_b Move the carpet.
134 				carpet->DoStep();
135 				if ( carpet->X() < 0 )
136 				{
137 					carpet->SetPos( SCREENX, carpet->Y() );
138 				}
139 
140 				engine->Draw();
141 			}
142 			break;
143 
144 			default:
145 				break;
146 		}
147 
148 	}
149 
150 	delete engine;
151 
152 	SDL_Quit();
153 	return 0;
154 }
155 
156