5caf4f4ad75f4b1e5c62ab4f5d5cf9efe4e2ad73
[dosdemo] / src / plasma.c
1 // Just a test with a run of the mill plasma
2
3 #include <stdlib.h>
4 #include <math.h>
5
6 #include "demo.h"
7 #include "screen.h"
8
9 static int init(void);
10 static void destroy(void);
11 static void start(long trans_time);
12 static void draw(void);
13
14 static struct screen scr = {
15         "plasma",
16         init,
17         destroy,
18         start,
19         0,
20         draw
21 };
22
23 static unsigned long startingTime;
24
25 #define PSIN_SIZE 4096
26 #define PPAL_SIZE 256
27
28 static unsigned char *psin1, *psin2, *psin3;
29 static unsigned short *plasmaPal;
30
31 static unsigned short myBuffer[320 * 240];
32
33
34 struct screen *plasma_screen(void)
35 {
36         return &scr;
37 }
38
39 static int init(void)
40 {
41         int i;
42
43         psin1 = (unsigned char*)malloc(sizeof(unsigned char) * PSIN_SIZE);
44         psin2 = (unsigned char*)malloc(sizeof(unsigned char) * PSIN_SIZE);
45         psin3 = (unsigned char*)malloc(sizeof(unsigned char) * PSIN_SIZE);
46
47         for (i = 0; i < PSIN_SIZE; i++)
48         {
49                 psin1[i] = (unsigned char)(sin((double)i / 45.0) * 63.0 + 63.0);
50                 psin2[i] = (unsigned char)(sin((double)i / 75.0) * 42.0 + 42.0);
51                 psin3[i] = (unsigned char)(sin((double)i / 32.0) * 88.0 + 88.0);
52         }
53
54         plasmaPal = (unsigned short*)malloc(sizeof(unsigned short) * PPAL_SIZE);
55         for (i=0; i<PPAL_SIZE; i++)
56         {
57                 int r, g, b;
58                 int c = i;
59                 if (c > 127) c = 255 - c;
60                 c >>= 2;
61                 g = 31 - c;
62                 r = c;
63                 b = g;
64                 plasmaPal[i] = (r<<11) | (g<<5) | b;
65         }
66
67         return 0;
68 }
69
70 static void destroy(void)
71 {
72         free(psin1);
73         free(psin2);
74         free(psin3);
75 }
76
77 static void start(long trans_time)
78 {
79         startingTime = time_msec;
80 }
81
82 static void draw(void)
83 {
84         int x, y;
85         unsigned char c;
86         unsigned char s1, s2;
87
88         float dt = (float)(time_msec - startingTime) / 1000.0f;
89         int t1 = sin(0.1f * dt) * 132 + 132;
90         int t2 = sin(0.2f * dt) * 248 + 248;
91         int t3 = sin(0.5f * dt) * 380 + 380;
92
93         unsigned int *vram32 = (unsigned int*)fb_pixels;
94         unsigned int p0, p1;
95         for (y = 0; y < fb_height; y++)
96         {
97                 s1 = psin2[y + t2];
98                 s2 = psin3[y + t1];
99                 for (x = 0; x < fb_width; x+=2)
100                 {
101                         c = psin1[x + t1] + s1 + psin3[x + y + t3] + psin1[psin2[x + t2] + s2 + t3];
102                         p0 = plasmaPal[c];
103                         c = psin1[x + 1 + t1] + s1 + psin3[x + 1 + y + t3] + psin1[psin2[x + 1 + t2] + s2 + t3];
104                         p1 = plasmaPal[c];
105
106                         *vram32++ = (p1 << 16) | p0;
107                 }
108         }
109
110         swap_buffers(0);
111 }