Class: Hokusai::Backends::SDLBackend

Inherits:
Object
  • Object
show all
Defined in:
ui/src/hokusai/backends/sdl2.rb,
ui/src/hokusai/backends/sdl2/font.rb,
ui/src/hokusai/backends/sdl2/keys.rb,
ui/src/hokusai/backends/sdl2/color.rb,
ui/src/hokusai/backends/sdl2/config.rb

Defined Under Namespace

Classes: Config, Font

Constant Summary collapse

SDL_PATH =
ENV["SDL_PATH"] || "#{__dir__}/../../../../vendor/lib"
OnWidthCb =
Proc.new do |char, raw|
  minxp = FFI::MemoryPointer.new :int
  maxxp = FFI::MemoryPointer.new :int
  minyp = FFI::MemoryPointer.new :int
  maxyp = FFI::MemoryPointer.new :int
  advancep = FFI::MemoryPointer.new :int

  SDL.TTF_GlyphMetrics32(raw, char.ord, minxp, maxxp, minyp, maxyp, advancep)

  metrics = [minxp.read_int, maxxp.read_int, minyp.read_int, maxyp.read_int, advancep.read_int]

  minxp.free
  maxxp.free
  minyp.free
  maxyp.free
  advancep.free

  metrics[4].to_f
end
Keys =
[
[:null, SDL::SDLK_UNKNOWN],
# [:left_super, SDL::SDLK_LSUPER],
# [:right_super, SDL::SDLK_RSUPER],
[:apostrophe, SDL::SDLK_QUOTE],
[:comma, SDL::SDLK_COMMA],
[:minus, SDL::SDLK_MINUS],
[:period, SDL::SDLK_PERIOD],
[:slash, SDL::SDLK_SLASH],
[:zero, SDL::SDLK_0],
[:one, SDL::SDLK_1],
[:two, SDL::SDLK_2],
[:three, SDL::SDLK_3],
[:four, SDL::SDLK_4],
[:five, SDL::SDLK_5],
[:six, SDL::SDLK_6],
[:seven, SDL::SDLK_7],
[:eight, SDL::SDLK_8],
[:nine, SDL::SDLK_9],
[:semicolon, SDL::SDLK_SEMICOLON],
[:equal, SDL::SDLK_EQUALS],
[:a, SDL::SDLK_a],
[:b, SDL::SDLK_b],
[:c, SDL::SDLK_c],
[:d, SDL::SDLK_d],
[:e, SDL::SDLK_e],
[:f, SDL::SDLK_f],
[:g, SDL::SDLK_g],
[:h, SDL::SDLK_h],
[:i, SDL::SDLK_i],
[:j, SDL::SDLK_j],
[:k, SDL::SDLK_k],
[:l, SDL::SDLK_l],
[:m, SDL::SDLK_m],
[:n, SDL::SDLK_n],
[:o, SDL::SDLK_o],
[:p, SDL::SDLK_p],
[:q, SDL::SDLK_q],
[:r, SDL::SDLK_r],
[:s, SDL::SDLK_s],
[:t, SDL::SDLK_t],
[:u, SDL::SDLK_u],
[:v, SDL::SDLK_v],
[:w, SDL::SDLK_w],
[:x, SDL::SDLK_x],
[:y, SDL::SDLK_y],
[:z, SDL::SDLK_z],
[:left_bracket, SDL::SDLK_LEFTBRACKET],
[:backslash, SDL::SDLK_BACKSLASH],
[:right_bracket, SDL::SDLK_RIGHTBRACKET],
[:grave, SDL::SDLK_BACKQUOTE],
[:space, SDL::SDLK_SPACE],
[:escape, SDL::SDLK_ESCAPE],
[:enter, SDL::SDLK_RETURN],
[:tab, SDL::SDLK_TAB],
[:backspace, SDL::SDLK_BACKSPACE],
[:insert, SDL::SDLK_INSERT],
[:delete, SDL::SDLK_DELETE],
[:right, SDL::SDLK_RIGHT],
[:left, SDL::SDLK_LEFT],
[:down, SDL::SDLK_DOWN],
[:up, SDL::SDLK_UP],
[:page_up, SDL::SDLK_PAGEUP],
[:page_down, SDL::SDLK_PAGEDOWN],
[:home, SDL::SDLK_HOME],
[:end, SDL::SDLK_END],
[:caps_lock, SDL::SDLK_CAPSLOCK],
[:scroll_lock, SDL::SDLK_SCROLLLOCK],
[:num_lock, SDL::SDLK_NUMLOCKCLEAR],
[:print_screen, SDL::SDLK_PRINTSCREEN],
[:pause, SDL::SDLK_PAUSE],
[:f1, SDL::SDLK_F1],
[:f2, SDL::SDLK_F2],
[:f3, SDL::SDLK_F3],
[:f4, SDL::SDLK_F4],
[:f5, SDL::SDLK_F5],
[:f6, SDL::SDLK_F6],
[:f7, SDL::SDLK_F7],
[:f8, SDL::SDLK_F8],
[:f9, SDL::SDLK_F9],
[:f10, SDL::SDLK_F10],
[:f11, SDL::SDLK_F11],
[:f12, SDL::SDLK_F12],
[:kb_menu, SDL::SDLK_MENU],
[:kp_0, SDL::SDLK_KP_0],
[:kp_1, SDL::SDLK_KP_1],
[:kp_2, SDL::SDLK_KP_2],
[:kp_3, SDL::SDLK_KP_3],
[:kp_4, SDL::SDLK_KP_4],
[:kp_5, SDL::SDLK_KP_5],
[:kp_6, SDL::SDLK_KP_6],
[:kp_7, SDL::SDLK_KP_7],
[:kp_8, SDL::SDLK_KP_8],
[:kp_9, SDL::SDLK_KP_9],
[:kp_decimal, SDL::SDLK_KP_DECIMAL],
[:kp_divide, SDL::SDLK_KP_DIVIDE],
[:kp_multiply, SDL::SDLK_KP_MULTIPLY],
[:kp_subtract, SDL::SDLK_KP_MINUS],
[:kp_add, SDL::SDLK_KP_PLUS],
[:kp_enter, SDL::SDLK_KP_ENTER],
[:kp_equal, SDL::SDLK_KP_EQUALS],
[:back, SDL::SDLK_AC_BACK],
[:menu, SDL::SDLK_MENU],
[:volume_up, SDL::SDLK_VOLUMEUP],
[:volume_down, SDL::SDLK_VOLUMEDOWN]].to_h {|(k,v)| [v, k]}
Modifiers =
[
  [SDL::KMOD_LSHIFT, :left_shift],
  [SDL::KMOD_RSHIFT, :right_shift],
  [SDL::KMOD_RALT, :right_alt],
  [SDL::KMOD_RCTRL, :right_control],
  [SDL::KMOD_LALT, :left_alt],
  [SDL::KMOD_LCTRL, :left_control],
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|@config| ... } ⇒ SDLBackend

Returns a new instance of SDLBackend.

Yields:



53
54
55
56
57
# File 'ui/src/hokusai/backends/sdl2.rb', line 53

def initialize
  @config = Config.new

  yield @config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'ui/src/hokusai/backends/sdl2.rb', line 9

def config
  @config
end

#render_heightObject

Returns the value of attribute render_height.



10
11
12
# File 'ui/src/hokusai/backends/sdl2.rb', line 10

def render_height
  @render_height
end

#render_widthObject

Returns the value of attribute render_width.



10
11
12
# File 'ui/src/hokusai/backends/sdl2.rb', line 10

def render_width
  @render_width
end

Class Method Details

.color(r, g, b, a = 255) ⇒ Object



3
4
5
6
7
8
9
10
# File 'ui/src/hokusai/backends/sdl2/color.rb', line 3

def self.color(r, g, b, a = 255)
  color = SDL::Color.new
  color[:r] = r
  color[:g] = g
  color[:b] = b
  color[:a] = a
  color
end

.cursorsObject



23
24
25
26
27
28
29
30
31
# File 'ui/src/hokusai/backends/sdl2.rb', line 23

def self.cursors
  @cursors ||= {
   default: SDL.CreateSystemCursor(SDL::SYSTEM_CURSOR_ARROW),
   arrow: SDL.CreateSystemCursor(SDL::SYSTEM_CURSOR_ARROW),
   ibeam: SDL.CreateSystemCursor(SDL::SYSTEM_CURSOR_IBEAM),
   crosshair: SDL.CreateSystemCursor(SDL::SYSTEM_CURSOR_CROSSHAIR),
   pointer: SDL.CreateSystemCursor(SDL::SYSTEM_CURSOR_HAND)
 }
end

.font_texturesObject



49
50
51
# File 'ui/src/hokusai/backends/sdl2.rb', line 49

def self.font_textures
  @font_textures ||= {}
end

.fontsObject



45
46
47
# File 'ui/src/hokusai/backends/sdl2.rb', line 45

def self.fonts
  @fonts ||= {}
end

.imagesObject



41
42
43
# File 'ui/src/hokusai/backends/sdl2.rb', line 41

def self.images
  @images ||= {}
end

.run(app) ⇒ Object



14
15
16
17
18
19
20
21
# File 'ui/src/hokusai/backends/sdl2.rb', line 14

def self.run(app)
  backend = new do |config|
    yield(config)
  end

  block = app.mount
  backend.run(block)
end

.stoppedObject



33
34
35
# File 'ui/src/hokusai/backends/sdl2.rb', line 33

def self.stopped
  @stopped ||= false
end

.stopped=(val) ⇒ Object



37
38
39
# File 'ui/src/hokusai/backends/sdl2.rb', line 37

def self.stopped=(val)
  @stopped = val
end

Instance Method Details

#inside_scissor(x, y, h = 0) ⇒ Object



303
304
305
306
307
308
309
# File 'ui/src/hokusai/backends/sdl2.rb', line 303

def inside_scissor(x, y, h = 0)
  return true if @scissor.nil?

  val = y + h >= @scissor[1] && y <= @scissor[1] + @scissor[3]
  # pp ["inside scissor", val, [x,y], @scissor]
  val
end

#mouse_button(clicked: false, down: false, released: false, up: false) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'ui/src/hokusai/backends/sdl2.rb', line 136

def mouse_button(clicked: false, down: false, released: false, up: false)
  if @mouse_button.nil?
    @mouse_button = LibHokusai::HmlInputMouseButton.create(clicked: clicked, down: down, released: released, up: up)
  else
    @mouse_button[:clicked] = clicked
    @mouse_button[:down] = down
    @mouse_button[:released] = released
    @mouse_button[:up] = up
  end

  @mouse_button
end

#nine_slice(x, y, w, h, rounding) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'ui/src/hokusai/backends/sdl2.rb', line 280

def nine_slice(x, y, w, h, rounding)
  tx = x + rounding
  ty = y
  tw = w - (rounding * 2)
  th = rounding

  mx = x + rounding
  my = y + rounding
  mw = w - (rounding * 2)
  mh = h - (rounding * 2)

  bx = x + rounding
  by = h - rounding
  bw = w - (rounding * 2)
  bh = rounding

  {
    top_center: [tx, ty, tw, th],
    middle_center: [mx, my, mw, mh],
    bottom_center: [bx, by, bw, bh]
  }
end

#process_input(input, event) ⇒ Object



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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'ui/src/hokusai/backends/sdl2.rb', line 172

def process_input(input, event)
  reset_keys(input)

  case event[:common][:type]
  when SDL::KEYDOWN
    modifier = event[:key][:keysym][:mod]
    code = event[:key][:keysym][:sym]
    hkey = Keys[code]

    LibHokusai.hoku_input_keyboard_start(input.raw)
    Modifiers.each do |(sdlk, hkey)|
      if modifier & sdlk == sdlk
        LibHokusai.hoku_input_keyboard_set_key(input.raw, hkey, true)
      end
    end

    LibHokusai.hoku_input_keyboard_set_key(input.raw, hkey, true) unless hkey.nil?
    LibHokusai.hoku_input_keyboard_stop(input.raw)

    return true
  when SDL::KEYUP
    modifier = event[:key][:keysym][:mod]
    code = event[:key][:keysym][:sym]
    hkey = Keys[code]

    LibHokusai.hoku_input_keyboard_start(input.raw)
    Modifiers.each do |(sdlk, hkey)|
      if modifier & sdlk == sdlk
        LibHokusai.hoku_input_keyboard_set_key(input.raw, hkey, false)
      end
    end

    LibHokusai.hoku_input_keyboard_set_key(input.raw, hkey, false) unless hkey.nil?
    LibHokusai.hoku_input_keyboard_stop(input.raw)
    return true
  when SDL::WINDOWEVENT
    if event[:window][:event] == SDL::WINDOWEVENT_RESIZED
      self.render_width = event[:window][:data1]
      self.render_height = event[:window][:data2]

      # free all static font textures
      self.class.fonts.each do |k, payload|
        texture, *_ = payload

        SDL.DestroyTexture(texture)
      end

      self.class.fonts.clear
    elsif event[:window][:event] == SDL::WINDOWEVENT_MOVED
      # pp ["moved", Time.now.strftime("%H:%M:%S %L")]
    end

    return true
  when SDL::MOUSEMOTION
    if input.raw[:mouse][:delta][:y] > 0
      # LibHokusai.hoku_input_mouse_set_scroll(input.raw, 0.0)
      input.raw[:mouse][:delta][:y] = 0.0
      input.raw[:mouse][:scroll_delta] = 0.0
      input.raw[:mouse][:scroll] = event[:motion][:y]
    end

    # pp ["set mouse", Time.now.strftime("%H:%M:%S %L")]
    hoku_mouse_pos = LibHokusai::HmlVec2.create(event[:motion][:x], event[:motion][:y])
    LibHokusai.hoku_input_set_mouse_position(input.raw, hoku_mouse_pos)

    # clear any click events
    [[:left, 0], [:right, 2], [:middle, 1]].each do |(btn, i)|
      button = input.raw[:mouse][btn]
      button[:clicked] = false
      LibHokusai.hoku_input_mouse_set_button(input.raw, button, i)
    end

    return true
  when SDL::MOUSEWHEEL
    LibHokusai.hoku_input_mouse_set_scroll(input.raw, event[:wheel][:preciseY])
    input.raw[:mouse][:delta][:y] = event[:wheel][:preciseY]

    # clear any click events
    [[:left, 0], [:right, 2], [:middle, 1]].each do |(btn, i)|
      button = input.raw[:mouse][btn]
      button[:clicked] = false
      LibHokusai.hoku_input_mouse_set_button(input.raw, button, i)
    end
    return true
  when SDL::MOUSEBUTTONDOWN
    clicked = event[:button][:clicks] > 0
    button = mouse_button(down: true, clicked: clicked)

    # LibHokusai.hoku_input_mouse_set_scroll(input.raw, 0.0)
    # input.raw[:mouse][:delta][:y] = 0.0

    LibHokusai.hoku_input_mouse_set_button(input.raw, button, event[:button][:which])
    return true
  when SDL::MOUSEBUTTONUP
    button = mouse_button(up: true)
    #
    # LibHokusai.hoku_input_mouse_set_scroll(input.raw, 0.0)
    # input.raw[:mouse][:delta][:y] = 0.0

    LibHokusai.hoku_input_mouse_set_button(input.raw, button, event[:button][:which])
    return true
  when SDL::TEXTINPUT
    return false
  else
    false
  end
end

#register_command_handlers(renderer, window) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'ui/src/hokusai/backends/sdl2.rb', line 312

def register_command_handlers(renderer, window)
  Hokusai.on_set_mouse_cursor do |type|
    sdl = self.class.cursors[type]
    SDL.SetCursor(sdl)
  end

  Hokusai.on_maximize_window do
    SDL.MaximizeWindow(window)
  end

  Hokusai.on_close_window do
    self.class.stopped = true
  end

  Hokusai.on_minimize_window do
    SDL.MinimizeWindow(window)
  end

  Hokusai.on_set_window_position do |(ox, oy)|
    xp = FFI::MemoryPointer.new :int
    yp = FFI::MemoryPointer.new :int

    SDL.GetWindowPosition(window, xp, yp)

    x = xp.read_int
    y = yp.read_int

    SDL.SetWindowPosition(window, (x + ox).to_i, (y + oy).to_i)
    xp.free
    yp.free
  end

  Hokusai.on_renderable do |canvas|
    inside_scissor(canvas.x, canvas.y, canvas.height)
  end

  Hokusai::Commands::ScissorBegin.on_draw do |command|
    rect = sdl_rect(command.x, command.y, command.width, command.height)
    SDL.RenderSetClipRect(renderer, rect)

    @scissor = [command.x, command.y, command.width, command.height]
  end

  Hokusai::Commands::ScissorEnd.on_draw do |_|
    SDL.RenderSetClipRect(renderer, nil)

    @scissor = nil
  end

  Hokusai::Commands::Circle.on_draw do |command|
    next unless inside_scissor(command.x, command.y)

    if command.outline > 0 && command.outline_color.a > 0
      SDL.filledCircleRGBA(
        renderer,
        command.x,
        command.y,
        command.radius + command.outline,
        command.color.r, command.color.g, command.color.b, command.color.a)
    end

    SDL.filledCircleRGBA(
      renderer,
      command.x,
      command.y,
      command.radius,
      command.color.r, command.color.g, command.color.b, command.color.a
    )
  end

  Hokusai::Commands::Image.on_draw do |command|
    next unless inside_scissor(command.x, command.y, command.height)

    texture = begin
                if self.class.images[command.source]
                  self.class.images[command.source]
                else
                  surface_ptr = SDL.IMG_Load(command.source)
                  if surface_ptr.null?
                    raise Hokusai::Error.new("Can't load Image: #{SDL.GetError.read_string}")
                  end

                  texture = SDL.CreateTextureFromSurface(renderer, surface_ptr)
                  self.class.images[command.source] = texture
                  SDL.FreeSurface(surface_ptr)
                  texture
                end
              end

    rect = sdl_rect(command.x, command.y, command.width, command.height)
    SDL.RenderCopy(renderer, texture, nil, rect)
  end

  Hokusai::Commands::Rect.on_draw do |command|
    next unless inside_scissor(command.x, command.y, command.height)
    # draw background first
    x, y, w, h = [command.x, command.y, command.width, command.height]

    if command.outline_uniform? && command.rounding > 0 && command.outline_color.a > 0
      SDL.roundedBoxRGBA(
        renderer, x, y, x + w, y + h, (command.rounding).ceil.to_i,
        command.outline_color.r,
        command.outline_color.g,
        command.outline_color.b,
        command.outline_color.a
      )
    elsif command.outline_uniform? && command.rounding <= 0 && command.outline_color.a > 0
      SDL.boxRGBA(
        renderer, x, y, x + w, y + h,
        command.outline_color.r,
        command.outline_color.g,
        command.outline_color.b,
        command.outline_color.a
      )
    elsif command.outline_color.a > 0 && command.rounding <= 0
      # x, y, w, h = command.background_boundary
      if command.outline.top > 0
        SDL.thickLineRGBA(
          renderer,
          x, y + (command.outline.top / 2), x + w, y + (command.outline.top / 2),
          command.outline.top,
          command.outline_color.r,
          command.outline_color.g,
          command.outline_color.b,
          command.outline_color.a
        )
      end

      if command.outline.right > 0
        SDL.thickLineRGBA(
          renderer,
          x + w - (command.outline.right / 2), y, x + w - (command.outline.right / 2), y + h,
          command.outline.right,
          command.outline_color.r,
          command.outline_color.g,
          command.outline_color.b,
          command.outline_color.a
        )
      end

      if command.outline.bottom > 0
        SDL.thickLineRGBA(
          renderer,
          x, y + h - (command.outline.bottom / 2), x + w, y + h - (command.outline.bottom / 2),
          command.outline.bottom,
          command.outline_color.r,
          command.outline_color.g,
          command.outline_color.b,
          command.outline_color.a
        )
      end

      if command.outline.left > 0
        SDL.thickLineRGBA(
          renderer,
          x + (command.outline.left / 2), y , x + (command.outline.left / 2), y + h,
          command.outline.left,
          command.outline_color.r,
          command.outline_color.g,
          command.outline_color.b,
          command.outline_color.a
        )
      end
    end


    x, y, w, h = command.background_boundary
    rounding = command.rounding

    if command.rounding > 0
      pp command.color
      SDL.roundedBoxRGBA(renderer, x, y, x + w, y + h, (command.rounding).ceil.to_i, command.color.r, command.color.g, command.color.b, command.color.a)
    else
      rect = SDL::Rect.new
      rect[:x] = x
      rect[:y] = y
      rect[:w] = w
      rect[:h] = h

      SDL.SetRenderDrawColor(renderer, command.color.r, command.color.g, command.color.b, command.color.a)
      SDL.RenderFillRect(renderer, rect)
    end
  end

  Hokusai::Commands::Text.on_draw do |command|
    next unless inside_scissor(command.x, command.y, command.size)

    unless command.content.empty?
      payload = begin
                  if self.class.fonts[command.hash] && command.static
                    self.class.fonts[command.hash]
                  else
                    font = (!command.font.nil? ? Hokusai.fonts.get(command.font) : Hokusai.fonts.active)
                    font.size = command.size
                    surface_ptr = font.render(command.content, command.color, bold: command.bold, italic: command.italic)
                    texture = SDL.CreateTextureFromSurface(renderer, surface_ptr)
                    surface = SDL::Surface.new(surface_ptr)
                    load = [texture, surface[:w], surface[:h]]
                    self.class.fonts[command.hash] = load
                    SDL.FreeSurface(surface_ptr)
                    load
                  end
                end

      texture, width, height = payload

      SDL.RenderCopy(renderer, texture, nil, sdl_rect(
        command.x + command.padding.left,
        command.y + command.padding.top,
        width,# - (command.padding.left + command.padding.right),
        height# - (command.padding.top + command.padding.bottom)
      ))
      SDL.DestroyTexture(texture) unless command.static
    end
  end
end

#reset_keys(input) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
# File 'ui/src/hokusai/backends/sdl2.rb', line 159

def reset_keys(input)
  LibHokusai.hoku_input_keyboard_start(input.raw)
  Modifiers.each do |(sdlk, hkey)|
    LibHokusai.hoku_input_keyboard_set_key(input.raw, hkey, false)
  end

  Keys.values.each do |hkey|
    LibHokusai.hoku_input_keyboard_set_key(input.raw, hkey, false)
  end

  LibHokusai.hoku_input_keyboard_stop(input.raw)
end

#run(block) ⇒ Object



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
# File 'ui/src/hokusai/backends/sdl2.rb', line 59

def run(block)
  resize = true
  self.render_width = config.width
  self.render_height = config.height

  case RbConfig::CONFIG['host_os']
  when /darwin/
    SDL.load_lib(
      "#{SDL_PATH}/libSDL2.dylib",
      true,
      gfx_libpath: "#{SDL_PATH}/libSDL2_gfx.dylib",
      ttf_libpath: "#{SDL_PATH}/libSDL2_ttf.dylib",
      image_libpath: "#{SDL_PATH}/libSDL2_image.dylib"
    )
  when /mswin|msys|mingw/
    SDL.load_lib("#{SDL_PATH}/SDL2.dll", true, gfx_libpath: "#{SDL_PATH}/libSDL2_gfx.dll" )
  when /linux/
    SDL.load_lib(
      "#{SDL_PATH}/libSDL2.so", true,
      gfx_libpath: "#{SDL_PATH}/libSDL2_gfx.so",
      ttf_libpath: "#{SDL_PATH}/libSDL2_ttf.so",
      image_libpath: "#{SDL_PATH}/libSDL2_image.so"
    )
  end
  SDL.Init(config.init_flags)
  SDL.TTF_Init

  window = SDL.CreateWindow(config.title, 30, 30, render_width, render_height, config.window_config_flags)
  SDL.SetWindowPosition(window, SDL::WINDOWPOS_CENTERED_MASK, SDL::WINDOWPOS_CENTERED_MASK)
  renderer = SDL.CreateRenderer(window, -1, SDL::RENDERER_TARGETTEXTURE | SDL::RENDERER_ACCELERATED)

  Hokusai.fonts.register "default", SDLBackend::Font.from("#{__dir__}/sdl2/Monaco.ttf", 121)
  Hokusai.fonts.activate "default"
  config.after_load_cb&.call

  register_command_handlers(renderer, window)
  # MemoryProfiler.start if ENV["PROFILE"]

  ptr = FFI::MemoryPointer.new :pointer
  LibHokusai.hoku_input_init(ptr)
  raw = LibHokusai::HmlInput.new(ptr.get_pointer(0))
  input = Hokusai::Input.new(raw)
  canvas = Hokusai::Canvas.new(render_width.to_f, render_height.to_f)
  event = SDL::Event.new

  loop do
    SDL.WaitEvent(event)
    break if event[:common][:type] == SDL::QUIT || self.class.stopped
    if process_input(input, event)
      # since we are using wait event, we need to process the render twice
      # once to capture all events, and once after updating block state.
      SDL.SetRenderDrawBlendMode(renderer, SDL::BLENDMODE_NONE)
      SDL.SetRenderDrawColor(renderer, config.background[:r], config.background[:g], config.background[:b], config.background[:a])
      SDL.RenderClear(renderer)
      canvas.reset(nil, nil, render_width.to_f, render_height.to_f)
      painter = Hokusai::Painter.new(block, input)
      painter.render(canvas, resize)
      block.update
      SDL.RenderPresent(renderer)

      SDL.SetRenderDrawBlendMode(renderer, SDL::BLENDMODE_NONE)
      SDL.SetRenderDrawColor(renderer, config.background[:r], config.background[:g], config.background[:b], config.background[:a])
      SDL.RenderClear(renderer)
      canvas.reset(nil, nil, render_width.to_f, render_height.to_f)
      painter = Hokusai::Painter.new(block, input)
      painter.render(canvas, resize, capture: false)
      SDL.RenderPresent(renderer)
    end
  end

  LibHokusai.hoku_input_free(input.raw)
  if ENV["PROFILE"]
    # report = MemoryProfiler.stop
    # report.pretty_print
  end
end

#sdl_rect(x, y, w, h) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'ui/src/hokusai/backends/sdl2.rb', line 149

def sdl_rect(x, y, w, h)
  @sdl_rect ||= SDL::Rect.new
  @sdl_rect[:x] = x
  @sdl_rect[:y] = y
  @sdl_rect[:w] = w
  @sdl_rect[:h] = h

  @sdl_rect
end