alltom-glapp 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,12 +2,8 @@ h1. GLApp - A tiny wrapper for ruby-opengl
2
2
 
3
3
  It's simple:
4
4
 
5
- # require "gl_app"
6
- # Include GLApp::Engine
7
-
8
- <pre><code>class MyGame
9
- include GLApp::Engine
10
- end</code></pre>
5
+ # require "glapp"
6
+ # Include GLApp
11
7
  # Override as many of the callbacks as you need:
12
8
 
13
9
  * setup
@@ -20,24 +16,25 @@ end</code></pre>
20
16
  * mouse_click(button, state, x, y)
21
17
  * mouse_dragging_motion(x, y)
22
18
  * mouse_passive_motion(x, y)
23
- # Instantiate GLApp with an instance of your engine, width, height and title, and do show.
19
+ # If you need more control, override the default behavior of these:
24
20
 
25
- <pre><code>app = GLApp.new(MyGame.new, 800, 600, "My game")
26
- app.show</code></pre>
27
- # Done!
21
+ * setup_context
22
+ * pre_draw
23
+ * post_draw
24
+ * resize
25
+ # Call show with width, height, title, and whether to be full-screen
28
26
 
29
27
  Look at the scripts in the examples/ directory.
30
28
  In fact, here's one:
31
29
 
32
- require 'gl_app'
30
+ require 'glapp'
33
31
 
34
32
  class MyGame
35
- include GLApp::Engine
33
+ include GLApp
36
34
  def draw
37
35
  glTranslate(0, 0, -5)
38
36
  glutSolidCube(2)
39
37
  end
40
38
  end
41
39
 
42
- app = GLApp.new(MyGame.new, 800, 600, "My game")
43
- app.show
40
+ MyGame.new.show 800, 600, "My Game"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.0.5
@@ -1,8 +1,7 @@
1
1
  require "rubygems"
2
2
  require "glapp"
3
3
 
4
- include GLApp::Engine
5
- public
4
+ include GLApp
6
5
 
7
6
  class Texture
8
7
  attr_reader :num, :width, :height
@@ -157,15 +156,15 @@ def draw
157
156
  # change coordinate system to match screen pixels
158
157
  glMatrixMode(GL_PROJECTION)
159
158
  glLoadIdentity
160
- glOrtho(0, 300, 300, 0, -1000, 1000)
159
+ glOrtho(0, width, height, 0, -1000, 1000)
161
160
  glMatrixMode(GL_MODELVIEW)
162
161
  glLoadIdentity
163
-
162
+
164
163
  # draw a hedgehog in the center of the screen
165
164
  glPushMatrix
166
- glTranslate(150, 150, 0)
165
+ glTranslate(width/2, height/2, 0)
167
166
  @sprites[@animator.frame].render
168
167
  glPopMatrix
169
168
  end
170
169
 
171
- GLApp.new(self, 300, 300).show
170
+ show 300, 300, "sprite demo"
@@ -34,7 +34,7 @@ class Triangle
34
34
  end
35
35
 
36
36
  class TriangleDemo
37
- include GLApp::Engine
37
+ include GLApp
38
38
 
39
39
  def setup
40
40
  @triangles = Triangle.boom(10)
@@ -53,5 +53,4 @@ class TriangleDemo
53
53
  end
54
54
  end
55
55
 
56
- app = GLApp.new(TriangleDemo.new, 800, 300, "Triangle demo")
57
- app.show
56
+ TriangleDemo.new.show 800, 300, "triangle demo"
@@ -4,8 +4,7 @@
4
4
  require "rubygems"
5
5
  require "glapp"
6
6
 
7
- include GLApp::Engine
8
- public
7
+ include GLApp
9
8
 
10
9
  class Triangle
11
10
  attr_accessor :angle
@@ -53,4 +52,4 @@ end
53
52
 
54
53
  @triangles = Triangle.boom(10)
55
54
 
56
- GLApp.new(self, 800, 300).show
55
+ show 800, 300, "triangle demo"
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{glapp}
5
- s.version = "0.0.4"
5
+ s.version = "0.0.5"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Tom Lieber"]
@@ -1,58 +1,106 @@
1
1
  require 'rubygems'
2
2
  require 'opengl'
3
3
 
4
- class GLApp
5
- def initialize(engine, width, height, title = "")
6
- @engine = engine
4
+ module GLApp
5
+ attr_reader :width, :height, :title
6
+
7
+ def show(width, height, title = "glapp", fullscreen = false)
8
+ glutInit
9
+ glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
10
+
7
11
  @width = width
8
12
  @height = height
9
13
  @title = title
10
- @running = false
11
- self.gl_init
14
+
15
+ fullscreen ? go_fullscreen : go_windowed
16
+ setup
17
+
18
+ setup_context
19
+ wire
20
+ glutMainLoop
12
21
  end
22
+
23
+ def go_windowed
24
+ if glutGameModeGet(GLUT_GAME_MODE_ACTIVE) != 0
25
+ glutLeaveGameMode
26
+ end
13
27
 
14
- def update(seconds)
15
- @engine.update(seconds)
28
+ unless @window
29
+ glutInitWindowSize(width, height)
30
+ @window = glutCreateWindow(title)
31
+ end
16
32
  end
17
33
 
18
- def draw
19
- @engine.draw
34
+ def go_fullscreen
35
+ glutGameModeString([width, height].join("x"))
36
+
37
+ if glutGameModeGet(GLUT_GAME_MODE_POSSIBLE)
38
+ glutEnterGameMode
39
+ if glutGameModeGet(GLUT_GAME_MODE_ACTIVE) == 0
40
+ go_windowed
41
+ end
42
+ else
43
+ go_windowed
44
+ end
20
45
  end
46
+
47
+ # begin hooks
21
48
 
22
- def keyboard(key, modifiers)
23
- @engine.keyboard_down(key, modifiers)
49
+ def setup_context
50
+ glEnable(GL_DEPTH_TEST)
51
+ glEnable(GL_BLEND)
52
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
53
+ glutIgnoreKeyRepeat(1)
54
+ end
55
+
56
+ def setup
24
57
  end
58
+
59
+ def pre_draw
60
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
61
+ glMatrixMode(GL_PROJECTION)
62
+ glLoadIdentity
63
+ gluPerspective(30.0, width / height, 0.1, 1000.0)
25
64
 
65
+ glMatrixMode(GL_MODELVIEW)
66
+ glLoadIdentity
67
+ end
68
+
69
+ def draw
70
+ end
71
+
72
+ def post_draw
73
+ glutSwapBuffers
74
+ end
75
+
76
+ def update(seconds)
77
+ end
78
+
79
+ def keyboard_down(key, modifiers)
80
+ end
81
+
26
82
  def keyboard_up(key, modifiers)
27
- @engine.keyboard_up(key, modifiers)
28
83
  end
29
-
30
- def special_keyboard(key, modifiers)
31
- @engine.special_keyboard_down(key, modifiers)
84
+
85
+ def special_keyboard_down(key, modifiers)
32
86
  end
33
-
87
+
34
88
  def special_keyboard_up(key, modifiers)
35
- @engine.special_keyboard_up(key, modifiers)
36
89
  end
37
-
90
+
38
91
  def mouse_click(button, state, x, y)
39
- @engine.mouse_click(button, state, x, y)
40
92
  end
41
-
42
- def mouse_active_motion(x, y)
43
- @engine.mouse_dragging_motion(x, y)
44
- @engine.mouse_motion(x, y)
93
+
94
+ def mouse_dragging_motion(x, y)
45
95
  end
46
-
96
+
47
97
  def mouse_passive_motion(x, y)
48
- @engine.mouse_passive_motion(x, y)
49
- @engine.mouse_motion(x, y)
50
98
  end
51
-
52
- def resize(width, height)
53
- # avoid divide-by-zero
54
- height = 1.0 if height <= 0
55
-
99
+
100
+ def mouse_motion(x, y)
101
+ end
102
+
103
+ def resize
56
104
  # Reset the coordinate system
57
105
  glMatrixMode(GL_PROJECTION)
58
106
  glLoadIdentity
@@ -67,72 +115,15 @@ class GLApp
67
115
  gluLookAt(0, 0, 5,
68
116
  0, 0, -1,
69
117
  0, 1, 0)
