Browse Source

Added textures examples.

Brucey 5 years ago
parent
commit
fa32aa9c28

+ 78 - 0
examples/textures/textures_background_scrolling.bmx

@@ -0,0 +1,78 @@
+SuperStrict
+
+Framework Ray.Lib
+
+' Initialization
+'--------------------------------------------------------------------------------------
+Const screenWidth:Int = 800
+Const screenHeight:Int = 450
+
+InitWindow(screenWidth, screenHeight, "raylib [textures] example - background scrolling")
+
+' NOTE: Be careful, background width must be equal or bigger than screen width
+' if not, texture should be draw more than two times for scrolling effect
+Local background:RTexture2D = LoadTexture("../../lib.mod/raylib/examples/textures/resources/cyberpunk_street_background.png")
+Local midground:RTexture2D = LoadTexture("../../lib.mod/raylib/examples/textures/resources/cyberpunk_street_midground.png")
+Local foreground:RTexture2D = LoadTexture("../../lib.mod/raylib/examples/textures/resources/cyberpunk_street_foreground.png")
+
+Local scrollingBack:Float = 0.0
+Local scrollingMid:Float = 0.0
+Local scrollingFore:Float = 0.0
+
+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
+	'----------------------------------------------------------------------------------
+	scrollingBack :- 0.1
+	scrollingMid :- 0.5
+	scrollingFore :- 1.0
+
+	' NOTE: Texture is scaled twice its size, so it sould be considered on scrolling
+	If scrollingBack <= -background.width*2 Then
+		scrollingBack = 0
+	End If
+	If scrollingMid <= -midground.width*2 Then
+		scrollingMid = 0
+	End If
+	If scrollingFore <= -foreground.width*2 Then
+		scrollingFore = 0
+	End If
+	'----------------------------------------------------------------------------------
+
+	' Draw
+	'----------------------------------------------------------------------------------
+	BeginDrawing()
+
+		ClearBackground(GetColor($052c46ff))
+
+		' Draw background image twice
+		' NOTE: Texture is scaled twice its size
+		DrawTextureEx(background, New RVector2(scrollingBack, 20), 0.0, 2.0, WHITE)
+		DrawTextureEx(background, New RVector2(background.width*2 + scrollingBack, 20), 0.0, 2.0, WHITE)
+
+		' Draw midground image twice
+		DrawTextureEx(midground, New RVector2(scrollingMid, 20), 0.0, 2.0, WHITE)
+		DrawTextureEx(midground, New RVector2(midground.width*2 + scrollingMid, 20), 0.0, 2.0, WHITE)
+
+		' Draw foreground image twice
+		DrawTextureEx(foreground, New RVector2(scrollingFore, 70), 0.0, 2.0, WHITE)
+		DrawTextureEx(foreground, New RVector2(foreground.width*2 + scrollingFore, 70), 0.0, 2.0, WHITE)
+
+		DrawText("BACKGROUND SCROLLING & PARALLAX", 10, 10, 20, RED)
+		DrawText("(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)", screenWidth - 330, screenHeight - 20, 10, RAYWHITE)
+
+	EndDrawing()
+	'----------------------------------------------------------------------------------
+Wend
+
+' De-Initialization
+'--------------------------------------------------------------------------------------
+UnloadTexture(background)  ' Unload background texture
+UnloadTexture(midground)   ' Unload midground texture
+UnloadTexture(foreground)  ' Unload foreground texture
+
+CloseWindow()              ' Close window and OpenGL context
+'--------------------------------------------------------------------------------------

+ 96 - 0
examples/textures/textures_bunnymark.bmx

@@ -0,0 +1,96 @@
+SuperStrict
+
+Framework Ray.Lib
+
+
+Const MAX_BUNNIES:Int = 50000    ' 50K bunnies limit
+
+' This is the maximum amount of elements (quads) per batch
+' NOTE: This value is defined in [rlgl] module and can be changed there
+Const MAX_BATCH_ELEMENTS:Int = 8192
+
+' Initialization
+'--------------------------------------------------------------------------------------
+Const screenWidth:Int = 800
+Const screenHeight:Int = 450
+
+InitWindow(screenWidth, screenHeight, "raylib [textures] example - bunnymark")
+
+' Load bunny texture
+Local texBunny:RTexture2D = LoadTexture("../../lib.mod/raylib/examples/textures/resources/wabbit_alpha.png")
+
+Local bunnies:SBunny[MAX_BUNNIES]    ' Bunnies array
+Local bunniesCount:Int = 0           ' Bunnies counter
+
+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
+	'----------------------------------------------------------------------------------
+	If IsMouseButtonDown(MOUSE_LEFT_BUTTON) Then
+		' Create more bunnies
+		For Local i:Int = 0 Until 100
+			If bunniesCount < MAX_BUNNIES Then
+				bunnies[bunniesCount].position = GetMousePosition()
+				bunnies[bunniesCount].speed.x = GetRandomValue(-250, 250)/60.0
+				bunnies[bunniesCount].speed.y = GetRandomValue(-250, 250)/60.0
+				bunnies[bunniesCount].color = New RColor(GetRandomValue(50, 240), GetRandomValue(80, 240), GetRandomValue(100, 240), 255)
+				bunniesCount :+ 1
+			End If
+		Next
+	End If
+
+	' Update bunnies
+	For Local i:Int = 0 Until bunniesCount
+		bunnies[i].position.x :+ bunnies[i].speed.x
+		bunnies[i].position.y :+ bunnies[i].speed.y
+
+		If ((bunnies[i].position.x + texBunny.width/2) > GetScreenWidth()) Or ((bunnies[i].position.x + texBunny.width/2) < 0) Then
+			bunnies[i].speed.x :* -1
+		End If
+		If ((bunnies[i].position.y + texBunny.height/2) > GetScreenHeight()) Or ((bunnies[i].position.y + texBunny.height/2 - 40) < 0) Then
+			bunnies[i].speed.y :* -1
+		End If
+	Next
+	'----------------------------------------------------------------------------------
+
+	' Draw
+	'----------------------------------------------------------------------------------
+	BeginDrawing()
+
+		ClearBackground(RAYWHITE)
+
+		For Local i:Int = 0 Until bunniesCount
+			' NOTE: When internal batch buffer limit is reached (MAX_BATCH_ELEMENTS),
+			' a draw call is launched and buffer starts being filled again
+			' before issuing a draw call, updated vertex data from internal CPU buffer is send to GPU...
+			' Process of sending data is costly and it could happen that GPU data has not been completely
+			' processed for drawing while new data is tried to be sent (updating current in-use buffers)
+			' it could generates a stall and consequently a frame drop, limiting the number of drawn bunnies
+			DrawTexture(texBunny, Int(bunnies[i].position.x), Int(bunnies[i].position.y), bunnies[i].color)
+		Next
+
+		DrawRectangle(0, 0, screenWidth, 40, BLACK)
+		DrawText("bunnies: " + bunniesCount, 120, 10, 20, GREEN)
+		DrawText("batched draw calls: " + (1 + bunniesCount/MAX_BATCH_ELEMENTS), 320, 10, 20, MAROON)
+
+		DrawFPS(10, 10)
+
+	EndDrawing()
+	'----------------------------------------------------------------------------------
+Wend
+
+' De-Initialization
+'--------------------------------------------------------------------------------------
+UnloadTexture(texBunny)    ' Unload bunny texture
+
+CloseWindow()              ' Close window and OpenGL context
+'--------------------------------------------------------------------------------------
+
+Struct SBunny
+	Field position:RVector2
+	Field speed:RVector2
+	Field color:RColor
+End Struct

+ 69 - 0
examples/textures/textures_image_drawing.bmx

@@ -0,0 +1,69 @@
+SuperStrict
+
+Framework Ray.Lib
+
+' Initialization
+'--------------------------------------------------------------------------------------
+Const screenWidth:Int = 800
+Const screenHeight:Int = 450
+
+InitWindow(screenWidth, screenHeight, "raylib [textures] example - image drawing")
+
+' NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
+
+Local cat:RImage = LoadImage("../../lib.mod/raylib/examples/textures/resources/cat.png")             ' Load image in CPU memory (RAM)
+ImageCrop(cat, New RRectangle(100, 10, 280, 380))      ' Crop an image piece
+ImageFlipHorizontal(cat)                              ' Flip cropped image horizontally
+ImageResize(cat, 150, 200)                            ' Resize flipped-cropped image
+
+Local parrots:RImage = LoadImage("../../lib.mod/raylib/examples/textures/resources/parrots.png")     ' Load image in CPU memory (RAM)
+
+' Draw one image over the other with a scaling of 1.5f
+ImageDraw(parrots, cat, New RRectangle(0, 0, cat.width, cat.height), New RRectangle(30, 40, cat.width*1.5, cat.height*1.5), WHITE)
+ImageCrop(parrots, New RRectangle(0, 50, parrots.width, parrots.height - 100)) ' Crop resulting image
+
+UnloadImage(cat)       ' Unload image from RAM
+
+' Load custom font for frawing on image
+Local font:RFont = LoadFont("../../lib.mod/raylib/examples/textures/resources/custom_jupiter_crash.png")
+
+' Draw over image using custom font
+ImageDrawTextEx(parrots, New RVector2(300, 230), font, "PARROTS & CAT", font.baseSize, -2, WHITE)
+
+UnloadFont(font) ' Unload custom spritefont (already drawn used on image)
+
+Local texture:RTexture2D = LoadTextureFromImage(parrots)      ' Image converted to texture, uploaded to GPU memory (VRAM)
+UnloadImage(parrots)   ' Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
+
+SetTargetFPS(60)
+'---------------------------------------------------------------------------------------
+
+' 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 - 40, WHITE)
+		DrawRectangleLines(screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2 - 40, texture.width, texture.height, DARKGRAY)
+
+		DrawText("We are drawing only one texture from various images composed!", 240, 350, 10, DARKGRAY)
+		DrawText("Source images have been cropped, scaled, flipped and copied one over the other.", 190, 370, 10, DARKGRAY)
+
+	EndDrawing()
+	'----------------------------------------------------------------------------------
+Wend
+
+' De-Initialization
+'--------------------------------------------------------------------------------------
+UnloadTexture(texture)       ' Texture unloading
+
+CloseWindow()                ' Close window and OpenGL context
+'--------------------------------------------------------------------------------------