Qbasicnews.com

Full Version: Simple graphics engine. Need advice.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Wow, it's literally been over a year since I last logged in.

Anyways, I just finished coding a simple graphics engine for future games and I was hoping I could get some advice. Here is the code:

Code:
DECLARE SUB graphicsEngine (graphic!(), graphicWidth!, graphicHeight!, xCoor!, yCoor!, angle!)
DECLARE SUB graphicsLoader (graphic!())
DIM test(100, 100)

LET speed = 5
LET xCoor = 160
LET yCoor = 100
LET bgColor = 2
LET graphicWidth = 26
LET graphicHeight = 29

LET angleIncreaseKey$ = "q"
LET angleDecreaseKey$ = "e"
LET moveUpKey$ = "w"
LET moveDownKey$ = "s"
LET moveLeftKey$ = "a"
LET moveRightKey$ = "d"

CLS
SCREEN 7, 0, 1, 0

PRINT "Graphics Engine Demonstation"
PRINT
PRINT "This graphics engine is capable of"
PRINT "2D rotation without overwriting"
PRINT "any background images."
PRINT
PRINT "Images must be stored in"
PRINT "2-dimensional arrays."
PRINT
PRINT "Q - Rotate Counter-Clockwise"
PRINT "E - Rotate Clockwise"
PRINT
PRINT "W - Shift Up"
PRINT "S - Shift Down"
PRINT "A - Shift Left"
PRINT "D - Shift Right"
PRINT
PRINT "X - Quit"
PRINT
PRINT "Press any key to continue..."
PCOPY 1, 0
DO WHILE key$ = ""
  LET key$ = INKEY$
LOOP

CALL graphicsLoader(test())
DO WHILE key$ <> "x"
  LET key$ = INKEY$
  IF key$ = angleDecreaseKey$ THEN
      LET angle = angle - 1
    ELSEIF key$ = angleIncreaseKey$ THEN
      LET angle = angle + 1
    ELSEIF key$ = moveUpKey$ THEN
      LET yCoor = yCoor - 1
    ELSEIF key$ = moveDownKey$ THEN
      LET yCoor = yCoor + 1
    ELSEIF key$ = moveLeftKey$ THEN
      LET xCoor = xCoor - 1
    ELSEIF key$ = moveRightKey$ THEN
      LET xCoor = xCoor + 1
  END IF
  IF angle >= 360 THEN
      LET angle = angle - 360
    ELSEIF angle <= -360 THEN
      LET angle = angle + 360
  END IF
  CLS
  LINE (0, 0)-(320, 200), bgColor
  LINE (320, 0)-(0, 200), bgColor
  LINE (160, 0)-(160, 200), bgColor
  LINE (0, 100)-(320, 100), bgColor
  PRINT "Angle ="; angle
  CALL graphicsEngine(test(), graphicWidth, graphicHeight, xCoor, yCoor, angle)
  PCOPY 1, 0
LOOP

END

