background_loading.bmx 6.5 KB

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