Advertisement
Dr_Davenstein

wipeoutfinalbeforerepocreation

May 19th, 2022
3,230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Include once "inc/raylib.bi"
  2. #include once "inc/chipmunk/chipmunk.bi"
  3. #define max( a, b ) iif( ( a ) > ( b ), ( a ), ( b ) )
  4. #define min( a, b ) iif( ( a ) < ( b ), ( a ), ( b ) )
  5. const player_speed=6.0f
  6.  
  7. '' NOTE: Required for virtual mouse, to clamp inside virtual game size
  8. function ClampValue( value as Vector2, min_ as Vector2, max_ as Vector2 ) as Vector2
  9.     dim as Vector2 result = value
  10.  
  11.     with result
  12.         .x = iif( .x > max_.x, max_.x, .x )
  13.         .x = iif( .x < min_.x, min_.x, .x )
  14.         .y = iif( .y > max_.y, max_.y, .y )
  15.         .y = iif( .y < min_.y, min_.y, .y )
  16.     end with
  17.  
  18.     return( result )
  19. end function
  20.  
  21. const as long _
  22. windowWidth = 1280, windowHeight = 720
  23.  
  24. SetConfigFlags( FLAG_WINDOW_RESIZABLE or FLAG_VSYNC_HINT )
  25. InitWindow( windowWidth, windowHeight, "raylib [core] example - window scale letterbox" )
  26. SetWindowMinSize( 320, 240 )
  27.  
  28.  
  29. dim as long _
  30. gameScreenWidth = 640, gameScreenHeight = 480
  31. dim as RenderTexture2D target = LoadRenderTexture( gameScreenWidth, gameScreenHeight )
  32. SetTextureFilter( target.texture, FILTER_POINT ) '' Texture scale filter to use
  33.  
  34.  
  35.  
  36. var _
  37. position => Vector2( 20.0f, 40.0f ), _
  38. frameRec => Rectangle( 0.0f, 0.0f, 13.0f, 27.0f )
  39.  
  40. dim  as Texture2D _
  41. ship => LoadTexture( "images/ship-0001.png" )
  42. SetTextureFilter(ship, FILTER_POINT)
  43.  
  44. SetTargetFPS( 60 )
  45.  
  46. type ship2
  47.     as vector2 position
  48.     as vector2 speed
  49.     as double acceleration
  50.     as double rotation
  51.     as vector2 drift
  52. end type
  53. dim players as ship2
  54.  
  55. players.position.x=150
  56. players.position.y=150
  57.  
  58. dim as Font fontTtf = LoadFontEx( "resources/fonts/PIXEAB.ttf", 11, 0, 250 )
  59. SetTextureFilter(fontTtf.texture, FILTER_POINT)
  60.  
  61. var sourceRec = Rectangle( 0.0f, 0.0f, 28, 16 )
  62. var destRec = Rectangle( 150, 150, 28, 15 )
  63. var destsRec = Rectangle( 50, 50, 30, 17 )
  64. var origin = Vector2( 14, 7.5 )
  65.  
  66.  
  67. dim as cpSpace ptr cpWorld = cpSpaceNew()
  68. cpSpaceSetDamping( cpWorld, .9)
  69.  
  70. dim as double frameStep = 180, simRate = 15
  71. dim as double frequency = 1.0/frameStep
  72. dim as double accumStep, deltaTime, curTime = timer
  73.  
  74.  
  75.  
  76. 'now we'll actually create a shape using vertices and attach it to the rigidBody container we just created
  77. dim as cpVect polyVerts(2)
  78.  
  79. polyVerts(0).x = -12
  80. polyVerts(0).y = -6
  81.  
  82. polyVerts(1).x = 10
  83. polyVerts(1).y = 0
  84.  
  85. polyVerts(2).x = -12
  86. polyVerts(2).y = 6
  87.  
  88. 'simple identity matrix
  89. dim as cpTransform shipTrans = type(1,0, 0,1, 0,0)
  90.  
  91. dim as cpFloat moi = cpMomentForPoly( 25, 3, @polyVerts(0), cpv(0,0), 1)
  92. dim as cpBody ptr shipBody = cpBodyNew( 25, moi )
  93. cpSpaceAddBody( cpWorld, shipBody)
  94.  
  95. dim as cpShape ptr shipShape = cpPolyShapeNew( shipBody, 3, @polyVerts(0), shipTrans, 1 )
  96. cpShapeSetElasticity(shipShape, .75)
  97. cpShapeSetFriction(shipShape, 1)
  98. cpSpaceAddShape( cpWorld, shipShape)
  99. cpBodySetPosition( shipBody, cpv(320, 250) )
  100.  
  101.  
  102. dim as cpVect boxVerts(4)
  103. boxVerts(0) = cpv(0,0)
  104. boxVerts(1) = cpv(0,420)
  105. boxVerts(2) = cpv(320,479)
  106. boxVerts(3) = cpv(639,420)
  107. boxVerts(4) = cpv(639,0)
  108.  
  109. 'since this shape will only be used for static collisions, we don't need to create a rigid body
  110. 'instead, we'll just grab a reference to the built-in static body and attach to that
  111. dim as cpShape ptr lineShape(ubound(boxVerts))
  112.  
  113. for l as integer = 0 to ubound(boxverts)
  114.  
  115.     dim as integer lAnd1 = (l+1) mod (ubound(boxVerts)+1)
  116.  
  117.     lineShape(l) = cpSegmentShapeNew( cpSpaceGetStaticBody(cpWorld), boxVerts(l), boxVerts(lAnd1), 1 )
  118.     cpShapeSetElasticity( lineShape(l), .95 )
  119.     cpShapeSetFriction( lineShape(l), .25 )
  120.     cpSpaceAddShape( cpWorld, lineShape(l) )
  121.  
  122. next
  123.  
  124.  
  125.  
  126.  
  127. do while( not WindowShouldClose() )
  128.  
  129.     dim as single scale = min( GetScreenWidth() / gameScreenWidth, GetScreenHeight() / gameScreenHeight )
  130.  
  131.  
  132.     '' Update virtual mouse (clamped mouse value behind game screen)
  133.     dim as Vector2 _
  134.     mouse = GetMousePosition(), _
  135.     virtualMouse
  136.  
  137.     virtualMouse.x = ( mouse.x - ( GetScreenWidth() - ( gameScreenWidth * scale ) ) * 0.5f ) / scale
  138.     virtualMouse.y = ( mouse.y - ( GetScreenHeight() - ( gameScreenHeight * scale ) ) * 0.5f ) / scale
  139.     virtualMouse = ClampValue( virtualMouse, Vector2( 0, 0 ), Vector2( gameScreenWidth, gameScreenHeight ) )
  140.  
  141.     SetMouseOffset(-(GetScreenWidth() - (gameScreenWidth*scale))*0.5f, -(GetScreenHeight() - (gameScreenHeight*scale))*0.5f)
  142.     SetMouseScale(1/scale, 1/scale)
  143.  
  144.  
  145.     'get the frame timing and run the simulation loop
  146.     deltaTime = timer - curTime
  147.     curTime = timer
  148.     accumStep += deltaTime
  149.  
  150.     dim as cpFloat eMass = cpBodyGetMass( shipBody )
  151.  
  152.  
  153.     if IsKeyDown( KEY_LEFT ) then
  154.  
  155.         cpBodyApplyForceAtLocalPoint( shipBody, cpv(eMass *200*deltaTime, 0), cpv(0, 1) )
  156.         cpBodyApplyForceAtLocalPoint( shipBody, cpv(-eMass*200*deltaTime, 0), cpv(0,-1) )
  157.  
  158.     end if
  159.  
  160.  
  161.     if IsKeyDown( KEY_RIGHT ) then
  162.  
  163.         cpBodyApplyForceAtLocalPoint( shipBody, cpv(-eMass*200*deltaTime, 0), cpv(0, 1) )
  164.         cpBodyApplyForceAtLocalPoint( shipBody, cpv( eMass*200*deltaTime, 0), cpv(0,-1) )
  165.  
  166.     end if
  167.  
  168.  
  169.     if IsKeyDown( KEY_UP ) then
  170.  
  171.         cpBodyApplyForceAtLocalPoint( shipBody, cpv(eMass*500*deltaTime, 0), cpv(-14,0) )
  172.  
  173.     end if
  174.    
  175.  
  176.     if IsKeyDown( KEY_DOWN ) then
  177.  
  178.         cpBodyApplyForceAtLocalPoint( shipBody, cpv(-eMass*500*deltaTime, 0), cpv(10,0) )
  179.  
  180.     end if
  181.    
  182.    
  183.     if IsKeyDown( KEY_SPACE ) then
  184.  
  185.         cpBodySetPosition( shipBody, cpv(320, 250) )
  186.  
  187.     end if
  188.  
  189.  
  190.     do
  191.  
  192.         cpSpaceStep( cpWorld, frequency*simRate )
  193.        
  194.         'if there are any additional forces, we'll add them in this block (for instance radial gravity)
  195.        
  196.         accumStep -= frequency
  197.  
  198.     loop while accumStep >= frequency
  199.  
  200.  
  201.  
  202.     BeginDrawing()
  203.     ClearBackground( BLACK )
  204.  
  205.     BeginTextureMode( target )
  206.     ClearBackground( raywhite )
  207.  
  208.     dim as cpVect position = cpBodyGetPosition(shipBody)
  209.  
  210.     destrec.x = position.x
  211.     destrec.y = position.y
  212.     destsrec.x = position.x
  213.     destsrec.y = position.y+10
  214.     players.rotation = cpBodyGetAngle(shipBody)*rad2Deg
  215.  
  216.     DrawTextEx( fontTtf, str(players.acceleration ), Vector2( 10.0f, 10.0f ), fontTtf.baseSize, 2, MAROON )
  217.     DrawTexturePro( ship, sourceRec, destsRec, origin, players.rotation, black )
  218.     DrawTexturePro( ship, sourceRec, destRec, origin, players.rotation, WHITE )
  219.     for l as integer = 0 to ubound(boxVerts)
  220.  
  221.             dim as integer lAnd1 = (l+1) mod (ubound(boxVerts)+1)
  222.  
  223.             dim as cpVect p1 = boxVerts(l)
  224.             dim as cpVect p2 = boxVerts(lAnd1)
  225.            
  226.             DrawLine(  p1.x, p1.y, p2.x, p2.y, BLACK )
  227.            
  228.         Next
  229.     EndTextureMode()
  230.    
  231.     DrawTexturePro(_
  232.     target.texture, Rectangle( 0.0f, 0.0f, target.texture.width, -target.texture.height ), _
  233.     Rectangle( ( GetScreenWidth() - ( gameScreenWidth * scale ) ) * 0.5, ( GetScreenHeight() - ( gameScreenHeight * scale ) ) * 0.5, _
  234.     gameScreenWidth * scale, gameScreenHeight * scale ), Vector2( 0, 0 ), 0.0f, WHITE )
  235.    
  236.     EndDrawing()
  237.  
  238. loop
  239.  
  240. '' De-Initialization
  241. cpSpaceFree(cpWorld)
  242. UnloadRenderTexture( target )
  243. UnloadTexture( ship )
  244. CloseWindow()
  245.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement