// -*- compile-command: "g++ -g -O0 -o event event.cpp $PLOT" -*-

//  File:    event.cpp
//  Author:  Andreas Wittmann <andreas.wittmann@wittnet.at>

//  Comment:

#include <cmath>
#include <iostream>

#include "plot.hpp"

using namespace std;

double deg2rad(double degrees)
{
    return degrees * M_PI / 180.0;
}

int x_offset = 0;
int y_factor = 200;
int iteration = 1;

void frame()
{
    PlotBackground(255, 255, 255);
    PlotCross();
    PlotPoint b;
    b.x = -plot_width / 2;
    b.y = sin(deg2rad(b.x - x_offset)) * y_factor;
    for (double sx = -plot_width / 2 + 1; sx <= plot_width / 2; ++sx) {
        PlotPoint a;
        a.x = sx;
        a.y = sin(deg2rad(sx - x_offset)) * y_factor;
        PlotLine(a, b);
        b = a;
    }
    PlotText(4, 480 / 2, "Frame: %03d", iteration++);
    x_offset -= 1;
}

void setup()
{
    PlotCanvas(640, 480, PLOT_MODE_CROSS);
}

bool event(SDL_Event* e)
{
    if (e->type == SDL_KEYDOWN) {
        switch (e->key.keysym.sym) {
        case SDLK_LEFT:
            printf("LEFT\n");
            x_offset -= 20;
            return true;
        case SDLK_RIGHT:
            printf("RIGHT\n");
            x_offset += 20;
            return true;
        case SDLK_UP:
            printf("UP\n");
            y_factor += 10;
            return true;
        case SDLK_DOWN:
            printf("DOWN\n");
            y_factor -= 10;
            return true;
        }
    }
    return false;
}
