1
0
Fork 0

Initial commit

This commit is contained in:
Henrik Hautakoski 2025-09-14 08:38:30 +02:00
commit 0245a5cb43
22 changed files with 610 additions and 0 deletions

24
engine/render/context.go Normal file
View file

@ -0,0 +1,24 @@
package render
import (
gfxfont "tetris/engine/graphics/font"
rl "github.com/gen2brain/raylib-go/raylib"
)
var (
texture rl.Texture2D
font *gfxfont.TileFont = nil
)
func SetTexture(tex rl.Texture2D) {
texture = tex
}
func GetTexture() rl.Texture2D {
return texture
}
func SetFont(fnt *gfxfont.TileFont) {
font = fnt
}

26
engine/render/rect.go Normal file
View file

@ -0,0 +1,26 @@
package render
import (
"image/color"
rl "github.com/gen2brain/raylib-go/raylib"
)
// DrawRectBorder draw a rectangle with a border around it.
// Note that the rectangle passed as the 'rect' parameter to the function represents the actual
// rectangle. The border will be drawn around it in all directions (outer border).
func DrawRectBorder(rect rl.RectangleInt32, col color.RGBA, border_size int32, border_col color.RGBA) {
rl.DrawRectangleRec(rl.Rectangle{
X: float32(rect.X),
Y: float32(rect.Y),
Width: float32(rect.Width),
Height: float32(rect.Height),
}, col)
rl.DrawRectangleLinesEx(rl.Rectangle{
X: float32(rect.X - border_size),
Y: float32(rect.Y - border_size),
Width: float32(rect.Width + (border_size * 2)),
Height: float32(rect.Height + (border_size * 2)),
}, float32(border_size), border_col)
}

107
engine/render/render.go Normal file
View file

@ -0,0 +1,107 @@
package render
import (
"image/color"
"tetris/engine/system"
rl "github.com/gen2brain/raylib-go/raylib"
)
type Config struct {
Title string
// Window
WindowWidth int32
WindowHeight int32
WindowFlags uint32
// Render target resolution
RenderHeight int32
RenderWidth int32
ScaleFlags ScaleFlags
}
// target is the off-screen render target where all drawing operations are performed.
// It has a fixed resolution and is later scaled to the window size.
var target rl.RenderTexture2D
var scaleFlags ScaleFlags
// Init initializes the rendering system.
//
// It creates an off-screen render texture with the fixed resolution defined by config.RenderWidth/Height.
// This texture is where all tiles and game graphics are drawn before being scaled and presented.
func Init(config Config) {
// hyprland has a bug where the height of the window is 37 pixels taller than requested.
// so we just subtract it here.
if system.IsHyprland() {
config.WindowHeight = config.WindowHeight - 37
}
scaleFlags = config.ScaleFlags
if config.WindowFlags > 0 {
rl.SetConfigFlags(config.WindowFlags)
}
// Setup window
rl.InitWindow(config.WindowWidth, config.WindowHeight, config.Title)
// Initialize render texture.
target = rl.LoadRenderTexture(config.RenderWidth, config.RenderHeight)
rl.SetTextureFilter(target.Texture, rl.TextureFilterNearest)
}
// Exit shuts down the rendering system.
//
// It unloads the render texture and frees any associated GPU resources.
// This should be called before the application exits.
func Exit() {
rl.UnloadRenderTexture(target)
rl.CloseWindow()
}
// Begin prepares the rendering system for a new frame.
//
// It binds the off-screen texture target and clears it to a black background.
// All tile drawing operations (e.g., DrawTile) should occur after Begin() and
// before End().
func Begin(col color.RGBA) {
// Bind texture so that all draw calls after are applied to target texture
rl.BeginTextureMode(target)
// Clear the texture
rl.ClearBackground(col)
}
// End finalizes the current frame and displays it on the screen.
//
// It ends the off-screen drawing session and stretches the texture target
// to fill the window. This function must be called after all drawing is done.
func End() {
// End drawing to the texture target.
rl.EndTextureMode()
// Begin drawing to screen buffer.
rl.BeginDrawing()
// Define source rectangle (flip vertically to match coordinate systems).
src := rl.Rectangle{
X: 0,
Y: 0,
Width: float32(target.Texture.Width),
Height: -float32(target.Texture.Height),
}
// Calculate the destination rectangle (window).
dest := scale(target.Texture.Width, target.Texture.Height,
int32(rl.GetRenderWidth()), int32(rl.GetRenderHeight()),
scaleFlags)
// Blit the off-screen texture to the screen's back buffer.
rl.DrawTexturePro(target.Texture, src, dest, rl.Vector2Zero(), 0.0, rl.White)
// Swap buffers
rl.EndDrawing()
}

56
engine/render/scale.go Normal file
View file

@ -0,0 +1,56 @@
package render
import (
"math"
rl "github.com/gen2brain/raylib-go/raylib"
)
type ScaleFlags byte
const (
// Use integer scaling (pixel-perfect). Implies SCALE_NO_DOWNSCALE because integer < 1 becomes 0.
SCALE_INTEGER ScaleFlags = 1 << iota
// Do not allow shrinking below 1 (clamp scale >= 1).
SCALE_NO_DOWNSCALE
// Use "cover" (crop) instead of "contain" (letterbox). Default is contain.
SCALE_COVER
)
func scale(srcW, srcH, destW, destH int32, flags ScaleFlags) rl.Rectangle {
scale := float64(1)
sx := float64(destW) / float64(srcW)
sy := float64(destH) / float64(srcH)
// Contain (letterbox) uses min; Cover (crop) uses max.
if flags&SCALE_COVER != 0 {
scale = math.Max(sx, sy)
} else {
scale = math.Min(sx, sy)
}
if flags&SCALE_INTEGER != 0 {
// For contain, floor; for cover, ceil.
if flags&SCALE_COVER != 0 {
scale = math.Ceil(scale)
} else {
scale = math.Floor(scale)
}
// IntegerScale implies no fractional; clamp at 1 to avoid zero.
scale = max(1.0, scale)
} else if flags&SCALE_NO_DOWNSCALE != 0 && scale < 1.0 {
scale = 1.0
}
w := float64(srcW) * scale
h := float64(srcH) * scale
return rl.Rectangle{
X: float32(math.Floor((float64(destW) - w) / 2.0)),
Y: float32(math.Floor((float64(destH) - h) / 2.0)),
Width: float32(w),
Height: float32(h),
}
}

31
engine/render/text.go Normal file
View file

@ -0,0 +1,31 @@
package render
import (
"image/color"
rl "github.com/gen2brain/raylib-go/raylib"
)
func DrawText(x, y, size int32, text string, col color.RGBA) {
destRect := rl.Rectangle{
X: float32(x),
Y: float32(y),
Width: float32(size),
Height: float32(size),
}
for _, ch := range text {
if ch == ' ' {
destRect.X += float32(size)
continue
}
tile := font.GetTile(ch)
if tile == nil {
destRect.X += float32(size)
continue
}
rl.DrawTexturePro(texture, tile.GetTexRect(), destRect, rl.Vector2Zero(), 0, col)
destRect.X += float32(size)
}
}

8
engine/render/texture.go Normal file
View file

@ -0,0 +1,8 @@
package render
import rl "github.com/gen2brain/raylib-go/raylib"
// DrawTextureRec - Draw a rectangle from the current set texture.
func DrawTextureRec(src rl.Rectangle, dest rl.Rectangle) {
rl.DrawTexturePro(texture, src, dest, rl.Vector2Zero(), 0.0, rl.White)
}