Blame timer.c

2af670
2af670
#include <math.h></math.h>
2af670
#include <stdio.h></stdio.h>
2af670
#include <helianthus.h></helianthus.h>
2af670
2af670
2af670
int hours, minutes, seconds;
2af670
double subSeconds;
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() {
2af670
  if (started) {
2af670
    if (alarm) {
2af670
      subSeconds += worldGetFrameTime();
2af670
      while(subSeconds >= 1) subSeconds -= 1;
2af670
    } else {
2af670
      subSeconds += worldGetFrameTime();
2af670
      while(subSeconds >= 1) { subSeconds -= 1; --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();
2af670
  translate( worldGetWidth()/2.0, worldGetHeight()/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);
2af670
    if (alarm) stroke(colorByRGBA(0, 0, 0, subSeconds));
2af670
2af670
    double mx = transformedMouseX();
2af670
    double my = transformedMouseY();
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
2af670
    char buf[32] = {};
2af670
    sprintf(buf, "%d", *value / 10);
2af670
    text(buf, -0.2*size, 0);
2af670
    sprintf(buf, "%d", *value % 10);
2af670
    text(buf,  0.2*size, 0);
2af670
2af670
    if (i > 0) {
2af670
      translate( -size/2, 0 );
2af670
      text(":", 0, 0);
2af670
    }
2af670
2af670
    restoreState();
2af670
  }
2af670
2af670
  if (started) {
2af670
    saveState();
2af670
    strokeWidth(0.1*size);
2af670
    if (alarm) stroke(colorByRGBA(0, 0, 0, subSeconds)); else rotate(-60*subSeconds);
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
2af670
    double mx = transformedMouseX();
2af670
    double my = transformedMouseY();
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) {
2af670
      text("pause", 0, 0);
2af670
      if (click) {
2af670
        soundStop(alarmSound);
2af670
        started = alarm = FALSE;
2af670
      }
2af670
    } else {
2af670
      text("start", 0, 0);
2af670
      if (click) { subSeconds = 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);
2af670
    text("reset", 0, 0);
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() {
2af670
  worldSetVariableFrameRate();
2af670
  worldSetInit(&init);
2af670
  worldSetDraw(&draw);
2af670
  worldRun();
2af670
  return 0;
2af670
}