Guest User

ruby 2d graphics / sdl test

a guest
Jun 22nd, 2013
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 <[email protected]>
  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.     # init and show screen
  52.     def initialize(width = 800, height = 600)
  53.         @width = width
  54.         @height = height
  55.         SDL.init SDL::INIT_VIDEO
  56.         @screen = SDL::set_video_mode @width, @height, 24, SDL::SWSURFACE
  57.         @color_bg = @screen.format.mapRGB 240, 240, 240
  58.         @color_fg = @screen.format.mapRGB 0, 0, 0
  59.         clear
  60.     end
  61.  
  62.     # clear the content of screen
  63.     def clear
  64.         @screen.fill_rect 0, 0, @width, @height, @color_bg
  65.     end
  66.  
  67.     # switch screen to the active buffer and show it
  68.     def show
  69.         @screen.flip
  70.     end
  71.  
  72.     # return an event in an array [:description, param1, param2, ...]
  73.     def event
  74.         while event = SDL::Event2.poll
  75.             case event
  76.             when SDL::Event2::Quit
  77.                 return [:quit]
  78.             when SDL::Event2::MouseButtonDown
  79.                 return [:mouse, event.x, event.y]
  80.             when SDL::Event2::KeyDown
  81.                 return [:escape] if event.sym == SDL::Key::ESCAPE
  82.                 return [:space]  if event.sym == SDL::Key::SPACE
  83.             end
  84.         end
  85.         return []
  86.     end
  87.  
  88.     # draw a point
  89.     def point(x, y)
  90.         @screen.drawLine x, y, x, y, @color_fg
  91.     end
  92.  
  93.     # draw a line
  94.     def line(x1, y1, x2, y2)
  95.         @screen.drawAALine x1, y1, x2, y2, @color_fg
  96.     end
  97.  
  98.     # draw a rectangle
  99.     def rect(x1, y1, x2, y2)
  100.         @screen.drawRectAlpha x1, y1, x2-x1, y2-y1, @color_fg, 255
  101.     end
  102.  
  103.     # draw a filled rectangle
  104.     def rect_filled(x1, y1, x2, y2)
  105.         @screen.drawFilledRectAlpha x1, y1, x2-x1, y2-y1, @color_fg, 255
  106.     end
  107.  
  108.     # draw a cirlce
  109.     def circle(x, y, r)
  110.         @screen.drawAACircle x, y, r, @color_fg
  111.     end
  112.  
  113.     # draw a filled cirlce
  114.     def circle_filled(x, y, r)
  115.         @screen.drawAAFilledCircle x, y, r, @color_fg
  116.     end
  117.  
  118.     # draw an ellipse
  119.     def ellipse(x, y, rx, ry)
  120.         @screen.drawAAEllipse x, y, rx, ry, @color_fg
  121.     end
  122.  
  123.     # draw a filled ellipse
  124.     def ellipse_filled(x, y, rx, ry)
  125.         @screen.drawAAFilledEllipse x, y, rx, ry, @color_fg
  126.     end
  127. end
  128.  
  129.  
  130.  
  131. # main
  132. s = Screen.new(600, 400)
  133.  
  134. s.line(130, 100, 450, 210)
  135. s.circle(130, 100, 50)
  136. s.show
  137.  
  138. # wait for escape key or window close to quit
  139. e = []
  140. while e[0] != :quit and e[0] != :escape do
  141.     e = s.event
  142.     sleep 0.01
  143. end
  144.  
  145. exit
Advertisement
Add Comment
Please, Sign In to add comment