66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
package graphics
|
|
|
|
import (
|
|
"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, colorIndex byte) {
|
|
color := Palette[colorIndex]
|
|
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, colorIndex byte) {
|
|
color := Palette[colorIndex]
|
|
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
|
|
}
|