Subversion Repositories Kolibri OS

Rev

Rev 7315 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7315 0CodErr 1
Comment *---------------------------+
2
|                                   |
3
|           Plasma Effect           |
4
|                                   |
5
+-----------------------------------*
6
.686
7
.Model Flat, StdCall
8
Option CaseMap: None
9
 
10
RepArg Macro Arg
11
  Local NewStr
12
    Quot SubStr , 1, 1
13
  IfIdn Quot, <">
14
    .Data
15
      NewStr db Arg,0
16
    .Code
17
    ExitM 
18
  Else
19
    ExitM 
20
  EndIf
21
EndM
22
 
23
@ Macro Function:Req, Args:VarArg
24
  Arg equ 
25
  For Var, 
26
    Arg CatStr Arg, <, RepArg(Var)>
27
  EndM
28
  Arg
29
  ExitM 
30
EndM
31
 
32
Public @Main
33
 
34
; Event Constants
35
REDRAW_EVENT      = 1
36
KEY_EVENT         = 2
37
 
38
; Event Mask Constants
39
EM_REDRAW         = 1
40
EM_KEY            = 2
41
 
42
; Window styles
43
WS_FILL_TRANSPARENT  = 40000000H
44
 
45
TSize Struct
46
  SizeY Word ?
47
  SizeX Word ?
48
TSize EndS
49
 
50
TRGBQuad Struct
51
  Blue     Byte ?
52
  Green    Byte ?
53
  Red      Byte ?
54
  reserved Byte ?
55
TRGBQuad EndS
56
 
57
SetEventMask    Proto EventMask:Dword
58
CheckEvent      Proto
59
BeginDraw       Proto
60
EndDraw         Proto
61
GetScreenSize   Proto
62
DrawWindow      Proto Left:SDword, Top:SDword, Right:SDword, Bottom:SDword, Caption:Ptr, BackColor:Dword, Style:Dword, CapStyle:Dword
63
DrawImageEx     Proto Image:Ptr, X:SDword, Y:SDword, XSize:Dword, YSize:Dword, BPP:Dword, Palette:Ptr, Padding:Dword
64
HeapCreate      Proto
65
HeapAllocate    Proto Bytes:Dword
66
GetTickCount    Proto
67
Sleep           Proto Time:Dword
68
ThreadTerminate Proto
69
 
70
.Const
71
Float_50 Real4 50.0 ; to keep the FrameRate
72
Float_40 Real4 40.0 ; around 40-50 FPS
73
; The table below can be calculated approximately as: SinTab[i] = Sin(2 * PI * i / 255) * 128 + 128
74
SinTab Label Byte
75
Byte 128, 131, 134, 137, 141, 144, 147, 150, 153, 156, 159, 162, 165, 168, 171, 174
76
Byte 177, 180, 183, 186, 189, 191, 194, 197, 199, 202, 205, 207, 209, 212, 214, 217
77
Byte 219, 221, 223, 225, 227, 229, 231, 233, 235, 236, 238, 240, 241, 243, 244, 245
78
Byte 246, 248, 249, 250, 251, 252, 252, 253, 254, 254, 255, 255, 255, 255, 255, 255
79
Byte 255, 255, 255, 255, 255, 255, 254, 254, 253, 253, 252, 251, 250, 249, 248, 247
80
Byte 246, 245, 243, 242, 240, 239, 237, 236, 234, 232, 230, 228, 226, 224, 222, 220
81
Byte 218, 215, 213, 211, 208, 206, 203, 201, 198, 195, 193, 190, 187, 184, 181, 179
82
Byte 176, 173, 170, 167, 164, 161, 158, 155, 152, 148, 145, 142, 139, 136, 133, 130
83
Byte 126, 123, 120, 117, 114, 111, 108, 104, 101,  98,  95,  92,  89,  86,  83,  80
84
Byte  77,  75,  72,  69,  66,  63,  61,  58,  55,  53,  50,  48,  45,  43,  41,  38
85
Byte  36,  34,  32,  30,  28,  26,  24,  22,  20,  19,  17,  16,  14,  13,  11,  10
86
Byte   9,   8,   7,   6,   5,   4,   3,   3,   2,   2,   1,   1,   0,   0,   0,   0
87
Byte   0,   0,   0,   1,   1,   1,   2,   2,   3,   4,   4,   5,   6,   7,   8,  10
88
Byte  11,  12,  13,  15,  16,  18,  20,  21,  23,  25,  27,  29,  31,  33,  35,  37
89
Byte  39,  42,  44,  47,  49,  51,  54,  57,  59,  62,  65,  67,  70,  73,  76,  79
90
Byte  82,  85,  88,  91,  94,  97, 100, 103, 106, 109, 112, 115, 119, 122, 125, 128
91
 
92
.Data
93
i1 Dword 50
94
j1 Dword 90
95
Freq Dword 100 ; // GetTickCount return count of 1/100s of second
96
Instant Real4 0.0
97
SleepTime Dword 0
98
 
99
.Data?
100
Screen TSize <>
101
i2 Dword ?
102
j2 Dword ?
103
Palette TRGBQuad 256 Dup (<>)
104
Image Dword ?
105
ImageWidth Dword ?
106
ImageHeight Dword ?
107
FrameStart Dword ?
108
 
109
.Code
110
CreatePalette Proc Uses ebx
111
Local i:Dword
112
Local Red:Dword, Green:Dword, Blue:Dword
113
  mov i, 0
