Dark Bit Factory & Gravity

PROGRAMMING => Other languages => ASM => Topic started by: Rbz on March 22, 2007

Title: The Coding Wizard - source code
Post by: Rbz on March 22, 2007
For Fasm lovers  :) , here's the source code for the 20 Seconds Compo.

Code: [Select]
;-------------------------------------------------------------------------
;
;               The Coding Wizard
;               20Secs Compo entry
;               by rbraz March 2007
;
;-------------------------------------------------------------------------

format PE GUI 4.0
entry start

include '%fasminc%\win32a.inc'
include '%fasminc%\ufmodlib\ufmod-const.inc'

;-------------------Equates
Scr_Width      equ 640
Scr_Height     equ 600
ChipIcon       equ 1

section '.data' data readable writeable

  wc WNDCLASS 0,WindowProc,0,0,NULL,NULL,NULL,COLOR_WINDOW,NULL,_class
  msg         MSG
  hwnd        dd ?
  hdc         dd ?
  rc          RECT
  ps          PAINTSTRUCT

  _class  db 'Win32',0
  _title  db '** The Coding Wizard ** 20 Secs Compo ** by rbraz 2007 **',0
  _error1 db 'Unable to create Window !',0
  _error2 db 'Unable to open Image !',0

  ;----------------Images
  _wizardImages: db 'media\wizard0.bmp',0
                 db 'media\wizard1.bmp',0
                 db 'media\wizard2.bmp',0
                 db 'media\wizard3.bmp',0
                 db 'media\wizard4.bmp',0

  _balloonImages: db 'media\balloon0.bmp',0
                  db 'media\balloon1.bmp',0
                  db 'media\balloon2.bmp',0
                  db 'media\balloon3.bmp',0
                  db 'media\balloon4.bmp',0
                  db 'media\balloon5.bmp',0

  _demoImages: db 'media\256b.bmp',0
               db 'media\1kb_.bmp',0
               db 'media\64kb.bmp',0
               db 'media\demo.bmp',0

  hBitmap     dd 15 dup(?)

  FontName db 'Times New Roman',0

  timerBuffer  rd 2

  startTime    dd 0

  elapsedTime  dd -1

  ;----Xm music file
  xm file 'media\uuaaiiuu.xm'
  xm_length = $ - xm


section '.code' code readable executable

  start:

        invoke  GetModuleHandle,0
        mov     [wc.hInstance], eax
        invoke  LoadIcon,0,IDI_APPLICATION
        mov     [wc.hIcon],eax
        invoke  LoadCursor,0,IDC_ARROW
        mov     [wc.hCursor],eax
        invoke  LoadIcon,[wc.hInstance],ChipIcon ;Chip Icon
        mov     [wc.hIcon], eax
        invoke  RegisterClass,wc
        test    eax,eax
        jz      error

        mov     [rc.left]   , 0
        mov     [rc.top]    , 0
        mov     [rc.right]  , Scr_Width
        mov     [rc.bottom] , Scr_Height
        invoke  GetSystemMetrics, SM_CXFIXEDFRAME
        shl     eax, 1
        add     [rc.right], eax
        invoke  GetSystemMetrics, SM_CYFIXEDFRAME
        shl     eax, 1
        add     [rc.bottom], eax
        invoke  GetSystemMetrics, SM_CYCAPTION
        add     [rc.bottom], eax
        ;---Center window
        invoke  GetSystemMetrics, SM_CXSCREEN
        sub     eax, Scr_Width
        shr     eax, 1
        mov     [rc.left], eax
        invoke  GetSystemMetrics, SM_CYSCREEN
        sub     eax, Scr_Height
        shr     eax, 1
        mov     [rc.top], eax

        invoke  CreateWindowEx,WS_EX_APPWINDOW,_class,_title,WS_VISIBLE+WS_DLGFRAME+WS_SYSMENU,\
                              [rc.left],[rc.top],[rc.right],[rc.bottom],NULL,NULL,[wc.hInstance],NULL
        test    eax,eax
        jz      error
        mov     [hwnd],eax

  msg_loop:
        invoke  InvalidateRect,[hwnd],NULL,FALSE
        invoke  GetMessage,msg,NULL,0,0
        or      eax,eax
        jz      end_loop
        invoke  TranslateMessage,msg
        invoke  DispatchMessage,msg
        jmp     msg_loop

  error:
        invoke  MessageBox,HWND_DESKTOP,_error1,_title,MB_OK or MB_ICONERROR

  end_loop:
        invoke  ExitProcess,[msg.wParam]

proc WindowProc hwnd,wmsg,wparam,lparam
        cmp     [wmsg],WM_CREATE
        je      .wmcreate
        cmp     [wmsg],WM_PAINT
        je      .wmpaint
        cmp     [wmsg],WM_KEYDOWN
        je      .wmkeydown
        cmp     [wmsg],WM_CLOSE
        je      .wmclose

  .defwndproc:
        invoke  DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
        jmp     .finish

  .wmcreate:
        invoke  GetDC,[hwnd]
        mov     [hdc],eax

        ;-------InitIntro
        stdcall initIntro

        xor     eax,eax
        jmp     .finish

  .wmpaint:
        invoke BeginPaint,[hwnd], ps
        mov    [ps.hdc], eax

        ;-------DrawIntro
        stdcall drawIntro

        invoke EndPaint,[hwnd], ps

        xor     eax,eax
        jmp     .finish

  .wmkeydown:
        cmp     [wparam],VK_ESCAPE
        jne     .defwndproc
        invoke  PostMessage,[hwnd],WM_CLOSE,0,0
        xor     eax,eax
        jmp     .finish

  .wmclose:
       invoke PostMessage,[hwnd],WM_QUIT,0,0
       xor     eax,eax

  .finish:
        ret
endp

;-------------Imports
section '.idata' import data readable writeable
  library kernel32,'KERNEL32.DLL',\
          user32,'USER32.DLL',\
          gdi32, 'GDI32.DLL',\
          winmm, 'WINMM.DLL'
  include '%fasminc%\api\kernel32.inc'
  include '%fasminc%\api\user32.inc'
  include '%fasminc%\api\gdi32.inc'
  include '%fasminc%\api\mmsystem.inc'

;-------------Resource
data resource
     directory RT_ICON,icons,RT_GROUP_ICON,group_icons
     resource icons,ChipIcon,LANG_NEUTRAL,icon_data
     resource group_icons,1,LANG_NEUTRAL,icon
     icon icon,icon_data,'chip.ico'
end data

;---------------------------------------------------------------------------------------------------
;---------------------------------------[Functions]-------------------------------------------------
;---------------------------------------------------------------------------------------------------
mediaLoadError:
     invoke  MessageBox,HWND_DESKTOP,_error2,_title,MB_OK or MB_ICONERROR
     invoke  ExitProcess,0
ret

proc initIntro
     push ebx edi

    ;----load 256b,1kb,64kb and demo images
    xor ebx, ebx
    .loop1:

        mov cl, 15
        mov eax, ebx
        mul cl
        mov edi, _demoImages
        add edi, eax
        invoke LoadImage,[wc.hInstance], edi, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE
        test eax, eax
        jz   mediaLoadError
        mov  [hBitmap+(ebx*4)], eax

    inc ebx
    cmp ebx, 4
    jne .loop1

    ;-----load wizard images
    xor ebx, ebx
    .loop2:

        mov cl, 18
        mov eax, ebx
        mul cl
        mov edi, _wizardImages
        add edi, eax
        invoke LoadImage,[wc.hInstance], edi, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE
        test eax, eax
        jz   mediaLoadError
        mov  [hBitmap+(4*4)+(ebx*4)], eax

    inc ebx
    cmp ebx, 5
    jne .loop2

    ;-----load balloon images
    xor ebx, ebx
    .loop3:

        mov cl, 19
        mov eax, ebx
        mul cl
        mov edi, _balloonImages
        add edi, eax
        invoke LoadImage,[wc.hInstance], edi, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE
        test eax, eax
        jz   mediaLoadError
        mov  [hBitmap+(9*4)+(ebx*4)], eax

    inc ebx
    cmp ebx, 6
    jne .loop3

     ;----Load Font
     mov     eax,96                        ;Height Of Font
     neg     eax
     invoke  CreateFont, eax,\             ;Height Of Font
                           0,\             ;Width Of Font
                           0,\             ;Angle Of Escapement
                           0,\             ;Orientation Angle
                     FW_BOLD,\             ;Font Weight
                           0,\             ;Italic
                           0,\             ;Underline
                           0,\             ;Strikeout
                ANSI_CHARSET,\             ;Character Set Identifier
               OUT_TT_PRECIS,\             ;Output Precision
         CLIP_DEFAULT_PRECIS,\             ;Clipping Precision
         ANTIALIASED_QUALITY,\             ;Output Quality
     FF_DONTCARE+DEFAULT_PITCH,\           ;Family And Pitch
                     FontName              ;Font Name
     invoke  SelectObject,[hdc],eax

     ;----Start playback
     stdcall uFMOD_PlaySong, xm, xm_length, XM_MEMORY

     pop edi ebx
ret
endp

proc drawIntro
     push   ebx

     mov    ebx, 1000
     invoke GetTickCount
     mov    ecx, eax
     sub    eax, [startTime]
     cmp    eax, ebx
     jge    .updateTime
     jmp    .skipUpdateTime
     .updateTime:
     mov    [startTime], ecx
     inc    [elapsedTime]

     ;----------------------Display timer
     stdcall itoa, [elapsedTime], timerBuffer, 10
     invoke TextOut,[hdc], 40, 480, timerBuffer, 2

     .skipUpdateTime:

     invoke SetBkMode,[ps.hdc],TRANSPARENT
     invoke SetTextColor,[ps.hdc],0

     ;----------------------Frame Display
     .256bimage:
     cmp [elapsedTime], 5
     jge .1kbimage
     stdcall drawImage, [hBitmap+(0*4)], 0,0,640,480
     jmp .wizard

     .1kbimage:
     cmp [elapsedTime], 10
     jge .64kbimage
     stdcall drawImage, [hBitmap+(1*4)], 0,0,640,480
     jmp .wizard

     .64kbimage:
     cmp [elapsedTime], 15
     jge .demoimage
     stdcall drawImage, [hBitmap+(2*4)], 0,0,640,480
     jmp .wizard

     .demoimage:
     stdcall drawImage, [hBitmap+(3*4)], 0,0,640,480

     ;----------------------Wizard animation
     .wizard:
     cmp [elapsedTime], 3
     jge .wizard1
     stdcall drawImage, [hBitmap+(4*4)], 150,480,140,120
     jmp .balloon0

     .wizard1:
     cmp [elapsedTime], 9
     jge .wizard2
     stdcall drawImage, [hBitmap+(5*4)], 150,480,140,120
     jmp .balloon0

     .wizard2:
     cmp [elapsedTime], 16
     jge .wizard3
     stdcall drawImage, [hBitmap+(6*4)], 150,480,140,120
     jmp .balloon0

     .wizard3:
     cmp [elapsedTime], 18
     jge .wizard4
     stdcall drawImage, [hBitmap+(7*4)], 150,480,140,120
     jmp .balloon0

     .wizard4:
     stdcall drawImage, [hBitmap+(8*4)], 150,480,140,120

     ;----------------------Balloon animation
     .balloon0:
     cmp [elapsedTime], 2
     jge .balloon1
     stdcall drawImage, [hBitmap+(9*4)], 290,480,350,120
     jmp .checkCounter

     .balloon1:
     cmp [elapsedTime], 6
     jge .balloon2
     stdcall drawImage, [hBitmap+(10*4)], 290,480,350,120
     jmp .checkCounter

     .balloon2:
     cmp [elapsedTime], 10
     jge .balloon3
     stdcall drawImage, [hBitmap+(11*4)], 290,480,350,120
     jmp .checkCounter

     .balloon3:
     cmp [elapsedTime], 15
     jge .balloon4
     stdcall drawImage, [hBitmap+(12*4)], 290,480,350,120
     jmp .checkCounter

     .balloon4:
     cmp [elapsedTime], 18
     jge .balloon5
     stdcall drawImage, [hBitmap+(13*4)], 290,480,350,120
     jmp .checkCounter

     .balloon5:
     stdcall drawImage, [hBitmap+(14*4)], 290,480,350,120

     ;----------------------Check time counter
     .checkCounter:
     cmp [elapsedTime], 20
     jne .skipEnd
     invoke PostMessage,[hwnd],WM_QUIT,0,0
     .skipEnd:

     pop   ebx
