Correction to bug in cycle detection. Updates to graphics library interface
svn: r2248
This commit is contained in:
parent
0bfecf5fb5
commit
0b777b2b5a
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
(module info (lib "infotab.ss" "setup")
|
||||
(define name "Java Graphics Teachpack")
|
||||
(define install-collection "installer.ss"))
|
||||
(define doc.txt "doc.txt")
|
||||
(define install-collection "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")
|
||||
)))
|
||||
|
|
|
@ -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)))
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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))
|
Loading…
Reference in New Issue
Block a user