background_loading.bmx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. SuperStrict
  2. ?win32
  3. Framework SDL.d3d9sdlmax2d
  4. ?Not win32
  5. Framework SDL.gl2sdlmax2d
  6. ?
  7. Import brl.pngloader
  8. Import brl.map
  9. ' -----------------------------------------------------------------------------
  10. ' MAKE SURE "Threaded Build" IS CHECKED IN THE Program -> Build Options menu!
  11. ' -----------------------------------------------------------------------------
  12. ' -----------------------------------------------------------------------------
  13. ' Loading screen...
  14. ' -----------------------------------------------------------------------------
  15. AppTitle = "Multi-threaded loading screen demo..."
  16. ' How to display an animated loading screen while loading images...
  17. ' Because only the main program thread can interact with DirectX/OpenGL,
  18. ' we have to use BlitzMax TPixmaps in the threaded loading routine.
  19. ' That's because BlitzMax's TImage is tied to the DirectX/OpenGL 'context',
  20. ' while pixmaps are just blocks of memory that the CPU can manipulate.
  21. ' After loading from disk, they can be 'loaded' from the in-memory TPixmap
  22. ' into proper images via LoadImage.
  23. ' You could just use DrawPixmap to skip this step, but you then can't use'
  24. ' realtime scaling, rotation, etc.
  25. ' The threaded function LoadPixmaps is at the bottom of this code...
  26. ' -----------------------------------------------------------------------------
  27. ' This is used to simulate slower loading in the LoadPixmaps thread...
  28. ' -----------------------------------------------------------------------------
  29. Global TestDelay:Int = 1000 ' Simulating more/larger images, 3D models, etc...
  30. ' -----------------------------------------------------------------------------
  31. ' Set up global TMap...
  32. ' -----------------------------------------------------------------------------
  33. Global Pixmaps:TMap = CreateMap ()
  34. ' -----------------------------------------------------------------------------
  35. ' Add list of pixmap filenames to be added to the Pixmaps TMap...
  36. ' -----------------------------------------------------------------------------
  37. AddPixmap ("bluboing.png")
  38. AddPixmap ("bluegem.png")
  39. AddPixmap ("boing.png")
  40. AddPixmap ("dead.png")
  41. AddPixmap ("greengem.png")
  42. AddPixmap ("redgem.png")
  43. ' -----------------------------------------------------------------------------
  44. ' Set up display...
  45. ' -----------------------------------------------------------------------------
  46. Graphics 640, 480, 0
  47. SetClsColor 32, 96, 128
  48. SetMaskColor 255, 0, 255
  49. AutoMidHandle True
  50. ' -----------------------------------------------------------------------------
  51. ' Start the LoadPixmaps thread...
  52. ' -----------------------------------------------------------------------------
  53. Local thread:TThread = CreateThread (LoadPixmaps, Null)
  54. ' -----------------------------------------------------------------------------
  55. ' This is the loading screen! Some movement and colours while pixmaps load...
  56. ' -----------------------------------------------------------------------------
  57. Local r:Int = 0
  58. Local g:Int = 255
  59. Local b:Int = 127
  60. ' -----------------------------------------------------------------------------
  61. ' Do this routine until the thread has finished its work...
  62. ' -----------------------------------------------------------------------------
  63. While ThreadRunning (thread) ' Important!
  64. Cls
  65. r = r + 8; If r > 255 Then r = 0
  66. g = g - 4; If g > 255 Then g = 0
  67. b = b + 2; If b > 255 Then b = 0
  68. SetColor 0, 0, 0
  69. DrawRect MouseX (), MouseY (), 32, 32
  70. SetColor r, g, b
  71. DrawRect MouseX (), MouseY (), 30, 30
  72. SetColor 0, 0, 0
  73. DrawText "Slow-ding, please wait...", 20, 20
  74. SetColor 255, 255, 255
  75. DrawText "Slow-ding, please wait...", 18, 18
  76. Flip
  77. Wend
  78. ' -----------------------------------------------------------------------------
  79. ' Right, the thread has finished. Should have a nice TMap filled with pixmaps!
  80. ' -----------------------------------------------------------------------------
  81. ' Just re-setting colours, 'scuse me...
  82. r = 255; g = 255; b = 255
  83. SetColor r, g, b
  84. ' -----------------------------------------------------------------------------
  85. ' Create a list of TImage objects and load the pixmaps into them...
  86. ' -----------------------------------------------------------------------------
  87. Local images:TList = CreateList ()
  88. For Local p:String = EachIn MapKeys (Pixmaps)
  89. ListAddLast images, LoadImage (TPixmap (MapValueForKey (Pixmaps, p$)))
  90. Next
  91. ' In reality, you would probably load each image based on the filename in the
  92. ' map. You could just pass each filename you passed to AddPixmap at the start,
  93. ' for example (untested)...
  94. ' rocket:TImage = LoadImage (TPixmap (MapValueForKey (Pixmaps, "boing.png")))
  95. ' -----------------------------------------------------------------------------
  96. ' Free the map and all TPixmap objects it holds...
  97. ' -----------------------------------------------------------------------------
  98. ClearMap Pixmaps
  99. ' -----------------------------------------------------------------------------
  100. ' Yay... into the main game! Woo! Fun!
  101. ' -----------------------------------------------------------------------------
  102. Local ang:Float
  103. Repeat
  104. Cls
  105. Local x:Int = 0
  106. Local y:Int = 0
  107. SetRotation ang; ang = ang + 1; If ang > 360 Then ang = 0
  108. ' Draw all images...
  109. For Local i:TImage = EachIn images
  110. DrawImage i, x, y
  111. x = x + 96
  112. y = y + 96
  113. Next
  114. SetRotation 0
  115. SetColor 0, 0, 0
  116. DrawRect MouseX (), MouseY (), 32, 32
  117. SetColor 255, 255, 255
  118. DrawRect MouseX (), MouseY (), 30, 30
  119. SetColor 0, 0, 0
  120. DrawText "All done! We're in-game now! Fun, fun, fun...", 20, 20
  121. SetColor 255, 255, 255
  122. DrawText "All done! We're in-game now! Fun, fun, fun...", 18, 18
  123. Flip
  124. Until KeyHit (KEY_ESCAPE)
  125. End
  126. ' -----------------------------------------------------------------------------
  127. ' Helper function for anyone scared of maps...
  128. ' -----------------------------------------------------------------------------
  129. Function AddPixmap (p$)
  130. ' Maps are similar to lists, but associated two values with each other;
  131. ' in this case, a filename and a TPixmap pointer, which is Null here.
  132. ' The LoadPixmaps function will load the pixmap for each filename in the
  133. ' map, and associated the resulting TPixmap with that filename.
  134. MapInsert (Pixmaps, p$, New TPixmap)
  135. End Function
  136. ' -----------------------------------------------------------------------------
  137. ' The threaded pixmap loading function...
  138. ' -----------------------------------------------------------------------------
  139. ' No mutexes are needed here since the global Pixmaps:TMap is only accessed by
  140. ' the main program after this thread is finished...
  141. Function LoadPixmaps:Object (data:Object)
  142. ' Iterate through the global Map...
  143. For Local p:String = EachIn MapKeys (Pixmaps)
  144. ' Load pixmaps into the existing [Null] TPixmap slots for each
  145. ' filename...
  146. Local pix:TPixmap = LoadPixmap (p$)
  147. MapInsert (Pixmaps, p$, pix)
  148. ' Fake delay to simulate loading bigger images for this demo!
  149. Delay TestDelay
  150. Next
  151. End Function