Skip to content

Commit b3348ab

Browse files
committed
WIP initial cmd removal + drivers
1 parent 2ba854a commit b3348ab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2812
-81
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# GOOM
22
DOOM engine written in Go
33

4-
![GOOM](/misc/goom.png?raw=true "GOOM")
4+
![GOOM](/resources/press/goom.png?raw=true "GOOM")
55

66
Make sure you have `DOOM1.WAD` in the root dir. Then type `make run` to run GOOM.
77

@@ -10,7 +10,7 @@ Make sure you have `DOOM1.WAD` in the root dir. Then type `make run` to run GOOM
1010
The project is an experiment and is still lacking a lot of
1111
features, such as shooting, enemy behavior, sound, menus, and more.
1212

13-
![DEMO](/misc/goom-preview.gif?raw=true "DEMO")
13+
![DEMO](/resources/press/goom-preview.gif?raw=true "DEMO")
1414

1515
# Development Setup
1616

cmd/doom/internal/game/world.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import (
88
"github.com/go-gl/mathgl/mgl32"
99

1010
"github.com/tinogoehlert/goom"
11-
"github.com/tinogoehlert/goom/geometry"
11+
"github.com/tinogoehlert/goom/utils"
1212
"github.com/tinogoehlert/goom/level"
1313
)
1414

