Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | None | 0 0
  1. #include "display.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6. #include <assert.h>
  7.  
  8. struct position {
  9.   int x;
  10.   int y;
  11. };
  12.  
  13. typedef struct position position;
  14.  
  15. struct state {
  16.   position current;
  17.   position prev;
  18.   bool pen;
  19.   display *d;
  20. };
  21.  
  22. typedef struct state state;
  23.  
  24. enum { DX, DY, DT, PEN};
  25.  
  26. int getOpcode(unsigned char bin) {
  27.   return (bin >> 6);
  28. }
  29.  
  30. int getOperand(unsigned char bin, int opcode) {
  31.   if (opcode == 0 || opcode == 1) {
  32.     return ((bin & 0x3F) - (((bin >> 5) & 1)*64));
  33.   }
  34.   else return (bin & 0x3F);
  35. }
  36.  
  37. char *initializeFile(char *args) {
  38.   char *file[11] = {"line", "square", "box", "oxo", "diag", "cross", "clear", "key", "pauses", "field", "lawn"};
  39.   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"};
  40.  for (int i = 0; i < 12; i++) {
  41.     if (strcmp(args, file[i]) == 0) {
  42.       return *(filename + i);
  43.     }
  44.   }
  45.   return "error";
  46. }
  47.  
  48.  
  49. void Operation(state *s, int value, int opcode) {
  50.   if(opcode == DX){
  51.     s->current.x += value;
  52.   }
  53.   else if(opcode == DY){
  54.     s->current.y += value;
  55.     if(s->pen == true){
  56.       line(s->d, s->prev.x, s->prev.y, s->current.x, s->current.y);
  57.     }
  58.     s->prev = s-> current;
  59.     }
  60.   else if(opcode == DT) {
  61.     pause(s->d, value *10);
  62.   }
  63.   else if(opcode == PEN) {
  64.     s -> pen = !s -> pen;
  65.   }
  66. }
  67.  
  68.  
  69.  
  70. void runFile(char *args) {
  71.     char *arg = initializeFile(args);
  72.     FILE *in = fopen(arg, "rb");
  73.     display *d = newDisplay(arg, 200, 200);
  74.     if (strcmp(args,"error")==0){
  75.         exit(1);
  76.     }
  77.     position start = {0, 0};
  78.     state s = {start, start, false, d};
  79.     unsigned char bin = fgetc(in);
  80.     while (! feof(in)) {
  81.       int opcode = getOpcode(bin);
  82.       int operand = getOperand(bin, opcode);
  83.       Operation(&s, operand, opcode);
  84.       bin = fgetc(in);
  85.     }
  86.     end(d);
  87.     fclose(in);
  88.   }
  89.  
  90.   int main(int n, char **args) {
  91.    if (n == 1) {
  92.       printf("x");
  93.     } else if (n == 2) {
  94.       runFile(args[1]);
  95.     return 0;
  96.   }
  97.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement