1
0
Fork 0

Initial commit

This commit is contained in:
Henrik Hautakoski 2025-06-07 20:26:00 +02:00
commit f26c478727
18 changed files with 621 additions and 0 deletions

65
graphics/sdl.go Normal file
View file

@ -0,0 +1,65 @@
package graphics
import (
"github.com/pnx/go-raytracer/math"
"github.com/veandco/go-sdl2/sdl"
)
type Context struct {
w, h int32
window *sdl.Window
renderer *sdl.Renderer
}
func (c Context) Width() int32 {
return c.w
}
func (c Context) Height() int32 {
return c.h
}
func (c Context) DrawLine(x1 int32, y1 int32, x2 int32, y2 int32, color math.Color) {
c.renderer.SetDrawColor(color.R, color.G, color.B, color.A)
c.renderer.DrawLine(x1, y1, x2, y2)
}
func (c Context) DrawRect(x, y, w, h int32, color math.Color) {
c.renderer.SetDrawColor(color.R, color.G, color.B, color.A)
c.renderer.FillRect(&sdl.Rect{
X: x,
Y: y,
W: w,
H: h,
})
}
func (c Context) Sync() {
c.renderer.Present()
}
func (c Context) Exit() {
c.window.Destroy()
c.renderer.Destroy()
sdl.Quit()
}
func Init(title string, w, h int32) (*Context, error) {
if err := sdl.Init(sdl.INIT_VIDEO); err != nil {
return nil, err
}
window, err := sdl.CreateWindow(title, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, w, h, sdl.WINDOW_SHOWN)
if err != nil {
return nil, err
}
renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_SOFTWARE)
if err != nil {
return nil, err
}
return &Context{
w: w,
h: h,
window: window,
renderer: renderer,
}, nil
}