doomfire 0.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ecbc683e034fa23927451cf7792b1b234ad41d45681a1eb59d05759f44b65304
4
- data.tar.gz: 7823ae5ad2eb86a5d090bd914193ae7c56c9b92b19b411457e6c3600884fd2fd
3
+ metadata.gz: 67bdf9fa995a19a70b46f2707adc899ee31780968ffb9f1d0fe4544f6f225313
4
+ data.tar.gz: 9d1234e1894955dd50614b9bbf85edcb0a2665980baac220fb23696568e27792
5
5
  SHA512:
6
- metadata.gz: 41592477937ec6d1fc954646117f9044c76714701eaa5a6784d509b6d70a12386afd84a25cc64db3840c4fd11dac6db82ec40c8637b8f01c67760e45b8a57eed
7
- data.tar.gz: 59087d4f46cb35fb1146a33f42cd43638d08c690a7cc2dd45456aa364c97d1fe5c8d77bdc3b0041a44b2ed3252b78452d98d7aa30f9c10553b9d5eeb794866ef
6
+ metadata.gz: e596f6b9b137e0343a106a2ee6d9371389ad88c0877ebbe7f72a919ecd60488c0012afb71ee63fa6b3dc226607d37ca32ffeafe139114e14e328f6df37a0fb66
7
+ data.tar.gz: f9e5d75205b31479ffffdb22014e2f965223dd58c9aa0fab17b5e7b5581b8ba2f5d2d932d3af3daa31b7892ad67563733e18ffc876276926403071f0273b253c
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # 0.3.0
2
+
3
+ - [ ] SDL output in progress.
4
+ - [ ] Improve SDL performance
5
+ - [ ] https://stackoverflow.com/questions/20753726/rendering-pixels-from-array-of-rgb-values-in-sdl-1-2/36504803#36504803
6
+
1
7
  # 0.2.0
2
8
 
3
9
  Terminal class now only works in the terminal and the new Spinner class should be used in the rake tasks. That should actually be a major version update to be honest but in 0.x versions I assume it's expected things can break.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- doomfire (0.2)
4
+ doomfire (0.3.0)
5
5
  paint
6
6
 
7
7
  GEM
data/exe/doomfire CHANGED
@@ -4,5 +4,21 @@
4
4
  require 'rubygems'
5
5
  require 'bundler/setup'
6
6
  require 'doomfire'
7
+ require 'optparse'
7
8
 
8
- Doomfire::Terminal.new.run
9
+ options = {}
10
+ OptionParser.new do |opts|
11
+ opts.banner = 'Usage: doomfire [options]'
12
+
13
+ options[:sdl] = false
14
+ opts.on('--sdl', 'Execute SDL version') do
15
+ options[:sdl] = true
16
+ end
17
+
18
+ opts.on('-h', '--help', 'Display this screen') do
19
+ puts opts
20
+ exit
21
+ end
22
+ end.parse!
23
+
24
+ options[:sdl] ? Doomfire::SDL.new.run : Doomfire::Terminal.new.run
data/lib/doomfire/base.rb CHANGED
@@ -41,8 +41,6 @@ module Doomfire
41
41
  [255, 255, 255]
42
42
  ].freeze
43
43
 
44
- FIRE_HEIGHT = 35
45
-
46
44
  attr_reader :exit_requested, :pixels, :fire_width
47
45
 
48
46
  def initialize
@@ -81,7 +79,7 @@ module Doomfire
81
79
  end
82
80
 
83
81
  def update_pixels
84
- (1...FIRE_HEIGHT).to_a.reverse.each do |x|
82
+ (1...@fire_height).to_a.reverse.each do |x|
85
83
  (0...@fire_width).each do |y|
86
84
  spread_fire(x * @fire_width + y)
87
85
  end
@@ -90,13 +88,14 @@ module Doomfire
90
88
 
91
89
  def initialize_pixels
92
90
  @pixels = []
93
- last_line = @fire_width * (FIRE_HEIGHT - 1)
91
+ last_line = @fire_width * (@fire_height - 1)
94
92
  @pixels.fill(0, 0...last_line)
95
93
  @pixels.fill(34, last_line..(last_line + @fire_width))
96
94
  end
97
95
 
98
96
  def prepare_output
99
97
  @fire_width = 80
98
+ @fire_height = 35
100
99
  end
101
100
 
102
101
  def fire_loop
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fiddle'
4
+ require 'fiddle/import'
5
+
6
+ module Doomfire
7
+ # very minimal FFI bindings for libSDL2
8
+ module FFI_SDL
9
+ extend Fiddle::Importer
10
+
11
+ dlload 'libSDL2.dylib'
12
+
13
+ typealias 'Uint32', :int
14
+ typealias 'Uint16', :int
15
+ typealias 'Uint8', :int
16
+
17
+ SDL_INIT_VIDEO = 0x00000020
18
+ SDL_WINDOW_OPENGL = 0x00000000
19
+ SDL_WINDOWPOS_UNDEFINED = 0x1FFF0000 | 0
20
+ SDL_WINDOWPOS_CENTERED = 0x2FFF0000 | 0
21
+ SDL_QUIT = 0x100
22
+ SDL_KEYDOWN = 0x300
23
+ SDL_PIXELFORMAT_ARGB8888 = 372_645_892
24
+ SDL_TEXTUREACCESS_STATIC = 0
25
+ SDL_TEXTUREACCESS_STREAMING = 1
26
+
27
+ SDL_Event = union [
28
+ 'Uint32 type'
29
+ ]
30
+
31
+ # basic SDL functions - creating windows, events etc
32
+ extern 'int SDL_Init(Uint32 flags)'
33
+ extern 'void* SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)'
34
+ extern 'void* SDL_CreateRenderer(void* window, int index, Uint32 flags)'
35
+ extern 'int SDL_PollEvent(void* event)'
36
+ extern 'int SDL_RenderClear(void* renderer)'
37
+ extern 'void SDL_RenderPresent(void* renderer)'
38
+ extern 'void SDL_Quit(void)'
39
+
40
+ # software-accelerated rendering
41
+ extern 'int SDL_SetRenderDrawColor(void* renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a)'
42
+ extern 'int SDL_RenderDrawPoint(void* renderer, int x, int y)'
43
+
44
+ # textures are hardware accelerated
45
+ extern 'void* SDL_CreateTexture(void* renderer, Uint32 format, int access, int w, int h)'
46
+ extern 'int SDL_RenderCopy(void* renderer, void* texture, const void* srcrect, const void* dstrect)'
47
+ extern 'int SDL_UpdateTexture(void* texture, const void* rect, const void* pixels, int pitch)'
48
+ end
49
+ end
@@ -0,0 +1,129 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pry'
4
+
5
+ module Doomfire
6
+ # Output to a separate SDL window
7
+ class SDL < Base
8
+ extend Doomfire::FFI_SDL
9
+
10
+ ARGB = [
11
+ 0x00000000,
12
+ 0x00070707,
13
+ 0xFF1F0707,
14
+ 0xFF2F0F07,
15
+ 0xFF571707,
16
+ 0xFF671F07,
17
+ 0xFF771F07,
18
+ 0xFF8F2707,
19
+ 0xFF9F2F07,
20
+ 0xFFAF3F07,
21
+ 0xFFBF4707,
22
+ 0xFFC74707,
23
+ 0xFFDF4F07,
24
+ 0xFFDF5707,
25
+ 0xFFDF5707,
26
+ 0xFFD7670F,
27
+ 0xFFCF6F0F,
28
+ 0xFFCF770F,
29
+ 0xFFCF7F0F,
30
+ 0xFFCF8717,
31
+ 0xFFC78717,
32
+ 0xFFC78F17,
33
+ 0xFFC7971F,
34
+ 0xFFBF9F1F,
35
+ 0xFFBF9F1F,
36
+ 0xFFBFA727,
37
+ 0xFFBFA727,
38
+ 0xFFBFAF2F,
39
+ 0xFFB7AF2F,
40
+ 0xFFB7B72F,
41
+ 0xFFB7B737,
42
+ 0xFFCFCF6F,
43
+ 0xFFDFDF9F,
44
+ 0xFFEFEFC7,
45
+ 0xFFFFFFFF
46
+ ].freeze
47
+
48
+ def run
49
+ fire_loop
50
+ end
51
+
52
+ def stop
53
+ @exit_requested = true
54
+ end
55
+
56
+ private
57
+
58
+ def fire_loop
59
+ loop do
60
+ next if @event.nil? || @window.nil? || @renderer.nil? || @texture.nil?
61
+
62
+ FFI_SDL.SDL_PollEvent(@event)
63
+ @exit_requested = true if @event.type == FFI_SDL::SDL_QUIT
64
+
65
+ if @exit_requested
66
+ stop_fire if @counter.zero?
67
+ break if @counter == 60
68
+
69
+ @counter += 1
70
+ end
71
+
72
+ clear
73
+ update_pixels
74
+ print_pixels
75
+ end
76
+
77
+ clear_screen
78
+ end
79
+
80
+ def update_pixels
81
+ (0..@fire_width).each do |x|
82
+ (1...@fire_height).each do |y|
83
+ spread_fire(y * @fire_width + x)
84
+ end
85
+ end
86
+
87
+ @texdata = @pixels.map { |val| ARGB[val] }
88
+ @texptr = Fiddle::Pointer[@texdata.pack('L*')]
89
+ end
90
+
91
+ def prepare_output
92
+ @fire_width = 640
93
+ @fire_height = 480
94
+
95
+ FFI_SDL.SDL_Init(FFI_SDL::SDL_INIT_VIDEO)
96
+ @window = FFI_SDL.SDL_CreateWindow(
97
+ 'Doomfire.rb',
98
+ FFI_SDL::SDL_WINDOWPOS_UNDEFINED,
99
+ FFI_SDL::SDL_WINDOWPOS_UNDEFINED,
100
+ @fire_width,
101
+ @fire_height,
102
+ FFI_SDL::SDL_WINDOW_OPENGL
103
+ )
104
+ @renderer = FFI_SDL.SDL_CreateRenderer(@window, -1, 0)
105
+ @texture = FFI_SDL.SDL_CreateTexture(
106
+ @renderer,
107
+ FFI_SDL::SDL_PIXELFORMAT_ARGB8888,
108
+ FFI_SDL::SDL_TEXTUREACCESS_STREAMING,
109
+ @fire_width,
110
+ @fire_height
111
+ )
112
+ @event = FFI_SDL::SDL_Event.malloc
113
+ end
114
+
115
+ def clear
116
+ FFI_SDL.SDL_RenderClear(@renderer)
117
+ end
118
+
119
+ def print_pixels
120
+ FFI_SDL.SDL_UpdateTexture(@texture, nil, @texptr, 4 * @fire_width)
121
+ FFI_SDL.SDL_RenderCopy(@renderer, @texture, 0, 0)
122
+ FFI_SDL.SDL_RenderPresent(@renderer)
123
+ end
124
+
125
+ def clear_screen
126
+ FFI_SDL.SDL_Quit()
127
+ end
128
+ end
129
+ end
@@ -16,6 +16,7 @@ module Doomfire
16
16
  private
