'=========================================================================== ' Subject: 2D POLYGON ENGINE Date: 08-22-96 (11:30) ' Author: Brent P. Newhall Code: QB, QBasic, PDS ' Origin: comp.lang.basic.misc Packet: GRAPHICS.ABC '=========================================================================== 'This is a C-to-BASIC conversion from Andre LaMothe's book "Teach 'Yourself Game Programming in 21 Days". It's a simple demo with three 'asteroids twirling on the screen and a scrolling starfield in the 'background. 'If you have need, take it and use it. There are enough SUBs to do most 'of what you should need to do. ' Polygon ' by Brent P. Newhall (BrentN@juno.com) DEFINT A-Z CONST FALSE = 0, TRUE = NOT FALSE CONST MAX.VERTICES = 15 CONST MAX.STARS = 30 TYPE VertexType x AS SINGLE y AS SINGLE END TYPE TYPE PolygonType border AS INTEGER ' Border color interior AS INTEGER ' Interior color closed AS INTEGER ' Is the polygon closed? filled AS INTEGER ' Is the polygon filled? lxo AS INTEGER ' Local X origin lyo AS INTEGER ' Local Y origin NumVertices AS INTEGER END TYPE TYPE StarType x AS INTEGER y AS INTEGER dist AS INTEGER END TYPE DECLARE SUB CreateTables () DECLARE SUB DrawPolygon (poly AS PolygonType, polyvert() AS VertexType) DECLARE SUB ErasePolygon (poly AS PolygonType, polyvert() AS VertexType) DECLARE SUB MovePolygon (poly AS PolygonType, dx AS INTEGER, dy AS INTEGER) DECLARE SUB ScalePolygon (poly AS PolygonType, polyvert() AS VertexType, scale AS SINGLE) DECLARE SUB RotatePolygon (poly AS PolygonType, polyvert() AS VertexType, angle AS INTEGER) DECLARE SUB StarField () DIM ast1 AS PolygonType ' Create basic asteroids DIM ast2 AS PolygonType DIM ast3 AS PolygonType ast1.NumVertices = 5 ast2.NumVertices = 5 ast3.NumVertices = 5 REDIM astvert1(1 TO ast1.NumVertices) AS VertexType ' Create vertices REDIM astvert2(1 TO ast2.NumVertices) AS VertexType REDIM astvert3(1 TO ast3.NumVertices) AS VertexType DIM SHARED star(1 TO MAX.STARS) AS StarType DIM SHARED SinLook(0 TO 361) AS DOUBLE, CosLook(0 TO 361) AS DOUBLE DIM SHARED c(1 TO 3) ' Color c(1) = 15: c(2) = 7: c(3) = 8 FOR cnt = 1 TO MAX.STARS ' Define all the stars star(cnt).x = INT(RND * 320) ' Create random position star(cnt).y = INT(RND * 200) star(cnt).dist = INT(RND * 3 + 1) NEXT cnt ast1.border = 8 ast1.interior = 8 ast1.closed = TRUE ast1.filled = FALSE ast1.lxo = 110 ast1.lyo = 100 FOR cnt1 = 1 TO ast1.NumVertices READ astvert1(cnt1).x, astvert1(cnt1).y NEXT cnt1 ast2.border = 8 ast2.interior = 8 ast2.closed = TRUE ast2.filled = FALSE ast2.lxo = 160 ast2.lyo = 80 FOR cnt1 = 1 TO ast2.NumVertices READ astvert2(cnt1).x, astvert2(cnt1).y NEXT cnt1 ast3.border = 8 ast3.interior = 8 ast3.closed = TRUE ast3.filled = FALSE ast3.lxo = 210 ast3.lyo = 100 FOR cnt1 = 1 TO ast3.NumVertices READ astvert3(cnt1).x, astvert3(cnt1).y NEXT cnt1 PRINT "Creating tables...." CreateTables SCREEN 7 DO StarField RotatePolygon ast1, astvert1(), 5 RotatePolygon ast2, astvert2(), 8 RotatePolygon ast3, astvert3(), -4 DrawPolygon ast1, astvert1() DrawPolygon ast2, astvert2() DrawPolygon ast3, astvert3() t! = TIMER: WHILE t! = TIMER: WEND ' Pause ErasePolygon ast1, astvert1() ErasePolygon ast2, astvert2() ErasePolygon ast3, astvert3() IF INKEY$ <> "" THEN quit = 1 LOOP UNTIL quit > 0 END ' Asteroid 1 vertices DATA 0,-15 DATA 20, 5 DATA 5, 7 DATA -1, 10 DATA -4, 1 ' Asteroid 2 vertices DATA 0,-15 DATA 20,-9 DATA 10, 7 DATA -1, 10 DATA -4, 1 ' Asteroid 3 vertices DATA 0,-15 DATA 10,-2 DATA 5, 7 DATA -1, 10 DATA -9, 1 DEFSNG A-Z SUB CreateTables FOR cnt = 0 TO 360 CosLook(cnt) = COS(cnt * 3.14159 / 180) SinLook(cnt) = SIN(cnt * 3.14159 / 180) NEXT cnt END SUB SUB DrawPolygon (poly AS PolygonType, polyvert() AS VertexType) xo = poly.lxo yo = poly.lyo FOR cnt = 1 TO poly.NumVertices - 1 LINE (xo + polyvert(cnt).x, yo + polyvert(cnt).y)-(xo + polyvert(cnt + 1).x, yo + polyvert(cnt + 1).y), poly.border NEXT cnt IF poly.closed THEN LINE (xo + polyvert(poly.NumVertices).x, yo + polyvert(poly.NumVertices).y)-(xo + polyvert(1).x, yo + polyvert(1).y), poly.border END IF END SUB SUB ErasePolygon (poly AS PolygonType, polyvert() AS VertexType) xo = poly.lxo yo = poly.lyo FOR cnt = 1 TO poly.NumVertices - 1 LINE (xo + polyvert(cnt).x, yo + polyvert(cnt).y)-(xo + polyvert(cnt + 1).x, yo + polyvert(cnt + 1).y), 0 NEXT cnt IF poly.closed THEN LINE (xo + polyvert(poly.NumVertices).x, yo + polyvert(poly.NumVertices).y)-(xo + polyvert(1).x, yo + polyvert(1).y), 0 END IF END SUB SUB MovePolygon (poly AS PolygonType, dx AS INTEGER, dy AS INTEGER) poly.lxo = poly.lxo + dx poly.lyo = poly.lyo + dy END SUB SUB RotatePolygon (poly AS PolygonType, polyvert() AS VertexType, angle AS INTEGER) IF angle >= 0 THEN si = SinLook(angle) cs = CosLook(angle) ELSE si = SinLook(angle + 360) cs = CosLook(angle + 360) END IF FOR cnt = 1 TO poly.NumVertices rx = polyvert(cnt).x * cs - polyvert(cnt).y * si ry = polyvert(cnt).y * cs + polyvert(cnt).x * si polyvert(cnt).x = rx polyvert(cnt).y = ry NEXT cnt END SUB SUB ScalePolygon (poly AS PolygonType, polyvert() AS VertexType, scale AS SINGLE) FOR cnt = 1 TO poly.NumVertices polyvert(cnt).x = polyvert(cnt).x * scale polyvert(cnt).y = polyvert(cnt).y * scale NEXT cnt END SUB DEFINT A-Z SUB StarField FOR cnt = 1 TO MAX.STARS PSET (star(cnt).x, star(cnt).y), 0 star(cnt).y = star(cnt).y + (4 - star(cnt).dist) IF star(cnt).y > 199 THEN star(cnt).x = INT(RND * 320) star(cnt).y = INT(RND * 200) star(cnt).dist = INT(RND * 3 + 1) END IF PSET (star(cnt).x, star(cnt).y), c(star(cnt).dist) NEXT cnt END SUB