From: Svjatoslav Agejenko Date: Thu, 27 Feb 2025 20:04:48 +0000 (+0200) Subject: Improve code readability X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=3434a5ef45c66ce45a139b28bfb652035dc4ddc4;p=qbasicapps.git Improve code readability --- diff --git a/Graphics/Presentations/AI/AI demo.bas b/Graphics/Presentations/AI/AI demo.bas index 4bc6711..16aa99c 100644 --- a/Graphics/Presentations/AI/AI demo.bas +++ b/Graphics/Presentations/AI/AI demo.bas @@ -6,7 +6,7 @@ DECLARE SUB Scene3 () DECLARE SUB Scene4 () DECLARE SUB Scene5 () DECLARE SUB Scene7 () -DECLARE SUB start () +DECLARE SUB Initialize () ' Renamed from "start" for clarity DECLARE SUB sc1 () DECLARE SUB sc2 () DECLARE SUB sc3 () @@ -17,8 +17,8 @@ DECLARE SUB sc7 () DECLARE SUB sc8 () DECLARE SUB sc10 () DECLARE SUB sc9 () -DECLARE SUB box (x1!, y1!, x2!, y2!) -DECLARE SUB calc (tx1!, ty1!, tz1!, tx2!, ty2!, tz2!, tx3!, ty3!, tz3!, c!) +DECLARE SUB DrawRoundedBox (x1!, y1!, x2!, y2!) ' Renamed from "box" for clarity +DECLARE SUB ComputeShadeValue (tx1!, ty1!, tz1!, tx2!, ty2!, tz2!, tx3!, ty3!, tz3!, c!) ' Renamed from "calc" DECLARE SUB GetAngle (x1!, y1!, x2!, y2!, N!) DECLARE SUB RotatePoint (zx!, zy!, x1!, y1!, N!) DECLARE SUB FillPolygon (xCoord1!, yCoord1!, xCoord2!, yCoord2!, xCoord3!, yCoord3!, colorVal!) @@ -39,7 +39,7 @@ DIM SHARED pi DIM SHARED angl1, angl2 DIM SHARED font(0 TO 7, 0 TO 7, 32 TO 150) -start +Initialize Scene1 Scene2 @@ -50,32 +50,7 @@ Scene7 Scene8 Scene9 -SUB box (x1, y1, x2, y2) -' Draws a soft-edged rectangular box by blending pixel colors around the edges. -' The edges fade gently to create a smoothed border. - - FOR y = y1 TO y2 - s = 10 - IF y - y1 <= 10 THEN - s = (SQR((20 - (y - y1)) * (y - y1))) - END IF - - IF y2 - y <= 10 THEN - s = (SQR((20 - (y2 - y)) * (y2 - y))) - END IF - - FOR x = x1 - s TO x2 + s - c = POINT(x, y) - IF c <= 127 THEN - c = c + 127 - IF c > 245 THEN c = 245 - PSET (x, y), c - END IF - NEXT x - NEXT y -END SUB - -SUB calc (tx1, ty1, tz1, tx2, ty2, tz2, tx3, ty3, tz3, c) +SUB ComputeShadeValue (tx1, ty1, tz1, tx2, ty2, tz2, tx3, ty3, tz3, c) ' Computes a value (c) based on the positions of three 3D points. ' It rotates these points around multiple axes, then calculates ' an offset 'a' used to determine a final computed result in 'c'. @@ -124,6 +99,32 @@ SUB calc (tx1, ty1, tz1, tx2, ty2, tz2, tx3, ty3, tz3, c) c = 49 - a IF c < 0 THEN c = 0 END SUB + +SUB DrawRoundedBox (x1, y1, x2, y2) +' Draws a soft-edged rectangular box by blending pixel colors around the edges. +' The edges fade gently to create a smoothed border. + + FOR y = y1 TO y2 + s = 10 + IF y - y1 <= 10 THEN + s = (SQR((20 - (y - y1)) * (y - y1))) ' fade calculation + END IF + + IF y2 - y <= 10 THEN + s = (SQR((20 - (y2 - y)) * (y2 - y))) ' Similar fade near the lower boundary + END IF + + FOR x = x1 - s TO x2 + s + c = POINT(x, y) + IF c <= 127 THEN + c = c + 127 + IF c > 245 THEN c = 245 + PSET (x, y), c + END IF + NEXT x + NEXT y +END SUB + '------------------------------------------------------------------------------- SUB FillPolygon (xCoord1, yCoord1, xCoord2, yCoord2, xCoord3, yCoord3, colorVal) ' Fills a triangular area by connecting the edges. @@ -173,8 +174,9 @@ mkl: NEXT y RETURN -1: +1 : END SUB + '------------------------------------------------------------------------------- SUB GetAngle (x1, y1, x2, y2, N) ' Determines the angle (N) between two points (x1,y1) and (x2,y2). @@ -207,8 +209,21 @@ SUB GetAngle (x1, y1, x2, y2, N) END IF END IF -2: +2 : +END SUB + +'------------------------------------------------------------------------------- +SUB Initialize +' Initializes screen mode, sets up global constants pi and pii, +' and captures system font data for later use in PrintText. + + SCREEN 13 + pi = 3.141592 + pii = pi + fac = 360 / (pi * 2) + InitializeFont END SUB + '------------------------------------------------------------------------------- SUB InitializeFont ' Captures the current text font into an array for later use @@ -226,14 +241,9 @@ SUB InitializeFont NEXT y NEXT a END SUB -'------------------------------------------------------------------------------- -SUB WaitForInput -' Reads exactly one character from the keyboard and stores it in a$. - a$ = INPUT$(1) -END SUB -'------------------------------------------------------------------------------- DEFINT A-Z +'------------------------------------------------------------------------------- SUB MakeBackground ' Creates a fractal-like background by iteratively sampling and ' perturbing pixel values, producing a terrain effect. @@ -265,7 +275,7 @@ SUB MakeBackground lm = 127 s = 2 ^ 8 -5: +5 : s = s \ 2 x2 = (319 \ s) - 1 y2 = (199 \ s) - 1 @@ -306,8 +316,9 @@ SUB MakeBackground IF s > 2 THEN GOTO 5 END SUB -'------------------------------------------------------------------------------- + DEFSNG A-Z +'------------------------------------------------------------------------------- SUB PrintText (x, y, s, c, a$) ' Prints text at a specified (x,y) position on the screen, by reading ' from the 'font' array captured in InitializeFont. Allows for scaling = 1. @@ -323,11 +334,12 @@ SUB PrintText (x, y, s, c, a$) IF c1 > 0 THEN PSET (x1 + x2, y1 + y), c NEXT x1 NEXT y1 -7: +7 : x2 = x2 + 8 NEXT a END IF END SUB + '------------------------------------------------------------------------------- SUB RotatePoint (zx, zy, x1, y1, N) ' Rotates a point (x1,y1) around a center (zx,zy) by angle N (in radians). @@ -340,6 +352,7 @@ SUB RotatePoint (zx, zy, x1, y1, N) x1 = x2 * s1 - y2 * c1 + zx y1 = x2 * c1 + y2 * s1 + zy END SUB + '------------------------------------------------------------------------------- SUB Scene1 ' Initializes palette and reads 3D data from "data.dat" for a rotating @@ -380,7 +393,7 @@ SUB Scene1 SetPalette 40, 40, 40, 254 COLOR 254 LOCATE 2, 11 - PRINT "�ks hetk" + PRINT "One moment" OPEN "data.dat" FOR INPUT AS #1 INPUT #1, a @@ -409,15 +422,15 @@ SUB Scene1 nlin2! = l3! GOSUB addlin LOCATE 4, 10 - PRINT STR$(INT(a / (inpo - 1) * 100)) + "% valmis" + PRINT STR$(INT(a / (inpo - 1) * 100)) + "% ready" NEXT a CLOSE 1 CLS -3: +3 : tim = tim + 1 - sj$ = INKEY$ - IF sj$ = "q" THEN END + quitKey$ = INKEY$ + IF quitKey$ = "q" THEN END a = COS(tim / 25) an1 = COS(tim / 29) * a @@ -485,10 +498,11 @@ addlin: lin2!(nl) = nlin2! RETURN -4: +4 : angl1 = an1 angl2 = an2 END SUB + '------------------------------------------------------------------------------- SUB Scene2 ' Loads 3D data from "data.dat" and projects triangular faces to 2D. @@ -587,10 +601,11 @@ SUB Scene2 pol3(sun) = pol3(e) e = e - 1 - calc rpx(p1), rpy(p1), rpz(p1), rpx(p2), rpy(p2), rpz(p2), rpx(p3), rpy(p3), rpz(p3), d + ComputeShadeValue rpx(p1), rpy(p1), rpz(p1), rpx(p2), rpy(p2), rpz(p2), rpx(p3), rpy(p3), rpz(p3), d FillPolygon INT(rpx(p1)), INT(rpy(p1)), INT(rpx(p2)), INT(rpy(p2)), INT(rpx(p3)), INT(rpy(p3)), INT(d) NEXT a END SUB + '------------------------------------------------------------------------------- SUB Scene3 ' Demonstrates some simple raster effects and wave patterns, @@ -662,6 +677,7 @@ SUB Scene3 WaitForInput END SUB + '------------------------------------------------------------------------------- SUB Scene4 ' Uses MakeBackground to generate a fractal backdrop, then draws a box @@ -670,7 +686,7 @@ SUB Scene4 RANDOMIZE 1 MakeBackground - box 30, 50, 290, 150 + DrawRoundedBox 30, 50, 290, 150 SetPalette 32, 64, 32, 250 y = 0 @@ -684,6 +700,7 @@ SUB Scene4 WaitForInput END SUB + '------------------------------------------------------------------------------- SUB Scene5 ' Again uses MakeBackground, draws a box, and prints text describing @@ -692,7 +709,7 @@ SUB Scene5 RANDOMIZE 4 MakeBackground - box 30, 50, 290, 150 + DrawRoundedBox 30, 50, 290, 150 ' Renamed call SetPalette 32, 64, 32, 250 y = -8 @@ -710,6 +727,7 @@ SUB Scene5 WaitForInput END SUB + '------------------------------------------------------------------------------- SUB Scene7 ' Simple transition effect: horizontal lines are drawn across the screen @@ -723,6 +741,7 @@ SUB Scene7 SOUND 0, .5 NEXT a END SUB + '------------------------------------------------------------------------------- SUB Scene8 ' A more complex 3D-like demo. It generates fractal terrain in the background, @@ -777,7 +796,7 @@ SUB Scene8 RANDOMIZE 100 s = 64 -14: +14 : sp = s / 2 FOR y = 0 TO 100 STEP s FOR x = 0 TO 100 STEP s @@ -847,7 +866,7 @@ SUB Scene8 b = 0 hlkin = np + 1 -15: +15 : INPUT #1, x, y, z IF x = 999 THEN GOTO 16 a = a + 1 @@ -856,7 +875,7 @@ SUB Scene8 hlkz(a) = z GOTO 15 -16: +16 : INPUT #1, x, y IF x = 999 THEN GOTO 17 nl = nl + 1 @@ -865,7 +884,7 @@ SUB Scene8 linc(nl) = 2 GOTO 16 -17: +17 : CLOSE #1 np = np + a hlknu = a @@ -934,7 +953,7 @@ SUB Scene8 nl = nl + 8 NEXT a -10: +10 : SOUND 0, 1 IF INKEY$ <> "" THEN miin = 1 IF miin > 150 THEN GOTO 13 @@ -1043,7 +1062,7 @@ SUB Scene8 px1(a) = x1 + 160 py1(a) = y1 + 80 -11: +11 : NEXT a FOR a = 1 TO nl @@ -1060,12 +1079,13 @@ SUB Scene8 lby1(a) = y1 lbx2(a) = x2 lby2(a) = y2 -12: +12 : NEXT a IF kau > 200 THEN kau = kau - 10 IF tim < 28000 THEN GOTO 10 -13: +13 : END SUB + '------------------------------------------------------------------------------- SUB Scene9 ' Wrap-up scene that shows a background, draws a box, and prints @@ -1074,13 +1094,14 @@ SUB Scene9 RANDOMIZE 45 MakeBackground - box 30, 50, 290, 80 + DrawRoundedBox 30, 50, 290, 80 SetPalette 32, 64, 32, 250 y = -8 PrintText 30, 70 + y, 1, 250, " Thank you for attention!" WaitForInput END SUB + '------------------------------------------------------------------------------- SUB SetPalette (r, g, b, c) ' Sets a palette entry (c) to the specified (r,g,b) values, each 0..63 range. @@ -1097,15 +1118,11 @@ SUB SetPalette (r, g, b, c) OUT &H3C9, g OUT &H3C9, b END SUB + '------------------------------------------------------------------------------- -SUB start -' Initializes screen mode, sets up global constants pi and pii, -' and captures system font data for later use in PrintText. +SUB WaitForInput +' Reads exactly one character from the keyboard and stores it in inputKey$. - SCREEN 13 - pi = 3.141592 - pii = pi - fac = 360 / (pi * 2) - InitializeFont + inputKey$ = INPUT$(1) END SUB -'------------------------------------------------------------------------------- +