5a5ad32991c8fa18c5af37b3b2b3d0c7610629d7
[matthijs/ABM2.git] / ABM2 / Engine / profiler.h
1 #ifndef PROFILER_H_INCLUDED\r
2 #define PROFILER_H_INCLUDED\r
3 \r
4 #define MAX_PROFILER_SAMPLES 50\r
5 \r
6 /* HACK to make things compile */\r
7 #define GetTickCount() 0\r
8 \r
9 class IProfilerOutputHandler;\r
10 class CProfileSample;\r
11 \r
12 class CProfileSample\r
13 {\r
14 public:\r
15         CProfileSample(std::string sampleName);\r
16         ~CProfileSample();\r
17 \r
18         static void Output();\r
19 \r
20         static void ResetSample(std::string sampleName);\r
21         static void ResetAll();\r
22 \r
23         static IProfilerOutputHandler *outputHandler;\r
24 \r
25         static bool bProfilerIsRunning;\r
26 \r
27 protected:\r
28         //index into the array of samples\r
29         int iSampleIndex;\r
30         int iParentIndex;\r
31 \r
32         inline float GetTime(){ return ((float)GetTickCount())/1000.0f; }\r
33 \r
34         static struct profileSample\r
35         {\r
36                 profileSample()\r
37                 {\r
38                         bIsValid=false; \r
39                         dataCount=0;\r
40                         averagePc=minPc=maxPc=-1;\r
41                 }\r
42 \r
43                 bool bIsValid;          //whether or not this sample is valid (for use with fixed-size arrays)\r
44                 bool bIsOpen;           //is this sample currently being profiled?\r
45                 unsigned int callCount; //number of times this sample has been profiled this frame\r
46                 std::string name;       //name of the sample\r
47                 \r
48                 float startTime;        //starting time on the clock, in seconds\r
49                 float totalTime;        //total time recorded across all profiles of this sample\r
50                 float childTime;        //total time taken by children of this sample\r
51 \r
52                 int parentCount;        //number of parents this sample has (useful for indenting)\r
53 \r
54                 float averagePc;        //average percentage of game loop time taken up\r
55                 float minPc;            //minimum percentage of game loop time taken up\r
56                 float maxPc;            //maximum percentage of game loop time taken up\r
57                 unsigned long dataCount;//number of percentage values that have been stored\r
58         } samples[MAX_PROFILER_SAMPLES];\r
59         static int lastOpenedSample;\r
60         static int openSampleCount;\r
61         static float rootBegin, rootEnd;\r
62 };\r
63 \r
64 class IProfilerOutputHandler\r
65 {\r
66 public:\r
67         virtual void BeginOutput(float tTotal)=0;\r
68         virtual void Sample(float fMin, float fAvg, float fMax, float tAvg, int callCount, std::string name, int parentCount)=0;\r
69         virtual void EndOutput()=0;\r
70 };\r
71 \r
72 #ifdef _DEBUG\r
73 #define PROFILE(name) CProfileSample _profile_sample(name);\r
74 #else\r
75 #define PROFILE(name)\r
76 #endif\r
77 \r
78 #endif\r