DATA 26,29
DATA 14,14,14,14,14,-1,-1,15,15,15,15,15,-1,-1,07,07,07,07,07,-1,-1,06,06,06,06,06
DATA -1,-1,14,-1,-1,-1,-1,15,-1,-1,-1,-1,-1,-1,07,-1,-1,-1,-1,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,14,-1,-1,-1,-1,15,15,15,-1,-1,-1,-1,07,07,07,07,07,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,14,-1,-1,-1,-1,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,07,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,14,-1,-1,-1,-1,15,15,15,15,15,-1,-1,07,07,07,07,07,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
DATA 14,14,14,14,14,-1,-1,15,15,15,15,15,-1,-1,07,07,07,07,07,-1,-1,06,06,06,06,06
DATA -1,-1,14,-1,-1,-1,-1,15,-1,-1,-1,-1,-1,-1,07,-1,-1,-1,-1,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,14,-1,-1,-1,-1,15,15,15,-1,-1,-1,-1,07,07,07,07,07,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,14,-1,-1,-1,-1,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,07,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,14,-1,-1,-1,-1,15,15,15,15,15,-1,-1,07,07,07,07,07,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
DATA 14,14,14,14,14,-1,-1,15,15,15,15,15,-1,-1,07,07,07,07,07,-1,-1,06,06,06,06,06
DATA -1,-1,14,-1,-1,-1,-1,15,-1,-1,-1,-1,-1,-1,07,-1,-1,-1,-1,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,14,-1,-1,-1,-1,15,15,15,-1,-1,-1,-1,07,07,07,07,07,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,14,-1,-1,-1,-1,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,07,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,14,-1,-1,-1,-1,15,15,15,15,15,-1,-1,07,07,07,07,07,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
DATA 14,14,14,14,14,-1,-1,15,15,15,15,15,-1,-1,07,07,07,07,07,-1,-1,06,06,06,06,06
DATA -1,-1,14,-1,-1,-1,-1,15,-1,-1,-1,-1,-1,-1,07,-1,-1,-1,-1,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,14,-1,-1,-1,-1,15,15,15,-1,-1,-1,-1,07,07,07,07,07,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,14,-1,-1,-1,-1,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,07,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,14,-1,-1,-1,-1,15,15,15,15,15,-1,-1,07,07,07,07,07,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
DATA 14,14,14,14,14,-1,-1,15,15,15,15,15,-1,-1,07,07,07,07,07,-1,-1,06,06,06,06,06
DATA -1,-1,14,-1,-1,-1,-1,15,-1,-1,-1,-1,-1,-1,07,-1,-1,-1,-1,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,14,-1,-1,-1,-1,15,15,15,-1,-1,-1,-1,07,07,07,07,07,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,14,-1,-1,-1,-1,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,07,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,14,-1,-1,-1,-1,15,15,15,15,15,-1,-1,07,07,07,07,07,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
DATA 14,14,14,14,14,-1,-1,15,15,15,15,15,-1,-1,07,07,07,07,07,-1,-1,06,06,06,06,06
DATA -1,-1,14,-1,-1,-1,-1,15,-1,-1,-1,-1,-1,-1,07,-1,-1,-1,-1,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,14,-1,-1,-1,-1,15,15,15,-1,-1,-1,-1,07,07,07,07,07,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,14,-1,-1,-1,-1,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,07,-1,-1,-1,-1,06,-1,-1
DATA -1,-1,14,-1,-1,-1,-1,15,15,15,15,15,-1,-1,07,07,07,07,07,-1,-1,-1,-1,06,-1,-1

SUB graphicsEngine (graphic(), graphicWidth, graphicHeight, xCoor, yCoor, negAngle)
  LET angle = -negAngle
  LET pi = 3.141592652589793#
  LET radians = angle * (pi / 180)
  LET shiftDistance = SQR((xCoor - (xCoor + (graphicWidth / 2))) ^ 2 + (yCoor - (yCoor + (graphicHeight / 2))) ^ 2)
  LET shiftRadians = pi + ATN((((graphicHeight / 2) + yCoor) - yCoor) / (((graphicWidth / 2) + xCoor) - xCoor))
  LET shiftXCoor = xCoor + shiftDistance * COS(radians + (shiftRadians))
  LET shiftYCoor = yCoor + shiftDistance * SIN(radians + (shiftRadians))
  FOR y = shiftYCoor + 1 TO shiftYCoor + graphicHeight
    FOR x = shiftXCoor + 1 TO shiftXCoor + graphicWidth
      IF graphic(x - shiftXCoor, y - shiftYCoor) <> -1 THEN
        LET distance = SQR((x - shiftXCoor) ^ 2 + (y - shiftYCoor) ^ 2)
        LET tRadians = radians + ATN(((y - shiftYCoor) * (pi / 180)) / ((x - shiftXCoor) * (pi / 180)))
        PSET (shiftXCoor + (distance * COS(tRadians)), shiftYCoor + (distance * SIN(tRadians))), graphic(x - shiftXCoor, y - shiftYCoor)
      END IF
    NEXT x
  NEXT y
END SUB

SUB graphicsLoader (graphic())
  READ graphicsWidth
  READ graphicsHeight
  FOR y = 1 TO graphicsHeight
    FOR x = 1 TO graphicsWidth
      READ graphic(x, y)
    NEXT x
  NEXT y
END SUB

The main code is contained within the graphicsEngine SUB (the math was incredibly complicated). If you load this into your QBasic editor and run it, you'll notice some open spaces in the picture (which just says "test" at the moment). I'm guessing some of my math wasn't accurate enough. I'm not sure what I'd need to change to fix it, though.

Also, how do you like it overall? How could I improve my code?

P.S. Is there a keyword for Pi? Right now I have to create a variable for it.
No, no keyword.

Just do:
PI = 3.141593 and that'll be fine. :wink:
I looked at it....VERY nice.
Heh, I fixed the problem. I just needed to round the x and y coordinates in the PSET command to the nearest pixel. Simple, yet so elusive.
You can do CONST Pi = 3.141593. It's a global constant that way.