Changeset 81
- Timestamp:
- 08/03/08 16:08:01 (4 months ago)
- Files:
-
- trunk/bin/yage3d.exe (modified) (previous)
- trunk/src/demo1/gameobj.d (modified) (1 diff)
- trunk/src/demo1/main.d (modified) (1 diff)
- trunk/src/demo2/main.d (modified) (10 diffs)
- trunk/src/demo2/ship.d (modified) (2 diffs)
- trunk/src/yage/candy.ddoc (modified) (1 diff)
- trunk/src/yage/core/array.d (modified) (4 diffs)
- trunk/src/yage/core/color.d (modified) (4 diffs)
- trunk/src/yage/core/matrix.d (modified) (3 diffs)
- trunk/src/yage/core/quatrn.d (modified) (2 diffs)
- trunk/src/yage/core/timer.d (modified) (4 diffs)
- trunk/src/yage/core/tree.d (modified) (4 diffs)
- trunk/src/yage/core/vector.d (modified) (10 diffs)
- trunk/src/yage/gui/style.d (modified) (1 diff)
- trunk/src/yage/gui/surface.d (modified) (8 diffs)
- trunk/src/yage/resource/all.d (modified) (1 diff)
- trunk/src/yage/resource/exception.d (added)
- trunk/src/yage/resource/font.d (added)
- trunk/src/yage/resource/image.d (modified) (4 diffs)
- trunk/src/yage/resource/material.d (modified) (12 diffs)
- trunk/src/yage/resource/model.d (modified) (9 diffs)
- trunk/src/yage/resource/ms3dloader.d (modified) (16 diffs)
- trunk/src/yage/resource/resource.d (modified) (8 diffs)
- trunk/src/yage/resource/sound.d (modified) (1 diff)
- trunk/src/yage/resource/texture.d (modified) (12 diffs)
- trunk/src/yage/scene/camera.d (modified) (4 diffs)
- trunk/src/yage/scene/model.d (modified) (4 diffs)
- trunk/src/yage/scene/scene.d (modified) (1 diff)
- trunk/src/yage/scene/sound.d (modified) (1 diff)
- trunk/src/yage/scene/terrain.d (modified) (1 diff)
- trunk/src/yage/system/all.d (modified) (1 diff)
- trunk/src/yage/system/constant.d (modified) (1 diff)
- trunk/src/yage/system/device.d (modified) (17 diffs)
- trunk/src/yage/system/input.d (modified) (8 diffs)
- trunk/src/yage/system/log.d (modified) (3 diffs)
- trunk/src/yage/system/playable.d (added)
- trunk/src/yage/system/render.d (modified) (6 diffs)
- trunk/src/yage/system/rendertarget.d (added)
- trunk/src/yage/util/flyer.d (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/demo1/gameobj.d
r73 r81 67 67 if (timer is null) 68 68 { timer = new Timer(); 69 timer.set(1); 69 timer.set(1); 70 70 } 71 71 trunk/src/demo1/main.d
r75 r81 124 124 Timer delta = new Timer(); 125 125 Log.write("Starting rendering loop."); 126 std.gc.fullCollect(); 126 std.gc.fullCollect(); 127 127 while(1) 128 128 { trunk/src/demo2/main.d
r73 r81 23 23 int main() 24 24 { 25 /*26 Object[] array1;27 Object[uint] array2;28 29 Timer a = new Timer();30 for (int j=0; j<100; j++)31 for (int i=0; i<1000; i++)32 array1 ~= new Object();33 writefln("array create: " ~ a.toString());34 35 36 a.reset();37 for (int j=0; j<100; j++)38 for (int i=0; i<1000; i++)39 {};40 writefln("array loop: " ~ a.toString());41 42 a.reset();43 for (int j=0; j<100; j++)44 for (int i=0; i<1000; i++)45 { Object b = new Object;46 array2[b.toHash()] = b;47 }48 writefln("aa create: " ~ a.toString());49 //array2.rehash;50 51 Timer c = new Timer();52 for (int j=0; j<100; j++)53 foreach (i; array2)54 {}55 writefln("aa loop: " ~ c.toString());56 57 return 0;58 */59 60 25 // Init (resolution, depth, fullscreen, aa-samples) 61 26 Device.init(800, 600, 32, false, 1); 27 //Device.init(1024, 768, 32, true); 28 //Device.init(1440, 900, 32, true); 62 29 63 30 // Paths … … 73 40 Scene scene = new Scene(); 74 41 scene.start(60); // update 60 times per second 75 76 42 Device.onExit = &scene.stop; 77 43 … … 92 58 camera.setView(2, 20000, 60, 0, 1); // wide angle view 93 59 94 Surface bg = new Surface(null); 95 bg.setTexture(camera.getTexture()); 96 bg.topLeft = Vec2f(0,0); 97 bg.bottomRight = Vec2f(1, 1); 98 bg.setVisibility(true); 99 100 void onMousedown(Surface self, byte buttons, Vec2i coordinates){ 101 self.grabMouse(!ship.input); 102 ship.input = !ship.input; 103 } 104 105 void onMousemove(Surface self, byte buttons, Vec2i rel){ 106 if(ship.input){ 107 ship.mouseDelta = ship.mouseDelta.add(rel); 108 } 109 } 110 111 void onResize(Surface self){ 112 camera.setResolution(self.size.x, self.size.y); 113 writefln("Camera resolution changed to ", self.size.x, " x ", self.size.y); 114 } 115 116 void onKeydown(Surface self, byte key){ 60 61 // Main surface where camera output is rendered. 62 Surface view = new Surface(null); 63 view.style.backgroundMaterial = camera.getTexture(); 64 view.style.set("bottom: 0; right: 0"); 65 Device.setSurface(view); 66 67 // Events for main surface. 68 view.onKeyDown = delegate void (Surface self, byte key){ 117 69 if (key == SDLK_ESCAPE) 118 70 Device.exit(0); 119 71 120 if (key == SDLK_SPACE) {121 Flare flare = new Flare(ship.getScene());72 if (key == SDLK_SPACE) 73 { Flare flare = new Flare(ship.getScene()); 122 74 flare.setPosition(ship.getAbsolutePosition()); 123 75 flare.setVelocity(Vec3f(0, 0, -150).rotate(ship.ship.getAbsoluteTransform())+ship.getVelocity()); … … 130 82 } 131 83 132 if(key == SDLK_c) {133 std.gc.fullCollect();84 if(key == SDLK_c) // Perform garbage collection, which used to crash. 85 { std.gc.fullCollect(); 134 86 writefln("garbage collected"); 135 87 } 136 } 137 138 bg.onMousedown = &onMousedown; 139 bg.onResize = &onResize; 140 bg.onMousemove = &onMousemove; 141 bg.onKeydown = &onKeydown; 142 143 GPUTexture active = new GPUTexture("test/clear.png"); 144 GPUTexture inactive = new GPUTexture("test/clearInactive.png"); 145 GPUTexture inactive2 = new GPUTexture("test/clearInactive2.png"); 146 147 void onMousedown2(Surface self, byte buttons, Vec2i coordinates){ 88 }; 89 view.onMouseDown = delegate void (Surface self, byte buttons, Vec2i coordinates){ 90 self.grabMouse(!ship.input); 91 ship.input = !ship.input; 92 }; 93 view.onMouseMove = delegate void (Surface self, byte buttons, Vec2i rel){ 94 if(ship.input) 95 ship.mouseDelta = ship.mouseDelta + rel; 96 }; 97 view.onResize = delegate void (Surface self, Vec2f amount){ 98 camera.setResolution(cast(int)self.width, cast(int)self.height); 99 }; 100 101 102 103 // Create some windows 104 void onMouseDown2(Surface self, byte buttons, Vec2i coordinates){ 148 105 self.raise(); 149 self.startDrag(); 150 } 151 void onMouseup2(Surface self, byte buttons, Vec2i coordinates){ 152 self.endDrag(); 153 } 154 void onMousemove2(Surface self, byte buttons, Vec2i diff){ 155 if(buttons == 1) self.drag(diff); 156 } 157 void onMouseenter(Surface self, byte buttons, Vec2i coordinates){ 158 self.setTexture(active); 159 } 160 void onMouseleave(Surface self, byte buttons, Vec2i coordinates){ 161 self.setTexture(inactive2); 162 } 163 164 Surface clear = new Surface(bg); 165 clear.setTexture(inactive2); 166 clear.topLeft = Vec2f(.65,0); 167 clear.bottomRight = Vec2f(1, .25); 168 clear.fill = stretched; 169 clear.setVisibility(true); 170 clear.onMousedown = &onMousedown2; 171 clear.onMousemove = &onMousemove2; 172 clear.onMouseup = &onMouseup2; 173 clear.onMouseenter = &onMouseenter; 174 clear.onMouseleave = &onMouseleave; 175 176 Surface clear2 = new Surface(clear); 177 clear2.setTexture(inactive2); 178 clear2.topLeft = Vec2f(.65,0); 179 clear2.bottomRight = Vec2f(1, .25); 180 clear2.fill = stretched; 181 clear2.setVisibility(true); 182 clear2.onMousedown = &onMousedown2; 183 clear2.onMousemove = &onMousemove2; 184 clear2.onMouseup = &onMouseup2; 185 clear2.onMouseenter = &onMouseenter; 186 clear2.onMouseleave = &onMouseleave; 187 188 Surface clear3 = new Surface(bg); 189 clear3.setTexture(inactive2); 190 clear3.topLeft = Vec2f(.65,0); 191 clear3.bottomRight = Vec2f(1, .25); 192 clear3.fill = stretched; 193 clear3.setVisibility(true); 194 clear3.onMousedown = &onMousedown2; 195 clear3.onMousemove = &onMousemove2; 196 clear3.onMouseup = &onMouseup2; 197 clear3.onMouseenter = &onMouseenter; 198 clear3.onMouseleave = &onMouseleave; 199 200 Surface clear4 = new Surface(bg); 201 clear4.setTexture(inactive2); 202 clear4.topLeft = Vec2f(.4,0); 203 clear4.bottomRight = Vec2f(1, .25); 204 clear4.fill = stretched; 205 clear4.setVisibility(true); 206 clear4.onMousedown = &onMousedown2; 207 clear4.onMousemove = &onMousemove2; 208 clear4.onMouseup = &onMouseup2; 209 clear4.onMouseenter = &onMouseenter; 210 clear4.onMouseleave = &onMouseleave; 211 212 Surface clear5 = new Surface(bg); 213 clear5.setTexture(inactive); 214 clear5.topLeft = Vec2f(.65,0); 215 clear5.bottomRight = Vec2f(1, .4); 216 clear5.fill = stretched; 217 clear5.setVisibility(true); 218 clear5.onMousedown = &onMousedown2; 219 clear5.onMousemove = &onMousemove2; 220 clear5.onMouseup = &onMouseup2; 221 clear5.onMouseenter = &onMouseenter; 222 clear5.onMouseleave = &onMouseleave; 106 self.focus(); 107 } 108 void onMouseUp2(Surface self, byte buttons, Vec2i coordinates){ 109 self.blur(); 110 } 111 void onMouseMove2(Surface self, byte buttons, Vec2i diff){ 112 if(buttons == 1) 113 self.move(cast(Vec2f)diff, true); 114 } 115 void onMouseOver(Surface self, byte buttons, Vec2i coordinates){ 116 self.style.set("background-material: url('gui/skin/clear3.png')"); 117 } 118 void onMouseOut(Surface self, byte buttons, Vec2i coordinates){ 119 self.style.set("background-material: url('gui/skin/clear2.png')"); 120 } 121 122 auto window1 = new Surface(view); 123 window1.style.set("top: 0; right: 0; width: 400; height: 60; background-position: 5px 5px; " ~ 124 "background-repeat: nineslice; background-material: url('gui/skin/clear2.png')"); 125 window1.onMouseDown = &onMouseDown2; 126 window1.onMouseMove = &onMouseMove2; 127 window1.onMouseUp = &onMouseUp2; 128 //window1.onMouseOver = &onMouseOver; 129 //window1.onMouseOut = &onMouseOut; 130 131 auto window2 = new Surface(window1); 132 window2.style.set("top: 30; right: 0; width: 50; height: 30; background-position: 5px 5px; " ~ 133 "background-repeat: nineslice; background-material: url('gui/skin/clear2.png')"); 134 window2.onMouseDown = &onMouseDown2; 135 window2.onMouseMove = &onMouseMove2; 136 window2.onMouseUp = &onMouseUp2; 137 window2.onMouseOver = &onMouseOver; 138 window2.onMouseOut = &onMouseOut; 139 140 window1.style.fontFamily = Resource.font("gui/font/Vera.ttf"); 141 window1.style.fontSize = 12; 142 window1.style.color = Color("black"); 143 144 // Beast 145 146 auto beast = new ModelNode(scene); 147 beast.setModel("beast/beast.ms3d"); 148 beast.setPosition(Vec3f(0, 25, -1000)); 149 beast.setRotation(Vec3f(0, 1.507, 0)); 150 151 152 void animate(Node self) 153 { ModelNode node = cast(ModelNode)self; 154 node.getModel().animate(node.getScene().getStartTime()); 155 } 156 beast.onUpdate(&animate); 157 223 158 224 159 // Music 225 SoundNodemusic = new SoundNode(camera);160 auto music = new SoundNode(camera); 226 161 music.setSound("music/celery - pages.ogg"); 227 162 music.setLooping(true); … … 229 164 230 165 // Lights 231 LightNodel1 = new LightNode(scene);166 auto l1 = new LightNode(scene); 232 167 l1.setDiffuse(Color(1, .85, .7)); 233 168 l1.setLightRadius(7000); … … 235 170 236 171 // Star 237 SpriteNodestar = new SpriteNode(l1);172 auto star = new SpriteNode(l1); 238 173 star.setMaterial("space/star.xml"); 239 174 star.setSize(Vec3f(2500)); … … 242 177 auto planet = new ModelNode(scene); 243 178 planet.setModel("space/planet.ms3d"); 244 planet.setSize(Vec3f(60 0));179 planet.setSize(Vec3f(60)); 245 180 planet.setAngularVelocity(Vec3f(0, -0.01, 0)); 246 181 … … 253 188 } 254 189 scene.onUpdate(&update); 255 256 bg.recalculate();257 190 258 191 // Rendering / Input Loop … … 261 194 Timer delta = new Timer(); 262 195 Log.write("Starting rendering loop."); 263 std.gc.fullCollect(); 264 while(1){ 196 //std.gc.fullCollect(); 197 198 199 Timer total = new Timer(); 200 total.setBounds(3, 5.0); 201 202 while(1) 203 { 204 writefln(total); 205 265 206 float dtime = delta.get(); 266 207 delta.reset(); … … 268 209 Input.processInput(); 269 210 camera.toTexture(); 270 bg.render(); 271 211 view.render(); 272 212 273 213 // Print framerate 274 214 fps++; 275 215 if (frame.get()>=0.25f) 276 { char[] caption = formatString(" Yage Test (%.2f fps) (%d objects, %d polygons, %d vertices rendered)\0",216 { char[] caption = formatString("(%.2f fps) (%d objects, %d polygons, %d vertices)", 277 217 fps/frame.get(), camera.getNodeCount(), camera.getPolyCount(), camera.getVertexCount()); 278 SDL_WM_SetCaption( caption.ptr, null);279 //deletecaption;218 SDL_WM_SetCaption((caption ~"\0").ptr, null); 219 window1.text = caption; 280 220 frame.reset(); 281 221 fps = 0; trunk/src/demo2/ship.d
r71 r81 62 62 63 63 // Accelerate forward 64 if (Input.key down[SDLK_UP] || Input.keydown[SDLK_w])64 if (Input.keyDown[SDLK_UP] || Input.keyDown[SDLK_w]) 65 65 { 66 66 accelerate(Vec3f(0, 0, -speed).rotate(pitch.getTransform()).rotate(getTransform())); … … 69 69 70 70 // Accelerate left, right, and backward 71 if (Input.key down[SDLK_LEFT] || Input.keydown[SDLK_a])71 if (Input.keyDown[SDLK_LEFT] || Input.keyDown[SDLK_a]) 72 72 accelerate(Vec3f(-speed/6, 0, 0).rotate(pitch.getTransform()).rotate(getTransform())); 73 if (Input.key down[SDLK_RIGHT] || Input.keydown[SDLK_d])73 if (Input.keyDown[SDLK_RIGHT] || Input.keyDown[SDLK_d]) 74 74 accelerate(Vec3f(speed/6, 0, 0).rotate(pitch.getTransform()).rotate(getTransform())); 75 if (Input.key down[SDLK_DOWN] || Input.keydown[SDLK_s])75 if (Input.keyDown[SDLK_DOWN] || Input.keyDown[SDLK_s]) 76 76 accelerate(Vec3f(0, 0, speed/3).rotate(pitch.getTransform()).rotate(getTransform())); 77 77 trunk/src/yage/candy.ddoc
r35 r81 86 86 </table> 87 87 <div id="docfooter"> 88 Yage and all source files © 2005-200 7Eric Poggel<br>88 Yage and all source files © 2005-2008 Eric Poggel<br> 89 89 Documentation generated with <a href="http://www.dsource.org/projects/helix/wiki/CandyDoc"> 90 90 <img src="img/candydoc.gif" alt="CandyDoc" style="vertical-align:middle; top:-1px"></a> trunk/src/yage/core/array.d
r73 r81 69 69 70 70 71 /// Return the element with the m aximum value of an array.71 /// Return the element with the minimum or maximum value of an array. 72 72 T amax(T)(T[] array, ) 73 73 { T m = array[0]; … … 77 77 return m; 78 78 } 79 /// 79 /// ditto 80 80 T amax(T, K)(T[] array, K delegate(T elem) getKey) 81 81 { T m = array[0]; … … 86 86 return m; 87 87 } 88 T amax(T, K)(T[T] array, K delegate(T elem) getKey) 89 { T m; 90 K mk; 91 foreach (T a; array) 92 { m = a; 93 mk = getKey(a); 94 break; 95 } 96 foreach (T a; array) 97 { if (getKey(a)>mk) 98 m=a; 99 } 100 return m; 101 } 102 103 /// Return the minimum value of an array. 88 /// ditto 104 89 T amin(T)(T[] array) 105 90 { T m = array[0]; … … 109 94 return m; 110 95 } 96 /// ditto 97 T amin(T, K)(T[] array, K delegate(T elem) getKey) 98 { T m = array[0]; 99 K mk = getKey(array[0]); 100 foreach (T a; array) 101 if (getKey(a)<mk) 102 m=a; 103 return m; 104 } 105 111 106 112 107 /** trunk/src/yage/core/color.d
r68 r81 19 19 * so Color(0x6633ff00).hex == "00FF3366" 20 20 * All Colors default to transparent black. 21 * TODO: Convert to using four floats for better arithmetic 21 * TODO: Convert to using four floats for better arithmetic? 22 22 * 23 23 * Example: … … 31 31 struct Color 32 32 { 33 private staticreal frac = 1.0f/255;33 private const real frac = 1.0f/255; 34 34 35 35 union … … 136 136 case "yellow": return Color(0xFF00FFFF); 137 137 default: break; 138 } 139 138 } 139 140 // Allow hex colors to start with hash. 141 if (string[0] == '#') 142 string = string[1..length]; 143 140 144 // Append alpha to 6-digit hex string. 141 145 if (string.length == 6) 142 string ~= "FF"; 143 144 // Convert string one char at a atime.146 string ~= "FF"; // creates garbage! 147 148 // Convert string one char at a time. 145 149 Color result; 146 150 int digit; … … 210 214 unittest 211 215 { assert(Color.sizeof == 4); 212 216 213 217 // Test initializers 214 218 assert(Color([0, 102, 51, 255]).hex == "006633FF"); trunk/src/yage/core/matrix.d
r51 r81 277 277 } 278 278 279 /// Perform Matrix multiplication in reverse (since Matrix multiplication is not cummulative.) 279 /// Perform Matrix multiplication in reverse (since Matrix multiplication is not cummulative.) Is this backwards? 280 280 Matrix postMultiply(Matrix b) 281 281 { return *this*b; 282 282 } 283 283 284 /// 285 Vec3f position() 286 { return Vec3f(v[12..15]); 287 } 288 284 289 /// 285 290 void *ptr() … … 425 430 } 426 431 427 /// Set the rotation values of this Matrix from another Matrix 432 /// 433 void setPosition(Vec3f position) 434 { v[12..15] = position.v[0..3]; 435 } 436 437 /// Set the rotation values of this Matrix from another Matrix or Quatrn. 428 438 void setRotation(Matrix rot) 429 439 { v[0..3] = rot.v[0..3]; 430 440 v[4..7] = rot.v[4..7]; 431 441 v[8..11] = rot.v[8..11]; 442 } 443 /// ditto 444 void setRotation(Quatrn rot) 445 { v[0] = 1-2*(rot.y*rot.y + rot.z*rot.z); 446 v[1] = 2*(rot.x*rot.y + rot.z*rot.w); 447 v[2] = 2*(rot.x*rot.z - rot.y*rot.w); 448 //v[3] = 0; 449 v[4] = 2*(rot.x*rot.y - rot.z*rot.w); 450 v[5] = 1-2*(rot.x*rot.x + rot.z*rot.z); 451 v[6] = 2*(rot.y*rot.z + rot.x*rot.w); 452 //v[7] = 0; 453 v[8] = 2*(rot.x*rot.z + rot.y*rot.w); 454 v[9] = 2*(rot.y*rot.z - rot.x*rot.w); 455 v[10] = 1-2*(rot.x*rot.x + rot.y*rot.y); 432 456 } 433 457 … … 467 491 return res; 468 492 } 469 470 493 471 494 /** Convert the rotation part of the Matrix to Euler angles. trunk/src/yage/core/quatrn.d
r51 r81 260 260 } 261 261 262 /// Set the rotation using a Vec3f of Euler angles. 262 /// Set the rotation using a Vec3f of Euler angles. TODO: replace with code below. 263 263 void setEuler(Vec3f euler) 264 264 { *this = … … 267 267 * Quatrn(0, 0, sin(euler.z*0.5), cos(euler.z*0.5)); // z 268 268 } 269 /* 270 inline void quatrn::fromvector(vector rot) 271 { 272 float hlf; 273 double sr, sp, sy, cr, cp, cy; 274 275 hlf = rot[2]*0.5f; 276 sy = sin(hlf); 277 cy = cos(hlf); 278 hlf = rot[1]*0.5f; 279 sp = sin(hlf); 280 cp = cos(hlf); 281 hlf = rot[0]*0.5f; 282 sr = sin(hlf); 283 cr = cos(hlf); 284 285 double crcp = cr*cp; 286 double srsp = sr*sp; 287 double srcp = sr*cp; 288 289 v[0] = float(srcp*cy- cr*sp*sy); 290 v[1] = float(cr*sp*cy+ srcp*sy); 291 v[2] = float(crcp*sy - srsp*cy); 292 v[3] = float(crcp*cy + srsp*sy); 293 }*/ 269 294 270 295 /** trunk/src/yage/core/timer.d
r32 r81 7 7 module yage.core.timer; 8 8 9 import std.c.math; 9 10 import std.perf; 10 11 import std.string; … … 29 30 { 30 31 protected bool paused = false; 31 //protected realmin = 0;32 //protected realmax = real.infinity;33 //protected realspeed = 1.0;32 protected double min = 0; 33 protected double max = real.infinity; 34 //protected double speed = 1.0; 34 35 protected ulong us = 0; // microsecond counter 35 36 protected PerformanceCounter hpc; … … 55 56 { hpc.stop(); 56 57 us += hpc.microseconds(); 57 real result = us*0.000001;58 58 hpc.start(); 59 59 } 60 return us*0.000001; 61 } } 60 } 61 double relative = us*0.000001; 62 63 real range = max-min; 64 if (relative > max) 65 relative = min + fmod(relative-min, range); 66 if (relative < min) 67 relative = max + fmod(relative-max, range); 68 69 return relative; 70 } 62 71 63 72 /// Is the Timer paused? … … 105 114 } 106 115 116 117 /** 118 * Set the timers rollunder and rollover values. 119 * Provide no arguments to reset them to the defaults of 0 and infinity. 120 * Params: 121 * min = The timer will never be less than this value. 122 * max = The timer will never be greater than this value and will rollover back to min after crossing it.*/ 123 void setBounds(double min=0, double max=double.infinity) 124 { this.min = min; 125 this.max = max; 126 } 127 128 107 129 char[] toString() 108 130 { return .toString(get()); trunk/src/yage/core/tree.d
r73 r81 17 17 * auto n = new Node(); 18 18 * n.addChild(new Node()); 19 * -------------------------------- */ 19 * -------------------------------- 20 */ 20 21 class Tree(T) 21 22 { 22 T parent; 23 T[] children; 24 int index=-1; 25 26 /// Ensure that child is removed from its parent. 27 // ~this() 28 // { remove(); 29 // } 30 23 protected T parent; // reference to parent 24 protected T[] children; // array of this element's children. 25 protected int index=-1; // index of this element in its parent's array, -1 if no parent. 26 31 27 /** 32 28 * Add a child element. 33 29 * Automatically detaches it from any other element's children. 34 * Returns: A self reference.*/ 30 * Params: 31 * child = 32 * Returns: A reference to the child. 33 */ 35 34 T addChild(T child) 36 35 in { … … 39 38 }body 40 39 { child.setParent(cast(T)this); 41 return c ast(T)this;40 return child; 42 41 } 43 42 … … 53 52 { return parent; 54 53 } 55 T setParent(T _parent) /// Ditto54 T setParent(T _parent) /// ditto 56 55 in { assert(_parent !is null); 57 56 }body … … 66 65 } 67 66 68 /// Is elem a child of this element? 67 /** 68 * Is elem a child of this element? 69 * This function will also return false if elem is null. */ 69 70 bool isChild(T elem) 70 { if ( elem.index < 0 || elem.index >= children.length)71 { if (!elem || elem.index < 0 || elem.index >= children.length) 71 72 return false; 72 73 return cast(bool)(children[elem.index] == elem); 73 74 } 74 75 75 /// Remove this element from its parent 76 /// Remove this element from its parent. 76 77 void remove() 77 78 { // this needs to happen because some children (like lights) may need to do more in their remove() function. trunk/src/yage/core/vector.d
r67 r81 41 41 } 42 42 43 invariant44 { foreach (T t; v)45 assert(t != float.nan);46 }47 48 43 /// Create a zero vector 49 44 static VST opCall() … … 72 67 VST res; 73 68 res.v[0..S] = s[0..S]; 74 return res;75 }76 77 ///78 VST add(VST s)79 { VST res;80 for (int i=0; i<v.length; i++)81 res.v[i] = v[i]+s.v[i];82 69 return res; 83 70 } … … 108 95 } 109 96 97 /// Is this Vector inside a box/cube/etc. defined by topLeft and bottomRight 98 bool inside(VST topLeft, VST bottomRight, bool inclusive=true) 99 { if (inclusive) 100 { for (int i=0; i<v.length; i++) 101 if (v[i] <= topLeft[i] || v[i] >= bottomRight[i]) 102 return false; 103 } else 104 { for (int i=0; i<v.length; i++) 105 if (v[i] < topLeft[i] || v[i] > bottomRight[i]) 106 return false; 107 } 108 return true; 109 } 110 110 111 /// Return the _length of the vector (the magnitude). 111 112 float length() … … 121 122 } 122 123 124 125 /// Allow for linear additions, subtractions, multiplcations, and divisions among Vectors of the same size and type. 126 VST opAdd(VST s) 127 { VST res; 128 for (int i=0; i<v.length; i++) 129 res.v[i] = v[i]+s.v[i]; 130 return res; 131 } 132 /// ditto 133 void opAddAssign(VST s) 134 { for (int i=0; i<v.length; i++) 135 v[i] += s.v[i]; 136 } 137 /// ditto 138 VST opSub(VST s) 139 { VST res; 140 for (int i=0; i<v.length; i++) 141 res.v[i] = v[i]-s.v[i]; 142 return res; 143 } 144 /// ditto 145 void opSubAssign(VST s) 146 { for (int i=0; i<v.length; i++) 147 v[i] -= s.v[i]; 148 } 149 150 /// Allow for additions, subtractions, multiplcations, and divisions where a scalar is applied to each vector component. 151 void opMulAssign(float s) 152 { for (int i=0; i<v.length; i++) 153 v[i] += s; 154 } 155 /// ditto 156 void opDivAssign(float s) 157 { for (int i=0; i<v.length; i++) 158 v[i] /= s; 159 } 160 161 /// Allow casting to float where appropriate 162 static if (is(T : float)) // if T can be implicitly cast to float 163 { Vec!(S, float) opCast() 164 { Vec!(S, float) result; 165 for (int i=0; i<v.length; i++) 166 result.v[i] = v[i]; 167 return result; 168 } } 169 123 170 /// Get the element at i 124 float opIndex( ubytei)171 float opIndex(size_t i) 125 172 { return v[i]; 126 173 } 127 174 128 175 /// Assign value to the element at i 129 float opIndexAssign(T value, ubytei)176 float opIndexAssign(T value, size_t i) 130 177 { return v[i] = value; 131 178 } … … 200 247 { float x, y, z; 201 248 } } 202 203 invariant204 { foreach (float t; v)205 assert(t != float.nan);206 }207 249 208 250 /** Test some of the more common and more complex functions. */ … … 334 376 { return Vec3f(1/x, 1/y, 1/z); 335 377 } 378 379 /** 380 * Unlike a transformation, we first apply the translation and then the rotation. */ 381 Vec3f inverseTransform(Matrix m) 382 { Vec3f copy = Vec3f(x-m[12], y-m[13], z-m[14]); // apply translation in reverse 383 return Vec3f( 384 copy.x*m[0] + copy.y*m[1] + copy.z*m[2], 385 copy.x*m[4] + copy.y*m[5] + copy.z*m[6], 386 copy.x*m[8] + copy.y*m[9] + copy.z*m[10] 387 ); 388 } 389 390 /// 391 Vec3f inverseRotate(Matrix m) 392 { Vec3f result; 393 result.v[0] = v[0]*m[0] + v[1]*m[1] + v[2]*m[2]; 394 result.v[1] = v[0]*m[4] + v[1]*m[5] + v[2]*m[6]; 395 result.v[2] = v[0]*m[8] + v[1]*m[9] + v[2]*m[10]; 396 return result; 397 } 398 399 /// 400 Vec3f inverseTranslate(Matrix m) 401 { return Vec3f(x-m[12], y-m[13], z-m[14]); 402 } 336 403 337 404 /// Return the length of the vector (the magnitude). … … 467 534 return res; 468 535 } 469 536 470 537 /// Return a copy of this vector with each component scaled by s. 471 538 Vec3f scale(float s) … … 517 584 518 585 /** 519 * Interpret the values of this vector as a rotation axis and convert to 520 * a Quatrn.*/ 586 * Interpret the values of this vector as a rotation axis and convert to a Quatrn.*/ 521 587 Quatrn toQuatrn() 522 588 { Quatrn res; … … 535 601 536 602 /** 537 * Interpret the values of this vector as a rotation axis and convert to 538 * a rotation Matrix.*/ 603 * Interpret the values of this vector as a rotation axis and convert to a rotation Matrix.*/ 539 604 Matrix toMatrix() 540 605 { Matrix res; … … 561 626 { return formatString("<%.4f %.4f %.4f>", x, y, z); 562 627 } 628 629 /// 630 void translate(Matrix m) 631 { x += m.v[12]; 632 y += m.v[13]; 633 z += m.v[14]; 634 } 635 636 /// Return a copy of this vector transformed by Matrix m. 637 Vec3f transform(Matrix m) 638 { return Vec3f( 639 x*m.v[0] + y*m.v[4] + z*m.v[8] + m.v[12], 640 x*m.v[1] + y*m.v[5] + z*m.v[9] + m.v[13], 641 x*m.v[2] + y*m.v[6] + z*m.v[10]+ m.v[14] 642 ); 643 } 563 644 } trunk/src/yage/gui/style.d
r67 r81 4 4 module yage.gui.style; 5 5 6 import std.regexp; 7 import std.string; 8 import std.stdio; 9 import std.conv; 6 10 import yage.core.color; 7 11 import yage.core.vector; 12 import yage.resource.font; 13 import yage.resource.resource; 8 14 import yage.resource.material; 15 import yage.resource.texture; 9 16 10 17 /** 11 18 * Specifies the style of a Surface. 12 * Inspired by the CSS specification (http://www.w3schools.com/css/css_reference.asp).13 * Defined here to keep things well separated.14 * Styles that have a top, right, bottom, left (like margin, border) are stored in arrays of length 4.*/19 * Inspired by the <a href="http://www.w3schools.com/css/css_reference.asp">CSS specification</a>. 20 * 21 * This struct is not fully documented. */ 15 22 struct Style 16 { 17 enum Unit {PX, PERCENT}; 18 19 Material backgroundMaterial; 20 Color backgroundColor; 21 float[4] backgroundCoordinates; // Texture coordinates for top, right, bottom, left edges 22 23 float[4] borderWidth; 24 Unit[4] borderWidthUnits; 25 Color[4] borderColor; 26 Material decoration; // Overrides radius and color if set 27 byte[5] decorationRepeat; // top, right, bottom, left, center 28 29 Material cursor; 30 bool visible = false;; 31 byte position; 32 33 //Font fontFamily; 34 float fontSize; 35 byte fontSizeUnits; 36 float fontWeight; 37 38 float[4] padding; 39 Unit[4] paddingUnits; 40 41 float[4] dimension; // top, right, bottom, left 42 &n
