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