gr-sim: add another demo

This commit is contained in:
Vince Weaver 2017-05-02 15:21:32 -04:00
parent f75da270f7
commit 453ca6bdb2
5 changed files with 268 additions and 4 deletions

View File

@ -1,11 +1,11 @@
CC = gcc
CFLAGS = -Wall -O2
CFLAGS = -Wall -O2 -g
LFLAGS =
SDL_LIBS= `sdl-config --libs`
SDL_INCLUDE= `sdl-config --cflags`
all: rainbow
all: rainbow sparkle kaleido
rainbow: rainbow.o gr-sim.o
$(CC) $(LFLAGS) $(SDL_LIBS) -o rainbow rainbow.o gr-sim.o
@ -14,10 +14,24 @@ rainbow.o: rainbow.c
$(CC) $(CFLAGS) -c rainbow.c
kaleido: kaleido.o gr-sim.o
$(CC) $(LFLAGS) $(SDL_LIBS) -o kaleido kaleido.o gr-sim.o
kaleido.o: kaleido.c
$(CC) $(CFLAGS) -c kaleido.c
sparkle: sparkle.o gr-sim.o
$(CC) $(LFLAGS) $(SDL_LIBS) -o sparkle sparkle.o gr-sim.o
sparkle.o: sparkle.c
$(CC) $(CFLAGS) -c sparkle.c
gr-sim.o: gr-sim.c
$(CC) $(CFLAGS) $(SDL_INCLUDE) -c gr-sim.c
clean:
rm -f *~ *.o gr-sim rainbow
rm -f *~ *.o gr-sim rainbow sparkle kaleido

View File

@ -137,11 +137,28 @@ int grsim_init(void) {
static int current_color=0;
int color_equals(int new_color) {
current_color=new_color;
current_color=new_color%16;
return 0;
}
int plot(int x, int y) {
if (x>40) {
printf("X too big %d\n",x);
return -1;
}
if (y>40) {
printf("Y too big %d\n",y);
return -1;
}
if (x<0) {
printf("X too small %d\n",x);
return -1;
}
if (y<0) {
printf("Y too small %d\n",y);
return -1;
}
framebuffer[x][y]=current_color;
return 0;
}
@ -164,4 +181,11 @@ int vlin(int y1, int y2, int at) {
return 0;
}
int gr(void) {
int x,y;
/* Init screen */
for(y=0;y<YSIZE;y++) for(x=0;x<XSIZE;x++) framebuffer[x][y]=0;
return 0;
}

View File

@ -6,3 +6,4 @@ int color_equals(int new_color);
int plot(int x, int y);
int hlin(int x1, int x2, int at);
int vlin(int y1, int y2, int at);
int gr(void);

118
gr-sim/kaleido.c Normal file
View File

@ -0,0 +1,118 @@
/* Based on code from "32 BASIC Programs for the Apple Computer" */
/* by Tom Rugg and Phil Feldman */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "gr-sim.h"
static int r[11];
static int x,y,j,a,b,x2,y2,n;
static void tooo(void) {
color_equals(r[n]);
plot(x2,y2);
return;
}
static void noo(void) {
int t,w;
// 900
color_equals(r[0]);
plot(x,y);
grsim_update();
if (j==1) return;
// 920
w=j/2;
t=j-w-1;
// 930
for(n=1;n<=w;n++) {
if (x==a) {
y2=y; x2=x+n; tooo();
x2=x-n; tooo();
continue;
}
if (y==b) {
x2=x; y2=y+n; tooo();
y2=y-n; tooo();
continue;
}
y2=y;
if (x<a) {
x2=x+n; tooo();
}
else {
x2=x-n; tooo();
}
// 990
x2=x;
if (y<b) {
y2=y+n; tooo();
}
else {
y2=y-n; tooo();
}
}
return;
}
int main(int argc, char **argv) {
int ch;
int p,d,m,k,l;
grsim_init();
// 120
gr();
// 125
p=19;
// 130
a=p; b=p; d=-1;
// 135
m=15;
label150:
// 150
for(j=0;j<=10;j++) {
r[j]=rand()%m;
}
// 180
d=-d; k=1; l=p;
if (d<=0) {
k=p;l=1;
}
// 200
for(j=k;j<=l;j+=d) {
x=a+j; y=b; noo();
x=a-j; noo();
x=a; y=b+j; noo();
y=b-j; noo();
x=a+j;y=b+j; noo();
x=a-j;y=b-j; noo();
y=b+j; noo();
x=a+j; y=b-j; noo();
}
for(j=1;j<10;j++) {
ch=grsim_input();
if (ch=='q') exit(0);
if (ch==' ') {
while(grsim_input()!=' ') usleep(10000);
}
usleep(10000);
}
goto label150;
return 0;
}

107
gr-sim/sparkle.c Normal file
View File

@ -0,0 +1,107 @@
/* Based on code from "32 BASIC Programs for the Apple Computer" */
/* by Tom Rugg and Phil Feldman */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "gr-sim.h"
int main(int argc, char **argv) {
int ch;
int j,k;
int t;
int r,w,c,m,n,l;
int x,y;
#define S 19
int a[S+1],b[S+1];
grsim_init();
// 120
gr();
// 125
// 130
x=19,y=19;
label140:
// 140
t=rand()%16;
// 150
for(j=0;j<=S;j++) {
a[j]=j;
b[j]=j;
}
// 160+170
for(j=0;j<=S;j++) {
r=rand()%(S+1);
w=a[j];
a[j]=a[r];
a[r]=w;
}
// 180+190
for(j=0;j<=S;j++) {
r=rand()%(S+1);
w=b[j];
b[j]=b[r];
b[r]=w;
}
// 200
for(j=0;j<=S;j++) {
for(k=0;k<=S;k++) {
// 210
r=a[j];
w=b[k];
c=r+w+t;
// 220
color_equals(c);
// 240
if (x+r>40) {
printf("ERROR! %d %d\n",x,r);
return -1;
}
plot(x+r,y+w);
plot(x+r,y-w);
plot(x-r,y-w);
plot(x-r,y+w);
plot(x+w,y+r);
plot(x+w,y-r);
plot(x-w,y-r);
plot(x-w,y+r);
grsim_update();
//320
}
}
// 350
for(j=1;j<10;j++) {
ch=grsim_input();
if (ch=='q') exit(0);
if (ch==' ') {
while(grsim_input()!=' ') usleep(10000);
}
usleep(10000);
}
// 400
m=15;
// 405
n=(random()%21)+10;
// 410
for(j=1;j<=n;j++) {
r=(random()%22)+1;
w=random()%m;
color_equals(w);
for(l=(y-S);l<=(y+S);l+=(r/4)+1) {
for(k=(x-S);k<=(x+S);k+=r) {
plot(k,l);
}
}
}
goto label140;
return 0;
}