Created
April 5, 2019 08:54
-
-
Save cuu/d0505bbad6a7fd591560bbf9f4e0400a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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