summaryrefslogtreecommitdiffhomepage
path: root/samples/07_advanced_rendering/05_render_primitives_as_hash/app/main.rb
blob: 907b0fb14b1d1f1a2285b9de1506b7d8e28cee0d (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
=begin

 Reminders:

 - Hashes: Collection of unique keys and their corresponding values. The value can be found
   using their keys.

   For example, if we have a "numbers" hash that stores numbers in English as the
   key and numbers in Spanish as the value, we'd have a hash that looks like this...
   numbers = { "one" => "uno", "two" => "dos", "three" => "tres" }
   and on it goes.

   Now if we wanted to find the corresponding value of the "one" key, we could say
   puts numbers["one"]
   which would print "uno" to the console.

 - args.outputs.sprites: An array. The values generate a sprite.
   The parameters are [X, Y, WIDTH, HEIGHT, PATH, ANGLE, ALPHA, RED, GREEN, BLUE]
   For more information about sprites, go to mygame/documentation/05-sprites.md.

 - args.outputs.labels: An array. The values generate a label.
   The parameters are [X, Y, TEXT, SIZE, ALIGNMENT, RED, GREEN, BLUE, ALPHA, FONT STYLE]
   For more information about labels, go to mygame/documentation/02-labels.md.

 - args.outputs.solids: An array. The values generate a solid.
   The parameters are [X, Y, WIDTH, HEIGHT, RED, GREEN, BLUE, ALPHA]
   For more information about solids, go to mygame/documentation/03-solids-and-borders.md.

 - args.outputs.borders: An array. The values generate a border.
   The parameters are the same as a solid.
   For more information about borders, go to mygame/documentation/03-solids-and-borders.md.

 - args.outputs.lines: An array. The values generate a line.
   The parameters are [X1, Y1, X2, Y2, RED, GREEN, BLUE]
   For more information about labels, go to mygame/documentation/02-labels.md.

=end

# This sample app demonstrates how hashes can be used to output different kinds of objects.

def tick args
  args.state.angle ||= 0 # initializes angle to 0
  args.state.angle  += 1 # increments angle by 1 every frame (60 times a second)

  # Outputs sprite using a hash
  args.outputs.sprites << {
    x: 30,                          # sprite position
    y: 550,
    w: 128,                         # sprite size
    h: 101,
    path: "dragonruby.png",         # image path
    angle: args.state.angle,        # angle
    a: 255,                         # alpha (transparency)
    r: 255,                         # color saturation
    g: 255,
    b: 255,
    tile_x:  0,                     # sprite sub division/tile
    tile_y:  0,
    tile_w: -1,
    tile_h: -1,
    flip_vertically: false,         # don't flip sprite
    flip_horizontally: false,
    angle_anchor_x: 0.5,            # rotation center set to middle
    angle_anchor_y: 0.5
  }

  # Outputs label using a hash
  args.outputs.labels << {
    x:              200,                 # label position
    y:              550,
    text:           "dragonruby",        # label text
    size_enum:      2,
    alignment_enum: 1,
    r:              155,                 # color saturation
    g:              50,
    b:              50,
    a:              255,                 # transparency
    font:           "fonts/manaspc.ttf"  # font style; without mentioned file, label won't output correctly
  }

  # Outputs solid using a hash
  # [X, Y, WIDTH, HEIGHT, RED, GREEN, BLUE, ALPHA]
  args.outputs.solids << {
    x: 400,                         # position
    y: 550,
    w: 160,                         # size
    h:  90,
    r: 120,                         # color saturation
    g:  50,
    b:  50,
    a: 255                          # transparency
  }

  # Outputs border using a hash
  # Same parameters as a solid
  args.outputs.borders << {
    x: 600,
    y: 550,
    w: 160,
    h:  90,
    r: 120,
    g:  50,
    b:  50,
    a: 255
  }

  # Outputs line using a hash
  args.outputs.lines << {
    x:  900,                        # starting position
    y:  550,
    x2: 1200,                       # ending position
    y2: 550,
    r:  120,                        # color saturation
    g:   50,
    b:   50,
    a:  255                         # transparency
  }

  # Outputs sprite as a primitive using a hash
  args.outputs.primitives << {
    x: 30,                          # position
    y: 200,
    w: 128,                         # size
    h: 101,
    path: "dragonruby.png",         # image path
    angle: args.state.angle,        # angle
    a: 255,                         # transparency
    r: 255,                         # color saturation
    g: 255,
    b: 255,
    tile_x:  0,                     # sprite sub division/tile
    tile_y:  0,
    tile_w: -1,
    tile_h: -1,
    flip_vertically: false,         # don't flip
    flip_horizontally: false,
    angle_anchor_x: 0.5,            # rotation center set to middle
    angle_anchor_y: 0.5
  }.sprite!

  # Outputs label as primitive using a hash
  args.outputs.primitives << {
    x:         200,                 # position
    y:         200,
    text:      "dragonruby",        # text
    size:      2,
    alignment: 1,
    r:         155,                 # color saturation
    g:         50,
    b:         50,
    a:         255,                 # transparency
    font:      "fonts/manaspc.ttf"  # font style
  }.label!

  # Outputs solid as primitive using a hash
  args.outputs.primitives << {
    x: 400,                         # position
    y: 200,
    w: 160,                         # size
    h:  90,
    r: 120,                         # color saturation
    g:  50,
    b:  50,
    a: 255                          # transparency
  }.solid!

  # Outputs border as primitive using a hash
  # Same parameters as solid
  args.outputs.primitives << {
    x: 600,                         # position
    y: 200,
    w: 160,                         # size
    h:  90,
    r: 120,                         # color saturation
    g:  50,
    b:  50,
    a: 255                          # transparency
  }.border!

  # Outputs line as primitive using a hash
  args.outputs.primitives << {
    x:  900,                        # starting position
    y:  200,
    x2: 1200,                       # ending position
    y2: 200,
    r:  120,                        # color saturation
    g:   50,
    b:   50,
    a:  255                         # transparency
  }.line!
end