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