News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

dinofun (more gdi testing)

Started by felipe, April 05, 2018, 01:47:15 PM

Previous topic - Next topic

felipe

Well i took some ideas from an old book about game development with c. But this is not what you should do to create games (that's why this isn't in the game development subforum). It's just another "fun" with gdi. With which i have little experience.
To try this "test program" unzip the 3 files in the same folder and then run the .exe from that folder. The source code is here:

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
; Created by Felipe at 2018-04-05
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

.686
.model      flat,stdcall
option      casemap:none
include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc
include     \masm32\include\gdi32.inc
include     \masm32\include\masm32.inc
include     \masm32\include\winmm.inc
includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\gdi32.lib
includelib  \masm32\lib\masm32.lib
includelib  \masm32\lib\winmm.lib


.data
align 1
class_name  byte        "main window class",0
wnd_name    byte        "PRESS THE LEFT BUTTON OF YOUR MOUSE!",0
laugh       byte        ".\laugh.wav",0
dino        byte        ".\dino.bmp",0


.data?
align 4
hinstance   dword       ?
hwindow     dword       ?
hdevicec    dword       ?
hdc         dword       ?
hbitmap     dword       ?
hdcmem      dword       ?


.code
align 4
start:
            push        ebp
            mov         ebp,esp
           
            push        NULL                                                ; Get a handle for the program instance.
            call        GetModuleHandle
           
            mov         hinstance,eax

            call        GetTickCount                                        ; Obtaining 
           
            push        eax                                                 ;   the seed.
            call        nseed

            sub         esp,76                                              ; WNDCLASSEX and MSG.
                                                     
            mov         dword ptr[ebp-48],48                                ; The size of the structure.
            mov         dword ptr[ebp-44],CS_HREDRAW or CS_VREDRAW          ; The style of the class.
            mov         [ebp-40],wndproc
            mov         dword ptr[ebp-36],0                                 ; No extra
            mov         dword ptr[ebp-32],0                                 ;   bytes.
            mov         eax,hinstance
            mov         [ebp-28],eax

            push        500                                                 ; The dino icon.
            push        hinstance                                           
            call        LoadIcon

            mov         [ebp-24],eax                                        ; hIcon.

            push        600                                                 ; The dino cursor.
            push        hinstance                                             
            call        LoadCursor

            mov         [ebp-20],eax                                        ; hCursor.

            push        WHITE_BRUSH                                         ; Typical background color for a window.
            call        GetStockObject                                      ; Get the handle of the brush.

            mov         [ebp-16],eax                                        ; hBrush.
            mov         dword ptr[ebp-12],NULL                              ; No menu.
            mov         [ebp-8],offset class_name
            mov         dword ptr[ebp-4],NULL                               ; Use the same app icon for the small one.

            lea         eax,[ebp-48]                                        ; WNDCLASSEX.

            push        eax
            call        RegisterClassEx

            push        NULL                                                ; No value passed to the window.
            push        hinstance
            push        NULL                                                ; No menu.
            push        NULL                                                ; No parent of the window.
            push        CW_USEDEFAULT                                       ; Default height.
            push        CW_USEDEFAULT                                       ; Default width.
            push        CW_USEDEFAULT                                       ; Default position
            push        CW_USEDEFAULT                                       ;  of the window.
            push        WS_OVERLAPPEDWINDOW                                 ; Typical main window style.
            push        offset wnd_name                                     ; Name of the main window.   
            push        offset class_name
            push        WS_EX_OVERLAPPEDWINDOW                              ; Some extension of the style.
            call        CreateWindowEx

            mov         hwindow,eax

            push        SW_SHOWNORMAL                                       ; Displaying the window for the first time.
            push        eax
            call        ShowWindow                                         

            push        hwindow
            call        UpdateWindow                                        ; Update the client area.

            lea         esi,[ebp-76]                                        ; MSG.

     

align 4
msgloop:
            push        0                                                   ; Retrieve all
            push        0                                                   ;  available messages.
            push        NULL                                                ; Messages of any window of this thread.
            push        esi                 
            call        GetMessage                                     

            cmp         eax,0                                               ; WM_QUIT?
            je          end_program
           
            push        esi
            call        TranslateMessage                                    ; Translate keyboard to messages.

            push        esi
            call        DispatchMessage                                     ; Dispatch messages to wndproc.

            jmp         msgloop                                             ; Loop until get the WM_QUIT message.

align 4
end_program:
      mov         eax,[ebp-68]                                        ; wParam (exit code).

            mov         esp,ebp
            pop         ebp
 
            push        eax                                 
            call        ExitProcess                                         ; End the program.


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

align 4
wndproc:                                                                    ; The window procedure for the main window class.
            push        ebp
            mov         ebp,esp

            cmp         dword ptr[ebp+12],WM_PAINT
            jne         check_mouse
           
            push        hwindow
            call        GetDC
            mov         hdc,eax                 

            sub         esp,48                                              ; RECT, x, y coordinates and BITMAP.

            push        esi                                                 ; Intel ABI.
            lea         esi,[ebp-24]                                        ; RECT.
            mov         dword ptr[ebp-8],0                                  ; y
            mov         dword ptr[ebp-4],0                                  ;  and x are zeroed.

            push        esi
            push        hwindow
            call        GetClientRect

            cmp         dword ptr[ebp-16],0                                 ; Right from RECT.
            jle         next_test                                           ; Just valid numbers.

            mov         eax,[ebp-16]
            sub         eax,[ebp-24]                                        ; Left from RECT.

            push        eax
            call        nrandom                                             ; A random number in between.

            mov         [ebp-4],eax                                         ; Becomes x coordinate.   

            mov         eax,[ebp-12]                                        ; Top from RECT.
            sub         eax,[ebp-20]                                        ; Bottom from RECT.

            push        eax
            call        nrandom                                             ; A random number in between.

            mov         [ebp-8],eax                                         ; Becomes y coordinate.

            push        LR_LOADFROMFILE
            push        0
            push        0
            push        IMAGE_BITMAP
            push        offset dino                                         ; The dino bitmap.
            push        0
            call        LoadImage

            mov         hbitmap,eax
   
            lea         esi,[ebp-48]                                        ; BITMAP.

            push        esi
            push        24                                                  ; Size of BITMAP.
            push        hbitmap
            call        GetObject

            push        hdc
            call        CreateCompatibleDC
            mov         hdcmem,eax   
                 
            push        hbitmap
            push        eax
            call        SelectObject

            push        SRCCOPY
            push        0
            push        0
            push        hdcmem
            push        dword ptr[ebp-40]                                   ; Height from RECT.
            push        dword ptr[ebp-44]                                   ; Width from RECT.
            push        dword ptr[ebp-8]                                    ; y and
            push        dword ptr[ebp-4]                                    ;  x coordinates.
            push        hdc
            call        BitBlt

            push        hdcmem                                              ; Cleaning
            call        DeleteDC
           
            push        hbitmap
            call        DeleteObject                                        ;   up.
           

align
next_test:
            pop         esi                                                 ; Intel ABI.

            mov         esp,ebp
            pop         ebp

            ret


align 4
check_mouse:
            cmp         dword ptr[ebp+12],WM_LBUTTONDOWN                    ; Left mouse button pressed in client area.
            jne         check_destroy

            push        SND_FILENAME
            push        NULL
            push        offset laugh
            call        PlaySound

            xor         eax,eax

            mov         esp,ebp
            pop         ebp

            ret         16


align 4
check_destroy:
            cmp         dword ptr[ebp+12],WM_DESTROY
            jne         do_the_default

            push        0                                                   ; Exit code (wParam in WM_QUIT).
            call        PostQuitMessage                                     ; Notify to windows that the end of the application.   
           
            xor         eax,eax
           
            mov         esp,ebp
            pop         ebp
           
            ret         16


align 4
do_the_default:
            push        dword ptr[ebp+20]                                   ; lParam.
            push        dword ptr[ebp+16]                                   ; wParam.
            push        dword ptr[ebp+12]                                   ; The message.
            push        dword ptr[ebp+8]                                    ; Handle of the window.
            call        DefWindowProc                                       ; Default processing for all the other messages.

            mov         esp,ebp
            pop         ebp

            ret         16

            end         start


This program does run correctly in my pc. I can resize the window, maximize, etc and there are not problems.

LordAdef

After this long journey, finished my work and opened your proggy.

Man, you made me laugh out loud! It´s funny.

Working ok on win 7, 64

zedd151

Quote from: felipe on April 05, 2018, 01:47:15 PM
Well i took some ideas from an old book about game development with c. But this is not what you should do to create games (that's why this isn't in the game development subforum).

In my opinion, this is just what should be in the Games Forum...

It might be helpful to others in their research into games, graphics, gdi, bitblt, etc...

Just a thought....

LordAdef

Quote from: zedd151 on April 05, 2018, 06:09:31 PM
Quote from: felipe on April 05, 2018, 01:47:15 PM
Well i took some ideas from an old book about game development with c. But this is not what you should do to create games (that's why this isn't in the game development subforum).

In my opinion, this is just what should be in the Games Forum...

It might be helpful to others in their research into games, graphics, gdi, bitblt, etc...

Just a thought....


I agree with Zedd 100%

zedd151

Quote
I agree with Zedd 100%

Seems to me that we could probably attract some new younger folks to assembly programming if our Games Dev forum had some useful tutorials on proper graphics handling, and other functions necessary for game development.

I myself would like to see a good tut on making bitmap sprites "move".

Also of interest would be collision detection, etc...

I know there are topics here as well as the old forum, but it would be great if we had tutorials in game dev.
Would probably help spark interest in ASM in general, and Game programming in particular.


zedd.  :icon_cool:

LordAdef

I have to agree with again!

I'm currently putting together a list of references to post there. We could use a single thread only for this

zedd151

This time, I agree. lol

Sorry for the thread hijack, btw.
Make sure the threads you link are actually useful and/or helpful. The less "noise" the better, imo.

Should ask hutch to pin it and lock it , for use as reference possibly?
This way the important stuff won't get lost in an avalanche of possible replies. But you would also need the ability to modify to add new material or remove unused .

felipe

Soon as i can i will upload something better to the game development subforum  :icon14:.

Quote from: felipe on April 05, 2018, 01:47:15 PM
This program does run correctly in my pc. I can resize the window, maximize, etc and there are not problems.

Well, actually after running it for too much long, it starts to hang the system  :redface:.
If i will be testing more of gdi later i will avoid this kinds of "big loops"  :idea:.

felipe

Quote from: felipe on April 05, 2018, 01:47:15 PM
But this is not what you should do to create games (that's why this isn't in the game development subforum).

What i mean here is that the game loop is not proper for a game... :idea:

Quote from: zedd151 on April 05, 2018, 06:49:14 PM
I myself would like to see a good tut on making bitmap sprites "move".

Did you looked the santa program (asm hohoho i think) source code?  :idea:

Quote from: zedd151 on April 05, 2018, 06:49:14 PM
Also of interest would be collision detection, etc...

Did you looked the red pistol source code?. Well, that's not the best implementation. I think IntersectRect has what is needed. It can detect when two rectangles (bitmaps as example) intersect and it tells you this in a very simple form: returns 0 or not 0.  :idea:

felipe

Quote from: LordAdef on April 05, 2018, 07:45:35 PM
I'm currently putting together a list of references to post there. We could use a single thread only for this

What happened with that idea?  :idea:

LordAdef

Quote from: felipe on May 23, 2018, 12:53:50 PM
Quote from: LordAdef on April 05, 2018, 07:45:35 PM
I'm currently putting together a list of references to post there. We could use a single thread only for this

What happened with that idea?  :idea:
I put three links there at the time.

felipe

I see, you mean this: http://masm32.com/board/index.php?topic=7032.0. I thought you were talking about links inside of this forum before.

felipe

Quote from: felipe on May 23, 2018, 12:48:17 PM
Did you looked the santa program (asm hohoho i think) source code?  :idea:

Zedd this is not well commented, but the main idea is to use some extended functionallity from Directx 9 to load images (bitmaps, png and others.) Or part of images (you can control the size). As this images are rectangles you make the background of the images transparent. Finally you associate to each image a RECT structure to move the image in the screen... :idea:

The difficult part (among others) is make the characters to jump... :icon_exclaim:
As you can see santa only can move his ass left and right by now... :biggrin:

daydreamer

#13
Quote from: felipe on May 23, 2018, 02:41:49 PM
Quote from: felipe on May 23, 2018, 12:48:17 PM
Did you looked the santa program (asm hohoho i think) source code?  :idea:

Zedd this is not well commented, but the main idea is to use some extended functionallity from Directx 9 to load images (bitmaps, png and others.) Or part of images (you can control the size). As this images are rectangles you make the background of the images transparent. Finally you associate to each image a RECT structure to move the image in the screen... :idea:

The difficult part (among others) is make the characters to jump... :icon_exclaim:
As you can see santa only can move his ass left and right by now... :biggrin:
You make a structure with x,y,xdelta,ydelta and make some simple physics ,a keypress for whatever key you want as jumpkey starts upgoing ydelta movement,and start a small loop that decreases ydelta speed until you hit either ground or bottom of pitfall
Combine it with xdelta has speeds for walk,run fast ,stop in both left/right direction
offcourse it was easier to have Graphics made of redefined character set,so you just checked ascii codes for enemies,ladders,floor,space around you
interesting in how Lord Adef does collision checks in his game against terrain?
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

felipe

Quote from: daydreamer on May 24, 2018, 01:12:30 AM
how Lord Adef does collision checks in his game against terrain?

He is using ascii codes for that?  :shock: