The MASM Forum

General => The Campus => Topic started by: kroz on July 09, 2012, 12:40:55 PM

Title: Why Does Starfield Keep Crashing?
Post by: kroz on July 09, 2012, 12:40:55 PM
Hi guys.
recently, i made a starfield in DIB (device independent bitmap)
it worked wonderfully (kinda) until i noticed that about 10-20s
into the program, it crashes with an access error exception :P
i couldn't find out why it crashes though.
I tried changing the screenwidth/height but that didn't work :(
the starfield works like this ...

STAR STRUCT
   color dd ?
   xpos  dd ?
   ypos  dd ?
STAR ENDS

StarData STAR NUM_STARS (<?>)

.const

NUM_STARS equ 50

could someone plz help me? :)

the source is included
Title: Re: Why Does Starfield Keep Crashing?
Post by: dedndave on July 09, 2012, 07:33:35 PM
start:
        invoke  GetModuleHandle,eax

i would use GetModuleHandle,NULL   :P

        invoke  GetDC, [hWnd]
        mov     hDC,eax
        invoke  CreateCompatibleDC, eax
        mov     [canvasDC], eax
        invoke  CreateDIBSection,hDC,ADDR canvas,DIB_RGB_COLORS, ADDR canvas_buffer, 0, 0
        mov     [canvasBmp], eax
        invoke  SelectObject, [canvasDC], eax
        invoke  ReleaseDC,hDC,0

when you use ReleaseDC, it should be
        invoke  ReleaseDC,[hWnd],hDC
also, you should use DeleteDC to delete canvasDC and DeleteObject to delete canvasBmp during WM_CLOSE
before you delete the canvasDC, you should select the original bmp back into it
i.e., after SelectObject,[canvasDC],eax -> EAX will return the original - save it as canvasOrigBmp
then, in WM_CLOSE...
        invoke  SelectObject,[canvasDC],[canvasOrigBmp]  ;returns canvasBmp in EAX
        invoke  DeleteObject,eax
        invoke  DeleteDC,[canvasDC]
        invoke  EndDialog,[hWnd],0

these are GDI objects and the system only has space for so many
by not "balancing the force", you can cause the system to behave strangely

        invoke  SetTimer, [hWnd],1,0,0   
        call    initstars

the SetTimer function has a minimum period of 10 mS
also - you may want to initstars before starting the timer
        call    initstars
        invoke  SetTimer, [hWnd],1,10,0   

another thing to add to WM_CLOSE is to use KillTimer   :t

the way you test for all handled messages for all messages is not very good
after each message has been handled, return with EAX = 0
Title: Re: Why Does Starfield Keep Crashing?
Post by: kroz on July 14, 2012, 06:36:39 AM
ah. ok :) thanks.
i fixed it.
Title: Re: Why Does Starfield Keep Crashing?
Post by: hfheatherfox07 on July 17, 2012, 08:25:44 AM
Quote from: kroz on July 14, 2012, 06:36:39 AM
ah. ok :) thanks.
i fixed it.

Hey I was wondering ..what else did you do to stop it from crashing because I did every thing dedndave suggested and mine still crashes
May be it is the way I defined "canvasOrigBmp"

Just like starfiled stuff .....
Title: Re: Why Does Starfield Keep Crashing?
Post by: kroz on July 21, 2012, 07:09:44 AM

initstars PROC
LOCAL color:DWORD
local xpos:DWORD
local ypos:DWORD
local counter:DWORD
local dwords:DWORD

mov counter, 0
mov dwords, 0

startinit:
invoke _random, 0FFFFFFh
mov color, eax
invoke _random, ScreenWidth-10
mov xpos, eax
invoke _random, ScreenHeight-1 ; STARS ARE 2 PIX HIGH!! - FIXED
mov ypos, eax
mov edx, dwords
mov eax, color
mov dword ptr [offset stardata+edx], eax
add edx, 4
mov eax, xpos
mov dword ptr [offset stardata+edx], eax
add edx, 4
mov eax, ypos
mov dword ptr [offset stardata+edx], eax
add edx, 4
mov dwords, edx
inc counter
cmp counter, NUM_STARS
je AllDone
jmp startinit
AllDone:
mov eax, 1
Ret
initstars EndP


and


MoveStars PROC
LOCAL color:DWORD
LOCAL xpos :DWORD
LOCAL ypos :DWORD
LOCAL dwords :DWORD
LOCAL counter:DWORD

mov counter, 0
mov dwords, 0
mov color, 0
mov xpos, 0
mov ypos, 0
startmove:
mov edx, dwords
mov eax, dword ptr [offset stardata+edx]
mov color, eax
add edx, 4
mov eax,dword ptr [offset stardata+edx]
mov xpos ,eax
add edx, 4
mov eax,dword ptr [offset stardata+edx]
mov ypos, eax
add edx, 4
add xpos, 2
cmp xpos, ScreenWidth-10
mov dwords, edx

je reseed
        ja reseed ; because xp + 2 may not hit the limit so .... ja to be safe. stops it crashing too.
mov edx, dwords
sub edx, 12
mov eax, color
mov dword ptr [offset stardata+edx], eax
add edx, 4
mov eax, xpos
mov dword ptr [offset stardata+edx], eax
add edx ,4
mov eax, ypos
mov dword ptr [offset stardata+edx], eax
add edx, 4
mov dwords, edx
inc counter
cmp counter, NUM_STARS
je alldone
jmp startmove
alldone:
BeginBlt:
mov dwords, 0
mov counter, 0
StartBlt:
mov edx, dwords
mov eax, dword ptr [offset stardata+edx]
mov color, eax
add edx, 4
mov eax, dword ptr [offset stardata+edx]
mov xpos, eax
add edx, 4
mov eax, dword ptr [offset stardata+edx]
mov ypos, eax
add edx, 4
mov dwords, edx
invoke BlitStar, xpos, ypos, color
inc counter
cmp counter, NUM_STARS
je EndAlls
jmp StartBlt
EndAlls:
mov eax, 1
Ret
reseed:
invoke _random, 0FFFFFFh
mov color, eax
invoke _random, ScreenHeight-2
mov ypos, eax
mov xpos, 0
sub dwords, 12
mov edx, dwords
mov eax, color
mov dword ptr [offset stardata+edx], eax
add edx, 4
mov eax, xpos
mov dword ptr [offset stardata+edx], eax
add edx ,4
mov eax, ypos
mov dword ptr [offset stardata+edx], eax
add edx, 4
sub edx, 12
mov dwords, edx
jmp startmove
MoveStars endp


copy/paste those two replacing the orig and it should works :)
Title: Re: Why Does Starfield Keep Crashing?
Post by: hfheatherfox07 on July 21, 2012, 07:42:53 AM
@ kroz
Thanks I will try that tonight when I get home .....
We worked on a few starfields on the old forum ....
So you can search for them in the old forum downloads( I have a bunch there...some of the examples don't work but the last one on every post was fixed and works 100%) ...We have added scrolling text to them too  :biggrin: