Created
June 2, 2024 05:36
-
-
Save Brom95/910e19d9b19d89379dc1d59a81fc20d2 to your computer and use it in GitHub Desktop.
равномерный гардиент в окне 800 на 600 с использованием sdl2 и go, но уже на массиве
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 ( | |
"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 *[winWidth * winHeight * 4]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() | |
var pixels [winWidth * winHeight * 4]byte //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), 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