Skip to content

Instantly share code, notes, and snippets.

@Happy-Ferret
Forked from diegomrodz/clip_spritesheet.go
Created June 11, 2019 12:56
Show Gist options
  • Save Happy-Ferret/158ad4acfd267807758548e2da6b6142 to your computer and use it in GitHub Desktop.
Save Happy-Ferret/158ad4acfd267807758548e2da6b6142 to your computer and use it in GitHub Desktop.
SDL Samples in Go SDL
package main
import (
"github.com/banthar/Go-SDL/sdl"
)
const (
SCREEN_WIDTH int = 640
SCREEN_HEIGHT int = 480
SCREEN_BPP int = 32
)
var (
dots *sdl.Surface
screen *sdl.Surface
clip []sdl.Rect
)
func loadImage(filename string) *sdl.Surface {
var loadedImage *sdl.Surface
var optimizedImage *sdl.Surface
loadedImage = sdl.Load(filename)
if loadedImage != nil {
optimizedImage = sdl.DisplayFormat(loadedImage)
loadedImage.Free()
colorKey := sdl.MapRGB(optimizedImage.Format, 0, 0xFF, 0xFF)
optimizedImage.SetColorKey(sdl.SRCCOLORKEY, colorKey)
}
return optimizedImage
}
func applySurface(x, y int16, src, dest *sdl.Surface, clip *sdl.Rect) {
offset := new(sdl.Rect)
offset.X = x
offset.Y = y
dest.Blit(offset, src, clip)
}
func initialise() bool {
if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
return false
}
screen = sdl.SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, sdl.SWSURFACE)
if screen == nil {
return false
}
sdl.WM_SetCaption("Event Test", "")
return true
}
func loadFiles() bool {
dots = loadImage("dots.png")
if dots == nil {
return false
}
return true
}
func cleanUp() {
dots.Free()
sdl.Quit()
}
func main() {
quit := false
if !initialise() || !loadFiles() {
panic(sdl.GetError())
}
clip = make([]sdl.Rect, 4)
//Clip range for the top left
clip[0].X = 0
clip[0].Y = 0
clip[0].W = 100
clip[0].H = 100
//Clip range for the top right
clip[1].X = 100
clip[1].Y = 0
clip[1].W = 100
clip[1].H = 100
//Clip range for the bottom left
clip[2].X = 0
clip[2].Y = 100
clip[2].W = 100
clip[2].H = 100
//Clip range for the bottom right
clip[3].X = 100
clip[3].Y = 100
clip[3].W = 100
clip[3].H = 100
screen.FillRect(&screen.Clip_rect, sdl.MapRGB(screen.Format, 0xFF, 0xFF, 0xFF))
applySurface(0, 0, dots, screen, &clip[0])
applySurface(540, 0, dots, screen, &clip[1])
applySurface(0, 380, dots, screen, &clip[2])
applySurface(540, 380, dots, screen, &clip[3])
if screen.Flip() == -1 {
panic(sdl.GetError())
}
for !quit {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) {
case *sdl.QuitEvent:
quit = true
break
}
}
}
cleanUp()
}
package main
import (
"github.com/banthar/Go-SDL/sdl"
)
const (
SCREEN_WIDTH int = 640
SCREEN_HEIGHT int = 480
SCREEN_BPP int = 32
)
var (
foo *sdl.Surface
background *sdl.Surface
screen *sdl.Surface
)
func loadImage(filename string) *sdl.Surface {
var loadedImage *sdl.Surface
var optimizedImage *sdl.Surface
loadedImage = sdl.Load(filename)
if loadedImage != nil {
optimizedImage = sdl.DisplayFormat(loadedImage)
loadedImage.Free()
colorKey := sdl.MapRGB(optimizedImage.Format, 0, 0xFF, 0xFF)
optimizedImage.SetColorKey(sdl.SRCCOLORKEY, colorKey)
}
return optimizedImage
}
func applySurface(x, y int16, src, dest *sdl.Surface) {
offset := new(sdl.Rect)
offset.X = x
offset.Y = y
dest.Blit(offset, src, nil)
}
func initialise() bool {
if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
return false
}
screen = sdl.SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, sdl.SWSURFACE)
if screen == nil {
return false
}
sdl.WM_SetCaption("Event Test", "")
return true
}
func loadFiles() bool {
background = loadImage("background.png")
if background == nil {
return false
}
foo = loadImage("foo.png")
if foo == nil {
return false
}
return true
}
func cleanUp() {
background.Free()
foo.Free()
sdl.Quit()
}
func main() {
quit := false
if !initialise() || !loadFiles() {
panic(sdl.GetError())
}
applySurface(0, 0, background, screen)
applySurface(240, 190, foo, screen)
if screen.Flip() == -1 {
panic(sdl.GetError())
}
for !quit {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) {
case *sdl.QuitEvent:
quit = true
break
}
}
}
cleanUp()
}
package main
import (
"github.com/banthar/Go-SDL/sdl"
)
const (
SCREEN_WIDTH int = 640
SCREEN_HEIGHT int = 480
SCREEN_BPP int = 32
)
var (
image *sdl.Surface
screen *sdl.Surface
)
func loadImage(filename string) *sdl.Surface {
var loadedImage *sdl.Surface
var optimizedImage *sdl.Surface
loadedImage = sdl.Load(filename)
if loadedImage != nil {
optimizedImage = sdl.DisplayFormat(loadedImage)
loadedImage.Free()
}
return optimizedImage
}
func applySurface(x, y int16, src, dest *sdl.Surface) {
offset := new(sdl.Rect)
offset.X = x
offset.Y = y
offset.W = uint16(src.W)
offset.H = uint16(src.H)
dest.Blit(offset, src, nil)
}
func initialise() bool {
if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
return false
}
screen = sdl.SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, sdl.SWSURFACE)
if screen == nil {
return false
}
sdl.WM_SetCaption("Event Test", "")
return true
}
func loadFiles() bool {
image = loadImage("x.png")
if image == nil {
return false
}
return true
}
func cleanUp() {
image.Free()
sdl.Quit()
}
func main() {
quit := false
if !initialise() || !loadFiles() {
panic(sdl.GetError())
}
applySurface(0, 0, image, screen)
if screen.Flip() == -1 {
panic(sdl.GetError())
}
for !quit {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) {
case *sdl.QuitEvent:
quit = true
break
}
}
}
cleanUp()
}
package main
import (
"github.com/banthar/Go-SDL/sdl"
"github.com/banthar/Go-SDL/ttf"
)
const (
SCREEN_WIDTH int = 640
SCREEN_HEIGHT int = 480
SCREEN_BPP int = 32
)
var (
background *sdl.Surface
message *sdl.Surface
screen *sdl.Surface
font *ttf.Font
color *sdl.Color
)
func loadImage(filename string) *sdl.Surface {
var loadedImage *sdl.Surface
var optimizedImage *sdl.Surface
loadedImage = sdl.Load(filename)
if loadedImage != nil {
optimizedImage = sdl.DisplayFormat(loadedImage)
loadedImage.Free()
colorKey := sdl.MapRGB(optimizedImage.Format, 0, 0xFF, 0xFF)
optimizedImage.SetColorKey(sdl.SRCCOLORKEY, colorKey)
}
return optimizedImage
}
func applySurface(x, y int16, src, dest *sdl.Surface, clip *sdl.Rect) {
offset := new(sdl.Rect)
offset.X = x
offset.Y = y
dest.Blit(offset, src, clip)
}
func initialise() bool {
if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
return false
}
screen = sdl.SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, sdl.SWSURFACE)
if screen == nil {
return false
}
if ttf.Init() != 0 {
return false
}
sdl.WM_SetCaption("Event Test", "")
return true
}
func loadFiles() bool {
background = loadImage("background.png")
font = ttf.OpenFont("lazy.ttf", 28)
if background == nil {
return false
}
if font == nil {
return false
}
return true
}
func cleanUp() {
background.Free()
message.Free()
font.Close()
ttf.Quit()
sdl.Quit()
}
func main() {
quit := false
if !initialise() || !loadFiles() {
panic(sdl.GetError())
}
screen.FillRect(&screen.Clip_rect, sdl.MapRGB(screen.Format, 0xFF, 0xFF, 0xFF))
color := sdl.Color{R: 255, G: 255, B: 255}
message = ttf.RenderText_Solid(font, "The quick brown fox jumps over the lay dog.", color)
if message == nil {
panic(sdl.GetError())
}
applySurface(0, 0, background, screen, nil)
applySurface(0, 150, message, screen, nil)
if screen.Flip() == -1 {
panic(sdl.GetError())
}
for !quit {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) {
case *sdl.QuitEvent:
quit = true
break
}
}
}
cleanUp()
}
package main
import (
"github.com/banthar/Go-SDL/sdl"
"strconv"
)
const (
SCREEN_WIDTH int = 640
SCREEN_HEIGHT int = 480
SCREEN_BPP int = 32
)
var (
image *sdl.Surface
screen *sdl.Surface
)
type Timer struct {
startTicks uint32
pausedTicks uint32
paused bool
started bool
}
func NewTimer() *Timer {
return &Timer{
startTicks: 0,
pausedTicks: 0,
paused: false,
started: false,
}
}
func (t *Timer) Start() {
t.started = true
t.paused = false
t.startTicks = sdl.GetTicks()
}
func (t *Timer) Stop() {
t.started = false
t.paused = false
}
func (t *Timer) Pause() {
if t.started && !t.paused {
t.paused = true
t.pausedTicks = sdl.GetTicks() - t.startTicks
}
}
func (t *Timer) Unpause() {
if t.paused {
t.paused = false
t.startTicks = sdl.GetTicks() - t.pausedTicks
t.pausedTicks = 0
}
}
func (t *Timer) GetTicks() uint32 {
if t.started {
if t.paused {
return t.pausedTicks
}
return sdl.GetTicks() - t.startTicks
}
return 0
}
func (t *Timer) IsStarted() bool { return t.started }
func (t *Timer) IsPaused() bool { return t.paused }
func LoadImage(filename string) *sdl.Surface {
var loadedImage *sdl.Surface
var optimizedImage *sdl.Surface
loadedImage = sdl.Load(filename)
if loadedImage != nil {
optimizedImage = sdl.DisplayFormat(loadedImage)
loadedImage.Free()
colorKey := sdl.MapRGB(optimizedImage.Format, 0, 0xFF, 0xFF)
optimizedImage.SetColorKey(sdl.SRCCOLORKEY, colorKey)
}
return optimizedImage
}
func ApplySurface(x, y int16, source, destination *sdl.Surface, clip *sdl.Rect) {
offset := new(sdl.Rect)
offset.X = x
offset.Y = y
destination.Blit(offset, source, clip)
}
func initialise() bool {
if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
return false
}
screen = sdl.SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, sdl.SWSURFACE)
if screen == nil {
return false
}
sdl.WM_SetCaption("SDL Foo", "")
return true
}
func loadFiles() bool {
image = LoadImage("testing.png")
if image == nil {
return false
}
return true
}
func cleanUp() {
screen.Free()
image.Free()
sdl.Quit()
}
func main() {
var quit bool = false
var frame uint32 = 0
var fps *Timer = NewTimer()
var update *Timer = NewTimer()
if !initialise() || !loadFiles() {
panic(sdl.GetError())
}
update.Start()
fps.Start()
for !quit {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) {
case *sdl.QuitEvent:
quit = true
break
}
}
ApplySurface(0, 0, image, screen, nil)
if screen.Flip() == -1 {
panic(sdl.GetError())
}
frame += 1
if update.GetTicks() > 1000 {
caption := "Average Frames Per Second "
caption += strconv.Itoa(int(frame / (fps.GetTicks() / 1000.0)))
caption += " fps"
sdl.WM_SetCaption(caption, "")
update.Start()
}
}
cleanUp()
}
package main
import (
"github.com/banthar/Go-SDL/sdl"
)
func main() {
if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
panic(sdl.GetError())
}
var (
screen *sdl.Surface
hello *sdl.Surface
)
screen = sdl.SetVideoMode(640, 480, 32, sdl.RESIZABLE)
if screen == nil {
panic(sdl.GetError())
}
hello = sdl.Load("hello.bmp")
if hello == nil {
panic(sdl.GetError())
}
screen.Blit(nil, hello, nil)
screen.Flip()
sdl.Delay(2000)
hello.Free()
sdl.Quit()
}
package main
import (
"github.com/banthar/Go-SDL/sdl"
"github.com/banthar/Go-SDL/ttf"
)
const (
SCREEN_WIDTH int = 640
SCREEN_HEIGHT int = 480
SCREEN_BPP int = 32
)
var (
background *sdl.Surface
message *sdl.Surface
upMessage *sdl.Surface
downMessage *sdl.Surface
leftMessage *sdl.Surface
rightMessage *sdl.Surface
screen *sdl.Surface
font *ttf.Font
color *sdl.Color
)
func loadImage(filename string) *sdl.Surface {
var loadedImage *sdl.Surface
var optimizedImage *sdl.Surface
loadedImage = sdl.Load(filename)
if loadedImage != nil {
optimizedImage = sdl.DisplayFormat(loadedImage)
loadedImage.Free()
colorKey := sdl.MapRGB(optimizedImage.Format, 0, 0xFF, 0xFF)
optimizedImage.SetColorKey(sdl.SRCCOLORKEY, colorKey)
}
return optimizedImage
}
func applySurface(x, y int16, src, dest *sdl.Surface, clip *sdl.Rect) {
offset := new(sdl.Rect)
offset.X = x
offset.Y = y
dest.Blit(offset, src, clip)
}
func initialise() bool {
if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
return false
}
screen = sdl.SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, sdl.SWSURFACE)
if screen == nil {
return false
}
if ttf.Init() != 0 {
return false
}
sdl.WM_SetCaption("Event Test", "")
return true
}
func loadFiles() bool {
background = loadImage("background.png")
font = ttf.OpenFont("lazy.ttf", 28)
if background == nil {
return false
}
if font == nil {
return false
}
return true
}
func cleanUp() {
background.Free()
message.Free()
upMessage.Free()
downMessage.Free()
leftMessage.Free()
rightMessage.Free()
font.Close()
ttf.Quit()
sdl.Quit()
}
func main() {
quit := false
if !initialise() || !loadFiles() {
panic(sdl.GetError())
}
screen.FillRect(&screen.Clip_rect, sdl.MapRGB(screen.Format, 0xFF, 0xFF, 0xFF))
color := sdl.Color{R: 255, G: 255, B: 255}
upMessage = ttf.RenderText_Solid(font, "Up was pressed", color)
downMessage = ttf.RenderText_Solid(font, "Down was pressed", color)
leftMessage = ttf.RenderText_Solid(font, "Left was pressed", color)
rightMessage = ttf.RenderText_Solid(font, "Right was pressed", color)
applySurface(0, 0, background, screen, nil)
if screen.Flip() == -1 {
panic(sdl.GetError())
}
for !quit {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch e := event.(type) {
case *sdl.KeyboardEvent:
switch e.Keysym.Sym {
case sdl.K_UP:
message = upMessage
break
case sdl.K_DOWN:
message = downMessage
break
case sdl.K_LEFT:
message = leftMessage
break
case sdl.K_RIGHT:
message = rightMessage
break
}
if message != nil {
applySurface(0, 0, background, screen, nil)
applySurface(int16((SCREEN_WIDTH - int(message.W)) / 2), int16((SCREEN_HEIGHT - int(message.H)) / 2), message, screen, nil)
message = nil
}
if screen.Flip() == -1 {
panic(sdl.GetError())
}
break
case *sdl.QuitEvent:
quit = true
break
}
}
}
cleanUp()
}
package main
import (
"github.com/banthar/Go-SDL/sdl"
)
const (
SCREEN_WIDTH int = 640
SCREEN_HEIGHT int = 480
SCREEN_BPP int = 32
)
var (
message *sdl.Surface
background *sdl.Surface
screen *sdl.Surface
)
func loadImage(filename string) *sdl.Surface {
var loadedImage *sdl.Surface
var optimizedImage *sdl.Surface
loadedImage = sdl.Load(filename)
if loadedImage != nil {
optimizedImage = sdl.DisplayFormat(loadedImage)
loadedImage.Free()
}
return optimizedImage
}
func applySurface(x, y int16, src, dest *sdl.Surface) {
offset := new(sdl.Rect)
offset.X = x
offset.Y = y
offset.W = uint16(src.W)
offset.H = uint16(src.H)
dest.Blit(offset, src, nil)
}
func main() {
if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
panic(sdl.GetError())
}
screen = sdl.SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, sdl.SWSURFACE)
if screen == nil {
panic(sdl.GetError())
}
sdl.WM_SetCaption("Hello, World!", "")
message = loadImage("hello.bmp")
background = loadImage("background.bmp")
applySurface(0, 0, background, screen)
applySurface(320, 0, background, screen)
applySurface(0, 240, background, screen)
applySurface(320, 240, background, screen)
applySurface(180, 140, message, screen)
screen.Flip()
sdl.Delay(2000)
message.Free()
background.Free()
sdl.Quit()
}
@Happy-Ferret
Copy link
Author

package main

import (
"fmt"
"time"

"github.com/veandco/go-sdl2/sdl"

)

const (
windowWidth = 640
windowHeight = 480
)

func main() {

var app Application
deltaTime := time.Now()
//ticker := time.NewTicker(time.Second / 60)

app.Setup("SDL2 Framework", sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED,
	windowWidth, windowHeight, false)

//Main Loop
for app.running {
	app.HandleEvents()
	app.Update()
	app.Render()

	fmt.Println(time.Since(deltaTime))
	deltaTime = time.Now()
	//<-ticker.C // limit framerate
}

//os.Exit(0)
app.Shutdown()

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment