DXGL r507 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r506‎ | r507 | r508 >
Date:21:33, 31 August 2014
Author:admin
Status:new
Tags:
Comment:
Convert DXGL internal timer from C++ to C.
Modified paths:
  • /ddraw/common.h (modified) (history)
  • /ddraw/ddraw.vcxproj (modified) (history)
  • /ddraw/ddraw.vcxproj.filters (modified) (history)
  • /ddraw/glRenderer.cpp (modified) (history)
  • /ddraw/timer.c (added) (history)
  • /ddraw/timer.cpp (deleted) (history)
  • /ddraw/timer.h (modified) (history)

Diff [purge]

Index: ddraw/timer.cpp
@@ -1,74 +0,0 @@
2 -// DXGL
3 -// Copyright (C) 2013 William Feely
4 -
5 -// This library is free software; you can redistribute it and/or
6 -// modify it under the terms of the GNU Lesser General Public
7 -// License as published by the Free Software Foundation; either
8 -// version 2.1 of the License, or (at your option) any later version.
9 -
10 -// This library is distributed in the hope that it will be useful,
11 -// but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 -// Lesser General Public License for more details.
14 -
15 -// You should have received a copy of the GNU Lesser General Public
16 -// License along with this library; if not, write to the Free Software
17 -// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 -
19 -#include "common.h"
20 -#include "timer.h"
21 -
22 -DXGLTimer::DXGLTimer()
23 -{
24 - TIMECAPS mmcaps;
25 - LARGE_INTEGER freq;
26 - timertype = 0;
27 - freq.QuadPart = 0;
28 - QueryPerformanceFrequency(&freq);
29 - if (!freq.QuadPart)
30 - {
31 - timeGetDevCaps(&mmcaps, sizeof(TIMECAPS));
32 - timeBeginPeriod(mmcaps.wPeriodMin);
33 - timer_frequency = ((double) mmcaps.wPeriodMin / 1.0) * 1000.0;
34 - timertype = 2;
35 - }
36 - else
37 - {
38 - timer_frequency = (double)freq.QuadPart;
39 - timertype = 1;
40 - }
41 -}
42 -
43 -void DXGLTimer::Calibrate(unsigned int lines, unsigned int frequency)
44 -{
45 - monitor_period = (double) frequency / 1000.0;
46 - if (timertype == 1)
47 - {
48 - QueryPerformanceCounter(&timer_base);
49 - vsync_lines = 16;
50 - this->lines = lines + vsync_lines;
51 - }
52 - else
53 - {
54 - timer_base.QuadPart = timeGetTime();
55 - double linesperms = (double) lines / ((1.0 / (double) frequency) * 1000.0);
56 - vsync_lines = linesperms * (frequency / 1000.0);
57 - this->lines = lines + vsync_lines;
58 - }
59 -}
60 -
61 -unsigned int DXGLTimer::GetScanLine()
62 -{
63 - LARGE_INTEGER timerpos;
64 - double sync_pos;
65 - if (timertype == 1) QueryPerformanceCounter(&timerpos);
66 - else timerpos.QuadPart = timeGetTime();
67 - timerpos.QuadPart -= timer_base.QuadPart;
68 - double milliseconds;
69 - if (timertype == 1) milliseconds = ((double) timerpos.QuadPart / (double)timer_frequency) * 1000.0;
70 - else milliseconds = (double) timerpos.QuadPart;
71 - sync_pos = fmod(milliseconds, monitor_period);
72 - sync_pos /= monitor_period;
73 - sync_pos *= (double)lines;
74 - return sync_pos;
75 -}
\ No newline at end of file
Index: ddraw/common.h
@@ -116,7 +116,5 @@
117117 extern CRITICAL_SECTION dll_cs;
118118
119119 #include "trace.h"
120 -#ifdef __cplusplus
121120 #include "../cfgmgr/cfgmgr.h"
122 -#endif
123121 #endif //_COMMON_H
Index: ddraw/ddraw.vcxproj
@@ -393,7 +393,14 @@
394394 <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug no DXGL|Win32'">NotUsing</PrecompiledHeader>
395395 <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug No MSVCRT|Win32'">NotUsing</PrecompiledHeader>
396396 </ClCompile>
397 - <ClCompile Include="timer.cpp" />
 397+ <ClCompile Include="timer.c">
 398+ <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug Trace|Win32'">NotUsing</PrecompiledHeader>
 399+ <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
 400+ <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release no DXGL|Win32'">NotUsing</PrecompiledHeader>
 401+ <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>
 402+ <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug no DXGL|Win32'">NotUsing</PrecompiledHeader>
 403+ <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug No MSVCRT|Win32'">NotUsing</PrecompiledHeader>
 404+ </ClCompile>
398405 <ClCompile Include="trace.c">
399406 <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug Trace|Win32'">NotUsing</PrecompiledHeader>
400407 <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
Index: ddraw/ddraw.vcxproj.filters
@@ -199,9 +199,6 @@
200200 <ClCompile Include="glDirect3DExecuteBuffer.cpp">
201201 <Filter>Source Files</Filter>
202202 </ClCompile>
203 - <ClCompile Include="timer.cpp">
204 - <Filter>Source Files</Filter>
205 - </ClCompile>
206203 <ClCompile Include="glUtil.cpp">
207204 <Filter>Source Files</Filter>
208205 </ClCompile>
@@ -235,6 +232,9 @@
236233 <ClCompile Include="ShaderGen2D.cpp">
237234 <Filter>Source Files</Filter>
238235 </ClCompile>
 236+ <ClCompile Include="timer.c">
 237+ <Filter>Source Files</Filter>
 238+ </ClCompile>
239239 </ItemGroup>
240240 <ItemGroup>
241241 <ResourceCompile Include="ddraw.rc">
Index: ddraw/glRenderer.cpp
@@ -683,7 +683,7 @@
684684 */
685685 unsigned int glRenderer_GetScanLine(glRenderer *This)
686686 {
687 - return This->timer.GetScanLine();
 687+ return DXGLTimer_GetScanLine(&This->timer);
688688 }
689689
690690 /**
@@ -888,7 +888,8 @@
889889 glRenderer__SetSwap(This,1);
890890 SwapBuffers(This->hDC);
891891 glFinish();
892 - This->timer.Calibrate(height, frequency);
 892+ DXGLTimer_Init(&This->timer);
 893+ DXGLTimer_Calibrate(&This->timer, height, frequency);
893894 glRenderer__SetSwap(This,0);
894895 This->util->SetViewport(0,0,width,height);
895896 glViewport(0,0,width,height);
@@ -1698,7 +1699,8 @@
16991700 LeaveCriticalSection(&dll_cs);
17001701 glRenderer__SetSwap(This,1);
17011702 SwapBuffers(This->hDC);
1702 - This->timer.Calibrate(height, frequency);
 1703+ DXGLTimer_Init(&This->timer);
 1704+ DXGLTimer_Calibrate(&This->timer, height, frequency);
17031705 glRenderer__SetSwap(This,0);
17041706 This->util->SetViewport(0,0,width,height);
17051707 }
Index: ddraw/timer.c
@@ -0,0 +1,76 @@
 2+// DXGL
 3+// Copyright (C) 2013 William Feely
 4+
 5+// This library is free software; you can redistribute it and/or
 6+// modify it under the terms of the GNU Lesser General Public
 7+// License as published by the Free Software Foundation; either
 8+// version 2.1 of the License, or (at your option) any later version.
 9+
 10+// This library is distributed in the hope that it will be useful,
 11+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 12+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 13+// Lesser General Public License for more details.
 14+
 15+// You should have received a copy of the GNU Lesser General Public
 16+// License along with this library; if not, write to the Free Software
 17+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 18+
 19+#include "common.h"
 20+#include <math.h>
 21+#include "timer.h"
 22+
 23+void DXGLTimer_Init(DXGLTimer *timer)
 24+{
 25+ TIMECAPS mmcaps;
 26+ LARGE_INTEGER freq;
 27+ timer->timertype = 0;
 28+ freq.QuadPart = 0;
 29+ QueryPerformanceFrequency(&freq);
 30+ if (!freq.QuadPart)
 31+ {
 32+ timeGetDevCaps(&mmcaps, sizeof(TIMECAPS));
 33+ timeBeginPeriod(mmcaps.wPeriodMin);
 34+ timer->timer_frequency = ((double) mmcaps.wPeriodMin / 1.0) * 1000.0;
 35+ timer->timertype = 2;
 36+ }
 37+ else
 38+ {
 39+ timer->timer_frequency = (double)freq.QuadPart;
 40+ timer->timertype = 1;
 41+ }
 42+}
 43+
 44+void DXGLTimer_Calibrate(DXGLTimer *timer, unsigned int lines, unsigned int frequency)
 45+{
 46+ double linesperms;
 47+ timer->monitor_period = (double) frequency / 1000.0;
 48+ if (timer->timertype == 1)
 49+ {
 50+ QueryPerformanceCounter(&timer->timer_base);
 51+ timer->vsync_lines = 16;
 52+ timer->lines = lines + timer->vsync_lines;
 53+ }
 54+ else
 55+ {
 56+ timer->timer_base.QuadPart = timeGetTime();
 57+ linesperms = (double) lines / ((1.0 / (double) frequency) * 1000.0);
 58+ timer->vsync_lines = (unsigned int)(linesperms * (frequency / 1000.0));
 59+ timer->lines = lines + timer->vsync_lines;
 60+ }
 61+}
 62+
 63+unsigned int DXGLTimer_GetScanLine(DXGLTimer *timer)
 64+{
 65+ LARGE_INTEGER timerpos;
 66+ double sync_pos;
 67+ double milliseconds;
 68+ if (timer->timertype == 1) QueryPerformanceCounter(&timerpos);
 69+ else timerpos.QuadPart = timeGetTime();
 70+ timerpos.QuadPart -= timer->timer_base.QuadPart;
 71+ if (timer->timertype == 1) milliseconds = ((double) timerpos.QuadPart / (double)timer->timer_frequency) * 1000.0;
 72+ else milliseconds = (double) timerpos.QuadPart;
 73+ sync_pos = fmod(milliseconds, timer->monitor_period);
 74+ sync_pos /= timer->monitor_period;
 75+ sync_pos *= (double)timer->lines;
 76+ return (unsigned int)sync_pos;
 77+}
\ No newline at end of file
Index: ddraw/timer.h
@@ -19,13 +19,12 @@
2020 #ifndef _TIMER_H
2121 #define _TIMER_H
2222
23 -class DXGLTimer
 23+#ifdef __cplusplus
 24+extern "C" {
 25+#endif
 26+
 27+typedef struct DXGLTimer
2428 {
25 -public:
26 - DXGLTimer();
27 - void Calibrate(unsigned int lines, unsigned int frequency);
28 - unsigned int GetScanLine();
29 -private:
3029 int timertype;
3130 unsigned int lines;
3231 unsigned int vsync_lines;
@@ -32,6 +31,15 @@
3332 double monitor_period;
3433 double timer_frequency;
3534 LARGE_INTEGER timer_base;
36 -};
 35+} DXGLTimer;
3736
 37+void DXGLTimer_Init(DXGLTimer *timer);
 38+void DXGLTimer_Calibrate(DXGLTimer *timer, unsigned int lines, unsigned int frequency);
 39+unsigned int DXGLTimer_GetScanLine(DXGLTimer *timer);
 40+
 41+#ifdef __cplusplus
 42+}
 43+#endif
 44+
 45+
3846 #endif
\ No newline at end of file