]>
git.r.bdr.sh - rbdr/pico-engine/blob - main.cpp
3 /*Global variables for the game.*/
5 bool app
= true; //Structure to check app lifecycle
6 bool keys
[323] = {false}; //Structures for keyboard/mouse status.
7 bool btns
[3] = {false};
10 char *musicpath
; //NOTE: Kill this when we have an actual music system
13 Game_overwatch
*overwatch
; //Overwatch is the game controller
14 Map
*mainmap
; //NOTE: Must change
15 Audio
*audio
; //NOTE: Probably needs to change.
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?)
21 char str
[12]; //NOTE: String for debugging info. MUST change
23 /*Definition for our surfaces and such.*/
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.
36 /*Definition of our functions */
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)
50 int main(int argc
, char *argv
[])
54 //When on apple, sit on top of the bundle resources directory.
56 CFBundleRef mainBundle
= CFBundleGetMainBundle();
57 CFURLRef resourcesURL
= CFBundleCopyResourcesDirectoryURL(mainBundle
);
58 char path
[MAXPATHLEN
];
59 if (!CFURLGetFileSystemRepresentation(resourcesURL
, TRUE
, (UInt8
*)path
, MAXPATHLEN
))
63 CFRelease(resourcesURL
);
66 std::cout
<< "Current Path: " << path
<< std::endl
;
69 //Initialize the Lua VM and debugger..
71 CLuaDebugger
dbg (vm
);
73 std::cout
<< ">>>Initialized Lua VM\n";
76 std::cout
<< "Initializing... \n";
78 std::cout
<< "Done! \n";
79 std::cout
<< "Loading Objects... \n";
81 std::cout
<< "Done! \n";
83 //play the music (infinite loop)
84 Mix_PlayMusic(music
, -1);
86 //Main Application Loop
91 ticks1
= SDL_GetTicks();
93 //Check and update the inputs.
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
);
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();
110 //render the actual scene
113 //Measure time again and extend frame.
114 ticks2
= SDL_GetTicks();
115 if ((ticks2
-ticks1
) < FRAME_TIME
) SDL_Delay(FRAME_TIME
- (ticks2
-ticks1
));
117 //Temporary pause menu. This is better abstracted with a FSM.
118 ticks1
= SDL_GetTicks();
122 ticks2
= SDL_GetTicks();
126 //When the loop dies, free and deinitialize.
133 /* Initialization of components */
137 //Initialize subsystems: SDL, Fonts, Audio, Overwatch and Map.
138 SDL_Init(SDL_INIT_EVERYTHING
);
140 Mix_OpenAudio(44100, AUDIO_S16SYS
, 1, 2048);
141 overwatch
= new Game_overwatch
;
145 /* deinitialization of components */
154 /*Load our basic objects*/
158 viewport
= SDL_SetVideoMode( SCREEN_WIDTH
, //Define our viewport
162 std::cout
<< ">>Created Viewport... \n";
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.
171 /**********LOAD SPRITES******************************/
172 new Actor(220,220,"./sprites/picosprite.png", "./scripts/main_actor.lua", mainmap
, audio
, overwatch
, vm
);
174 pauseimage
= IMG_Load("./pauseimage.png");
175 bgimage
= IMG_Load("./picobg1.png");
176 /**********LOAD AUDIO STUFF***************************/
178 music
= Mix_LoadMUS("./bgm/newpicoambient.ogg"); //este es el audio de fondo
180 std::cout
<< ">>Loaded BGM.\n";
182 /**********LOAD FONT STUFF*****************************/
184 font
= TTF_OpenFont("./dejavubold.ttf", 18);
185 std::cout
<< ">>Loaded fonts.\n";
189 /*Automatic Updates*/
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
;}
205 //Special input for debugging. Should probably add a console.
209 //The rest of the input is handled by the lua scripts.
213 new Actor(mainmap
->get_sx()+mouse
[0],mainmap
->get_sy()+mouse
[1],"./sprites/walkersprite.png", "./scripts/npc_walker.lua", mainmap
, audio
, overwatch
, vm
);
220 new Actor(mainmap
->get_sx()+mouse
[0],mainmap
->get_sy()+mouse
[1],"./sprites/walkersprite.png", "./scripts/npc_jumper.lua", mainmap
, audio
, overwatch
, vm
);
225 if (dudelock
&& !keys
[SDLK_q
] && !keys
[SDLK_w
]) {
241 if(event
.type
== SDL_QUIT
) app
= false;
257 if(event
.type
== SDL_QUIT
) app
= false;
260 /*Free objects, save memory*/
263 SDL_FreeSurface(text
);
265 Mix_FreeMusic(music
);
268 /*Render the actual scene... Delegate to the Overwatch draw method when we're in-game*/
271 SDL_FillRect(viewport
,NULL
,0);
272 Gfx::drawsurface(bgimage
,0,0,viewport
);
273 mainmap
->drawmap(viewport
);
274 Gfx::drawsurface(backg
,0,0,viewport
);
276 overwatch
->draw(viewport
, mainmap
);
278 Gfx::drawsurface(text
,10,10,viewport
);
281 Gfx::drawsurface(pauseimage
,0,0,viewport
);