Created
November 11, 2009 16:29
-
-
Save banthar/232088 to your computer and use it in GitHub Desktop.
Go SDL bindings (new version here: http://github.com/banthar/Go-SDL)
This file contains hidden or 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
**.o | |
**.8 | |
_obj/ | |
main | |
**.cgo?.go | |
**.cgo?.c | |
**.cgo?.8 | |
sdl_defs.go | |
**~ |
This file contains hidden or 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 "sdl" | |
//import "math" | |
import "fmt" | |
import "image/png"; | |
import "os" | |
func main() { | |
sdl.Init(sdl.INIT_VIDEO); | |
var screen = sdl.SetVideoMode(640, 480, 32, 0); | |
var image = sdl.Load("test.png"); | |
var running = true; | |
var x, y int16; | |
for running { | |
x++; | |
y++; | |
e := &sdl.Event{}; | |
for (e.Poll()) | |
{ | |
switch(e.Type) | |
{ | |
case sdl.QUIT: | |
file,err := os.Open("shoot.png",os.O_CREATE|os.O_WRONLY,0766); | |
println(err); | |
png.Encode(file, screen); | |
file.Close(); | |
running = false; | |
break; | |
case sdl.KEYDOWN: | |
println(e.Keyboard().Keysym.Sym); | |
case sdl.MOUSEBUTTONDOWN: | |
println("Click:",e.MouseButton().X,e.MouseButton().Y); | |
x=int16(e.MouseButton().X); | |
y=int16(e.MouseButton().Y); | |
default: | |
fmt.Printf("Event: %08x\n",e.Type); | |
} | |
} | |
screen.FillRect(nil, 0x302019); | |
screen.Blit(&sdl.Rect{x, y, 0, 0}, image, nil); | |
screen.Flip(); | |
sdl.Delay(25); | |
} | |
image.Free(); | |
screen.Free(); | |
sdl.Quit(); | |
} |
This file contains hidden or 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
# Copyright 2009 The Go Authors. All rights reserved. | |
# Use of this source code is governed by a BSD-style | |
# license that can be found in the LICENSE file. | |
include $(GOROOT)/src/Make.$(GOARCH) | |
TARG=sdl | |
GOFILES:=sdl_defs.go | |
CGOFILES:=sdl.go | |
CGO_LDFLAGS:=`pkg-config --libs sdl` -lSDL_image | |
CGO_CFLAGS:=`pkg-config --cflags sdl` | |
CLEANFILES+=sdl | |
include $(GOROOT)/src/Make.pkg | |
sdl_defs.go: sdl.c | |
godefs -g sdl sdl.c > sdl_defs.go | |
main: main.go install | |
$(GC) main.go | |
$(LD) -o $@ main.$(O) | |
This file contains hidden or 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
#include <SDL/SDL.h> | |
// for i in `grep '#define SDL_.*0x' -r SDL_video.h | sed 's/\s/\n/g' | grep SDL_ | sed s/SDL_//` ; do echo -e "\t\$$i = SDL_$i," ; done | |
typedef SDL_Surface $Surface; | |
typedef SDL_PixelFormat $PixelFormat; | |
typedef SDL_Rect $Rect; | |
typedef SDL_Color $Color; | |
typedef SDL_Palette $Palette; | |
typedef SDL_VideoInfo $VideoInfo; | |
typedef SDL_Overlay $Overlay; | |
typedef SDL_ActiveEvent $ActiveEvent; | |
typedef SDL_KeyboardEvent $KeyboardEvent; | |
typedef SDL_MouseMotionEvent $MouseMotionEvent; | |
typedef SDL_MouseButtonEvent $MouseButtonEvent; | |
typedef SDL_JoyAxisEvent $JoyAxisEvent; | |
typedef SDL_JoyBallEvent $JoyBallEvent; | |
typedef SDL_JoyHatEvent $JoyHatEvent; | |
typedef SDL_JoyButtonEvent $JoyButtonEvent; | |
typedef SDL_ResizeEvent $ResizeEvent; | |
typedef SDL_ExposeEvent $ExposeEvent; | |
typedef SDL_QuitEvent $QuitEvent; | |
typedef SDL_UserEvent $UserEvent; | |
typedef SDL_SysWMmsg $SysWMmsg; | |
typedef SDL_SysWMEvent $SysWMEvent; | |
typedef SDL_Event $Event; | |
typedef SDL_keysym $Keysym; | |
enum | |
{ | |
// init flags | |
$INIT_AUDIO = SDL_INIT_AUDIO, | |
$INIT_VIDEO = SDL_INIT_VIDEO, | |
$INIT_CDROM = SDL_INIT_CDROM, | |
$INIT_JOYSTICK = SDL_INIT_JOYSTICK, | |
$INIT_NOPARACHUTE = SDL_INIT_NOPARACHUTE, | |
$INIT_EVENTTHREAD = SDL_INIT_EVENTTHREAD, | |
$INIT_EVERYTHING = SDL_INIT_EVERYTHING, | |
// setvideo flags | |
$SWSURFACE = SDL_SWSURFACE, | |
$HWSURFACE = SDL_HWSURFACE, | |
$ASYNCBLIT = SDL_ASYNCBLIT, | |
$ANYFORMAT = SDL_ANYFORMAT, | |
$HWPALETTE = SDL_HWPALETTE, | |
$DOUBLEBUF = SDL_DOUBLEBUF, | |
$FULLSCREEN = SDL_FULLSCREEN, | |
$OPENGL = SDL_OPENGL, | |
$OPENGLBLIT = SDL_OPENGLBLIT, | |
$RESIZABLE = SDL_RESIZABLE, | |
$NOFRAME = SDL_NOFRAME, | |
$HWACCEL = SDL_HWACCEL, | |
$SRCCOLORKEY = SDL_SRCCOLORKEY, | |
$RLEACCELOK = SDL_RLEACCELOK, | |
$RLEACCEL = SDL_RLEACCEL, | |
$SRCALPHA = SDL_SRCALPHA, | |
$PREALLOC = SDL_PREALLOC, | |
$YV12_OVERLAY = SDL_YV12_OVERLAY, | |
$IYUV_OVERLAY = SDL_IYUV_OVERLAY, | |
$YUY2_OVERLAY = SDL_YUY2_OVERLAY, | |
$UYVY_OVERLAY = SDL_UYVY_OVERLAY, | |
$YVYU_OVERLAY = SDL_YVYU_OVERLAY, | |
$LOGPAL = SDL_LOGPAL, | |
$PHYSPAL = SDL_PHYSPAL, | |
// event types | |
$NOEVENT = SDL_NOEVENT, | |
$ACTIVEEVENT = SDL_ACTIVEEVENT, | |
$KEYDOWN = SDL_KEYDOWN, | |
$KEYUP = SDL_KEYUP, | |
$MOUSEMOTION = SDL_MOUSEMOTION, | |
$MOUSEBUTTONDOWN = SDL_MOUSEBUTTONDOWN, | |
$MOUSEBUTTONUP = SDL_MOUSEBUTTONUP, | |
$JOYAXISMOTION = SDL_JOYAXISMOTION, | |
$JOYBALLMOTION = SDL_JOYBALLMOTION, | |
$JOYHATMOTION = SDL_JOYHATMOTION, | |
$JOYBUTTONDOWN = SDL_JOYBUTTONDOWN, | |
$JOYBUTTONUP = SDL_JOYBUTTONUP, | |
$QUIT = SDL_QUIT, | |
$SYSWMEVENT = SDL_SYSWMEVENT, | |
$EVENT_RESERVEDA = SDL_EVENT_RESERVEDA, | |
$EVENT_RESERVEDB = SDL_EVENT_RESERVEDB, | |
$VIDEORESIZE = SDL_VIDEORESIZE, | |
$VIDEOEXPOSE = SDL_VIDEOEXPOSE, | |
$EVENT_RESERVED2 = SDL_EVENT_RESERVED2, | |
$EVENT_RESERVED3 = SDL_EVENT_RESERVED3, | |
$EVENT_RESERVED4 = SDL_EVENT_RESERVED4, | |
$EVENT_RESERVED5 = SDL_EVENT_RESERVED5, | |
$EVENT_RESERVED6 = SDL_EVENT_RESERVED6, | |
$EVENT_RESERVED7 = SDL_EVENT_RESERVED7, | |
$USEREVENT = SDL_USEREVENT, | |
$NUMEVENTS = SDL_NUMEVENTS, | |
// event masks | |
$ACTIVEEVENTMASK = SDL_ACTIVEEVENTMASK, | |
$KEYDOWNMASK = SDL_KEYDOWNMASK, | |
$KEYUPMASK = SDL_KEYUPMASK, | |
$KEYEVENTMASK = SDL_KEYEVENTMASK, | |
$MOUSEMOTIONMASK = SDL_MOUSEMOTIONMASK, | |
$MOUSEBUTTONDOWNMASK = SDL_MOUSEBUTTONDOWNMASK, | |
$MOUSEBUTTONUPMASK = SDL_MOUSEBUTTONUPMASK, | |
$MOUSEEVENTMASK = SDL_MOUSEEVENTMASK, | |
$JOYAXISMOTIONMASK = SDL_JOYAXISMOTIONMASK, | |
$JOYBALLMOTIONMASK = SDL_JOYBALLMOTIONMASK, | |
$JOYHATMOTIONMASK = SDL_JOYHATMOTIONMASK, | |
$JOYBUTTONDOWNMASK = SDL_JOYBUTTONDOWNMASK, | |
$JOYBUTTONUPMASK = SDL_JOYBUTTONUPMASK, | |
$JOYEVENTMASK = SDL_JOYEVENTMASK, | |
$VIDEORESIZEMASK = SDL_VIDEORESIZEMASK, | |
$VIDEOEXPOSEMASK = SDL_VIDEOEXPOSEMASK, | |
$QUITMASK = SDL_QUITMASK, | |
$SYSWMEVENTMASK = SDL_SYSWMEVENTMASK, | |
}; | |
This file contains hidden or 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 sdl | |
// struct private_hwdata{}; | |
// struct SDL_BlitMap{}; | |
// #define map _map | |
// #include <SDL/SDL.h> | |
// #include <SDL/SDL_image.h> | |
import "C" | |
import "unsafe" | |
import "image" | |
//type Surface C.SDL_Surface; | |
type cast unsafe.Pointer | |
//SDL | |
func Init(flags uint32) int { return int(C.SDL_Init(C.Uint32(flags))) } | |
func Quit() { C.SDL_Quit() } | |
//SDL_video | |
func SetVideoMode(w int, h int, bpp int, flags uint32) *Surface { | |
var screen = C.SDL_SetVideoMode(C.int(w), C.int(h), C.int(bpp), C.Uint32(flags)); | |
return (*Surface)(cast(screen)); | |
} | |
func (screen *Surface) Flip() int { return int(C.SDL_Flip((*C.SDL_Surface)(cast(screen)))) } | |
func (screen *Surface) Free() { C.SDL_FreeSurface((*C.SDL_Surface)(cast(screen))) } | |
func (screen *Surface) Lock() int { | |
return int(C.SDL_LockSurface((*C.SDL_Surface)(cast(screen)))) | |
} | |
func (screen *Surface) Unlock() int { return int(C.SDL_Flip((*C.SDL_Surface)(cast(screen)))) } | |
func GetVideoSurface() *Surface { | |
var screen = C.SDL_GetVideoSurface(); | |
return (*Surface)(cast(screen)); | |
} | |
func (dst *Surface) Blit(dstrect *Rect, src *Surface, srcrect *Rect) int { | |
var ret = C.SDL_UpperBlit( | |
(*C.SDL_Surface)(cast(src)), | |
(*C.SDL_Rect)(cast(srcrect)), | |
(*C.SDL_Surface)(cast(dst)), | |
(*C.SDL_Rect)(cast(dstrect))); | |
return int(ret); | |
} | |
func (dst *Surface) FillRect(dstrect *Rect, color uint32) int { | |
var ret = C.SDL_FillRect( | |
(*C.SDL_Surface)(cast(dst)), | |
(*C.SDL_Rect)(cast(dstrect)), | |
C.Uint32(color)); | |
return int(ret); | |
} | |
// surface --> Image | |
func (surface *Surface) ColorModel() image.ColorModel | |
{ | |
return nil; | |
} | |
func (surface *Surface) Width() int | |
{ | |
return int(surface.W); | |
} | |
func (surface *Surface) Height() int | |
{ | |
return int(surface.H); | |
} | |
func (surface *Surface) At(x, y int) image.Color | |
{ | |
//TODO endianess, bpp, alpha, etc | |
var bpp = int(surface.Format.BytesPerPixel); | |
var pixel=uintptr(unsafe.Pointer(surface.Pixels)); | |
pixel += uintptr( y * int(surface.Pitch) + x * bpp); | |
var color= *((*image.RGBAColor)(unsafe.Pointer(pixel))); | |
color.A=255; | |
return color; | |
} | |
//SDL image | |
func Load(file string) *Surface { | |
var screen = C.IMG_Load(C.CString(file)); | |
return (*Surface)(cast(screen)); | |
} | |
//SDL events | |
func (event *Event) Wait() bool { | |
var ret = C.SDL_WaitEvent((*C.SDL_Event)(cast(event))); | |
return ret != 0; | |
} | |
func (event *Event) Poll() bool { | |
var ret = C.SDL_PollEvent((*C.SDL_Event)(cast(event))); | |
return ret != 0; | |
} | |
func (event *Event) Keyboard() *KeyboardEvent | |
{ | |
if(event.Type==KEYUP || event.Type==KEYDOWN) | |
{ | |
return (*KeyboardEvent)(cast(event)); | |
} | |
return nil; | |
} | |
func (event *Event) MouseButton() *MouseButtonEvent | |
{ | |
if(event.Type==MOUSEBUTTONDOWN || event.Type==MOUSEBUTTONUP) | |
{ | |
return (*MouseButtonEvent)(cast(event)); | |
} | |
return nil; | |
} | |
func (event *Event) MouseMotion() *MouseMotionEvent | |
{ | |
if(event.Type==MOUSEMOTION) | |
{ | |
return (*MouseMotionEvent)(cast(event)); | |
} | |
return nil; | |
} | |
//SDL time | |
func Delay(ms uint32) { C.SDL_Delay(C.Uint32(ms)) } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment