summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 6b217f1786e21658f6e65835850ced161fdc36bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <vector>

#include "SDL.h"
#include "Qualum.h"
#include "Rendering.h"
#include "Color.h"

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
const char SLASH = '\\';
#else
const char SLASH = '/';
#endif

SDL_Window* window;

float TIME_SCALE = 4.0f;

void quit()
{
    SDL_DestroyWindow(window);
    SDL_Quit();
}

char* empty_string()
{
    // To avoid write-string warnings
    char* s = (char*)malloc(1);
    *s = 0;
    return s;
}

char* getdirname(char* filename)
{
    // Gets, e.g. a/b/ from a/b/c.txt
    char* dir = (char*)malloc(strlen(filename)+1);
    strcpy(dir, filename);
    char* lastSlash = strrchr(dir, SLASH);
    if (lastSlash)
        *(lastSlash+1) = 0;
    else
        return empty_string();
    return dir;
}

char* getfilename(char* path)
{
    char* filename = (char*)malloc(strlen(path)+1);
    strcpy(filename, path);
    char* lastSlash = strrchr(path, SLASH);
    if (lastSlash)
        return lastSlash + 1;
    return filename;
}

void read_file(char* directory, char* filename, int x, int y, int speedx, int speedy)
{
    // Create the qualums from the given file
    // Increasing their positions by (x,y) and their speeds by (speedx, speedy)
    char* path = (char*)malloc(4096); // Full path to file
    sprintf(path, "%s%s", directory, filename);
    
    FILE* input_file = fopen(path, "r"); // Skip first line
    if (!input_file)
    {    
        fprintf(stderr, "File not found: %s\n", path);
        exit(1);
    }
    char* buffer = (char*)malloc(4096);
    fgets(buffer, 4096, input_file); // Skip first line
    
    int xrel, yrel, speedxrel, speedyrel; // Read input file
    char* color_str = (char*) malloc(4096);
    
    while (fgets(buffer, 4096, input_file))
    {
        buffer += strspn(buffer, " \t"); // Remove whitespace at start
        if (buffer[0] == '%') // Other data (width, height, etc.)
        {
            
            continue;
        }
       
        buffer[strcspn(buffer, "#\0")] = 0;
        if (sscanf(buffer, "%d %d %d %d %s", &xrel, &yrel, &speedxrel, &speedyrel, color_str) > 0)
        {
            if (color_str[0] == '!') // Include
            {
                color_str++;
                char* newdir = (char*) malloc(4096);
                strcpy(newdir, directory);
                strcat(newdir, getdirname(color_str));
                read_file(newdir, getfilename(color_str), x + xrel, y + yrel, speedx + speedxrel, speedy + speedyrel);
            }
            else // Color
            {
                Color color = Colors::read_color(color_str);
                Qualum::create_qualum(x + xrel, y + yrel, color, speedx + speedxrel, speedy + speedyrel);
            }
        }
    }
}

int main(int argc, char** argv) 
{
    if (argc < 2)
    {
        fprintf(stderr, "Error: No input file provided.\n");
        return 1;
    }
    
    Qualum::initialize();
    window = SDL_CreateWindow("Qualums", SDL_WINDOWPOS_UNDEFINED,
        SDL_WINDOWPOS_UNDEFINED, Rendering::WIDTH, Rendering::HEIGHT,
        SDL_WINDOW_SHOWN);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    
    SDL_Event event;
    bool will_quit = false;
    
    read_file(getdirname(argv[1]), getfilename(argv[1]), 0, 0, 0, 0);
    
    int i = 0, last_printed = -1;
    while (!will_quit)
    {
        i++;
        if (Qualum::iterations != last_printed)
        {
            printf("%d\n", Qualum::iterations);
            last_printed = Qualum::iterations;
        }
        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
        SDL_RenderClear(renderer);
        Qualum::update_all(i % ((int)(300/TIME_SCALE)) == 0);
        Qualum::render_all(renderer);
        SDL_RenderPresent(renderer);
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
                case SDL_QUIT:
                    will_quit = true;
                    break;
                case SDL_KEYDOWN:
                    if (event.key.keysym.sym >= SDLK_0 && event.key.keysym.sym <= SDLK_9)
                    {
                        TIME_SCALE = (float)(event.key.keysym.sym - SDLK_0);
                        break;
                    }
                    switch (event.key.keysym.sym)
                    {
                        case SDLK_SPACE:
                            Qualum::update_all(true);
                            break;
                        case SDLK_ESCAPE:
                            will_quit = true;
                            break;
                    }
                    break;
            }
        }
    }
    
    return 0;
}