Blame timer.c

2af670
2af670
#include <math.h></math.h>
2af670
#include <stdio.h></stdio.h>
2af670
#include <helianthus.h></helianthus.h>
2af670
2af670
b9036e
unsigned long long prevMonotonicMs;
b9036e
2af670
int hours, minutes, seconds;
b9036e
int mills;
2af670
2af670
int started;
2af670
int alarm;
2af670
2af670
Sound alarmSound;
2af670
2af670
2af670
void init() {
2af670
  alarmSound = createSound("data/sound/alarmclock.ogg");
2af670
}
2af670
2af670
2af670
void draw() {
b9036e
  unsigned long long monotonicMs = windowGetMonotonicMilliseconds();
b9036e
  int timeMs = (int)(monotonicMs - prevMonotonicMs);
b9036e
  prevMonotonicMs = monotonicMs;
b9036e
2af670
  if (started) {
2af670
    if (alarm) {
b9036e
      mills += timeMs;
b9036e
      while(mills >= 1000) mills -= 1000;
2af670
    } else {
b9036e
      mills += timeMs;
b9036e
      while(mills >= 1000) { mills -= 1000; --seconds; }
2af670
      while(seconds < 0) { --minutes; seconds += 60; }
2af670
      while(minutes < 0) { --hours; minutes += 60; }
2af670
      if (hours < 0 || (hours == 0 && minutes == 0 && seconds == 0)) {
2af670
        hours = 0;
2af670
        minutes = 0;
2af670
        seconds = 0;
2af670
        alarm = TRUE;
2af670
        soundPlay(alarmSound, TRUE);
2af670
      }
2af670
    }
2af670
  }
2af670
2af670
  saveState();
ef1455
  translate( windowGetWidth()/2.0, windowGetHeight()/2.0 );
2af670
2af670
  textAlign(HALIGN_CENTER, VALIGN_CENTER);
2af670
2af670
  double size = 100;
2af670
  int *values[3] = { &hours, &minutes, &seconds };
2af670
  for(int i = 0; i < 3; ++i) {
2af670
    int *value = values[i];
2af670
2af670
    saveState();
2af670
    translate( (i-1)*size, 0 );
2af670
    textSize(64);
b9036e
    if (alarm) stroke(colorByRGBA(0, 0, 0, mills*0.001));
2af670
ef1455
    double mx = mouseTransformedX();
ef1455
    double my = mouseTransformedY();
2af670
    if (fabs(mx) <= 0.45*size && fabs(my) <= 0.45*size) {
2af670
      *value += mouseScrolledY();
2af670
      while(*value <   0) *value += 60;
2af670
      while(*value >= 60) *value -= 60;
2af670
      if (mouseWentDown("left")) *value = 0;
2af670
      if (mouseWentDown("right")) *value = 30;
2af670
    }
2af670
ef1455
    textf(-0.2*size, 0, "%d", *value / 10);
ef1455
    textf( 0.2*size, 0, "%d", *value % 10);
2af670
2af670
    if (i > 0) {
2af670
      translate( -size/2, 0 );
ef1455
      text(0, 0, ":");
2af670
    }
2af670
2af670
    restoreState();
2af670
  }
2af670
2af670
  if (started) {
2af670
    saveState();
2af670
    strokeWidth(0.1*size);
b9036e
    if (alarm) stroke(colorByRGBA(0, 0, 0, mills*0.001)); else rotate(-60*0.001*mills);
2af670
    for(int i = 0; i < 12; ++i) {
2af670
      point(2*size, 0);
2af670
      rotate(-30);
2af670
    }
2af670
    restoreState();
2af670
  }
2af670
2af670
  saveState();
2af670
  {
2af670
    noFill();
2af670
    textSize(24);
2af670
    double buttonWidth = size*1.5;
2af670
    double buttonHeight = size*0.5;
2af670
    translate(-0.5*buttonWidth, 0.75*size);
2af670
ef1455
    double mx = mouseTransformedX();
ef1455
    double my = mouseTransformedY();
2af670
    int click = mouseWentDown("left") && fabs(mx) <= 0.4*buttonWidth && fabs(my) <= 0.4*buttonHeight;
2af670
    rect(-0.4*buttonWidth, -0.4*buttonHeight, 0.8*buttonWidth, 0.8*buttonHeight);
2af670
    if (started) {
ef1455
      text(0, 0, "pause");
2af670
      if (click) {
2af670
        soundStop(alarmSound);
2af670
        started = alarm = FALSE;
2af670
      }
2af670
    } else {
ef1455
      text(0, 0, "start");
b9036e
      if (click) { mills = 0; started = TRUE; }
2af670
    }
2af670
2af670
    translate(buttonWidth, 0);
2af670
    mx -= buttonWidth;
2af670
    click = mouseWentDown("left") && fabs(mx) <= 0.4*buttonWidth && fabs(my) <= 0.4*buttonHeight;
2af670
    rect(-0.4*buttonWidth, -0.4*buttonHeight, 0.8*buttonWidth, 0.8*buttonHeight);
ef1455
    text(0, 0, "reset");
2af670
    if (click) {
2af670
      soundStop(alarmSound);
2af670
      started = alarm = FALSE;
2af670
      hours = minutes= seconds = 0;
2af670
    }
2af670
  }
2af670
  restoreState();
2af670
2af670
  restoreState();
2af670
}
2af670
2af670
2af670
int main() {
e0033b
  windowSetTitle("Timer");
ef1455
  windowSetVariableFrameRate();
ef1455
  windowSetInit(&init);
ef1455
  windowSetDraw(&draw);
ef1455
  windowRun();
2af670
  return 0;
2af670
}