Kaynağa Gözat

Added more examples.

Brucey 5 yıl önce
ebeveyn
işleme
c2089e2639

+ 90 - 0
examples/core/core_3d_picking.bmx

@@ -0,0 +1,90 @@
+SuperStrict
+
+Framework Ray.Lib
+
+' Initialization
+'--------------------------------------------------------------------------------------
+Const screenWidth:Int = 800
+Const screenHeight:Int = 450
+
+InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d picking")
+
+' Define the camera to look into our 3d world
+Local camera:RCamera
+camera.position = New RVector3(10.0, 10.0, 10.0) ' Camera position
+camera.target = New RVector3(0.0, 0.0, 0.0)      ' Camera looking at point
+camera.up = New RVector3(0.0, 1.0, 0.0)          ' Camera up vector (rotation towards target)
+camera.fovy = 45.0                                ' Camera field-of-view Y
+camera.cameraType = CAMERA_PERSPECTIVE                   ' Camera mode type
+
+Local cubePosition:RVector3 = New RVector3(0.0, 1.0, 0.0)
+Local cubeSize:RVector3 = New RVector3(2.0, 2.0, 2.0)
+
+Local ray:RRay                    ' Picking line ray
+
+Local collision:Int = False
+
+SetCameraMode(camera, CAMERA_FREE) ' Set a free camera mode
+
+SetTargetFPS(60)                   ' Set our game to run at 60 frames-per-second
+'--------------------------------------------------------------------------------------
+
+' Main game loop
+While Not WindowShouldClose()        ' Detect window close button or ESC key
+	' Update
+	'----------------------------------------------------------------------------------
+	UpdateCamera(camera)          ' Update camera
+
+	If IsMouseButtonPressed(MOUSE_LEFT_BUTTON) Then
+		If Not collision Then
+			ray = GetMouseRay(GetMousePosition(), camera)
+
+			' Check collision between ray and box
+			collision = CheckCollisionRayBox(ray, ..
+						New RBoundingBox(New RVector3(cubePosition.x - cubeSize.x/2, cubePosition.y - cubeSize.y/2, cubePosition.z - cubeSize.z/2), ..
+									     New RVector3(cubePosition.x + cubeSize.x/2, cubePosition.y + cubeSize.y/2, cubePosition.z + cubeSize.z/2)))
+		Else
+			collision = False
+		End If
+	End If
+	'----------------------------------------------------------------------------------
+
+	' Draw
+	'----------------------------------------------------------------------------------
+	BeginDrawing()
+
+		ClearBackground(RAYWHITE)
+
+		BeginMode3D(camera)
+
+			If collision Then
+				DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, RED)
+				DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, MAROON)
+
+				DrawCubeWires(cubePosition, cubeSize.x + 0.2, cubeSize.y + 0.2, cubeSize.z + 0.2, GREEN)
+			Else
+				DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, GRAY)
+				DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, DARKGRAY)
+			End If
+
+			DrawRay(ray, MAROON)
+			DrawGrid(10, 1.0)
+
+		EndMode3D()
+
+		DrawText("Try selecting the box with mouse!", 240, 10, 20, DARKGRAY)
+
+		If collision Then
+			DrawText("BOX SELECTED", (screenWidth - MeasureText("BOX SELECTED", 30)) / 2, screenHeight * 0.1, 30, GREEN)
+		End If
+
+		DrawFPS(10, 10)
+
+	EndDrawing()
+	'----------------------------------------------------------------------------------
+Wend
+
+' De-Initialization
+'--------------------------------------------------------------------------------------
+CloseWindow()        ' Close window and OpenGL context
+'--------------------------------------------------------------------------------------

+ 49 - 0
examples/core/core_random_values.bmx

@@ -0,0 +1,49 @@
+SuperStrict
+
+Framework Ray.Lib
+
+' Initialization
+'--------------------------------------------------------------------------------------
+Const screenWidth:Int = 800
+Const screenHeight:Int = 450
+
+InitWindow(screenWidth, screenHeight, "raylib [core] example - generate random values")
+
+Local framesCounter:Int = 0          ' Variable used to count frames
+
+Local randValue:Int = GetRandomValue(-8, 5)   ' Get a random integer number between -8 and 5 (both included)
+
+SetTargetFPS(60)               ' Set our game to run at 60 frames-per-second
+'--------------------------------------------------------------------------------------
+
+' Main game loop
+While Not WindowShouldClose()    ' Detect window close button or ESC key
+	' Update
+	'----------------------------------------------------------------------------------
+	framesCounter :+ 1
+
+	' Every two seconds (120 frames) a new random value is generated
+	If ((framesCounter/120) Mod 2) = 1 Then
+		randValue = GetRandomValue(-8, 5)
+		framesCounter = 0
+	End If
+	'----------------------------------------------------------------------------------
+
+	' Draw
+	'----------------------------------------------------------------------------------
+	BeginDrawing()
+
+		ClearBackground(RAYWHITE)
+
+		DrawText("Every 2 seconds a new random value is generated:", 130, 100, 20, MAROON)
+
+		DrawText(randValue, 360, 180, 80, LIGHTGRAY)
+
+	EndDrawing()
+	'----------------------------------------------------------------------------------
+Wend
+
+' De-Initialization
+'--------------------------------------------------------------------------------------
+CloseWindow()        ' Close window and OpenGL context
+'--------------------------------------------------------------------------------------

+ 63 - 0
examples/core/core_world_screen.bmx

@@ -0,0 +1,63 @@
+SuperStrict
+
+Framework Ray.Lib
+
+' Initialization
+'--------------------------------------------------------------------------------------
+Const screenWidth:Int = 800
+Const screenHeight:Int = 450
+
+InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera free")
+
+' Define the camera to look into our 3d world
+Local camera:RCamera
+camera.position = New RVector3(10.0, 10.0, 10.0)
+camera.target = New RVector3(0.0, 0.0, 0.0)
+camera.up = New RVector3(0.0, 1.0, 0.0)
+camera.fovy = 45.0
+camera.cameraType = CAMERA_PERSPECTIVE
+
+Local cubePosition:RVector3 = New RVector3(0.0, 0.0, 0.0)
+Local cubeScreenPosition:RVector2 = New RVector2(0.0, 0.0)
+
+SetCameraMode(camera, CAMERA_FREE) ' Set a free camera mode
+
+SetTargetFPS(60)                   ' Set our game to run at 60 frames-per-second
+'--------------------------------------------------------------------------------------
+
+' Main game loop
+While Not WindowShouldClose()        ' Detect window close button or ESC key
+	' Update
+	'----------------------------------------------------------------------------------
+	UpdateCamera(camera)          ' Update camera
+
+	' Calculate cube screen space position (with a little offset to be in top)
+	cubeScreenPosition = GetWorldToScreen(New RVector3(cubePosition.x, cubePosition.y + 2.5, cubePosition.z), camera)
+	'----------------------------------------------------------------------------------
+
+	' Draw
+	'----------------------------------------------------------------------------------
+	BeginDrawing()
+
+		ClearBackground(RAYWHITE)
+
+		BeginMode3D(camera)
+
+			DrawCube(cubePosition, 2.0, 2.0, 2.0, RED)
+			DrawCubeWires(cubePosition, 2.0, 2.0, 2.0, MAROON)
+
+			DrawGrid(10, 1.0)
+
+		EndMode3D()
+
+		DrawText("Enemy: 100 / 100", Int(cubeScreenPosition.x - MeasureText("Enemy: 100/100", 20)/2), Int(cubeScreenPosition.y), 20, BLACK)
+		DrawText("Text is always on top of the cube", (screenWidth - MeasureText("Text is always on top of the cube", 20))/2, 25, 20, GRAY)
+
+	EndDrawing()
+	'----------------------------------------------------------------------------------
+Wend
+
+' De-Initialization
+'--------------------------------------------------------------------------------------
+CloseWindow()        ' Close window and OpenGL context
+'--------------------------------------------------------------------------------------

+ 67 - 0
examples/textures/textures_srcrec_dstrec.bmx

@@ -0,0 +1,67 @@
+SuperStrict
+
+Framework Ray.Lib
+
+' Initialization
+'--------------------------------------------------------------------------------------
+Const screenWidth:Int = 800
+Const screenHeight:Int = 450
+
+InitWindow(screenWidth, screenHeight, "raylib [textures] examples - texture source and destination rectangles")
+
+' NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
+
+Local scarfy:RTexture2D = LoadTexture("../../lib.mod/raylib/examples/textures/resources/scarfy.png")        ' Texture loading
+
+Local frameWidth:Int = scarfy.width/6
+Local frameHeight:Int = scarfy.height
+
+' Source rectangle (part of the texture to use for drawing)
+Local sourceRec:RRectangle = New RRectangle(0.0, 0.0, frameWidth, frameHeight)
+
+' Destination rectangle (screen rectangle where drawing part of texture)
+Local destRec:RRectangle = New RRectangle(screenWidth/2, screenHeight/2, frameWidth*2, frameHeight*2)
+
+' Origin of the texture (rotation/scale point), it's relative to destination rectangle size
+Local origin:RVector2 = New RVector2(frameWidth, frameHeight)
+
+Local Rotation:Int = 0
+
+SetTargetFPS(60)
+'--------------------------------------------------------------------------------------
+
+' Main game loop
+While Not WindowShouldClose()    ' Detect window close button or ESC key
+	' Update
+	'----------------------------------------------------------------------------------
+	Rotation :+ 1
+	'----------------------------------------------------------------------------------
+
+	' Draw
+	'----------------------------------------------------------------------------------
+	BeginDrawing()
+
+		ClearBackground(RAYWHITE)
+
+		' NOTE: Using DrawTexturePro() we can easily rotate and scale the part of the texture we draw
+		' sourceRec defines the part of the texture we use for drawing
+		' destRec defines the rectangle where our texture part will fit (scaling it to fit)
+		' origin defines the point of the texture used as reference for rotation and scaling
+		' rotation defines the texture rotation (using origin as rotation point)
+		DrawTexturePro(scarfy, sourceRec, destRec, origin, Float(Rotation), WHITE)
+
+		DrawLine(Int(destRec.x), 0, Int(destRec.x), screenHeight, GRAY)
+		DrawLine(0, Int(destRec.y), screenWidth, Int(destRec.y), GRAY)
+
+		DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, GRAY)
+
+	EndDrawing()
+	'----------------------------------------------------------------------------------
+Wend
+
+' De-Initialization
+'--------------------------------------------------------------------------------------
+UnloadTexture(scarfy)        ' Texture unloading
+
+CloseWindow()                ' Close window and OpenGL context
+'--------------------------------------------------------------------------------------

+ 51 - 0
examples/textures/textures_to_image.bmx

@@ -0,0 +1,51 @@
+SuperStrict
+
+Framework Ray.Lib
+
+' Initialization
+'--------------------------------------------------------------------------------------
+Const screenWidth:Int = 800
+Const screenHeight:Int = 450
+
+InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture to image")
+
+' NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
+
+Local image:RImage = LoadImage("../../lib.mod/raylib/examples/textures/resources/raylib_logo.png")  ' Load image data into CPU memory (RAM)
+Local texture:RTexture2D = LoadTextureFromImage(image)       ' Image converted to texture, GPU memory (RAM -> VRAM)
+UnloadImage(image)                                    ' Unload image data from CPU memory (RAM)
+
+image = GetTextureData(texture)                       ' Retrieve image data from GPU memory (VRAM -> RAM)
+UnloadTexture(texture)                                ' Unload texture from GPU memory (VRAM)
+
+texture = LoadTextureFromImage(image)                 ' Recreate texture from retrieved image data (RAM -> VRAM)
+UnloadImage(image)                                    ' Unload retrieved image data from CPU memory (RAM)
+'---------------------------------------------------------------------------------------
+
+' Main game loop
+While Not WindowShouldClose()    ' Detect window close button or ESC key
+	' Update
+	'----------------------------------------------------------------------------------
+	' TODO: Update your variables here
+	'----------------------------------------------------------------------------------
+
+	' Draw
+	'----------------------------------------------------------------------------------
+	BeginDrawing()
+
+		ClearBackground(RAYWHITE)
+
+		DrawTexture(texture, screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2, WHITE)
+
+		DrawText("this IS a texture loaded from an image!", 300, 370, 10, GRAY)
+
+	EndDrawing()
+	'----------------------------------------------------------------------------------
+Wend
+
+' De-Initialization
+'--------------------------------------------------------------------------------------
+UnloadTexture(texture)       ' Texture unloading
+
+CloseWindow()                ' Close window and OpenGL context
+'--------------------------------------------------------------------------------------