_sprites.tcl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #attributes
  2. #expr {([vdpreg 11] << 15) + (([vdpreg 5] & 0xf8) << 7)}
  3. namespace eval osd_sprite_info {
  4. #todo: more sprite info / better layout (sprite mode/ hex values/ any other info)
  5. variable title_pos 0
  6. variable max_sprites 0
  7. proc sprite_viewer {} {
  8. bind -layer sprite_viewer "keyb LEFT" -repeat {osd_sprite_info::sprite_viewer_action 1}
  9. bind -layer sprite_viewer "keyb RIGHT" -repeat {osd_sprite_info::sprite_viewer_action 2}
  10. bind -layer sprite_viewer "keyb ESCAPE" -repeat {osd_sprite_info::sprite_viewer_hide}
  11. bind -layer sprite_viewer "keyb SPACE" -repeat {osd_sprite_info::sprite_viewer_action 0}
  12. activate_input_layer sprite_viewer
  13. osd create rectangle sprite_viewer -x 5 -y 100 -w 128 -h 190 -rgba 0x00000080
  14. osd create rectangle sprite_viewer.title -x 0 -y 15 -w 128 -h 32 -rgba 0x0000ff80 -clip on
  15. osd create text sprite_viewer.title.text -x 24 -text "" -size 18 -rgba 0xffffffff
  16. osd create text sprite_viewer.index -x 70 -y 38 -text "" -size 8 -rgba 0xffffffff
  17. osd create text sprite_viewer.refresh -x 8 -y 170 -size 8 -text "\[Space\] Refresh Sprite" -rgba 0xffffffff
  18. osd create text sprite_viewer.escape -x 8 -y 180 -size 8 -text "\[Escape\] to Exit Viewer" -rgba 0xffffffff
  19. sprite_viewer_action 0
  20. }
  21. proc sprite_viewer_action {action} {
  22. variable title
  23. variable title_pos
  24. variable max_sprites
  25. if {$action == 1} {incr title_pos -1}
  26. if {$action == 2} {incr title_pos 1}
  27. if {$title_pos > $max_sprites} {set title_pos 0}
  28. if {$title_pos < 0} {set title_pos $max_sprites}
  29. #show sprite matrix
  30. show_sprite $title_pos
  31. #fade/ease
  32. osd configure sprite_viewer.title.text -text "Sprite $title_pos" -fadeCurrent 0
  33. osd configure sprite_viewer.index -text "In mem: $max_sprites"
  34. ease_text sprite_viewer.title.text 0 $action
  35. }
  36. proc sprite_viewer_hide {} {
  37. osd destroy sprite_viewer
  38. deactivate_input_layer sprite_viewer
  39. }
  40. proc ease_text {osd_object {frame_render 0} {action 0}} {
  41. #Ease in length
  42. set x 16
  43. #Intro Fade
  44. if {$frame_render == 0} {
  45. osd configure $osd_object -x $x -fadeTarget 1 -fadePeriod 0.25
  46. }
  47. if {$frame_render > $x} {
  48. #leave routine
  49. return ""
  50. }
  51. #ease in
  52. if {$action == 2} {
  53. osd configure $osd_object -x [expr {$frame_render - $x}]
  54. } else {
  55. osd configure $osd_object -x [expr {$x - $frame_render}]
  56. }
  57. incr frame_render 4
  58. #call same function
  59. after realtime 0.05 [namespace code [list ease_text $osd_object $frame_render $action]]
  60. }
  61. proc show_sprite {sprite} {
  62. variable max_sprites
  63. set sprite_size [expr {([vdpreg 1] & 2) ? 16 : 8}]
  64. set factor [expr {($sprite_size == 8) ? 1 : 4}]
  65. set max_sprites [expr {(256 / $factor) - 1}]
  66. if {($sprite > $max_sprites) || ($sprite < 0)} {
  67. error "Please choose a value between 0 and $max_sprites"
  68. }
  69. osd destroy sprite_viewer.sprite_osd
  70. draw_matrix "sprite_viewer.sprite_osd" 7 56 7 $sprite_size 1
  71. set addr [expr {([vdpreg 6] << 11) + ($sprite * $factor * 8)}]
  72. for {set y 0} {$y < $sprite_size} {incr y; incr addr} {
  73. set pattern [vpeek $addr]
  74. set mask [expr {($sprite_size == 8) ? 0x80 : 0x8000}]
  75. if {$sprite_size == 16} {
  76. set pattern [expr {($pattern << 8) + [vpeek [expr {$addr + 16}]]}]
  77. }
  78. for {set x 0} {$x < $sprite_size} {incr x} {
  79. if {$pattern & $mask} {
  80. osd configure sprite_viewer.sprite_osd.${x}_${y} -rgba 0xffffffff
  81. }
  82. set mask [expr {$mask >> 1}]
  83. }
  84. }
  85. }
  86. proc draw_matrix {matrixname x y blocksize matrixsize matrixgap} {
  87. osd create rectangle $matrixname \
  88. -x $x \
  89. -y $y \
  90. -h [expr {$matrixsize * $blocksize}] \
  91. -w [expr {$matrixsize * $blocksize}] \
  92. -rgba 0x00000040
  93. for {set x 0} {$x < $matrixsize} {incr x} {
  94. for {set y 0} {$y < $matrixsize} {incr y} {
  95. osd create rectangle $matrixname.${x}_${y} \
  96. -h [expr {$blocksize - $matrixgap}] \
  97. -w [expr {$blocksize - $matrixgap}] \
  98. -x [expr {$x * $blocksize}] \
  99. -y [expr {$y * $blocksize}] \
  100. -rgba 0x0000ff80
  101. }
  102. }
  103. return ""
  104. }
  105. namespace export sprite_viewer
  106. } ;# namespace osd_menu
  107. namespace import osd_sprite_info::*