Skip to content

Commit 15ba002

Browse files
committed
replace GLFW with MiniFB
1 parent cbd4030 commit 15ba002

File tree

5 files changed

+44
-79
lines changed

5 files changed

+44
-79
lines changed

Project.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ version = "0.1.0"
66
[deps]
77
ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
88
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
9-
GLFW = "f7f18e0c-5ee9-5ccd-a5bf-e8befd85ed98"
109
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
11-
ModernGL = "66fc600b-dfda-50eb-8b99-91cfa97b1301"
10+
MiniFB = "cc649173-1898-4bb8-8a16-ca5f88bd87d9"
1211
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"

src/VoxelSpace.jl

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
# strongly inspired by https://github.com/s-macke/VoxelSpace
12
module VoxelSpace
2-
using GLFW, ModernGL, Printf, ColorTypes, FileIO
3+
using MiniFB, Printf, ColorTypes, FileIO
34
using Base: unsafe_trunc
45

56
include("map_utils.jl")
@@ -15,33 +16,17 @@ const MAP_HEIGHT = 1024
1516
function run(map="C1W")
1617
# Load map and create buffer
1718
datac, datah = read_map(map)
18-
data = similar(datac, WIN_WIDTH, WIN_HEIGHT)
19+
data = Matrix{RGB24}(undef, WIN_WIDTH, WIN_HEIGHT)
1920
hbuffer = Vector{Int}(undef, WIN_WIDTH)
2021

2122
# Create window
22-
GLFW.WindowHint(GLFW.DOUBLEBUFFER, false)
23-
window = GLFW.CreateWindow(WIN_WIDTH, WIN_HEIGHT, "VoxelSpace.jl")
24-
GLFW.MakeContextCurrent(window)
25-
26-
# Generate texture
27-
tex = glGenTextures()
28-
glBindTexture(GL_TEXTURE_2D,tex)
29-
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,WIN_WIDTH,WIN_HEIGHT,0,GL_RGB,GL_UNSIGNED_BYTE,data)
30-
31-
# Generate and bind the read framebuffer
32-
readFboId = Ref{Cuint}(0)
33-
glGenFramebuffers(1,readFboId)
34-
glBindFramebuffer(GL_READ_FRAMEBUFFER,readFboId[])
35-
36-
# Bind texture to the read framebuffer
37-
glFramebufferTexture2D(GL_READ_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,tex,0)
38-
39-
# Bind the default framebuffer (0) to draw
40-
glBindFramebuffer(GL_DRAW_FRAMEBUFFER,0)
23+
window = mfb_open_ex("VoxelSpace.jl", WIN_WIDTH, WIN_HEIGHT, MiniFB.WF_RESIZABLE);
4124

4225
# Set up input callbacks
43-
GLFW.SetKeyCallback(window,key_cb_fun)
44-
GLFW.SetCursorPosCallback(window,mouse_cb_fun)
26+
key_cb_fun_c = @cfunction(key_cb_fun, Cvoid, (Ptr{Cvoid}, mfb_key, mfb_key_mod, Bool))
27+
mouse_cb_fun_c = @cfunction(mouse_cb_fun, Cvoid, (Ptr{Cvoid}, Int32, Int32))
28+
mfb_set_keyboard_callback(window, key_cb_fun_c)
29+
mfb_set_mouse_move_callback(window, mouse_cb_fun_c)
4530

4631
# Set up FPS counter
4732
t1 = time_ns()
@@ -54,10 +39,7 @@ function run(map="C1W")
5439
θ = 0f0
5540

5641
try
57-
while !GLFW.WindowShouldClose(window)
58-
# Check for inputs
59-
GLFW.PollEvents()
60-
42+
while mfb_wait_sync(window)
6143
# Compute movement direction and apply to pos
6244
dx,dy,dz,dθ = compute_movement!(θ)
6345
px += dx
@@ -66,32 +48,21 @@ function run(map="C1W")
6648
θ +=
6749

6850
# Update texture
69-
render!(data,hbuffer,datac,datah,(px,py,pz),θ[],WIN_HEIGHT÷2,WIN_HEIGHT÷2)
70-
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,WIN_WIDTH,WIN_HEIGHT,GL_RGB,GL_UNSIGNED_BYTE,data)
51+
render!(data,hbuffer,datac,datah,(px,py,pz),θ,WIN_HEIGHT>>1,WIN_HEIGHT>>1)
7152

72-
# Blit read framebuffer (texture) to draw framebuffer (display)
73-
glBlitFramebuffer(0,0,WIN_WIDTH,WIN_HEIGHT,0,0,WIN_WIDTH,WIN_HEIGHT,GL_COLOR_BUFFER_BIT,GL_LINEAR)
74-
glFlush()
75-
#GLFW.SwapBuffers(window)
53+
state = mfb_update(window, data)
54+
state == MiniFB.STATE_OK || break
7655

7756
# Compute FPS
7857
t2 = time_ns()
7958
@printf("\b\b\b%3.0f", 1E9/(t2-t1))
8059
t1 = t2
8160
end
8261
finally
83-
GLFW.DestroyWindow(window)
62+
mfb_close(window)
8463
println()
8564
end
8665
return nothing
8766
end
8867

89-
90-
function ModernGL.glGenTextures()
91-
id = Ref{GLuint}(0)
92-
glGenTextures(1, id)
93-
id[] <= 0 && @error "glGenTextures returned an invalid id. Is the OpenGL context active?"
94-
return id[]
95-
end
96-
9768
end # module

