From 0b777b2b5a54f97c193b39948a20be5806734e92 Mon Sep 17 00:00:00 2001 From: Kathy Gray Date: Wed, 15 Feb 2006 23:33:51 +0000 Subject: [PATCH] Correction to bug in cycle detection. Updates to graphics library interface svn: r2248 --- collects/htdch/graphics/GameWorld.djava | 30 ++++---- collects/htdch/graphics/View.djava | 29 +++++--- collects/htdch/graphics/World.java | 2 - collects/htdch/graphics/info.ss | 3 +- collects/htdch/graphics/installer.ss | 14 ++-- collects/htdch/graphics/rename.ss | 6 +- collects/profj/compile.ss | 1 + collects/profj/to-scheme.ss | 11 +-- collects/tests/profj/intermediate-tests.ss | 79 ++++++++++++++++++++++ 9 files changed, 131 insertions(+), 44 deletions(-) diff --git a/collects/htdch/graphics/GameWorld.djava b/collects/htdch/graphics/GameWorld.djava index 2d4ca7fd86..bb8defd5f8 100644 --- a/collects/htdch/graphics/GameWorld.djava +++ b/collects/htdch/graphics/GameWorld.djava @@ -11,14 +11,6 @@ public abstract class GameWorld extends World { super(new View()); } - public World transition( World last ){ - if (!(last instanceof GameWorld)) - throw new RuntimeException("Cannot transition from a non-GameWorld to a GameWorld"); - this.display = last.display; - this.timer = ((GameWorld) last).timer; - return this; - } - public World endOfWorld() { timer.stop(); return this; @@ -26,29 +18,31 @@ public abstract class GameWorld extends World { World nextWorld = this; + void oneStepPrivate(World oldWorld, World newWorld) { + ((GameWorld) newWorld).timer = ((GameWorld) oldWorld).timer; + newWorld.display = oldWorld.display; + display.allowImage(false); + oldWorld.erase(); + newWorld.draw(); + display.allowImage(true); + } + //Produces a World that will animate with a clock tick of rate public final boolean animate( int width, int height, int rate ) { class TimerCallBack { - TimerCallBack() { } public void callBack() { World old = GameWorld.this.nextWorld; GameWorld.this.nextWorld = GameWorld.this.nextWorld.onTick(); - GameWorld.this.display.allowImage(false); - old.erase(); - GameWorld.this.nextWorld.draw(); - GameWorld.this.display.allowImage(true); + GameWorld.this.oneStepPrivate(old, GameWorld.this.nextWorld); } } class KeyCallBack { - public void callBack(dynamic key) { + public void callBack(String key) { World old = GameWorld.this.nextWorld; GameWorld.this.nextWorld = GameWorld.this.nextWorld.onKey(key); - GameWorld.this.display.allowImage(false); - old.erase(); - GameWorld.this.nextWorld.draw(); - GameWorld.this.display.allowImage(true); + GameWorld.this.oneStepPrivate(old,GameWorld.this.nextWorld); } } diff --git a/collects/htdch/graphics/View.djava b/collects/htdch/graphics/View.djava index a10d0a88b5..aea08a4705 100644 --- a/collects/htdch/graphics/View.djava +++ b/collects/htdch/graphics/View.djava @@ -33,12 +33,15 @@ public class View { class CanvasInner { public void callBack(dynamic canvas, dynamic dc) { - dc.drawBitmap( View.this.buffer.getBitmap(), 0,0); + dc.drawBitmap( View.this.buffer.getBitmap(), 0,0); } } frame = rename.newObject(mred.frameObj, name, false, x+15, y+20); - canvas = rename.newObject(rename.callBackCanvasObj, frame, rename.emptyList, rename.innerToFunction( 2, new CanvasInner())); + canvas = rename.newObject(rename.callBackCanvasObj, frame, + rename.styleList, +// rename.consL(rename.q(rename.toSymbol("no-autoclear")), rename.emptyList), + rename.innerToFunction( 2, new CanvasInner())); dc = canvas.getDc(); this.clear(); @@ -62,19 +65,19 @@ public class View { } //The Image is a equality testable version of the canvas after the drawing command - public Image draw( Command c) { - drawToCanvas(c); + public Image draw( Image i) { + drawToCanvas(i); dc.drawBitmap(buffer.getBitmap(), 0 ,0); return getBufferCopy(); } //The Image is again an equality testable version of the canvas after applying all commands //Issues the commands in reverse order - public Image drawSequence( CommandSequence commands ) { - commands.drawAll(this); - dc.drawBitmap(buffer.getBitmap(), 0 ,0); - return getBufferCopy(); - } +// public Image drawSequence( CommandSequence commands ) { +// commands.drawAll(this); +// dc.drawBitmap(buffer.getBitmap(), 0 ,0); +// return getBufferCopy(); +// } //Methods not seen by students @@ -108,10 +111,14 @@ public class View { } //Issues the drawing command in c, causing it to draw to the canvas - void drawToCanvas( Command c) { + void drawToCanvas( Image i) { if (!visible) throw new RuntimeException("View must be displayed in order to draw in it"); - c.issue(buffer); + dynamic bitmap = i.getBitmap(); + buffer.drawBitmap(bitmap, 0, 0, rename.toSymbol("solid"), + rename.newObject(mred.colorObj, "white"), bitmap.getLoadedMask()); + + //c.issue(buffer); } } \ No newline at end of file diff --git a/collects/htdch/graphics/World.java b/collects/htdch/graphics/World.java index 452c143caf..5e805fc200 100644 --- a/collects/htdch/graphics/World.java +++ b/collects/htdch/graphics/World.java @@ -17,8 +17,6 @@ public abstract class World { //Produces a World that will animate with a clock tick of rate public abstract boolean animate( int width, int height, int rate ); - public abstract World transition( World w); - public abstract Image draw(); public abstract Image erase(); diff --git a/collects/htdch/graphics/info.ss b/collects/htdch/graphics/info.ss index 9dc97ba113..4c2858af15 100644 --- a/collects/htdch/graphics/info.ss +++ b/collects/htdch/graphics/info.ss @@ -1,3 +1,4 @@ (module info (lib "infotab.ss" "setup") (define name "Java Graphics Teachpack") - (define install-collection "installer.ss")) \ No newline at end of file + (define doc.txt "doc.txt") + (define install-collection "installer.ss")) diff --git a/collects/htdch/graphics/installer.ss b/collects/htdch/graphics/installer.ss index 49cf9ade2c..1366987140 100644 --- a/collects/htdch/graphics/installer.ss +++ b/collects/htdch/graphics/installer.ss @@ -24,14 +24,14 @@ (javac "Red.java") (javac "White.java") (javac "Yellow.java") - (javac "Command.djava") - (javac "DrawLine.djava") - (javac "DrawRectangle.djava") - (javac "DrawImage.djava") + ;(javac "Command.djava") + ;(javac "DrawLine.djava") + ;(javac "DrawRectangle.djava") + ;(javac "DrawImage.djava") (javac "View.djava") - (javac "CommandSequence.java") - (javac "LargerSeq.java") - (javac "EmptySeq.java") + ;(javac "CommandSequence.java") + ;(javac "LargerSeq.java") + ;(javac "EmptySeq.java") (javac "World.java") (javac "GameWorld.djava") ))) diff --git a/collects/htdch/graphics/rename.ss b/collects/htdch/graphics/rename.ss index 58fa17fad1..49c2c4694d 100644 --- a/collects/htdch/graphics/rename.ss +++ b/collects/htdch/graphics/rename.ss @@ -6,7 +6,7 @@ (lib "imageeq.ss" "lang" "private" )) (provide to-symbol new-object call-back-canvas% overlay-x-y - inner->function image-eq? empty-list printer) + inner->function image-eq? empty-list printer cons-l style-list) (define (inner->function num-args inner) (cond @@ -22,6 +22,10 @@ (define to-symbol string->symbol) + (define style-list `(list 'no-autoclear)) + + (define cons-l cons) + (define (new-object class . args) ((current-eval) #`(make-object #,class #,@args))) diff --git a/collects/profj/compile.ss b/collects/profj/compile.ss index 82d651f38c..e6442144a1 100644 --- a/collects/profj/compile.ss +++ b/collects/profj/compile.ss @@ -270,6 +270,7 @@ ;order-cus (list compilation-unit) type-records -> (list compilation-unit) (define (order-cus cus type-recs) + ;(printf "~a~n" cus) (let-values (((work-list ordered) (split-cu cus cus null null type-recs))) ;(printf "order-cus: ~a ~a ~a ~n" (length cus) (length work-list) (length ordered)) (unless (null? work-list) diff --git a/collects/profj/to-scheme.ss b/collects/profj/to-scheme.ss index cc80bfafee..e73b756c68 100644 --- a/collects/profj/to-scheme.ss +++ b/collects/profj/to-scheme.ss @@ -252,8 +252,8 @@ (cur-cycle-length 0) (current-cycle null)) - (letrec ((already-in-cycle? (lambda (n) (eq? 'in-cycle (hash-table-get marks n)))) - + (letrec ((already-in-cycle? + (lambda (n) (eq? 'in-a-cycle (hash-table-get marks n)))) (in-current-cycle? (lambda (n) (hash-table-get current-cycle n (lambda () #f)))) (current-cycle-memq @@ -271,7 +271,8 @@ (unless (already-in-cycle? node) ;(printf "componentize ~a ~a ~a~n" node successors member?) (let ((added? #f) - (cur-length cur-cycle-length)) + (cur-length cur-cycle-length) + (old-mark (hash-table-get marks node))) (when (and (not member?) (current-cycle-memq successors)) (set! added? #t) (add-to-current-cycle node)) @@ -284,13 +285,15 @@ successors) ;(printf "finished successors for ~a~n" node) (if (or added? (= cur-length cur-cycle-length)) - (hash-table-put! marks node 'no-info) + (hash-table-put! marks node old-mark) (componentize node successors #f))))))) (for-each-node graph (lambda (n) (hash-table-put! marks n 'no-info))) (for-each-node graph (lambda (node) + ;(hash-table-for-each marks (lambda (key val) (printf "~a -> ~a~n" (eq-hash-code key) val))) + ;(printf "Working on ~a~n~n" (eq-hash-code node)) (when (eq? (hash-table-get marks node) 'no-info) (set! current-cycle (make-hash-table)) (add-to-current-cycle node) diff --git a/collects/tests/profj/intermediate-tests.ss b/collects/tests/profj/intermediate-tests.ss index 3793161e66..7e56315287 100644 --- a/collects/tests/profj/intermediate-tests.ss +++ b/collects/tests/profj/intermediate-tests.ss @@ -455,4 +455,83 @@ '(1.0 1.0 (void) 0.0 1.0 1) "Double-int conversions") + (interact-test + "import draw.*; +class BlockWorld extends World { + int WIDTH = 100; + int HEIGHT = 100; + Color BACKGROUND = new Red(); + DrpBlock block; + BlockWorld(DrpBlock block) { + this. block = block; + } + World onTick() { + return new BlockWorld(this. block.drop()); + } + + World onKeyEvent(String ke) { + return this; + } + boolean erase() { + return this. drawBackground(); + } + boolean draw() { + return this. block.draw(this); + } + boolean drawBackground() { + return this. theCanvas.drawRect(new Posn(0,0),this. WIDTH,this. HEIGHT,this. BACKGROUND); + } +} + +class Examples extends BlockWorld { + Examples() { + super(new DrpBlock(10,0)); + } +} +class DrpBlock { + int down; + int right; + int HEIGHT = 10; + int WIDTH = 10; + int deltaY = 5; + int deltaX = 3; + + DrpBlock(int down, int right) { + this. down = down; + this. right = right; + } + DrpBlock drop() { + return new DrpBlock(this. down + this. deltaY,this. right); + } + boolean draw(World w) { + return w.theCanvas.drawRect(new Posn(this.right,this.down),this.HEIGHT,this.WIDTH,new Red()); + } + boolean erase(BlockWorld w) { + return w.theCanvas.drawRect(new Posn(this.right,this.down),this.HEIGHT,this.WIDTH,w.BACKGROUND); + } + boolean hasLanded(BlockWorld w) { + if (this. down + this. HEIGHT >= w.HEIGHT) + return true; + else + return false; + } + DrpBlock steer(String ke) { + if (ke.equals(\"left\")) + return new DrpBlock(this. down,this. right - this. deltaX); + else if (ke.equals(\"right\")) + return new DrpBlock(this. down,this. right + this. deltaX); + else + return this; + } + boolean toStop(BlockWorld w, int down) { + if (this. down + this. HEIGHT >= down) + return true; + else + return false; + } +}" + 'intermediate + '("Examples a = new Examples();") '((void)) + "Cycle: used to cause multiple declarations of a class") + (report-test-results)) \ No newline at end of file