Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

ruby 2d graphics / sdl test

By: a guest on Jun 22nd, 2013  |  syntax: Ruby  |  size: 3.47 KB  |  views: 83  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env ruby
  2. # info: Screen class for simple 2D graphics using SDL
  3. # depends: libsdl-ruby
  4. # original example code taken from:
  5. # http://lorenzod8n.wordpress.com/2007/05/30/ruby-meet-sdl/
  6. # license: GPLv3+ <http://www.gnu.org/licenses/gpl.txt>
  7. # Andras Horvath <mail@log69.com>
  8. #
  9. # SDL INSTALL:
  10. #
  11. # on Debian or Ubuntu:
  12. # sudo apt-get install libsdl-sge
  13. # sudo gem install rubsdl
  14. #
  15. # on Fedora, RHEL or clones:
  16. # su -c "yum group install 'Development Tools'"
  17. # su -c "yum install ruby ruby-devel rubygems SDL SDL-devel SDL_image SDL_image-devel freetype-devel"
  18. #
  19. # # compile and install ruby 1.9.x rather from source if only 1.8.x is available
  20. #
  21. # # get source code from these sites:
  22. # # http://www.digitalfanatics.org/cal/sge/
  23. # # http://packages.debian.org/source/stable/libsdl-sge
  24. #
  25. # wget "http://ftp.de.debian.org/debian/pool/main/libs/libsdl-sge/libsdl-sge_030809dfsg.orig.tar.gz"
  26. # wget "http://ftp.de.debian.org/debian/pool/main/libs/libsdl-sge/libsdl-sge_030809dfsg-3.debian.tar.gz"
  27. # tar xf *sge*orig*
  28. # tar xf *sge*debian*
  29. #
  30. # cd sge*
  31. # find ../debian/patches/ -iname "*.diff" | sort | while read FF; do patch < "$FF"; done
  32. #
  33. # make
  34. # su -c "make install; ldconfig"
  35. #
  36. # su -c "gem install rubysdl"
  37. #
  38.  
  39.  
  40. require 'sdl'
  41.  
  42.  
  43. # Screen object using SDL
  44. # rubysdl home:
  45. # http://www.kmc.gr.jp/~ohai/rubysdl.en.html
  46. # SDL doc:
  47. # http://www.kmc.gr.jp/~ohai/rubysdl_doc.en.html
  48. class Screen
  49.         attr_reader     :width, :height
  50.  
  51.         def initialize(width = 800, height = 600)
  52.                 @width = width
  53.                 @height = height
  54.         end
  55.  
  56.         # init and show screen
  57.         def create
  58.                 SDL.init SDL::INIT_VIDEO
  59.                 @screen = SDL::set_video_mode @width, @height, 24, SDL::SWSURFACE
  60.                 @color_bg = @screen.format.mapRGB 240, 240, 240
  61.                 @color_fg = @screen.format.mapRGB 0, 0, 0
  62.                 clear
  63.         end
  64.  
  65.         # clear the content of screen
  66.         def clear
  67.                 @screen.fill_rect 0, 0, @width, @height, @color_bg
  68.         end
  69.  
  70.         # switch screen to the active buffer and show it
  71.         def show
  72.                 @screen.flip
  73.         end
  74.  
  75.         # return an event in an array [:description, param1, param2, ...]
  76.         def event
  77.                 while event = SDL::Event2.poll
  78.                         case event
  79.                         when SDL::Event2::Quit
  80.                                 return [:quit]
  81.                         when SDL::Event2::MouseButtonDown
  82.                                 return [:mouse, event.x, event.y]
  83.                         when SDL::Event2::KeyDown
  84.                                 return [:escape] if event.sym == SDL::Key::ESCAPE
  85.                                 return [:space]  if event.sym == SDL::Key::SPACE
  86.                         end
  87.                 end
  88.                 return []
  89.         end
  90.  
  91.         # draw a point
  92.         def point(x, y)
  93.                 @screen.drawLine x, y, x, y, @color_fg
  94.         end
  95.  
  96.         # draw a line
  97.         def line(x1, y1, x2, y2)
  98.                 @screen.drawAALine x1, y1, x2, y2, @color_fg
  99.         end
  100.  
  101.         # draw a rectangle
  102.         def rect(x1, y1, x2, y2)
  103.                 @screen.drawRectAlpha x1, y1, x2-x1, y2-y1, @color_fg, 255
  104.         end
  105.  
  106.         # draw a filled rectangle
  107.         def rect_filled(x1, y1, x2, y2)
  108.                 @screen.drawFilledRectAlpha x1, y1, x2-x1, y2-y1, @color_fg, 255
  109.         end
  110.  
  111.         # draw a cirlce
  112.         def circle(x, y, r)
  113.                 @screen.drawAACircle x, y, r, @color_fg
  114.         end
  115.  
  116.         # draw a filled cirlce
  117.         def circle_filled(x, y, r)
  118.                 @screen.drawAAFilledCircle x, y, r, @color_fg
  119.         end
  120.  
  121.         # draw an ellipse
  122.         def ellipse(x, y, rx, ry)
  123.                 @screen.drawAAEllipse x, y, rx, ry, @color_fg
  124.         end
  125.  
  126.         # draw a filled ellipse
  127.         def ellipse_filled(x, y, rx, ry)
  128.                 @screen.drawAAFilledEllipse x, y, rx, ry, @color_fg
  129.         end
  130. end
  131.  
  132.  
  133.  
  134. # main
  135. s = Screen.new(600, 400)
  136. s.create
  137.  
  138. s.line(130, 100, 450, 210)
  139. s.circle(130, 100, 50)
  140. s.show
  141.  
  142. # wait for escape key or window close to quit
  143. e = []
  144. while e[0] != :quit and e[0] != :escape do
  145.         e = s.event
  146.         sleep 0.01
  147. end
  148.  
  149. exit
clone this paste RAW Paste Data