70
-
71
- @width, @height = width, height
72
118
  end
73
-
74
- def show
75
- if glutGameModeGet(GLUT_GAME_MODE_ACTIVE) != 0
76
- glutLeaveGameMode
77
- end
78
-
79
- unless @window
80
- glutInitWindowSize(@width, @height)
81
- @window = glutCreateWindow(@title)
82
- end
83
-
84
- self.setup_context
85
- self.go unless running?
86
- end
87
-
88
- protected
89
-
90
- def go_fullscreen(width, height, title = "")
91
- glutGameModeString([width, height].join("x"))
92
-
93
- if glutGameModeGet(GLUT_GAME_MODE_POSSIBLE)
94
- glutEnterGameMode
95
- if glutGameModeGet(GLUT_GAME_MODE_ACTIVE) == 0
96
- self.go_windowed
97
- end
98
- else
99
- go_windowed
100
- end
101
-
102
- self.setup_context
103
- self.go unless self.running?
104
- end
105
-
106
- def go
107
- @running = true
108
- @engine.setup
109
- glutMainLoop
110
- end
111
-
112
- def running?
113
- @running
114
- end
115
-
116
- def gl_init
117
- glutInit
118
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
119
- end
120
-
121
- def setup_context
122
- glEnable(GL_DEPTH_TEST)
123
-
124
- glutIgnoreKeyRepeat(1)
125
-
119
+
120
+ # end hooks
121
+
122
+ def wire
126
123
  glutDisplayFunc(lambda do
127
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
128
- glMatrixMode(GL_PROJECTION)
129
- glLoadIdentity
130
- gluPerspective(30.0, @width / @height, 0.1, 1000.0)
131
-
132
- glMatrixMode(GL_MODELVIEW)
133
- glLoadIdentity
124
+ pre_draw
134
125
  draw
135
- glutSwapBuffers
126
+ post_draw
136
127
  end)
137
128
 
138
129
  glutIdleFunc(lambda do
@@ -145,7 +136,7 @@ class GLApp
145
136
  end)
146
137
 
147
138
  glutKeyboardFunc(lambda do |key, x, y|
148
- keyboard(key, glutGetModifiers)
139
+ keyboard_down(key, glutGetModifiers)
149
140
  end)
150
141
 
151
142
  glutKeyboardUpFunc(lambda do |key, x, y|
@@ -153,7 +144,7 @@ class GLApp
153
144
  end)
154
145
 
155
146
  glutSpecialFunc(lambda do |key, x, y|
156
- special_keyboard(key, glutGetModifiers)
147
+ special_keyboard_down(key, glutGetModifiers)
157
148
  end)
158
149
 
159
150
  glutSpecialUpFunc(lambda do |key, x, y|
@@ -165,33 +156,19 @@ class GLApp
165
156
  end)
166
157
 
167
158
  glutMotionFunc(lambda do |x, y|
168
- mouse_active_motion(x, y)
159
+ mouse_dragging_motion(x, y)
160
+ mouse_motion(x, y)
169
161
  end)
170
162
 
171
163
  glutPassiveMotionFunc(lambda do |x, y|
172
164
  mouse_passive_motion(x, y)
165
+ mouse_motion(x, y)
173
166
  end)
174
167
 
175
168
  glutReshapeFunc(lambda do |width, height|
176
- resize(width, height)
169
+ @width = width
170
+ @height = height
171
+ resize
177
172
  end)
178
-
179
- glEnable(GL_BLEND)
180
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
181
- end
182
-
183
- module Engine
184
- def setup() end
185
- def update(seconds) end
186
- def draw() end
187
- def keyboard_down(key, modifiers) end
188
- def keyboard_up(key, modifiers) end
189
- def special_keyboard_down(key, modifiers) end
190
- def special_keyboard_up(key, modifiers) end
191
- def mouse_click(button, state, x, y) end
192
- def mouse_dragging_motion(x, y) end
193
- def mouse_passive_motion(x, y) end
194
- def mouse_motion(x, y) end
195
173
  end
196
174
  end
197
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alltom-glapp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Lieber