commit 26f18da0d8a615b24baf01e76c3b57eb85138078 Author: cuu Date: Tue Jun 5 23:56:48 2018 +0800 first commit diff --git a/gogame/color/color.go b/gogame/color/color.go new file mode 100644 index 0000000..aa8d3f2 --- /dev/null +++ b/gogame/color/color.go @@ -0,0 +1,36 @@ +package color + +//import "github.com/veandco/go-sdl2/sdl" + + +type Color struct { + R uint32 + G uint32 + B uint32 + A uint32 +} + +func (c *Color) ToHex() int { + return int( c.A<< 24 | c.R << 16 | c.G << 8 | c.B ) +} + +func (c *Color) ToBytes() []byte { + bytes := make([]byte,4) + bytes[0] = byte(c.R) + bytes[1] = byte(c.G) + bytes[2] = byte(c.B) + bytes[3] = byte(c.A) + return bytes +} + +func (c *Color) RGBA() (r, g, b, a uint32) { + r = uint32(c.R) + r |= r << 8 + g = uint32(c.G) + g |= g << 8 + b = uint32(c.B) + b |= b << 8 + a = uint32(c.A) + a |= a << 8 + return +} diff --git a/gogame/color/color_test.go b/gogame/color/color_test.go new file mode 100644 index 0000000..1955d26 --- /dev/null +++ b/gogame/color/color_test.go @@ -0,0 +1,14 @@ +package color + +// > go test color.go color_test.go -v +// Or +// > go test -v +// to test all test files + +import "testing" + +func TestColor(t *testing.T) { + c := &Color{244,124,244,0} + t.Logf("%x", c.ToHex()) +} + diff --git a/gogame/display/display.go b/gogame/display/display.go new file mode 100644 index 0000000..77b7b6c --- /dev/null +++ b/gogame/display/display.go @@ -0,0 +1,63 @@ +package display + +import ( + "github.com/veandco/go-sdl2/sdl" + "../../gogame" +) + +var Inited = false +var window *sdl.Window + +func AssertInited() { + if Inited == false { + panic("run gogame.DisplayInit first") + } +} + +func Init() bool { + sdl.Do(func() { + + if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil { + panic(err) + } + + Inited = true + }) + + return Inited +} + + +func SetMode(w,h,flags,depth int32) *sdl.Surface { + var err error + var surface *sdl.Surface + AssertInited() + + sdl.Do(func() { + window, err = sdl.CreateWindow("gogame", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, + w, h, uint32( gogame.SHOWN | flags)) + + if err != nil { + panic(err) + } + + surface,err = window.GetSurface() + if err != nil { + panic(err) + } + }) + + return surface +} + +func Flip() { + sdl.Do(func() { + + if window != nil { + window.UpdateSurface() + } + }) +} + + + diff --git a/gogame/draw/draw.go b/gogame/draw/draw.go new file mode 100644 index 0000000..2a95795f --- /dev/null +++ b/gogame/draw/draw.go @@ -0,0 +1,450 @@ +package draw + +import ( + "fmt" + "math" + "github.com/veandco/go-sdl2/sdl" + "../color" + +) + +const ( + LEFT_EDGE=0x1 + RIGHT_EDGE=0x2 + BOTTOM_EDGE=0x4 + TOP_EDGE=0x8 +) + +func Line(surf *sdl.Surface, col color.Color,x1,y1,x2,y2 ,width int) { + +} + +func Rect(surf *sdl.Surface,color color.Color, _rect *sdl.Rect, border_width uint32) { + color_hex := color.ToHex() + fmt.Printf("%x\n",color_hex) + +} + +func clip_and_draw_line(surf *sdl.Surface, rect *sdl.Rect, col color.Color, pts []int) int { + if clipline(pts, int(rect.X),int(rect.Y),int(rect.X+ rect.W-1), int(rect.Y+rect.H-1) ) == 0 { + return 0 + } + + if pts[1] == pts[3] { + drawhorzline(surf, col, pts[0],pts[1],pts[2]) + }else if pts[0] == pts[2] { + drawvertline(surf,col, pts[0],pts[1],pts[2]) + }else { + drawline(surf, col, pts[0],pts[1],pts[2],pts[3]) + } + + return 1 +} + + +func clip_and_draw_line_width(surf *sdl.Surface,rect *sdl.Rect,col color.Color, width int, pts []int) int { + loop := 0 + xinc :=0 + yinc :=0 + newpts :=make([]int,4) + range_ := make([]int,4) + anydraw := 0 + if abs(pts[0]-pts[2]) > abs(pts[1]-pts[3]) { + yinc = 1 + }else{ + xinc = 1 + } + copy(newpts, pts) + if clip_and_draw_line(surf,rect,col, newpts) > 0 { + anydraw = 1 + copy(range_,newpts) + }else { + range_[0] = 10000 + range_[1] = 10000 + range_[2] = -10000 + range_[3] = -10000 + } + + for loop = 1; loop < width; loop +=2 { + newpts[0] = pts[0] + xinc*(loop/2+1) + newpts[1] = pts[1] + yinc*(loop/2+1) + newpts[2] = pts[2] + xinc*(loop/2+1) + newpts[3] = pts[3] + yinc*(loop/2+1) + if clip_and_draw_line(surf,rect,col,newpts) > 0 { + anydraw = 1 + range_[0] = min(newpts[0],range_[0]) + range_[1] = min(newpts[1],range_[1]) + range_[2] = max(newpts[2],range_[2]) + range_[3] = max(newpts[3],range_[3]) + } + if (loop + 1) < width { + newpts[0] = pts[0] - xinc*(loop/2+1) + newpts[1] = pts[1] - yinc*(loop/2+1) + newpts[2] = pts[2] - xinc*(loop/2+1) + newpts[3] = pts[3] - yinc*(loop/2+1) + if clip_and_draw_line(surf,rect,col, newpts) > 0 { + anydraw = 1 + range_[0] = min(newpts[0],range_[0]) + range_[1] = min(newpts[1],range_[1]) + range_[2] = max(newpts[2],range_[2]) + range_[3] = max(newpts[3],range_[3]) + } + } + } + if anydraw > 0 { + copy(pts,range_) + } + return anydraw +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} + +func abs(n int) int { + return int(math.Abs(float64(n))) +} + +func encode(x,y,left,top,right,bottom int) int { + code := 0 + if (x < left ) { + code |= LEFT_EDGE + } + if (x > right) { + code |= RIGHT_EDGE + } + if (y < top) { + code |= TOP_EDGE + } + if (y > bottom) { + code |= BOTTOM_EDGE + } + return code +} + +func inside(a int) bool { + if a > 0 { + return false + } + return true +} + +func accept(a,b int) bool { + ret := a | b + if ret > 0 { + return false + }else { + return true + } +} + +func reject(a,b int) bool { + ret := a & b + if ret > 0 { + return true + } + return false +} + +func clipline(pts []int, left,top,right,bottom int) int { + + x1 := pts[0] + y1 := pts[1] + x2 := pts[2] + y2 := pts[3] + + var code1 int + var code2 int + draw := 0 + var swaptmp int + var m float64 /*slope*/ + + for true { + code1 = encode(x1,y1,left,top,right,bottom) + code2 = encode(x2,y2,left,top,right,bottom) + if ( accept(code1,code2) ) { + draw = 1 + break + } else if ( reject(code1,code2 ) ) { + break + }else { + if inside(code1) { + swaptmp = x2 + x2 = x1 + x1 = swaptmp + swaptmp = y2 + y2 = y1 + y1 = swaptmp + swaptmp = code2 + code2 = code1 + code1 = swaptmp + } + if x2 != x1 { + m = float64(y2 - y1) / float64(x2-x1) + }else { + m = 1.0 + } + if (code1 & LEFT_EDGE) > 0 { + y1 += int(float64(left-x1)*m) + x1 = left + }else if (code1 & RIGHT_EDGE) > 0 { + y1 += int(float64(right-x1)*m) + x1 = right + }else if (code1 & BOTTOM_EDGE) > 0 { + if x2 != x1 { + x1 += int(float64(bottom-y1) / m) + } + y1 = bottom + }else if (code1 & TOP_EDGE) > 0 { + if x2 != x1 { + x1 += int( float64(top-y1) / m) + } + y1 = top + } + } + } + + if draw > 0 { + pts[0] = x1 + pts[1] = y1 + pts[2] = x2 + pts[3] = y2 + } + + return draw +} + +func drawline(surf *sdl.Surface, col color.Color, x1,y1,x2,y2 int) { + deltax := x2 - x1 + deltay := y2 - y1 + + signx := 0 + signy := 0 + + if deltax < 0 { + signx = -1 + }else { + signx = 1 + } + + if deltay < 0 { + signy = -1 + }else { + signy = 1 + } + + deltax = signx * deltax + 1 + deltay = signy * deltay + 1 + + bytes_per_pixel := surf.BytesPerPixel() + + pixx := int(bytes_per_pixel) + pixy := int(surf.Pitch) + + addr := int(pixy) * y1 + x1 * bytes_per_pixel + + pixx *= int(signx) + pixy *= int(signy) + + swaptmp := 0 + if deltax < deltay { + swaptmp = deltax + deltax = deltay + deltay = swaptmp + swaptmp = pixx + pixx = pixy + pixy = swaptmp + } + + x := 0 + y := 0 + + color_bytes := col.ToBytes() + pixels := surf.Pixels() + + switch bytes_per_pixel { + case 1: + for ; x < deltax; x++ { + addr += pixx + pixels[addr] = color_bytes[0] + y += deltay + if y >= deltax { + y -= deltax + addr += pixy + } + } + break + case 2: + for ; x < deltax;x++ { + addr += pixx + pixels[addr] = color_bytes[0] + pixels[addr+1] = color_bytes[1] + y+= deltay + if y >= deltax { + y -= deltax + addr += pixy + } + } + break + case 3: + for ; x < deltax; x++ { + addr+= pixx + pixels[addr] = color_bytes[0] + pixels[addr+1] = color_bytes[1] + pixels[addr+2] = color_bytes[2] + y+=deltay + if y >= deltax { + y-=deltax + addr += pixy + } + } + break + case 4: + for ; x < deltax; x++ { + addr+= pixx + pixels[addr] = color_bytes[0] + pixels[addr+1] = color_bytes[1] + pixels[addr+2] = color_bytes[2] + pixels[addr+3] = color_bytes[3] + y+=deltay + if y >= deltax { + y-=deltax + addr += pixy + } + } + break + } + +} + +func drawhorzline(surf *sdl.Surface, col color.Color, x1,y1,x2 int) { + if x1 == x2 { + pixel(surf,col,x1,y1) + return + } + + bytes_per_pixel := surf.BytesPerPixel() + color_bytes := col.ToBytes() + pixels := surf.Pixels() + + addr := int(surf.Pitch) * y1 + end := 0 + start := 0 + if x1 < x2 { + end = addr + x2*bytes_per_pixel + start = x1 *bytes_per_pixel + }else { + end = addr + x1 *bytes_per_pixel + start = x2 * bytes_per_pixel + } + + switch bytes_per_pixel { + case 1: + for ; start <=end; start++ { + pixels[start] = color_bytes[0] + } + case 2: + for ; start <= end; start+=2 { + pixels[start] = color_bytes[0] + pixels[start+1] = color_bytes[1] + } + case 3: + for ; start <= end; start+=3 { + pixels[start] = color_bytes[0] + pixels[start+1] = color_bytes[1] + pixels[start+2] = color_bytes[2] + } + case 4: + for ; start <= end; start +=4 { + pixels[start] = color_bytes[0] + pixels[start+1] = color_bytes[1] + pixels[start+2] = color_bytes[2] + pixels[start+3] = color_bytes[3] + } + } + +} + +func drawvertline(surf *sdl.Surface, col color.Color, x1,y1,y2 int) { + +} + +func pixel(surf *sdl.Surface, c color.Color, x,y int) { + pixels := surf.Pixels() + bytes_per_pixel := surf.BytesPerPixel() + + addr := y * int(surf.Pitch) + x*bytes_per_pixel // 1 2 3 4 + + color_bytes := c.ToBytes() + + surf.Lock() + + if bytes_per_pixel == 1 { + pixels[addr] = color_bytes[0] + } + + if bytes_per_pixel == 2 { + for i :=0; i < bytes_per_pixel; i++ { + pixels[addr+i] = color_bytes[i] + } + } + + if bytes_per_pixel == 3 { + for i :=0; i < bytes_per_pixel; i++ { + pixels[addr+i] = color_bytes[i] + } + } + + if bytes_per_pixel == 4 { + for i :=0; i < bytes_per_pixel; i++ { + pixels[addr+i] = color_bytes[i] + } + } + + surf.Unlock() +} + +func Point(surf *sdl.Surface, c color.Color, x,y int) { + pixels := surf.Pixels() + bytes_per_pixel := surf.BytesPerPixel() + + addr := y * int(surf.Pitch) + x*bytes_per_pixel // 1 2 3 4 + + color_bytes := c.ToBytes() + + surf.Lock() + + if bytes_per_pixel == 1 { + pixels[addr] = color_bytes[0] + } + + if bytes_per_pixel == 2 { + for i :=0; i < bytes_per_pixel; i++ { + pixels[addr+i] = color_bytes[i] + } + } + + if bytes_per_pixel == 3 { + for i :=0; i < bytes_per_pixel; i++ { + pixels[addr+i] = color_bytes[i] + } + } + + if bytes_per_pixel == 4 { + for i :=0; i < bytes_per_pixel; i++ { + pixels[addr+i] = color_bytes[i] + } + } + + surf.Unlock() +} diff --git a/gogame/event/event.go b/gogame/event/event.go new file mode 100644 index 0000000..d019bda --- /dev/null +++ b/gogame/event/event.go @@ -0,0 +1,313 @@ +package event + +import ( +// "fmt" + + "strconv" + + "github.com/veandco/go-sdl2/sdl" + +) + +const ( + NOEVENT = iota + QUIT + KEYDOWN + KEYUP + USEREVENT + +) + +var sdlKeyDict = map[int]string{ +sdl.K_UNKNOWN:"", +sdl.K_RETURN:"Return", +sdl.K_ESCAPE:"Escape", +sdl.K_BACKSPACE:"Backspace", +sdl.K_TAB:"Tab", +sdl.K_SPACE:"Space", +sdl.K_EXCLAIM:"!", +sdl.K_QUOTEDBL:"\"", +sdl.K_HASH:"#", +sdl.K_PERCENT:"%", +sdl.K_DOLLAR:"$", +sdl.K_AMPERSAND:"&", +sdl.K_QUOTE:"'", +sdl.K_LEFTPAREN:"(", +sdl.K_RIGHTPAREN:")", +sdl.K_ASTERISK:"*", +sdl.K_PLUS:"+", +sdl.K_COMMA:",", +sdl.K_MINUS:"-", +sdl.K_PERIOD:".", +sdl.K_SLASH:"/", +sdl.K_0:"0", +sdl.K_1:"1", +sdl.K_2:"2", +sdl.K_3:"3", +sdl.K_4:"4", +sdl.K_5:"5", +sdl.K_6:"6", +sdl.K_7:"7", +sdl.K_8:"8", +sdl.K_9:"9", +sdl.K_COLON:":", +sdl.K_SEMICOLON:";", +sdl.K_LESS:"<", +sdl.K_EQUALS:"=", +sdl.K_GREATER:">", +sdl.K_QUESTION:"?", +sdl.K_AT:"@", +sdl.K_LEFTBRACKET:"[", +sdl.K_BACKSLASH:"\\", +sdl.K_RIGHTBRACKET:"]", +sdl.K_CARET:"^", +sdl.K_UNDERSCORE:"_", +sdl.K_BACKQUOTE:"`", +sdl.K_a:"A", +sdl.K_b:"B", +sdl.K_c:"C", +sdl.K_d:"D", +sdl.K_e:"E", +sdl.K_f:"F", +sdl.K_g:"G", +sdl.K_h:"H", +sdl.K_i:"I", +sdl.K_j:"J", +sdl.K_k:"K", +sdl.K_l:"L", +sdl.K_m:"M", +sdl.K_n:"N", +sdl.K_o:"O", +sdl.K_p:"P", +sdl.K_q:"Q", +sdl.K_r:"R", +sdl.K_s:"S", +sdl.K_t:"T", +sdl.K_u:"U", +sdl.K_v:"V", +sdl.K_w:"W", +sdl.K_x:"X", +sdl.K_y:"Y", +sdl.K_z:"Z", +sdl.K_CAPSLOCK:"CapsLock", +sdl.K_F1:"F1", +sdl.K_F2:"F2", +sdl.K_F3:"F3", +sdl.K_F4:"F4", +sdl.K_F5:"F5", +sdl.K_F6:"F6", +sdl.K_F7:"F7", +sdl.K_F8:"F8", +sdl.K_F9:"F9", +sdl.K_F10:"F10", +sdl.K_F11:"F11", +sdl.K_F12:"F12", +sdl.K_PRINTSCREEN:"PrintScreen", +sdl.K_SCROLLLOCK:"ScrollLock", +sdl.K_PAUSE:"Pause", +sdl.K_INSERT:"Insert", +sdl.K_HOME:"Home", +sdl.K_PAGEUP:"PageUp", +sdl.K_DELETE:"Delete", +sdl.K_END:"End", +sdl.K_PAGEDOWN:"PageDown", +sdl.K_RIGHT:"Right", +sdl.K_LEFT:"Left", +sdl.K_DOWN:"Down", +sdl.K_UP:"Up", +sdl.K_NUMLOCKCLEAR:"Numlock", +sdl.K_KP_DIVIDE:"Keypad /", +sdl.K_KP_MULTIPLY:"Keypad *", +sdl.K_KP_MINUS:"Keypad -", +sdl.K_KP_PLUS:"Keypad +", +sdl.K_KP_ENTER:"Keypad Enter", +sdl.K_KP_1:"Keypad 1", +sdl.K_KP_2:"Keypad 2", +sdl.K_KP_3:"Keypad 3", +sdl.K_KP_4:"Keypad 4", +sdl.K_KP_5:"Keypad 5", +sdl.K_KP_6:"Keypad 6", +sdl.K_KP_7:"Keypad 7", +sdl.K_KP_8:"Keypad 8", +sdl.K_KP_9:"Keypad 9", +sdl.K_KP_0:"Keypad 0", +sdl.K_KP_PERIOD:"Keypad .", +sdl.K_APPLICATION:"Application", +sdl.K_POWER:"Power", +sdl.K_KP_EQUALS:"Keypad =", +sdl.K_F13:"F13", +sdl.K_F14:"F14", +sdl.K_F15:"F15", +sdl.K_F16:"F16", +sdl.K_F17:"F17", +sdl.K_F18:"F18", +sdl.K_F19:"F19", +sdl.K_F20:"F20", +sdl.K_F21:"F21", +sdl.K_F22:"F22", +sdl.K_F23:"F23", +sdl.K_F24:"F24", +sdl.K_EXECUTE:"Execute", +sdl.K_HELP:"Help", +sdl.K_MENU:"Menu", +sdl.K_SELECT:"Select", +sdl.K_STOP:"Stop", +sdl.K_AGAIN:"Again", +sdl.K_UNDO:"Undo", +sdl.K_CUT:"Cut", +sdl.K_COPY:"Copy", +sdl.K_PASTE:"Paste", +sdl.K_FIND:"Find", +sdl.K_MUTE:"Mute", +sdl.K_VOLUMEUP:"VolumeUp", +sdl.K_VOLUMEDOWN:"VolumeDown", +sdl.K_KP_COMMA:"Keypad ,", +sdl.K_KP_EQUALSAS400:"Keypad = (AS400)", +sdl.K_ALTERASE:"AltErase", +sdl.K_SYSREQ:"SysReq", +sdl.K_CANCEL:"Cancel", +sdl.K_CLEAR:"Clear", +sdl.K_PRIOR:"Prior", +sdl.K_RETURN2:"Return", +sdl.K_SEPARATOR:"Separator", +sdl.K_OUT:"Out", +sdl.K_OPER:"Oper", +sdl.K_CLEARAGAIN:"Clear / Again", +sdl.K_CRSEL:"CrSel", +sdl.K_EXSEL:"ExSel", +sdl.K_KP_00:"Keypad 00", +sdl.K_KP_000:"Keypad 000", +sdl.K_THOUSANDSSEPARATOR:"ThousandsSeparator", +sdl.K_DECIMALSEPARATOR:"DecimalSeparator", +sdl.K_CURRENCYUNIT:"CurrencyUnit", +sdl.K_CURRENCYSUBUNIT:"CurrencySubUnit", +sdl.K_KP_LEFTPAREN:"Keypad (", +sdl.K_KP_RIGHTPAREN:"Keypad )", +sdl.K_KP_LEFTBRACE:"Keypad {", +sdl.K_KP_RIGHTBRACE:"Keypad }", +sdl.K_KP_TAB:"Keypad Tab", +sdl.K_KP_BACKSPACE:"Keypad Backspace", +sdl.K_KP_A:"Keypad A", +sdl.K_KP_B:"Keypad B", +sdl.K_KP_C:"Keypad C", +sdl.K_KP_D:"Keypad D", +sdl.K_KP_E:"Keypad E", +sdl.K_KP_F:"Keypad F", +sdl.K_KP_XOR:"Keypad XOR", +sdl.K_KP_POWER:"Keypad ^", +sdl.K_KP_PERCENT:"Keypad %", +sdl.K_KP_LESS:"Keypad <", +sdl.K_KP_GREATER:"Keypad >", +sdl.K_KP_AMPERSAND:"Keypad &", +sdl.K_KP_DBLAMPERSAND:"Keypad &&", +sdl.K_KP_VERTICALBAR:"Keypad |", +sdl.K_KP_DBLVERTICALBAR:"Keypad ||", +sdl.K_KP_COLON:"Keypad :", +sdl.K_KP_HASH:"Keypad #", +sdl.K_KP_SPACE:"Keypad Space", +sdl.K_KP_AT:"Keypad @", +sdl.K_KP_EXCLAM:"Keypad !", +sdl.K_KP_MEMSTORE:"Keypad MemStore", +sdl.K_KP_MEMRECALL:"Keypad MemRecall", +sdl.K_KP_MEMCLEAR:"Keypad MemClear", +sdl.K_KP_MEMADD:"Keypad MemAdd", +sdl.K_KP_MEMSUBTRACT:"Keypad MemSubtract", +sdl.K_KP_MEMMULTIPLY:"Keypad MemMultiply", +sdl.K_KP_MEMDIVIDE:"Keypad MemDivide", +sdl.K_KP_PLUSMINUS:"Keypad +/-", +sdl.K_KP_CLEAR:"Keypad Clear", +sdl.K_KP_CLEARENTRY:"Keypad ClearEntry", +sdl.K_KP_BINARY:"Keypad Binary", +sdl.K_KP_OCTAL:"Keypad Octal", +sdl.K_KP_DECIMAL:"Keypad Decimal", +sdl.K_KP_HEXADECIMAL:"Keypad Hexadecimal", +sdl.K_LCTRL:"Left Ctrl", +sdl.K_LSHIFT:"Left Shift", +sdl.K_LALT:"Left Alt", +sdl.K_LGUI:"Left GUI", +sdl.K_RCTRL:"Right Ctrl", +sdl.K_RSHIFT:"Right Shift", +sdl.K_RALT:"Right Alt", +sdl.K_RGUI:"Right GUI", +sdl.K_MODE:"ModeSwitch", +sdl.K_AUDIONEXT:"AudioNext", +sdl.K_AUDIOPREV:"AudioPrev", +sdl.K_AUDIOSTOP:"AudioStop", +sdl.K_AUDIOPLAY:"AudioPlay", +sdl.K_AUDIOMUTE:"AudioMute", +sdl.K_MEDIASELECT:"MediaSelect", +sdl.K_WWW:"WWW", +sdl.K_MAIL:"Mail", +sdl.K_CALCULATOR:"Calculator", +sdl.K_COMPUTER:"Computer", +sdl.K_AC_SEARCH:"AC Search", +sdl.K_AC_HOME:"AC Home", +sdl.K_AC_BACK:"AC Back", +sdl.K_AC_FORWARD:"AC Forward", +sdl.K_AC_STOP:"AC Stop", +sdl.K_AC_REFRESH:"AC Refresh", +sdl.K_AC_BOOKMARKS:"AC Bookmarks", +sdl.K_BRIGHTNESSDOWN:"BrightnessDown", +sdl.K_BRIGHTNESSUP:"BrightnessUp", +sdl.K_DISPLAYSWITCH:"DisplaySwitch", +sdl.K_KBDILLUMTOGGLE:"KBDIllumToggle", +sdl.K_KBDILLUMDOWN:"KBDIllumDown", +sdl.K_KBDILLUMUP:"KBDIllumUp", +sdl.K_EJECT:"Eject", +sdl.K_SLEEP:"Sleep", +} + + +type Event struct { + Type uint32 + Data map[string]string +} + +func map_events( event sdl.Event) Event { + var ret Event + if event != nil { + switch t := event.(type) { + case *sdl.QuitEvent: + ret.Type = QUIT + case *sdl.KeyboardEvent: + if t.Type == sdl.KEYDOWN { + ret.Type = KEYDOWN + } + + if t.Type == sdl.KEYUP { + ret.Type = KEYUP + } + + ret.Data = make(map[string]string) + ret.Data["Repeat"]= strconv.Itoa( int(t.Repeat) ) + ret.Data["Key"] = sdlKeyDict[ int(t.Keysym.Sym) ] + ret.Data["Mod"] = strconv.Itoa( int(t.Keysym.Mod) ) + + default: +// fmt.Printf("unknow type %T\n", t) + ret.Type = NOEVENT + } + } + + return ret +} + +func Poll() Event { + var ret Event + + sdl.Do(func() { + event := sdl.PollEvent() + ret = map_events(event) + }) + + return ret +} + +func Wait() Event { + var ret Event + + event := sdl.WaitEvent() + ret = map_events(event) + + return ret +} diff --git a/gogame/flags.go b/gogame/flags.go new file mode 100644 index 0000000..ef139a0 --- /dev/null +++ b/gogame/flags.go @@ -0,0 +1,13 @@ +package gogame + +import "github.com/veandco/go-sdl2/sdl" + +const ( + + SHOWN = sdl.WINDOW_SHOWN + OPENGL = sdl.WINDOW_OPENGL + HIDDEN = sdl.WINDOW_HIDDEN + FULLSCREEN = sdl.WINDOW_FULLSCREEN + RESIZABLE = sdl.WINDOW_RESIZABLE + +) diff --git a/gogame/rect/rect.go b/gogame/rect/rect.go new file mode 100644 index 0000000..05506f4 --- /dev/null +++ b/gogame/rect/rect.go @@ -0,0 +1,9 @@ +package rect + +import ( + "github.com/veandco/go-sdl2/sdl" +) + +func Rect(top ,left, width,height int32) sdl.Rect { + return sdl.Rect{top,left,width,height} +} diff --git a/gogame/structs.go b/gogame/structs.go new file mode 100644 index 0000000..a6bbcb4 --- /dev/null +++ b/gogame/structs.go @@ -0,0 +1,4 @@ +package gogame + +//import "github.com/veandco/go-sdl2/sdl" + diff --git a/gogame/surface/surface.go b/gogame/surface/surface.go new file mode 100644 index 0000000..8be322e --- /dev/null +++ b/gogame/surface/surface.go @@ -0,0 +1,16 @@ +package surface + +import ( + "github.com/veandco/go-sdl2/sdl" +) + + +func FillRect(surface *sdl.Surface,rect *sdl.Rect, color uint32) { + + sdl.Do(func() { + surface.FillRect(rect,color) + }) + + return +} + diff --git a/test b/test new file mode 100755 index 0000000..6982478 Binary files /dev/null and b/test differ diff --git a/test.go b/test.go new file mode 100644 index 0000000..9722b14 --- /dev/null +++ b/test.go @@ -0,0 +1,71 @@ +package main + +import ( + "os" + "fmt" + + "github.com/veandco/go-sdl2/sdl" + + //"./gogame" + "./gogame/color" + "./gogame/display" + "./gogame/surface" + "./gogame/event" + "./gogame/rect" + "./gogame/draw" + +) + +func run() int { + + display.Init() + + screen := display.SetMode(320,240,0,32) + + surface.FillRect(screen,nil, 0) + + rect := rect.Rect(0, 0, 200, 200) + + //surface.FillRect(screen,&rect, 0xffff0000) + + rect.X = 12 + + draw.Rect(screen,color.Color{129,235,234,0},&rect,1) + + fmt.Println(screen.Pitch) + fmt.Println( screen.BytesPerPixel() ) + + for i:=1; i<319;i++ { + draw.Point(screen, color.Color{255,44,255,0}, i,20) + } + display.Flip() + + + running := true + for running { + ev := event.Wait() + if ev.Type == event.QUIT { + running = false + break + } + + if ev.Type == event.KEYDOWN { + fmt.Println(ev) + if ev.Data["Key"] == "Q" { + return 0 + } + } + } + + return 0 +} + +func main() { + var exitcode int + + sdl.Main(func() { + exitcode = run() + }) + + os.Exit(exitcode) +}