summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2018-03-24 00:25:38 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2018-03-24 00:25:38 -0400
commit4801bd34ac44fd28ec71600eaaf91ee9917f7440 (patch)
treecdf2576e1eb1e642cadade2f8d5a9aab4ff7d379
parent4c7f3585f8408b9d2a97b73dd104563d9a0a113e (diff)
Added variables.
-rw-r--r--README.md24
-rw-r--r--examples/add8test2.qua1
-rw-r--r--examples/orangetest.qua4
-rw-r--r--src/main.cpp95
4 files changed, 107 insertions, 17 deletions
diff --git a/README.md b/README.md
index c9cb00c..ffcf7a2 100644
--- a/README.md
+++ b/README.md
@@ -94,7 +94,7 @@ x y speedX speedY color
```
#### Modifying properties
-% is used to modify properties in .qua files. Here is a list of properties:
+`%` is used to modify properties in .qua files. Here is a list of properties:
- width - Controls the width of the screen in qualums
- height - Controls the height of the screen in qualums
@@ -112,3 +112,25 @@ x y s s color
%scale 1
```
This creates a 1x3 pixel window.
+
+#### Variables
+`%` can also be used to create variables. There are two types of variables: global and local. Global variables can be accessed by any file after they are defined. Local variables can only be accessed in the file they are defined in. Local variables must start with `.`. `$` is used to access a variable. For example,
+
+`a.qua`:
+```
+x y s s color
+%.color BL # This can only be accessed in a.qua
+%x 50 # This can be accessed in a.qua and b.qua
+$x 5 0 0 $.color
+0 0 0 0 !b.qua
+```
+`b.qua`:
+```
+x y s s color
+# a.qua's .color cannot be accessed from here.
+# However, a local variable .color can also be created here.
+%.color GE
+$x 6 0 0 $.color # This will be green
+```
+
+You can also access properties' (width, etc.) values using `$`.
diff --git a/examples/add8test2.qua b/examples/add8test2.qua
deleted file mode 100644
index 8b13789..0000000
--- a/examples/add8test2.qua
+++ /dev/null
@@ -1 +0,0 @@
-
diff --git a/examples/orangetest.qua b/examples/orangetest.qua
deleted file mode 100644
index f8ef442..0000000
--- a/examples/orangetest.qua
+++ /dev/null
@@ -1,4 +0,0 @@
-x y speedX speedY color
-50 0 0 1 GE
-50 50 0 0 GA
-100 50 0 0 OR
diff --git a/src/main.cpp b/src/main.cpp
index c95cf87..f700d97 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,5 +1,10 @@
#include <iostream>
#include <vector>
+#include <map>
+#include <string>
+#include <sstream>
+#include <algorithm>
+#include <iterator>
#include "SDL.h"
#include "Qualum.h"
@@ -14,7 +19,9 @@ const char SLASH = '/';
SDL_Window* window;
+int global_file_identifier = 0; // What the global file count is at (used for creating local variables)
double TIME_SCALE = 4.0;
+std::map<std::string, std::string> variables;
void quit()
{
@@ -75,12 +82,29 @@ void set_property(const char* property, const char* value)
{
TIME_SCALE = double_value;
}
+ variables.insert(std::make_pair<std::string,std::string>(std::string(property), std::string(value)));
+}
+
+const char* get_property(const char* property)
+{
+ try
+ {
+ return variables.at(property).c_str();
+ }
+ catch (...)
+ {
+ fprintf(stderr, "Variable not found: %s", property);
+ exit(1);
+ }
}
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)
+
+ int file_identifier = global_file_identifier++; // Used for creating local variables
+
char* path = (char*)malloc(4096); // Full path to file
sprintf(path, "%s%s", directory, filename);
@@ -104,34 +128,83 @@ void read_file(char* directory, char* filename, int x, int y, int speedx, int sp
if (buffer[0] == '%') // Other data (width, height, etc.)
{
buffer++;
+ char* variable = (char*) malloc(4096);
sscanf(buffer, "%s %s", property, value);
str_tolower(property);
- set_property(property, value);
+ if (property[0] == '.')
+ {
+ property++;
+ sprintf(variable, "%d %s", file_identifier, property);
+ }
+ else
+ {
+ strcpy(variable, property);
+ }
+ set_property(variable, value);
+
continue;
}
buffer[strcspn(buffer, "#\0")] = 0;
- if (sscanf(buffer, "%d %d %d %d %s", &xrel, &yrel, &speedxrel, &speedyrel, color_str) > 0)
+
+ std::string line(buffer);
+ std::istringstream iss(line);
+ std::vector<std::string> tokens{std::istream_iterator<std::string>(iss),
+ std::istream_iterator<std::string>()}; // Split line
+
+ int* int_ptrs[] = {&xrel, &yrel, &speedxrel, &speedyrel, NULL};
+ int index = 0;
+ for (std::string token: tokens)
{
- if (color_str[0] == '!') // Include
+ if (int_ptrs[index]) // Set integer values (x pos, y pos, x speed, y speed)
{
- 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);
+ if (token[0] == '$')
+ {
+ if (token[1] == '.') // Local
+ {
+ sprintf(property, "%d %s", file_identifier, token.c_str() + 2); // Create a variable with file-dependent name, e.g. ".foo" -> "1 foo"
+ }
+ else // Global
+ {
+ strcpy(property, token.c_str());
+ property++;
+ }
+ str_tolower(property);
+ *int_ptrs[index] = atoi(get_property(property));
+ }
+ else
+ {
+ if (sscanf(token.c_str(), "%d", int_ptrs[index]) <= 0)
+ continue;
+ }
}
- else // Color
+ else // Set color/include
{
- Color color = Colors::read_color(color_str);
- Qualum::create_qualum(x + xrel, y + yrel, color, speedx + speedxrel, speedy + speedyrel);
+ strcpy(color_str, token.c_str());
+ 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);
+ }
}
+ index++;
}
+
}
}
int main(int argc, char** argv)
{
+
+
if (argc < 2)
{
fprintf(stderr, "Error: No input file provided.\n");