src/input.jl

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,40 @@ const mov_up = Ref(false)
77
const mov_down = Ref(false)
88
const mov_boost = Ref(false)
99

10-
function key_cb_fun(_, key, scancode, action, mods)
11-
if key == GLFW.KEY_W # up
12-
action == GLFW.PRESS && (mov_forward[] = true)
13-
action == GLFW.RELEASE && (mov_forward[] = false)
14-
elseif key == GLFW.KEY_A # left
15-
action == GLFW.PRESS && (mov_left[] = true)
16-
action == GLFW.RELEASE && (mov_left[] = false)
17-
elseif key == GLFW.KEY_D # right
18-
action == GLFW.PRESS && (mov_right[] = true)
19-
action == GLFW.RELEASE && (mov_right[] = false)
20-
elseif key == GLFW.KEY_S # down
21-
action == GLFW.PRESS && (mov_back[] = true)
22-
action == GLFW.RELEASE && (mov_back[] = false)
23-
elseif scancode == 57 # space
24-
action == GLFW.PRESS && (mov_up[] = true)
25-
action == GLFW.RELEASE && (mov_up[] = false)
26-
elseif scancode == 29 # l-ctrl
27-
action == GLFW.PRESS && (mov_down[] = true)
28-
action == GLFW.RELEASE && (mov_down[] = false)
29-
elseif scancode == 42 # l-shift
30-
action == GLFW.PRESS && (mov_boost[] = true)
31-
action == GLFW.RELEASE && (mov_boost[] = false)
10+
function key_cb_fun(_, key, _, action)
11+
if key == MiniFB.KB_KEY_W # up
12+
mov_forward[] = action
13+
elseif key == MiniFB.KB_KEY_A # left
14+
mov_left[] = action
15+
elseif key == MiniFB.KB_KEY_D # right
16+
mov_right[] = action
17+
elseif key == MiniFB.KB_KEY_S # down
18+
mov_back[] = action
19+
elseif key == MiniFB.KB_KEY_SPACE # space
20+
mov_up[] = action
21+
elseif key == MiniFB.KB_KEY_LEFT_CONTROL # l-ctrl
22+
mov_down[] = action
23+
elseif key == MiniFB.KB_KEY_LEFT_SHIFT # l-shift
24+
mov_boost[] = action
3225
end
3326
return nothing
3427
end
3528

36-
const mouse_pos = Float32[0,0]
37-
const mouse_move = Float32[0,0]
29+
30+
const mouse_pos = Int32[0,0]
31+
const mouse_mov = Int32[0,0]
3832

3933
function mouse_cb_fun(_, x, y)
40-
mouse_move .= (x,y) .- mouse_pos
41-
mouse_pos .= (x,y)
34+
mouse_mov .= (x,y) .- mouse_pos
35+
mouse_pos .= (x,y)
4236
return nothing
4337
end
4438

39+
4540
function compute_movement!(θ)
4641
# mouse
47-
= -mouse_move[1]*1f-2
48-
mouse_move .= 0f0
42+
= -mouse_mov[1]*1f-2
43+
fill!(mouse_mov, zero(Int32))
4944
# keyboard; signs empirically adjusted...
5045
sθ, cθ = sincos(θ)
5146
dx = (mov_right[]-mov_left[])*- (mov_forward[]-mov_back[])*

src/map_utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function read_map(map::String)
22
map_dir = joinpath(dirname(@__DIR__), "maps")
3-
datac = load(joinpath(map_dir,map)*".png")
3+
datac = RGB24.(load(joinpath(map_dir,map)*".png"))
44
datah = Float32.(reinterpret(UInt8,load(joinpath(map_dir,MAP_LIST[map])*".png")))
55
return datac, datah
66
end

src/render.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
function render!(data, hbuffer, colormap, heightmap, pos::Tuple{T,T,T}, θ::T, horizon, zscale) where T<:Number
1+
function render!(data, hbuffer, colormap, heightmap, pos::Tuple{T,T,T}, θ::T, horizon, zscale) where T<:Real
22
# draw background (light blue sky)
3-
fill!(data, RGB(0.529f0, 0.808f0, 0.980f0))
4-
fill!(hbuffer, 1)
3+
fill!(data, RGB24(0.529f0, 0.808f0, 0.980f0))
4+
fill!(hbuffer, WIN_HEIGHT)
55
# precompute some constants
66
w = Base.multiplicativeinverse(MAP_WIDTH%UInt)
77
h = Base.multiplicativeinverse(MAP_HEIGHT%UInt)
@@ -21,14 +21,14 @@ function render!(data, hbuffer, colormap, heightmap, pos::Tuple{T,T,T}, θ::T, h
2121
pxi = 1 + rem(unsafe_trunc(Int, px)%UInt, w)
2222
pyi = 1 + rem(unsafe_trunc(Int, py)%UInt, h)
2323
# compute the on-screen height of the feature
24-
height = min(WIN_HEIGHT, unsafe_trunc(Int, (heightmap[pxi,pyi]-pos[3])*scale) + horizon)
24+
height = unsafe_trunc(Int, (pos[3]-heightmap[pxi,pyi])*scale) + horizon
2525
# assign color to column
2626
color = colormap[pxi,pyi]
27-
for i in hbuffer[j] : height
27+
for i in max(height,1) : hbuffer[j]
2828
data[j,i] = color
2929
end
3030
# keep track of feature height
31-
if height > hbuffer[j]
31+
if height < hbuffer[j]
3232
hbuffer[j] = height
3333
end
3434
# move to next view-line feature position

0 commit comments

Comments
 (0)