Skip to content

Commit

Permalink
Other: 'Twister' experiment.
Browse files Browse the repository at this point in the history
  • Loading branch information
deanthecoder committed Mar 4, 2024
1 parent c7bbea4 commit 2b54433
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,4 @@ Experiments/GameOfLife/Conway
Experiments/FireFX/fire
Experiments/TheMatrix/matrix
Experiments/Sand/sand
Experiments/Twister/twister
Binary file added Experiments/Twister/Twister.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
105 changes: 105 additions & 0 deletions Experiments/Twister/twister.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// zcc +zx -lndos -create-app -lmz -DAMALLOC -o twister twister.c
#include <conio.h>
#include <input.h>
#include <graphics.h>
#include <math.h>
#include <arch/zx/spectrum.h>

static unsigned char* attrMem = 22528;
static uint8_t x, y;
static unsigned int i;

static void line(int8_t x1, int8_t x2, uint8_t b, uint8_t attr)
{
uint8_t c = 0;
while (x1 < x2)
{
attrMem[b * 32 + x1 + 16] = attr + (c++ < 3 ? BRIGHT : 0);
x1++;
}
}

void main()
{
zx_border(BLUE);
textbackground(BLACK);
textcolor(WHITE);
clrscr();

// Prep.
in_GetKeyReset();

// Footer.
gotoxy(41, 23);
printf("Twister (@DeanTheCoder)");

// Precalc.
int8_t* anim = malloc(120 * 24 * 6);
for (i = 0; i < 120; ++i)
{
gotoxy(0, 0);
printf("%d%%", (int)((i + 1) * 0.833));

float am = 120.0f + cos(i * 0.0524f) * 100.0f;
float an = -3.141f + sin(i * 0.0524f) * 3.141f;

for (y = 0; y < 23; ++y)
{
int8_t* p = &anim[24 * 6 * i + y * 6];

float fv = 8.0f * y / am + an;
int8_t x1 = 8.0f * sin(fv);
int8_t x2 = 8.0f * sin(fv + 1.571f);
int8_t x3 = -x1;
int8_t x4 = -x2;

p[0] = x1;
if (x2 < p[0]) p[0] = x2;
if (x3 < p[0]) p[0] = x3;
if (x4 < p[0]) p[0] = x4;
p[5] = x1;
if (x2 > p[5]) p[5] = x2;
if (x3 > p[5]) p[5] = x3;
if (x4 > p[5]) p[5] = x4;

if (x1 < x2)
{
p[1] = x1;
p[2] = x2;
} else if (x3 < x4)
{
p[1] = x3;
p[2] = x4;
}

if (x2 < x3)
{
p[3] = x2;
p[4] = x3;
} else if (x4 < x1)
{
p[3] = x4;
p[4] = x1;
}
}
}

gotoxy(0, 0);
printf(" ");

// Loop.
while (!in_GetKey())
{
for (i = 0; i < 120; ++i)
{
for (y = 0; y < 23; ++y)
{
int8_t* p = &anim[24 * 6 * i + y * 6];
line(-8, p[0], y, PAPER_BLACK);
line(p[1], p[2], y, PAPER_YELLOW);
line(p[3], p[4], y, PAPER_RED);
line(p[5], 8, y, PAPER_BLACK);
}
}
}
}
Binary file added Experiments/Twister/twister.tap
Binary file not shown.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ Iterating over a fixed number of grains, advancing each depending on what is bel
I'm making use of the [z88dk](https://z88dk.org/site/)'s 'chunky' pixel blitting routines too.
![](Experiments/Sand/Sand.png)

# Experiments - Twister
Pushing my graphical skills on the ZX Spectrum, resulting in the creation of a classic 'twister' effect. The code is written in C and compiled into Z80 machine code ([here](Experiments/Twister/twister.tap)) - Needed to keep the performance up.

The twister effect, a staple of early computer graphics and demoscene productions, seemed like the perfect challenge - I've always wanted to make one of these but never got round to it. Helped with details from [8bitshack](https://8bitshack.org/post/twister/).
It takes a while to complete the precaching, but I like the result.
![](Experiments/Twister/Twister.png)

## Contribution and Improvements
ZX Speculator is an ongoing project and contributions are welcome. Whether it's improving emulation accuracy, testing on different platforms, or enhancing existing features, your input is valuable (although I can't always promise a fast response, as this is a side project).

Expand Down

0 comments on commit 2b54433

Please sign in to comment.