2018-07-22 00:12:22 +00:00
|
|
|
// Package sdl provides an SDL2 renderer for Doodle.
|
|
|
|
package sdl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2019-04-10 00:35:44 +00:00
|
|
|
"git.kirsle.net/apps/doodle/lib/events"
|
|
|
|
"git.kirsle.net/apps/doodle/lib/render"
|
2019-04-10 00:47:03 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/log"
|
2018-07-22 00:12:22 +00:00
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
|
|
"github.com/veandco/go-sdl2/ttf"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Renderer manages the SDL state.
|
|
|
|
type Renderer struct {
|
|
|
|
// Configurable fields.
|
|
|
|
title string
|
|
|
|
width int32
|
|
|
|
height int32
|
|
|
|
startTime time.Time
|
|
|
|
|
|
|
|
// Private fields.
|
|
|
|
events *events.State
|
|
|
|
window *sdl.Window
|
|
|
|
renderer *sdl.Renderer
|
|
|
|
running bool
|
|
|
|
ticks uint64
|
|
|
|
|
|
|
|
// Optimizations to minimize SDL calls.
|
|
|
|
lastColor render.Color
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates the SDL renderer.
|
2018-10-08 17:38:49 +00:00
|
|
|
func New(title string, width, height int) *Renderer {
|
2018-07-22 00:12:22 +00:00
|
|
|
return &Renderer{
|
|
|
|
events: events.New(),
|
|
|
|
title: title,
|
2018-10-08 17:38:49 +00:00
|
|
|
width: int32(width),
|
|
|
|
height: int32(height),
|
2018-07-22 00:12:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Teardown tasks when exiting the program.
|
|
|
|
func (r *Renderer) Teardown() {
|
|
|
|
r.renderer.Destroy()
|
|
|
|
r.window.Destroy()
|
|
|
|
sdl.Quit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the renderer.
|
|
|
|
func (r *Renderer) Setup() error {
|
|
|
|
// Initialize SDL.
|
|
|
|
log.Info("Initializing SDL")
|
|
|
|
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize SDL_TTF.
|
|
|
|
log.Info("Initializing SDL_TTF")
|
|
|
|
if err := ttf.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create our window.
|
|
|
|
log.Info("Creating the Main Window")
|
|
|
|
window, err := sdl.CreateWindow(
|
|
|
|
r.title,
|
|
|
|
sdl.WINDOWPOS_CENTERED,
|
|
|
|
sdl.WINDOWPOS_CENTERED,
|
|
|
|
r.width,
|
|
|
|
r.height,
|
2018-10-19 20:31:58 +00:00
|
|
|
sdl.WINDOW_SHOWN|sdl.WINDOW_RESIZABLE,
|
2018-07-22 00:12:22 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.window = window
|
|
|
|
|
|
|
|
// Blank out the window in white.
|
|
|
|
log.Info("Creating the SDL Renderer")
|
|
|
|
renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
Menu Toolbar for Editor + Shell Prompts + Theme
* Added a "menu toolbar" to the top of the Edit Mode with useful buttons
that work: New Level, New Doodad (same thing), Save, Save as, Open.
* Added ability for the dev console to prompt the user for a question,
which opens the console automatically. "Save", "Save as" and "Load"
ask for their filenames this way.
* Started groundwork for theming the app. The palette window is a light
brown with an orange title bar, the Menu Toolbar has a black
background, etc.
* Added support for multiple fonts instead of just monospace. DejaVu
Sans (normal and bold) are used now for most labels and window titles,
respectively. The dev console uses DejaVu Sans Mono as before.
* Update ui.Label to accept PadX and PadY separately instead of only
having the Padding option which did both.
* Improvements to Frame packing algorithm.
* Set the SDL draw mode to BLEND so we can use alpha colors properly,
so now the dev console is semi-translucent.
2018-08-12 00:30:00 +00:00
|
|
|
renderer.SetDrawBlendMode(sdl.BLENDMODE_BLEND)
|
2018-07-22 00:12:22 +00:00
|
|
|
r.renderer = renderer
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTicks gets SDL's current tick count.
|
|
|
|
func (r *Renderer) GetTicks() uint32 {
|
|
|
|
return sdl.GetTicks()
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
// WindowSize returns the SDL window size.
|
|
|
|
func (r *Renderer) WindowSize() (int, int) {
|
|
|
|
w, h := r.window.GetSize()
|
|
|
|
return int(w), int(h)
|
2018-07-22 00:12:22 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 03:43:01 +00:00
|
|
|
// Present the current frame.
|
|
|
|
func (r *Renderer) Present() error {
|
2018-07-22 00:12:22 +00:00
|
|
|
r.renderer.Present()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delay using sdl.Delay
|
|
|
|
func (r *Renderer) Delay(time uint32) {
|
|
|
|
sdl.Delay(time)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop is the main loop.
|
|
|
|
func (r *Renderer) Loop() error {
|
|
|
|
return nil
|
|
|
|
}
|