summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPreston Pan <preston@nullring.xyz>2023-02-06 11:21:04 -0800
committerPreston Pan <preston@nullring.xyz>2023-02-06 11:21:04 -0800
commit516bd04e96d1ae7138a4a7d3413216167247f446 (patch)
tree8a103a0b6787ec1d8b1ce36ec38f15df0a423120
parent030695f7a6834d0e32d960e37a164b8f757e7cef (diff)
switch to ISO c99 standard; more correct install command
-rw-r--r--Makefile19
-rw-r--r--colors.h118
-rw-r--r--doc/snake3.1 (renamed from doc/snake3.man)0
-rw-r--r--snake.c29
4 files changed, 88 insertions, 78 deletions
diff --git a/Makefile b/Makefile
index ceb2cce..0cfadc2 100644
--- a/Makefile
+++ b/Makefile
@@ -1,17 +1,18 @@
CC=gcc
-CFLAGS=-I. -pthread
-DESTDIR=/usr/local/bin
-MANDIR=/usr/share/man/
+CFLAGS=-I. -pthread -Wall -Wextra -std=c99 -Wpedantic
+PREFIX ?= /usr
+BINDIR ?= ${PREFIX}/bin
+MANDIR = ${PREFIX}/share/man/
+
generate:
$(CC) -o snake3 snake.c vector.c conio.c $(CFLAGS)
-install:
- cp snake3 $(DESTDIR)
- cp doc/snake3.man $(MANDIR)/man1/snake3.1
-
+install: snake3
+ install -d ${DESTDIR}${BINDIR} ${DESTDIR}${MANDIR}/man1
+ install snake3 ${DESTDIR}${BINDIR}
+ install -m 644 doc/snake3.1 ${DESTDIR}${MANDIR}/man1
clean:
rm snake3
uninstall:
- -rm $(DESTDIR)/snake3
- -rm $(MANDIR)/man1/snake3.1
+ -rm -f ${DESTDIR}${BINDIR}/snake3 ${DESTDIR}${MANDIR}/man1/snake3.1
diff --git a/colors.h b/colors.h
index d6cfab0..1516661 100644
--- a/colors.h
+++ b/colors.h
@@ -3,78 +3,78 @@
#ifndef COLORS_H
#define COLORS_H
-#define BLK "\e[0;30m"
-#define RED "\e[0;31m"
-#define GRN "\e[0;32m"
-#define YEL "\e[0;33m"
-#define BLU "\e[0;34m"
-#define MAG "\e[0;35m"
-#define CYN "\e[0;36m"
-#define WHT "\e[0;37m"
+#define BLK "\033[0;30m"
+#define RED "\033[0;31m"
+#define GRN "\033[0;32m"
+#define YEL "\033[0;33m"
+#define BLU "\033[0;34m"
+#define MAG "\033[0;35m"
+#define CYN "\033[0;36m"
+#define WHT "\033[0;37m"
//Regular bold text
-#define BBLK "\e[1;30m"
-#define BRED "\e[1;31m"
-#define BGRN "\e[1;32m"
-#define BYEL "\e[1;33m"
-#define BBLU "\e[1;34m"
-#define BMAG "\e[1;35m"
-#define BCYN "\e[1;36m"
-#define BWHT "\e[1;37m"
+#define BBLK "\033[1;30m"
+#define BRED "\033[1;31m"
+#define BGRN "\033[1;32m"
+#define BYEL "\033[1;33m"
+#define BBLU "\033[1;34m"
+#define BMAG "\033[1;35m"
+#define BCYN "\033[1;36m"
+#define BWHT "\033[1;37m"
//Regular underline text
-#define UBLK "\e[4;30m"
-#define URED "\e[4;31m"
-#define UGRN "\e[4;32m"
-#define UYEL "\e[4;33m"
-#define UBLU "\e[4;34m"
-#define UMAG "\e[4;35m"
-#define UCYN "\e[4;36m"
-#define UWHT "\e[4;37m"
+#define UBLK "\033[4;30m"
+#define URED "\033[4;31m"
+#define UGRN "\033[4;32m"
+#define UYEL "\033[4;33m"
+#define UBLU "\033[4;34m"
+#define UMAG "\033[4;35m"
+#define UCYN "\033[4;36m"
+#define UWHT "\033[4;37m"
//Regular background
-#define BLKB "\e[40m"
-#define REDB "\e[41m"
-#define GRNB "\e[42m"
-#define YELB "\e[43m"
-#define BLUB "\e[44m"
-#define MAGB "\e[45m"
-#define CYNB "\e[46m"
-#define WHTB "\e[47m"
+#define BLKB "\033[40m"
+#define REDB "\033[41m"
+#define GRNB "\033[42m"
+#define YELB "\033[43m"
+#define BLUB "\033[44m"
+#define MAGB "\033[45m"
+#define CYNB "\033[46m"
+#define WHTB "\033[47m"
//High intensty background
-#define BLKHB "\e[0;100m"
-#define REDHB "\e[0;101m"
-#define GRNHB "\e[0;102m"
-#define YELHB "\e[0;103m"
-#define BLUHB "\e[0;104m"
-#define MAGHB "\e[0;105m"
-#define CYNHB "\e[0;106m"
-#define WHTHB "\e[0;107m"
+#define BLKHB "\033[0;100m"
+#define REDHB "\033[0;101m"
+#define GRNHB "\033[0;102m"
+#define YELHB "\033[0;103m"
+#define BLUHB "\033[0;104m"
+#define MAGHB "\033[0;105m"
+#define CYNHB "\033[0;106m"
+#define WHTHB "\033[0;107m"
//High intensty text
-#define HBLK "\e[0;90m"
-#define HRED "\e[0;91m"
-#define HGRN "\e[0;92m"
-#define HYEL "\e[0;93m"
-#define HBLU "\e[0;94m"
-#define HMAG "\e[0;95m"
-#define HCYN "\e[0;96m"
-#define HWHT "\e[0;97m"
+#define HBLK "\033[0;90m"
+#define HRED "\033[0;91m"
+#define HGRN "\033[0;92m"
+#define HYEL "\033[0;93m"
+#define HBLU "\033[0;94m"
+#define HMAG "\033[0;95m"
+#define HCYN "\033[0;96m"
+#define HWHT "\033[0;97m"
//Bold high intensity text
-#define BHBLK "\e[1;90m"
-#define BHRED "\e[1;91m"
-#define BHGRN "\e[1;92m"
-#define BHYEL "\e[1;93m"
-#define BHBLU "\e[1;94m"
-#define BHMAG "\e[1;95m"
-#define BHCYN "\e[1;96m"
-#define BHWHT "\e[1;97m"
+#define BHBLK "\033[1;90m"
+#define BHRED "\033[1;91m"
+#define BHGRN "\033[1;92m"
+#define BHYEL "\033[1;93m"
+#define BHBLU "\033[1;94m"
+#define BHMAG "\033[1;95m"
+#define BHCYN "\033[1;96m"
+#define BHWHT "\033[1;97m"
//Reset
-#define reset "\e[0m"
-#define CRESET "\e[0m"
-#define COLOR_RESET "\e[0m"
+#define reset "\033[0m"
+#define CRESET "\033[0m"
+#define COLOR_RESET "\033[0m"
#endif
diff --git a/doc/snake3.man b/doc/snake3.1
index 1cd8446..1cd8446 100644
--- a/doc/snake3.man
+++ b/doc/snake3.1
diff --git a/snake.c b/snake.c
index fd37da5..ec1f79f 100644
--- a/snake.c
+++ b/snake.c
@@ -11,19 +11,27 @@
#include "conio.h"
#include "colors.h"
-volatile sig_atomic_t INT_CTRL_C = 0;
+#define UNUSED(x) (void)(x)
-//unsafe functions like printf cannot be called in async interrupt handler
-void interrupt_handler(int sig) {
- INT_CTRL_C = 1;
-}
+/*** GLOBAL VARIABLES ***/
+volatile sig_atomic_t INT_CTRL_C = 0; //flag for ctrl-c interrupt
+int SCORE = 0;
int boardSize = 15;
const int updateInterval = 300000000L;
+
vectorp_t snakePositions;
vector_t foodPos;
vector_t lastMoveDir;
+void interrupt_handler(int sig); // passed into signal(3) to handle ctrl-c interrupts
+
+//unsafe functions like printf cannot be called in async interrupt handler
+void interrupt_handler(int sig) {
+ UNUSED(sig);
+ INT_CTRL_C = 1;
+}
+
int main(int argc, char **argv) {
for (int i = 0; i < argc; i ++) {
if (strcmp("--version", argv[i]) == 0) {
@@ -46,7 +54,7 @@ int main(int argc, char **argv) {
int smallestAxis = width / 2 < height ? width / 2 : height;
boardSize = smallestAxis < boardSize ? smallestAxis : boardSize;
//hide cursor
- printf("\e[?25l");
+ printf("\033[?25l");
//make a new thread handling interrupts
signal(SIGINT, interrupt_handler);
//init values
@@ -76,10 +84,10 @@ int main(int argc, char **argv) {
//handle ctrl-c interrupt
if (INT_CTRL_C) {
//unhide cursor
- printf("\e[?25h");
+ printf("\033[?25h");
//kill the listener thread
*cancellationToken = 1;
- printf("Press any key to exit\n");
+ printf("Your score was %d. Press any key to exit\n", SCORE);
pthread_join(listenerID, NULL);
free(cancellationToken);
free((int*)lastInput);
@@ -106,6 +114,7 @@ int main(int argc, char **argv) {
int ateFood = 0;
if((snakePositions.vectors[0].mx + lastMoveDir.mx == foodPos.mx) && (snakePositions.vectors[0].my + lastMoveDir.my == foodPos.my)) {
ateFood = 1;
+ SCORE = SCORE + 1;
//move the food
while(1) {
foodPos = init_vector(1 + (rand() % (boardSize - 2)), 1 + (rand() % (boardSize - 2)));
@@ -139,8 +148,8 @@ int main(int argc, char **argv) {
}
//clear the last getch call from the listener
*cancellationToken = 1;
- printf("\n%sYou died. Press any key to exit%s\n", RED, COLOR_RESET);
- printf("\e[?25h");
+ printf("\n%sYou died. Score: %d. Press any key to exit%s\n", RED, SCORE, COLOR_RESET);
+ printf("\033[?25h");
pthread_join(listenerID, NULL);
free(cancellationToken);
free((int*)lastInput);