Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "display.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- #include <assert.h>
- struct position {
- int x;
- int y;
- };
- typedef struct position position;
- struct state {
- position current;
- position prev;
- bool pen;
- display *d;
- };
- typedef struct state state;
- enum { DX, DY, DT, PEN};
- int getOpcode(unsigned char bin) {
- return (bin >> 6);
- }
- int getOperand(unsigned char bin, int opcode) {
- if (opcode == 0 || opcode == 1) {
- return ((bin & 0x3F) - (((bin >> 5) & 1)*64));
- }
- else return (bin & 0x3F);
- }
- char *initializeFile(char *args) {
- char *file[11] = {"line", "square", "box", "oxo", "diag", "cross", "clear", "key", "pauses", "field", "lawn"};
- char *filename[12] = {"line.sketch", "square.sketch", "box.sketch", "oxo.sketch", "diag.sketch", "cross.sketch", "clear.sketch", "key.sketch", "pauses.sketch", "field.sketch", "lawn.sketch"};
- for (int i = 0; i < 12; i++) {
- if (strcmp(args, file[i]) == 0) {
- return *(filename + i);
- }
- }
- return "error";
- }
- void Operation(state *s, int value, int opcode) {
- if(opcode == DX){
- s->current.x += value;
- }
- else if(opcode == DY){
- s->current.y += value;
- if(s->pen == true){
- line(s->d, s->prev.x, s->prev.y, s->current.x, s->current.y);
- }
- s->prev = s-> current;
- }
- else if(opcode == DT) {
- pause(s->d, value *10);
- }
- else if(opcode == PEN) {
- s -> pen = !s -> pen;
- }
- }
- void runFile(char *args) {
- char *arg = initializeFile(args);
- FILE *in = fopen(arg, "rb");
- display *d = newDisplay(arg, 200, 200);
- if (strcmp(args,"error")==0){
- exit(1);
- }
- position start = {0, 0};
- state s = {start, start, false, d};
- unsigned char bin = fgetc(in);
- while (! feof(in)) {
- int opcode = getOpcode(bin);
- int operand = getOperand(bin, opcode);
- Operation(&s, operand, opcode);
- bin = fgetc(in);
- }
- end(d);
- fclose(in);
- }
- int main(int n, char **args) {
- if (n == 1) {
- printf("x");
- } else if (n == 2) {
- runFile(args[1]);
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement