NOGDUS $1670.00 has been donated to NOGDUS!
August 23, 2017, 08:12:43 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: RubySDL Starfield Demo  (Read 2624 times)
0 Members and 1 Guest are viewing this topic.
Richard Marks
Administrator
Offline Offline

Respect: 3425
« on: July 05, 2008, 08:24:09 PM »

RubySDL Starfield Demo



Tutorial Coming Soon!
I need some time to type up the tutorial text for this one. For now here is the whole code for the demo.  Cool
Hit me up on IRC if you have questions.

download this font and place it in your project directory before running this.
Note: If you're using linux, rename the file to be all lowercase.

Code:
#!/usr/bin/env ruby
# rubysdl_starfield1.rb
# A RubySDL 2D parallax scrolling starfield program
# by Richard Marks ccpsceo@gmail.com

require "sdl"

# LoadBitmap
# returns a bitmap suitable to be displayed with SDL
def LoadBitmap(fileName, useColorKey=false, colorKey=nil)
surface = SDL::Surface.loadBMP(fileName)
if useColorKey == true then
if colorKey.nil? then
# use the color at 0,0 in the image as the color key
surface.setColorKey(SDL::SRCCOLORKEY, surface[0, 0])
else
# use the color key specified
surface.setColorKey(SDL::SRCCOLORKEY, colorKey)
end # end if no color key specified
end # end if we are using a color key on this image
surface = surface.displayFormat
return surface
end # end function LoadBitmap

def DrawText(font, surface, text, x, y, colorArray=[0xff, 0xff, 0xff])
font.drawSolidUTF8(surface, text, x, y, *colorArray)
end # end function DrawText

class Star
attr_accessor :x, :y, :xv, :yv, :color
def initialize(x,y,xv,yv,color)
@x = x
@y = y
@xv = xv
@yv = yv
@color = color
end
end # end class Star

class Starfield
def initialize(surface, animDirection=0, starCount=150)
#
# animDirection can be 0-3
#
# 0 - stars scroll from the bottom to the top
# 1 - stars scroll from the top to the bottom
# 2 - stars scroll from the left to the right
# 3 - stars scroll from the right to the left

@animDirection = animDirection
@starCount = starCount
@stars = Array.new
@surface = surface
@width = @surface.w
@height = @surface.h

(1..@starCount).each do |index|
x = (rand() * @width).to_i
y = (rand() * @height).to_i
xv = 2 + (rand() * 3 - 2).to_f
yv = 2 + (rand() * 3 - 2).to_f
color = [0xff, 0xff, 0xff]
@stars[index] = Star.new(x,y,xv,yv,color)
end # end loop over all stars

end # end method initialize

def SetScrollingDirection(animDirection)
@animDirection = animDirection
end

def Update
(1..@starCount).each do |index|
case @animDirection
when 0
# 0 - stars scroll from the bottom to the top
@stars[index].y -= @stars[index].yv
when 1
# 1 - stars scroll from the top to the bottom
@stars[index].y += @stars[index].yv
when 2
# 2 - stars scroll from the left to the right
@stars[index].x += @stars[index].xv
when 3
# 3 - stars scroll from the right to the left
@stars[index].x -= @stars[index].xv
end # end case animation direction

# keep the stars scrolling by wrapping
if @stars[index].x < 0 then @stars[index].x = @width end
if @stars[index].y < 0 then @stars[index].y = @height end
if @stars[index].x > @width then @stars[index].x = 0 end
if @stars[index].y > @height then @stars[index].y = 0 end
end # end loop over all stars
end # end method Update

def Render
(1..@starCount).each do |index|
x = @stars[index].x
y = @stars[index].y
color = @stars[index].color
@surface.putPixel(x, y, color)
end # end loop over all stars
end # end method Render
end # end class Starfield

class Demo
def initialize
# init SDL
SDL.init(SDL::INIT_VIDEO)

# init video mode (create a window 640x480 with 16 bpp)
@screen = SDL.setVideoMode(640, 480, 16, SDL::SWSURFACE)

# set the window manager window caption
# SDL::WM.setCaption(windowed_caption, icon_caption)
SDL::WM.setCaption("RubySDL Starfield!", "RubySDL Starfield!")

# init SDL_TTF
SDL::TTF.init

# load a ttf font
@defFont = SDL::TTF.open('astrolyte.ttf', 48)
@defFont.style = SDL::TTF::STYLE_NORMAL

@smallFont = SDL::TTF.open('astrolyte.ttf', 18)
@smallFont.style = SDL::TTF::STYLE_NORMAL

# SDL event handler
@event = SDL::Event.new

# starfield
@starField = Starfield.new(@screen)

end # end method initialize

def Run
# start the infinitely executing main loop
while true
# poll for events
if @event.poll != 0 then
# check for quit and keydown events
case @event.type
when SDL::Event::QUIT
break
when SDL::Event::KEYDOWN
# check for the ESC key
if @event.keySym == SDL::Key::ESCAPE then
break
end # end if ESC pressed
end # end case event.type
end # end event if events in event.poll

SDL::Key.scan
if SDL::Key.press?(SDL::Key::K1) then @starField.SetScrollingDirection 0 end
if SDL::Key.press?(SDL::Key::K2) then @starField.SetScrollingDirection 1 end
if SDL::Key.press?(SDL::Key::K3) then @starField.SetScrollingDirection 2 end
if SDL::Key.press?(SDL::Key::K4) then @starField.SetScrollingDirection 3 end


@starField.Update

# fill the screen buffer with black
@screen.fillRect(0, 0, 640, 480, 0)

@starField.Render

DrawText(@defFont, @screen, "RubySDL Starfield!", 78, 198)

DrawText(@smallFont, @screen, "Press 1..4 to change scrolling direction.", 78, 270)
DrawText(@smallFont, @screen, "Press ESC to close the demo.", 78, 290)

# ensure garbage collection
ObjectSpace.garbage_collect

# flip screen buffer contents to visible screen
@screen.flip
end # end main loop
end # end method Run
end # end class Demo

if __FILE__ == $0 then
# create an instance of our demo class (calls initialize automatically)
demo = Demo.new
# call the Run method of our class to get this sucker running!
demo.Run
end # end if this file is the running file (not just being required from another file)
Logged

Tags:
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines
.: Theme by Richard Marks :.
Valid XHTML 1.0! Valid CSS!