Skip to content

Instantly share code, notes, and snippets.

@Brom95
Last active June 2, 2024 05:37
Show Gist options
  • Save Brom95/cce762b97b64335c8434db21fb6b1ab0 to your computer and use it in GitHub Desktop.
Save Brom95/cce762b97b64335c8434db21fb6b1ab0 to your computer and use it in GitHub Desktop.
равномерный гардиент в окне 800 на 600 с использованием sdl2 и go
package main
import (
"unsafe"
"github.com/veandco/go-sdl2/sdl"
)
const (
winWidth int32 = 800
winHeight int32 = 600
)
type Color struct {
r, g, b byte
}
func setPixel(x, y int, color Color, pixels []byte) {
index := (y*int(winWidth) + x) * 4
if index > len(pixels)-4 || index < 0 {
return
}
pixels[index+3] = color.r
pixels[index+2] = color.g
pixels[index+1] = color.b
}
func main() {
window, err := sdl.CreateWindow("ИГРА!", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_SHOWN)
if err != nil {
panic(err)
}
defer window.Destroy()
renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
if err != nil {
panic(err)
}
defer renderer.Destroy()
tex, err := renderer.CreateTexture(sdl.PIXELFORMAT_RGBA8888, sdl.TEXTUREACCESS_STREAMING, winWidth, winHeight)
if err != nil {
panic(err)
}
defer tex.Destroy()
pixels := make([]byte, winWidth*winHeight*4)
yScaleFactor := (int(winHeight) + 255) / 255
xScaleFactor := (int(winWidth) + 255) / 255
for yFrame := 0; yFrame < int(winHeight); yFrame += yScaleFactor {
for y := 0; y < yScaleFactor; y++ {
for xFrame := 0; xFrame < int(winWidth); xFrame += xScaleFactor {
for x := 0; x < xScaleFactor; x++ {
setPixel(xFrame+x, yFrame+y, Color{r: byte(xFrame / xScaleFactor % 255), g: byte(yFrame / yScaleFactor % 255), b: byte(0)}, pixels)
}
}
}
}
tex.Update(nil, unsafe.Pointer(&pixels[0]), int(winWidth)*4)
renderer.Copy(tex, nil, nil)
renderer.Present()
sdl.Delay(20 * 1000)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment