Removed all code that uses OpenGL from Image.
[io/quag.git] / addons / OpenGL / samples / game.io
blob204b6afe1dbb4b82b20b3c446e7d1d50c6c470be
1 #!/usr/bin/env io
3 # game.io - A simple ship flying around a window
4 # (C) 2002 Mike Austin
5 # Updated by Steve Dekorte
6 # Significant cleanup and simplification by Jonathan Wright - Oct 2006
9 appendProto(OpenGL)
11 Ship := Object clone do(
12 newSlot("location")
13 newSlot("velocity")
14 newSlot("impulse")
15 newSlot("angle", 180)
17 newSlot("thrust", 0.05)
19 newSlot("turningLeft", false)
20 newSlot("turningRight", false)
21 newSlot("thrusting", false)
23 newSlot("screenSize")
25 init := method(
26 # Reuse the vectors so we don't put presure on the garbage collector
27 location = vector(0, 0, 0)
28 velocity = vector(0, 0, 0)
29 impulse = vector(0, 0, 0)
32 draw := method(
33 glPushMatrix
35 location glTranslate
36 glRotated(angle, 0, 0, 1)
38 glColor4d(1, 1, 1, 1)
39 glScaled(5, 5, 0)
41 # nice anti-aliased outline with rounded corners due to the
42 # glEnable(GL_LINE_SMOOTH) and glLineWidth(2)
43 glBegin(GL_LINE_LOOP)
44 glVertex3d(0,-3, 0)
45 glVertex3d(-2, 2, 0)
46 glVertex3d(2, 2, 0)
47 glEnd
49 # fill in the outline
50 glBegin(GL_POLYGON)
51 glVertex3d(0,-3, 0)
52 glVertex3d(-2, 2, 0)
53 glVertex3d(2, 2, 0)
54 glEnd
56 glPopMatrix
59 update := method(
60 if(turningLeft,
61 angle = angle + 5
64 if(turningRight,
65 angle = angle - 5
68 angle = if(angle < 0, angle + 360, angle % 360)
70 if(thrusting,
71 a := angle * (Number constants pi / 180)
72 velocity += impulse set(a sin * thrust, a cos * -thrust, 0)
75 location += velocity
77 wrapLocation
80 centerOnScreen := method(
81 location = screenSize / 2
84 wrapLocation := method(
85 if (location x > screenSize x, location setX(0))
86 if (location x < 0, location setX(screenSize x))
88 if (location y > screenSize y, location setY(0))
89 if (location y < 0, location setY(screenSize y))
93 Screen := Object clone do(
94 newSlot("ship")
96 newSlot("size") # set by the reshape callback
97 newSlot("scale")
99 init := method(
100 size = vector(0, 0)
101 ship = Ship clone setScreenSize(size)
104 width := method(size x)
105 height := method(size y)
107 docSlot("reshape", "Reshape callback for glut.")
108 reshape := method(w, h,
109 size set(w, h)
110 scale = vector(2,2) / size
112 ship centerOnScreen
114 glViewport(0, 0, w, h)
115 glMatrixMode(GL_PROJECTION)
116 glLoadIdentity
117 gluOrtho2D(-1, -1, 1, 1)
118 glMatrixMode(GL_MODELVIEW)
119 glLoadIdentity
122 docSlot("display", "Display callback for glut.")
123 display := method(
124 glClear(GL_COLOR_BUFFER_BIT)
125 glLoadIdentity
126 glTranslated(-1, -1, 0)
127 scale glScale
129 ship draw
131 glFlush
132 glutSwapBuffers
135 docSlot("timer", "Timer callback for glut.")
136 timer := method(v,
137 glutTimerFunc(10, 0)
139 ship update
141 self glutPostRedisplay
144 docSlot("keyboard", "Key down callback for glut.")
145 keyboard := method(key, x, y,
146 if(key asCharacter == "4", ship setTurningLeft(true))
147 if(key asCharacter == "6", ship setTurningRight(true))
148 if(key asCharacter == "8", ship setThrusting(true))
151 docSlot("keyboardUp", "Key up callback for glut.")
152 keyboardUp := method(key, x, y,
153 if(key asCharacter == "4", ship setTurningLeft(false))
154 if(key asCharacter == "6", ship setTurningRight(false))
155 if(key asCharacter == "8", ship setThrusting(false))
159 docSlot("run", "Sets up GLUT, creates the window and sets a few of the opengl defaults.")
160 run := method(
161 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
162 glutInitWindowPosition(200, 100)
163 glutInitWindowSize(512, 512)
164 glutInit
165 glutCreateWindow("Io Ship")
167 glutEventTarget(Screen clone)
168 glutReshapeFunc
169 glutDisplayFunc
170 glutKeyboardFunc
171 glutKeyboardUpFunc
172 glutTimerFunc(0, 0)
174 glClearColor(0, 0, 0, 1)
175 glutIgnoreKeyRepeat(1)
177 glEnable(GL_BLEND)
178 glEnable(GL_LINE_SMOOTH)
179 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
180 glLineWidth(2)
182 glutMainLoop
185 writeln("Controls\n forward keypad 8\n left keypad 4\n right keypad 6")
188 # vim:ts=4:sts=4:sw=4:et: