2020-06-12 09:46:44 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <ncurses.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <math.h>
|
2020-06-18 08:44:21 +08:00
|
|
|
#include <time.h>
|
2020-06-12 09:46:44 +08:00
|
|
|
#include "grid.h"
|
|
|
|
#include "vect.h"
|
|
|
|
|
2020-06-13 05:50:03 +08:00
|
|
|
|
|
|
|
static void contain(int* pos, int* velocity, int min, int max);
|
|
|
|
|
2020-06-12 09:46:44 +08:00
|
|
|
int main()
|
|
|
|
{
|
2020-06-18 08:44:21 +08:00
|
|
|
// init
|
|
|
|
bool running = true;
|
2020-06-12 09:46:44 +08:00
|
|
|
initscr();
|
2020-06-18 08:44:21 +08:00
|
|
|
raw();
|
|
|
|
// doesn't wait for user to input.
|
|
|
|
// timeout(100) waits for 100ms for input.
|
|
|
|
timeout(0);
|
2020-06-12 09:46:44 +08:00
|
|
|
curs_set(FALSE);
|
2020-06-18 08:44:21 +08:00
|
|
|
|
2020-06-12 09:46:44 +08:00
|
|
|
int width = 0;
|
|
|
|
int height = 0;
|
|
|
|
|
2020-06-18 08:44:21 +08:00
|
|
|
// stdscr is screen created by initscr()
|
2020-06-12 09:46:44 +08:00
|
|
|
getmaxyx(stdscr, height, width);
|
|
|
|
|
2020-06-18 08:44:21 +08:00
|
|
|
// hz
|
|
|
|
const int FRAME_RATE = 100;
|
2020-06-15 08:12:14 +08:00
|
|
|
|
|
|
|
const int DELAY = (float)pow(10,6)/(float)FRAME_RATE;
|
2020-06-12 09:46:44 +08:00
|
|
|
|
|
|
|
Grid grid;
|
|
|
|
initGrid(&grid, width, height);
|
2020-06-15 08:12:14 +08:00
|
|
|
randomizeGrid(&grid);
|
2020-06-12 09:46:44 +08:00
|
|
|
|
2020-06-18 08:44:21 +08:00
|
|
|
clock_t t = 0;
|
2020-06-13 05:50:03 +08:00
|
|
|
while (running) {
|
2020-06-18 08:44:21 +08:00
|
|
|
clock_t start_t = clock();
|
|
|
|
char ch = getch();
|
|
|
|
if (ch == 'q') running = false;
|
|
|
|
|
2020-06-12 09:46:44 +08:00
|
|
|
drawGrid(&grid);
|
|
|
|
|
|
|
|
refresh();
|
|
|
|
|
2020-06-15 08:12:14 +08:00
|
|
|
updateGrid(&grid);
|
2020-06-18 08:44:21 +08:00
|
|
|
t += clock()-start_t;
|
2020-06-12 09:46:44 +08:00
|
|
|
|
|
|
|
usleep(DELAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
endwin();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-13 05:50:03 +08:00
|
|
|
static void contain(int* pos, int* velocity, int min, int max)
|
2020-06-12 09:46:44 +08:00
|
|
|
{
|
|
|
|
bool above_max = max < *pos;
|
|
|
|
bool below_min = *pos < min;
|
|
|
|
bool not_in_range = below_min || above_max;
|
|
|
|
if (not_in_range) {
|
|
|
|
if (below_min) *pos = min;
|
|
|
|
else if (above_max) *pos = max;
|
|
|
|
*velocity *= -1;
|
|
|
|
}
|
|
|
|
}
|