1515
// Wall a DOOM Wall
1616
type Wall struct {
17-
Start geometry.Vec2
18-
End geometry.Vec2
19-
Normal geometry.Vec2
20-
Tangent geometry.Vec2
17+
Start utils.Vec2
18+
End utils.Vec2
19+
Normal utils.Vec2
20+
Tangent utils.Vec2
2121
Length float32
2222
Sides struct {
2323
Right *level.SideDef
@@ -149,7 +149,7 @@ func (w *World) SetPlayer(num int) error {
149149

150150
// Update updates the world (monster think and player position)
151151
func (w *World) Update(t float32) {
152-
ppos := geometry.V2(w.me.position[0], w.me.position[1])
152+
ppos := utils.V2(w.me.position[0], w.me.position[1])
153153
for _, m := range w.monsters {
154154
if !m.IsCorpse() {
155155
m.Update()
@@ -160,14 +160,14 @@ func (w *World) Update(t float32) {
160160
for e := w.projectiles.Front(); e != nil; e = e.Next() {
161161
var (
162162
p = e.Value.(*Projectile)
163-
projPos = geometry.V2(p.position[0], p.position[1])
163+
projPos = utils.V2(p.position[0], p.position[1])
164164
)
165165
for _, m := range w.monsters {
166166
if m.IsCorpse() {
167167
continue
168168
}
169169
if w.hitThing(m, p, m.sizeX, m.sizeY) {
170-
mpos := geometry.V2(m.position[0], m.position[1])
170+
mpos := utils.V2(m.position[0], m.position[1])
171171
dist := ppos.DistanceTo(mpos)
172172
w.projectiles.Remove(e)
173173
m.Hit(p.damage, dist)

cmd/doom/internal/opengl/geometry.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ type glWorldGeometry struct {
1717
seqTime time.Time
1818
}
1919

20-
func addGlWorldGeometry(dst []*glWorldGeometry, src *glWorldGeometry) []*glWorldGeometry {
20+
func addGlWorldutils(dst []*glWorldGeometry, src *glWorldGeometry) []*glWorldGeometry {
2121
if src == nil {
2222
return dst
2323
}
2424
return append(dst, src)
2525
}
2626

27-
func newGlWorldGeometry(data []float32, light float32, texture []*glTexture) *glWorldGeometry {
27+
func newGlWorldutils(data []float32, light float32, texture []*glTexture) *glWorldGeometry {
2828
m := &glWorldGeometry{
2929
data: data,
3030
light: light,

cmd/doom/internal/opengl/level.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package opengl
22

33
import (
44
"github.com/go-gl/gl/v2.1/gl"
5-
"github.com/tinogoehlert/goom"
5+
"github.com/tinogoehlert/goom/goom"
66
"github.com/tinogoehlert/goom/level"
77
)
88

@@ -73,12 +73,12 @@ func (s *subSector) addFlats(md *level.Level, gd *goom.GameData, ts glTextureSto
7373

7474
s.sector = sector
7575
if len(gd.Flat(sector.FloorTexture())) > 0 {
76-
fm := newGlWorldGeometry(floorData, sector.LightLevel(), ts[sector.FloorTexture()])
77-
s.floors = addGlWorldGeometry(s.floors, fm)
76+
fm := newGlWorldutils(floorData, sector.LightLevel(), ts[sector.FloorTexture()])
77+
s.floors = addGlWorldutils(s.floors, fm)
7878
}
7979
if len(gd.Flat(sector.CeilTexture())) > 0 {
80-
cm := newGlWorldGeometry(ceilData, md.Sectors[side.Sector].LightLevel(), ts[sector.CeilTexture()])
81-
s.ceilings = addGlWorldGeometry(s.ceilings, cm)
80+
cm := newGlWorldutils(ceilData, md.Sectors[side.Sector].LightLevel(), ts[sector.CeilTexture()])
81+
s.ceilings = addGlWorldutils(s.ceilings, cm)
8282
}
8383
}
8484

@@ -135,8 +135,8 @@ func (s *subSector) addWalls(md *level.Level, gd *goom.GameData, ts glTextureSto
135135
-start.X(), sector.CeilHeight(), start.Y(), -sLen / tw, height / th,
136136
}
137137
if gd.Texture(side.Upper()) != nil {
138-
wm := newGlWorldGeometry(wallData, sector.LightLevel(), ts[side.Upper()])
139-
s.walls = addGlWorldGeometry(s.walls, wm)
138+
wm := newGlWorldutils(wallData, sector.LightLevel(), ts[side.Upper()])
139+
s.walls = addGlWorldutils(s.walls, wm)
140140
}
141141
}
142142

@@ -159,8 +159,8 @@ func (s *subSector) addWalls(md *level.Level, gd *goom.GameData, ts glTextureSto
159159
-start.X(), sector.FloorHeight(), start.Y(), -sLen / tw, height / th,
160160
}
161161
if gd.Texture(side.Lower()) != nil {
162-
wm := newGlWorldGeometry(wallData, sector.LightLevel(), ts[side.Lower()])
163-
s.walls = addGlWorldGeometry(s.walls, wm)
162+
wm := newGlWorldutils(wallData, sector.LightLevel(), ts[side.Lower()])
163+
s.walls = addGlWorldutils(s.walls, wm)
164164
}
165165
}
166166

@@ -181,8 +181,8 @@ func (s *subSector) addWalls(md *level.Level, gd *goom.GameData, ts glTextureSto
181181
-end.X(), sector.FloorHeight(), end.Y(), -eLen / tw, height / th,
182182
-start.X(), sector.FloorHeight(), start.Y(), -sLen / tw, height / th,
183183
}
184-
wm := newGlWorldGeometry(wallData, sector.LightLevel(), ts[side.Middle()])
185-
s.walls = addGlWorldGeometry(s.walls, wm)
184+
wm := newGlWorldutils(wallData, sector.LightLevel(), ts[side.Middle()])
185+
s.walls = addGlWorldutils(s.walls, wm)
186186
}
187187
}
188188
}

cmd/doom/internal/opengl/renderer.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"github.com/go-gl/glfw/v3.2/glfw"
1010
"github.com/go-gl/mathgl/mgl32"
1111

12-
"github.com/tinogoehlert/goom"
13-
"github.com/tinogoehlert/goom/cmd/doom/internal/game"
12+
"github.com/tinogoehlert/goom/game"
13+
"github.com/tinogoehlert/goom/goom"
1414
"github.com/tinogoehlert/goom/graphics"
1515
"github.com/tinogoehlert/goom/level"
1616
)
@@ -285,8 +285,9 @@ func (gr *GLRenderer) Loop(drawCB func(), inputCB func(win *glfw.Window, frameti
285285
targetFrameTime := time.Second / time.Duration(gr.fpsCap)
286286
for !gr.window.ShouldClose() {
287287
t0 := time.Now()
288-
gr.fbWidth, gr.fbHeight = gr.window.GetFramebufferSize()
289-
gl.Viewport(0, 0, int32(gr.fbWidth), int32(gr.fbHeight))
288+
gr.fbWidth = 1024
289+
gr.fbHeight = 768
290+
gl.Viewport(0, 0, 1024, 768)
290291
// Do OpenGL stuff.
291292
gl.Enable(gl.DEPTH_TEST)
292293
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

cmd/doom/main.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ import (
99

1010
"github.com/go-gl/glfw/v3.2/glfw"
1111

12-
"github.com/tinogoehlert/goom"
12+
"github.com/tinogoehlert/goom/goom"
1313
"github.com/tinogoehlert/goom/audio/midi"
14-
"github.com/tinogoehlert/goom/cmd/doom/internal/game"
14+
"github.com/tinogoehlert/goom/game"
1515
"github.com/tinogoehlert/goom/cmd/doom/internal/opengl"
1616
"github.com/tinogoehlert/goom/graphics"
1717
"github.com/tinogoehlert/goom/level"
18+
"github.com/tinogoehlert/goom/utils"
1819
)
1920

2021
var (
2122
shaderDir = "resources/shaders"
22-
log = goom.GoomConsole
23+
log = utils.GoomConsole
2324
)
2425

2526
type renderStats struct {

cmd/maptest/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/faiface/pixel/imdraw"
1313
"github.com/faiface/pixel/pixelgl"
1414
"github.com/tinogoehlert/goom"
15-
"github.com/tinogoehlert/goom/geometry"
15+
"github.com/tinogoehlert/goom/utils"
1616
"github.com/ttacon/chalk"
1717
"golang.org/x/image/colornames"
1818
)
@@ -133,7 +133,7 @@ func run() {
133133
var (
134134
ls = e1m1.Vert(uint32(line.Start)).DivScalar(down)
135135
le = e1m1.Vert(uint32(line.End)).DivScalar(down)
136-
mid = geometry.V2((ls.X()+le.X())/2, (ls.Y()+le.Y())/2)
136+
mid = utils.V2((ls.X()+le.X())/2, (ls.Y()+le.Y())/2)
137137
)
138138

139139
tangent := le.Sub(ls).Normalize()

drivers/audio.go

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package drivers
2+
3+
type AudioDriver interface {
4+
}

drivers/glfw/glfw.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package glfw
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
7+
"github.com/go-gl/glfw/v3.2/glfw"
8+
)
9+
10+
// Init initialize glfw
11+
func Init() error {
12+
runtime.LockOSThread()
13+
14+
if err := glfw.Init(); err != nil {
15+
return fmt.Errorf("could not initialize GLFW: %s", err.Error())
16+
}
17+
return nil
18+
}
19+
20+
// Destroy terminates the GLFW Driver
21+
func Destroy() {
22+
glfw.Terminate()
23+
}

drivers/glfw/input.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package glfw
2+
3+
import (
4+
"github.com/go-gl/glfw/v3.2/glfw"
5+
"github.com/tinogoehlert/goom/drivers"
6+
)
7+
8+
type Keycode int
9+
10+
// InputDriver handles GLFW Input Events
11+
type InputDriver struct {
12+
keyStates chan drivers.Key
13+
}
14+
15+
// NewInputDriver creates a new GLFW Input Driver
16+
func NewInputDriver() *InputDriver {
17+
return &InputDriver{
18+
keyStates: make(chan drivers.Key, 2),
19+
}
20+
}
21+
22+
// Poll polls for events
23+
func (id *InputDriver) poll(win *Window) {
24+
glfw.PollEvents()
25+
}
26+
27+
// KeyStates returns a channel where key state changes will be published
28+
func (id *InputDriver) KeyStates() chan drivers.Key {
29+
return id.keyStates
30+
}
31+
32+
func (id *InputDriver) NormalizeKeyCode(key glfw.Key) drivers.Keycode {
33+
switch key {
34+
case glfw.KeyUp:
35+
return drivers.KeyUp
36+
case glfw.KeyDown:
37+
return drivers.KeyDown
38+
case glfw.KeyLeft:
39+
return drivers.KeyLeft
40+
case glfw.KeyRight:
41+
return drivers.KeyRight
42+
default:
43+
return 0
44+
}
45+
}

drivers/glfw/window.go

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package glfw
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/go-gl/glfw/v3.2/glfw"
7+
"github.com/tinogoehlert/goom/drivers"
8+
)
9+
10+
// Window GLFW implementation for Window
11+
type Window struct {
12+
window *glfw.Window
13+
width int
14+
height int
15+
fbWidth int
16+
fbHeight int
17+
secsPerUpdate float64
18+
fbSizeChanged func(width int, height int)
19+
inputDrv *InputDriver
20+
}
21+
22+
// NewWindow creates a new GLFW Window
23+
func NewWindow(title string, width, height int) (*Window, error) {
24+
glfw.WindowHint(glfw.Resizable, glfw.True)
25+
glfw.WindowHint(glfw.ContextVersionMajor, 3)
26+
glfw.WindowHint(glfw.ContextVersionMinor, 2)
27+
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
28+
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
29+
glfwWin, err := glfw.CreateWindow(width, height, title, nil, nil)
30+
if err != nil {
31+
return nil, fmt.Errorf("could not create GLFW Window: %s", err.Error())
32+
}
33+
glfwWin.MakeContextCurrent()
34+
win := &Window{
35+
window: glfwWin,
36+
width: width,
37+
height: height,
38+
secsPerUpdate: 1.0 / 120.0,
39+
inputDrv: NewInputDriver(),
40+
}
41+
42+
win.fbWidth, win.fbHeight = glfwWin.GetFramebufferSize()
43+
44+
glfwWin.SetFramebufferSizeCallback(func(w *glfw.Window, width int, height int) {
45+
win.fbWidth = width
46+
win.fbHeight = height
47+
if win.fbSizeChanged != nil {
48+
win.fbSizeChanged(width, height)
49+
}
50+
})
51+
52+
glfwWin.SetSizeCallback(func(w *glfw.Window, width int, height int) {
53+
win.width = width
54+
win.height = height
55+
})
56+
57+
return win, nil
58+
}
59+
60+
func (w *Window) Input() drivers.InputDriver {
61+
return w.inputDrv
62+
}
63+
64+
// Size Returns the current size of the Window
65+
func (w *Window) Size() (int, int) {
66+
return w.width, w.height
67+
}
68+
69+
// Size Returns the current size of the Window
70+
func (w *Window) FrameBufferSize() (int, int) {
71+
return w.fbWidth, w.fbHeight
72+
}
73+
74+
func (w *Window) onKeyCallback(win *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
75+
w.inputDrv.keyStates <- drivers.Key{
76+
Keycode: w.inputDrv.NormalizeKeyCode(key),
77+
State: drivers.KeyState(action),
78+
}
79+
}
80+
81+
// Run runs the window loop
82+
func (w *Window) Run(loop func(elapsed float32)) {
83+
var (
84+
previous = glfw.GetTime()
85+
)
86+
87+
w.window.SetKeyCallback(w.onKeyCallback)
88+
89+
for !w.window.ShouldClose() {
90+
var (
91+
frameTime = glfw.GetTime() - previous
92+
waitTime = w.secsPerUpdate - frameTime
93+
)
94+
loop(float32(waitTime))
95+
if waitTime > 0 {
96+
glfw.WaitEventsTimeout(waitTime)
97+
}
98+
99+
w.window.SwapBuffers()
100+
glfw.PollEvents()
101+
previous = glfw.GetTime()
102+
}
103+
}
104+
105+
// Close closes the window
106+
func (w *Window) Close() {
107+
w.window.Destroy()
108+
}

0 commit comments

Comments
 (0)