Liberty BASIC Community Forum
« More Psuedo 3D Road »

Welcome Guest. Please Login or Register.
Feb 18th, 2018, 3:47pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: More Psuedo 3D Road  (Read 60 times)
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5655
xx More Psuedo 3D Road
« Thread started on: Feb 3rd, 2018, 07:03am »

I have read Lew's tutorial but I am not really grasping Segment based roads. So I did this. I will try moving texture now instead of line blitting.


Code:
    nomainwin
    'define our road and screen parameters
    roadlines = 299     'how many y lines of the screen are road
                        'we wont draw them all because some are too
                        'small to see
    speed = 10          'scrolling speed, same as car speed
    camY = -1           'camera height above road
    midX = 400          'mid screen x
    bakX = 400          'background x scroll position
    bakY = 3            'background y scroll
    scrY = 600          'screen height
    playerY = 8         'What line is the player sprite on?
    halfwidth = 300     'width of the road
    screenline = scrY-1 'start drawing at bottom of screen
    heightline = scrY-1 'we are always on top of the bottom line of the road
    horizon = scrY-1    'we always start horizon at bottom of screen
    texturepos = 100    'starting position for texture pointer

    trackpointer=1      'starting position for our trackdata


    'build our ZMap data, this is a distance value for each y line
    'we will use it to decide when to draw a light line and when to
    'draw a dark line
    dim ZMap(roadlines) 'Initialize ZMap array
    for i = 1 TO roadlines
        ZMap(i) = camY / (i - (scrY / 2))
        'print i,using("#.######",ZMap(i))
    next

    ' Normalize ZMap so the line with the player on it is scale=1
    b = 1 / ZMap(playerY)
    b = b * 100
    for i = 1 TO roadlines
        ZMap(i) = ZMap(i) * b
        'print i,ZMap(i)
    next

    'build our track curve info, a curve followed by a straight
    dim track(2000)
    dim hill(2000)
    i=1
    track(0)=0
    hill(0)=0
    'long straight
    for deg=1 to 300
        track(i)=0
        i=i+1
    next
    'long curve
    for deg=1 to 360
        track(i)=4*sin(deg/57.29577951)-track(i-1)
        i=i+1
    next
    'short straight
    for deg=1 to 100
        track(i)=0
        i=i+1
    next
    'sharp curve
    for deg=1 to 180
        track(i)=8*sin(deg/57.29577951)-track(i-1)
        i=i+1
    next
    maxtrack=i-1
    deg=1
    for i = 1 to maxtrack
        hill(i)=2*sin(deg/57.29577951)-hill(i-1)
        'print i,using("##.######",hill(i))
        deg=deg+2
        if deg>360 then deg=deg-360
    next

    'nomainwin
    WindowWidth = 800
    WindowHeight = 600
    UpperLeftX=int((DisplayWidth-WindowWidth)/2)
    UpperLeftY=int((DisplayHeight-WindowHeight)/2)
    graphicbox #main.g,   0,0,800,600
    open "Road Race" for window_nf as #main
    #main, "trapclose [quit]"
    #main.g, "down"

    'set up our bitmaps, open device contexts and store our bitmaps in them
    #main.g "fill cyan ; getbmp scr 0 0 800 600"
    'draw the background and road images
    randomize .5
    #main.g "color darkcyan"
    d=rnd(0)
    y=150
    for n=0 to 1599
        #main.g "place ";n;" 300 ; go ";y
        y=y+d
        if y>200+rnd(0)*100 then d=0-d
        if y<100+rnd(0)*50 then d=rnd(0)
    next
    #main.g "color darkgreen"
    y=75
    for n=0 to 1599
        #main.g "place ";n;" 300 ; go ";y
        y=y+d
        if y>150+rnd(0)*50 then d=0-d
        if y<50+rnd(0)*25 then d=rnd(0)
    next
    #main.g "color green"
    y=25
    for n=0 to 1599
        #main.g "place ";n;" 300 ; go ";y
        y=y+d
        if y>50+rnd(0)*20 then d=0-d
        if y<10+rnd(0)*10 then d=rnd(0)
    next
    #main.g "backcolor yellow ; place 400 50 ; circlefilled 20"
    'dark line
    #main.g "color darkgreen ; line 0 0 200 0"
    #main.g "color white ; line 200 0 210 0"
    #main.g "color black ; line 210 0 595 0"
    #main.g "color white ; line 595 0 605 0"
    #main.g "color darkgreen ; line 605 0 800 0"
    'light line
    #main.g "color green ; line 0 1 200 1"
    #main.g "color lightgray ; line 200 1 210 1"
    #main.g "color black ; line 210 1 395 1"
    #main.g "color white ; line 395 1 405 1"
    #main.g "color black ; line 405 1 595 1"
    #main.g "color lightgray ; line 595 1 605 1"
    #main.g "color darkgreen ; line 605 1 800 1"
    #main.g "getbmp road 0 0 800 600"
    'motorcyclist
    #main.g "backcolor black ; color black ;place 0 300 ; boxfilled 40 390"
    #main.g "backcolor red ; place 20 325 ; circlefilled 10"
    #main.g "place 10 330 ; boxfilled 30 360"
    #main.g "backcolor blue ; place 5 330 ; boxfilled 10 350"
    #main.g "backcolor blue ; place 30 330 ; boxfilled 35 350"
    #main.g "place 10 360 ; boxfilled 30 380"
    #main.g "backcolor darkgray ;place 15 360 ; boxfilled 25 390"

    #main.g "getbmp road 0 0 800 600"

    ' open the dll file used for TransparentBlt
    open "msimg32.dll" for dll as #m

    'ScreenDC is our graphics screen/graphicbox
    ScreenDC=GetDC(hwnd(#main.g))

    'GraphicsDC is a bitmap held in memory from which we get all our graphics
    GraphicsDC=CreateCompatibleDC(ScreenDC)
    hBitmap=hbmp("road")
    oldBmp=SelectObject(GraphicsDC,hBitmap)

    'BufferDC is a copy of the screen in memory used as a buffer to draw graphics to
    BufferDC=CreateCompatibleDC(ScreenDC)
    hBitmap=hbmp("scr")
    oldBmp=SelectObject(BufferDC,hBitmap)

    'display the background for the first time
    call StretchBlt,ScreenDC,0,0,800,500,GraphicsDC,2,300,250,200




    timer 56, [redraw]
    wait



    [redraw]

        ' flip the buffer to the screen so that it is visible and stable for as long as possible
        call StretchBlt,ScreenDC,0,0,800,600,BufferDC,0,0,800,600

        ' Set up the next frame
        bakX=bakX+track(trackpointer)*speed/20
        if bakX>600 then bakX=bakX-600
        if bakX<0 then bakX=bakX+600
        bakY=bakY+hill(trackpointer)*(speed>0)

        X = midX 'mid screen
        trackindex=trackpointer

        'draw the sky/background
        call StretchBlt,BufferDC,0,0,800,500,GraphicsDC,int(bakX),int(2+bakY),200,300

        halfwidth = 300 'width of the road
        screenline = scrY - 1 'start at bottom of screen
        heightline = screenline 'we are always on top of the bottom line of the road
        horizon = screenline ' we always start horizon at bottom of screen

        for i = 1 to roadlines-50 step 2
            'what color/texture should the line be
            if (ZMap(i) + texturepos) mod 100 > 50 then
                t=0
            else
                t=1
            end if

            'if the road line is above the horizon draw it
            if heightline>=horizon then
                'draw grass
                call StretchBlt,BufferDC,0,screenline,800,2,GraphicsDC,0,t,200,1
                'draw road
                call StretchBlt,BufferDC,int(X-halfwidth),int(screenline),int(halfwidth+halfwidth),2,GraphicsDC,200,t,400,1
            end if

            'if the road was visible go up a roadline
            if heightline<screenline then
                screenline = screenline - 2
            end if

            halfwidth = halfwidth - 2 + hill(trackindex)

            heightline=heightline - 2 + hill(trackindex)

            'remember how high we got
            if heightline<horizon then horizon=heightline

            X=X+track(trackindex)
            trackindex=trackindex+1
            if trackindex>maxtrack then trackindex=1

        next i

        'draw the motorcycle, this really needs z order/roadhight code
        'so simply draw it right now
        call TransparentBlt,BufferDC,int(X-33),400,66,146,GraphicsDC,0,300,40,90,0

        texturepos = texturepos + speed
        if texturepos>200 then texturepos=texturepos-200

        trackpointer=trackpointer+speed/10
        if trackpointer>maxtrack then trackpointer=trackpointer-maxtrack

        if GetAsnycKeyState(_VK_UP) then
            speed=speed+(speed<20)
        end if
        if GetAsnycKeyState(_VK_DOWN) then
            speed=speed-(speed>0)
        end if
        if GetAsnycKeyState(_VK_RIGHT) then

        end if
        if GetAsnycKeyState(_VK_LEFT) then

        end if
        if GetAsnycKeyState(_VK_ESCAPE) then
            goto [quit]
        end if

        wait




[quit]
    timer 0
    unloadbmp "road"
    unloadbmp "scr"
    call ReleaseDC hwnd(#main), ScreenDC
    call ReleaseDC hwnd(#main), BufferDC
    call ReleaseDC hwnd(#main), GraphicsDC
    call DeleteDC ScreenDC
    call DeleteDC BufferDC
    call DeleteDC GraphicsDC
    close #main
    close #m
    end

function GetAsnycKeyState(key)
    calldll #user32, "GetAsyncKeyState",_
    key as long,ret as long
    if ret <> 0 then GetAsnycKeyState = 1
end function


 

« Last Edit: Feb 3rd, 2018, 07:03am by Rod » User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5655
xx Re: More Psuedo 3D Road
« Reply #1 on: Feb 3rd, 2018, 07:04am »

part two

Code:

'=============================Window and DC functions=================================
Function GetDC(hWnd)
    CallDLL #user32, "GetDC",_
        hWnd As Ulong,_  'window or control handle
        GetDC As Ulong   'returns device context
    End Function

Sub ReleaseDC hWnd, hDC
    CallDLL#user32,"ReleaseDC",_
        hWnd As Ulong,_  'window or control handle
        hDC As Ulong,_   'handle of DC to delete
        result As Long
    End Sub

Function CreateCompatibleDC(hDC)
    CallDLL #gdi32,"CreateCompatibleDC",_
        hDC As Ulong,_               'window DC
        CreateCompatibleDC As Ulong  'memory DC
    End Function

Sub DeleteDC hDC
        CallDLL #gdi32, "DeleteDC",_
        hDC As Ulong,_   'memory DC to delete
        result As Boolean
    End Sub

Sub StretchBlt hDCdest,x,y,w,h,hDCsrc,x2,y2,w2,h2
    CallDLL #gdi32, "SetStretchBltMode",_
        hDCdest As Ulong,_       'device context
        _COLORONCOLOR As Long,_ 'color reduction mode
        result As Long
    CallDLL #gdi32, "StretchBlt",_
        hDCdest As Ulong,_   'destination
        x As Long,_         'destination x pos
        y As Long,_         'destination y pos
        w As Long,_         'destination width desired
        h As Long,_         'destination height desired
        hDCsrc As Ulong,_    'source
        x2 As Long,_        'x location to start from source
        y2 As Long,_        'y location to start from source
        w2 As Long,_        'width desired from source
        h2 As Long,_        'height desired from source
        _SRCCOPY As Ulong,_ 'dwRasterOperation
        result As Boolean
    End Sub

Sub TransparentBlt hDCdest,x,y,w,h,hDCsrc,x2,y2,w2,h2,crTransparent
    calldll #m, "TransparentBlt",_
        hDCdest As ULong,_   'destination
        x As Long,_         'destination x pos
        y As Long,_         'destination y pos
        w As Long,_         'destination width desired
        h As Long,_         'destination height desired
        hDCsrc As ULong,_    'source
        x2 As Long,_        'x location to start from source
        y2 As Long,_        'y location to start from source
        w2 As Long,_        'width desired from source
        h2 As Long,_        'height desired from source
        crTransparent as Ulong,_ 'color to make transparent
        result as boolean
    end sub

Function SelectObject(hDC,hObject)
    CallDLL #gdi32,"SelectObject",_
        hDC As Ulong,_        'memory device context
        hObject As Ulong,_    'handle of object
        SelectObject As Ulong 'returns previously selected object
    End Function

Function SetPixel(hdc,x,y,rgbColor)
    CallDll #gdi32, "SetPixel",_
        hdc as Ulong,_       'the handle of the Device context from GetDC
        x as long,_         'the x coordinate to draw the pixel
        y as long,_         'the y coordinate to draw the pixel
        rgbColor as long,_
        SetPixel as long
    End Function
 
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls