Kevin Song
I'm a student at UT (that's the one in Austin) who studies things.
Devices which allow us to interact with computers.
Examples:
// Main Loop
void draw(){
if (checkQuit()){
exit_application;
}
doBigHeavyComputation();
}
Even short delays (~100ms) in processing input feel absolutely awful to the user. Extra FeelsBad points if the delay is variable.
DALL-E still can't quite do text correctly...
Typically, when an event is triggered, the program will call a function. In most general-purpose languages, it is up to us to specify which functions are called on which events.
void handle_sigint(int signum) {
printf("Received SIGINT signal. Exiting...\n");
exit(0);
}
int main() {
struct sigaction sa;
sa.sa_handler = handle_sigint;
sigaction(SIGINT, &sa, NULL) == -1;
// Rest of the program
}
When Processing receives an event, it sets several special variables and tries to call an event handler.
If no event handler exists, then the handler is not called.
Examples of event handlers:
mousePressed()
mouseReleased()
mouseMoved()
mouseDragged()
keyPressed()
keyReleased()
mousePressed
: stores whether mouse button is pressed or notmouseButton
: stores the most recently pressed button, one of LEFT
, CENTER
, or RIGHT
mouseX,mouseY
: The coordinates of the mouse cursor in the current windowpmouseX
, pmouseY
: The coordinate of the mouse cursor in the window on the previous frame (useful for determining the direction of mouse motion, e.g. for dragging).void setup(){
size(800,800);
}
void mousePressed(){
if (mouseButton == LEFT){
textSize(128);
text("LEFT", mouseX, mouseY);
} else {
textSize(168);
text("RIGHT",mouseX, mouseY);
}
}
void draw(){}
Why is the draw() function empty?
keyPressed
: stores whether a key has is pressed or notBACKSPACE
, TAB
, ENTER
, RETURN
, ESC
, DELETE
keyCode:
stores keypresses for non-ASCII characters. Useful values include ALT
, CONTROL
, SHIFT
, UP
, DOWN
, LEFT
, RIGHT
int x = 0;
int y = 0;
void setup(){
size(800,800);
}
void keyPressed(){
if(keyCode == DOWN){ y += 5; }
else if (keyCode == UP){ y -= 5; }
else if (keyCode == LEFT){ x -= 5; }
// Why not else?
else if (keyCode == RIGHT){ x += 5; }
}
void draw(){
fill(0);
background(200); // Reset the screen
ellipse(x, y, 30, 30);
}
A kind of system-generated event which is triggered every 16.66 ms (60 times per second).
When event triggers, calls the draw() function.
public static void main(String[] args){
// Create a timer which fires every 16ms
Trigger draw_tr = create_timer(16);
// Tell the program that when the event
// occurs, it should respond by calling draw()
register_event_handler(draw_tr, draw());
setup();
wait_for_exit();
}
But we as the programmer can control when this happens!
noLoop()
stops the draw()
commandloop()
resumes the draw()
commandredraw()
executes the draw()
command exactly onceThese should be thought of as efficiency tools. If your code relies on noLoop()
or loop()
for correctness, it is probably broken!
int x = 0;
int y = 0;
void setup(){
size(200,200);
}
void keyPressed(){
if(keyCode == DOWN){ y += 5; }
else if (keyCode == UP){ y -= 5; }
else if (keyCode == LEFT){ x -= 5; }
// Why not else?
else if (keyCode == RIGHT){ x += 5; }
}
void draw(){
fill(0);
background(200);
expensive_operation();
ellipse(x, y, 30, 30);
}
int x = 0;
int y = 0;
void setup(){
size(200,200);
noLoop();
redraw();
}
void keyPressed(){
if(keyCode == DOWN){ y += 5; }
else if (keyCode == UP){ y -= 5; }
else if (keyCode == LEFT){ x -= 5; }
// Why not else?
else if (keyCode == RIGHT){ x += 5; }
redraw();
}
void draw(){
fill(0);
background(200); // Reset the screen
expensive_operation();
ellipse(x, y, 30, 30);
}
mousePressed
and mouseButton
in the draw() loop to control the background color of the sketch.draw()
and reimplement this in the mousePressed()
function.mouseX
and mouseY
in the mouseMoved()
function to draw a circle that follows the mouse.keyPressed()
function.keyReleased()
but keep the background color changes and color.By Kevin Song
I'm a student at UT (that's the one in Austin) who studies things.