From: Svjatoslav Agejenko Date: Tue, 15 Oct 2024 18:01:23 +0000 (+0300) Subject: Refactoring code for better readability X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=6089ce4511d41c155cc1b6f143fcf9b92d84abf5;p=qbasicapps.git Refactoring code for better readability --- diff --git a/Graphics/3D/3dtext.bas b/Graphics/3D/3dtext.bas index 840d4bc..ed71abc 100755 --- a/Graphics/3D/3dtext.bas +++ b/Graphics/3D/3dtext.bas @@ -3,7 +3,7 @@ ' in 2003.12 ' H-Page: svjatoslav.eu ' E-Mail: svjatoslav@svjatoslav.eu - + DECLARE SUB prn (x!, y!, a$) DECLARE SUB pch (x!, y!, a$) DECLARE SUB readfnt () @@ -39,14 +39,13 @@ myx = 0 myy = 0 myz = -100 - - start nait3d SUB getcor +' Define the corners of a cube xn(nump + 1) = -150 yn(nump + 1) = -125 zn(nump + 1) = -200 @@ -63,7 +62,6 @@ xn(nump + 4) = -150 yn(nump + 4) = 125 zn(nump + 4) = -200 - xn(nump + 5) = -150 yn(nump + 5) = -125 zn(nump + 5) = 200 @@ -80,7 +78,7 @@ xn(nump + 8) = -150 yn(nump + 8) = 125 zn(nump + 8) = 200 - +' Define the lines connecting the corners point1(numl + 1) = nump + 1 point2(numl + 1) = nump + 2 @@ -120,9 +118,7 @@ point2(numl + 12) = nump + 4 nump = nump + 8 numl = numl + 12 - - - +' Define the corners of rectangle xn(nump + 1) = -150 yn(nump + 1) = -125 + 201 zn(nump + 1) = 0 @@ -139,6 +135,7 @@ xn(nump + 4) = -150 yn(nump + 4) = -125 zn(nump + 4) = 0 +' Define the lines connecting these corners point1(numl + 1) = nump + 1 point2(numl + 1) = nump + 2 @@ -154,31 +151,31 @@ point2(numl + 4) = nump + 1 nump = nump + 4 numl = numl + 4 - - prn 0, 0, "three dimensional " prn 0, -3, "text example" prn 0, -6, "etc etc etc" - - END SUB SUB kuus (x, y, z, s) +' Initialize variables b = 0 f = .3925 -FOR a = 0 + f TO 6 + f STEP 6.28 / 8 -x1 = SIN(a) * s -y1 = COS(a) * s -b = b + 1 - -xn(nump + b) = x1 + x -yn(nump + b) = y -zn(nump + b) = y1 + z +' Loop to create points of the octagon +FOR a = 0 + f TO 6 + f STEP 6.28 / 8 + x1 = SIN(a) * s + y1 = COS(a) * s + b = b + 1 + + ' Store the points in the shared arrays + xn(nump + b) = x1 + x + yn(nump + b) = y + zn(nump + b) = y1 + z NEXT a +' Define the lines connecting these points point1(numl + 1) = nump + 1 point2(numl + 1) = nump + 2 col(numl + 1) = 12 @@ -199,7 +196,6 @@ point1(numl + 5) = nump + 5 point2(numl + 5) = nump + 6 col(numl + 5) = 12 - point1(numl + 6) = nump + 6 point2(numl + 6) = nump + 7 col(numl + 6) = 12 @@ -214,206 +210,238 @@ col(numl + 8) = 12 nump = nump + b numl = numl + 8 + 'LOCATE 1, 1 'PRINT b - - - END SUB SUB nait3d +' Main loop for the 3D rendering 1 myx = myx + SIN(deg1) * mye myz = myz + COS(deg1) * mye - + myx = myx + COS(deg1) * myk myz = myz - SIN(deg1) * myk deg1 = deg1 + d1 Deg2 = Deg2 + d2 - + C1 = COS(deg1): S1 = SIN(deg1) C2 = COS(Deg2): S2 = SIN(Deg2) - -FOR a = 1 TO nump -xo = xn(a) - myx -yo = -yn(a) - myy -zo = zn(a) - myz - -x1 = (xo * C1 - zo * S1) -z1 = (xo * S1 + zo * C1) - -y1 = (yo * C2 - z1 * S2) -z2 = (yo * S2 + z1 * C2) - - -xo(a) = x(a) -yo(a) = y(a) -IF z2 < 20 THEN -x(a) = -1 -ELSE -x(a) = 320 + (x1 / z2 * 500) - -y(a) = 240 + (y1 / z2 * 500) -END IF +' Transform the coordinates +FOR a = 1 TO nump + xo = xn(a) - myx + yo = -yn(a) - myy + zo = zn(a) - myz + + x1 = (xo * C1 - zo * S1) + z1 = (xo * S1 + zo * C1) + + y1 = (yo * C2 - z1 * S2) + z2 = (yo * S2 + z1 * C2) + + ' Store the transformed coordinates + xo(a) = x(a) + yo(a) = y(a) + + ' Check if the point is within the view + IF z2 < 20 THEN + x(a) = -1 + ELSE + ' Apply perspective transformation + x(a) = 320 + (x1 / z2 * 500) + y(a) = 240 + (y1 / z2 * 500) + END IF NEXT - +' Draw the lines FOR a = 1 TO numl -p1 = point1(a) -p2 = point2(a) -IF xo(p1) = -1 OR xo(p2) = -1 THEN ELSE LINE (xo(p1), yo(p1))-(xo(p2), yo(p2)), 0 -IF x(p1) = -1 OR x(p2) = -1 THEN ELSE LINE (x(p1), y(p1))-(x(p2), y(p2)), col(a) + p1 = point1(a) + p2 = point2(a) + + ' Check if the points are within the view + IF xo(p1) = -1 OR xo(p2) = -1 THEN + ' Do nothing + ELSE + ' erase line at old coordinates + LINE (xo(p1), yo(p1))-(xo(p2), yo(p2)), 0 + END IF + + IF x(p1) = -1 OR x(p2) = -1 THEN + ' Do nothing + ELSE + ' draw line at new coordinates + LINE (x(p1), y(p1))-(x(p2), y(p2)), col(a) + END IF NEXT - +' Handle user input K$ = INKEY$ IF K$ <> "" THEN -SELECT CASE K$ + SELECT CASE K$ -CASE CHR$(0) + "P" -mye = mye - 1 + CASE CHR$(0) + "P" + mye = mye - 1 -CASE CHR$(0) + "H" -mye = mye + 1 + CASE CHR$(0) + "H" + mye = mye + 1 -CASE CHR$(0) + "M" -myk = myk + 1 + CASE CHR$(0) + "M" + myk = myk + 1 -CASE CHR$(0) + "K" -myk = myk - 1 + CASE CHR$(0) + "K" + myk = myk - 1 -CASE "+" -myy = myy + 3 + CASE "+" + myy = myy + 3 -CASE "-" -myy = myy - 3 + CASE "-" + myy = myy - 3 -CASE "6" -d1 = d1 + .01 + CASE "6" + d1 = d1 + .01 -CASE "4" -d1 = d1 - .01 + CASE "4" + d1 = d1 - .01 -CASE "8" -d2 = d2 - .01 + CASE "8" + d2 = d2 - .01 -CASE "2" -d2 = d2 + .01 + CASE "2" + d2 = d2 + .01 + CASE " " + d1 = d1 / 2 + d2 = d2 / 2 + d3 = d3 / 2 + mye = mye / 2 + myk = myk / 2 -CASE " " -d1 = d1 / 2 -d2 = d2 / 2 -d3 = d3 / 2 -mye = mye / 2 -myk = myk / 2 + CASE "q" + SYSTEM -CASE "q" -SYSTEM + CASE CHR$(27) + SYSTEM -CASE CHR$(27) -SYSTEM - -END SELECT + END SELECT END IF GOTO 1 + END SUB SUB pch (x, y, a$) - +' Initialize variables b = ASC(a$) up = 0 ul = 0 +' Loop to create points for the character FOR c = 0 TO 100 -IF tpx(c, b) = 999 THEN GOTO 4 -up = up + 1 -xn(nump + up) = x + tpx(c, b) -yn(nump + up) = y - tpy(c, b) -zn(nump + up) = 0 + IF tpx(c, b) = 999 THEN GOTO 4 + up = up + 1 + xn(nump + up) = x + tpx(c, b) + yn(nump + up) = y - tpy(c, b) + zn(nump + up) = 0 NEXT c 4 +' Loop to define the lines for the character FOR c = 0 TO 100 -IF tl1(c, b) = 999 THEN GOTO 5 -ul = ul + 1 -point1(numl + ul) = tl1(c, b) + nump + 1 -point2(numl + ul) = tl2(c, b) + nump + 1 -col(numl + ul) = 4 + IF tl1(c, b) = 999 THEN GOTO 5 + ul = ul + 1 + point1(numl + ul) = tl1(c, b) + nump + 1 + point2(numl + ul) = tl2(c, b) + nump + 1 + col(numl + ul) = 4 NEXT c 5 - - +' Update the counters nump = nump + up numl = numl + ul - END SUB SUB porand +' do floor of the 3D room +' Loop to create floor tiles made of hexagons and squares FOR x = -100 TO 0 STEP 12.067 + .3 -FOR z = -100 TO 0 STEP 12.067 + .3 -kuus x, -125, z, 6.53 -ruut x + 6.033 + .15, -125, z + 6.033 + .15, 3.111 + .3 -NEXT z + FOR z = -100 TO 0 STEP 12.067 + .3 + kuus x, -125, z, 6.53 + ruut x + 6.033 + .15, -125, z + 6.033 + .15, 3.111 + .3 + NEXT z NEXT x +' Loop to create squares FOR y = -100 TO 0 STEP 20.3 -FOR x = -100 TO 0 STEP 20.3 -ruut2 x, y, 200, 10 -NEXT x + FOR x = -100 TO 0 STEP 20.3 + ruut2 x, y, 200, 10 + NEXT x NEXT y - END SUB SUB prn (x, y, a$) +' Loop to print each character FOR b = 1 TO LEN(a$) -c$ = RIGHT$(LEFT$(a$, b), 1) -pch x + b * 3, y, c$ + c$ = RIGHT$(LEFT$(a$, b), 1) + pch x + b * 3, y, c$ NEXT b + END SUB SUB readfnt + +' Open the font file OPEN "font.dat" FOR INPUT AS #1 + +' Loop to read the font data 3 IF EOF(1) <> 0 THEN GOTO 2 LINE INPUT #1, a$ + IF LEFT$(a$, 1) = "#" THEN -chr = ASC(RIGHT$(LEFT$(a$, 3), 1)) -pp = 0 -lp = 0 + chr = ASC(RIGHT$(LEFT$(a$, 3), 1)) + + ' Initialize counters + pp = 0 + lp = 0 END IF + +' read the points for the character IF LEFT$(a$, 1) = "p" THEN -tpx(pp, chr) = VAL(RIGHT$(LEFT$(a$, 3), 1)) -tpy(pp, chr) = VAL(RIGHT$(LEFT$(a$, 5), 1)) -pp = pp + 1 + tpx(pp, chr) = VAL(RIGHT$(LEFT$(a$, 3), 1)) + tpy(pp, chr) = VAL(RIGHT$(LEFT$(a$, 5), 1)) + pp = pp + 1 END IF + +' read the lines for the character IF LEFT$(a$, 1) = "l" THEN -tl1(lp, chr) = VAL(RIGHT$(LEFT$(a$, 3), 1)) -tl2(lp, chr) = VAL(RIGHT$(LEFT$(a$, 5), 1)) -lp = lp + 1 + tl1(lp, chr) = VAL(RIGHT$(LEFT$(a$, 3), 1)) + tl2(lp, chr) = VAL(RIGHT$(LEFT$(a$, 5), 1)) + lp = lp + 1 END IF GOTO 3 + +' Close the font file 2 CLOSE #1 - END SUB SUB ruut (x, y, z, s) + +' Define the corners of the rectangle xn(nump + 1) = x yn(nump + 1) = y zn(nump + 1) = z + s @@ -430,6 +458,7 @@ xn(nump + 4) = x - s yn(nump + 4) = y zn(nump + 4) = z +' Define the lines connecting these corners point1(numl + 1) = nump + 1 point2(numl + 1) = nump + 2 col(numl + 1) = 10 @@ -446,11 +475,15 @@ point1(numl + 4) = nump + 4 point2(numl + 4) = nump + 1 col(numl + 4) = 10 +' Update the counters nump = nump + 4 numl = numl + 4 + END SUB SUB ruut2 (x, y, z, s) + +' Define the corners of the rectangle xn(nump + 1) = x - s yn(nump + 1) = y - s zn(nump + 1) = z @@ -467,6 +500,7 @@ xn(nump + 4) = x - s yn(nump + 4) = y + s zn(nump + 4) = z +' Define the lines connecting these corners point1(numl + 1) = nump + 1 point2(numl + 1) = nump + 2 col(numl + 1) = 14 @@ -483,32 +517,41 @@ point1(numl + 4) = nump + 4 point2(numl + 4) = nump + 1 col(numl + 4) = 14 +' Update the counters nump = nump + 4 numl = numl + 4 END SUB SUB start + +' Initialize the screen and clear it SCREEN 12 CLS +' Set the default color for all points FOR a = 1 TO 4000 -col(a) = 15 + col(a) = 15 NEXT a +' Initialize counters nump = 0 numl = 0 + +' Initialize font data FOR a = 0 TO 255 -FOR b = 0 TO 10 -tpx(b, a) = 999 -tpy(b, a) = 999 -tl1(b, a) = 999 -tl2(b, a) = 999 -NEXT b + FOR b = 0 TO 10 + tpx(b, a) = 999 + tpy(b, a) = 999 + tl1(b, a) = 999 + tl2(b, a) = 999 + NEXT b NEXT a - +' Read the font data readfnt + +' Define the corners of the cubes getcor END SUB