Repository navigation
Expand file tree
/
Copy pathapp.nim
More file actions
83 lines (61 loc) · 1.97 KB
/
Copy pathapp.nim
File metadata and controls
83 lines (61 loc) · 1.97 KB
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
import sdl2
import render_buffer, color
var
window: WindowPtr
render: RendererPtr = nil
screenHeight: int
proc initApp*(width, height: int) =
discard sdl2.init(INIT_VIDEO)
var
dm: DisplayMode
x = cint(0)
y = cint(0)
if getCurrentDisplayMode(0, dm) == SdlSuccess:
x = cint(dm.w div 2 - width div 2)
y = cint(dm.h div 2 - height div 2)
else:
echo "Unable to get display mode"
window = createWindow("Own GL", x, y, cint(width), cint(height), SDL_WINDOW_SHOWN)
render = createRenderer(window, -1, Renderer_Software)
screenHeight = height
proc drawRenderBufferToWindow*(buf: RenderBuffer[RenderColor]) =
if render == nil:
raise newException(SystemError, "Init app first.")
render.setDrawColor 0, 0, 0, 255
render.clear
var pointOperations = 0
for x, y, col in buf.entries():
when defined(debug):
echo $x & "x" & $y & ": " & $col.red & " " & $col.green & " " & $col.blue & " " & $col.alpha
render.setDrawColor col.red, col.green, col.blue, col.alpha
render.drawPoint cint(x), cint(screenHeight - y)
inc(pointOperations)
echo "Point operations: " & $pointOperations
render.present
proc drawZBufferToWindow*(zBuffer: RenderBuffer[float], near: float = 1.0, far: float = 30.0) =
if render == nil:
raise newException(SystemError, "Init app first.")
render.setDrawColor 0, 0, 0, 255
render.clear
var pointOperations = 0
for x, y, z in zBuffer.entries():
let c = byte(255.0 * (2.0 * near) / (far + near - z * (far - near)))
render.setDrawColor c, c, c, 255
render.drawPoint cint(x), cint(screenHeight - y)
inc(pointOperations)
echo "Point operations: " & $pointOperations
render.present
proc waitForAppClose*() =
var
done = false
evt = sdl2.defaultEvent
while not done:
while pollEvent(evt):
if evt.kind == QuitEvent:
done = true
break
elif evt.kind == WindowEvent:
render.present
delay 10
destroy render
destroy window