Skip to content
Browse files

Remove vblank mode from run now that it is done programmatically ; co…

…ntinue work on lua bindings
  • Loading branch information...
1 parent a6976d8 commit 4c5a537e140960b831f33d042ff76ffa80955435 @ludamad committed Dec 17, 2012
View
107 res/main.lua
@@ -24,80 +24,69 @@ dofile "res/tests/progressions.lua"
local paused = false
--- lanarts main loop
--- this controls the games behaviour every step!
-function main_loop()
- local single_player = (#world.players() == 1)
-
- perf_timer_begin("**Game Frame**")
-
- local total_timer = timer()
-
- if key_pressed(keys.F2) then
- if single_player then gamestate.resources_reload() end
- end
-
- if key_pressed(keys.F3) then
- if single_player then gamestate.level_regenerate() end
- end
-
- if key_pressed(keys.F4) then
- paused = not paused
- end
-
- net.sync_message_consume()
-
- if key_pressed(keys.F6) then
- if single_player then gamestate.level_regenerate() end
- end
-
- perf_timer_end("**Game Frame**")
-end
-
-
-local timer = timer_create()
-local paused = false
-
-function main_loop_simple()
- local single_player = (settings.connection_type == net.NONE)
-
- perf_timer_begin("**Game Frame**")
-
- if key_pressed(keys.F2) and single_player then
- game.resources_load()
- end
-
- if key_pressed(keys.F3) and single_player then
- level.regenerate()
- end
-
- if key_pressed(keys.F4) then
- paused = not paused
- end
+local function mainloop(steponly)
+ perf.timing_begin("**Game Frame**")
net.sync_message_consume()
- if key_pressed(keys.F6) and single_player then
- game.load("savefile.save")
- game.input_capture(true) -- capture new input
- end
+ local timer = timer_create()
- timer:start()
+ if not steponly then
+ perf.timing_begin("**Draw**")
+ game.draw()
+ perf.timing_end("**Draw**")
+ end
- game.draw()
+ perf.timing_begin("**Step**")
game.step()
+ perf.timing_end("**Step**")
if not game.input_handle() then
return false
end
- local surplus = settings.time_per_step - timer:get_microseconds() / 1000
+ local surplus = settings.time_per_step - timer:get_milliseconds()
if surplus > 0 then
game.wait(surplus)
end
- perf_timer_end("**Game Frame**")
-
+ perf.timing_end("**Game Frame**")
+
return true;
end
+
+function main()
+
+ game.input_capture()
+
+ while true do
+ local single_player = (settings.connection_type == net.NONE)
+
+ if key_pressed(keys.F2) and single_player then
+ game.resources_load()
+ end
+
+ if key_pressed(keys.F3) and single_player then
+ level.regenerate()
+ end
+
+ if key_pressed(keys.F4) then
+ paused = not paused
+ end
+
+ if key_pressed(keys.F6) and single_player then
+ game.load("savefile.save")
+ game.input_capture(true) -- capture new input
+ end
+
+ if not mainloop(false) then
+ break
+ end
+ end
+
+ perf.timing_print()
+
+ print( "Step time: " .. string.format("%f", perf.get_timing("**Step**")) )
+ print( "Draw time: " .. string.format("%f", perf.get_timing("**Draw**")) )
+end
View
2 run.sh
@@ -6,4 +6,4 @@ fi
# Run lanarts
cd ../lanarts
-vblank_mode=0 ../lanarts_build/src/lanarts/lanarts
+../lanarts_build/src/lanarts/lanarts
View
1 src/common/include/lcommon/PerfTimer.h
@@ -26,6 +26,7 @@ class PerfTimer {
// Note: Does not work with recursive methods
void begin(const char* method);
void end(const char* method);
+ double average_time(const char* method);
void print_results();
void clear();
private:
View
1 src/common/include/lcommon/perf_timer.h
@@ -20,6 +20,7 @@
void perf_timer_begin(const char* funcname);
void perf_timer_end(const char* funcname);
+double perf_timer_average_time(const char* funcname);
void perf_timer_clear();
void perf_print_results();
View
10 src/common/src/PerfTimer.cpp
@@ -36,6 +36,12 @@ void PerfTimer::print_results() {
printf("**** END PERFORMANCE STATS ****\n");
}
+double PerfTimer::average_time(const char* method) {
+ MethodPerfProfile& mpp = perf_map[method];
+ float total = mpp.total_microseconds / 1000.0f;
+ return total / mpp.total_calls;
+}
+
void PerfTimer::clear() {
perf_map.clear();
}
@@ -55,6 +61,10 @@ void perf_timer_end(const char* funcname) {
__global_timer.end(funcname);
}
+double perf_timer_average_time(const char* funcname) {
+ return __global_timer.average_time(funcname);
+}
+
void perf_print_results() {
__global_timer.print_results();
}
View
3 src/common/src/lua/lua_lcommon.cpp
@@ -28,7 +28,8 @@ void lua_safe_dostring(lua_State* L, const char* code) {
void lua_safe_dofile(lua_State* L, const char* fname) {
if (luaL_loadfile(L, fname) != 0) {
- printf("Fatal error when loading lua file %s\n", fname);
+ printf("Fatal error when loading lua file %s:\n%s\n", fname,
+ lua_tostring(L, -1));
exit(0);
}
if (lua_pcall(L, 0, 0, 0)) {
View
17 src/common/src/lua/lua_timer.cpp
@@ -19,12 +19,18 @@ static unsigned long get_microseconds(Timer* timer) {
return timer->get_microseconds();
}
+static double get_milliseconds(Timer* timer) {
+ return timer->get_microseconds() / 1000.0;
+}
+
+
LuaValue lua_timermetatable(lua_State* L) {
LuaValue meta = luameta_new(L, "Timer");
LuaValue methods = luameta_methods(meta);
methods["start"].bind_function(start);
methods["get_microseconds"].bind_function(get_microseconds);
+ methods["get_milliseconds"].bind_function(get_milliseconds);
luameta_gc<Timer>(meta);
@@ -40,9 +46,12 @@ void lua_register_timer(lua_State *L, const LuaValue& module) {
module["timer_create"].bind_function(newtimer);
- module["perf_timer_begin"].bind_function(perf_timer_begin);
- module["perf_timer_end"].bind_function(perf_timer_end);
- module["perf_timer_clear"].bind_function(perf_timer_clear);
- module["perf_print_results"].bind_function(perf_print_results);
+ LuaValue perf_timer = module["perf"].ensure_table();
+
+ perf_timer["timing_begin"].bind_function(perf_timer_begin);
+ perf_timer["timing_end"].bind_function(perf_timer_end);
+ perf_timer["timing_clear"].bind_function(perf_timer_clear);
+ perf_timer["get_timing"].bind_function(perf_timer_average_time);
+ perf_timer["timing_print"].bind_function(perf_print_results);
}
View
185 src/lanarts/src/main.cpp
@@ -82,98 +82,95 @@ static void game_loop(GameState* gs) {
Timer total_timer, step_timer, draw_timer;
- gs->pre_step();
- for (int i = 1; cont; i++) {
- lua_State* L = gs->luastate();
- luawrap::globals(L)["main_loop_simple"].push();
- if (!luawrap::call<bool>(L)) {
- break;
- }
-
- continue;
- perf_timer_begin("**Game Frame**");
- total_timer.start();
-
- if (gs->key_press_state(SDLK_F2)) {
- if (gs->player_data().all_players().size() == 1) {
- init_lua_data(gs, gs->luastate());
- init_game_data(settings, gs->luastate());
- }
- }
- if (gs->key_press_state(SDLK_F3)) {
- if (gs->player_data().all_players().size() == 1) {
- gs->game_world().regen_level(gs->get_level()->id());
- }
- }
- if (gs->key_press_state(SDLK_F4)) {
- paused = !paused;
- }
- gs->net_connection().consume_sync_messages(gs);
- if (gs->key_press_state(SDLK_F6)) {
- if (gs->player_data().all_players().size() == 1) {
- load_game_from_file(gs, "savefile.save");
- gs->update_iostate(true);
- }
- }
-
-//Draw event
- draw_timer.start();
- bool draw_this_step = (i > 1 && i % settings.steps_per_draw == 0);
- if (draw_this_step) {
- gs->draw();
- }
-
- draw_events++;
- draw_time += draw_timer.get_microseconds();
-
- int repeat_amount = 1;
- if (gs->key_down_state(SDLK_F1))
- repeat_amount = 4;
-
- for (int repeat = 0; repeat < repeat_amount; repeat++) {
- //Step event
-
- step_timer.start();
- if (!paused) {
- gs->step();
- }
- step_events++;
- step_time += step_timer.get_microseconds();
-
- // Do not move this. It is done after the step event,
- // but before actions are queued for the next turn
- if (gs->key_press_state(SDLK_F5)) {
- net_send_state_and_sync(gs->net_connection(), gs);
- save_game_to_file(gs, "savefile.save");
- }
-
- // The following will queue actions for the next step
- bool is_done = (!paused && !gs->pre_step())
- || (paused && !gs->update_iostate());
- if (is_done) {
- cont = false;
- break;
- }
- }
-
- accumulated_time += total_timer.get_microseconds();
-
- long microwait = settings.time_per_step * 1000 * settings.steps_per_draw
- - accumulated_time;
- if (draw_this_step) {
- long delayms = microwait / 1000;
- if (delayms > 0) {
- SDL_Delay(delayms);
- }
- accumulated_time = 0;
- }
- perf_timer_end("**Game Frame**");
- }
-
- perf_print_results();
-
- printf("Step time: %f\n", float(step_time) / step_events / 1000);
- printf("Draw time: %f\n", float(draw_time) / draw_events / 1000);
+ lua_State* L = gs->luastate();
+ luawrap::globals(L)["main"].push();
+ luawrap::call<void>(L);
+ fflush(stdout);
+// gs->pre_step();
+// for (int i = 1; cont; i++) {
+// perf_timer_begin("**Game Frame**");
+// total_timer.start();
+//
+// if (gs->key_press_state(SDLK_F2)) {
+// if (gs->player_data().all_players().size() == 1) {
+// init_lua_data(gs, gs->luastate());
+// init_game_data(settings, gs->luastate());
+// }
+// }
+// if (gs->key_press_state(SDLK_F3)) {
+// if (gs->player_data().all_players().size() == 1) {
+// gs->game_world().regen_level(gs->get_level()->id());
+// }
+// }
+// if (gs->key_press_state(SDLK_F4)) {
+// paused = !paused;
+// }
+// gs->net_connection().consume_sync_messages(gs);
+// if (gs->key_press_state(SDLK_F6)) {
+// if (gs->player_data().all_players().size() == 1) {
+// load_game_from_file(gs, "savefile.save");
+// gs->update_iostate(true);
+// }
+// }
+//
+////Draw event
+// draw_timer.start();
+// bool draw_this_step = (i > 1 && i % settings.steps_per_draw == 0);
+// if (draw_this_step) {
+// gs->draw();
+// }
+//
+// draw_events++;
+// draw_time += draw_timer.get_microseconds();
+//
+// int repeat_amount = 1;
+// if (gs->key_down_state(SDLK_F1))
+// repeat_amount = 4;
+//
+// for (int repeat = 0; repeat < repeat_amount; repeat++) {
+// //Step event
+//
+// step_timer.start();
+// if (!paused) {
+// gs->step();
+// }
+// step_events++;
+// step_time += step_timer.get_microseconds();
+//
+// // Do not move this. It is done after the step event,
+// // but before actions are queued for the next turn
+// if (gs->key_press_state(SDLK_F5)) {
+// net_send_state_and_sync(gs->net_connection(), gs);
+// save_game_to_file(gs, "savefile.save");
+// }
+//
+// // The following will queue actions for the next step
+// bool is_done = (!paused && !gs->pre_step())
+// || (paused && !gs->update_iostate());
+// if (is_done) {
+// cont = false;
+// break;
+// }
+// }
+//
+// accumulated_time += total_timer.get_microseconds();
+//
+// long microwait = settings.time_per_step * 1000 * settings.steps_per_draw
+// - accumulated_time;
+// if (draw_this_step) {
+// long delayms = microwait / 1000;
+// if (delayms > 0) {
+// SDL_Delay(delayms);
+// }
+// accumulated_time = 0;
+// }
+// perf_timer_end("**Game Frame**");
+// }
+//
+// perf_print_results();
+//
+// printf("Step time: %f\n", float(step_time) / step_events / 1000);
+// printf("Draw time: %f\n", float(draw_time) / draw_events / 1000);
}
int main(int argc, char** argv) {
@@ -196,7 +193,9 @@ int main(int argc, char** argv) {
if (exitcode == 0) {
gs->start_game();
- game_loop(gs);
+ luawrap::globals(L)["main"].push();
+ luawrap::call<void>(L);
+ fflush(stdout);
}
save_settings_data(gs->game_settings(), "saved_settings.yaml");

0 comments on commit 4c5a537

Please sign in to comment.
Something went wrong with that request. Please try again.