]> git.r.bdr.sh - rbdr/pico-engine/blob - main.cpp
Update project so it compiles
[rbdr/pico-engine] / main.cpp
1 #include "stdafx.h"
2
3 /*Global variables for the game.*/
4
5 bool app = true; //Structure to check app lifecycle
6 bool keys[323] = {false}; //Structures for keyboard/mouse status.
7 bool btns[3] = {false};
8 int mouse[2] = {0,0};
9
10 char *musicpath; //NOTE: Kill this when we have an actual music system
11
12 //basic systems.
13 Game_overwatch *overwatch; //Overwatch is the game controller
14 Map *mainmap; //NOTE: Must change
15 Audio *audio; //NOTE: Probably needs to change.
16
17 SDL_Surface *pauseimage, *bgimage; //NOTE: This should probably change.
18 int ticks1, ticks2; //Timing for proper FPS
19 bool pauselock, gamepaused, dudelock; //Locking variables. Probably needs a better system for this. (semaphores?)
20
21 char str[12]; //NOTE: String for debugging info. MUST change
22
23 /*Definition for our surfaces and such.*/
24
25 SDL_Surface *viewport, *backg, *buddy, *text; //our surfaces: the viewport, the background, the actor and the text
26 SDL_Event event; //our event handler
27 SDL_Color white = {255,255,255,0}; //some colors
28 SDL_Color grey = {65,65,65,0};
29 SDL_Color magenta = {255,0,255,0};
30 SDL_Color black = {34, 34, 34, 0};
31 Mix_Music *music; //background music
32 TTF_Font *font; //font
33 CLuaVirtualMachine vm; //Lua VM.
34
35
36 /*Definition of our functions */
37
38 void init(); //Initialize Game objects
39 void loadobjects(); //Load Game Objects
40 void updateinput(); //Update Input
41 void handleinput(); //Act on input
42 void renderscene(); //Render Game Objects
43 void freeobjects(); //Free Game Objects
44 void deinit(); //Deinitialize
45 void pauseinput(); //Pause input handling (this will be better abstracted in a State machine)
46
47
48
49 /*Entry point*/
50 int main(int argc, char *argv[])
51 {
52
53
54 //When on apple, sit on top of the bundle resources directory.
55 #ifdef __APPLE__
56 CFBundleRef mainBundle = CFBundleGetMainBundle();
57 CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
58 char path[MAXPATHLEN];
59 if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, MAXPATHLEN))
60 {
61 // error!
62 }
63 CFRelease(resourcesURL);
64
65 chdir(path);
66 std::cout << "Current Path: " << path << std::endl;
67 #endif
68
69 //Initialize the Lua VM and debugger..
70 vm.InitialiseVM ();
71 CLuaDebugger dbg (vm);
72 dbg.SetCount (10);
73 std::cout << ">>>Initialized Lua VM\n";
74
75 //load stuff
76 std::cout << "Initializing... \n";
77 init();
78 std::cout << "Done! \n";
79 std::cout << "Loading Objects... \n";
80 loadobjects();
81 std::cout << "Done! \n";
82
83 //play the music (infinite loop)
84 Mix_PlayMusic(music, -1);
85
86 //Main Application Loop
87 while(app)
88 {
89 if(!gamepaused){
90 //Measure time
91 ticks1 = SDL_GetTicks();
92
93 //Check and update the inputs.
94 updateinput();
95 handleinput();
96
97 //Temporary debugging of population counts.
98 sprintf(str, "%d", overwatch->get_population());
99 SDL_FreeSurface(text);
100 text = TTF_RenderText_Blended(font, str, white);
101
102 //Make everyone act, animate, handle physics, and do collision work
103 overwatch->act(viewport, mainmap, keys);
104 overwatch->animate();
105 overwatch->handlephysics(mainmap, ticks1, ticks2);
106 overwatch->reset_collisions();
107 overwatch->check_collisions(mainmap);
108 overwatch->dieloop();
109
110 //render the actual scene
111 renderscene();
112
113 //Measure time again and extend frame.
114 ticks2 = SDL_GetTicks();
115 if ((ticks2-ticks1) < FRAME_TIME) SDL_Delay(FRAME_TIME - (ticks2-ticks1));
116 }else{
117 //Temporary pause menu. This is better abstracted with a FSM.
118 ticks1 = SDL_GetTicks();
119 renderscene();
120 updateinput();
121 pauseinput();
122 ticks2 = SDL_GetTicks();
123 }
124 }
125
126 //When the loop dies, free and deinitialize.
127 freeobjects();
128 deinit();
129
130 return 0;
131 }
132
133 /* Initialization of components */
134
135 void init()
136 {
137 //Initialize subsystems: SDL, Fonts, Audio, Overwatch and Map.
138 SDL_Init(SDL_INIT_EVERYTHING);
139 TTF_Init();
140 Mix_OpenAudio(44100, AUDIO_S16SYS, 1, 2048);
141 overwatch = new Game_overwatch;
142 mainmap = new Map;
143 }
144
145 /* deinitialization of components */
146
147 void deinit()
148 {
149 TTF_Quit();
150 Mix_CloseAudio();
151 SDL_Quit();
152 }
153
154 /*Load our basic objects*/
155
156 void loadobjects()
157 {
158 viewport = SDL_SetVideoMode( SCREEN_WIDTH, //Define our viewport
159 SCREEN_HEIGHT,
160 SCREEN_DEPTH,
161 SCREEN_PROPS );
162 std::cout << ">>Created Viewport... \n";
163
164
165 /*
166 *NOTE: Actor and audio loading should be handled by the map system.
167 * Fonts should be handled by the yet inexistant Debug and Message systems.
168 */
169
170
171 /**********LOAD SPRITES******************************/
172 new Actor(220,220,"./sprites/picosprite.png", "./scripts/main_actor.lua", mainmap, audio, overwatch, vm);
173
174 pauseimage = IMG_Load("./pauseimage.png");
175 bgimage = IMG_Load("./picobg1.png");
176 /**********LOAD AUDIO STUFF***************************/
177
178 music = Mix_LoadMUS("./bgm/newpicoambient.ogg"); //este es el audio de fondo
179 Mix_VolumeMusic(64);
180 std::cout << ">>Loaded BGM.\n";
181
182 /**********LOAD FONT STUFF*****************************/
183
184 font = TTF_OpenFont("./dejavubold.ttf", 18);
185 std::cout << ">>Loaded fonts.\n";
186
187 }
188
189 /*Automatic Updates*/
190 void updateinput()
191 {
192 while(SDL_PollEvent(&event)){
193 if (event.type == SDL_KEYDOWN) keys[event.key.keysym.sym] = true; //Set pressed keys to true, depressed to false
194 if (event.type == SDL_KEYUP) keys[event.key.keysym.sym] = false;
195 if (event.type == SDL_MOUSEBUTTONDOWN) btns[event.button.button] = true;
196 if (event.type == SDL_MOUSEBUTTONUP) btns[event.button.button] = false;
197 if (event.type == SDL_MOUSEMOTION){ mouse[0]= event.motion.x; mouse[1]= event.motion.y;}
198 }
199 }
200
201 /*Handle Input*/
202 void handleinput()
203 {
204
205 //Special input for debugging. Should probably add a console.
206 //w: adds a walker
207 //q: adds a jumper
208 //p: pauses the game
209 //The rest of the input is handled by the lua scripts.
210
211 if(keys[SDLK_w]){
212 if(!dudelock){
213 new Actor(mainmap->get_sx()+mouse[0],mainmap->get_sy()+mouse[1],"./sprites/walkersprite.png", "./scripts/npc_walker.lua", mainmap, audio, overwatch, vm);
214 dudelock = true;
215 }
216 }
217
218 if(keys[SDLK_q]){
219 if(!dudelock){
220 new Actor(mainmap->get_sx()+mouse[0],mainmap->get_sy()+mouse[1],"./sprites/walkersprite.png", "./scripts/npc_jumper.lua", mainmap, audio, overwatch, vm);
221 dudelock = true;
222 }
223 }
224
225 if (dudelock && !keys[SDLK_q] && !keys[SDLK_w]) {
226 dudelock = false;
227 }
228
229 if(keys[SDLK_p]){
230 if(!pauselock){
231 gamepaused = true;
232 pauselock = true;
233 Mix_VolumeMusic(10);
234 }
235 }else{
236 if(pauselock){
237 pauselock = false;
238 }
239 }
240
241 if(event.type == SDL_QUIT) app = false;
242 }
243
244 void pauseinput(){
245 if(keys[SDLK_p]){
246 if(!pauselock){
247 gamepaused = false;
248 pauselock = true;
249 Mix_VolumeMusic(64);
250 }
251 }else{
252 if(pauselock){
253 pauselock = false;
254 }
255 }
256
257 if(event.type == SDL_QUIT) app = false;
258 }
259
260 /*Free objects, save memory*/
261 void freeobjects()
262 {
263 SDL_FreeSurface(text);
264 TTF_CloseFont(font);
265 Mix_FreeMusic(music);
266 }
267
268 /*Render the actual scene... Delegate to the Overwatch draw method when we're in-game*/
269 void renderscene()
270 {
271 SDL_FillRect(viewport,NULL,0);
272 Gfx::drawsurface(bgimage,0,0,viewport);
273 mainmap->drawmap(viewport);
274 Gfx::drawsurface(backg,0,0,viewport);
275
276 overwatch->draw(viewport, mainmap);
277
278 Gfx::drawsurface(text,10,10,viewport);
279
280 if(gamepaused){
281 Gfx::drawsurface(pauseimage,0,0,viewport);
282 }
283 SDL_Flip(viewport);
284 }