Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. ; MINI.ASM
  2. ; --------
  3. ; Minimalistic uFMOD usage example.
  4.  
  5. ; Shows how to play an XM track in memory,
  6. ; including proper error handling.
  7.  
  8. BITS 32
  9. org 0
  10. db "MENUET01"
  11. dd 1
  12. dd START         ; Entry point
  13. dd uFMOD_IMG_END ; End of code and initialized data
  14. dd MEMORY_END    ; End of uninitialized (BSS) data
  15. dd STACK_B       ; Bottom of the stack
  16. dd 0             ; Args
  17. dd 0             ; Reserved
  18.  
  19. ; uFMOD setup:
  20. %define f48000   ; Set sampling rate to 48KHz  (22050, 44100, 48000)
  21. %define STRONG   ; Select STRONG interpolation (NONE, WEAK, STRONG)
  22. %define UNSAFE   ; Select UNSAFE mode          (NORMAL, UNSAFE)
  23. %define NODEBUG  ; Skip debug-board messages
  24. %define NOLINKER ; Select "no linker" mode
  25.  
  26. ; uFMOD constants:
  27. %define uFMOD_MIN_VOL     0
  28. %define uFMOD_MAX_VOL     25
  29. %define uFMOD_DEFAULT_VOL 25
  30.  
  31. ; The XM track.
  32. xm        incbin "..\ufmodlib\media\mini.xm"
  33. xm_length equ $ - xm
  34.  
  35. ; Optimization:
  36. ; This header file is suitable for mini.xm track only!
  37. ; If you change the track, update the optimization header.
  38. ; (Use the standart eff.inc file for a general purpose player app.)
  39. %include "..\ufmodlib\media\mini.eff.inc"
  40.  
  41. ; UI text messages.
  42. msg_txt   db "uFMOD ruleZ!"
  43. msg_txt_l equ $ - msg_txt
  44. msg_cap   db "NASM",0
  45. err_txt   db "Error"
  46. err_txt_l equ $ - err_txt
  47. err_cap   db ":-(",0
  48.  
  49. START:
  50.         ; Start playback.
  51.         push XM_MEMORY
  52.         push xm_length
  53.         push xm
  54.         call _uFMOD_LoadSong
  55.  
  56.         ; Stack fixing is required here, but in this simple
  57.         ; example leaving ESP as it is won't harm. In a real
  58.         ; application you should uncomment the following line:
  59.         ; add esp,12
  60.  
  61.         test eax,eax
  62.         jz error
  63.  
  64.         ; Wait for user input.
  65.         push _uFMOD_WaveOut ; cbProc <- continue fetching data!
  66.         push msg_txt_l      ; cbString
  67.         push msg_txt        ; lpString
  68.         push msg_cap        ; szCap
  69.         call _MessageBoxCB
  70.         ; add esp,16
  71.  
  72.         ; Stop playback.
  73.         call _uFMOD_StopSong
  74.  
  75. r:      ; Exit.
  76.         xor eax,eax
  77.         dec eax
  78.         int 40h
  79.  
  80. error:
  81.         push 0              ; cbProc <- no callback
  82.         push err_txt_l      ; cbString
  83.         push err_txt        ; lpString
  84.         push err_cap        ; szCap
  85.         call _MessageBoxCB
  86.         ; add esp,16
  87.         jmp r
  88.  
  89. ; ---------------------------------------------------------------
  90. ; void _cdecl _MessageBoxCB(szCap, lpString, cbString, cbProc);
  91. ; ---------------------------------------------------------------
  92.  
  93. ; This is similar to a Win32 MessageBox. The box is centered
  94. ; on screen. It contains a single-line text message and an
  95. ; "OK" button. This function returns when user closes the
  96. ; box (via the X button or via the OK button). An optional
  97. ; callback subroutine may be specified to be called when no
  98. ; events are pending in the event queue.
  99.  
  100. ; NOTE: Doesn't work if you already have defined a window
  101. ; in the current process! Doesn't modify the event mask. So,
  102. ; make sure keyboard events are enabled before calling this
  103. ; function. This function doesn't check the validity of the
  104. ; supplied parameters!
  105.  
  106. ; Parameters:
  107. ; szCap     - A pointer to the ASCIIz string containing the
  108. ;             caption. A trailing zero char IS required.
  109. ; lpString  - A pointer to an ASCII string containing a single
  110. ;             line message to pop up in the box. No trailing
  111. ;             zero char is required.
  112. ; cbString  - number of characters in string.
  113. ; cbProc    - Address of the callback subroutine. Can be NULL.
  114.  
  115. sOK db "OK"
  116. _MessageBoxCB:
  117.         push ebp
  118.         push esi
  119.         push edi
  120.         push ebx
  121.         xor ebp,ebp      ; global 0
  122.         mov esi,[esp+28] ; cbString
  123.         mov edi,[esp+20] ; szCap
  124.  
  125.         ; Get screen metrics.
  126.         lea eax,[ebp+14]
  127.         int 40h
  128.         mov ecx,eax
  129.         movzx eax,ax
  130.         shr ecx,16         ; screen w
  131.         xchg eax,edx       ; screen h
  132.         lea ebx,[esi*2+esi]
  133.         lea ebx,[ebx*2+28] ; w = string len * 6 + 28
  134.         sub ecx,ebx
  135.         shr ecx,1
  136.         shl ecx,16
  137.         or ebx,ecx
  138.         lea ecx,[ebp+52h]  ; h = 52h
  139.         sub edx,ecx
  140.         shr edx,1
  141.         shl edx,16
  142.         or ecx,edx         ; y = (screen h - window h) / 2
  143.         mov edx,ebx        ; x = (screen w - window w) / 2
  144.  
  145. _MessageBoxCB_redraw:
  146.         ; Start redraw.
  147.         push edx
  148.         lea eax,[ebp+12]
  149.         lea ebx,[ebp+1]
  150.         int 40h
  151.  
  152.         ; Define and draw window.
  153.         xor eax,eax
  154.         mov ebx,edx        ; x, w (ECX: y, h)
  155.         mov edx,34C0C0C0h  ; style and BG color
  156.         int 40h
  157.  
  158.         ; Define the OK button.
  159.         push esi
  160.         lea eax,[ebp+8]
  161.         sub ebx,28+0Ah
  162.         shr bx,1
  163.         shl ebx,16         ; x = (window w - button w) / 2
  164.         mov bx,18+0Ah      ; w = 18 + 0Ah
  165.         mov ecx,001C0012h  ; y = 1Ch, h = 12h
  166.         lea edx,[ebp+1]    ; ID = close
  167.         mov esi,0C0C0C0h   ; color
  168.         int 40h
  169.  
  170.         ; Draw the OK label.
  171.         lea eax,[ebp+4]
  172.         add ebx,90000h     ; x = button x + 9
  173.         mov bx,22h         ; y = 22h
  174.         xor ecx,ecx        ; style, font and color
  175.         mov edx,sOK        ; string
  176.         lea esi,[ebp+2]    ; length
  177.         int 40h
  178.         pop esi
  179.  
  180.         ; Draw text string.
  181.         lea eax,[ebp+4]
  182.         mov ebx,000A000Ah  ; x = 0Ah, y = 0Ah
  183.         xor ecx,ecx        ; style, font and color
  184.         mov edx,[esp+28]   ; lpString
  185.         int 40h
  186.  
  187.         ; End redraw.
  188.         lea eax,[ebp+12]
  189.         lea ebx,[ebp+2]
  190.         int 40h
  191.  
  192. _MessageBoxCB_eventloop:
  193.         mov edx,[esp+36]   ; cbProc
  194.         test edx,edx
  195.         lea eax,[ebp+10]
  196.         jz _MessageBoxCB_peekevent
  197.  
  198.         ; Invoke the callback.
  199.         call edx
  200.  
  201.         lea eax,[ebp+23]
  202.         lea ebx,[ebp+10] ; wait for at most 0.1 sec
  203. _MessageBoxCB_peekevent:
  204.         int 40h
  205.         dec eax
  206.         js _MessageBoxCB_eventloop
  207.  
  208.         pop edx
  209.         jz _MessageBoxCB_redraw
  210.  
  211.         pop ebx
  212.         pop edi
  213.         pop esi
  214.         pop ebp
  215.         ret
  216.  
  217.         ; Include the whole uFMOD sources here. (Right after
  218.         ; your main code to avoid naming conflicts, but still
  219.         ; inside your code section.)
  220.         %include "nasm.asm"
  221.  
  222. alignb 4
  223.         resb 1020
  224. STACK_B resd 1 ; Stack bottom
  225. MEMORY_END:    ; End of uninitialized (BSS) data
  226.