main soggy/cotton / src / main.c
 1#include "cotton.h"
 2#include "cottonwindow.h"
 3#include "interpreter.h"
 4#include <stdio.h>
 5#include <stdlib.h>
 6#include <string.h>
 7
 8int main
 9(int argc, char **argv)
10{
11
12    // added this specifically with the new user input command in mind, makes it so the terminal isnt so cluttered for when there is user input needed, atleast for me :P
13    printf("\033[2J\033[H");
14
15    // thank you chld for the idea of changing the scale and having it be optional ^^
16    int scale = 4;
17    char *filename = NULL;
18
19    if (argc == 4 && strcmp(argv[1], "-s") == 0) {
20        scale = atoi(argv[2]);
21        filename = argv[3];
22    } else if (argc == 2) {
23        filename = argv[1];
24    } else {
25        fprintf(stderr, "usage: cotton <-s scale (scale being 2 or 4)> <file.cot>\n");
26        return 1;
27    }
28
29    CottonWindow cw;
30
31    if (!cottonwindow_init(&cw, "cotton", VIDEO_WIDTH * scale, VIDEO_HEIGHT * scale, VIDEO_WIDTH, VIDEO_HEIGHT)) {
32        return 1;
33    }
34
35    Cotton cotton;
36    cotton_init(&cotton);
37
38    FILE *file = fopen(filename, "r");
39    if (!file) {
40        fprintf(stderr, "cotton couldn't open \"%s\" :(\n", filename);
41        return 1;
42    }
43
44    int pitch = sizeof(cotton.video[0]) * VIDEO_WIDTH;
45    int quit = 0;
46
47    while (!quit) {
48        quit = cottonwindow_process_input(&cw, NULL);
49
50        if (SDL_GetTicks() >= cotton.ticktock) {
51            cotton_interpret(&cotton, &cw, file);
52        }
53
54        cottonwindow_update(&cw, cotton.video, pitch);
55    }
56
57    fclose(file);
58    cottonwindow_destroy(&cw);
59    return 0;
60}