Browse Source

Refactor stuff.

legacy
Sam Hocevar sam 15 years ago
parent
commit
57a0cb5102
20 changed files with 84 additions and 82 deletions
  1. +12
    -12
      src/debugfps.cpp
  2. +1
    -1
      src/debugfps.h
  3. +3
    -3
      src/debugrecord.cpp
  4. +1
    -1
      src/debugrecord.h
  5. +6
    -4
      src/debugsprite.cpp
  6. +1
    -1
      src/debugsprite.h
  7. +5
    -5
      src/entity.cpp
  8. +4
    -4
      src/entity.h
  9. +2
    -2
      src/font.cpp
  10. +1
    -1
      src/font.h
  11. +2
    -2
      src/game.cpp
  12. +1
    -1
      src/game.h
  13. +1
    -1
      src/gtk/editor.cpp
  14. +7
    -7
      src/profiler.cpp
  15. +2
    -2
      src/profiler.h
  16. +1
    -1
      src/test-map.cpp
  17. +30
    -30
      src/ticker.cpp
  18. +1
    -1
      src/ticker.h
  19. +2
    -2
      src/tileset.cpp
  20. +1
    -1
      src/tileset.h

+ 12
- 12
src/debugfps.cpp View File

@@ -42,9 +42,9 @@ Entity::Group DebugFps::GetGroup()
return GROUP_AFTER; return GROUP_AFTER;
} }


void DebugFps::TickRender(float deltams)
void DebugFps::TickDraw(float deltams)
{ {
Entity::TickRender(deltams);
Entity::TickDraw(deltams);


data->frame++; data->frame++;


@@ -52,26 +52,26 @@ void DebugFps::TickRender(float deltams)
Font *font = Forge::GetFont(data->fontid); Font *font = Forge::GetFont(data->fontid);


sprintf(buf, "%2.2f fps (%i)", sprintf(buf, "%2.2f fps (%i)",
1e3f / Profiler::GetMean(Profiler::STAT_TICK_FRAME), data->frame);
1e3f / Profiler::GetAvg(Profiler::STAT_TICK_FRAME), data->frame);
font->PrintBold(10, 10, buf); font->PrintBold(10, 10, buf);


sprintf(buf, "Game % 7.2f % 7.2f",
Profiler::GetMean(Profiler::STAT_TICK_GAME),
sprintf(buf, "Game % 7.2f % 7.2f",
Profiler::GetAvg(Profiler::STAT_TICK_GAME),
Profiler::GetMax(Profiler::STAT_TICK_GAME)); Profiler::GetMax(Profiler::STAT_TICK_GAME));
font->PrintBold(10, 34, buf); font->PrintBold(10, 34, buf);


sprintf(buf, "Render % 7.2f % 7.2f",
Profiler::GetMean(Profiler::STAT_TICK_RENDER),
Profiler::GetMax(Profiler::STAT_TICK_RENDER));
sprintf(buf, "Draw % 7.2f % 7.2f",
Profiler::GetAvg(Profiler::STAT_TICK_DRAW),
Profiler::GetMax(Profiler::STAT_TICK_DRAW));
font->PrintBold(10, 50, buf); font->PrintBold(10, 50, buf);


sprintf(buf, "Blit % 7.2f % 7.2f",
Profiler::GetMean(Profiler::STAT_TICK_BLIT),
sprintf(buf, "Blit % 7.2f % 7.2f",
Profiler::GetAvg(Profiler::STAT_TICK_BLIT),
Profiler::GetMax(Profiler::STAT_TICK_BLIT)); Profiler::GetMax(Profiler::STAT_TICK_BLIT));
font->PrintBold(10, 66, buf); font->PrintBold(10, 66, buf);


sprintf(buf, "Frame % 7.2f % 7.2f",
Profiler::GetMean(Profiler::STAT_TICK_FRAME),
sprintf(buf, "Frame % 7.2f % 7.2f",
Profiler::GetAvg(Profiler::STAT_TICK_FRAME),
Profiler::GetMax(Profiler::STAT_TICK_FRAME)); Profiler::GetMax(Profiler::STAT_TICK_FRAME));
font->PrintBold(10, 82, buf); font->PrintBold(10, 82, buf);
} }


+ 1
- 1
src/debugfps.h View File

@@ -23,7 +23,7 @@ public:


protected: protected:
virtual Group GetGroup(); virtual Group GetGroup();
virtual void TickRender(float deltams);
virtual void TickDraw(float deltams);


private: private:
DebugFpsData *data; DebugFpsData *data;


+ 3
- 3
src/debugrecord.cpp View File

@@ -44,7 +44,7 @@ DebugRecord::DebugRecord(char const *path)


Entity::Group DebugRecord::GetGroup() Entity::Group DebugRecord::GetGroup()
{ {
return GROUP_RENDER_CAPTURE;
return GROUP_DRAW_CAPTURE;
} }


void DebugRecord::TickGame(float deltams) void DebugRecord::TickGame(float deltams)
@@ -52,9 +52,9 @@ void DebugRecord::TickGame(float deltams)
Entity::TickGame(deltams); Entity::TickGame(deltams);
} }


void DebugRecord::TickRender(float deltams)
void DebugRecord::TickDraw(float deltams)
{ {
Entity::TickRender(deltams);
Entity::TickDraw(deltams);


int width = Video::GetWidth(); int width = Video::GetWidth();
int height = Video::GetHeight(); int height = Video::GetHeight();


+ 1
- 1
src/debugrecord.h View File

@@ -24,7 +24,7 @@ public:
protected: protected:
virtual Group GetGroup(); virtual Group GetGroup();
virtual void TickGame(float deltams); virtual void TickGame(float deltams);
virtual void TickRender(float deltams);
virtual void TickDraw(float deltams);


private: private:
DebugRecordData *data; DebugRecordData *data;


+ 6
- 4
src/debugsprite.cpp View File

@@ -55,16 +55,18 @@ void DebugSprite::TickGame(float deltams)
data->y += 0.1f * deltams * axis.y; data->y += 0.1f * deltams * axis.y;
} }


void DebugSprite::TickRender(float deltams)
void DebugSprite::TickDraw(float deltams)
{ {
Entity::TickRender(deltams);
Entity::TickDraw(deltams);


int x = data->x; int x = data->x;
int y = data->y; int y = data->y;
int z = data->z; int z = data->z;


data->game->GetScene()->AddTile((data->tiler << 16) | 15, x, y, z + 32, 1);
data->game->GetScene()->AddTile((data->tiler << 16) | 31, x, y, z, 1);
data->game->GetScene()->AddTile((data->tiler << 16) | 15,
x - 16, y - 32, z + 32, 1);
data->game->GetScene()->AddTile((data->tiler << 16) | 31,
x - 16, y - 32, z, 1);
} }


DebugSprite::~DebugSprite() DebugSprite::~DebugSprite()


+ 1
- 1
src/debugsprite.h View File

@@ -25,7 +25,7 @@ public:
protected: protected:
virtual Group GetGroup(); virtual Group GetGroup();
virtual void TickGame(float deltams); virtual void TickGame(float deltams);
virtual void TickRender(float deltams);
virtual void TickDraw(float deltams);


private: private:
DebugSpriteData *data; DebugSpriteData *data;


+ 5
- 5
src/entity.cpp View File

@@ -37,7 +37,7 @@ Entity::~Entity()


char const *Entity::GetName() char const *Entity::GetName()
{ {
return "Generic entity";
return "<entity>";
} }


Entity::Group Entity::GetGroup() Entity::Group Entity::GetGroup()
@@ -54,12 +54,12 @@ void Entity::TickGame(float deltams)
#endif #endif
} }


void Entity::TickRender(float deltams)
void Entity::TickDraw(float deltams)
{ {
#if !FINAL_RELEASE #if !FINAL_RELEASE
if (state != STATE_PRETICK_RENDER)
fprintf(stderr, "ERROR: invalid entity render tick\n");
state = STATE_POSTTICK_RENDER;
if (state != STATE_PRETICK_DRAW)
fprintf(stderr, "ERROR: invalid entity draw tick\n");
state = STATE_POSTTICK_DRAW;
#endif #endif
} }




+ 4
- 4
src/entity.h View File

@@ -32,7 +32,7 @@ protected:
GROUP_BEFORE = 0, GROUP_BEFORE = 0,
GROUP_DEFAULT, GROUP_DEFAULT,
GROUP_AFTER, GROUP_AFTER,
GROUP_RENDER_CAPTURE,
GROUP_DRAW_CAPTURE,
// Must be the last element // Must be the last element
GROUP_COUNT GROUP_COUNT
} }
@@ -45,7 +45,7 @@ protected:
virtual Group GetGroup(); virtual Group GetGroup();


virtual void TickGame(float deltams); virtual void TickGame(float deltams);
virtual void TickRender(float deltams);
virtual void TickDraw(float deltams);


Entity *next; Entity *next;
int ref, destroy; int ref, destroy;
@@ -56,8 +56,8 @@ protected:
STATE_IDLE = 0, STATE_IDLE = 0,
STATE_PRETICK_GAME, STATE_PRETICK_GAME,
STATE_POSTTICK_GAME, STATE_POSTTICK_GAME,
STATE_PRETICK_RENDER,
STATE_POSTTICK_RENDER,
STATE_PRETICK_DRAW,
STATE_POSTTICK_DRAW,
} }
state; state;
#endif #endif


+ 2
- 2
src/font.cpp View File

@@ -81,9 +81,9 @@ Entity::Group Font::GetGroup()
return GROUP_BEFORE; return GROUP_BEFORE;
} }


void Font::TickRender(float deltams)
void Font::TickDraw(float deltams)
{ {
Entity::TickRender(deltams);
Entity::TickDraw(deltams);
} }


char const *Font::GetName() char const *Font::GetName()


+ 1
- 1
src/font.h View File

@@ -25,7 +25,7 @@ protected:
/* Inherited from Entity */ /* Inherited from Entity */
virtual char const *GetName(); virtual char const *GetName();
virtual Group GetGroup(); virtual Group GetGroup();
virtual void TickRender(float deltams);
virtual void TickDraw(float deltams);


public: public:
/* New methods */ /* New methods */


+ 2
- 2
src/game.cpp View File

@@ -57,9 +57,9 @@ void Game::TickGame(float deltams)
Entity::TickGame(deltams); Entity::TickGame(deltams);
} }


void Game::TickRender(float deltams)
void Game::TickDraw(float deltams)
{ {
Entity::TickRender(deltams);
Entity::TickDraw(deltams);


GetScene(); GetScene();




+ 1
- 1
src/game.h View File

@@ -26,7 +26,7 @@ protected:
/* Inherited from Entity */ /* Inherited from Entity */
virtual Group GetGroup(); virtual Group GetGroup();
virtual void TickGame(float deltams); virtual void TickGame(float deltams);
virtual void TickRender(float deltams);
virtual void TickDraw(float deltams);


public: public:
/* New methods */ /* New methods */


+ 1
- 1
src/gtk/editor.cpp View File

@@ -78,7 +78,7 @@ static gint draw(GtkWidget *widget, GdkEventExpose *event)
/* Clear the screen, tick the renderer, show the frame and /* Clear the screen, tick the renderer, show the frame and
* clamp to desired framerate. */ * clamp to desired framerate. */
Video::Clear(); Video::Clear();
Ticker::TickRender();
Ticker::TickDraw();
gtk_gl_area_swapbuffers(GTK_GL_AREA(widget)); gtk_gl_area_swapbuffers(GTK_GL_AREA(widget));
while (g_main_context_iteration(NULL, FALSE)) while (g_main_context_iteration(NULL, FALSE))
; ;


+ 7
- 7
src/profiler.cpp View File

@@ -29,14 +29,14 @@ public:
for (int i = 0; i < HISTORY; i++) for (int i = 0; i < HISTORY; i++)
history[i] = 0.0f; history[i] = 0.0f;
frame = 0; frame = 0;
mean = max = 0.0f;
avg = max = 0.0f;
} }


private: private:
float history[HISTORY]; float history[HISTORY];
Timer timer; Timer timer;
int frame; int frame;
float mean, max;
float avg, max;
} }
data[Profiler::STAT_COUNT]; data[Profiler::STAT_COUNT];


@@ -55,21 +55,21 @@ void Profiler::Stop(int id)


data[id].history[data->frame % ProfilerData::HISTORY] = deltams; data[id].history[data->frame % ProfilerData::HISTORY] = deltams;
data[id].frame++; data[id].frame++;
data[id].mean = 0.0f;
data[id].avg = 0.0f;
data[id].max = 0.0f; data[id].max = 0.0f;


for (int i = 0; i < ProfilerData::HISTORY; i++) for (int i = 0; i < ProfilerData::HISTORY; i++)
{ {
data[id].mean += data[id].history[i];
data[id].avg += data[id].history[i];
if (data[id].history[i] > data[id].max) if (data[id].history[i] > data[id].max)
data[id].max = data[id].history[i]; data[id].max = data[id].history[i];
} }
data[id].mean /= ProfilerData::HISTORY;
data[id].avg /= ProfilerData::HISTORY;
} }


float Profiler::GetMean(int id)
float Profiler::GetAvg(int id)
{ {
return data[id].mean;
return data[id].avg;
} }


float Profiler::GetMax(int id) float Profiler::GetMax(int id)


+ 2
- 2
src/profiler.h View File

@@ -21,14 +21,14 @@ public:
{ {
STAT_TICK_FRAME = 0, STAT_TICK_FRAME = 0,
STAT_TICK_GAME, STAT_TICK_GAME,
STAT_TICK_RENDER,
STAT_TICK_DRAW,
STAT_TICK_BLIT, STAT_TICK_BLIT,
STAT_COUNT STAT_COUNT
}; };


static void Start(int id); static void Start(int id);
static void Stop(int id); static void Stop(int id);
static float GetMean(int id);
static float GetAvg(int id);
static float GetMax(int id); static float GetMax(int id);
}; };




+ 1
- 1
src/test-map.cpp View File

@@ -61,7 +61,7 @@ int main(int argc, char **argv)
/* Clear the screen, tick the renderer, show the frame and /* Clear the screen, tick the renderer, show the frame and
* clamp to desired framerate. */ * clamp to desired framerate. */
Video::Clear(); Video::Clear();
Ticker::TickRender();
Ticker::TickDraw();
SDL_GL_SwapBuffers(); SDL_GL_SwapBuffers();
Ticker::ClampFps(1000.0f / FPS); Ticker::ClampFps(1000.0f / FPS);
} }


+ 30
- 30
src/ticker.cpp View File

@@ -80,74 +80,74 @@ void Ticker::TickGame()
* before inserting awaiting objects, because there is no way these * before inserting awaiting objects, because there is no way these
* are already marked for destruction. */ * are already marked for destruction. */
for (int i = 0; i < Entity::GROUP_COUNT; i++) for (int i = 0; i < Entity::GROUP_COUNT; i++)
for (Entity *a = data->list[i], *prev = NULL; a; prev = a, a = a->next)
if (a->destroy)
for (Entity *e = data->list[i], *prev = NULL; e; prev = e, e = e->next)
if (e->destroy)
{ {
if (prev) if (prev)
prev->next = a->next;
prev->next = e->next;
else else
data->list[i] = a->next;
data->list[i] = e->next;


data->nentities--; data->nentities--;
delete a;
delete e;
} }


/* Insert waiting objects into the appropriate lists */ /* Insert waiting objects into the appropriate lists */
while (data->todo) while (data->todo)
{ {
Entity *a = data->todo;
data->todo = a->next;
Entity *e = data->todo;
data->todo = e->next;


int i = a->GetGroup();
a->next = data->list[i];
data->list[i] = a;
int i = e->GetGroup();
e->next = data->list[i];
data->list[i] = e;
data->nentities++; data->nentities++;
} }


/* Tick objects for the game loop */ /* Tick objects for the game loop */
for (int i = 0; i < Entity::GROUP_COUNT; i++) for (int i = 0; i < Entity::GROUP_COUNT; i++)
for (Entity *a = data->list[i]; a; a = a->next)
if (!a->destroy)
for (Entity *e = data->list[i]; e; e = e->next)
if (!e->destroy)
{ {
#if !FINAL_RELEASE #if !FINAL_RELEASE
if (a->state != Entity::STATE_IDLE)
if (e->state != Entity::STATE_IDLE)
fprintf(stderr, "ERROR: entity not idle for game tick\n"); fprintf(stderr, "ERROR: entity not idle for game tick\n");
a->state = Entity::STATE_PRETICK_GAME;
e->state = Entity::STATE_PRETICK_GAME;
#endif #endif
a->TickGame(data->deltams);
e->TickGame(data->deltams);
#if !FINAL_RELEASE #if !FINAL_RELEASE
if (a->state != Entity::STATE_POSTTICK_GAME)
if (e->state != Entity::STATE_POSTTICK_GAME)
fprintf(stderr, "ERROR: entity missed super game tick\n"); fprintf(stderr, "ERROR: entity missed super game tick\n");
a->state = Entity::STATE_IDLE;
e->state = Entity::STATE_IDLE;
#endif #endif
} }


Profiler::Stop(Profiler::STAT_TICK_GAME); Profiler::Stop(Profiler::STAT_TICK_GAME);
} }


void Ticker::TickRender()
void Ticker::TickDraw()
{ {
Profiler::Start(Profiler::STAT_TICK_RENDER);
Profiler::Start(Profiler::STAT_TICK_DRAW);


/* Tick objects for the render loop */
/* Tick objects for the draw loop */
for (int i = 0; i < Entity::GROUP_COUNT; i++) for (int i = 0; i < Entity::GROUP_COUNT; i++)
for (Entity *a = data->list[i]; a; a = a->next)
if (!a->destroy)
for (Entity *e = data->list[i]; e; e = e->next)
if (!e->destroy)
{ {
#if !FINAL_RELEASE #if !FINAL_RELEASE
if (a->state != Entity::STATE_IDLE)
fprintf(stderr, "ERROR: entity not idle for render tick\n");
a->state = Entity::STATE_PRETICK_RENDER;
if (e->state != Entity::STATE_IDLE)
fprintf(stderr, "ERROR: entity not idle for draw tick\n");
e->state = Entity::STATE_PRETICK_DRAW;
#endif #endif
a->TickRender(data->deltams);
e->TickDraw(data->deltams);
#if !FINAL_RELEASE #if !FINAL_RELEASE
if (a->state != Entity::STATE_POSTTICK_RENDER)
fprintf(stderr, "ERROR: entity missed super render tick\n");
a->state = Entity::STATE_IDLE;
if (e->state != Entity::STATE_POSTTICK_DRAW)
fprintf(stderr, "ERROR: entity missed super draw tick\n");
e->state = Entity::STATE_IDLE;
#endif #endif
} }


Profiler::Stop(Profiler::STAT_TICK_RENDER);
Profiler::Stop(Profiler::STAT_TICK_DRAW);
Profiler::Start(Profiler::STAT_TICK_BLIT); Profiler::Start(Profiler::STAT_TICK_BLIT);
} }




+ 1
- 1
src/ticker.h View File

@@ -22,7 +22,7 @@ public:
static void Register(Entity *entity); static void Register(Entity *entity);


static void TickGame(); static void TickGame();
static void TickRender();
static void TickDraw();
static void ClampFps(float deltams); static void ClampFps(float deltams);
}; };




+ 2
- 2
src/tileset.cpp View File

@@ -79,9 +79,9 @@ Entity::Group TileSet::GetGroup()
return GROUP_BEFORE; return GROUP_BEFORE;
} }


void TileSet::TickRender(float deltams)
void TileSet::TickDraw(float deltams)
{ {
Entity::TickRender(deltams);
Entity::TickDraw(deltams);


if (data->img) if (data->img)
{ {


+ 1
- 1
src/tileset.h View File

@@ -30,7 +30,7 @@ protected:
/* Inherited from Entity */ /* Inherited from Entity */
virtual char const *GetName(); virtual char const *GetName();
virtual Group GetGroup(); virtual Group GetGroup();
virtual void TickRender(float deltams);
virtual void TickDraw(float deltams);


public: public:
/* New methods */ /* New methods */


Loading…
Cancel
Save