retro_render 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YTJmNGRmN2M2OTZmZDRmZjg3MjM1ZTA3MWI4NjExYjU0YzkyY2I1Zg==
5
+ data.tar.gz: !binary |-
6
+ YjNlNzJkZWIyNGJlZWE0NzUzNzIxMTAwMTczMDYyODZjZjliNjhjNQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YjM5OTEzMzE5OTY4NmNlZWQzMjc0MWI1ZDlhZDYyYTU3NzZkMmI3YTkwN2Zk
10
+ ZTA0Yzc2MjU2N2U0MTQwYmU0MWQzM2E2ZWE3MDlmNDcxMzM1N2E1YTcyODY5
11
+ OTQzNjBkNTA5MWE1YjQ2MjE1ZTlhMDIzNjA5ODRmNjc1ZmI4OGE=
12
+ data.tar.gz: !binary |-
13
+ MzgxYmFjMzkxNTY3OTY1ZmVlNDBhNzY1OWEzZTg2OTBlZDViOTE3MmQ1YWY4
14
+ MTRkY2VjMmQ0NDdiNTMwYWFiYTk5MjFlMWIwMGNiNzBmZjNlNDA0MGU4NWRj
15
+ OGE1YTNlZjYyMjUzOTUzN2IwNDBiY2RjODE3YWQyOThhZTk2NDA=
@@ -0,0 +1,153 @@
1
+ module RetroRender
2
+ class Dialbox
3
+ def initialize(window, opts = {})
4
+ pointor_size = opts.has_key?(:pointor_size) ? opts[:pointor_size] : [24, 36]
5
+ dialbox_size = opts.has_key?(:dialbox_size) ? opts[:dialbox_size] : [12, 12]
6
+ font_size = opts.has_key?(:font_size) ? opts[:font_size] : [16, 16]
7
+ # setup
8
+ @step_duration = opts.has_key?(:step_duration) ? opts[:step_duration] : 5 # milliseconds
9
+ @pointor_duration = opts.has_key?(:pointor_duration) ? opts[:pointor_duration] : 200 # milliseconds
10
+ @max_lines = opts.has_key?(:max_lines) ? opts[:max_lines] : 4
11
+ @padding = opts.has_key?(:padding) ? opts[:padding] : 24
12
+ @space = opts.has_key?(:space) ? opts[:space] : 16
13
+ @line_jump = opts.has_key?(:line_jump) ? opts[:line_jump] : font_size[1]
14
+ @window = window
15
+ @gfx = {
16
+ :pointor => Gosu::Image.load_tiles('gfx/dialbox/pointor.png', pointor_size[0], pointor_size[1], :retro=>true)
17
+ }
18
+ @dialbox_window = Gosu::Image.load_tiles('gfx/dialbox/base.png', dialbox_size[0], dialbox_size[1], :retro=>true)
19
+ @font = Gosu::Image.load_tiles('gfx/dialbox/font.png', font_size[0], font_size[1], :retro=>true)
20
+ index = 0
21
+ signs = ['!', '?', ':', '/', '-', '.', ',', "'"]
22
+ @letters = Hash.new
23
+ ((('a'..'z').to_a) + ('A'..'Z').to_a + ('0'..'9').to_a + signs).each do |letter|
24
+ @letters[letter] = index
25
+ index += 1
26
+ end
27
+ @done = true
28
+ @step = 0
29
+ @step_tick = Gosu::milliseconds
30
+ @pointor_frame = 0
31
+ @pointor_tick = Gosu::milliseconds
32
+ end
33
+
34
+ def done?
35
+ return @done
36
+ end
37
+
38
+ def button_down(id)
39
+ if id == Gosu::KbReturn
40
+ if (@current_line == @first_line + @max_lines - 1 or @current_line == @lines_to_display.size - 1) and @step == @lines_to_display[@current_line].size
41
+ # the total rows were displayed, we change
42
+ @first_line += @max_lines
43
+ @current_line += 1
44
+ @step = 0
45
+ else
46
+ # let's force the three lines... immediate Display
47
+ if @first_line + @max_lines - 1 < @lines_to_display.size - 1
48
+ @current_line = @first_line + @max_lines - 1
49
+ else
50
+ @current_line = @lines_to_display.size - 1
51
+ end
52
+ @step = @lines_to_display[@current_line].size
53
+ end
54
+ end
55
+ end
56
+
57
+ def set_text(text)
58
+ @done = false
59
+ @step = 0
60
+ @first_line = 0
61
+ @current_line = 0
62
+ @step_tick = Gosu::milliseconds
63
+ cut_text(text)
64
+ end
65
+
66
+ def pixel_length(word)
67
+ return word.size * 16
68
+ end
69
+
70
+ def cut_text(text)
71
+ @lines_to_display = Array.new
72
+ @lines_to_display.push ""
73
+ text.split(/ /).each do |word|
74
+ if pixel_length(@lines_to_display.last) + pixel_length(word) <= @window.width - 2 * @padding
75
+ @lines_to_display[@lines_to_display.size - 1] += word + " "
76
+ if word.include?("\n")
77
+ @lines_to_display.push ""
78
+ end
79
+ else
80
+ @lines_to_display.push word + " "
81
+ end
82
+ end
83
+ end
84
+
85
+ def draw_word(text, x, y, z)
86
+ text.split('').each do |letter|
87
+ raise("the letter \"#{letter}\" doesn't exist in this Font") if !@letters.has_key?(letter)
88
+ @font[@letters[letter]].draw(x, y, z)
89
+ x += @font.first.width
90
+ end
91
+ end
92
+
93
+ def draw(x = 0.0, y = 0.0, z = 0.0)
94
+ # DIALBOX window
95
+ content_height = ((@max_lines - 1) * @line_jump + @max_lines * @font.first.height).to_f + @padding * 2 - @dialbox_window[1].height - @dialbox_window[7].height
96
+ @dialbox_window[0].draw(x, y, z) # top left corner
97
+ @dialbox_window[1].draw(x + @dialbox_window[0].width, y, z, (@window.width - 2 * @dialbox_window[0].width).to_f / @dialbox_window[1].width, 1) # top border
98
+ @dialbox_window[2].draw(@window.width - @dialbox_window[2].width, y, z) # top right corner
99
+ @dialbox_window[3].draw(x, y + @dialbox_window[0].height, z, 1, (content_height / @dialbox_window[3].height)) # left border
100
+ @dialbox_window[4].draw(x + @dialbox_window[3].width, y + @dialbox_window[1].height, z, (@window.width - @dialbox_window[3].width - @dialbox_window[5].width).to_f / @dialbox_window[4].width, content_height / @dialbox_window[4].height) # main fill
101
+ @dialbox_window[5].draw(@window.width - @dialbox_window[5].width, y + @dialbox_window[1].height, z, 1, (content_height / @dialbox_window[5].height)) # right border
102
+ @dialbox_window[6].draw(x, y + content_height + @dialbox_window[1].height, z) # bottom left corner
103
+ @dialbox_window[7].draw(x + @dialbox_window[6].width, y + content_height + @dialbox_window[1].height, z, (@window.width - @dialbox_window[3].width - @dialbox_window[5].width).to_f / @dialbox_window[7].width, 1) # bottom border
104
+ @dialbox_window[8].draw(@window.width - @dialbox_window[8].width, y + content_height + @dialbox_window[1].height, z) # bottom right corner
105
+
106
+ # TEXT
107
+ x, y = @padding, @padding
108
+ for line in @first_line...@first_line + @max_lines
109
+ if line < @lines_to_display.size
110
+ if line <= @current_line
111
+ text_to_draw = @lines_to_display[line]
112
+ if line == @current_line
113
+ text_to_draw = text_to_draw.slice(0, @step)
114
+ end
115
+ text_to_draw.split(' ').each do |word|
116
+ draw_word(word, x, y, z)
117
+ x += pixel_length(word) + @space
118
+ end
119
+ x = @padding; y += @line_jump + @font.first.height
120
+ end
121
+ end
122
+ end
123
+
124
+ if @current_line <= @lines_to_display.size - 1
125
+ # text step handling
126
+ if Gosu::milliseconds - @step_tick >= @step_duration
127
+ if @step >= @lines_to_display[@current_line].size and @current_line + 1 < @first_line + @max_lines
128
+ @step = 0; @current_line += 1
129
+ if @current_line > @lines_to_display.size - 1
130
+ @current_line = @lines_to_display.size - 1
131
+ @step = @lines_to_display.last.size
132
+ end
133
+ else
134
+ @step_tick = Gosu::milliseconds
135
+ @step += 1 if @step < @lines_to_display[@current_line].size
136
+ end
137
+ end
138
+ # pointor drawing
139
+ if @current_line <= @lines_to_display.size - 1
140
+ if @step == @lines_to_display[@current_line].size and (@current_line == @first_line + @max_lines - 1 or @current_line == @lines_to_display.size - 1)
141
+ if Gosu::milliseconds - @pointor_tick >= @pointor_duration
142
+ @pointor_frame = (@pointor_frame == 0 ? 1 : 0)
143
+ @pointor_tick = Gosu::milliseconds
144
+ end
145
+ @gfx[:pointor][@pointor_frame].draw(@window.width - @dialbox_window[8].width * 3, content_height - 12, 0)
146
+ end
147
+ end
148
+ else
149
+ @done = true
150
+ end
151
+ end
152
+ end
153
+ end
Binary file
Binary file
@@ -0,0 +1,186 @@
1
+ require 'gosu'
2
+ require 'opengl'
3
+ require 'glu'
4
+ include Gl, Glu
5
+
6
+ class Gosu::GLTexInfo
7
+ def tex_coord_2d(x, y)
8
+ tex_x = self.left + x * (self.right - self.left)
9
+ tex_y = (self.bottom + y * (self.top - self.bottom))
10
+ glTexCoord2d(tex_x, tex_y)
11
+ end
12
+
13
+ def bottom_left; tex_coord_2d(0.0, 0.0); end
14
+ def bottom_right; tex_coord_2d(1.0, 0.0); end
15
+ def top_left; tex_coord_2d(0.0, 1.0); end
16
+ def top_right; tex_coord_2d(1.0, 1.0); end
17
+ end
18
+
19
+ class Gosu::Image
20
+ # x, y and z are relative to the center of the image
21
+ def draw_3d(x, y, z, options = {})
22
+ width, height = self.width, self.height
23
+ x, y, w, h = 0.0, 0.0, 1.0, 1.0
24
+
25
+ if options.has_key?(:rect)
26
+ # rect uses the convention [x, y, w, h], expressed in opengl (from 0.0 to 1.0, origin on bottom-left corner)
27
+ x, y, w, h = options[:rect][0].to_f, options[:rect][1].to_f, options[:rect][2].to_f, options[:rect][3].to_f
28
+ x = 0.0 if x < 0.0 or x > 1.0
29
+ y = 0.0 if y < 0.0 or y > 1.0
30
+ w = 0.0 if w < 0.0 or w > 1.0
31
+ h = 0.0 if h < 0.0 or h > 1.0
32
+ width, height = w * self.width, h * self.height
33
+ end
34
+
35
+ glBindTexture(GL_TEXTURE_2D, self.gl_tex_info.tex_name)
36
+ glEnable(GL_ALPHA_TEST)
37
+ glPushMatrix
38
+ glTranslate(x, y, z)
39
+ glScale(width, height, 1.0)
40
+
41
+ if options.has_key?(:rot)
42
+ # rot uses the convention [x, y, z], expressed in degrees
43
+ glRotate(options[:rot][0], 1, 0, 0)
44
+ glRotate(options[:rot][1], 0, 1, 0)
45
+ glRotate(options[:rot][2], 0, 0, 1)
46
+ end
47
+
48
+ glBegin(GL_QUADS)
49
+ self.gl_tex_info.tex_coord_2d(x, y); glVertex3f(-0.5, -0.5, 0.0)
50
+ self.gl_tex_info.tex_coord_2d(x + w, y); glVertex3f(0.5, -0.5, 0.0)
51
+ self.gl_tex_info.tex_coord_2d(x + w, y + h); glVertex3f(0.5, 0.5, 0.0)
52
+ self.gl_tex_info.tex_coord_2d(x, y + h); glVertex3f(-0.5, 0.5, 0.0)
53
+ glEnd
54
+ glPopMatrix
55
+ glDisable(GL_ALPHA_TEST)
56
+ end
57
+ end
58
+
59
+ module RetroRender
60
+ class Vector3
61
+ attr_accessor :x, :y, :z
62
+ # @param [Float] x
63
+ # @param [Float] y
64
+ # @param [Float] z
65
+ def initialize(x = 0.0, y = 0.0, z = 0.0)
66
+ @x, @y, @z = x, y, z
67
+ end
68
+ end
69
+
70
+ class OpenGLContext
71
+ def initialize
72
+ glEnable(GL_TEXTURE_2D)
73
+ glEnable(GL_DEPTH_TEST)
74
+ glClearColor(0.0, 1.0, 0.0, 0.0)
75
+ end
76
+
77
+ def clear
78
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
79
+ end
80
+ end
81
+
82
+ class Camera
83
+ attr_accessor :position, :target, :fovy, :ratio, :near, :far, :angles, :distance
84
+ # == ARGS :
85
+ # Hash that can take the following keys :
86
+ # @param [Symbol] :mode (defaults to: :fps)
87
+ # @param [Vector3] :position (defaults to: Vector3.new(0.0, 0.0, 0.0))
88
+ # @param [Vector3] :target (defaults to: Vector3.new(0.0, 0.0, 0.0))
89
+ # @param [Vector3] :angles (defaults to: Vector3.new(0.0, 0.0, 0.0))
90
+ # @param [Float] :fovy (defaults to: 45.0)
91
+ # @param [Float] :ratio (defaults to: 1.333)
92
+ # @param [Float] :near (defaults to: 0.1)
93
+ # @param [Float] :far (defaults to: 1000.0)
94
+ # @param [Float] :distance (defaults to: 64.0 - distance between the position and the target)
95
+ def initialize(args = {})
96
+ @mode = args.has_key?(:mode) ? args[:mode] : :fps
97
+ @position = args.has_key?(:position) ? args[:position] : Vector3.new
98
+ @target = args.has_key?(:target) ? args[:target] : Vector3.new
99
+ @angles = args.has_key?(:angles) ? args[:angles] : Vector3.new
100
+ @fovy = args.has_key?(:fovy) ? args[:fovy] : 45.0
101
+ @ratio = args.has_key?(:ratio) ? args[:ratio] : 1.333
102
+ @near = args.has_key?(:near) ? args[:near] : 0.1
103
+ @far = args.has_key?(:far) ? args[:far] : 1000.0
104
+ @distance = args.has_key?(:distance) ? args[:distance] : 64.0
105
+ @deg_to_rad = Math::PI / 180.0
106
+ end
107
+
108
+ def move_forward(velocity)
109
+ if @mode == :fps
110
+ @position.x += velocity * Math::cos(@angles.x * @deg_to_rad)
111
+ @position.z += velocity * Math::sin(@angles.x * @deg_to_rad)
112
+ else
113
+ @target.x += velocity * Math::cos(@angles.x * @deg_to_rad)
114
+ @target.z += velocity * Math::sin(@angles.x * @deg_to_rad)
115
+ end
116
+ end
117
+
118
+ def move_left(velocity)
119
+ if @mode == :fps
120
+ @position.x += velocity * Math::cos((@angles.x - 90.0) * @deg_to_rad)
121
+ @position.z += velocity * Math::sin((@angles.x - 90.0) * @deg_to_rad)
122
+ else
123
+ @target.x += velocity * Math::cos((@angles.x - 90.0) * @deg_to_rad)
124
+ @target.z += velocity * Math::sin((@angles.x - 90.0) * @deg_to_rad)
125
+ end
126
+ end
127
+
128
+ def move_right(velocity)
129
+ if @mode == :fps
130
+ @position.x -= velocity * Math::cos((@angles.x - 90.0) * @deg_to_rad)
131
+ @position.z -= velocity * Math::sin((@angles.x - 90.0) * @deg_to_rad)
132
+ else
133
+ @target.x -= velocity * Math::cos((@angles.x - 90.0) * @deg_to_rad)
134
+ @target.z -= velocity * Math::sin((@angles.x - 90.0) * @deg_to_rad)
135
+ end
136
+ end
137
+
138
+ def move_backward(velocity)
139
+ if @mode == :fps
140
+ @position.x -= velocity * Math::cos(@angles.x * @deg_to_rad)
141
+ @position.z -= velocity * Math::sin(@angles.x * @deg_to_rad)
142
+ else
143
+ @target.x -= velocity * Math::cos(@angles.x * @deg_to_rad)
144
+ @target.z -= velocity * Math::sin(@angles.x * @deg_to_rad)
145
+ end
146
+ end
147
+
148
+ def rotate(axis, value)
149
+ if axis == :x
150
+ @angles.x += value
151
+ elsif axis == :y
152
+ @angles.y += value
153
+ elsif axis == :z
154
+ @angles.z += value
155
+ else
156
+ raise("axis must be a symbol as :x, :y or :z")
157
+ end
158
+ end
159
+
160
+ def update
161
+ if @mode == :fps
162
+ @target.x = @position.x + @distance * Math::cos(@angles.y * @deg_to_rad)
163
+ @target.z = @position.z + @distance * Math::cos(@angles.y * @deg_to_rad)
164
+ @target.x = @position.x + @distance * Math::cos(@angles.x * @deg_to_rad)
165
+ @target.y = @position.y + @distance * Math::sin(@angles.y * @deg_to_rad)
166
+ @target.z = @position.z + @distance * Math::sin(@angles.x * @deg_to_rad)
167
+ else
168
+ @position.x = @target.x - @distance * Math::cos(@angles.y * @deg_to_rad)
169
+ @position.z = @target.z - @distance * Math::cos(@angles.y * @deg_to_rad)
170
+ @position.x = @target.x - @distance * Math::cos(@angles.x * @deg_to_rad)
171
+ @position.y = @target.y - @distance * Math::sin(@angles.y * @deg_to_rad)
172
+ @position.z = @target.z - @distance * Math::sin(@angles.x * @deg_to_rad)
173
+ end
174
+ end
175
+
176
+ # Sets the OpenGL matrices according to the camera
177
+ def look
178
+ glMatrixMode(GL_PROJECTION)
179
+ glLoadIdentity
180
+ gluPerspective(@fovy, @ratio, @near, @far)
181
+ glMatrixMode(GL_MODELVIEW)
182
+ glLoadIdentity
183
+ gluLookAt(@position.x, @position.y, @position.z, @target.x, @target.y, @target.z, 0, 1, 0)
184
+ end
185
+ end
186
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: retro_render
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.8
5
+ platform: ruby
6
+ authors:
7
+ - SolidBoko
8
+ - Michto
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-11-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: gosu
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ! '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: opengl
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: glu
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ description: WIP Gem - Will evoluate a lot !
57
+ email: solidboko@retrorender.net
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/dialbox.rb
63
+ - lib/gfx/dialbox/base.png
64
+ - lib/gfx/dialbox/font.png
65
+ - lib/gfx/dialbox/pointor.png
66
+ - lib/retro_render.rb
67
+ homepage: http://www.retrorender.net/rdoc
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.5.0
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: A useful collection of premade classes
91
+ test_files: []
92
+ has_rdoc: