Skip to content

Commit 859580f

Browse files
committed
[pingpong] Game with moving ball
1 parent d343c3a commit 859580f

File tree

10 files changed

+109
-12
lines changed

10 files changed

+109
-12
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
int syscall_0_keyboard(int operation,int a1,int a2,int a3, int user_ds);

src/drivers/keyboard/keyboard.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ char keyboard_get_key_pressed_poll() {
174174
return c;
175175
}
176176

177+
int keyboard_get_kbhit() {
178+
for (int i = 0; !keyboard_scanner_ascii_is_available() && i < 100; i++) {
179+
keyboard_scanner_step();
180+
}
181+
return keyboard_scanner_ascii_is_available()>0;
182+
}
183+
177184
void keyboard_init() {
178185
unsigned char original_colors = get_color_fgbg();
179186
sleep_mini(3000000);

src/drivers/keyboard/keyboard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ void keyboard_init();
44

55
char keyboard_get_key_pressed_blocking();
66
char keyboard_get_key_pressed_poll();
7+
int keyboard_get_kbhit();

src/kernel/syscall/keyboard.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <fuzzy/kernel/syscall/keyboard.h>
2+
3+
#include <conio.h>
4+
5+
static int _keyboard_getch() {
6+
return keyboard_get_key_pressed_poll();
7+
}
8+
9+
static int _keyboard_kbhit() {
10+
return keyboard_get_kbhit();
11+
}
12+
13+
int syscall_0_keyboard(int operation,int a1,int a2,int a3, int user_ds) {
14+
switch (operation) {
15+
case SYSCALL_KEYBOARD_SUB_GETCH:
16+
return _keyboard_getch();
17+
case SYSCALL_KEYBOARD_SUB_KBHIT:
18+
return _keyboard_kbhit();
19+
}
20+
return -1;
21+
}

src/kernel/syscall/syscall.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <fuzzy/kernel/syscall/file_handler.h>
44
#include <fuzzy/kernel/syscall/console.h>
55
#include <fuzzy/kernel/syscall/graphics.h>
6+
#include <fuzzy/kernel/syscall/keyboard.h>
67
#include <fuzzy/kernel/syscall/syscall.h>
78

89
#include <sys/syscall.h>
@@ -14,16 +15,12 @@
1415

1516
int SYSCALL_TABLE[SYSCALL_SIZE];
1617

17-
int syscall_0_keyboard_getch(int a0,int a1,int a2,int a3, int user_ds) {
18-
return keyboard_get_key_pressed_poll();
19-
}
20-
2118
extern int interrupt_handler_0x32_syscall_handler();
2219

2320
void interrupt_register_0x32_syscall() {
2421
print_log("Registering syscalls.");
2522
populate_idt_entry_32bit(IDT_SYSCALL, (unsigned int)interrupt_handler_0x32_syscall_handler, 0, 1);
26-
SYSCALL_TABLE[SYSCALL_KEYBOARD]=syscall_0_keyboard_getch;
23+
SYSCALL_TABLE[SYSCALL_KEYBOARD]=syscall_0_keyboard;
2724
SYSCALL_TABLE[SYSCALL_PROCESS]=syscall_1_process;
2825
SYSCALL_TABLE[SYSCALL_FILE_OP]=syscall_2_file_handler;
2926
SYSCALL_TABLE[SYSCALL_CONSOLE]=syscall_3_console;

src/usr/include/conio.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#pragma once
22

3-
char getch();
3+
#define SYSCALL_KEYBOARD_SUB_GETCH 0
4+
#define SYSCALL_KEYBOARD_SUB_KBHIT 1
5+
6+
int getch();
7+
int kbhit();
48

59
void clrscr();

src/usr/include/graphics.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ int getbkcolor();
4343

4444
void putpixel_noflush(int x, int y, int color);
4545
void putpixel(int x, int y, int color);
46+
47+
void line(int x1, int y1, int x2, int y2);
48+
void rectangle(int left, int top, int right, int bottom);
49+
void bar(int left, int top, int right, int bottom);
50+
void fillellipse(int xcenter, int ycenter, int x_radius, int y_radius);

src/usr/lib/conio.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
#include <sys/syscall.h>
22
#include <stdio.h>
3+
#include <conio.h>
34

4-
char getch() {
5+
int getch() {
56
// syscall is irq blocking,
67
// so try to keep them as short as possible.
78
while (1) {
8-
char c = SYSCALL_A0(SYSCALL_KEYBOARD);
9+
char c = SYSCALL_A1(SYSCALL_KEYBOARD, SYSCALL_KEYBOARD_SUB_GETCH);
910
if (c) return c;
1011
yield();
1112
}
1213
}
1314

15+
int kbhit() {
16+
return SYSCALL_A1(SYSCALL_KEYBOARD, SYSCALL_KEYBOARD_SUB_KBHIT);
17+
}
18+
1419
void clrscr() {
1520
SYSCALL_A1(SYSCALL_CONSOLE, SYSCALL_CONSOLE_SUB_CLRSCR);
16-
}
21+
}

src/usr/lib/graphics.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,29 @@ void ellipse(int x, int y, int x_radius, int y_radius) {
151151
// TODO
152152
}
153153

154-
void fillellipse(int x, int y, int x_radius, int y_radius) {
155-
// TODO
154+
void fillellipse(int xcenter, int ycenter, int x_radius, int y_radius) {
155+
// x**2/a**2 + y**2/b**2 <= 1
156+
// (x*b)**2 + (y*a)**2 <= (a*b)**2
157+
const int color = getcolor();
158+
159+
int t3 = x_radius*y_radius;
160+
t3 *= t3;
161+
for (int y = -y_radius; y <= y_radius; y++) {
162+
for (int x = -x_radius; x <= x_radius; x++) {
163+
int t1 = (x*y_radius);
164+
t1 *= t1;
165+
int t2 = (y*x_radius);
166+
t2 *= t2;
167+
if (t1+t2 <= t3) {
168+
int fx = xcenter + x;
169+
int fy = ycenter + y;
170+
if(fx>=0 && fy>=0 && fx<GRAPHICS_MAX_WIDTH && fy<GRAPHICS_MAX_HEIGHT) {
171+
BUFFER[fy][fx]=color;
172+
}
173+
}
174+
}
175+
}
176+
graphflush();
156177
}
157178

158179
void floodfill(int x,int y, int color) {

src/usr/local/src/pingpong.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,41 @@ const int bat_maxy = (WINDOW_HEIGHT-1)-BAT_HALF_HEIGHT;
2121
const int p1_x = 10;
2222
const int p2_x = (WINDOW_WIDTH-1)-p1_x;
2323

24+
const int BALL_SPEED = 2;
25+
const int BALL_RADIUS = 3;
26+
2427
struct playerState {
2528
int y;
2629
};
2730

31+
struct {
32+
int x,y;
33+
int xspeed, yspeed;
34+
} ball;
35+
2836
struct {
2937
struct playerState p1, p2;
3038
} gstate;
3139

40+
void frame_wait() {
41+
// busy wait
42+
volatile int counter = 1e7;
43+
while (counter--);
44+
}
45+
3246
void game_init() {
3347
gstate.p1.y = (bat_miny+bat_maxy)/2;
3448
gstate.p2 = gstate.p1;
49+
50+
ball.x = WINDOW_WIDTH/2;
51+
ball.y = WINDOW_HEIGHT/2;
52+
ball.xspeed = BALL_SPEED*0.8;
53+
ball.yspeed = BALL_SPEED*0.6;
3554
}
3655

3756
void draw_board() {
3857
// clear screen
39-
setbkcolor(LIGHT_GREEN);
58+
setbkcolor(GREEN);
4059
cleardevice();
4160

4261
setcolor(BLUE);
@@ -46,6 +65,9 @@ void draw_board() {
4665
setcolor(RED);
4766
bar(p2_x-BAT_HALF_WIDTH, gstate.p2.y-BAT_HALF_HEIGHT,
4867
p2_x+BAT_HALF_WIDTH, gstate.p2.y+BAT_HALF_HEIGHT);
68+
69+
setcolor(WHITE);
70+
fillellipse(ball.x, ball.y, BALL_RADIUS, BALL_RADIUS);
4971
}
5072

5173
void move_bat(int player_id, int dir) {
@@ -68,11 +90,22 @@ void move_bat(int player_id, int dir) {
6890
p->y = max(bat_miny, min(bat_maxy, p->y));
6991
}
7092

93+
void move_ball() {
94+
ball.x += ball.xspeed;
95+
ball.y += ball.yspeed;
96+
}
97+
7198
void game() {
7299
game_init();
73100
while (1) {
74101
draw_board();
75102

103+
move_ball();
104+
105+
if(!kbhit()) {
106+
frame_wait();
107+
continue;
108+
}
76109
char c = getch();
77110
if(c=='e') {
78111
return;

0 commit comments

Comments
 (0)