83 lines
2.8 KiB
Go
83 lines
2.8 KiB
Go
// Package render provides a tile-based rendering system inspired by the NES 8-bit graphics API.
|
||
//
|
||
// This is not an emulator, but rather a rendering layer designed to mimic the style
|
||
// and constraints of 8-bit consoles. It exposes a simplified API for tile-based drawing,
|
||
// allowing rendering of graphics in a grid-based fashion similar to early consoles.
|
||
//
|
||
// The render target is an off-screen texture with a fixed resolution of 224×288 pixels,
|
||
// divided into 8x8 pixel tiles:
|
||
//
|
||
// - Screen dimensions: 28 tiles wide, 36 tiles high
|
||
// - Tile size: 8x8 pixels
|
||
//
|
||
// This off-screen texture is then scaled to fit the actual window size when presented to the screen.
|
||
// Rendering is double-buffered and must follow a Begin/Draw/End pattern each frame.
|
||
package render
|
||
|
||
import rl "github.com/gen2brain/raylib-go/raylib"
|
||
|
||
// target is the off-screen render target where all drawing operations are performed.
|
||
// It has a fixed resolution of WIDTHxHEIGHT and is later scaled to the window size.
|
||
var target rl.RenderTexture2D
|
||
|
||
// Init initializes the rendering system.
|
||
//
|
||
// It creates an off-screen render texture with the fixed resolution defined by WIDTH and HEIGHT.
|
||
// This texture is where all tiles and game graphics are drawn before being scaled and presented.
|
||
func Init() {
|
||
target = rl.LoadRenderTexture(WIDTH, HEIGHT)
|
||
}
|
||
|
||
// 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)
|
||
}
|
||
|
||
// 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() {
|
||
// Bind texture so that all draw calls after are applied to target texture
|
||
rl.BeginTextureMode(target)
|
||
// Clear background
|
||
rl.ClearBackground(rl.Black)
|
||
}
|
||
|
||
// 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),
|
||
}
|
||
|
||
// Define destination rectangle (window).
|
||
dest := rl.Rectangle{
|
||
X: 0,
|
||
Y: 0,
|
||
Width: float32(rl.GetScreenWidth()),
|
||
Height: float32(rl.GetScreenHeight()),
|
||
}
|
||
|
||
// Blit the off-screen texture to the screen.
|
||
rl.DrawTexturePro(target.Texture, src, dest, rl.Vector2Zero(), 0.0, rl.White)
|
||
|
||
// End drawing to screen.
|
||
rl.EndDrawing()
|
||
}
|