diff --git a/Makefile b/Makefile index d99197c..3142aa2 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,14 @@ # **************************************************************** # Makefile for SPL +# detect install path +# if Ubuntu, place in /usr/... otherwise +# assume Fedora which is placed in /usr/local/... +path:=/usr +ifneq ($(findstring Ubuntu,$(shell uname -v)),Ubuntu) + path+=/local +endif + SHELL=/bin/bash BUILD = \ @@ -319,11 +327,11 @@ stanford/spl/JavaBackEnd.class: java/src/stanford/spl/*.java # install install: build/lib/libcs.a $(JAR) - rm -rf /usr/local/include/spl - cp -r build/include /usr/local/include/spl - chmod -R a+rX /usr/local/include/spl - cp build/lib/{libcs.a,spl.jar} /usr/local/lib/ - chmod -R a+r /usr/local/lib/{libcs.a,spl.jar} + rm -rf $(path)/include/spl + cp -r build/include $(path)/include/spl + chmod -R a+rX $(path)/include/spl + cp build/lib/{libcs.a,spl.jar} $(path)/lib/ + chmod -R a+r $(path)/lib/{libcs.a,spl.jar} # *************************************************************** diff --git a/README.md b/README.md index 911c81a..412317a 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,11 @@ This is CS50's fork of Eric Roberts' Stanford Portable Library. ### Ubuntu -_coming soon_ + sudo apt-get install -y bash binutils coreutils findutils gcc java-1.?.0-openjdk-devel + git clone git@github.com:cs50/spl.git + cd spl + make + sudo make install ## TODO diff --git a/c/examples/CanvasColors b/c/examples/CanvasColors new file mode 100755 index 0000000..95b6618 Binary files /dev/null and b/c/examples/CanvasColors differ diff --git a/c/examples/CanvasColors.c b/c/examples/CanvasColors.c new file mode 100644 index 0000000..9d69672 --- /dev/null +++ b/c/examples/CanvasColors.c @@ -0,0 +1,62 @@ +// standard libraries +#define _XOPEN_SOURCE +#include +#include +#include +#include +#include + +// Stanford Portable Library +#include +#include +#include + +// height and width of game's window in pixels +#define HEIGHT 600 +#define WIDTH 400 + +// number of rows of bricks +#define ROWS 5 + +// number of columns of bricks +#define COLS 10 + +// brick properties +#define BRICK_SPACING 5 +#define BRICK_WIDTH (WIDTH - BRICK_SPACING * COLS) / COLS +#define BRICK_HEIGHT HEIGHT / 40 + +void initBricks(GWindow window); + +int main(void) +{ + // instantiate window + GWindow window = newGWindow(WIDTH, HEIGHT); + setBackground(window, "red"); + setForeground(window, "black"); // set default obj color to black + initBricks(window); + return 0; +} + +/** + * * Initializes window with a grid of bricks. + * */ +void initBricks(GWindow window) +{ + for (int row = 0; row < ROWS; row++) + { + for (int col = 0; col < COLS; col++) + { + GRect brick = newGRect( + BRICK_SPACING + (BRICK_WIDTH + BRICK_SPACING) * col, + BRICK_SPACING + (BRICK_HEIGHT + BRICK_SPACING) * row, + BRICK_WIDTH, BRICK_HEIGHT + ); + //setColor(brick, "blue"); + setFilled(brick, true); + add(window, brick); + } + } +} + + diff --git a/c/examples/Makefile b/c/examples/Makefile index d18ad81..fcc352f 100644 --- a/c/examples/Makefile +++ b/c/examples/Makefile @@ -10,7 +10,8 @@ PROGRAMS = \ StopSign \ TestGraphics \ TestInteractors \ - USFlag + USFlag \ + CanvasColors FLAGS = -L../lib -lcs -lm @@ -75,6 +76,12 @@ USFlag: USFlag.o USFlag.o: USFlag.c gcc -c -I../include USFlag.c +CanvasColors: CanvasColors.o + gcc -o CanvasColors CanvasColors.o $(FLAGS) + +CanvasColors.o: CanvasColors.c + gcc -c -I../include CanvasColors.c + # *************************************************************** # Standard entries to remove files from the directories diff --git a/c/include/gwindow.h b/c/include/gwindow.h index 49ad1ae..8f560c2 100644 --- a/c/include/gwindow.h +++ b/c/include/gwindow.h @@ -202,6 +202,41 @@ void fillRect(GWindow gw, double x, double y, double width, double height); void setColorGWindow(GWindow gw, string color); +/* + * Function: setBackground + * Usage: setBackground(gw, color); + * --------------------------- + * Sets the window background color. The color parameter is + * usually one of the predefined color names from Java: BLACK, + * BLUE, CYAN, DARK_GRAY, + * GRAY, GREEN, LIGHT_GRAY, + * MAGENTA, ORANGE, PINK, + * RED, WHITE, or YELLOW. + * The case of the individual letters in the color name is ignored, + * as are spaces and underscores, so that the Java color + * DARK_GRAY could be written as "Dark Gray". + */ + +void setBackground(GWindow gw, string color); + +/* + * Function: setForeground + * Usage: setForeground(gw, color); + * --------------------------- + * Sets the window background color. The color parameter is + * usually one of the predefined color names from Java: BLACK, + * BLUE, CYAN, DARK_GRAY, + * GRAY, GREEN, LIGHT_GRAY, + * MAGENTA, ORANGE, PINK, + * RED, WHITE, or YELLOW. + * The case of the individual letters in the color name is ignored, + * as are spaces and underscores, so that the Java color + * DARK_GRAY could be written as "Dark Gray". + */ + +void setForeground(GWindow gw, string color); + + /* * Function: getColorGWindow * Usage: color = getColorGWindow(gw); diff --git a/c/include/platform.h b/c/include/platform.h index d05d3fd..49f4603 100644 --- a/c/include/platform.h +++ b/c/include/platform.h @@ -118,6 +118,8 @@ void sendToBackOp(GObject gobj); void setVisibleGObjectOp(GObject gobj, bool flag); void setColorOp(GObject gobj, string color); +void setBackgroundOp(GWindow gw, string color); +void setForegroundOp(GWindow gw, string color); void setLocationOp(GObject gobj, double x, double y); void setSizeOp(GObject gobj, double width, double height); diff --git a/c/src/gwindow.c b/c/src/gwindow.c index 086cb48..502f4db 100644 --- a/c/src/gwindow.c +++ b/c/src/gwindow.c @@ -125,6 +125,14 @@ void fillRect(GWindow gw, double x, double y, double width, double height) { freeGObject(gobj); } +void setBackground(GWindow gw, string color) { + setBackgroundOp(gw, color); +} + +void setForeground(GWindow gw, string color) { + setForegroundOp(gw, color); +} + void setColorGWindow(GWindow gw, string color) { gw->color = color; } diff --git a/c/src/platform.c b/c/src/platform.c index 2e61c90..99311cc 100644 --- a/c/src/platform.c +++ b/c/src/platform.c @@ -696,6 +696,14 @@ void setColorOp(GObject gobj, string color) { putPipe("GObject.setColor(\"0x%lX\", \"%s\")", (long) gobj, color); } +void setBackgroundOp(GWindow gw, string color) { + putPipe("JBECanvas.setBackground(\"0x%lX\", \"%s\")", (long) gw, color); +} + +void setForegroundOp(GWindow gw, string color) { + putPipe("JBECanvas.setForeground(\"0x%lX\", \"%s\")", (long) gw, color); +} + void setLocationOp(GObject gobj, double x, double y) { putPipe("GObject.setLocation(\"0x%lX\", %g, %g)", (long) gobj, x, y); } diff --git a/java/src/stanford/spl/JBECommand.java b/java/src/stanford/spl/JBECommand.java index 08079e3..179122a 100644 --- a/java/src/stanford/spl/JBECommand.java +++ b/java/src/stanford/spl/JBECommand.java @@ -106,6 +106,8 @@ public static HashMap createCommandTable() { cmdTable.put("GObject.sendToBack", new GObject_sendToBack()); cmdTable.put("GObject.sendToFront", new GObject_sendToFront()); cmdTable.put("GObject.setColor", new GObject_setColor()); + cmdTable.put("JBECanvas.setBackground", new JBECanvas_setBackground()); + cmdTable.put("JBECanvas.setForeground", new JBECanvas_setForeground()); cmdTable.put("GObject.setFillColor", new GObject_setFillColor()); cmdTable.put("GObject.setFilled", new GObject_setFilled()); cmdTable.put("GObject.setLineWidth", new GObject_setLineWidth()); @@ -913,6 +915,34 @@ public void execute(TokenScanner scanner, JavaBackEnd jbe) { } } +class JBECanvas_setBackground extends JBECommand { + public void execute(TokenScanner scanner, JavaBackEnd jbe) { + scanner.verifyToken("("); + String id = nextString(scanner); + JBEWindow jw = jbe.getWindow(id); + scanner.verifyToken(","); + String color = nextString(scanner); + scanner.verifyToken(")"); + if (jw != null) { + jw.getCanvas().setBackground(color.equals("") ? null : JTFTools.decodeColor(color)); + } + } +} + +class JBECanvas_setForeground extends JBECommand { + public void execute(TokenScanner scanner, JavaBackEnd jbe) { + scanner.verifyToken("("); + String id = nextString(scanner); + JBEWindow jw = jbe.getWindow(id); + scanner.verifyToken(","); + String color = nextString(scanner); + scanner.verifyToken(")"); + if (jw != null) { + jw.getCanvas().setForeground(color.equals("") ? null : JTFTools.decodeColor(color)); + } + } +} + class GObject_setLocation extends JBECommand { public void execute(TokenScanner scanner, JavaBackEnd jbe) { scanner.verifyToken("(");