aboutsummaryrefslogtreecommitdiff
path: root/main.cpp
blob: 335db20d3658688ae4b95b2bf9446bbc9a7968ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "stdafx.h"

/*Global variables for the game.*/

bool             app =          true;               //Structure to check app lifecycle
bool             keys[323] =    {false};            //Structures for keyboard/mouse status.
bool             btns[3] =      {false};
int				 mouse[2] =		{0,0};

char			 *musicpath;                        //NOTE: Kill this when we have an actual music system

//basic systems.
Game_overwatch	 *overwatch;                        //Overwatch is the game controller
Map				 *mainmap;                          //NOTE: Must change
Audio			 *audio;                            //NOTE: Probably needs to change.

SDL_Surface		 *pauseimage, *bgimage;             //NOTE: This should probably change.
int              ticks1, ticks2;                    //Timing for proper FPS
bool			 pauselock, gamepaused, dudelock;   //Locking variables. Probably needs a better system for this. (semaphores?)

char str[12];                                       //NOTE: String for debugging info. MUST change

/*Definition for our surfaces and such.*/

SDL_Surface *viewport, *backg, *buddy, *text;		//our surfaces: the viewport, the background, the actor and the text
SDL_Event event;									//our event handler
SDL_Color white = {255,255,255,0};					//some colors
SDL_Color grey = {65,65,65,0};
SDL_Color magenta = {255,0,255,0};
SDL_Color black = {34, 34, 34, 0};
Mix_Music *music;									//background music
TTF_Font *font;										//font
CLuaVirtualMachine vm;                              //Lua VM.


/*Definition of our functions */

void init();                                        //Initialize Game objects
void loadobjects();                                 //Load Game Objects
void updateinput();                                 //Update Input
void handleinput();									//Act on input
void renderscene();                                 //Render Game Objects
void freeobjects();                                 //Free Game Objects
void deinit();                                      //Deinitialize
void pauseinput();                                  //Pause input handling (this will be better abstracted in a State machine)



/*Entry point*/
int main(int argc, char *argv[])
{

	
//When on apple, sit on top of the bundle resources directory.
#ifdef __APPLE__
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
    char path[MAXPATHLEN];
    if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, MAXPATHLEN))
    {
        // error!
    }
    CFRelease(resourcesURL);
	
    chdir(path);
    std::cout << "Current Path: " << path << std::endl;
#endif
	
    //Initialize the Lua VM and debugger..
	vm.InitialiseVM ();
	CLuaDebugger dbg (vm);
	dbg.SetCount (10);
	std::cout << ">>>Initialized Lua VM\n";
    
	//load stuff
    std::cout << "Initializing... \n";
	init();
	std::cout << "Done! \n";
	std::cout << "Loading Objects... \n";
    loadobjects();
    std::cout << "Done! \n";
    
	//play the music (infinite loop)
    Mix_PlayMusic(music, -1);
    
    //Main Application Loop
	while(app)
    {
		if(!gamepaused){
			//Measure time
			ticks1 = SDL_GetTicks();
			
			//Check and update the inputs.
			updateinput();
			handleinput();
				
			//Temporary debugging of population counts.
			sprintf(str, "%d", overwatch->get_population());
            SDL_FreeSurface(text);
			text = TTF_RenderText_Blended(font, str, white);
			
			//Make everyone act, animate, handle physics, and do collision work
			overwatch->act(viewport, mainmap, keys);
			overwatch->animate();
			overwatch->handlephysics(mainmap, ticks1, ticks2);
			overwatch->reset_collisions();
			overwatch->check_collisions(mainmap);
			overwatch->dieloop();
			
			//render the actual scene
			renderscene();
			
			//Measure time again and extend frame.
			ticks2 = SDL_GetTicks();
			if ((ticks2-ticks1) < FRAME_TIME) SDL_Delay(FRAME_TIME - (ticks2-ticks1));
		}else{
            //Temporary pause menu. This is better abstracted with a FSM.            
			ticks1 = SDL_GetTicks();
			renderscene();
			updateinput();
			pauseinput();
			ticks2 = SDL_GetTicks();
		}
    }
    
    //When the loop dies, free and deinitialize.
    freeobjects();
    deinit();
    
    return 0;
}

/* Initialization of components */

void init()
{
    //Initialize subsystems: SDL, Fonts, Audio, Overwatch and Map.
    SDL_Init(SDL_INIT_EVERYTHING);                                              
    TTF_Init();                                                                 
    Mix_OpenAudio(44100, AUDIO_S16SYS, 1, 2048);
	overwatch = new Game_overwatch;
	mainmap = new Map;
}

/* deinitialization of components */

void deinit()
{
    TTF_Quit();                                                             
    Mix_CloseAudio();                                                          
    SDL_Quit();                                                                 
}

/*Load our basic objects*/

void loadobjects()
{
    viewport = SDL_SetVideoMode( SCREEN_WIDTH,                                  //Define our viewport
								SCREEN_HEIGHT,
								SCREEN_DEPTH,
								SCREEN_PROPS );                                
	std::cout << ">>Created Viewport... \n";
    
    
	/*
     *NOTE: Actor and audio loading should be handled by the map system.
     *      Fonts should be handled by the yet inexistant Debug and Message systems.
     */
    
    
    /**********LOAD SPRITES******************************/
	new Actor(220,220,"./sprites/picosprite.png", "./scripts/main_actor.lua", mainmap, audio, overwatch, vm);
		
	pauseimage = IMG_Load("./pauseimage.png");
	bgimage = IMG_Load("./picobg1.png");
    /**********LOAD AUDIO STUFF***************************/
	
    music = Mix_LoadMUS("./bgm/newpicoambient.ogg");											 //este es el audio de fondo
	Mix_VolumeMusic(64);
	std::cout << ">>Loaded BGM.\n";
    
    /**********LOAD FONT STUFF*****************************/
	
    font = TTF_OpenFont("./dejavubold.ttf", 18);
	std::cout << ">>Loaded fonts.\n";
	
}

/*Automatic Updates*/
void updateinput()
{
    while(SDL_PollEvent(&event)){                                               
        if (event.type == SDL_KEYDOWN) keys[event.key.keysym.sym] = true;       //Set pressed keys to true, depressed to false
        if (event.type == SDL_KEYUP) keys[event.key.keysym.sym] = false;
        if (event.type == SDL_MOUSEBUTTONDOWN) btns[event.button.button] = true;
        if (event.type == SDL_MOUSEBUTTONUP) btns[event.button.button] = false;
		if (event.type == SDL_MOUSEMOTION){ mouse[0]= event.motion.x; mouse[1]= event.motion.y;}
    }
}

/*Handle Input*/
void handleinput()
{
    
    //Special input for debugging. Should probably add a console.
    //w: adds a walker
    //q: adds a jumper
    //p: pauses the game
    //The rest of the input is handled by the lua scripts.
	
	if(keys[SDLK_w]){
		if(!dudelock){
			new Actor(mainmap->get_sx()+mouse[0],mainmap->get_sy()+mouse[1],"./sprites/walkersprite.png", "./scripts/npc_walker.lua", mainmap, audio, overwatch, vm);
			dudelock = true;
		}
	}
	
	if(keys[SDLK_q]){
		if(!dudelock){
			new Actor(mainmap->get_sx()+mouse[0],mainmap->get_sy()+mouse[1],"./sprites/walkersprite.png", "./scripts/npc_jumper.lua", mainmap, audio, overwatch, vm);
			dudelock = true;
		}
	}
	
	if (dudelock && !keys[SDLK_q] && !keys[SDLK_w]) {
		dudelock = false;
	}
	
	if(keys[SDLK_p]){
		if(!pauselock){
			gamepaused = true;
			pauselock = true;
			Mix_VolumeMusic(10);
		}
    }else{
		if(pauselock){
			pauselock = false;
		}
    }
	
    if(event.type == SDL_QUIT) app = false;
}

void pauseinput(){
	if(keys[SDLK_p]){
		if(!pauselock){
			gamepaused = false;
			pauselock = true;
			Mix_VolumeMusic(64);
		}
    }else{
		if(pauselock){
			pauselock = false;
		}
    }
	
    if(event.type == SDL_QUIT) app = false;
}

/*Free objects, save memory*/
void freeobjects()
{
    SDL_FreeSurface(text);
    TTF_CloseFont(font);
    Mix_FreeMusic(music);
}

/*Render the actual scene... Delegate to the Overwatch draw method when we're in-game*/
void renderscene()
{	
	SDL_FillRect(viewport,NULL,0);
	Gfx::drawsurface(bgimage,0,0,viewport);
	mainmap->drawmap(viewport);
	Gfx::drawsurface(backg,0,0,viewport);
	
	overwatch->draw(viewport, mainmap);
	
	Gfx::drawsurface(text,10,10,viewport);
	
	if(gamepaused){
			Gfx::drawsurface(pauseimage,0,0,viewport);
	}
    SDL_Flip(viewport);
}