first commit
This commit is contained in:
		
						commit
						e14c7e8576
					
				
							
								
								
									
										39
									
								
								grid.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								grid.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,39 @@
 | 
				
			|||||||
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					#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, " ");
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										17
									
								
								grid.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								grid.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,17 @@
 | 
				
			|||||||
 | 
					#include <stdbool.h>
 | 
				
			||||||
 | 
					#include <ncurses.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
							
								
								
									
										62
									
								
								main.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								main.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,62 @@
 | 
				
			|||||||
 | 
					#include <stdio.h>
 | 
				
			||||||
 | 
					#include <stdbool.h>
 | 
				
			||||||
 | 
					#include <ncurses.h>
 | 
				
			||||||
 | 
					#include <unistd.h>
 | 
				
			||||||
 | 
					#include <math.h>
 | 
				
			||||||
 | 
					#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;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										4
									
								
								makefile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								makefile
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
				
			|||||||
 | 
					LDFLAGS=-lncurses
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					main: main.c grid.c vect.c
 | 
				
			||||||
 | 
						gcc main.c grid.c vect.c -o main ${LDFLAGS}
 | 
				
			||||||
							
								
								
									
										14
									
								
								vect.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								vect.c
									
									
									
									
									
										Normal file
									
								
							@ -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;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user