Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C interface to set window background and foreground #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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 = \
Expand Down Expand Up @@ -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}


# ***************************************************************
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]:cs50/spl.git
cd spl
make
sudo make install

## TODO

Expand Down
Binary file added c/examples/CanvasColors
Binary file not shown.
62 changes: 62 additions & 0 deletions c/examples/CanvasColors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// standard libraries
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <assert.h>

// Stanford Portable Library
#include <spl/gevents.h>
#include <spl/gobjects.h>
#include <spl/gwindow.h>

// 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);
}
}
}


9 changes: 8 additions & 1 deletion c/examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ PROGRAMS = \
StopSign \
TestGraphics \
TestInteractors \
USFlag
USFlag \
CanvasColors

FLAGS = -L../lib -lcs -lm

Expand Down Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions c/include/gwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>color</code> parameter is
* usually one of the predefined color names from Java: <code>BLACK</code>,
* <code>BLUE</code>, <code>CYAN</code>, <code>DARK_GRAY</code>,
* <code>GRAY</code>, <code>GREEN</code>, <code>LIGHT_GRAY</code>,
* <code>MAGENTA</code>, <code>ORANGE</code>, <code>PINK</code>,
* <code>RED</code>, <code>WHITE</code>, or <code>YELLOW</code>.
* The case of the individual letters in the color name is ignored,
* as are spaces and underscores, so that the Java color
* <code>DARK_GRAY</code> could be written as <code>"Dark&nbsp;Gray"</code>.
*/

void setBackground(GWindow gw, string color);

/*
* Function: setForeground
* Usage: setForeground(gw, color);
* ---------------------------
* Sets the window background color. The <code>color</code> parameter is
* usually one of the predefined color names from Java: <code>BLACK</code>,
* <code>BLUE</code>, <code>CYAN</code>, <code>DARK_GRAY</code>,
* <code>GRAY</code>, <code>GREEN</code>, <code>LIGHT_GRAY</code>,
* <code>MAGENTA</code>, <code>ORANGE</code>, <code>PINK</code>,
* <code>RED</code>, <code>WHITE</code>, or <code>YELLOW</code>.
* The case of the individual letters in the color name is ignored,
* as are spaces and underscores, so that the Java color
* <code>DARK_GRAY</code> could be written as <code>"Dark&nbsp;Gray"</code>.
*/

void setForeground(GWindow gw, string color);


/*
* Function: getColorGWindow
* Usage: color = getColorGWindow(gw);
Expand Down
2 changes: 2 additions & 0 deletions c/include/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
8 changes: 8 additions & 0 deletions c/src/gwindow.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 8 additions & 0 deletions c/src/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
30 changes: 30 additions & 0 deletions java/src/stanford/spl/JBECommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public static HashMap<String,JBECommand> 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());
Expand Down Expand Up @@ -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("(");
Expand Down