17
17
 
18
18
  def prepare_output
19
+ @fire_height = 35
19
20
  @fire_width = Doomfire::WindowSize.new.terminal_width
20
21
  Paint.mode = 0xFFFFFF
21
22
  clear_screen
@@ -19,7 +19,7 @@ module Doomfire
19
19
  loop do
20
20
  if @exit_requested
21
21
  stop_fire if @counter.zero?
22
- break if @counter == FIRE_HEIGHT
22
+ break if @counter == @fire_height
23
23
 
24
24
  @counter += 1
25
25
  end
@@ -33,6 +33,7 @@ module Doomfire
33
33
  end
34
34
 
35
35
  def prepare_output
36
+ @fire_height = 35
36
37
  @fire_width = Doomfire::WindowSize.new.terminal_width
37
38
  Paint.mode = 0xFFFFFF
38
39
  Kernel.trap('INT') { @exit_requested = true }
@@ -45,7 +46,7 @@ module Doomfire
45
46
  end
46
47
 
47
48
  def print_pixels
48
- (0...FIRE_HEIGHT).each do |x|
49
+ (0...@fire_height).each do |x|
49
50
  (0...@fire_width).each do |y|
50
51
  print Paint[' ', nil, RGB[@pixels[x * @fire_width + y]]]
51
52
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Doomfire
4
- VERSION = '0.2'
4
+ VERSION = '0.3.0'
5
5
  end
data/lib/doomfire.rb CHANGED
@@ -3,5 +3,7 @@
3
3
  require 'doomfire/base'
4
4
  require 'doomfire/terminal'
5
5
  require 'doomfire/spinner'
6
+ require 'doomfire/ffi_sdl'
7
+ require 'doomfire/sdl'
6
8
  require 'doomfire/version'
7
9
  require 'doomfire/window_size'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: doomfire
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcin Ruszkiewicz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-29 00:00:00.000000000 Z
11
+ date: 2019-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: paint
@@ -99,11 +99,12 @@ files:
99
99
  - Rakefile
100
100
  - doomfire.gemspec
101
101
  - examples/long.rake
102
- - examples/sdl_test.rb
103
102
  - examples/terminal.png
104
103
  - exe/doomfire
105
104
  - lib/doomfire.rb
106
105
  - lib/doomfire/base.rb
106
+ - lib/doomfire/ffi_sdl.rb
107
+ - lib/doomfire/sdl.rb
107
108
  - lib/doomfire/spinner.rb
108
109
  - lib/doomfire/terminal.rb
109
110
  - lib/doomfire/version.rb
data/examples/sdl_test.rb DELETED
@@ -1,57 +0,0 @@
1
- require 'fiddle'
2
- require 'fiddle/import'
3
- require 'pry'
4
-
5
- module SDL
6
- extend Fiddle::Importer
7
-
8
- dlload 'libSDL2.dylib'
9
-
10
- typealias 'Uint32', :int
11
- typealias 'Uint16', :int
12
- typealias 'Uint8', :int
13
-
14
- SDL_INIT_VIDEO = 0x00000020
15
- SDL_WINDOW_OPENGL = 0x00000000
16
- SDL_WINDOWPOS_UNDEFINED = 0x1FFF0000 | 0
17
- SDL_WINDOWPOS_CENTERED = 0x2FFF0000 | 0
18
- SDL_QUIT = 0x100
19
- SDL_KEYDOWN = 0x300
20
- SDL_PIXELFORMAT_ARGB8888 = 0
21
- SDL_TEXTUREACCESS_STATIC = 0
22
-
23
- SDL_Event = union [
24
- 'Uint32 type'
25
- ]
26
-
27
- extern 'int SDL_Init(Uint32 flags)'
28
- extern 'void* SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)'
29
- extern 'void* SDL_CreateRenderer(void* window, int index, Uint32 flags)'
30
- extern 'void* SDL_CreateTexture(void* renderer, Uint32 format, int access, int w, int h)'
31
- extern 'int SDL_WaitEvent(void* event)'
32
- extern 'void SDL_Quit(void)'
33
- extern 'void SDL_DestroyWindow(void* window)'
34
- extern 'void SDL_DestroyRenderer(void* renderer)'
35
- extern 'void SDL_DestroyTexture(void* texture)'
36
- end
37
-
38
- # https://dzone.com/articles/sdl2-pixel-drawing
39
- # http://wiki.libsdl.org/SDL_Init
40
- # https://github.com/davidsiaw/SDL2/blob/master/include/SDL_stdinc.h
41
- # http://ruby-doc.org/stdlib-2.6.3/libdoc/fiddle/rdoc/Fiddle.html#method-c-dlopen
42
- # https://www.honeybadger.io/blog/use-any-c-library-from-ruby-via-fiddle-the-ruby-standard-librarys-best-kept-secret/
43
- # https://bitbucket.org/dandago/gigilabs/src/master/Sdl2PixelDrawing/Sdl2PixelDrawing/main.cpp
44
-
45
- SDL.SDL_Init(SDL::SDL_INIT_VIDEO)
46
- window = SDL.SDL_CreateWindow("Doomfire.rb", SDL::SDL_WINDOWPOS_UNDEFINED, SDL::SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL::SDL_WINDOW_OPENGL)
47
- renderer = SDL.SDL_CreateRenderer(window, -1, 0)
48
- texture = SDL.SDL_CreateTexture(renderer, SDL::SDL_PIXELFORMAT_ARGB8888, SDL::SDL_TEXTUREACCESS_STATIC, 640, 480)
49
-
50
- event = SDL::SDL_Event.malloc
51
- quit = false
52
-
53
- until quit
54
- SDL.SDL_WaitEvent(event)
55
-
56
- quit = true if event.type == SDL::SDL_QUIT
57
- end