2020-08-14 02:59:08 +00:00
|
|
|
#include <u.h>
|
|
|
|
#include <libc.h>
|
2020-08-15 21:01:10 +00:00
|
|
|
#include <draw.h>
|
2020-08-16 23:55:53 +00:00
|
|
|
#include <event.h>
|
2020-08-15 22:49:02 +00:00
|
|
|
#include "graphics.h"
|
2020-08-14 02:59:08 +00:00
|
|
|
|
2020-08-16 23:55:53 +00:00
|
|
|
enum {
|
2020-08-29 15:57:06 +00:00
|
|
|
framems = 20,
|
2020-08-16 23:55:53 +00:00
|
|
|
Kdel = 0x7f
|
|
|
|
};
|
2020-08-15 22:49:02 +00:00
|
|
|
|
|
|
|
/* TODO: move these sorts of things to something like state.c/.h. */
|
|
|
|
extern Point screenRes; /* TODO: ew, coupling. */
|
|
|
|
extern int r; /* TODO: also coupling. */
|
|
|
|
Point ballPos = {40, 40};
|
2020-08-18 02:31:48 +00:00
|
|
|
double ballXDelta = 10;
|
|
|
|
double ballYDelta = 10;
|
|
|
|
int killgame = 0;
|
2020-08-15 22:49:02 +00:00
|
|
|
|
|
|
|
void changeState(void) {
|
|
|
|
if (ballPos.x > screenRes.x - (r*3) || ballPos.x < -r) {
|
|
|
|
ballXDelta *= -1;
|
|
|
|
}
|
|
|
|
if (ballPos.y > screenRes.y - (r*3) || ballPos.y < -r) {
|
|
|
|
ballYDelta *= -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ballPos.x = ballPos.x + ballXDelta;
|
|
|
|
ballPos.y = ballPos.y + ballYDelta;
|
|
|
|
}
|
|
|
|
|
2020-08-16 23:55:53 +00:00
|
|
|
void main(void)
|
2020-08-16 17:23:41 +00:00
|
|
|
{
|
2020-08-15 21:01:10 +00:00
|
|
|
if(initdraw(nil, nil, "Another Plan") < 0) {
|
|
|
|
sysfatal("initdraw failed: %r");
|
|
|
|
}
|
2020-09-01 02:14:44 +00:00
|
|
|
resizewin(Pt(SCREEN_WIDTH * DEFAULT_SCALE, SCREEN_HEIGHT * DEFAULT_SCALE));
|
|
|
|
|
2020-08-22 01:34:00 +00:00
|
|
|
initGraphics(&ballPos);
|
2020-08-16 23:55:53 +00:00
|
|
|
einit(Emouse | Ekeyboard);
|
2020-08-16 17:23:41 +00:00
|
|
|
|
2020-08-18 02:31:48 +00:00
|
|
|
int child = rfork(RFPROC|RFMEM);
|
|
|
|
if (child) {
|
|
|
|
vlong startms = nsec()/1000000LL;
|
|
|
|
for(;;) {
|
|
|
|
changeState();
|
|
|
|
render();
|
|
|
|
flushimage(display, 1);
|
|
|
|
if (killgame) {
|
|
|
|
exits(0);
|
|
|
|
}
|
2020-08-20 03:52:10 +00:00
|
|
|
vlong endms = nsec()/1000000LL;
|
2020-08-18 02:31:48 +00:00
|
|
|
sleep (framems - (endms-startms));
|
|
|
|
startms = endms;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(;;) {
|
|
|
|
if( ecankbd() ) {
|
|
|
|
switch(ekbd()) {
|
|
|
|
case 'q': case Kdel: killgame = 1; exits(0); break;
|
|
|
|
}
|
2020-08-16 23:55:53 +00:00
|
|
|
}
|
2020-08-15 21:01:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-29 16:15:03 +00:00
|
|
|
}
|