Browse Source

Added more textures examples.

Brucey 5 years ago
parent
commit
fbb0b3aff9

+ 76 - 0
examples/textures/textures_raw_data.bmx

@@ -0,0 +1,76 @@
+SuperStrict
+
+Framework Ray.Lib
+
+' Initialization
+'--------------------------------------------------------------------------------------
+Const screenWidth:Int = 800
+Const screenHeight:Int = 450
+
+InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture from raw data")
+
+' NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
+
+' Load RAW image data (512x512, 32bit RGBA, no file header)
+Local fudesumiRaw:RImage = LoadImageRaw("../../lib.mod/raylib/examples/textures/resources/fudesumi.raw", 384, 512, UNCOMPRESSED_R8G8B8A8, 0)
+Local fudesumi:RTexture2D = LoadTextureFromImage(fudesumiRaw)  ' Upload CPU (RAM) image to GPU (VRAM)
+UnloadImage(fudesumiRaw)                                ' Unload CPU (RAM) image data
+
+' Generate a checked texture by code
+Local width:Int = 960
+Local height:Int = 480
+
+' Dynamic memory allocation to store pixels data (Color type)
+Local pixels:RColor[width*height]
+
+For Local y:Int = 0 Until height
+	For Local x:Int = 0 Until width
+		If ((x/32+y/32)/1) Mod 2 = 0 Then
+			pixels[y*width + x] = ORANGE
+		Else
+			pixels[y*width + x] = GOLD
+		End If
+	Next
+Next
+
+' Load pixels data into an image structure and create texture
+Local checkedIm:RImage = LoadImageEx(pixels, width, height)
+Local checked:RTexture2D = LoadTextureFromImage(checkedIm)
+UnloadImage(checkedIm)         ' Unload CPU (RAM) image data
+
+
+'---------------------------------------------------------------------------------------
+
+' Main game loop
+While Not WindowShouldClose()    ' Detect window close button or ESC key
+	' Update
+	'----------------------------------------------------------------------------------
+	' TODO: Update your variables here
+	'----------------------------------------------------------------------------------
+
+	' Draw
+	'----------------------------------------------------------------------------------
+	BeginDrawing()
+
+		ClearBackground(RAYWHITE)
+
+		DrawTexture(checked, screenWidth/2 - checked.width/2, screenHeight/2 - checked.height/2, Fade(WHITE, 0.5))
+		DrawTexture(fudesumi, 430, -30, WHITE)
+
+		DrawText("CHECKED TEXTURE ", 84, 85, 30, BROWN)
+		DrawText("GENERATED by CODE", 72, 148, 30, BROWN)
+		DrawText("and RAW IMAGE LOADING", 46, 210, 30, BROWN)
+
+		DrawText("(c) Fudesumi sprite by Eiden Marsal", 310, screenHeight - 20, 10, BROWN)
+
+	EndDrawing()
+	'----------------------------------------------------------------------------------
+Wend
+
+' De-Initialization
+'--------------------------------------------------------------------------------------
+UnloadTexture(fudesumi)    ' Texture unloading
+UnloadTexture(checked)     ' Texture unloading
+
+CloseWindow()              ' Close window and OpenGL context
+'--------------------------------------------------------------------------------------

+ 96 - 0
examples/textures/textures_rectangle.bmx

@@ -0,0 +1,96 @@
+SuperStrict
+
+Framework Ray.Lib
+Import Text.Format
+
+
+Const MAX_FRAME_SPEED:Int = 15
+Const MIN_FRAME_SPEED:Int = 1
+
+Local formatter:TFormatter = TFormatter.Create("%02i FPS")
+
+' Initialization
+'--------------------------------------------------------------------------------------
+Const screenWidth:Int = 800
+Const screenHeight:Int = 450
+
+InitWindow(screenWidth, screenHeight, "raylib [texture] example - texture rectangle")
+
+' 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 position:RVector2 = New RVector2(350.0, 280.0)
+Local frameRec:RRectangle = New RRectangle(0.0, 0.0, scarfy.width/6.0, scarfy.height)
+Local currentFrame:Int = 0
+
+Local framesCounter:Int = 0
+Local framesSpeed:Int = 8            ' Number of spritesheet frames shown by second
+
+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
+
+	If framesCounter >= (60/framesSpeed) Then
+		framesCounter = 0
+		currentFrame :+ 1
+
+		If currentFrame > 5 Then
+			currentFrame = 0
+		End If
+
+		frameRec.x = currentFrame*scarfy.width/6.0
+	End If
+
+	If IsKeyPressed(KEY_RIGHT) Then
+		framesSpeed :+ 1
+	Else If IsKeyPressed(KEY_LEFT) Then
+		framesSpeed :- 1
+	End If
+
+	If framesSpeed > MAX_FRAME_SPEED Then
+		framesSpeed = MAX_FRAME_SPEED
+	Else If framesSpeed < MIN_FRAME_SPEED Then
+		framesSpeed = MIN_FRAME_SPEED
+	End If
+	'----------------------------------------------------------------------------------
+
+	' Draw
+	'----------------------------------------------------------------------------------
+	BeginDrawing()
+
+		ClearBackground(RAYWHITE)
+
+		DrawTexture(scarfy, 15, 40, WHITE)
+		DrawRectangleLines(15, 40, scarfy.width, scarfy.height, LIME)
+		DrawRectangleLines(Int(15 + frameRec.x), Int(40 + frameRec.y), Int(frameRec.width), Int(frameRec.height), RED)
+
+		DrawText("FRAME SPEED: ", 165, 210, 10, DARKGRAY)
+		DrawText(formatter.Clear().Arg(framesSpeed).Format(), 575, 210, 10, DARKGRAY)
+		DrawText("PRESS RIGHT/LEFT KEYS to CHANGE SPEED!", 290, 240, 10, DARKGRAY)
+
+		For Local i:Int = 0 Until MAX_FRAME_SPEED
+			If i < framesSpeed Then
+				DrawRectangle(250 + 21*i, 205, 20, 20, RED)
+			End If
+			DrawRectangleLines(250 + 21*i, 205, 20, 20, MAROON)
+		Next
+
+		DrawTextureRec(scarfy, frameRec, position, WHITE)  ' Draw part of the texture
+
+		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
+'--------------------------------------------------------------------------------------

+ 87 - 0
examples/textures/textures_sprite_button.bmx

@@ -0,0 +1,87 @@
+SuperStrict
+
+Framework Ray.Lib
+Import Ray.Audio
+
+Const NUM_FRAMES:Int = 3       ' Number of frames (rectangles) for the button sprite texture
+
+' Initialization
+'--------------------------------------------------------------------------------------
+Const screenWidth:Int = 800
+Const screenHeight:Int = 450
+
+InitWindow(screenWidth, screenHeight, "raylib [textures] example - sprite button")
+
+InitAudioDevice()      ' Initialize audio device
+
+Local fxButton:RSound = LoadSound("../../lib.mod/raylib/examples/textures/resources/buttonfx.wav")   ' Load button sound
+Local button:RTexture2D = LoadTexture("../../lib.mod/raylib/examples/textures/resources/button.png") ' Load button texture
+
+' Define frame rectangle for drawing
+Local frameHeight:Int = button.height/NUM_FRAMES
+Local sourceRec:RRectangle = New RRectangle(0, 0, button.width, frameHeight)
+
+' Define button bounds on screen
+Local btnBounds:RRectangle = New RRectangle(screenWidth/2 - button.width/2, screenHeight/2 - button.height/NUM_FRAMES/2, button.width, frameHeight)
+
+Local btnState:Int = 0               ' Button state: 0-NORMAL, 1-MOUSE_HOVER, 2-PRESSED
+Local btnAction:Int = False         ' Button action should be activated
+
+Local mousePoint:RVector2 = New RVector2(0.0, 0.0)
+
+SetTargetFPS(60)
+'--------------------------------------------------------------------------------------
+
+' Main game loop
+While Not WindowShouldClose()    ' Detect window close button or ESC key
+	' Update
+	'----------------------------------------------------------------------------------
+	mousePoint = GetMousePosition()
+	btnAction = False
+
+	' Check button state
+	If CheckCollisionPointRec(mousePoint, btnBounds) Then
+		If IsMouseButtonDown(MOUSE_LEFT_BUTTON) Then
+			btnState = 2
+		Else
+			btnState = 1
+		End If
+
+		If IsMouseButtonReleased(MOUSE_LEFT_BUTTON) Then
+			btnAction = True
+		End If
+	Else
+		btnState = 0
+	End If
+
+	If btnAction Then
+		PlaySound(fxButton)
+
+		' TODO: Any desired action
+	End If
+
+	' Calculate button frame rectangle to draw depending on button state
+	sourceRec.y = btnState*frameHeight
+	'----------------------------------------------------------------------------------
+
+	' Draw
+	'----------------------------------------------------------------------------------
+	BeginDrawing()
+
+		ClearBackground(RAYWHITE)
+
+		DrawTextureRec(button, sourceRec, New RVector2(btnBounds.x, btnBounds.y), WHITE) ' Draw button frame
+
+	EndDrawing()
+	'----------------------------------------------------------------------------------
+Wend
+
+' De-Initialization
+'--------------------------------------------------------------------------------------
+UnloadTexture(button)  ' Unload button texture
+UnloadSound(fxButton)  ' Unload sound
+
+CloseAudioDevice()     ' Close audio device
+
+CloseWindow()          ' Close window and OpenGL context
+'--------------------------------------------------------------------------------------

+ 103 - 0
examples/textures/textures_sprite_explosion.bmx

@@ -0,0 +1,103 @@
+SuperStrict
+
+Framework Ray.Lib
+Import Ray.Audio
+
+Const NUM_FRAMES:Int = 8
+Const NUM_LINES:Int = 6
+
+' Initialization
+'--------------------------------------------------------------------------------------
+Const screenWidth:Int = 800
+Const screenHeight:Int = 450
+
+InitWindow(screenWidth, screenHeight, "raylib [textures] example - sprite explosion")
+
+InitAudioDevice()
+
+' Load explosion sound
+Local fxBoom:RSound = LoadSound("../../lib.mod/raylib/examples/textures/resources/boom.wav")
+
+' Load explosion texture
+Local explosion:RTexture2D = LoadTexture("../../lib.mod/raylib/examples/textures/resources/explosion.png")
+
+' Init variables for animation
+Local frameWidth:Int = explosion.width/NUM_FRAMES    ' Sprite one frame rectangle width
+Local frameHeight:Int = explosion.height/NUM_LINES   ' Sprite one frame rectangle height
+Local currentFrame:Int = 0
+Local currentLine:Int = 0
+
+Local frameRec:RRectangle = New RRectangle(0, 0, frameWidth, frameHeight)
+Local position:RVector2 = New RVector2(0.0, 0.0)
+
+Local active:Int = False
+Local framesCounter:Int = 0
+
+SetTargetFPS(120)
+'--------------------------------------------------------------------------------------
+
+' Main game loop
+While Not WindowShouldClose()    ' Detect window close button or ESC key
+	' Update
+	'----------------------------------------------------------------------------------
+
+	' Check for mouse button pressed and activate explosion (if not active)
+	If IsMouseButtonPressed(MOUSE_LEFT_BUTTON) And Not active Then
+		position = GetMousePosition()
+		active = True
+
+		position.x :- frameWidth/2
+		position.y :- frameHeight/2
+
+		PlaySound(fxBoom)
+	End If
+
+	' Compute explosion animation frames
+	If active Then
+		framesCounter :+ 1
+
+		If framesCounter > 2 Then
+			currentFrame :+ 1
+
+			If currentFrame >= NUM_FRAMES Then
+				currentFrame = 0
+				currentLine :+ 1
+
+				If currentLine >= NUM_LINES Then
+					currentLine = 0
+					active = False
+				End If
+			End If
+
+			framesCounter = 0
+		End If
+	End If
+
+	frameRec.x = frameWidth*currentFrame
+	frameRec.y = frameHeight*currentLine
+	'----------------------------------------------------------------------------------
+
+	' Draw
+	'----------------------------------------------------------------------------------
+	BeginDrawing()
+
+		ClearBackground(RAYWHITE)
+
+		' Draw explosion required frame rectangle
+		If active Then
+			DrawTextureRec(explosion, frameRec, position, WHITE)
+		End If
+
+	EndDrawing()
+	'----------------------------------------------------------------------------------
+Wend
+
+' De-Initialization
+'--------------------------------------------------------------------------------------
+UnloadTexture(explosion)   ' Unload texture
+UnloadSound(fxBoom)        ' Unload sound
+
+CloseAudioDevice()
+
+CloseWindow()              ' Close window and OpenGL context
+'--------------------------------------------------------------------------------------