This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fps.cpp
112 lines (103 loc) · 3.16 KB
/
fps.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
Frames per second counting class.
FPS class based off what I was taught by LazyFoo's tutorials on lazyfoo.net.
It isn't nessesarally the same. I didn't copy it, because I don't remember it all
exactly.
It also contains the credit and logo rendering functionality.
Copyright 2010 James Russell
*/
//INCLUDES//
#include "fps.h"
//External definitions//
extern SDL_Surface *AC_FPSSurf;
extern TTF_Font *AC_Font_ArialBig;
extern void AC_ShowSurface(SDL_Surface *AC_ShowSurfSrc, int x, int y);
////////////////CLASS////////////////
//Init of FPS class.
AC_C_FPS::AC_C_FPS(void)
{
//Init the variables.
TimeLastChecked=SDL_GetTicks();
FpsNumber=0;
//Say you init the FPS class.
printf("FPS class constructed.\n");
}
//Init what couln't when the class was contructed.
void AC_C_FPS::InitFPS(void)
{
//Convert the actual FPS number into a string
// for the SDL_WM_SetCaption function, which needs a string.
sprintf(FpsAsString,"%d",FpsNumber);
//Render the text surface.
AC_FPSSurf=TTF_RenderText_Solid(AC_Font_ArialBig,FpsAsString,AC_Color_LightGrey);
//Render the credit surface as text.
AC_CreditSurf=TTF_RenderText_Blended(AC_Font_ArialSmall,"A creation of Oh! Poop! Productions!",AC_Color_LightGrey);
//If it failed...
if(AC_CreditSurf==NULL)
{
//Say so and why.
printf("***Error rendering credit surface: %s***\n",TTF_GetError());
}
}
//Show the FPS.
void AC_C_FPS::ShowFPS(void)
{
//If the user says to render the FPS...
if(AC_RenderFPS==true)
{
//Then show the FPS surface.
AC_ShowSurface(AC_FPSSurf,595-AC_FPSSurf->w/2,25);
//Also show the logo...
AC_ShowSurface(AC_Logo,490,25);
//...and the credit surface.
AC_ShowSurface(AC_CreditSurf,616-AC_CreditSurf->w,95);
}
}
//Check how many milliseconds have passed since last time set.
void AC_C_FPS::CheckTime(Uint32 Milliseconds)
{
//If 1 second has passed...
if(SDL_GetTicks()>TimeLastChecked+Milliseconds)
{
//Free the old fps text surface.
SDL_FreeSurface(AC_FPSSurf);
//Convert the actual FPS number into a string
// for the SDL_WM_SetCaption function, which needs a string.
sprintf(FpsAsString,"%d",FpsNumber);
//Reset the FPS counter.
FpsNumber=0;
//Reset the time.
SetTime();
//Then render the number to the surface.
AC_FPSSurf=TTF_RenderText_Solid(AC_Font_ArialBig,FpsAsString,AC_Color_LightGrey);
}
}
//Set the current time to compare to.
void AC_C_FPS::SetTime(void)
{
//Set the time variable to whatever time is now.
TimeLastChecked=SDL_GetTicks();
}
//Increment the fps number func.
void AC_C_FPS::IncrementFPS(void)
{
//Increment the FPS counter.
FpsNumber++;
}
//Change weather the FPS is shown or not.
void AC_C_FPS::ChangeShowState(void)
{
//If the FPS is not being rendered right now...
if(AC_RenderFPS==false)
{
//Then say that it should be.
AC_RenderFPS=true;
}
//Otherwise, it must be rendering...
else
{
//So tell the computer to stop.s
AC_RenderFPS=false;
}
}
////////////////EOF////////////////