1
1
package main
2
2
3
3
import (
4
- "fmt"
5
- "gl "
6
- "math "
7
- "os "
8
- "github.com/banthar/Go-SDL/sdl "
4
+ "fmt"
5
+ "github.com/banthar/Go-SDL/sdl "
6
+ "github.com/banthar/gl "
7
+ "math "
8
+ "os "
9
9
)
10
10
11
11
const (
12
- SCREEN_WIDTH = 1366
13
- SCREEN_HEIGHT = 768
14
- SCREEN_BPP = 32
12
+ SCREEN_WIDTH = 1366
13
+ SCREEN_HEIGHT = 768
14
+ SCREEN_BPP = 32
15
15
)
16
16
17
17
var (
18
- surface * sdl.Surface
18
+ surface * sdl.Surface
19
19
)
20
20
21
21
// release/destroy our resources and restoring the old desktop
22
22
func Quit (status int ) {
23
- // clean up the window
24
- sdl .Quit ()
23
+ // clean up the window
24
+ sdl .Quit ()
25
25
26
- // and exit appropriately
27
- os .Exit (status )
26
+ // and exit appropriately
27
+ os .Exit (status )
28
28
}
29
29
30
30
// reset our viewport after a window resize
31
31
func resizeWindow (width , height int ) {
32
- // protect against a divide by zero
33
- if height == 0 {
34
- height = 1
35
- }
36
-
37
- // Setup our viewport
38
- gl .Viewport (0 , 0 , width , height )
39
-
40
- // change to the projection matrix and set our viewing volume.
41
- gl .MatrixMode (gl .PROJECTION )
42
- gl .LoadIdentity ()
43
-
44
- // aspect ratio
45
- aspect := gl .GLdouble (width / height )
46
-
47
- // Set our perspective.
48
- // This code is equivalent to using gluPerspective as in the original tutorial.
49
- var fov , near , far gl.GLdouble
50
- fov = 45.0
51
- near = 0.1
52
- far = 100.0
53
- top := gl .GLdouble (math .Tan (float64 (fov * math .Pi / 360.0 ))) * near
54
- bottom := - top
55
- left := aspect * bottom
56
- right := aspect * top
57
- gl .Frustum (float64 (left ), float64 (right ), float64 (bottom ), float64 (top ), float64 (near ), float64 (far ))
58
-
59
- // Make sure we're changing the model view and not the projection
60
- gl .MatrixMode (gl .MODELVIEW )
61
-
62
- // Reset the view
63
- gl .LoadIdentity ()
32
+ // protect against a divide by zero
33
+ if height == 0 {
34
+ height = 1
35
+ }
36
+
37
+ // Setup our viewport
38
+ gl .Viewport (0 , 0 , width , height )
39
+
40
+ // change to the projection matrix and set our viewing volume.
41
+ gl .MatrixMode (gl .PROJECTION )
42
+ gl .LoadIdentity ()
43
+
44
+ // aspect ratio
45
+ aspect := gl .GLdouble (width / height )
46
+
47
+ // Set our perspective.
48
+ // This code is equivalent to using gluPerspective as in the original tutorial.
49
+ var fov , near , far gl.GLdouble
50
+ fov = 45.0
51
+ near = 0.1
52
+ far = 100.0
53
+ top := gl .GLdouble (math .Tan (float64 (fov * math .Pi / 360.0 ))) * near
54
+ bottom := - top
55
+ left := aspect * bottom
56
+ right := aspect * top
57
+ gl .Frustum (float64 (left ), float64 (right ), float64 (bottom ), float64 (top ), float64 (near ), float64 (far ))
58
+
59
+ // Make sure we're changing the model view and not the projection
60
+ gl .MatrixMode (gl .MODELVIEW )
61
+
62
+ // Reset the view
63
+ gl .LoadIdentity ()
64
64
}
65
65
66
66
// handle key press events
67
67
func handleKeyPress (keysym sdl.Keysym ) {
68
- switch keysym .Sym {
69
- case sdl .K_ESCAPE :
70
- Quit (0 )
71
- case sdl .K_F1 :
72
- sdl .WM_ToggleFullScreen (surface )
73
- }
68
+ switch keysym .Sym {
69
+ case sdl .K_ESCAPE :
70
+ Quit (0 )
71
+ case sdl .K_F1 :
72
+ sdl .WM_ToggleFullScreen (surface )
73
+ }
74
74
}
75
75
76
76
// general OpenGL initialization
77
77
func initGL () {
78
- // enable smooth shading
79
- gl .ShadeModel (gl .SMOOTH )
78
+ // enable smooth shading
79
+ gl .ShadeModel (gl .SMOOTH )
80
80
81
- // Set the background to black
82
- gl .ClearColor (0.0 , 0.0 , 0.0 , 0.0 )
81
+ // Set the background to black
82
+ gl .ClearColor (0.0 , 0.0 , 0.0 , 0.0 )
83
83
84
- // Depth buffer setup
85
- gl .ClearDepth (1.0 )
84
+ // Depth buffer setup
85
+ gl .ClearDepth (1.0 )
86
86
87
- // Enable depth testing
88
- gl .Enable (gl .DEPTH_TEST )
87
+ // Enable depth testing
88
+ gl .Enable (gl .DEPTH_TEST )
89
89
90
- // The type of test
91
- gl .DepthFunc (gl .LEQUAL )
90
+ // The type of test
91
+ gl .DepthFunc (gl .LEQUAL )
92
92
93
- // Nicest perspective correction
94
- gl .Hint (gl .PERSPECTIVE_CORRECTION_HINT , gl .NICEST )
93
+ // Nicest perspective correction
94
+ gl .Hint (gl .PERSPECTIVE_CORRECTION_HINT , gl .NICEST )
95
95
}
96
96
97
97
// used to calculate fps
@@ -100,90 +100,89 @@ var frames uint32
100
100
101
101
// Here goes our drawing code
102
102
func drawGLScene () {
103
- // Clear the screen and depth buffer
104
- gl .Clear (gl .COLOR_BUFFER_BIT | gl .DEPTH_BUFFER_BIT )
105
-
106
- // reset the view
107
- gl .LoadIdentity ()
108
-
109
- // Draw to the screen
110
- sdl .GL_SwapBuffers ()
111
-
112
- // Gather our frames per second
113
- frames ++
114
- t := sdl .GetTicks ()
115
- if t - t0 >= 5000 {
116
- seconds := (t - t0 ) / 1000.0
117
- fps := frames / seconds
118
- fmt .Println (frames , "frames in" , seconds , "seconds =" , fps , "FPS" )
119
- t0 = t
120
- frames = 0
121
- }
103
+ // Clear the screen and depth buffer
104
+ gl .Clear (gl .COLOR_BUFFER_BIT | gl .DEPTH_BUFFER_BIT )
105
+
106
+ // reset the view
107
+ gl .LoadIdentity ()
108
+
109
+ // Draw to the screen
110
+ sdl .GL_SwapBuffers ()
111
+
112
+ // Gather our frames per second
113
+ frames ++
114
+ t := sdl .GetTicks ()
115
+ if t - t0 >= 5000 {
116
+ seconds := (t - t0 ) / 1000.0
117
+ fps := frames / seconds
118
+ fmt .Println (frames , "frames in" , seconds , "seconds =" , fps , "FPS" )
119
+ t0 = t
120
+ frames = 0
121
+ }
122
122
}
123
123
124
124
func main () {
125
- // Initialize SDL
126
- if sdl .Init (sdl .INIT_VIDEO ) < 0 {
127
- panic ("Video initialization failed: " + sdl .GetError ())
128
- }
129
-
130
- // flags to pass to sdl.SetVideoMode
131
- videoFlags := sdl .OPENGL // Enable OpenGL in SDL
132
- videoFlags |= sdl .DOUBLEBUF // Enable double buffering
133
- videoFlags |= sdl .HWPALETTE // Store the palette in hardware
134
- // FIXME: this causes segfault.
135
- // videoFlags |= sdl.RESIZABLE // Enable window resizing
136
-
137
- // get a SDL surface
138
- surface = sdl .SetVideoMode (SCREEN_WIDTH , SCREEN_HEIGHT , SCREEN_BPP , uint32 (videoFlags ))
139
-
140
- // verify there is a surface
141
- if surface == nil {
142
- panic ("Video mode set failed: " + sdl .GetError ())
143
- Quit (1 )
144
- }
145
-
146
- // When this function is finished, clean up and exit.
147
- defer Quit (0 )
148
-
149
- // Sets up OpenGL double buffering
150
- sdl .GL_SetAttribute (sdl .GL_DOUBLEBUFFER , 1 )
151
-
152
- // Execute everything needed for OpenGL
153
- initGL ()
154
-
155
- // Resize the initial window
156
- resizeWindow (SCREEN_WIDTH , SCREEN_HEIGHT )
157
-
158
- // wait for events
159
- running := true
160
- isActive := true
161
- for running {
162
- for ev := sdl .PollEvent (); ev != nil ; ev = sdl .PollEvent () {
163
- switch e := ev .(type ) {
164
- case * sdl.ActiveEvent :
165
- isActive = e .Gain != 0
166
- case * sdl.ResizeEvent :
167
- width , height := int (e .W ), int (e .H )
168
- surface = sdl .SetVideoMode (width , height , SCREEN_BPP , uint32 (videoFlags ))
169
-
170
- if surface == nil {
171
- fmt .Println ("Could not get a surface after resize:" , sdl .GetError ())
172
- Quit (1 )
173
- }
174
- resizeWindow (width , height )
175
- case * sdl.KeyboardEvent :
176
- if (e .Type == sdl .KEYDOWN ) {
177
- handleKeyPress (e .Keysym )
178
- }
179
- case * sdl.QuitEvent :
180
- running = false
181
- }
182
- }
183
-
184
- // draw the scene
185
- if isActive {
186
- drawGLScene ()
187
- }
188
- }
125
+ // Initialize SDL
126
+ if sdl .Init (sdl .INIT_VIDEO ) < 0 {
127
+ panic ("Video initialization failed: " + sdl .GetError ())
128
+ }
129
+
130
+ // flags to pass to sdl.SetVideoMode
131
+ videoFlags := sdl .OPENGL // Enable OpenGL in SDL
132
+ videoFlags |= sdl .DOUBLEBUF // Enable double buffering
133
+ videoFlags |= sdl .HWPALETTE // Store the palette in hardware
134
+ videoFlags |= sdl .RESIZABLE // Enable window resizing
135
+
136
+ // get a SDL surface
137
+ surface = sdl .SetVideoMode (SCREEN_WIDTH , SCREEN_HEIGHT , SCREEN_BPP , uint32 (videoFlags ))
138
+
139
+ // verify there is a surface
140
+ if surface == nil {
141
+ panic ("Video mode set failed: " + sdl .GetError ())
142
+ Quit (1 )
143
+ }
144
+
145
+ // When this function is finished, clean up and exit.
146
+ defer Quit (0 )
147
+
148
+ // Sets up OpenGL double buffering
149
+ sdl .GL_SetAttribute (sdl .GL_DOUBLEBUFFER , 1 )
150
+
151
+ // Execute everything needed for OpenGL
152
+ initGL ()
153
+
154
+ // Resize the initial window
155
+ resizeWindow (SCREEN_WIDTH , SCREEN_HEIGHT )
156
+
157
+ // wait for events
158
+ running := true
159
+ isActive := true
160
+ for running {
161
+ for ev := sdl .PollEvent (); ev != nil ; ev = sdl .PollEvent () {
162
+ switch e := ev .(type ) {
163
+ case * sdl.ActiveEvent :
164
+ isActive = e .Gain != 0
165
+ case * sdl.ResizeEvent :
166
+ width , height := int (e .W ), int (e .H )
167
+ surface = sdl .SetVideoMode (width , height , SCREEN_BPP , uint32 (videoFlags ))
168
+
169
+ if surface == nil {
170
+ fmt .Println ("Could not get a surface after resize:" , sdl .GetError ())
171
+ Quit (1 )
172
+ }
173
+ resizeWindow (width , height )
174
+ case * sdl.KeyboardEvent :
175
+ if e .Type == sdl .KEYDOWN {
176
+ handleKeyPress (e .Keysym )
177
+ }
178
+ case * sdl.QuitEvent :
179
+ running = false
180
+ }
181
+ }
182
+
183
+ // draw the scene
184
+ if isActive {
185
+ drawGLScene ()
186
+ }
187
+ }
189
188
}
0 commit comments