ret
endp

proc drawImage, handleBMP, x1, y1, x2, y2
     push edi

     invoke CreateCompatibleDC,[ps.hdc]
     mov edi, eax
     invoke SelectObject,edi,[handleBMP]
     invoke BitBlt,[ps.hdc],[x1],[y1],[x2],[y2],edi,0,0,SRCCOPY
     invoke DeleteDC,edi

     pop edi
ret
endp

proc itoa, integer, txtbuffer, base
        push  ecx edx edi ebx

        ; eax = number, ebx = base, edi = buffer
        mov     eax, [integer]
        mov     ebx, [base]
        mov     edi, [txtbuffer]

        xor     ecx,ecx
      .new:
        xor     edx,edx
        div     ebx
        push    edx
        inc     ecx
        test    eax,eax
        jnz     .new
      .loop:
        pop     eax
        add     al,30h
        cmp     al,'9'
        jng     .ok
        add     al,7
      .ok:
        stosb
        loop    .loop
        mov     al,32  ;add space, should be 0 (null terminated string)
        stosb

        pop  ebx edi edx ecx
ret
endp

;--------------------------------------------------------------------------
;                           uFmod library
;--------------------------------------------------------------------------
        ; Include the whole uFMOD  sources here. (Right after
        ; your main code to avoid naming conflicts, but still
        ; inside your code section.)
        macro PUBLIC symbol {} ; hide all publics
        include '%fasminc%\ufmodlib\fasm.asm'



Title: Re: The Coding Wizard - source code
Post by: benny! on March 22, 2007
Thanks for sharing the code, rbraz. And nice having a pure asm entry
in the competition !

inc karma
Title: Re: The Coding Wizard - source code
Post by: rdc on March 22, 2007
Wow, I think I understand most of this. I'll have to check out FASM I think.
Title: Re: The Coding Wizard - source code
Post by: .:] Druid [:. on March 22, 2007
thanks for your support!  karma included of course!
Title: Re: The Coding Wizard - source code
Post by: Shockwave on March 22, 2007
Thanks for sharing the source Rbraz :) I didn't realise that your entry was in asm :D
Title: Re: The Coding Wizard - source code
Post by: Voltage on March 22, 2007
Yeah, I didn't realize that it was a Win 32 ASM entry!  Kick ass.  Windows programming is difficult enough with visual basic :) 

Great work mate.
Title: Re: The Coding Wizard - source code
Post by: Clyde on March 23, 2007
Nice one Rbraz dude :)
Title: Re: The Coding Wizard - source code
Post by: rain_storm on March 23, 2007
This is really impressive to see especially since we had less than one month to put our entries together and still the quality did not suffer as a result thanks for posting it rbraz
Title: Re: The Coding Wizard - source code
Post by: Rbz on March 23, 2007
Thanks dudes, much appreciated !

Btw, coding in asm it's not that hard, I've spended much more time thinking on what to do :)

 
Title: Re: The Coding Wizard - source code
Post by: mike_g on March 24, 2007
Looks hard to me :o Its quite impressive what you can do with just ASM. Well done done man :)