diff options
| author | Aleksa Vučković <aleksav013@gmail.com> | 2022-06-01 17:02:08 +0200 |
|---|---|---|
| committer | Aleksa Vučković <aleksav013@gmail.com> | 2022-06-01 17:02:08 +0200 |
| commit | dd038cfb10cae6dba5afabc786a129224da5ef8c (patch) | |
| tree | e9d94f187db87f880a52be2c4e7c8e276477c9ab | |
| parent | 602037ec658da6ab2f04f1c806bfccdcd8125f27 (diff) | |
simple snake game
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | src/Makefile | 1 | ||||
| -rw-r--r-- | src/c/keyboard.c | 27 | ||||
| -rw-r--r-- | src/c/shell/game.c | 135 | ||||
| -rw-r--r-- | src/c/shell/neofetch.c | 40 | ||||
| -rw-r--r-- | src/c/shell/uptime.c | 10 | ||||
| -rw-r--r-- | src/c/timer.c | 9 | ||||
| -rw-r--r-- | src/c/tty.c | 53 | ||||
| -rw-r--r-- | src/c/vga.c | 3 | ||||
| -rw-r--r-- | src/include/source/shell/game.h | 8 | ||||
| -rw-r--r-- | src/include/source/shell/neofetch.h | 6 | ||||
| -rw-r--r-- | src/include/source/shell/uptime.h | 6 | ||||
| -rw-r--r-- | src/include/source/timer.h | 2 | ||||
| -rw-r--r-- | src/include/source/tty.h | 5 |
14 files changed, 253 insertions, 54 deletions
@@ -54,7 +54,7 @@ AS_OBJ=boot.o ioport.o gdt.o idt.o irq.o paging.o export AS_OBJECTS=$(patsubst %,$(AS_OBJECT_DIR)/%,$(AS_OBJ)) ## C OBJECTS -C_OBJ=gdt.o heap.o idt.o kernel.o keyboard.o keymap.o stdio.o string.o tty.o vga.o irq_handler.o stack_protector.o timer.o paging.o +C_OBJ=gdt.o heap.o idt.o kernel.o keyboard.o keymap.o stdio.o string.o tty.o vga.o irq_handler.o stack_protector.o timer.o paging.o shell/uptime.o shell/neofetch.o shell/game.o export C_OBJECTS=$(patsubst %,$(C_OBJECT_DIR)/%,$(C_OBJ)) ## ALL OBJECTS IN ORDER diff --git a/src/Makefile b/src/Makefile index 91c565b..dc331c6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -8,4 +8,5 @@ build_as: build_c: $(MKDIR) $(C_OBJECT_DIR) + $(MKDIR) $(C_OBJECT_DIR)/shell @$(MAKE) --directory c diff --git a/src/c/keyboard.c b/src/c/keyboard.c index 5242bb0..fb4361d 100644 --- a/src/c/keyboard.c +++ b/src/c/keyboard.c @@ -5,6 +5,7 @@ #include<source/keymap.h> #include<source/vga.h> #include<source/tty.h> +#include<source/shell/game.h> char buffer[BUFFER_LOG][BUFFER_SIZE]; size_t buffer_size[BUFFER_LOG]; @@ -53,7 +54,6 @@ void enter() } buffer_index=0; } - prompt(); return; } @@ -95,14 +95,8 @@ void keyright() { } -void keyboard_handler() +void tty_keyboard_handler(uint16_t keycode) { - ioport_out(PIC1_COMMAND_PORT, 0x20); - uint8_t status = ioport_in(KEYBOARD_STATUS_PORT); - - if (status & 0x1) - { - uint8_t keycode = ioport_in(KEYBOARD_DATA_PORT); if(keycode<0x80) { char c=charcode[keycode]; @@ -165,5 +159,22 @@ void keyboard_handler() { ispressed[keycode-0x80]=0; } + +} + +extern uint8_t process_id; +uint8_t process_id = PROCESS_TTY_ID; + +void keyboard_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + uint8_t status = ioport_in(KEYBOARD_STATUS_PORT); + + if (status & 0x1) + { + uint8_t keycode = ioport_in(KEYBOARD_DATA_PORT); + if (process_id == PROCESS_TTY_ID) tty_keyboard_handler(keycode); + else if (process_id == PROCESS_GAME_ID) game_keyboard_handler(keycode); + else printf("Keyboard is not bound to any process\n"); } } diff --git a/src/c/shell/game.c b/src/c/shell/game.c new file mode 100644 index 0000000..e7c5d1e --- /dev/null +++ b/src/c/shell/game.c @@ -0,0 +1,135 @@ +#include<source/shell/game.h> +#include<types.h> +#include<source/vga.h> +#include<source/tty.h> +#include<source/timer.h> +#include<source/stdio.h> + +extern uint16_t* terminal_buffer; +extern uint8_t terminal_color; +extern uint8_t process_id; + +#define N 1000 + +uint16_t x; +uint16_t y; +uint16_t rep[N]; + +uint32_t game_time; +uint32_t game_tick; +uint8_t gameover; +uint8_t duzina; +uint8_t smer; + +uint16_t jabuka; +uint16_t x_jabuke; +uint16_t y_jabuke; + +void game_keyboard_handler(uint16_t keycode) +{ + switch(keycode) + { + // space(); + case 0x39: + process_id = PROCESS_TTY_ID; + clear(); + prompt(); + break; + // keyup(); + case 72: + smer = 1; + break; + // keydown(); + case 80: + smer = 2; + break; + // keyleft(); + case 75: + smer = 3; + break; + // keyright(); + case 77: + smer = 4; + break; + } +} + +void game_init(void) +{ + x=VGA_WIDTH/2; + y=VGA_HEIGHT/2; + game_time=0; + game_tick=0; + duzina=1; + gameover=0; + smer=0; + + x_jabuke=5; + y_jabuke=5; + + process_id = PROCESS_GAME_ID; + clear(); +} + +void game_timer_handler() +{ + game_tick++; + if (game_tick == TICKS_PER_SECOND/8 && !gameover) + { + switch(smer) + { + case 1: + if(y>0) y--; + else y=VGA_HEIGHT-1; + break; + case 2: + if(y<VGA_HEIGHT) y++; + else y=0; + break; + case 3: + if(x>0) x--; + else x=VGA_WIDTH-1; + break; + case 4: + if(x<VGA_WIDTH) x++; + else x=0; + break; + default: + break; + } + game_tick=0; + game_time+=1; + + for(uint8_t i = duzina; i > 0; i--) rep[i]=rep[i-1]; + rep[0]=(uint16_t)(y*80+x); + + if (rep[0] == y_jabuke*80+x_jabuke) + { + duzina++; + + uint8_t ind; + do { + ind = 1; + x_jabuke = (uint16_t)((game_time*game_time)%80); + y_jabuke = (uint16_t)((game_time*game_time)%25); + jabuka = (uint16_t)(y_jabuke*80+x_jabuke); + for(uint8_t i = 0; i < duzina; i++) if(rep[i] == jabuka) ind=0; + game_time++; + } while(!ind); + + terminal_buffer[jabuka]=(uint16_t)(terminal_color<<8|'J'); + } + terminal_buffer[rep[duzina]]=(uint16_t)(terminal_color<<8|' '); + terminal_buffer[rep[0]]=(uint16_t)(terminal_color<<8|'#'); + + set_color(VGA_COLOR_RED,VGA_COLOR_BLACK); + terminal_buffer[y_jabuke*80+x_jabuke]=(uint16_t)(terminal_color<<8|'J'); + set_color(VGA_COLOR_LIGHT_GREY,VGA_COLOR_BLACK); + + for(uint8_t i = 1; i < duzina; i++) if(rep[0] == rep[i]) + { + printf("GAME OVER\nPress <Space> to return to shell\n"); + gameover=1; + } + } +} diff --git a/src/c/shell/neofetch.c b/src/c/shell/neofetch.c new file mode 100644 index 0000000..e770428 --- /dev/null +++ b/src/c/shell/neofetch.c @@ -0,0 +1,40 @@ +#include<source/shell/neofetch.h> +#include<source/shell/uptime.h> +#include<source/vga.h> +#include<source/stdio.h> +#include<source/timer.h> + +void neofetch(void) +{ + set_color(VGA_COLOR_WHITE,VGA_COLOR_BLACK); + printf(" . "); printf("Dobrodosli u moj \n"); + printf(" J:L (\"\"\") "); printf("operativni sistem :) \n"); + printf(" |:| III "); printf("Uzivajte! \n"); + printf(" |:| III "); printf(" \n"); + printf(" |:| III "); printf("Welcome to my \n"); + printf(" |:| __III__ "); printf("operating system :) \n"); + printf(" |:| /:-.___,-:\\ "); printf("Enjoy your stay! \n"); + printf(" |:| \\] |:| [/ "); printf(" \n"); + printf(" |:| |:| "); printf(" \n"); + printf(" |:| |:| "); printf(" \n"); + printf(" |:| |:| "); printf(" \n"); + printf(" /] |:| [\\ |:| "); printf(" \n"); + printf(" \\:-'\"\"\"`-:/ |:| "); printf(" \n"); + printf(" \"\"III\"\" |:| "); printf(" \n"); + printf(" III |:| "); printf(" \n"); + printf(" III |:| "); printf(" \n"); + printf(" III |:| "); printf("napravio/made by: \n"); + printf(" (___) J:F "); printf("Aleksa Vuckovic \n"); + printf(" \" "); printf(" \n"); + + for(size_t i=0;i<16;i++) + { + set_color(0,i); + printf(" ",i); + } + printf("\n"); + + + set_color(VGA_COLOR_LIGHT_GREY,VGA_COLOR_BLACK); + uptime(); +} diff --git a/src/c/shell/uptime.c b/src/c/shell/uptime.c new file mode 100644 index 0000000..fc78579 --- /dev/null +++ b/src/c/shell/uptime.c @@ -0,0 +1,10 @@ +#include<source/shell/uptime.h> +#include<source/stdio.h> +#include<types.h> + +extern uint32_t time; + +void uptime(void) +{ + printf("System uptime is: %d seconds\n",time); +} diff --git a/src/c/timer.c b/src/c/timer.c index 3a8f159..ff36882 100644 --- a/src/c/timer.c +++ b/src/c/timer.c @@ -2,12 +2,15 @@ #include<types.h> #include<asm.h> #include<source/stdio.h> +#include<source/tty.h> +#include<source/shell/game.h> uint32_t tick=0; -const uint32_t TICKS_PER_SECOND=50; extern uint32_t time; uint32_t time=0; +extern uint8_t process_id; + void timer_handler(void) { tick++; @@ -18,7 +21,9 @@ void timer_handler(void) } ioport_out(0x20, 0x20); - ioport_out(0xa0,0x20); + ioport_out(0xa0, 0x20); + + if (process_id == PROCESS_GAME_ID) game_timer_handler(); } void init_timer(uint32_t frequency) diff --git a/src/c/tty.c b/src/c/tty.c index 344c720..0ec1408 100644 --- a/src/c/tty.c +++ b/src/c/tty.c @@ -3,9 +3,9 @@ #include<source/string.h> #include<source/stdio.h> #include<source/vga.h> - - -extern uint32_t time; +#include<source/shell/uptime.h> +#include<source/shell/neofetch.h> +#include<source/shell/game.h> size_t pieces(char pieces[][CMD_LENGTH],char *buffer) { @@ -63,10 +63,6 @@ void number(size_t numberof,char parts[][CMD_LENGTH]) } } -void uptime(void) -{ - printf("System uptime is: %d seconds\n",time); -} void prompt(void) { @@ -85,47 +81,15 @@ void prompt(void) printf("$ "); } -void neofetch(void) -{ - set_color(VGA_COLOR_WHITE,VGA_COLOR_BLACK); - printf(" . "); printf("Dobrodosli u moj \n"); - printf(" J:L (\"\"\") "); printf("operativni sistem :) \n"); - printf(" |:| III "); printf("Uzivajte! \n"); - printf(" |:| III "); printf(" \n"); - printf(" |:| III "); printf("Welcome to my \n"); - printf(" |:| __III__ "); printf("operating system :) \n"); - printf(" |:| /:-.___,-:\\ "); printf("Enjoy your stay! \n"); - printf(" |:| \\] |:| [/ "); printf(" \n"); - printf(" |:| |:| "); printf(" \n"); - printf(" |:| |:| "); printf(" \n"); - printf(" |:| |:| "); printf(" \n"); - printf(" /] |:| [\\ |:| "); printf(" \n"); - printf(" \\:-'\"\"\"`-:/ |:| "); printf(" \n"); - printf(" \"\"III\"\" |:| "); printf(" \n"); - printf(" III |:| "); printf(" \n"); - printf(" III |:| "); printf(" \n"); - printf(" III |:| "); printf("napravio/made by: \n"); - printf(" (___) J:F "); printf("Aleksa Vuckovic \n"); - printf(" \" "); printf(" \n"); - - for(size_t i=0;i<16;i++) - { - set_color(0,i); - printf(" ",i); - } - printf("\n"); - - - set_color(VGA_COLOR_LIGHT_GREY,VGA_COLOR_BLACK); - uptime(); -} - void help(void) { printf("Currently available commands:\n"); - printf("clear echo merge ls number uptime neofetch help\n"); + printf("clear echo game merge ls number uptime neofetch help\n"); } +extern uint8_t process_id; +extern uint16_t* terminal_buffer; + void tty(char *buffer) { char parts[CMD_LENGTH][CMD_LENGTH]; @@ -139,5 +103,8 @@ void tty(char *buffer) else if(stringcmp(parts[0],"uptime")) uptime(); else if(stringcmp(parts[0],"neofetch")) neofetch(); else if(stringcmp(parts[0],"help")) help(); + else if(stringcmp(parts[0],"game")) game_init(); else printf("command not found: %s\n",parts[0]); + + if (!stringcmp(parts[0],"game")) prompt(); } diff --git a/src/c/vga.c b/src/c/vga.c index ac1e409..84eb538 100644 --- a/src/c/vga.c +++ b/src/c/vga.c @@ -3,6 +3,9 @@ #include<source/string.h> #include<asm.h> +extern uint16_t* terminal_buffer; +extern uint8_t terminal_color; + size_t terminal_row; size_t terminal_column; uint8_t terminal_color; diff --git a/src/include/source/shell/game.h b/src/include/source/shell/game.h new file mode 100644 index 0000000..f6dcbc8 --- /dev/null +++ b/src/include/source/shell/game.h @@ -0,0 +1,8 @@ +#ifndef SOURCE_SHELL_GAME +#define SOURCE_SHELL_GAME + +#include<types.h> + +void game_keyboard_handler(uint16_t keycode); + +#endif diff --git a/src/include/source/shell/neofetch.h b/src/include/source/shell/neofetch.h new file mode 100644 index 0000000..d629d3c --- /dev/null +++ b/src/include/source/shell/neofetch.h @@ -0,0 +1,6 @@ +#ifndef SOURCE_SHELL_NEOFETCH +#define SOURCE_SHELL_NEOFETCH + +void neofetch(void); + +#endif diff --git a/src/include/source/shell/uptime.h b/src/include/source/shell/uptime.h new file mode 100644 index 0000000..740b7e3 --- /dev/null +++ b/src/include/source/shell/uptime.h @@ -0,0 +1,6 @@ +#ifndef SOURCE_SHELL_UPTIME +#define SOURCE_SHELL_UPTIME + +void uptime(void); + +#endif diff --git a/src/include/source/timer.h b/src/include/source/timer.h index bfdc4cd..dcbeca3 100644 --- a/src/include/source/timer.h +++ b/src/include/source/timer.h @@ -1,6 +1,8 @@ #ifndef SOURCE_TIMER_H #define SOURCE_TIMER_H +#define TICKS_PER_SECOND 50 + #include<types.h> void timer_handler(void); diff --git a/src/include/source/tty.h b/src/include/source/tty.h index 2b22eff..9684e15 100644 --- a/src/include/source/tty.h +++ b/src/include/source/tty.h @@ -5,6 +5,9 @@ #define CMD_LENGTH 20 +#define PROCESS_TTY_ID 1 +#define PROCESS_GAME_ID 2 + size_t pieces(char pieces[][CMD_LENGTH], char *buffer); void echo(size_t numberof, char parts[][CMD_LENGTH]); void merge(char parts[][CMD_LENGTH]); @@ -14,6 +17,8 @@ void uptime(void); void prompt(void); void neofetch(void); void help(void); +void game_init(void); void tty(char *buffer); +void tty_keyboard_handler(uint16_t keycode); #endif |
