From c1851f2c2be9f65e4d44bfb4a7e131b5c1550746 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sun, 22 Aug 2010 17:52:12 +0000 Subject: [PATCH] Store the frame number in the Ticker instead of duplicating the information in each class that uses it. --- src/debugfps.cpp | 7 ++---- src/gtk/glmapview.cpp | 52 +++++++++++++++++++++---------------------- src/gtk/glmapview.h | 18 +++++++-------- src/profiler.cpp | 7 ++---- src/ticker.cpp | 13 ++++++++--- src/ticker.h | 1 + 6 files changed, 50 insertions(+), 48 deletions(-) diff --git a/src/debugfps.cpp b/src/debugfps.cpp index a09c87fc..4f97c063 100644 --- a/src/debugfps.cpp +++ b/src/debugfps.cpp @@ -22,7 +22,6 @@ class DebugFpsData private: int fontid; - int frame; }; /* @@ -34,7 +33,6 @@ DebugFps::DebugFps() data = new DebugFpsData(); data->fontid = Forge::Register("gfx/font/ascii.png"); - data->frame = 0; } Entity::Group DebugFps::GetGroup() @@ -46,13 +44,12 @@ void DebugFps::TickDraw(float deltams) { Entity::TickDraw(deltams); - data->frame++; - char buf[1024]; Font *font = Forge::GetFont(data->fontid); sprintf(buf, "%2.2f fps (%i)", - 1e3f / Profiler::GetAvg(Profiler::STAT_TICK_FRAME), data->frame); + 1e3f / Profiler::GetAvg(Profiler::STAT_TICK_FRAME), + Ticker::GetFrameNum()); font->PrintBold(10, 10, buf); sprintf(buf, "Game % 7.2f % 7.2f", diff --git a/src/gtk/glmapview.cpp b/src/gtk/glmapview.cpp index b3c27867..1bc83894 100644 --- a/src/gtk/glmapview.cpp +++ b/src/gtk/glmapview.cpp @@ -118,9 +118,9 @@ gboolean GlMapView::Destroy() return TRUE; } -gboolean GlMapView::Draw(GdkEventExpose *event) +gboolean GlMapView::Draw(GdkEventExpose *e) { - if (event->count > 0) + if (e->count > 0) return TRUE; /* OpenGL functions can be called only if make_current returns true */ @@ -186,19 +186,19 @@ gboolean GlMapView::MoveAdjustments(double dx, double dy) return TRUE; } -gboolean GlMapView::MouseButton(GdkEventButton *event) +gboolean GlMapView::MouseButton(GdkEventButton *e) { - if (event->type == GDK_BUTTON_PRESS && event->button == 2) + if (e->type == GDK_BUTTON_PRESS && e->button == 2) { panning = TRUE; - xpan = event->x; - ypan = event->y; + xpan = e->x; + ypan = e->y; GdkCursor *cursor = gdk_cursor_new(GDK_HAND1); gdk_window_set_cursor(glarea->window, cursor); gdk_cursor_unref(cursor); return FALSE; } - else if (event->type == GDK_BUTTON_RELEASE && event->button == 2) + else if (e->type == GDK_BUTTON_RELEASE && e->button == 2) { panning = FALSE; gdk_window_set_cursor(glarea->window, NULL); @@ -208,28 +208,28 @@ gboolean GlMapView::MouseButton(GdkEventButton *event) return TRUE; } -gboolean GlMapView::MouseMotion(GdkEventMotion *event) +gboolean GlMapView::MouseMotion(GdkEventMotion *e) { if (panning) { - if (event->x != xpan) + if (e->x != xpan) { double val = gtk_adjustment_get_value(hadj); int map_width = mapviewer->GetWidth(); - val += xpan - event->x; - xpan = event->x; + val += xpan - e->x; + xpan = e->x; if (val + glarea->allocation.width > map_width) val = map_width - glarea->allocation.width; gtk_adjustment_set_value(hadj, val); gtk_adjustment_value_changed(hadj); } - if (event->y != ypan) + if (e->y != ypan) { double val = gtk_adjustment_get_value(vadj); int map_height = mapviewer->GetHeight(); - val += ypan - event->y; - ypan = event->y; + val += ypan - e->y; + ypan = e->y; if (val + glarea->allocation.height > map_height) val = map_height - glarea->allocation.height; gtk_adjustment_set_value(vadj, val); @@ -240,9 +240,9 @@ gboolean GlMapView::MouseMotion(GdkEventMotion *event) return TRUE; } -gboolean GlMapView::KeyPress(GdkEventKey *event) +gboolean GlMapView::KeyPress(GdkEventKey *e) { - switch (event->keyval) + switch (e->keyval) { case GDK_Up: MoveAdjustments( 0.0, -10.0); break; case GDK_Down: MoveAdjustments( 0.0, 10.0); break; @@ -272,39 +272,39 @@ gboolean GlMapView::DestroySignal(GtkWidget *w, GlMapView *that) return that->Destroy(); } -gboolean GlMapView::DrawSignal(GtkWidget *w, GdkEventExpose *event, +gboolean GlMapView::DrawSignal(GtkWidget *w, GdkEventExpose *e, GlMapView *that) { (void)w; - return that->Draw(event); + return that->Draw(e); } -gboolean GlMapView::ReshapeSignal(GtkWidget *w, GdkEventConfigure *event, +gboolean GlMapView::ReshapeSignal(GtkWidget *w, GdkEventConfigure *e, GlMapView *that) { (void)w; - (void)event; + (void)e; return that->Setup(); } -gboolean GlMapView::MouseButtonSignal(GtkWidget *w, GdkEventButton *event, +gboolean GlMapView::MouseButtonSignal(GtkWidget *w, GdkEventButton *e, GlMapView *that) { (void)w; - return that->MouseButton(event); + return that->MouseButton(e); } -gboolean GlMapView::MouseMotionSignal(GtkWidget *w, GdkEventMotion *event, +gboolean GlMapView::MouseMotionSignal(GtkWidget *w, GdkEventMotion *e, GlMapView *that) { (void)w; - return that->MouseMotion(event); + return that->MouseMotion(e); } -gboolean GlMapView::KeyPressSignal(GtkWidget *w, GdkEventKey *event, +gboolean GlMapView::KeyPressSignal(GtkWidget *w, GdkEventKey *e, GlMapView *that) { (void)w; - return that->KeyPress(event); + return that->KeyPress(e); } diff --git a/src/gtk/glmapview.h b/src/gtk/glmapview.h index 02d1fa50..49305ba2 100644 --- a/src/gtk/glmapview.h +++ b/src/gtk/glmapview.h @@ -20,26 +20,26 @@ private: gboolean IdleTick(); gboolean Setup(); gboolean Destroy(); - gboolean Draw(GdkEventExpose *event); + gboolean Draw(GdkEventExpose *e); gboolean UpdateAdjustments(); gboolean MoveAdjustments(double dx, double dy); - gboolean MouseButton(GdkEventButton *event); - gboolean MouseMotion(GdkEventMotion *event); - gboolean KeyPress(GdkEventKey *event); + gboolean MouseButton(GdkEventButton *e); + gboolean MouseMotion(GdkEventMotion *e); + gboolean KeyPress(GdkEventKey *e); /* Private signal slots */ static gboolean IdleTickSignal(GlMapView *that); static gboolean SetupSignal(GtkWidget *w, GlMapView *that); static gboolean DestroySignal(GtkWidget *w, GlMapView *that); - static gboolean DrawSignal(GtkWidget *w, GdkEventExpose *event, + static gboolean DrawSignal(GtkWidget *w, GdkEventExpose *e, GlMapView *that); - static gboolean ReshapeSignal(GtkWidget *w, GdkEventConfigure *event, + static gboolean ReshapeSignal(GtkWidget *w, GdkEventConfigure *e, GlMapView *that); - static gboolean MouseButtonSignal(GtkWidget *w, GdkEventButton *event, + static gboolean MouseButtonSignal(GtkWidget *w, GdkEventButton *e, GlMapView *that); - static gboolean MouseMotionSignal(GtkWidget *w, GdkEventMotion *event, + static gboolean MouseMotionSignal(GtkWidget *w, GdkEventMotion *e, GlMapView *that); - static gboolean KeyPressSignal(GtkWidget *w, GdkEventKey *event, + static gboolean KeyPressSignal(GtkWidget *w, GdkEventKey *e, GlMapView *that); private: diff --git a/src/profiler.cpp b/src/profiler.cpp index 12693fa7..80dc33b3 100644 --- a/src/profiler.cpp +++ b/src/profiler.cpp @@ -21,21 +21,19 @@ static class ProfilerData { friend class Profiler; - static int const HISTORY = 30; + static int const HISTORY = 32; public: ProfilerData() { for (int i = 0; i < HISTORY; i++) history[i] = 0.0f; - frame = 0; avg = max = 0.0f; } private: float history[HISTORY]; Timer timer; - int frame; float avg, max; } data[Profiler::STAT_COUNT]; @@ -53,8 +51,7 @@ void Profiler::Stop(int id) { float deltams = data[id].timer.GetMs(); - data[id].history[data->frame % ProfilerData::HISTORY] = deltams; - data[id].frame++; + data[id].history[Ticker::GetFrameNum() % ProfilerData::HISTORY] = deltams; data[id].avg = 0.0f; data[id].max = 0.0f; diff --git a/src/ticker.cpp b/src/ticker.cpp index a5c0a15d..0a23edc8 100644 --- a/src/ticker.cpp +++ b/src/ticker.cpp @@ -23,12 +23,11 @@ static class TickerData public: TickerData() : - todo(0), - nentities(0) + todo(0), nentities(0), + frame(0), deltams(0), bias(0) { for (int i = 0; i < Entity::GROUP_COUNT; i++) list[i] = NULL; - bias = 0.0f; } ~TickerData() @@ -46,6 +45,7 @@ private: int nentities; /* Fixed framerate management */ + int frame; Timer timer; float deltams, bias; } @@ -73,6 +73,8 @@ void Ticker::TickGame() Profiler::Start(Profiler::STAT_TICK_GAME); + data->frame++; + data->deltams = data->timer.GetMs(); data->bias += data->deltams; @@ -162,3 +164,8 @@ void Ticker::ClampFps(float deltams) data->bias -= deltams; } +int Ticker::GetFrameNum() +{ + return data->frame; +} + diff --git a/src/ticker.h b/src/ticker.h index d2a42fc6..8711d98d 100644 --- a/src/ticker.h +++ b/src/ticker.h @@ -24,6 +24,7 @@ public: static void TickGame(); static void TickDraw(); static void ClampFps(float deltams); + static int GetFrameNum(); }; #endif // __DH_TICKER_H__