1+ # strongly inspired by https://github.com/s-macke/VoxelSpace
12module VoxelSpace
2- using GLFW, ModernGL , Printf, ColorTypes, FileIO
3+ using MiniFB , Printf, ColorTypes, FileIO
34using Base: unsafe_trunc
45
56include (" map_utils.jl" )
@@ -15,33 +16,17 @@ const MAP_HEIGHT = 1024
1516function 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 θ += dθ
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
8766end
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-
9768end # module
0 commit comments