Welcome, Guest |
You have to register before you can post on our site.
|
Latest Threads |
[split] Lines of Code
Forum: General Discussion
Last Post: mdijkens
48 minutes ago
» Replies: 12
» Views: 85
|
_CONSOLEINPUT is blocking
Forum: General Discussion
Last Post: mdijkens
2 hours ago
» Replies: 12
» Views: 170
|
[split] BAM and _IIF with...
Forum: QBJS, BAM, and Other BASICs
Last Post: CharlieJV
7 hours ago
» Replies: 8
» Views: 55
|
Who wants to PLAY?
Forum: QBJS, BAM, and Other BASICs
Last Post: dbox
10 hours ago
» Replies: 17
» Views: 294
|
QBJS v0.9.0 - Release
Forum: QBJS, BAM, and Other BASICs
Last Post: bplus
11 hours ago
» Replies: 23
» Views: 415
|
Curious if I am thinking ...
Forum: Help Me!
Last Post: bplus
11 hours ago
» Replies: 27
» Views: 606
|
ADPCM compression
Forum: Petr
Last Post: Petr
Yesterday, 09:37 PM
» Replies: 1
» Views: 58
|
Aloha from Maui guys.
Forum: General Discussion
Last Post: Pete
Yesterday, 07:33 PM
» Replies: 18
» Views: 519
|
Qix line monster
Forum: Programs
Last Post: bplus
Yesterday, 05:38 PM
» Replies: 1
» Views: 56
|
Trojan infection !
Forum: Help Me!
Last Post: PhilOfPerth
Yesterday, 09:14 AM
» Replies: 4
» Views: 107
|
|
|
Qix line monster |
Posted by: Abazek - Yesterday, 07:35 AM - Forum: Programs
- Replies (1)
|
|
Anyone remember the 1980's arcade game (and later home computer / console game) Qix ?
I was thinking about how to re-create the "line monster" (the Qix) from this game. Basically it is an array of lines (ie: (x1,y1) and (x2,y2) coordinates). A line is drawn, then the color is dimmed a little, and the next line is drawn. This produces a fade out effect. The coordinates of the first line are changed each time the monster moves. Each remaining line is a copy of the one before it in the array. The code would be something like this:
for q = last_line to 2 step -1
line(q) = line(q-1)
next q
update_coords line(1)
In the 1980's game, the array contained only a handful of lines, maybe a dozen or so at most. This was likely done because of hardware limitations (both CPU speeds, and screen resolution). Today, with our high-res screens and faster CPUs, we can make the array much longer...
Below is my first attempt at doing this. It is not the most elegant code, but it works. The array is 135 lines long (const cnqlen, ie: Qix Length). It doesn't look exactly like the arcade. It sort of reminds me of the old "Mystify" screen saver for Windows... The line will change colors every few seconds, or you can press 1-7 to change colors.
Code: (Select All)
'' Qix line experiment 1 (first attempt at reproducing the "line monster" from the 1980's game Qix)
'' by Abazek 2025-Jan-19
''
'' Future plans:
'' 1. Optimize the code (there are parts of this I could do differently/better)
'' 2. Greater variety of movement (perhaps RND, or SIN and COS, so cx/cy can be more than just 1, 0, or -1)
'' 3. Qix size limits (ie: if qixA and qixB get too far apart, change cx/cy to bring them closer together)
Const cnqlen = 135
Const cnfade = 2
Const cnSPD = 0.05
Const cnResX = 1280
Const cnResY = 960
Const cnMinChg = 25
Const cnMaxChg = 100
Const defx1 = 620
Const defy1 = 460
Const defx2 = 660
Const defy2 = 500
Type coord
x As Integer
y As Integer
c As Integer
End Type
Dim qixA(cnqlen) As coord
Dim qixB(cnqlen) As coord
Dim scn&, pixcolr&
Dim a$, qq, d1, d2, colr, fw, flag, cl, clrchg, whenclr
Dim showc
Dim Shared cx, cy, chgdirA, chgdirB, whenChgA, whenChgB
Randomize Timer
scn& = _NewImage(cnResX, cnResY, 32)
Screen scn&
Cls 0, _RGB(0, 0, 0)
_Title "Qix Experiment 1"
For qq = 1 To cnqlen
qixA(qq).x = defx1
qixA(qq).y = defy1
qixA(qq).c = 7
qixB(qq).x = defx2
qixB(qq).y = defy2
qixB(qq).c = 7
Next qq
GoSub changedirA
GoSub changedirB
chgdirA = 0
chgdirB = 0
clrchg = 0
whenclr = cnMaxChg
showc = 0
flag = 0
While flag = 0
GoSub MoveQix
If showc = 1 Then GoSub ShowCoords
GoSub DrawQix
Color _RGB32(128, 128, 128)
Locate 1, 1
Print "ESC or SPACE to exit. S to show coordintates. D to hide coordintates. 1-7 manually change color."
''GoSub framewait
_Delay cnSPD
''GoSub WaitKey
a$ = InKey$
If a$ = Chr$(27) Then flag = 1
If a$ = Chr$(32) Then flag = 1
If a$ = "S" Or a$ = "s" Then showc = 1: Cls
If a$ = "D" Or a$ = "d" Then showc = 0: Cls
If a$ = "1" Then cl = 1: GoSub ManualClr
If a$ = "2" Then cl = 2: GoSub ManualClr
If a$ = "3" Then cl = 3: GoSub ManualClr
If a$ = "4" Then cl = 4: GoSub ManualClr
If a$ = "5" Then cl = 5: GoSub ManualClr
If a$ = "6" Then cl = 6: GoSub ManualClr
If a$ = "7" Then cl = 7: GoSub ManualClr
Wend
End
WaitKey:
a$ = InKey$
If a$ = "" Then GoTo WaitKey
If a$ = Chr$(27) Then flag = 1
Return
framewait:
fw = Timer
While (Abs(Timer - fw) < cnSPD)
Wend
Return
ShowCoords:
For qq = 1 To cnqlen
If qq < 51 Then
Locate (qq + 1), 1
ElseIf qq < 101 Then
Locate (qq - 49), 50
ElseIf qq < 151 Then
Locate (qq - 99), 100
Else
Locate 51, 100
End If
Color _RGB32(128, 128, 128)
Print "Coords "; qq; " = ("; qixA(qq).x; ","; qixA(qq).y; ") - ("; qixB(qq).x; ","; qixB(qq).y; ")"
Next qq
Locate 52, 1
Print "Timer = "; Timer; " Delay="; cnSPD; " ChgdirA="; chgdirA; "/"; whenChgA; " ChgdirB="; chgdirB; "/"; whenChgB;
colr = 128
GoSub setColor
Color pixcolr&
Print " Color "; clrchg; "/"; whenclr; " "
Return
DrawQix:
colr = 1
For qq = cnqlen To 1 Step -1
cl = qixA(qq).c
GoSub setColor
Color pixcolr&
Line (qixA(qq).x, qixA(qq).y)-(qixB(qq).x, qixB(qq).y), pixcolr&
colr = colr + cnfade
If colr > 255 Then colr = 255
Next qq
Color _RGB32(1, 1, 1)
Return
MoveQix:
For qq = cnqlen To 2 Step -1
qixA(qq).x = qixA(qq - 1).x
qixA(qq).y = qixA(qq - 1).y
qixA(qq).c = qixA(qq - 1).c
qixB(qq).x = qixB(qq - 1).x
qixB(qq).y = qixB(qq - 1).y
qixB(qq).c = qixB(qq - 1).c
Next qq
qq = 1
chgdirA = chgdirA + 1
chgdirB = chgdirB + 1
clrchg = clrchg + 1
If chgdirA > whenChgA Then
GoSub changedirA
End If
If chgdirB > whenChgB Then
GoSub changedirB
End If
If clrchg > whenclr Then
GoSub changeClr
qixA(qq).c = cl
qixB(qq).c = cl
End If
SetMoveDir d1
cx = cx * 2
cy = cy * 2
qixA(qq).x = qixA(qq).x + cx
qixA(qq).y = qixA(qq).y + cy
If qixA(qq).x < 1 Then qixA(qq).x = 1: GoSub changedirA
If qixA(qq).x > cnResX Then qixA(qq).x = cnResX: GoSub changedirA
If qixA(qq).y < 1 Then qixA(qq).y = 1: GoSub changedirA
If qixA(qq).y > cnResY Then qixA(qq).y = cnResY: GoSub changedirA
SetMoveDir d2
cx = cx * 2
cy = cy * 2
qixB(qq).x = qixB(qq).x + cx
qixB(qq).y = qixB(qq).y + cy
If qixB(qq).x < 1 Then qixB(qq).x = 1: GoSub changedirB
If qixB(qq).x > cnResX Then qixB(qq).x = cnResX: GoSub changedirB
If qixB(qq).y < 1 Then qixB(qq).y = 1: GoSub changedirB
If qixB(qq).y > cnResY Then qixB(qq).y = cnResY: GoSub changedirB
Return
changedirA:
d1 = Int(Rnd * 8) + 1
cl = cnMaxChg - cnMinChg
whenChgA = Int(Rnd * cl) + cnMinChg
chgdirA = 0
Return
changedirB:
d2 = Int(Rnd * 8) + 1
cl = cnMaxChg - cnMinChg
whenChgB = Int(Rnd * cl) + cnMinChg
chgdirB = 0
Return
changeClr:
cl = Int(Rnd * 7) + 1
clrchg = 0
Return
ManualClr:
qixA(1).c = cl
qixB(1).c = cl
clrchg = 0
Return
setColor:
Select Case cl
Case 1
pixcolr& = _RGB32(colr, 0, 0)
Case 2
pixcolr& = _RGB32(0, colr, 0)
Case 3
pixcolr& = _RGB32(colr, colr, 0)
Case 4
pixcolr& = _RGB32(0, 0, colr)
Case 5
pixcolr& = _RGB32(colr, 0, colr)
Case 6
pixcolr& = _RGB32(0, colr, colr)
Case Else
pixcolr& = _RGB32(colr, colr, colr)
End Select
Return
Sub SetMoveDir (dir)
Select Case dir
Case 1
cx = 1: cy = 0
Case 2
cx = 0: cy = 1
Case 3
cx = -1: cy = 0
Case 4
cx = 0: cy = -1
Case 5
cx = 1: cy = -1
Case 6
cx = -1: cy = 1
Case 7
cx = -1: cy = -1
Case Else
cx = 1: cy = 1
End Select
End Sub
|
|
|
Curious if I am thinking about this right. |
Posted by: protocog - 01-19-2025, 03:58 PM - Forum: Help Me!
- Replies (27)
|
|
I am trying to get good at writing simple code that is easy to read. I took up that assignment of writing a Inches to millimeters conversion calculator in Lesson 2 of the tutorial. All I am asking is is there any way to improve my code? I added the option to convert to millimeters to inches.
Code: (Select All)
Dim inches# 'container for inches
Dim op% 'which operation (either in to mm or mm to in
Dim mm# 'container for mm
Const Conv# = 25.4 '25.4 mm every inch
Print "1. mm to in"
Print "2. in to mm"
Input "Please enter the operation you would like to do. > ", op%
If op% = 1 Then
Input "Enter mm >", mm#
Print mm# / Conv#
ElseIf op% = 2 Then
Input "Enter Inches", inches#
Print inches# * Conv#
Else
Print "No Op..."
End If
Thanks for any feedback!
|
|
|
ADPCM compression |
Posted by: Petr - 01-19-2025, 03:13 PM - Forum: Petr
- Replies (1)
|
|
Some WAV formats use (but there are really very few of them) ADPCM compression. And since its implementation is not too complicated, I tried it. So what's the point here:
If you want to slightly reduce the sound in WAV format, you have these options:
1) Reduce the bit depth (so instead of a 32-bit WAV file you use an 8-bit one) - the sound is slightly worse, but on cheap speakers you won't notice the difference. But it's not Hi-fi anymore. The space saving is fourfold.
2) Use mono instead of stereo. You'll lose the sound depth. This will reduce the sound in WAV format by half.
3) Reduce the sound refresh rate - so instead of 44100 records per minute you'll use only 22050, or 11025. This is a drastic loss of sound samples and it's very audible. The ratio of the original and new sizes is given by the ratio of discarded samples (if you go from 44100 to 22050, you get half the size, etc.)
4) You use a better form of data recording with minimal signal difference.
This is an example. ADPCM simply calculates the difference between two consecutive samples and records only this difference. The step it uses to do this is divided into 8 levels (because an 8-bit signal has 256 levels and ADPCM uses 4-bit notation to save space). The maximum value with a sign of 8 can fit into a 4-bit notation (one bit indicates whether it is a positive or negative value). In this way, sound can be stored with minimal loss of quality in half the file size. This compression cause small noise in signal.
The first example shows a function on an array of numbers and does not require any music file:
Code: (Select All)
' ADPCM compression and decompression in QB64
' ----------------------------------------------------------
' This example uses 4-bit quantization for ADPCM compression
Dim originalSamples(0 To 9) As Single
Dim compressedData(0 To 9) As Integer
Dim decompressedSamples(0 To 9) As Single
'Original signal values
originalSamples(0) = 0
originalSamples(1) = 10
originalSamples(2) = 20
originalSamples(3) = 35
originalSamples(4) = 25
originalSamples(5) = 10
originalSamples(6) = 25
originalSamples(7) = 15
originalSamples(8) = 5
originalSamples(9) = 0
Print "Original Values:"
For i = 0 To 9
Print Using "###.##"; originalSamples(i);
Next
Print
' Compression (ADPCM)
Dim predicted As Single
Dim difference As Single
predicted = 0 ' first prediction
For i = 0 To 9
difference = originalSamples(i) - predicted
compressedData(i) = Quantize(difference) ' Kvantování rozdílu
predicted = predicted + Dequantize(compressedData(i)) ' Aktualizace predikce
Next
Print "Compressed Data (4 bite): "
For i = 0 To 9
Print compressedData(i);
Next
Print
' Decompressing process
predicted = 0 ' First prediciton
For i = 0 To 9
decompressedSamples(i) = predicted + Dequantize(compressedData(i))
predicted = decompressedSamples(i)
Next
Print "Decompressed Samples:"
For i = 0 To 9
Print Using "###.##"; decompressedSamples(i);
Next
End
' Difference quantization function
Function Quantize (difference As Single)
Q = Int(difference / 5) ' Quentization step 5
If Q > 7 Then Q = 7 ' limit to 4 bite
If Q < -8 Then Q = -8
Quantize = Q
End Function
' Difference dequantization function
Function Dequantize (quantizedDifference As Integer)
Dequantize = quantizedDifference * 5 ' The reverse process of quantization
End Function
The second example shows the use on real audio (change the MP3 to another MP3 in stereo on line 20)
The result is the sound so as it would sound if written in 4 bits. Note the small background noise, which is an quantization bug of the original, because this program does not have floating compression.
This program actually saves an 8 bit stereo WAV file with the original sample rate, but in 4 bits. In this case only to memory, it does not save anything to the hard disk.
Code: (Select All)
' ADPCM compression and decompression in QB64 for an 8-bit audio signal
' -----------------------------------
' This example uses 4-bit quantization for ADPCM compression of an audio signal in the range 0 to 255.
_Title "ADPCM compression in QB4PE"
Screen _NewImage(100, 24, 0)
Dim originalSamplesL(0 To 9) As Integer
Dim compressedDataL(0 To 9) As Integer
Dim decompressedSamplesL(0 To 9) As Integer
Dim originalSamplesR(0 To 9) As Integer
Dim compressedDataR(0 To 9) As Integer
Dim decompressedSamplesR(0 To 9) As Integer
Dim m As _MEM, Snd As Long
Snd = _SndOpen("A.mp3")
m = _MemSound(Snd, 0)
Locate 1
Do Until a& >= m.SIZE
j = 0
'load music samples
For i = 0 To 9
originalSamplesL(i) = 128 + (_MemGet(m, m.OFFSET + a&, Single) * 127)
originalSamplesR(i) = 128 + (_MemGet(m, m.OFFSET + a& + 4, Single) * 127)
a& = a& + 8
If a& >= m.SIZE Then Exit Do
Next i
' Compression (ADPCM)
ReDim predicted As Single
ReDim difference As Single
predictedL = 128
predictedR = 128 ' Initial assumption (mean value for 8-bit range)
For i = 0 To 9
differenceL = originalSamplesL(i) - predictedL
differenceR = originalSamplesR(i) - predictedR
compressedDataL(i) = Quantize(differenceL) ' Difference quantization
compressedDataR(i) = Quantize(differenceR)
predictedL = predictedL + Dequantize(compressedDataL(i)) ' Update prediction
predictedR = predictedR + Dequantize(compressedDataR(i)) ' Update prediction
'Range verification for prediction
If predictedL < 0 Then predictedL = 0
If predictedL > 255 Then predictedL = 255
If predictedR < 0 Then predictedR = 0
If predictedR > 255 Then predictedR = 255
Next
Print
Print "Original sound samples (0-255) (Left):"
For i = 0 To 9
Print Using "####"; originalSamplesL(i);
Next
Print
Print "Original sound samples (0-255) (Right):"
For i = 0 To 9
Print Using "####"; originalSamplesR(i);
Next
Print
Print "Compressed data (4bite):"
For i = 0 To 9
Print compressedDataL(i);
Print compressedDataR(i);
Next
Print
' Decompresing process
predictedL = 128 'Initial assumption (mean value for 8-bit range)
predictedR = 128
For i = 0 To 9
decompressedSamplesL(i) = predictedL + Dequantize(compressedDataL(i))
decompressedSamplesR(i) = predictedR + Dequantize(compressedDataR(i))
' Range verification for reconstructed samples
If decompressedSamplesL(i) < 0 Then decompressedSamplesL(i) = 0
If decompressedSamplesL(i) > 255 Then decompressedSamplesL(i) = 255
If decompressedSamplesR(i) < 0 Then decompressedSamplesR(i) = 0
If decompressedSamplesR(i) > 255 Then decompressedSamplesR(i) = 255
predictedL = decompressedSamplesL(i)
predictedR = decompressedSamplesR(i)
Next
Print
Print "Decompressed samples (0-255) Left:"
For i = 0 To 9
Print Using "####"; decompressedSamplesL(i);
Next
Print
Print "Decompressed samples (0-255) Right:"
For i = 0 To 9
Print Using "####"; decompressedSamplesR(i);
Next
For i = 0 To 9
L = (decompressedSamplesL(i) - 128) / 128
R = (decompressedSamplesR(i) - 128) / 128
_SndRaw L, R
Next i
Do Until _SndRawLen < .1
_Display
_Limit 20
Loop
Loop
End
' Difference quantization function
Function Quantize# (difference As Single)
Dim Q As Integer
Q = Int(difference / 8) ' Quantization step 8 for 8-bit signal
If Q > 7 Then Q = 7 ' limit to 4 bites
If Q < -8 Then Q = -8
Quantize# = Q
End Function
' Function for dequantizing the difference
Function Dequantize (quantizedDifference As Integer)
Dequantize = quantizedDifference * 8 ' The reverse process of quantization
End Function
|
|
|
Trojan infection ! |
Posted by: PhilOfPerth - 01-19-2025, 12:32 PM - Forum: Help Me!
- Replies (4)
|
|
I just received notification from my virus-scanner that it has trapped a Trojan virus that it found in one of my QB64PE folders.
It was named MalwareX-gen [TRJ], and AVG claims it's a Trojan.
It was found in QB64PE\internal\C\C_Compiler\bin\gcov.exe
Is this likely to be a false positive?
|
|
|
Glow Bug |
Posted by: SierraKen - 01-19-2025, 01:23 AM - Forum: Programs
- Replies (7)
|
|
Ever since I saw B+ and others make these, I've always wanted to try and make something like this myself. So I finally did today, without any help! Well besides Steve's fill-in circles.
But going back even further to the 80's, I've tried for countless times to make the math of this and never could figure it out, until today!
Thanks B+ and others for the inspiration!
Feel free to use the code for anything you want. This is just a glow bug example, to quit press Esc.
Code: (Select All)
_Title "Glow Bug - by SierraKen"
Screen _NewImage(800, 600, 32)
Randomize Timer
oldx = 400
oldy = 300
Do
bugs xx, yy, oldx, oldy
Loop Until InKey$ = Chr$(27)
Sub bugs (xx, yy, oldx, oldy)
Do
If d1 > d2 Then s = s + .1
If d2 > d1 Then s = s - .1
d = d + 1
If d > t Then
oldx = oldx + x
oldy = oldy + y
morebugs d1, d2, d, t
End If
x = Cos(s * _Pi / 180) * d
y = Sin(s * _Pi / 180) * d
xx = x + oldx
yy = y + oldy
If xx > 750 Then oldx = 50: Cls: morebugs d1, d2, d, t
If xx < 50 Then oldx = 750: Cls: morebugs d1, d2, d, t
If yy > 550 Then oldy = 50: Cls: morebugs d1, d2, d, t
If yy < 50 Then oldy = 550: Cls: morebugs d1, d2, d, t
fillCircle xx, yy, 10, _RGB32(255, 255, 0)
_Delay .001
Line (0, 0)-(800, 600), _RGB32(0, 0, 0, 10), BF
_Display
Loop Until InKey$ = Chr$(27)
End
End Sub
Sub morebugs (d1, d2, d, t)
d1 = Rnd * 360
d2 = Rnd * 360
d = 0
t = Int(Rnd * 360) + 1
End Sub
'from Steve Gold standard
Sub fillCircle (CX As Integer, CY As Integer, R As Integer, C As _Unsigned Long)
Dim Radius As Integer, RadiusError As Integer
Dim X As Integer, Y As Integer
Radius = Abs(R): RadiusError = -Radius: X = Radius: Y = 0
If Radius = 0 Then PSet (CX, CY), C: Exit Sub
Line (CX - X, CY)-(CX + X, CY), C, BF
While X > Y
RadiusError = RadiusError + Y * 2 + 1
If RadiusError >= 0 Then
If X <> Y + 1 Then
Line (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
Line (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
End If
X = X - 1
RadiusError = RadiusError - X * 2
End If
Y = Y + 1
Line (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
Line (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
Wend
End Sub
|
|
|
_CONSOLEINPUT is blocking |
Posted by: mdijkens - 01-17-2025, 02:21 PM - Forum: General Discussion
- Replies (12)
|
|
I can't find it in the documentation, but _CONSOLEINPUT seems blocking (waiting for a key) after the first call.
Code: (Select All)
$Console:Only
Do
x%% = _ConsoleInput
Print x%%;
Loop
Will only show one 0 and then waits for a keypress...
Is there any other way to check non-blocking for a key in a $CONSOLE window?
|
|
|
Most efficient way to build a big variable length string? |
Posted by: mdijkens - 01-17-2025, 09:37 AM - Forum: General Discussion
- Replies (9)
|
|
I need to build a huge variable length string (>1GB) in memory before calling a function or writing to a file.
I already found out I can most efficiently build it up in a _MEM block.
But what is the best way to move that in the end to a variable string without copying and doubling the memory used.
So basically I am looking to a more efficient way to do:
Code: (Select All)
sql$ = Space$(mpos): _MemGet m, m.OFFSET, sql$
for very big mpos?
Is that even possible?
related to this, what is the most efficient coding to work with a big string where you have to remove a lot of pieces out without doing something like:
Code: (Select All)
sql$ = Left$(sql$, mpos - 1) + Mid$(sql$, mpos + 1)
With very big strings this is also expensive
|
|
|
Methods in types |
Posted by: bobalooie - 01-17-2025, 01:02 AM - Forum: General Discussion
- No Replies
|
|
I apologize if this has been discussed before, a search of the forum was a bit inconclusive. I much prefer working in QBPE but I recently did a couple projects in FreeBASIC because FreeBASIC allows having methods in UDTs, which was very advantageous to writing the solutions. How difficult would it be to add the capability to include methods with a TYPE definition in QBPE? TIA
|
|
|
|