func mainLoop(width, height int) { // SDL window must be created in the same thread where the events are // polled. Hence this stuff must be in a separate goroutine along with the // event loop. initFlags := int64(C.SDL_INIT_VIDEO) | int64(C.SDL_INIT_AUDIO) screenFlags := 0 if C.SDL_Init(C.Uint32(initFlags)) == C.int(-1) { panic(getError()) } screen := C.SDL_SetVideoMode( C.int(width), C.int(height), 32, C.Uint32(screenFlags)) if screen == nil { panic(getError()) } C.SDL_EnableUNICODE(1) C.SDL_EnableKeyRepeat(C.SDL_DEFAULT_REPEAT_DELAY, C.SDL_DEFAULT_REPEAT_INTERVAL) initAudio() // Synchronize with Run function. coord <- true eventLoop() C.SDL_Quit() // Synchronize with Stop function. coord <- true runLevel = off }
func MasksToPixelFormatEnum(bpp int, rm, gm, bm, am uint32) (uint32, error) { f := C.SDL_MasksToPixelFormatEnum(C.int(bpp), C.Uint32(rm), C.Uint32(gm), C.Uint32(bm), C.Uint32(am)) if f == PIXELFORMAT_UNKNOWN { return 0, getError() } return uint32(f), nil }
func (s *Surface) ConvertFormat(pf uint32, flags uint32) (*Surface, error) { cs := C.SDL_ConvertSurfaceFormat(s.c(), C.Uint32(pf), C.Uint32(flags)) if cs == nil { return nil, getError() } return (*Surface)(unsafe.Pointer(cs)), nil }
func (this Surface) UpdateRect(rect Rect) { c_surface := (*C.SDL_Surface)(this.Ptr) c_x := C.Sint32(rect.X) c_y := C.Sint32(rect.Y) c_w := C.Uint32(rect.W) c_h := C.Uint32(rect.H) C.SDL_UpdateRect(c_surface, c_x, c_y, c_w, c_h) }
// Makes sure the given area is updated on the given screen. If x, y, w, and // h are all 0, the whole screen will be updated. func (screen *Surface) UpdateRect(x int32, y int32, w uint32, h uint32) { GlobalMutex.Lock() screen.mutex.Lock() C.SDL_UpdateRect(screen.cSurface, C.Sint32(x), C.Sint32(y), C.Uint32(w), C.Uint32(h)) screen.mutex.Unlock() GlobalMutex.Unlock() }
// Creates an empty Surface. func CreateRGBSurface(flags uint32, width int, height int, bpp int, Rmask uint32, Gmask uint32, Bmask uint32, Amask uint32) *Surface { GlobalMutex.Lock() p := C.SDL_CreateRGBSurface(C.Uint32(flags), C.int(width), C.int(height), C.int(bpp), C.Uint32(Rmask), C.Uint32(Gmask), C.Uint32(Bmask), C.Uint32(Amask)) GlobalMutex.Unlock() return wrap(p) }
func CreateRGBSurface(flags uint32, width, height, depth int32, Rmask, Gmask, Bmask, Amask uint32) *Surface { return (*Surface)(unsafe.Pointer(C.SDL_CreateRGBSurface(C.Uint32(flags), C.int(width), C.int(height), C.int(depth), C.Uint32(Rmask), C.Uint32(Gmask), C.Uint32(Bmask), C.Uint32(Amask)))) }
func CreateRGBSurfaceFrom(pixels unsafe.Pointer, width, height, depth, pitch int, Rmask, Gmask, Bmask, Amask uint32) *Surface { return (*Surface)(unsafe.Pointer(C.SDL_CreateRGBSurfaceFrom(pixels, C.int(width), C.int(height), C.int(depth), C.int(pitch), C.Uint32(Rmask), C.Uint32(Gmask), C.Uint32(Bmask), C.Uint32(Amask)))) }
// Initializes SDL. func Init(flags uint32) int { status := int(C.SDL_Init(C.Uint32(flags))) if (status != 0) && (runtime.GOOS == "darwin") && (flags&INIT_VIDEO != 0) { if os.Getenv("SDL_VIDEODRIVER") == "" { os.Setenv("SDL_VIDEODRIVER", "x11") status = int(C.SDL_Init(C.Uint32(flags))) if status != 0 { os.Setenv("SDL_VIDEODRIVER", "") } } } return status }
// CreateRGBSurface (https://wiki.libsdl.org/SDL_CreateRGBSurface) func CreateRGBSurface(flags uint32, width, height, depth int32, Rmask, Gmask, Bmask, Amask uint32) (*Surface, error) { surface := (*Surface)(unsafe.Pointer(C.SDL_CreateRGBSurface( C.Uint32(flags), C.int(width), C.int(height), C.int(depth), C.Uint32(Rmask), C.Uint32(Gmask), C.Uint32(Bmask), C.Uint32(Amask)))) if surface == nil { return nil, GetError() } return surface, nil }
// Initializes subsystems. func InitSubSystem(flags uint32) int { GlobalMutex.Lock() status := int(C.SDL_InitSubSystem(C.Uint32(flags))) if (status != 0) && (runtime.GOOS == "darwin") && (flags&INIT_VIDEO != 0) { if os.Getenv("SDL_VIDEODRIVER") == "" { os.Setenv("SDL_VIDEODRIVER", "x11") status = int(C.SDL_InitSubSystem(C.Uint32(flags))) if status != 0 { os.Setenv("SDL_VIDEODRIVER", "") } } } GlobalMutex.Unlock() return status }
func FilledCircleColor(renderer *sdl.Renderer, x, y, rad int, color sdl.Color) bool { _x := C.Sint16(x) _y := C.Sint16(y) _rad := C.Sint16(rad) _color := C.Uint32(gfxColor(color)) return C.filledCircleColor(renderer, _x, _y, _rad, _color) == 0 }
// AllocFormat creates a PixelFormat structure from a uint. // AllocFormat (https://wiki.libsdl.org/SDL_AllocFormat) func AllocFormat(format uint) (*PixelFormat, error) { r := (*PixelFormat)(unsafe.Pointer(C.SDL_AllocFormat(C.Uint32(format)))) if r == nil { return nil, GetError() } return r, nil }
func StringColor(renderer *sdl.Renderer, x, y int, s string, color sdl.Color) bool { _x := C.Sint16(x) _y := C.Sint16(y) _s := C.CString(s) _color := C.Uint32(gfxColor(color)) return C.stringColor(renderer, _x, _y, _s, _color) == 0 }
func HlineColor(renderer *sdl.Renderer, x1, x2, y int, color sdl.Color) bool { _x1 := C.Sint16(x1) _x2 := C.Sint16(x2) _y := C.Sint16(y) _color := C.Uint32(gfxColor(color)) return C.hlineColor(renderer, _x1, _x2, _y, _color) == 0 }
func InitSubSystem(flags uint32) error { if C.SDL_InitSubSystem(C.Uint32(flags)) != 0 { return getError() } return nil }
// NewWindow returns a new window. func NewWindow(title string, w, h int) *Window { win := &Window{ events: make(chan interface{}, eventChanSize), imgs: make(map[string]texture), } do(func() { ctitle := C.CString(title) defer C.free(unsafe.Pointer(ctitle)) x, y := C.SDL_WINDOWPOS_UNDEFINED, C.SDL_WINDOWPOS_UNDEFINED flags := C.SDL_WINDOW_SHOWN | C.SDL_WINDOW_OPENGL win.win = C.SDL_CreateWindow(ctitle, C.int(x), C.int(y), C.int(w), C.int(h), C.Uint32(flags)) if win.win == nil { panic(sdlError()) } win.rend = C.SDL_CreateRenderer(win.win, 0, C.SDL_RENDERER_ACCELERATED) if win.rend == nil { panic(sdlError()) } if C.SDL_SetRenderDrawBlendMode(win.rend, C.SDL_BLENDMODE_BLEND) < 0 { panic(sdlError()) } win.id = windowID(C.SDL_GetWindowID(win.win)) windows[win.id] = win }) return win }
// CreateSemaphore (https://wiki.libsdl.org/SDL_CreateSemaphore) func CreateSemaphore(initialValue uint32) (*Sem, error) { sem := C.SDL_CreateSemaphore(C.Uint32(initialValue)) if sem == nil { return nil, GetError() } return (*Sem)(unsafe.Pointer(sem)), nil }
// Sets up a video mode with the specified width, height, bits-per-pixel and // returns a corresponding surface. You don't need to call the Free method // of the returned surface, as it will be done automatically by sdl.Quit. func SetVideoMode(w int, h int, bpp int, flags uint32) *Surface { GlobalMutex.Lock() var screen = C.SDL_SetVideoMode(C.int(w), C.int(h), C.int(bpp), C.Uint32(flags)) currentVideoSurface = wrap(screen) GlobalMutex.Unlock() return currentVideoSurface }
// CreateRenderer (https://wiki.libsdl.org/SDL_CreateRenderer) func CreateRenderer(window *Window, index int, flags uint32) (*Renderer, error) { _renderer := C.SDL_CreateRenderer(window.cptr(), C.int(index), C.Uint32(flags)) if _renderer == nil { return nil, GetError() } return (*Renderer)(unsafe.Pointer(_renderer)), nil }
// Renderer (https://wiki.libsdl.org/SDL_RenderReadPixels) func (renderer *Renderer) ReadPixels(rect *Rect, format uint32, pixels unsafe.Pointer, pitch int) error { _ret := C.SDL_RenderReadPixels(renderer.cptr(), rect.cptr(), C.Uint32(format), pixels, C.int(pitch)) if _ret < 0 { return GetError() } return nil }
func (win *Window) SetFullscreen(flags WindowFlags) error { if C.SDL_SetWindowFullscreen(win.c, C.Uint32(flags)) != 0 { return getError() } return nil }
func (w *Window) ShowSimpleMessageBox(flags uint32, title, message string) { ctitle, cmessage := C.CString(title), C.CString(message) C.SDL_ShowSimpleMessageBox(C.Uint32(flags), ctitle, cmessage, w.cWindow) C.free(unsafe.Pointer(ctitle)) C.free(unsafe.Pointer(cmessage)) }
func CreateWindow(title string, x int, y int, w int, h int, flags uint32) *Window { _title := C.CString(title) defer C.free(unsafe.Pointer(_title)) var window = C.SDL_CreateWindow(_title, C.int(x), C.int(y), C.int(w), C.int(h), C.Uint32(flags)) return (*Window)(unsafe.Pointer(window)) }
func SetVideoMode(width int, height int, bpp int, flags int) { screen := C.SDL_SetVideoMode(C.int(width), C.int(height), C.int(bpp), C.Uint32(flags)) if screen == nil { panic("unable to set video mode") } }
func FilledPolygonColor(renderer *sdl.Renderer, vx, vy []int16, color sdl.Color) bool { _vx := (*C.Sint16)(unsafe.Pointer(&vx[0])) _vy := (*C.Sint16)(unsafe.Pointer(&vy[0])) _len := C.int(min(len(vx), len(vy))) _color := C.Uint32(gfxColor(color)) return C.filledPolygonColor(renderer, _vx, _vy, _len, _color) == 0 }
// Returns the list of available screen dimensions for the given format. // // NOTE: The result of this function uses a different encoding than the underlying C function. // It returns an empty array if no modes are available, // and nil if any dimension is okay for the given format. func ListModes(format *PixelFormat, flags uint32) []Rect { modes := C.SDL_ListModes((*C.SDL_PixelFormat)(cast(format)), C.Uint32(flags)) // No modes available if modes == nil { return make([]Rect, 0) } // (modes == -1) --> Any dimension is ok if uintptr(unsafe.Pointer(modes))+1 == uintptr(0) { return nil } count := 0 ptr := *modes //first element in the list for ptr != nil { count++ ptr = *(**C.SDL_Rect)(unsafe.Pointer(uintptr(unsafe.Pointer(modes)) + uintptr(count*unsafe.Sizeof(ptr)))) } ret := make([]Rect, count) for i := 0; i < count; i++ { ptr := (**C.SDL_Rect)(unsafe.Pointer(uintptr(unsafe.Pointer(modes)) + uintptr(i*unsafe.Sizeof(*modes)))) var r *C.SDL_Rect = *ptr ret[i].X = int16(r.x) ret[i].Y = int16(r.y) ret[i].W = uint16(r.w) ret[i].H = uint16(r.h) } return ret }
func CharacterColor(renderer *sdl.Renderer, x, y int, c byte, color sdl.Color) bool { _x := C.Sint16(x) _y := C.Sint16(y) _c := C.char(c) _color := C.Uint32(gfxColor(color)) return C.characterColor(renderer, _x, _y, _c, _color) == 0 }
func VlineColor(renderer *sdl.Renderer, x, y1, y2 int, color sdl.Color) bool { _x := C.Sint16(x) _y1 := C.Sint16(y1) _y2 := C.Sint16(y2) _color := C.Uint32(gfxColor(color)) return C.vlineColor(renderer, _x, _y1, _y2, _color) == 0 }
// GetWindowFromID (https://wiki.libsdl.org/SDL_GetWindowFromID) func GetWindowFromID(id uint32) (*Window, error) { _window := C.SDL_GetWindowFromID(C.Uint32(id)) if _window == nil { return nil, GetError() } return (*Window)(unsafe.Pointer((_window))), nil }