Browse Source

Added textures examples.

Brucey 5 years ago
parent
commit
b9c40a3bbe

+ 97 - 0
examples/textures/textures_image_generation.bmx

@@ -0,0 +1,97 @@
+SuperStrict
+
+Framework Ray.Lib
+
+Const NUM_TEXTURES:Int = 7      ' Currently we have 7 generation algorithms
+
+' Initialization
+'--------------------------------------------------------------------------------------
+Const screenWidth:Int = 800
+Const screenHeight:Int = 450
+
+InitWindow(screenWidth, screenHeight, "raylib [textures] example - procedural images generation")
+
+Local verticalGradient:RImage = GenImageGradientV(screenWidth, screenHeight, RED, BLUE)
+Local horizontalGradient:RImage = GenImageGradientH(screenWidth, screenHeight, RED, BLUE)
+Local radialGradient:RImage = GenImageGradientRadial(screenWidth, screenHeight, 0.0, WHITE, BLACK)
+Local checked:RImage = GenImageChecked(screenWidth, screenHeight, 32, 32, RED, BLUE)
+Local whiteNoise:RImage = GenImageWhiteNoise(screenWidth, screenHeight, 0.5)
+Local perlinNoise:RImage = GenImagePerlinNoise(screenWidth, screenHeight, 50, 50, 4.0)
+Local cellular:RImage = GenImageCellular(screenWidth, screenHeight, 32)
+
+Local textures:RTexture2D[NUM_TEXTURES]
+
+textures[0] = LoadTextureFromImage(verticalGradient)
+textures[1] = LoadTextureFromImage(horizontalGradient)
+textures[2] = LoadTextureFromImage(radialGradient)
+textures[3] = LoadTextureFromImage(checked)
+textures[4] = LoadTextureFromImage(whiteNoise)
+textures[5] = LoadTextureFromImage(perlinNoise)
+textures[6] = LoadTextureFromImage(cellular)
+
+' Unload image data (CPU RAM)
+UnloadImage(verticalGradient)
+UnloadImage(horizontalGradient)
+UnloadImage(radialGradient)
+UnloadImage(checked)
+UnloadImage(whiteNoise)
+UnloadImage(perlinNoise)
+UnloadImage(cellular)
+
+Local currentTexture:Int = 0
+
+SetTargetFPS(60)
+'---------------------------------------------------------------------------------------
+
+' Main game loop
+While Not WindowShouldClose()
+	' Update
+	'----------------------------------------------------------------------------------
+	If IsMouseButtonPressed(MOUSE_LEFT_BUTTON) Or IsKeyPressed(KEY_RIGHT) Then
+		currentTexture = (currentTexture + 1) Mod NUM_TEXTURES ' Cycle between the textures
+	End If
+	'----------------------------------------------------------------------------------
+
+	' Draw
+	'----------------------------------------------------------------------------------
+	BeginDrawing()
+
+		ClearBackground(RAYWHITE)
+
+		DrawTexture(textures[currentTexture], 0, 0, WHITE)
+
+		DrawRectangle(30, 400, 325, 30, Fade(SKYBLUE, 0.5))
+		DrawRectangleLines(30, 400, 325, 30, Fade(WHITE, 0.5))
+		DrawText("MOUSE LEFT BUTTON to CYCLE PROCEDURAL TEXTURES", 40, 410, 10, WHITE)
+
+		Select currentTexture
+			Case 0
+				DrawText("VERTICAL GRADIENT", 560, 10, 20, RAYWHITE)
+			Case 1
+				DrawText("HORIZONTAL GRADIENT", 540, 10, 20, RAYWHITE)
+			Case 2
+				DrawText("RADIAL GRADIENT", 580, 10, 20, LIGHTGRAY)
+			Case 3
+				DrawText("CHECKED", 680, 10, 20, RAYWHITE)
+			Case 4
+				DrawText("WHITE NOISE", 640, 10, 20, RED)
+			Case 5
+				DrawText("PERLIN NOISE", 630, 10, 20, RAYWHITE)
+			Case 6
+				DrawText("CELLULAR", 670, 10, 20, RAYWHITE)
+		End Select
+
+	EndDrawing()
+	'----------------------------------------------------------------------------------
+Wend
+
+' De-Initialization
+'--------------------------------------------------------------------------------------
+
+' Unload textures data (GPU VRAM)
+For Local i:Int = 0 Until NUM_TEXTURES
+	UnloadTexture(textures[i])
+Next
+
+CloseWindow()                ' Close window and OpenGL context
+'--------------------------------------------------------------------------------------

+ 46 - 0
examples/textures/textures_image_loading.bmx

@@ -0,0 +1,46 @@
+SuperStrict
+
+Framework Ray.Lib
+
+' Initialization
+'--------------------------------------------------------------------------------------
+Const screenWidth:Int = 800
+Const screenHeight:Int = 450
+
+InitWindow(screenWidth, screenHeight, "raylib [textures] example - image loading")
+
+' 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")     ' Loaded in CPU memory (RAM)
+Local texture:RTexture2D = LoadTextureFromImage(image)          ' Image converted to texture, GPU memory (VRAM)
+
+UnloadImage(image)   ' Once image has been converted to texture and uploaded to VRAM, it can be unloaded from 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
+'--------------------------------------------------------------------------------------

+ 139 - 0
examples/textures/textures_image_processing.bmx

@@ -0,0 +1,139 @@
+SuperStrict
+
+Framework Ray.Lib
+
+Const NUM_PROCESSES:Int = 8
+
+Local processText:String[] = [ ..
+    "NO PROCESSING", ..
+    "COLOR GRAYSCALE", ..
+    "COLOR TINT", ..
+    "COLOR INVERT", ..
+    "COLOR CONTRAST", ..
+    "COLOR BRIGHTNESS", ..
+    "FLIP VERTICAL", ..
+    "FLIP HORIZONTAL"]
+
+' Initialization
+'--------------------------------------------------------------------------------------
+Const screenWidth:Int = 800
+Const screenHeight:Int = 450
+
+InitWindow(screenWidth, screenHeight, "raylib [textures] example - image processing")
+
+' NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
+
+Local image:RImage = LoadImage("../../lib.mod/raylib/examples/textures/resources/parrots.png")   ' Loaded in CPU memory (RAM)
+ImageFormat(image, UNCOMPRESSED_R8G8B8A8)         ' Format image to RGBA 32bit (required for texture update) <-- ISSUE
+Local texture:RTexture2D = LoadTextureFromImage(image)    ' Image converted to texture, GPU memory (VRAM)
+
+Local currentProcess:Int = NONE
+Local textureReload:Int = False
+
+Local selectRecs:RRectangle[NUM_PROCESSES]
+
+For Local i:Int = 0 Until NUM_PROCESSES
+	selectRecs[i] = New RRectangle(40.0, Float(50 + 32*i), 150.0, 30.0)
+Next
+
+SetTargetFPS(60)
+'---------------------------------------------------------------------------------------
+
+' Main game loop
+While Not WindowShouldClose()    ' Detect window close button or ESC key
+	' Update
+	'----------------------------------------------------------------------------------
+	If IsKeyPressed(KEY_DOWN) Then
+		currentProcess :+ 1
+		If currentProcess > 7 Then
+			currentProcess = 0
+		End If
+		textureReload = True
+	Else If IsKeyPressed(KEY_UP) Then
+		currentProcess :- 1
+		If currentProcess < 0 Then
+			currentProcess = 7
+		End If
+		textureReload = True
+	End If
+
+	If textureReload Then
+		UnloadImage(image)                         ' Unload current image data
+		image = LoadImage("../../lib.mod/raylib/examples/textures/resources/parrots.png") ' Re-load image data
+
+		' NOTE: Image processing is a costly CPU process to be done every frame,
+		' If image processing is required in a frame-basis, it should be done
+		' with a texture and by shaders
+		Select currentProcess
+			Case COLOR_GRAYSCALE
+				ImageColorGrayscale(image)
+			Case COLOR_TINT
+				ImageColorTint(image, GREEN)
+			Case COLOR_INVERT
+				ImageColorInvert(image)
+			Case COLOR_CONTRAST
+				ImageColorContrast(image, -40)
+			Case COLOR_BRIGHTNESS
+				ImageColorBrightness(image, -80)
+			Case FLIP_VERTICAL
+				ImageFlipVertical(image)
+			Case FLIP_HORIZONTAL
+				ImageFlipHorizontal(image)
+		End Select
+
+		Local pixels:RColor Ptr = GetImageData(image)        ' Get pixel data from image (RGBA 32bit)
+		UpdateTexture(texture, pixels)             ' Update texture with new image data
+		RLFree(pixels)                               ' Unload pixels data from RAM
+
+		textureReload = False
+	End If
+	'----------------------------------------------------------------------------------
+
+	' Draw
+	'----------------------------------------------------------------------------------
+	BeginDrawing()
+
+		ClearBackground(RAYWHITE)
+
+		DrawText("IMAGE PROCESSING:", 40, 30, 10, DARKGRAY)
+
+		' Draw rectangles
+		For Local i:Int = 0 Until NUM_PROCESSES
+			Local recCol:RColor = LIGHTGRAY
+			Local lineCol:RColor = GRAY
+			Local txtCol:RColor = DARKGRAY
+			
+			If i = currentProcess Then
+				recCol = SKYBLUE
+				lineCol = BLUE
+				txtCol = DARKBLUE
+			End If
+			
+			DrawRectangleRec(selectRecs[i], recCol)
+			DrawRectangleLines(Int(selectRecs[i].x), Int(selectRecs[i].y), Int(selectRecs[i].width), Int(selectRecs[i].height), lineCol)
+			DrawText( processText[i], Int( selectRecs[i].x + selectRecs[i].width/2 - MeasureText(processText[i], 10)/2), Int(selectRecs[i].y + 11), 10, txtCol)
+		Next
+
+		DrawTexture(texture, screenWidth - texture.width - 60, screenHeight/2 - texture.height/2, WHITE)
+		DrawRectangleLines(screenWidth - texture.width - 60, screenHeight/2 - texture.height/2, texture.width, texture.height, BLACK)
+
+	EndDrawing()
+	'----------------------------------------------------------------------------------
+Wend
+
+' De-Initialization
+'--------------------------------------------------------------------------------------
+UnloadTexture(texture)       ' Unload texture from VRAM
+UnloadImage(image)           ' Unload image from RAM
+
+CloseWindow()                ' Close window and OpenGL context
+'--------------------------------------------------------------------------------------
+
+Const NONE:Int = 0
+Const COLOR_GRAYSCALE:Int = 1
+Const COLOR_TINT:Int = 2
+Const COLOR_INVERT:Int = 3
+Const COLOR_CONTRAST:Int = 4
+Const COLOR_BRIGHTNESS:Int = 5
+Const FLIP_VERTICAL:Int = 6
+Const FLIP_HORIZONTAL:Int = 7

+ 70 - 0
examples/textures/textures_image_text.bmx

@@ -0,0 +1,70 @@
+SuperStrict
+
+Framework Ray.Lib
+
+' Initialization
+'--------------------------------------------------------------------------------------
+Const screenWidth:Int = 800
+Const screenHeight:Int = 450
+
+InitWindow(screenWidth, screenHeight, "raylib [texture] example - image text drawing")
+
+Local parrots:RImage = LoadImage("../../lib.mod/raylib/examples/textures/resources/parrots.png") ' Load image in CPU memory (RAM)
+
+' TTF Font loading with custom generation parameters
+Local font:RFont = LoadFontEx("../../lib.mod/raylib/examples/textures/resources/KAISG.ttf", 64, 0, 0)
+
+' Draw over image using custom font
+ImageDrawTextEx(parrots, New RVector2(20.0, 20.0), font, "[Parrots font drawing]", Float(font.baseSize), 0.0, RED)
+
+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
+
+Local position:RVector2 = New RVector2(Float(screenWidth/2 - texture.width/2), Float(screenHeight/2 - texture.height/2 - 20))
+
+Local showFont:Int = False
+
+SetTargetFPS(60)
+'--------------------------------------------------------------------------------------
+
+' Main game loop
+While Not WindowShouldClose()    ' Detect window close button or ESC key
+	' Update
+	'----------------------------------------------------------------------------------
+	If IsKeyDown(KEY_SPACE) Then
+		showFont = True
+	Else
+		showFont = False
+	End If
+	'----------------------------------------------------------------------------------
+
+	' Draw
+	'----------------------------------------------------------------------------------
+	BeginDrawing()
+
+		ClearBackground(RAYWHITE)
+
+		If Not showFont Then
+			' Draw texture with text already drawn inside
+			DrawTextureV(texture, position, WHITE)
+
+			' Draw text directly using sprite font
+			DrawTextEx(font, "[Parrots font drawing]", New RVector2(position.x + 20, position.y + 20 + 280), Float(font.baseSize), 0.0, WHITE)
+		Else
+			DrawTexture(font.texture, screenWidth/2 - font.texture.width/2, 50, BLACK)
+		End If
+
+		DrawText("PRESS SPACE to SEE USED SPRITEFONT ", 290, 420, 10, DARKGRAY)
+
+	EndDrawing()
+	'----------------------------------------------------------------------------------
+Wend
+
+' De-Initialization
+'--------------------------------------------------------------------------------------
+UnloadTexture(texture)     ' Texture unloading
+
+UnloadFont(font)           ' Unload custom spritefont
+
+CloseWindow()              ' Close window and OpenGL context
+'--------------------------------------------------------------------------------------

+ 42 - 0
examples/textures/textures_logo_raylib.bmx

@@ -0,0 +1,42 @@
+SuperStrict
+
+Framework Ray.Lib
+
+' Initialization
+'--------------------------------------------------------------------------------------
+Const screenWidth:Int = 800
+Const screenHeight:Int = 450
+
+InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture loading and drawing")
+
+' NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
+Local texture:RTexture2D = LoadTexture("../../lib.mod/raylib/examples/textures/resources/raylib_logo.png")        ' Texture loading
+'---------------------------------------------------------------------------------------
+
+' 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!", 360, 370, 10, GRAY)
+
+	EndDrawing()
+	'----------------------------------------------------------------------------------
+Wend
+
+' De-Initialization
+'--------------------------------------------------------------------------------------
+UnloadTexture(texture)       ' Texture unloading
+
+CloseWindow()                ' Close window and OpenGL context
+'--------------------------------------------------------------------------------------

+ 212 - 0
examples/textures/textures_mouse_painting.bmx

@@ -0,0 +1,212 @@
+SuperStrict
+
+Framework Ray.Lib
+Import brl.standardio
+
+Const MAX_COLORS_COUNT:Int = 23 ' Number of colors available
+
+' Initialization
+'--------------------------------------------------------------------------------------
+Const screenWidth:Int = 800
+Const screenHeight:Int = 450
+
+InitWindow(screenWidth, screenHeight, "raylib [textures] example - mouse painting")
+
+' Colours to choose from
+Local colors:RColor[] = [ ..
+	RAYWHITE, YELLOW, GOLD, ORANGE, PINK, RED, MAROON, GREEN, LIME, DARKGREEN, ..
+	SKYBLUE, BLUE, DARKBLUE, PURPLE, VIOLET, DARKPURPLE, BEIGE, BROWN, DARKBROWN, ..
+	LIGHTGRAY, GRAY, DARKGRAY, BLACK]
+	
+' Define colorsRecs data (for every rectangle)
+Local colorsRecs:RRectangle[MAX_COLORS_COUNT]
+
+For Local i:Int = 0 Until MAX_COLORS_COUNT
+	colorsRecs[i].x = 10 + 30*i + 2*i
+	colorsRecs[i].y = 10
+	colorsRecs[i].width = 30
+	colorsRecs[i].height = 30
+Next
+
+Local colorSelected:Int = 0
+Local colorSelectedPrev:Int = colorSelected
+Local colorMouseHover:Int = 0
+Local brushSize:Int = 20
+
+Local btnSaveRec:RRectangle = New RRectangle(750, 10, 40, 30)
+Local btnSaveMouseHover:Int = False
+Local showSaveMessage:Int = False
+Local saveMessageCounter:Int = 0
+
+' Create a RenderTexture2D to use as a canvas
+Local target:RRenderTexture2D = LoadRenderTexture(screenWidth, screenHeight)
+
+' Clear render texture before entering the game loop
+BeginTextureMode(target)
+ClearBackground(colors[0])
+EndTextureMode()
+
+SetTargetFPS(120)              ' Set our game to run at 120 frames-per-second
+'--------------------------------------------------------------------------------------
+
+' Main game loop
+While Not WindowShouldClose()    ' Detect window close button or ESC key
+	' Update
+	'----------------------------------------------------------------------------------
+	Local mousePos:RVector2 = GetMousePosition()
+	
+	' Move between colors with keys
+	If IsKeyPressed(KEY_RIGHT) Then
+		colorSelected :+ 1
+	Else If IsKeyPressed(KEY_LEFT) Then
+		colorSelected :- 1
+	End If
+	
+	If colorSelected >= MAX_COLORS_COUNT Then
+		colorSelected = MAX_COLORS_COUNT - 1
+	Else If colorSelected < 0 Then
+		colorSelected = 0
+	End If
+
+	' Choose color with mouse
+	For Local i:Int = 0 Until MAX_COLORS_COUNT
+		If CheckCollisionPointRec(mousePos, colorsRecs[i]) Then
+			colorMouseHover = i
+			Exit
+		Else
+			colorMouseHover = -1
+		End If
+	Next
+	
+	If (colorMouseHover >= 0) And IsMouseButtonPressed(MOUSE_LEFT_BUTTON) Then
+		colorSelected = colorMouseHover
+		colorSelectedPrev = colorSelected
+	End If
+
+	' Change brush size
+	brushSize :+ GetMouseWheelMove()*5
+	If brushSize < 2 Then
+		brushSize = 2
+	End If
+	If brushSize > 50 Then
+		brushSize = 50
+	End If
+
+	If IsKeyPressed(KEY_C) Then
+		' Clear render texture to clear color
+		BeginTextureMode(target)
+		ClearBackground(colors[0])
+		EndTextureMode()
+	End If
+
+	If IsMouseButtonDown(MOUSE_LEFT_BUTTON) Then
+		' Paint circle into render texture
+		' NOTE: To avoid discontinuous circles, we could store
+		' previous-next mouse points and just draw a line using brush size
+		BeginTextureMode(target)
+		If mousePos.y > 50 Then
+			DrawCircle(mousePos.x, mousePos.y, brushSize, colors[colorSelected])
+		End If
+		EndTextureMode()
+	End If
+
+	If IsMouseButtonDown(MOUSE_RIGHT_BUTTON) Then
+		colorSelected = 0
+		
+		' Erase circle from render texture
+		BeginTextureMode(target)
+		If mousePos.y > 50 Then
+			DrawCircle(mousePos.x, mousePos.y, brushSize, colors[0])
+		End If
+		EndTextureMode()
+	Else
+		colorSelected = colorSelectedPrev
+	End If
+	
+	' Check mouse hover save button
+	If CheckCollisionPointRec(mousePos, btnSaveRec) Then
+		btnSaveMouseHover = True
+	Else
+		btnSaveMouseHover = False
+	End If
+	
+	' Image saving logic
+	' NOTE: Saving painted texture to a default named image
+	If (btnSaveMouseHover And IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) Or IsKeyPressed(KEY_S) Then
+		Local image:RImage = GetTextureData(target.texture)
+		ImageFlipVertical(image)
+		ExportImage(image, "my_amazing_texture_painting.png")
+		UnloadImage(image)
+		showSaveMessage = True
+	End If
+	
+	If showSaveMessage Then
+		' On saving, show a full screen message for 2 seconds
+		saveMessageCounter :+ 1
+		If saveMessageCounter > 240 Then
+			showSaveMessage = False
+			saveMessageCounter = 0
+		End If
+	End If
+	'----------------------------------------------------------------------------------
+
+	' Draw
+	'----------------------------------------------------------------------------------
+	BeginDrawing()
+
+		ClearBackground(RAYWHITE)
+
+		' NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
+		DrawTextureRec(target.texture, New RRectangle(0, 0, target.texture.width, -target.texture.height), New RVector2(0, 0), WHITE)
+
+		' Draw drawing circle for reference
+		If mousePos.y > 50 Then
+			If IsMouseButtonDown(MOUSE_RIGHT_BUTTON) Then
+				DrawCircleLines(mousePos.x, mousePos.y, brushSize, colors[colorSelected])
+			Else
+				DrawCircle(GetMouseX(), GetMouseY(), brushSize, colors[colorSelected])
+			End If
+		End If
+		
+		' Draw top panel
+		DrawRectangle(0, 0, GetScreenWidth(), 50, RAYWHITE)
+		DrawLine(0, 50, GetScreenWidth(), 50, LIGHTGRAY)
+
+		' Draw color selection rectangles
+		For Local i:Int = 0 Until MAX_COLORS_COUNT
+			DrawRectangleRec(colorsRecs[i], colors[i])
+		Next
+		DrawRectangleLines(10, 10, 30, 30, LIGHTGRAY)
+
+		If colorMouseHover >= 0 Then
+			DrawRectangleRec(colorsRecs[colorMouseHover], Fade(WHITE, 0.6))
+		End If
+
+		DrawRectangleLinesEx(New RRectangle(colorsRecs[colorSelected].x - 2, colorsRecs[colorSelected].y - 2, ..
+							 colorsRecs[colorSelected].width + 4, colorsRecs[colorSelected].height + 4), 2, BLACK)
+
+		' Draw save image button
+		Local buttonCol:RColor = BLACK
+		If btnSaveMouseHover Then
+			buttonCol = RED
+		End If
+		DrawRectangleLinesEx(btnSaveRec, 2, buttonCol)
+		DrawText("SAVE!", 755, 20, 10, buttonCol)
+		
+		' Draw save image message
+		If showSaveMessage Then
+			DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, 0.8))
+			DrawRectangle(0, 150, GetScreenWidth(), 80, BLACK)
+			DrawText("IMAGE SAVED:  my_amazing_texture_painting.png", 150, 180, 20, RAYWHITE)
+		End If
+		
+	EndDrawing()
+	'----------------------------------------------------------------------------------
+Wend
+
+' De-Initialization
+'--------------------------------------------------------------------------------------
+UnloadRenderTexture(target)    ' Unload render texture
+
+CloseWindow()                  ' Close window and OpenGL context
+'--------------------------------------------------------------------------------------