-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtitle.rb
93 lines (75 loc) · 2.24 KB
/
title.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#
# title.rb
#
class Title
FONTNAME = "image/boxfont2.ttf"
TITLECGNAME = "image/title.bmp"
COL_HILIGHT = [0,0,255]
COL_NORMAL = [100,100,100]
Menuitem = Struct.new(:name,:ret)
def initialize(screen)
@screen = screen
@font = SDL::TTF.open(FONTNAME,30)
@titleback = SDL::Surface.loadBMP(TITLECGNAME)
@menu = []
@menu << Menuitem.new("START",:game)
@menu << Menuitem.new("CONFIG",:config)
@menu << Menuitem.new("EXIT",nil)
@cursor = 0
@snd_move = SDL::Mixer::Wave.load("sound/change.wav") unless $OPT_s
end
attr_accessor :cursor
MENU_START = 0 # @cursor takes(?) one of these values
MENU_CONFIG = 1
MENU_EXIT = 2
def run
#check value
@cursor = 0 if @cursor < 0
@cursor = @menu.size-1 if @cursor > @menu.size-1
#main loop
while true
#event check
while (event=SDL::Event2.poll)
case event
when SDL::Event2::Quit
return nil
when SDL::Event2::KeyDown
#key check
case event.sym
when SDL::Key::UP, SDL::Key::K
SDL::Mixer.playChannel(-1,@snd_move,0) if $CONF_SOUND #0=no loop(play only one time)
@cursor-=1
@cursor = @menu.size-1 if @cursor<0
when SDL::Key::DOWN, SDL::Key::J
SDL::Mixer.playChannel(-1,@snd_move,0) if $CONF_SOUND
@cursor+=1
@cursor = 0 if @cursor>@menu.size-1
when SDL::Key::RETURN, SDL::Key::SPACE
return @menu[@cursor].ret
when SDL::Key::ESCAPE
return nil
end
end
end
#drawing
@screen.put(@titleback,0,0)
@menu.each_with_index do |item,i|
color = (i==@cursor) ? COL_HILIGHT : COL_NORMAL
s = (i==@cursor) ? "<#{item.name}>" : item.name
x = (@screen.w - @font.textSize(s)[0])/2
@font.drawBlendedUTF8(@screen, s, x, 300+i*50, *color)
end
@screen.flip
end
end
end
#test
if __FILE__==$0 then
require 'sdl'
SDL.init(SDL::INIT_VIDEO|SDL::INIT_AUDIO)
screen = SDL::setVideoMode(640,480,16,SDL::SWSURFACE)
SDL::TTF.init
SDL::Mixer.open(22050,SDL::Mixer::FORMAT_S8,1)
#SDL::Mixer.open()
Title.new(screen).run
end