Skip to content

Commit c38faa2

Browse files
Create c_util.lua
1 parent 7e86616 commit c38faa2

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
local DEBUG = true
2+
local rootElement = getRootElement()
3+
local resName = getResourceName(getThisResource())
4+
5+
local c_DefaultPopupTimeout = 5000 --ms
6+
local c_FadeDelta = .03 --alpha per frame
7+
local c_MaxAlpha = .9
8+
9+
local function fadeIn(wnd)
10+
local function raiseAlpha()
11+
local newAlpha = guiGetAlpha(wnd) + c_FadeDelta
12+
if newAlpha <= c_MaxAlpha then
13+
guiSetAlpha(wnd, newAlpha)
14+
else
15+
removeEventHandler("onClientRender", rootElement, raiseAlpha)
16+
end
17+
end
18+
addEventHandler("onClientRender", rootElement, raiseAlpha)
19+
end
20+
21+
local function fadeOut(wnd)
22+
local function lowerAlpha()
23+
local newAlpha = guiGetAlpha(wnd) - c_FadeDelta
24+
if newAlpha >= 0 then
25+
guiSetAlpha(wnd, newAlpha)
26+
else
27+
removeEventHandler("onClientRender", rootElement, lowerAlpha)
28+
destroyElement(wnd)
29+
end
30+
end
31+
addEventHandler("onClientRender", rootElement, lowerAlpha)
32+
end
33+
34+
function outputGuiPopup(text, timeout)
35+
local screenX, screenY = guiGetScreenSize()
36+
local width = 500
37+
local height = 20
38+
local wndPopup = guiCreateWindow((screenX - width) / 2, screenY - height, width, height, '', false)
39+
40+
guiSetAlpha(wndPopup, 0)
41+
guiSetFont(wndPopup, "clear-normal")
42+
guiSetText(wndPopup, text)
43+
44+
guiWindowSetMovable(wndPopup, false)
45+
guiWindowSetSizable(wndPopup, false)
46+
47+
fadeIn(wndPopup)
48+
setTimer(fadeOut, timeout or c_DefaultPopupTimeout, 1, wndPopup)
49+
end
50+
51+
52+
---- FPS
53+
54+
FPSMax = 1
55+
FPSAvg = 1
56+
FPSCalc = 0
57+
FPSTime = getTickCount() + 1000
58+
AVGTbl = {}
59+
val = 1
60+
61+
function CalcFps( )
62+
if (getTickCount() < FPSTime) then
63+
FPSCalc = FPSCalc + 1
64+
else
65+
if (FPSCalc > FPSMax) then
66+
FPSMax = FPSCalc
67+
end
68+
if val == 101 then val = 1 end
69+
AVGTbl[val] = FPSCalc
70+
FPSAvg = 0
71+
for k,v in pairs(AVGTbl) do
72+
FPSAvg = FPSAvg + v
73+
end
74+
FPSAvg = math.floor(FPSAvg / #AVGTbl)
75+
FPSCalc = 0
76+
FPSTime = getTickCount() + 1000
77+
val = val + 1
78+
end
79+
end
80+
81+
82+
function GetCurrentFps()
83+
return FPSCalc
84+
end
85+
86+
87+
function GetAverageFps()
88+
return FPSAvg
89+
end
90+
91+
92+
--
93+
-- DEBUG
94+
--
95+
function alert(message, channel)
96+
if not DEBUG then return end
97+
98+
message = resName..": "..tostring(message)
99+
100+
if channel == "console" then
101+
outputConsole(message)
102+
return
103+
end
104+
105+
if channel == "chat" then
106+
outputChatBox(message)
107+
return
108+
end
109+
110+
outputDebugString(message)
111+
end

0 commit comments

Comments
 (0)