Skip to content

Commit d343c3a

Browse files
committed
Add pingpong board and two bat
1 parent 8f7604a commit d343c3a

File tree

6 files changed

+164
-24
lines changed

6 files changed

+164
-24
lines changed

src/kernel/syscall/graphics.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ static int _graphics_init() {
1212
return -1;
1313
}
1414
_graphics_prev_mode = graphics_get_mode();
15-
int err = graphics_set_mode(GRAPHIC_MODE_320x200x16);
15+
int err = graphics_set_mode(GRAPHIC_MODE_320x200x256);
1616
return err;
1717
}
1818

src/usr/include/graphics.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#pragma once
22

3-
#define GRAPHICS_MAX_HEIGHT 320
4-
#define GRAPHICS_MAX_WIDTH 200
3+
#define GRAPHICS_MAX_WIDTH 320
4+
#define GRAPHICS_MAX_HEIGHT 200
55

66
#define DETECT 0
77
#define GRAPHIC_DRIVER_VGA 1
88

9-
#define GRAPHIC_MODE_320x200x16 0x13
9+
#define GRAPHIC_MODE_320x200x256 0x13
1010

1111
#define BLACK 0x0
1212
#define BLUE 0x1
@@ -34,8 +34,12 @@ int graphresult();
3434
void closegraph();
3535
void cleardevice();
3636

37+
int graphflush();
3738

3839
void setcolor(int color);
3940
int getcolor();
4041
void setbkcolor(int color);
4142
int getbkcolor();
43+
44+
void putpixel_noflush(int x, int y, int color);
45+
void putpixel(int x, int y, int color);

src/usr/include/stdlib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ int atoi (const char *s);
77
// And only base 10 is considered as signed int.
88
void itoa(int num, char *s, int base);
99
int min(int, int);
10+
int max(int, int);
11+
int abs(int a);
1012

1113
void exit(int status);

src/usr/lib/graphics.c

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void initgraph(int *graphdetect, int *graphmode, char *not_used) {
1818
// only one graphics driver is supported for now.
1919
return -4;
2020
}
21-
*graphmode = GRAPHIC_MODE_320x200x16;
21+
*graphmode = GRAPHIC_MODE_320x200x256;
2222
gstate.last_err = SYSCALL_A1(SYSCALL_GRAPHICS, SYSCALL_GRAPHICS_INITGRAPH);
2323
}
2424

@@ -32,7 +32,7 @@ void closegraph() {
3232
gstate.last_err = SYSCALL_A1(SYSCALL_GRAPHICS, SYSCALL_GRAPHICS_CLOSEGRAPH);
3333
}
3434

35-
static int graphflush() {
35+
int graphflush() {
3636
gstate.last_err = SYSCALL_A2(SYSCALL_GRAPHICS, SYSCALL_GRAPHICS_COPYBUFFER, BUFFER);
3737
return gstate.last_err;
3838
}
@@ -48,35 +48,70 @@ void setviewport(int left, int top, int right, int bottom, int clip) {
4848
}
4949

5050
int getmaxx() {
51-
// TODO
51+
return GRAPHICS_MAX_WIDTH-1;
5252
}
5353

5454
int getmaxy() {
55-
// TODO
55+
return GRAPHICS_MAX_HEIGHT-1;
5656
}
5757

5858
void setcolor(int color) {
59-
// TODO
59+
gstate.color = color;
6060
}
6161

62-
int getcolor() {
63-
// TODO
62+
inline int getcolor() {
63+
return gstate.color;
6464
}
6565

66-
void setbkcolor(int color) {
66+
inline void setbkcolor(int color) {
6767
gstate.bkcolor = color;
6868
}
6969

7070
int getbkcolor() {
71-
// TODO
71+
return gstate.bkcolor;
7272
}
7373

7474
void putpixel(int x, int y, int color) {
75-
// TODO
75+
BUFFER[y][x]=color;
76+
graphflush();
77+
}
78+
79+
void putpixel_noflush(int x, int y, int color) {
80+
BUFFER[y][x]=color;
7681
}
7782

7883
void line(int x1, int y1, int x2, int y2) {
79-
// TODO
84+
const int color = getcolor();
85+
if(abs(x1-x2)>=abs(y1-y2)) {
86+
// horizontal length is longer
87+
if(x1>x2) {
88+
int t;
89+
t = x1; x1 = x2; x2 = t;
90+
t = y1; y1 = y2; y2 = t;
91+
}
92+
// x1<=x2
93+
const int xdiff = x2-x1;
94+
const int ydiff = y2-y1;
95+
for (int x = x1; x <= x2; x++) {
96+
int y = ydiff*(x-x1)/xdiff;
97+
BUFFER[y][x]=color;
98+
}
99+
} else {
100+
// vertical length is longer
101+
if(y1>y2) {
102+
int t;
103+
t = x1; x1 = x2; x2 = t;
104+
t = y1; y1 = y2; y2 = t;
105+
}
106+
// y1<=y2
107+
const int xdiff = x2-x1;
108+
const int ydiff = y2-y1;
109+
for (int y = y1; y <= y2; y++) {
110+
int x = y*xdiff/ydiff+x1;
111+
BUFFER[y][x]=color;
112+
}
113+
}
114+
graphflush();
80115
}
81116

82117
void drawpoly(int num, int polypoints[]) {
@@ -88,15 +123,28 @@ void filpoly(int num, int polypoints[]) {
88123
}
89124

90125
void rectangle(int left, int top, int right, int bottom) {
91-
line(left, top, right, top);
92-
line(left, bottom, right, bottom);
93-
line(left, top, left, bottom);
94-
line(right, top, right, bottom);
126+
const int color = getcolor();
127+
128+
for(int x=left;x<=right;x++) {
129+
BUFFER[top][x]=color;
130+
BUFFER[bottom][x]=color;
131+
}
132+
for(int y=top+1;y<bottom;y++) {
133+
BUFFER[y][left]=color;
134+
BUFFER[y][right]=color;
135+
}
136+
graphflush();
95137
}
96138

97139
void bar(int left, int top, int right, int bottom) {
98-
// filled rectangle
99-
// TODO
140+
const int color = getcolor();
141+
142+
for(int y=top;y<=bottom;y++) {
143+
for(int x=left;x<=right;x++) {
144+
BUFFER[y][x]=color;
145+
}
146+
}
147+
graphflush();
100148
}
101149

102150
void ellipse(int x, int y, int x_radius, int y_radius) {

src/usr/lib/stdlib.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ int min(int a, int b) {
66
return (a<b)?a:b;
77
}
88

9+
int max(int a, int b) {
10+
return (a>b)?a:b;
11+
}
12+
13+
int abs(int a) {
14+
return (a>=0)?a:-a;
15+
}
16+
917
int atoi(const char *s) {
1018
int neg = 0;
1119
if ((*s) == '-' || (*s) == '+') {

src/usr/local/src/pingpong.c

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,89 @@
33
#include <conio.h>
44
#include <graphics.h>
55

6-
void game() {
6+
#define DIR_UP -1
7+
#define DIR_DOWN 1
8+
#define PLAYER_1 1
9+
#define PLAYER_2 2
10+
11+
const int WINDOW_HEIGHT = GRAPHICS_MAX_HEIGHT;
12+
const int WINDOW_WIDTH = GRAPHICS_MAX_WIDTH;
13+
14+
const int BAT_HALF_HEIGHT = 15;
15+
const int BAT_HALF_WIDTH = 2;
16+
const int BAT_SPEED = 5;
17+
18+
const int bat_miny = BAT_HALF_HEIGHT;
19+
const int bat_maxy = (WINDOW_HEIGHT-1)-BAT_HALF_HEIGHT;
20+
21+
const int p1_x = 10;
22+
const int p2_x = (WINDOW_WIDTH-1)-p1_x;
23+
24+
struct playerState {
25+
int y;
26+
};
27+
28+
struct {
29+
struct playerState p1, p2;
30+
} gstate;
31+
32+
void game_init() {
33+
gstate.p1.y = (bat_miny+bat_maxy)/2;
34+
gstate.p2 = gstate.p1;
35+
}
36+
37+
void draw_board() {
738
// clear screen
8-
setbkcolor(WHITE);
39+
setbkcolor(LIGHT_GREEN);
940
cleardevice();
10-
getch();
41+
42+
setcolor(BLUE);
43+
bar(p1_x-BAT_HALF_WIDTH, gstate.p1.y-BAT_HALF_HEIGHT,
44+
p1_x+BAT_HALF_WIDTH, gstate.p1.y+BAT_HALF_HEIGHT);
45+
46+
setcolor(RED);
47+
bar(p2_x-BAT_HALF_WIDTH, gstate.p2.y-BAT_HALF_HEIGHT,
48+
p2_x+BAT_HALF_WIDTH, gstate.p2.y+BAT_HALF_HEIGHT);
49+
}
50+
51+
void move_bat(int player_id, int dir) {
52+
struct playerState* p = NULL;
53+
if(player_id==PLAYER_1) {
54+
p = &gstate.p1;
55+
} else if(player_id==PLAYER_2) {
56+
p = &gstate.p2;
57+
} else {
58+
return;
59+
}
60+
61+
if(dir==DIR_UP) {
62+
p->y -= BAT_SPEED;
63+
} else if(dir==DIR_DOWN) {
64+
p->y += BAT_SPEED;
65+
}
66+
67+
// keep bat in bounds
68+
p->y = max(bat_miny, min(bat_maxy, p->y));
69+
}
70+
71+
void game() {
72+
game_init();
73+
while (1) {
74+
draw_board();
75+
76+
char c = getch();
77+
if(c=='e') {
78+
return;
79+
} else if(c=='w') {
80+
move_bat(PLAYER_1, DIR_UP);
81+
} else if(c=='s') {
82+
move_bat(PLAYER_1, DIR_DOWN);
83+
} else if(c=='p') {
84+
move_bat(PLAYER_2, DIR_UP);
85+
} else if(c=='l') {
86+
move_bat(PLAYER_2, DIR_DOWN);
87+
}
88+
}
1189
}
1290

1391
int main(int argc,char *argv[]) {

0 commit comments

Comments
 (0)