Skip to content

Instantly share code, notes, and snippets.

@cuu
Created April 5, 2019 08:54
Show Gist options
  • Save cuu/d0505bbad6a7fd591560bbf9f4e0400a to your computer and use it in GitHub Desktop.
Save cuu/d0505bbad6a7fd591560bbf9f4e0400a to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
"github.com/veandco/go-sdl2/sdl"
"os"
)
var winTitle string = "Go-SDL2 Render"
const (
winWidth = 320
winHeight = 240
)
func run() int {
var window *sdl.Window
var renderer *sdl.Renderer
var event sdl.Event
window, err := sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
int32(winWidth), int32(winHeight), sdl.WINDOW_SHOWN)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err)
return 1
}
defer window.Destroy()
renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err)
return 2
}
defer renderer.Destroy()
texture,_ := renderer.CreateTexture(sdl.PIXELFORMAT_ARGB8888,sdl.TEXTUREACCESS_STATIC,winWidth,winHeight)
frames := 0
prev_time := sdl.GetTicks()
current_time := sdl.GetTicks()
fps := 0
color := 0
addr :=0
var pixels [ winWidth*winHeight ]byte
for y:=0;y<int(winHeight);y++ {
for x:=0;x<int(winWidth);x++ {
addr = x + y *int(winWidth)
pixels[addr] = byte( rand.Intn(255))
}
}
running := true
for running {
for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch t := event.(type) {
case *sdl.QuitEvent:
running = false
break
case *sdl.KeyboardEvent:
if t.Type == sdl.KEYDOWN {
if t.Keysym.Sym == sdl.K_q || t.Keysym.Sym == sdl.K_ESCAPE {
running = false
break
}
}
}
}
for y:=0;y<int(winHeight);y++ {
for x:=0;x<int(winWidth);x++ {
addr = x + y *int(winWidth)
color = rand.Int() % 255
pixels[addr] = byte( color)
}
}
texture.Update(nil,pixels[:],int(winWidth))
renderer.Clear()
renderer.Copy(texture, nil,nil)
renderer.Present()
frames+=1
current_time = sdl.GetTicks()
if current_time - prev_time > 10000 {
fps = frames / 10
fmt.Println("fps is ",fps)
frames = 0
prev_time = current_time
}
}
return 0
}
func main() {
os.Exit(run())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment