-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameloop_test.go
54 lines (50 loc) · 1.5 KB
/
gameloop_test.go
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
package macaw
import (
"github.com/tubelz/macaw/entity"
"github.com/tubelz/macaw/input"
"github.com/tubelz/macaw/internal/utils"
"github.com/tubelz/macaw/system"
"github.com/veandco/go-sdl2/sdl"
"testing"
"time"
)
// TestGameLoop_Run checks if our gameloop runs with a basic setup
func TestGameLoop_Run(t *testing.T) {
utils.SetupLog(t)
// This is also consider setup. We are adding what it needs to be added so our game loop can be ran
input := &input.Manager{}
em := &entity.Manager{}
sceneGame := &Scene{Name: "game"}
sceneGame.AddRenderSystem(&system.RenderSystem{Camera: &entity.Entity{}, EntityManager: em})
// Initialize the mocked systems to test whether systems are executed in sequence or not
var val int
mockFunc1 := func() {
if val != 0 {
t.Errorf("Got %d; want 0", val)
}
val++
}
mockFunc2 := func() {
if val != 1 {
t.Errorf("Got %d; want 1", val)
}
// add big delay to avoid the systems to be ran again
duration := time.Millisecond * 10
time.Sleep(duration)
}
mockSystem1 := &utils.MockSystem{MockFunc: mockFunc1}
mockSystem2 := &utils.MockSystem{MockFunc: mockFunc2}
sceneGame.AddGameUpdateSystem(mockSystem1)
sceneGame.AddGameUpdateSystem(mockSystem2)
gameLoop := &GameLoop{InputManager: input}
gameLoop.AddScene(sceneGame)
// close game after 100 milliseconds
go func() {
duration := time.Millisecond
time.Sleep(duration)
quitEvent := &sdl.QuitEvent{Type: sdl.QUIT, Timestamp: sdl.GetTicks()}
sdl.PushEvent(quitEvent)
}()
gameLoop.Run()
utils.TeardownLog()
}