Changeset 345
- Timestamp:
- 05/30/08 04:27:11 (6 months ago)
- Files:
-
- trunk/qd/SDL_ttf.d (modified) (5 diffs)
- trunk/qd/dt.d (modified) (15 diffs)
- trunk/qd/qd.d (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/qd/SDL_ttf.d
r343 r345 9 9 } 10 10 11 import std.string: toStringz, toString ;11 import std.string: toStringz, toString, atoi; 12 12 import std.file: read; 13 13 import tools.base; … … 75 75 if (color.length == 3) color = color[0]~"0"~color[1]~"0"~color[2]~"0"; 76 76 auto res = rgb(color[0 .. 2].htoi(), color[2 .. 4].htoi(), color[4 .. 6].htoi()); 77 push(pre, "color", (ref fontsettings fs) { fs.color = res; logln("fs.color: ", fs.color); }); 78 }, "[/color]": close_tag("color") 77 push(pre, "color", (ref fontsettings fs) { fs.color = res; }); 78 }, "[/color]": close_tag("color"), 79 "[size ": (string pre, ref string post) { 80 int dir; 81 if (post[0] == '+') dir = 1; 82 else if (post[0] == '-') dir = -1; 83 if (post[0] == '+' /or/ '-') post = post[1 .. $]; 84 string s; ptuple(s, post) = post.splitAt("]"); 85 auto n = s.atoi(); 86 push(pre, "size", (ref fontsettings fs) { 87 if (!dir) fs.size = n; 88 else fs.size += n * dir; 89 }); 90 }, "[/size]": close_tag("size") 79 91 ]; 80 92 text.glomp_parse(tags, (string s) { … … 89 101 } body { 90 102 auto surfs = new Area[strings.length]; 103 scope(exit) foreach (surf; surfs) SDL_FreeSurface(surf.surface); 91 104 int width, height; 92 105 foreach (i, str; strings) { … … 106 119 107 120 void print(int x, int y, Align how, rgb col, string what) { 108 if (!(14 in standard)) initFont();109 121 fontsettings foo; foo.color = col; 110 auto surf=standard[14].render(what, foo); 111 display.select(surf.size, pt(x, y), how).blit(surf); 112 SDL_FreeSurface(surf.surface); 122 print(x, y, how, [foo], [what]); 113 123 } 114 124 … … 156 166 auto res=_render(text, s); 157 167 if (!res) throw new Exception("Couldn't render "~'"'~text~'"'~": "~.toString(SDL_GetError)); 158 return Area( pt(0, 0), pt(res.w, res.h),res);168 return Area(res); 159 169 } 160 170 void[] file_buffer; trunk/qd/dt.d
r343 r345 28 28 class LivingThing : WorldThing { 29 29 private { 30 int life ;30 int life, age; 31 31 void delegate(LivingThing) onDeath; 32 32 } 33 override void update() { age++; } 33 34 void damage(int howmuch) { life-=howmuch; if (life!>0) onDeath(this); } 34 35 void die() { damage(life); onDeath(this); } … … 111 112 bufferedCircle(pos.to!(int).tuple, radius, Black, Fill=Blue~White~White); 112 113 if (life<maxlife) circle(pos.to!(int).tuple, cast(int)(radius*0.8*(1f-life * 1f / maxlife)), Background~Red, Fill=Background~Red); 113 .print(pos.to!(int).tuple, Center, Black, Format(life)); 114 string fmt; 115 if (life>1000) fmt = Format("[size -5]", life, "[/size]"); 116 else if (life > 100) fmt = Format("[size -3]", life, "[/size]"); 117 else fmt = Format(life); 118 fmt = "[color #040]"~fmt~"[/color]"; 119 .prettyprint(pos.to!(int).tuple, Center, fmt); 114 120 } 115 121 override vec2f getMotionVector() { return train.delta; } … … 136 142 } 137 143 138 class Rocket: LivingThing {144 class Ammo : LivingThing { 139 145 TargetGroup targets; 140 vec2f delta; 146 // vec2f delta; 147 float length, angle; 141 148 struct Settings { 142 float speed, aim, momentum;149 float speed, aim, speed_momentum, dir_momentum; 143 150 int damage, lifetime; 144 151 bool linger, punchthrough, retarget; … … 146 153 Settings settings; 147 154 mixin This!("settings, targets, super(onDeath, pos, radius=3, life=10) 148 #life=settings.damage; # delta=vec2f(0); ");155 #life=settings.damage; #angle=0f; "); 149 156 LivingThing enemy; 150 157 void draw() { 151 158 //line(x, y, enemy.x, enemy.y, White); 152 bufferedCircle(pos.to!(int).tuple, radius, Fill=Black); 159 auto color = Black.blend(White, age * 1f / settings.lifetime); 160 bufferedCircle(pos.to!(int).tuple, radius, Fill=color, color); 153 161 } 154 162 void damage(typeof(life) d) { 163 int prelife = life; 155 164 super.damage(d); 156 if (life!>0) addEffect!(RingEffect)(Red, pos, 3, 3);165 if (life!>0) addEffect!(RingEffect)(Red, pos, 3, cast(int) (log(prelife) + PI_2)); // I WANNA PIE TOO 157 166 } 158 167 void hit(LivingThing target) { … … 163 172 } 164 173 void update() { 165 damage(settings.damage/settings.lifetime); 174 if (!(pos in display)) die(); 175 super.update(); 176 if (age > settings.lifetime) die(); 166 177 if (enemy && enemy.life!>0) enemy=null; // This enemy is ded. 167 178 if (!enemy) { 168 if (!settings.retarget) { die(); return; } 169 else { 179 if (settings.retarget) { 170 180 enemy=targets.getTarget(this); 171 if (!enemy && !settings.linger) { die(); return;}172 }181 } 182 if (!enemy && !settings.linger) { die(); return; } 173 183 } 174 184 vec2f new_motion; 185 vec2f delta() { 186 auto res = vec2f(1, 0); 187 res.rotate(angle); res.length = length; 188 return res; 189 } 175 190 if (enemy) { 176 191 new_motion = (enemy.pos - pos).normalized() * (1f - settings.aim); … … 179 194 } 180 195 new_motion.length = settings.speed; 181 } else new_motion = vec2f(0f, 0f); 182 delta *= settings.momentum; delta += new_motion * (1f - settings.momentum); 196 197 auto old_motion = delta(); 198 //logln("old_motion: ", old_motion, ", angle ", angle, ", length ", length); 199 //logln("new_motion: ", new_motion, ", their angle ", old_motion.angle(new_motion)); 200 auto d_angle = old_motion.angle(new_motion) * (1f - settings.dir_momentum); 201 if (isnan(length)) { 202 d_angle = vec2f(1, 0).angle(new_motion); 203 length = 0f; 204 } 205 //logln("d_angle: ", d_angle); 206 angle += d_angle; 207 208 length = length * settings.speed_momentum + new_motion.length * (1f - settings.speed_momentum); 209 } 183 210 pos += delta; 184 211 if (enemy && collide(enemy)) { hit(enemy); return; } … … 259 286 } 260 287 Settings settings; 261 Rocket.Settings rsettings;288 Ammo.Settings rsettings; 262 289 private { 263 290 int counter; 264 291 TargetGroup targets; 265 292 } 266 Group!( Rocket) rockets;293 Group!(Ammo) Teh_Ordnance; 267 294 mixin This!("settings, rsettings, targets, super(pos, radius=12), 268 #counter=settings.rate > 0 ? settings.rate : 1; # rockets=new Group!(Rocket);");295 #counter=settings.rate > 0 ? settings.rate : 1; #Teh_Ordnance=new Group!(Ammo);"); 269 296 void draw() { 270 297 auto ipos = pos.to!(int); … … 275 302 } 276 303 bufferedCircle(ipos.tuple, radius, Black, Fill=White~Green); 277 rockets.draw;304 Teh_Ordnance.draw; 278 305 } 279 306 private void die(LivingThing not_so_living_now_are_you) { 280 rockets.remove(cast(Rocket)cast(void*)not_so_living_now_are_you); // trust me. this is correct.307 Teh_Ordnance.remove(cast(Ammo)cast(void*)not_so_living_now_are_you); // trust me. this is correct. 281 308 } 282 309 void update() { 283 scope(exit) rockets.update;310 scope(exit) Teh_Ordnance.update; 284 311 auto enemy=targets.getTarget(this, settings.range); 285 312 if (!enemy) return; … … 289 316 if (settings.rate < 0) times = abs(settings.rate); 290 317 Range[times].each = { 291 auto newThing=new Rocket(rsettings, targets, &die, pos); 292 if (settings.kickstart) newThing.delta = vec2f.rand() / 2f; 318 auto newThing=new Ammo(rsettings, targets, &die, pos); 319 if (settings.kickstart) { 320 with (vec2f.rand()) { 321 newThing.angle = angle(vec2f(1, 0)); 322 newThing.length = length(); 323 } 324 } 293 325 newThing.enemy=enemy; 294 rockets.addThing(newThing); newThing.update;326 Teh_Ordnance.addThing(newThing); newThing.update; 295 327 }; 296 328 } 329 } 330 331 void withSet(T)(ref T var, T val, void delegate() dg) { 332 auto backup = var; var = val; 333 scope(exit) var = backup; 334 dg(); 297 335 } 298 336 … … 309 347 #group_left=groupsize; #critters=new TargetGroup; #break_mode=true; #break_left=breaktime; "); 310 348 void draw() { 311 bufferedCircle(pos.to!(int).tuple, radius, White, Fill=Background~Black);312 349 string text; 313 350 if (break_mode) … … 316 353 else text = Format("GO"); 317 354 else if (group_left) text = Format(group_left); 355 critters.draw; 356 withSet(alpha, 64, { circle(pos.to!(int).tuple, radius, White, Fill=Background~Black); }); 318 357 .print(pos.to!(int).tuple, Center, Black, text); 319 critters.draw;320 358 } 321 359 void win() { throw new Exception("The critters win!"); } … … 392 430 flip(); 393 431 pc.stop; 394 writefln("Flipped - took ", pc.milliseconds, "ms!");432 //writefln("Flipped - took ", pc.milliseconds, "ms!"); 395 433 cur=SDL_GetTicks(); 396 434 } 397 435 scope(exit) count=cur; 398 436 if (fps && (cur-count<ms)) { 399 writefln("Delay ", ms-(cur-count));437 //writefln("Delay ", ms-(cur-count)); 400 438 SDL_Delay(ms-(cur-count)); 401 } else writefln("No delay - ", (cur-count)-ms, " over!");439 } //else writefln("No delay - ", (cur-count)-ms, " over!"); 402 440 } 403 441 } … … 448 486 group=new typeof(group)([cast(Thing)spawner]); 449 487 group.addThing(new Turret( 450 Turret.Settings(115, 2 , false),451 Rocket.Settings(/*speed*/ 1.1f, /*aim*/ 0.1, /*momentum*/0.97, /*damage*/ 1, /*lifetime*/256, /*linger*/false, /*punchthrough*/false),488 Turret.Settings(115, 24, false), 489 Ammo.Settings(/*speed*/ 1.5f, /*aim*/ 0.25, /*momentum_speed*/0, /*momentum_dir*/ 1, /*damage*/ 8, /*lifetime*/256, /*linger*/true, /*punchthrough*/false), 452 490 spawner.critters, vec2f(85, 220) 453 491 )); 454 492 group.addThing(new Turret( 455 Turret.Settings(115, 2, false),456 Rocket.Settings(/*speed*/ 1.1f, /*aim*/ 0.1, /*momentum*/0.97, /*damage*/ 1, /*lifetime*/256, false, false),493 Turret.Settings(115, 6, true), 494 Ammo.Settings(/*speed*/ 0.9f, /*aim*/ 0, /*momentum_speed*/0.9, /*momentum_dir*/ 0.98, /*damage*/ 2, /*lifetime*/1024, true, false, true), 457 495 spawner.critters, vec2f(185, 135) 458 496 )); … … 469 507 display.blit(bg_surf); 470 508 prettyprint(0, 0, Bottom|Right, 471 "Test .. test![b]Bold, [i]italic and [u]underlined![/u][/i][/b] [i]Just italic now.[/i] [b][color #f00]Red?[/color][/b]Done!");509 "Test .. [size -1]test![/size] [b]Bold, [i]italic and [u]underlined![/u][/i][/b] [i]Just italic now.[/i] [b][color #f00]Red?[/color][/b]Done!"); 472 510 draw; 473 511 if (drag) { … … 503 541 writefln(fps, " FPS"); 504 542 ulong rcount=0; 505 foreach (entry; group) if (auto t=cast(Turret) entry) rcount+=t. rockets.length;506 writefln(rcount, " rockets");543 foreach (entry; group) if (auto t=cast(Turret) entry) rcount+=t.Teh_Ordnance.length; 544 writefln(rcount, " explody thingies"); 507 545 avg+=fps; ++count; 508 546 fps=0; trunk/qd/qd.d
r343 r345 217 217 } 218 218 219 int alpha; 220 219 221 void putpixel(int x, int y, uint col) { 220 222 auto transformed=display.tl+pt(x, y); … … 370 372 template UnPoint(T...) { mixin _UnPoint!(0, T); } 371 373 374 static import std.math; 375 bool hasnan(T...)(T t) { 376 foreach (value; t) 377 static if (is(typeof(value): real)) if (std.math.isnan(cast(real) value)) return true; 378 return false; 379 } 380 372 381 void line(_T...)(_T _p) { 382 if (hasnan(_p)) return; 373 383 alias UnPoint!(_T).tuple T; 374 384 auto p = mixin("stuple("~Replace!(UnPoint!(_T).value, "%", "_p")~")"); … … 457 467 } 458 468 459 void hline(int x, int y, int w, uint col) { for (int pos=x; pos<=x+w; ++pos) putpixel(pos, y, col); } 469 void hline(int x, int y, int w, uint col) { 470 if (alpha) { 471 ubyte[4] cur_col; 472 auto color = (cast(ubyte*) &col)[0..4]; // this was originally an error, but it's an awesome graphics effect. 473 for (int pos=x; pos<=x+w; ++pos) { 474 getpixel32(display.surface, pos, y, &cur_col); 475 color[0]=cast(ubyte)((cur_col[0]*alpha+color[0]*(255-alpha))>>8); 476 color[1]=cast(ubyte)((cur_col[1]*alpha+color[1]*(255-alpha))>>8); 477 color[2]=cast(ubyte)((cur_col[2]*alpha+color[2]*(255-alpha))>>8); 478 putpixel32(display.surface, pos, y, SDL_MapRGBA(display.surface.format, color[0], color[1], color[2], color[3])); 479 } 480 } else { 481 for (int pos=x; pos<=x+w; ++pos) putpixel(pos, y, col); 482 } 483 } 460 484 461 485 import std.stdio; … … 699 723 return res; 700 724 } 701 bool opIn_r( pt p) { return (p.x>=tl.x && p.x<=(tl.x+size.x)) && (p.y>=tl.y && p.y<=(tl.y+size.y)); }725 bool opIn_r(T)(T vt) { return (vt.x>=tl.x && vt.x<=(tl.x+size.x)) && (vt.y>=tl.y && vt.y<=(tl.y+size.y)); } 702 726 Area select(int x, int y, int w=int.max, int h=int.max) { 703 727 if (w==int.max) w=size.x;
