anotherplan/anotherplan.c

66 lines
1.3 KiB
C

#include <u.h>
#include <libc.h>
#include <draw.h>
#include <event.h>
#include "graphics.h"
enum {
framems = 20,
Kdel = 0x7f
};
/* 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};
double ballXDelta = 10;
double ballYDelta = 10;
int killgame = 0;
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;
}
void main(void)
{
if(initdraw(nil, nil, "Another Plan") < 0) {
sysfatal("initdraw failed: %r");
}
resizewin(Pt(SCREEN_WIDTH * DEFAULT_SCALE, SCREEN_HEIGHT * DEFAULT_SCALE));
initGraphics(&ballPos);
einit(Emouse | Ekeyboard);
int child = rfork(RFPROC|RFMEM);
if (child) {
vlong startms = nsec()/1000000LL;
for(;;) {
changeState();
render();
flushimage(display, 1);
if (killgame) {
exits(0);
}
vlong endms = nsec()/1000000LL;
sleep (framems - (endms-startms));
startms = endms;
}
} else {
for(;;) {
if( ecankbd() ) {
switch(ekbd()) {
case 'q': case Kdel: killgame = 1; exits(0); break;
}
}
}
}
}