summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2018-03-23 21:01:28 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2018-03-23 21:01:28 -0400
commitacb5114c79a9bc157602d75c6ee76a898980117e (patch)
tree912d33855c69d09d379e692bd6313c7692b33e11
parentf4456093b25812e3e890d29c0734f87bfd0d4df2 (diff)
Added properties.
-rw-r--r--README.md26
-rw-r--r--examples/add8test.qua3
-rwxr-xr-xqualumsbin35224 -> 35720 bytes
-rw-r--r--src/Qualum.cpp4
-rw-r--r--src/Qualum.h2
-rw-r--r--src/Rendering.cpp21
-rw-r--r--src/Rendering.h8
-rw-r--r--src/main.cpp39
8 files changed, 94 insertions, 9 deletions
diff --git a/README.md b/README.md
index c27b987..44374a8 100644
--- a/README.md
+++ b/README.md
@@ -41,6 +41,13 @@ The qualum repository includes several example .qua files (including an 8-bit ad
./qualums add8test.qua
```
You can change the numbers added in `add8test.qua` by editing the file.
+
+#### Keyboard shortcuts
+Escape - Quit
+0 - Pause
+1-9 - Sets the speed to 1-9x (the default is 4x)
+Space - Advance one iteration
+
### .qua files
.qua files begin with a one-line header. **The first line of a .qua file is always ignored.** It can be helpful to add
```
@@ -77,6 +84,7 @@ x y speedX speedY color
2 0 0 0 YE
# A horizontal wall to stop qualums
```
+
`anotherfile.qua`:
```
x y speedX speedY color
@@ -84,3 +92,21 @@ x y speedX speedY color
# Since speedX = 1, the wall will be moving to the right.
41 20 0 1 GE # This qualum will move downwards and collide with the wall
```
+
+#### Modifying 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
+- scale - Controls the size of qualums in pixels
+- speed - The initial program speed
+Property names are case insensitive.
+For example,
+```
+x y s s color
+0 0 0 1 RE
+0 2 0 0 YE
+%width 1
+%height 3
+%scale 1
+```
+This creates a 1x3 pixel window.
diff --git a/examples/add8test.qua b/examples/add8test.qua
index 4197d53..7a733de 100644
--- a/examples/add8test.qua
+++ b/examples/add8test.qua
@@ -17,4 +17,7 @@ x y speedX speedY color
50 45 1 0 GE
50 46 1 0 BL
70 47 0 0 !add8.qua
+%WIDTH 400
+%HEIGHT 300
+%SCALE 2
# 11001101 (205) + 01011110 (94) = 100101011 (299) \ No newline at end of file
diff --git a/qualums b/qualums
index 0e00315..c58e48c 100755
--- a/qualums
+++ b/qualums
Binary files differ
diff --git a/src/Qualum.cpp b/src/Qualum.cpp
index 02327e8..3fbe47a 100644
--- a/src/Qualum.cpp
+++ b/src/Qualum.cpp
@@ -5,14 +5,16 @@
#include <vector>
#include <iostream>
-int Qualum::locations[Rendering::GRID_HEIGHT][Rendering::GRID_WIDTH];
+int** Qualum::locations;
std::vector<Qualum*> Qualum::qualums;
int Qualum::iterations = 0;
void Qualum::initialize()
{
+ locations = (int**) malloc(Rendering::GRID_HEIGHT*sizeof(int*));
for (int i = 0; i < Rendering::GRID_HEIGHT; i++)
{
+ locations[i] = (int*) malloc(Rendering::GRID_WIDTH*sizeof(int));
for (int j = 0; j < Rendering::GRID_WIDTH; j++)
{
locations[i][j] = -1;
diff --git a/src/Qualum.h b/src/Qualum.h
index 49a92b2..d88a287 100644
--- a/src/Qualum.h
+++ b/src/Qualum.h
@@ -19,7 +19,7 @@ public:
static void initialize();
static int iterations;
private:
- static int locations[Rendering::GRID_HEIGHT][Rendering::GRID_WIDTH];
+ static int** locations;
static void two_perpendiculars(int speedx, int speedy, int* speedx1, int* speedy1, int* speedx2, int* speedy2);
static void combine_speeds(int speedx1, int speedy1, int speedx2, int speedy2, int* speedx, int* speedy);
void collide_with(Qualum* q);
diff --git a/src/Rendering.cpp b/src/Rendering.cpp
index 542c679..e5209cc 100644
--- a/src/Rendering.cpp
+++ b/src/Rendering.cpp
@@ -1,5 +1,26 @@
#include "Rendering.h"
int Rendering::RENDER_SCALE = 3;
+int Rendering::GRID_WIDTH = 400;
+int Rendering::GRID_HEIGHT = 300;
int Rendering::WIDTH = GRID_WIDTH * RENDER_SCALE;
int Rendering::HEIGHT = GRID_HEIGHT * RENDER_SCALE;
+
+void Rendering::set_grid_width(int new_value)
+{
+ GRID_WIDTH = new_value;
+ WIDTH = GRID_WIDTH * RENDER_SCALE;
+}
+
+void Rendering::set_grid_height(int new_value)
+{
+ GRID_HEIGHT = new_value;
+ HEIGHT = GRID_HEIGHT * RENDER_SCALE;
+}
+
+void Rendering::set_render_scale(int new_value)
+{
+ RENDER_SCALE = new_value;
+ WIDTH = GRID_WIDTH * RENDER_SCALE;
+ HEIGHT = GRID_HEIGHT * RENDER_SCALE;
+} \ No newline at end of file
diff --git a/src/Rendering.h b/src/Rendering.h
index fcc147b..2b07fc9 100644
--- a/src/Rendering.h
+++ b/src/Rendering.h
@@ -4,12 +4,14 @@
class Rendering {
public:
static int RENDER_SCALE;
- static const int GRID_WIDTH = 400;
- static const int GRID_HEIGHT = 300;
+ static int GRID_WIDTH;
+ static int GRID_HEIGHT;
static int WIDTH;
static int HEIGHT;
+ static void set_grid_width(int new_value);
+ static void set_grid_height(int new_value);
+ static void set_render_scale(int new_value);
private:
-
};
#endif /* RENDERING_H */
diff --git a/src/main.cpp b/src/main.cpp
index 6b217f1..c95cf87 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -14,7 +14,7 @@ const char SLASH = '/';
SDL_Window* window;
-float TIME_SCALE = 4.0f;
+double TIME_SCALE = 4.0;
void quit()
{
@@ -53,6 +53,30 @@ char* getfilename(char* path)
return filename;
}
+void str_tolower(char* str)
+{
+ int i;
+ for (i = 0; str[i]; i++)
+ str[i] = tolower(str[i]);
+
+}
+
+void set_property(const char* property, const char* value)
+{
+ int int_value;
+ double double_value;
+ if (!strcmp(property, "width") && (int_value = atoi(value)))
+ Rendering::set_grid_width(int_value);
+ else if (!strcmp(property, "height") && (int_value = atoi(value)))
+ Rendering::set_grid_height(int_value);
+ else if (!strcmp(property, "scale") && (int_value = atoi(value)))
+ Rendering::set_render_scale(int_value);
+ else if (!strcmp(property, "speed"))
+ {
+ TIME_SCALE = double_value;
+ }
+}
+
void read_file(char* directory, char* filename, int x, int y, int speedx, int speedy)
{
// Create the qualums from the given file
@@ -71,13 +95,18 @@ void read_file(char* directory, char* filename, int x, int y, int speedx, int sp
int xrel, yrel, speedxrel, speedyrel; // Read input file
char* color_str = (char*) malloc(4096);
+ char* property = (char*) malloc(4096);
+ char* value = (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.)
{
-
+ buffer++;
+ sscanf(buffer, "%s %s", property, value);
+ str_tolower(property);
+ set_property(property, value);
continue;
}
@@ -109,7 +138,9 @@ int main(int argc, char** argv)
return 1;
}
+ read_file(getdirname(argv[1]), getfilename(argv[1]), 0, 0, 0, 0);
Qualum::initialize();
+
window = SDL_CreateWindow("Qualums", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, Rendering::WIDTH, Rendering::HEIGHT,
SDL_WINDOW_SHOWN);
@@ -118,7 +149,7 @@ int main(int argc, char** argv)
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)
@@ -144,7 +175,7 @@ int main(int argc, char** argv)
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);
+ TIME_SCALE = (double)(event.key.keysym.sym - SDLK_0);
break;
}
switch (event.key.keysym.sym)