114
  .Repeat
115
    mov eax, i
116
    .If eax <= 63
117
      ; do nothing
118
    .ElseIf eax <= 127
119
      neg eax
120
      add eax, 127
121
    .ElseIf eax <= 189
122
      sub eax, 128
123
    .ElseIf eax <= 255
124
      neg eax
125
      add eax, 255
126
    .EndIf
127
 
128
    mov Red, eax
129
    mov Green, eax
130
    mov Blue, eax
131
 
132
    .If i <= 127
133
      shl Green, 1
134
      shl Blue, 2
135
    .ElseIf i <= 255
136
      shl Red, 2
137
      shl Blue, 1
138
    .EndIf
139
 
140
    mov eax, Red   ;
141
    cmp eax, 255   ;
142
    setna bl       ; IF Red > 255 THEN bl = 255
143
    dec bl         ;              ELSE bl = Red
144
    or bl, al      ;
145
 
146
    mov eax, Green ;
147
    cmp eax, 255   ;
148
    setna cl       ; IF Green > 255 THEN cl = 255
149
    dec cl         ;                ELSE cl = Green
150
    or cl, al      ;
151
 
152
    mov eax, Blue  ;
153
    cmp eax, 255   ;
154
    setna dl       ; IF Blue > 255 THEN dl = 255
155
    dec dl         ;               ELSE dl = Blue
156
    or dl, al      ;
157
 
158
    mov eax, i
159
    mov Palette.Red[eax * SizeOf(Type(Palette))], bl
160
    mov Palette.Green[eax * SizeOf(Type(Palette))], cl
161
    mov Palette.Blue[eax * SizeOf(Type(Palette))], dl
162
 
163
    inc i
164
  .Until i > 255
165
  ret
166
CreatePalette EndP
167
;*******************************************************************************
168
Render Proc
169
Local X:Dword, Y:Dword
170
Local Row:Ptr
171
  dec i1
172
  add j1, 2
173
 
174
  mov Y, 0
175
  Align 4
176
  .Repeat
177
    mov eax, Y
178
    add eax, i1
179
    and eax, 255
180
    movzx eax, SinTab[eax]
181
    mov i2, eax
182
 
183
    mov eax, j1
184
    and eax, 255
185
    movzx eax, SinTab[eax]
186
    mov j2, eax
187
 
188
    mov eax, Y
189
    mul ImageWidth
190
    add eax, Image
191
    mov Row, eax
192
 
193
    mov X, 0
194
    Align 4
195
    .Repeat
196
      mov eax, X
197
      add eax, i2
198
      and eax, 255
199
      movzx ecx, SinTab[eax]
200
 
201
      mov eax, Y
202
      add eax, j2
203
      and eax, 255
204
      movzx eax, SinTab[eax]
205
      add ecx, eax
206
 
207
      mov eax, Row
208
      add eax, X
209
      mov [eax], cl
210
 
211
      inc X
212
      mov eax, ImageWidth
213
    .Until eax == X
214
    inc Y
215
    mov eax, ImageHeight
216
  .Until eax == Y
217
  ret
218
Render EndP
219
;*******************************************************************************
220
QueryPerf Proc
221
Local Diff:Dword
222
  Invoke GetTickCount
223
  sub eax, FrameStart
224
  mov Diff, eax
225
  fild Diff
226
  fild Freq
227
  fdivrp
228
  fstp Instant
229
  mov FrameStart, @(GetTickCount)
230
  ret
231
QueryPerf EndP
232
;*******************************************************************************
233
Waiting Proc
234
; Keep the FrameRate around 40-50 FPS
235
  fld Float_50
236
  fld Instant
237
  fcomip st(0), st(1)
238
  fstp st(0)
7316 0CodErr 239
  .If !CARRY? && !ZERO?
7315 0CodErr 240
    inc SleepTime
241
  .Else
242
    fld Float_40
243
    fld Instant
244
    fcomip st(0), st(1)
245
    fstp st(0)
7316 0CodErr 246
    .If CARRY? && (SleepTime != 0)
7315 0CodErr 247
      dec SleepTime
248
    .EndIf
249
  .EndIf
250
  Invoke Sleep, SleepTime
251
  ret
252
Waiting EndP
253
;*******************************************************************************
254
@Main:
255
  Invoke HeapCreate
256
  mov Screen, @(GetScreenSize)
257
  Invoke CreatePalette
258
  movzx eax, Screen.SizeX
259
  movzx ecx, Screen.SizeY
260
  mov ImageWidth, eax
261
  mov ImageHeight, ecx
262
  mul ecx
263
  mov Image, @(HeapAllocate, eax)
264
  mov FrameStart, @(GetTickCount)
265
  Invoke SetEventMask, EM_REDRAW + EM_KEY
266
  .Repeat
267
    Invoke CheckEvent
268
    .If eax == REDRAW_EVENT
269
      Invoke BeginDraw
270
      Invoke DrawWindow, 0, 0, ImageWidth, ImageHeight, 0, 0, WS_FILL_TRANSPARENT, 0
271
      Invoke EndDraw
272
    .ElseIf eax == KEY_EVENT
273
      Invoke ThreadTerminate
274
    .Else
275
      Invoke Render
276
      Invoke QueryPerf
277
      Invoke DrawImageEx, Image, 0, 0, ImageWidth, ImageHeight, 8, Offset Palette, 0
278
      Invoke Waiting
279
    .EndIf
280
  .Until 0
281
END