-
-
Save soundyogi/269b1b6569d6d47e7403d711ff60f2a9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// One thing I do different from other DOD users is that I hide implementation a bit more | |
// in cases where the implementation takes a bit more time or experimentation to get right. | |
// You can still dive in when you need to change it, but as long as it works, you can ignore it. | |
void mainThreadThing() | |
{ | |
//OOP: | |
TimingThing time = new TimingThing(); | |
time.saveCurrentTime(); | |
TimingThing otherTime = new TimingThing(); | |
otherTime.saveCurrentTime(); | |
int secondsPassed = time.SecondsSince(otherTime); | |
// replacement | |
int timeCount = Time::GetCurrent(); | |
int otherTime = Time::GetCurrent(); | |
int timeCountPerSecond = Time::GetCountsPerSecond(); | |
int countsPassed = timeCount - otherTime; | |
int secondsPassed = countsPassed / timeCountPerSecond; | |
// a lot more of the code is here. But if I do the count to Second conversion a lot, or if the conversion is weirder, | |
// I'd create a function that I can use multiple times. | |
} | |
class TimingThing | |
{ | |
// Casey points out: why the fuck would you use a class if you're gonna start with public | |
public: | |
TimingThing(); | |
void saveCurrentTime();// stores count and counts per second in | |
int SecondsSince( TimingThing& other ); | |
// in some languages you would now need getter and setter functions, I think C++ allows you to access other privates if they're the same class, for some reason. | |
// TO keep this brief I'm gonna assume that's the case. | |
private: | |
// Windows API implementation details: | |
int m_counts; | |
int m_countsPerSecond; | |
}; | |
TimingThing::TimingThing() | |
{ | |
saveCurrentTime(); | |
// Windows implementation details | |
LARGE_INTEGER countsPerSecond; | |
if (QueryPerformanceFrequency( &countsPerSecond )) | |
{ | |
this.m_countsPerSecond = countsPerSecond.QuadPart; | |
} | |
else | |
{ | |
Log::LastWinApiError( "Querying performance counter frequency" ); | |
return 0; | |
} | |
} | |
int TimingThing::SecondsSince(TimingThing& other) | |
{ | |
int countDifference = this.m_counts - other.m_counts; | |
return countDifference / this.m_countsPerSecond; | |
// Notice the magic 'this' pointer/reference everything (I might have gotten the -> . stuff wrong here, don't want to bother checking right now) | |
// You could even leave the 'this' out, and it would be even more magical. | |
} | |
// I now pretty much use namespaces everywhere to make code easier to read. | |
// I usually still use the namespace CompanyName around that for naming conflicts. | |
// But I don't bother going down many levels, because then you start needing multiple inheritance bullshit. | |
// This is simply so that everything is in context to its namespace, and you avoid C-style longAssName bullshit | |
// c-style longAssName bullshit example: VkPhysicalDeviceMemoryProperties | |
// Vk can be left out because you can use that as the wrapping namespace, OR just changing the way you include files | |
// the rest could be split in multiple ways depending on how you want to use it. | |
// one example would be that MemoryProperties is a struct in the PhysicalDevice namespace. | |
// It's tempting to make everything a nested namespace, | |
/// but that makes it a bitch to modify if you later decide you need to use this code in a different subcategoriy | |
namespace Time | |
{ | |
int GetCurrent() | |
{ | |
// Windows implementation details | |
LARGE_INTEGER now; | |
if (QueryPerformanceCounter( &now )) | |
{ | |
return now.QuadPart; | |
} | |
else | |
{ | |
Log::LastWinApiError( "Querying performance counter" ); | |
return 0; | |
} | |
} | |
int GetCountsPerSecond() | |
{ | |
LARGE_INTEGER countsPerSecond; | |
if (QueryPerformanceFrequency( &countsPerSecond )) | |
{ | |
return countsPerSecond.QuadPart; | |
} | |
else | |
{ | |
Log::LastWinApiError( "Querying performance counter frequency" ); | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment