Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more Core & Shader examples #289

Merged
merged 4 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions examples/core/basic_window_manager/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package main

import (
"fmt"

rl "github.com/gen2brain/raylib-go/raylib"
)

const screenW = int32(1280)
const screenH = int32(720)

type gameScreen int

const (
LOGO = iota
TITLE
GAMEPLAY
ENDING
)

func main() {

rl.InitWindow(screenW, screenH, "raylib [core] example - basic screen manager")

var currentScreen gameScreen
currentScreen = LOGO
frames := 0

rl.SetTargetFPS(60)

for !rl.WindowShouldClose() {

switch currentScreen {
case LOGO:
frames++
if frames > 240 {
currentScreen = TITLE
}
case TITLE:
if rl.IsKeyPressed(rl.KeyEnter) {
currentScreen = GAMEPLAY
}
case GAMEPLAY:
if rl.IsKeyPressed(rl.KeyEnter) {
currentScreen = ENDING
}
case ENDING:
if rl.IsKeyPressed(rl.KeyEnter) {
currentScreen = LOGO
frames = 0
}
}

rl.BeginDrawing()

rl.ClearBackground(rl.Black)
rec := rl.NewRectangle(0, 0, float32(screenW), float32(screenH))
switch currentScreen {
case LOGO:
txt := "YOUR LOGO GOES HERE"
txtlen := rl.MeasureText(txt, 50)
rl.DrawText(txt, screenW/2-txtlen/2-3, screenH/2-50+3, 50, rl.Magenta)
rl.DrawText(txt, screenW/2-txtlen/2-1, screenH/2-50+1, 50, rl.Black)
rl.DrawText(txt, screenW/2-txtlen/2, screenH/2-50, 50, rl.White)
txt = "this message disappears in " + fmt.Sprint(240-frames) + " frames"
txtlen = rl.MeasureText(txt, 30)
rl.DrawText(txt, screenW/2-txtlen/2-3, screenH/2+3, 30, rl.Magenta)
rl.DrawText(txt, screenW/2-txtlen/2-1, screenH/2+1, 30, rl.Black)
rl.DrawText(txt, screenW/2-txtlen/2, screenH/2, 30, rl.White)
case TITLE:
rl.DrawRectangleRec(rec, rl.DarkGreen)
txt := "AN AMAZING TITLE GOES HERE"
txtlen := rl.MeasureText(txt, 50)
rl.DrawText(txt, screenW/2-txtlen/2-2, screenH/2-50+2, 50, rl.Black)
rl.DrawText(txt, screenW/2-txtlen/2, screenH/2-50, 50, rl.White)
txt = "press enter to move to next screen"
txtlen = rl.MeasureText(txt, 30)
rl.DrawText(txt, screenW/2-txtlen/2-2, screenH/2+2, 30, rl.Black)
rl.DrawText(txt, screenW/2-txtlen/2, screenH/2, 30, rl.White)
case GAMEPLAY:
rl.DrawRectangleRec(rec, rl.DarkPurple)
txt := "FUN GAMEPLAY GOES HERE"
txtlen := rl.MeasureText(txt, 50)
rl.DrawText(txt, screenW/2-txtlen/2-2, screenH/2-50+2, 50, rl.Black)
rl.DrawText(txt, screenW/2-txtlen/2, screenH/2-50, 50, rl.White)
txt = "press enter to move to next screen"
txtlen = rl.MeasureText(txt, 30)
rl.DrawText(txt, screenW/2-txtlen/2-2, screenH/2+2, 30, rl.Black)
rl.DrawText(txt, screenW/2-txtlen/2, screenH/2, 30, rl.White)
case ENDING:
rl.DrawRectangleRec(rec, rl.DarkBlue)
txt := "A DRAMATIC ENDING GOES HERE"
txtlen := rl.MeasureText(txt, 50)
rl.DrawText(txt, screenW/2-txtlen/2-2, screenH/2-50+2, 50, rl.Black)
rl.DrawText(txt, screenW/2-txtlen/2, screenH/2-50, 50, rl.White)
txt = "press enter to move to next screen"
txtlen = rl.MeasureText(txt, 30)
rl.DrawText(txt, screenW/2-txtlen/2-2, screenH/2+2, 30, rl.Black)
rl.DrawText(txt, screenW/2-txtlen/2, screenH/2, 30, rl.White)
}

rl.EndDrawing()
}

rl.CloseWindow()
}
85 changes: 85 additions & 0 deletions examples/core/smooth_pixelperfect/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package main

import (
"fmt"
"math"

rl "github.com/gen2brain/raylib-go/raylib"
)

const screenW = int32(1280)
const screenH = int32(720)
const virtualScreenW = screenW / 5
const virtualScreenH = screenH / 5
const virtualRatio = float32(screenW) / float32(virtualScreenW)

func main() {

rl.InitWindow(screenW, screenH, "raylib [core] example - smooth pixel-perfect camera")

worldSpaceCam := rl.Camera2D{}
worldSpaceCam.Zoom = 1

screenSpaceCam := rl.Camera2D{}
screenSpaceCam.Zoom = 1

target := rl.LoadRenderTexture(virtualScreenW, virtualScreenH)

rec1 := rl.NewRectangle(120, 60, 80, 40)
rec2 := rl.NewRectangle(130, 70, 90, 30)
rec3 := rl.NewRectangle(140, 80, 65, 45)

sourceRec := rl.NewRectangle(0, 0, float32(target.Texture.Width), -float32(target.Texture.Height))
destRec := rl.NewRectangle(-virtualRatio, -virtualRatio, float32(screenW)+(virtualRatio*2), float32(screenH)+(virtualRatio*2))

origin := rl.NewVector2(0, 0)

rotation, camX, camY := float32(0), float32(0), float32(0)

rl.SetTargetFPS(60)

for !rl.WindowShouldClose() {

rotation += 60 * rl.GetFrameTime()

camX = float32(math.Sin(rl.GetTime()) * 50)
camY = float32(math.Cos(rl.GetTime()) * 30)

screenSpaceCam.Target = rl.NewVector2(camX, camY)

worldSpaceCam.Target.X = screenSpaceCam.Target.X
screenSpaceCam.Target.X -= worldSpaceCam.Target.X
screenSpaceCam.Target.X *= virtualRatio

worldSpaceCam.Target.Y = screenSpaceCam.Target.Y
screenSpaceCam.Target.Y -= worldSpaceCam.Target.Y
screenSpaceCam.Target.Y *= virtualRatio

rl.BeginTextureMode(target)
rl.ClearBackground(rl.RayWhite)
rl.BeginMode2D(worldSpaceCam)

rl.DrawRectanglePro(rec1, origin, rotation, rl.Black)
rl.DrawRectanglePro(rec2, origin, -rotation, rl.Red)
rl.DrawRectanglePro(rec3, origin, rotation+45, rl.Blue)

rl.EndMode2D()
rl.EndTextureMode()

rl.BeginDrawing()
rl.ClearBackground(rl.Red)
rl.BeginMode2D(screenSpaceCam)
rl.DrawTexturePro(target.Texture, sourceRec, destRec, origin, 0, rl.White)
rl.EndMode2D()

rl.DrawText("screen res "+fmt.Sprint(screenW)+"x"+fmt.Sprint(screenH), 10, 10, 20, rl.Black)
rl.DrawText("world res "+fmt.Sprint(virtualScreenW)+"x"+fmt.Sprint(virtualScreenH), 10, 30, 20, rl.Black)
rl.DrawFPS(screenW-100, 10)

rl.EndDrawing()
}

rl.UnloadRenderTexture(target)

rl.CloseWindow()
}
27 changes: 27 additions & 0 deletions examples/shaders/multi_sample2d/color_mix.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#version 330

// Input vertex attributes (from vertex shader)
in vec3 vertexPos;
in vec2 fragTexCoord;
in vec4 fragColor;

// Input uniform values
uniform sampler2D texture0;
uniform sampler2D texture1;
uniform vec4 colDiffuse;

uniform float divider = 0.5;

out vec4 finalColor;

void main()
{
// Texel color fetching from texture sampler
vec4 texelColor0 = texture(texture0, fragTexCoord);
vec4 texelColor1 = texture(texture1, fragTexCoord);

float x = fract(fragTexCoord.s);
float final = smoothstep(divider - 0.1, divider + 0.1, x);

finalColor = mix(texelColor0, texelColor1, final);
}
69 changes: 69 additions & 0 deletions examples/shaders/multi_sample2d/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
rl "github.com/gen2brain/raylib-go/raylib"
)

func main() {
screenWidth := int32(800)
screenHeight := int32(450)

rl.SetConfigFlags(rl.FlagMsaa4xHint) // Enable Multi Sampling Anti Aliasing 4x (if available)

rl.InitWindow(screenWidth, screenHeight, "raylib [shaders] example - multiple sample2D")

imRed := rl.GenImageColor(int(screenWidth), int(screenHeight), rl.NewColor(uint8(255), uint8(0), uint8(0), uint8(255)))
texRed := rl.LoadTextureFromImage(imRed)
rl.UnloadImage(imRed)

imBlue := rl.GenImageColor(int(screenWidth), int(screenHeight), rl.NewColor(uint8(0), uint8(0), uint8(255), uint8(255)))
texBlue := rl.LoadTextureFromImage(imBlue)
rl.UnloadImage(imBlue)

shader := rl.LoadShader("", "color_mix.fs")

texBlueLoc := rl.GetShaderLocation(shader, "texture1")
dividerLoc := rl.GetShaderLocation(shader, "divider")
dividerValue := []float32{0.5}

rl.SetTargetFPS(60)

for !rl.WindowShouldClose() {

if rl.IsKeyDown(rl.KeyRight) {
dividerValue[0] += 0.01
} else if rl.IsKeyDown(rl.KeyLeft) {
dividerValue[0] -= 0.01
}

if dividerValue[0] < 0 {
dividerValue[0] = 0
} else if dividerValue[0] > 1 {
dividerValue[0] = 1
}

rl.SetShaderValue(shader, dividerLoc, dividerValue, rl.ShaderUniformFloat)

rl.BeginDrawing()

rl.ClearBackground(rl.RayWhite)

rl.BeginShaderMode(shader)

rl.SetShaderValueTexture(shader, texBlueLoc, texBlue)
rl.DrawTexture(texRed, 0, 0, rl.White)

rl.EndShaderMode()

rl.DrawText("USE LEFT/RIGHT ARROW KEYS TO MIX COLORS", 15, 17, 20, rl.Black)
rl.DrawText("USE LEFT/RIGHT ARROW KEYS TO MIX COLORS", 16, 16, 20, rl.White)

rl.EndDrawing()
}

rl.UnloadShader(shader) // Unload shader
rl.UnloadTexture(texRed) // Unload texture
rl.UnloadTexture(texBlue) // Unload texture

rl.CloseWindow()
}
Loading