summaryrefslogtreecommitdiffhomepage
path: root/samples/99_genre_lowrez/nokia_3310/app/nokia.rb
blob: 7bcb5b1bd002bad57ffa5fc7d8647303f531fd75 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# Emulation of a 64x64 canvas. Don't change this file unless you know what you're doing :-)
# Head over to main.rb and study the code there.

NOKIA_WIDTH           = 84
NOKIA_HEIGHT          = 48
NOKIA_ZOOM            = 12
NOKIA_ZOOMED_WIDTH    = NOKIA_WIDTH  * NOKIA_ZOOM
NOKIA_ZOOMED_HEIGHT   = NOKIA_HEIGHT * NOKIA_ZOOM
NOKIA_X_OFFSET        = (1280 - NOKIA_ZOOMED_WIDTH).half
NOKIA_Y_OFFSET        = ( 720 - NOKIA_ZOOMED_HEIGHT).half

NOKIA_FONT_XL         = -1
NOKIA_FONT_XL_HEIGHT  = 20

NOKIA_FONT_LG         = -3.5
NOKIA_FONT_LG_HEIGHT  = 15

NOKIA_FONT_MD         = -6
NOKIA_FONT_MD_HEIGHT  = 10

NOKIA_FONT_SM         = -8.5
NOKIA_FONT_SM_HEIGHT  = 5

NOKIA_FONT_PATH       = 'fonts/lowrez.ttf'


class NokiaOutputs
  attr_accessor :width, :height

  def initialize args
    @args = args
  end

  def outputs_nokia
    return @args.outputs if @args.state.tick_count <= 0
    return @args.outputs[:nokia]
  end

  def solids
    outputs_nokia.solids
  end

  def borders
    outputs_nokia.borders
  end

  def sprites
    outputs_nokia.sprites
  end

  def labels
    outputs_nokia.labels
  end

  def default_label
    {
      x: 0,
      y: 63,
      text: "",
      size_enum: NOKIA_FONT_SM,
      alignment_enum: 0,
      r: 0,
      g: 0,
      b: 0,
      a: 255,
      font: NOKIA_FONT_PATH
    }
  end

  def lines
    outputs_nokia.lines
  end

  def primitives
    outputs_nokia.primitives
  end

  def click
    return nil unless @args.inputs.mouse.click
    mouse
  end

  def mouse_click
    click
  end

  def mouse_down
    @args.inputs.mouse.down
  end

  def mouse_up
    @args.inputs.mouse.up
  end

  def mouse
    [
      ((@args.inputs.mouse.x - NOKIA_X_OFFSET).idiv(NOKIA_ZOOM)),
      ((@args.inputs.mouse.y - NOKIA_Y_OFFSET).idiv(NOKIA_ZOOM))
    ]
  end

  def mouse_position
    mouse
  end

  def keyboard
    @args.inputs.keyboard
  end
end

class GTK::Args
  def init_nokia
    return if @nokia
    @nokia = NokiaOutputs.new self
  end

  def nokia
    @nokia
  end
end

module GTK
  class Runtime
    alias_method :__original_tick_core__, :tick_core unless Runtime.instance_methods.include?(:__original_tick_core__)

    def tick_core
      @args.init_nokia

      __original_tick_core__

      return if @args.state.tick_count <= 0

      @args.render_target(:nokia)
           .labels
           .each do |l|
        l.y  += 1
        if (l.a || 255) > 128
          l.r = 67
          l.g = 82
          l.b = 61
          l.a = 255
        else
          l.a = 0
        end
      end

      @args.render_target(:nokia)
           .sprites
           .each do |s|
        if (s.a || 255) > 128
          s.a = 255
        else
          s.a = 0
        end
      end

      @args.render_target(:nokia)
           .solids
           .each do |s|
        if (s.a || 255) > 128
          s.r = 67
          s.g = 82
          s.b = 61
          s.a = 255
        else
          s.a = 0
        end
      end

      @args.render_target(:nokia)
           .borders
           .each do |s|
        if (s.a || 255) > 128
          s.r = 67
          s.g = 82
          s.b = 61
          s.a = 255
        else
          s.a = 0
        end
      end

      @args.render_target(:nokia)
           .lines
           .each do |l|
        l.y  += 1
        l.y2 += 1
        l.y2 += 1 if l.y1 != l.y2
        l.x2 += 1 if l.x1 != l.x2

        if (l.a || 255) > 128
          l.r = 67
          l.g = 82
          l.b = 61
          l.a = 255
        else
          l.a = 0
        end
      end

      @args.outputs.borders << {
        x: NOKIA_X_OFFSET      - 1,
        y: NOKIA_Y_OFFSET      - 1,
        w: NOKIA_ZOOMED_WIDTH  + 2,
        h: NOKIA_ZOOMED_HEIGHT + 2,
        r: 128, g: 128, b: 128
      }


      @args.outputs.background_color = [199, 240, 216]

      @args.outputs.solids << [0, 0, NOKIA_X_OFFSET, 720]
      @args.outputs.solids << [0, 0, 1280, NOKIA_Y_OFFSET]
      @args.outputs.solids << [NOKIA_X_OFFSET + NOKIA_ZOOMED_WIDTH, 0, NOKIA_X_OFFSET, 720]
      @args.outputs.solids << [0, NOKIA_Y_OFFSET.from_top, 1280, NOKIA_Y_OFFSET]

      @args.outputs
           .sprites << { x: NOKIA_X_OFFSET,
                         y: NOKIA_Y_OFFSET,
                         w: NOKIA_ZOOMED_WIDTH,
                         h: NOKIA_ZOOMED_HEIGHT,
                         source_x: 0,
                         source_y: 0,
                         source_w: NOKIA_WIDTH,
                         source_h: NOKIA_HEIGHT,
                         path: :nokia }

      if [email protected]_rendered
        (NOKIA_HEIGHT + 1).map_with_index do |i|
          @args.outputs.static_lines << {
            x:  NOKIA_X_OFFSET,
            y:  NOKIA_Y_OFFSET + (i * NOKIA_ZOOM),
            x2: NOKIA_X_OFFSET + NOKIA_ZOOMED_WIDTH,
            y2: NOKIA_Y_OFFSET + (i * NOKIA_ZOOM),
            r: 199,
            g: 240,
            b: 216,
            a: 100
          }.line!
        end

        (NOKIA_WIDTH + 1).map_with_index do |i|
          @args.outputs.static_lines << {
            x:  NOKIA_X_OFFSET + (i * NOKIA_ZOOM),
            y:  NOKIA_Y_OFFSET,
            x2: NOKIA_X_OFFSET + (i * NOKIA_ZOOM),
            y2: NOKIA_Y_OFFSET + NOKIA_ZOOMED_HEIGHT,
            r: 199,
            g: 240,
            b: 216,
            a: 100
          }.line!
        end

        @args.state.overlay_rendered = true
      end
    end
  end
end