{"id":973,"date":"2012-09-21T12:11:08","date_gmt":"2012-09-21T12:11:08","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=973"},"modified":"2012-09-21T12:30:21","modified_gmt":"2012-09-21T12:30:21","slug":"a-simple-golang-sdl-example","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2012\/09\/a-simple-golang-sdl-example\/","title":{"rendered":"A Simple Golang SDL example"},"content":{"rendered":"<p>First install golang, SDL and the golang SDL package. The following is for Ubuntu 12.04:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nsudo apt-get install golang-go\r\n\r\nInstall SDL libraries:\r\n\r\nsudo apt-get install libsdl1.2-dev\r\nsudo apt-get install libsdl-mixer*\r\nsudo apt-get install libsdl-image*\r\nsudo apt-get install libsdl-ttf*\r\nsudo go get -v github.com\/0xe2-0x9a-0x9b\/Go-SDL\/...\r\n<\/pre>\n<p>Then create the following go program. This write random pixel data to the screen directly. If you save it to main.go you can execute it with:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ngo run main.go\r\n<\/pre>\n<p>Here&#8217;s the code (please note there&#8217;s probably a lot wrong with it, it&#8217;s my first go program):<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\npackage main\r\n\r\nimport (\r\n&quot;github.com\/0xe2-0x9a-0x9b\/Go-SDL\/sdl&quot;\r\n&quot;log&quot;\r\n&quot;unsafe&quot;\r\n&quot;math\/rand&quot;\r\n)\r\n\r\nfunc draw_point(x int32,y int32,value uint32,screen* sdl.Surface) {\r\n  var pix = uintptr(screen.Pixels);\r\n  pix += (uintptr)((y*screen.W)+x)*unsafe.Sizeof(value);\r\n  var pu = unsafe.Pointer(pix);\r\n  var pp *uint32;\r\n  pp = (*uint32)(pu);\r\n  *pp = value;\r\n}\r\n\r\nfunc main() {\r\n\r\n  var screen = sdl.SetVideoMode(640, 480, 32, sdl.RESIZABLE)\r\n\r\n  if screen == nil {\r\n    log.Fatal(sdl.GetError())\r\n  }\r\n\r\n  var n int32;\r\n  for n=0;n&lt;1000000;n++ {\r\n\r\n    var y int32 =rand.Int31()%480;\r\n    var x int32 =rand.Int31()%640;\r\n    var value uint32 = rand.Uint32();\r\n    draw_point(x,y,value,screen);\r\n\r\n    screen.Flip();\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>First install golang, SDL and the golang SDL package. The following is for Ubuntu 12.04: sudo apt-get install golang-go Install SDL libraries: sudo apt-get install libsdl1.2-dev sudo apt-get install libsdl-mixer* sudo apt-get install libsdl-image* sudo apt-get install libsdl-ttf* sudo go get -v github.com\/0xe2-0x9a-0x9b\/Go-SDL\/&#8230; Then create the following go program. This write random pixel data to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-973","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-fH","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=973"}],"version-history":[{"count":4,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/973\/revisions"}],"predecessor-version":[{"id":979,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/973\/revisions\/979"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}