commit e14c7e85767810f7b9cac39ccbbf65de797643f1 Author: Sheldon Lee Date: Fri Jun 12 02:46:44 2020 +0100 first commit diff --git a/grid.c b/grid.c new file mode 100644 index 0000000..ce4542c --- /dev/null +++ b/grid.c @@ -0,0 +1,39 @@ +#include +#include "grid.h" + +// maps x, y coordinate to array index of grid +unsigned int toIndex(Grid* grid, int x, int y) +{ + return (grid->width*y + x)%grid->size; +} + + +void clearGrid(Grid* grid) +{ + for (int i = 0; i < grid->size; i++) grid->arr[i]=false; +} + +void initGrid(Grid* grid, unsigned int width, unsigned int height) +{ + grid->size = width*height; + grid->width = width; + grid->arr = (bool*)malloc(sizeof(bool)*grid->size); +} + +void putPixel(Grid* grid, int x, int y) +{ + grid->arr[toIndex(grid, x, y)] = true; +} + +void drawGrid(Grid* grid) +{ + unsigned int width, height; + width = grid->width; + height = grid->size/width; + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + if (grid->arr[toIndex(grid, x, y)]) mvprintw(y, x, "x"); + else mvprintw(y, x, " "); + } + } +} diff --git a/grid.h b/grid.h new file mode 100644 index 0000000..1151ce4 --- /dev/null +++ b/grid.h @@ -0,0 +1,17 @@ +#include +#include + +typedef struct Grid { + unsigned int size; + unsigned int width; + bool* arr; +} Grid; + +void initGrid(Grid* grid, unsigned int width, unsigned int height); + +void clearGrid(Grid* grid); + +void putPixel(Grid* grid, int x, int y); + +void drawGrid(Grid* grid); + diff --git a/main.c b/main.c new file mode 100644 index 0000000..16598e5 --- /dev/null +++ b/main.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include +#include "grid.h" +#include "vect.h" + +void contain(int* pos, int* velocity, int min, int max); +int main() +{ + //init + initscr(); + //noecho(); + + curs_set(FALSE); + int width = 0; + int height = 0; + + // stdscr created by initscr() + getmaxyx(stdscr, height, width); + + Vect2i pos = {0, 0}; + Vect2i vel = {1, 1}; + + const int DELAY = 33*pow(10,3); + + Grid grid; + initGrid(&grid, width, height); + putPixel(&grid, pos.x, pos.y); + + while (1) { + clear(); + + clearGrid(&grid); + putPixel(&grid, pos.x, pos.y); + drawGrid(&grid); + + refresh(); + + moveVect2i(&pos, vel.x, vel.y); + contain(&pos.x, &vel.x, 0, width-1); + contain(&pos.y, &vel.y, 0, height-1); + + usleep(DELAY); + } + + endwin(); + return 0; +} + +void contain(int* pos, int* velocity, int min, int max) +{ + 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; + } +} diff --git a/makefile b/makefile new file mode 100644 index 0000000..4cffbd8 --- /dev/null +++ b/makefile @@ -0,0 +1,4 @@ +LDFLAGS=-lncurses + +main: main.c grid.c vect.c + gcc main.c grid.c vect.c -o main ${LDFLAGS} diff --git a/vect.c b/vect.c new file mode 100644 index 0000000..b2562a7 --- /dev/null +++ b/vect.c @@ -0,0 +1,14 @@ +#include "vect.h" + +void moveVect2i(Vect2i* vect, int x, int y) +{ + vect->x += x; + vect->y += y; +} + + +void moveVect2f(Vect2f* vect, float x, float y) +{ + vect->x += x; + vect->y += y; +} diff --git a/vect.h b/vect.h new file mode 100644 index 0000000..8c71000 --- /dev/null +++ b/vect.h @@ -0,0 +1,11 @@ +typedef struct Vect2i{ + int x, y; +} Vect2i; + +typedef struct Vect2f{ + float x, y; +} Vect2f; + +void moveVect2i(Vect2i* vect, int x, int y); + +void moveVect2f(Vect2f* vect, float x, float y);