News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

SWGPTG - 01

Started by avcaballero, April 06, 2018, 05:50:35 AM

Previous topic - Next topic

avcaballero

SWGPTG - Small Windows Graphics Programming Tutorial with GDI

For the occasion of the opening of this new thread I have decided to post a small tutorial on the programming of graphics with GDI, which is in fact a subset of my Windows Graphics Tutorial with GDI. I'll drop a new post here every weekend (or so) until you/I get tired (that will be soon), as well as here (when I have time) and there. It has two quizzes to exercise us, which we will review in the next weekend (or so).

The size of the client area will be 640x400, which meets the golden ratio of proportionality: 1.6.

You can make use of what you find interesting here with the logical constraints: plagiarism is not allowed and reference is required if you use it.

Surely, many of you who are reading this have already programmed graphics in MS-DOS, probably in 13H mode. The programming of graphics for Windows with GDI is not very different.

PD. Excuse me if any mistake. I did it all in a hurry.


1.- We need a canvas where to paint on

In MS-DOS we wrote at the segment:address A000:0000, now we can not do that, but we will write on the hDC (display device context) that gives us the BeginPaint function that we will place inside the WM_PAINT section / message. However this is very very slow, because it has many different options to deal with. If we paint point to point the wait can be eternalized. To avoid this great inconvenience we will use a data buffer that we will send at once, which is almost immediate.

The data buffer we will create will be a bitmap that we will send to the hDC with a BitBlt or StretchBlt function. We will use the last one. For the creation of the bitmap we will use the function CreateDIBSection, which returns a pointer to the bit values of the created bitmap. This pointer is very important because it is the one that will allow us to write in it very quickly.

Writing to our DIB buffer can be done manually or through GDI functions, such as SetPixel. Contrary to what many people say, SetPixel is not particularly slow in itself, as I demonstrated some time ago here. The writing on the hDC is what is slow. Although, of course, we will always use what gives us the fastest writing speed.

In summary, we could identify the address A000:0000 of MS-DOS with the hDC of WM_PAINT, to which to access effectively we will need an intermediate data buffer with DIB format.


2.- Template

To finish this first post I will attach the templates of several compilers. If they are compiled and executed they will simply show a window populated by a default color. It is not advisable to eat your head on how this file is structured, you just have to take into account the following procedures within the code:

  * Inicio. Here you will include the code that you want to run only once at the application starting. It is called within WM_CREATE. For example to initialize certain variables, etc.
  * PintaObjeto. Here you will include the code that you want to run every time the pre-established time is met. I've put it every 20 milliseconds. It is the code that will renew what appears in our window. It runs inside WM_PAINT.
 
It would be interesting to stop at the following points:
  * To paint in our DIB bitmap we will use the variable pMainDIB, which is the pointer that directs it. To this address we will pass the desired color of the point that has the format AARRGGBB, each one are byte-size, where AA (alpha) = 0 the rest are from 0 to 0xFF. This means that the size of the point to paint is dword, so to move to the next point we will have to increase our pointer in 4 bytes. The pointer starts at the upper left corner and increases to the right, when it ends with the length of the line we will see appear at the first point on the left of the line below.
  * The background color of our main window, set in the WNDCLASSEX structure, must be 0, which indicates that it has no color, otherwise you will get flickering on the screen.
  * The capture of the WinMain procedure messages is done with GetMessage instead of PeekMessage because the latter generates an unnecessary increase in work in most cases, as shown here.
  * When we set the dimension of the window, it is not the client area, which will be lower, so if we want it for this we will have to do some extra accounts, although given that the window is resizable, it may not be worth it .
 
 
3.- Quiz 1
Make a small program that draws a gradient of colors (by your own).



4.- Quiz 2
Free theme to upload note.

EDITED: added the templates 32 and 64 bits for GoAsm and NasmX and a little 64 bits demo for NasmX and the same with a tune in the background made with Fasm 32 bits.

zedd151

#1
Gracias, caballero! This is the kind of topic that should jumpstart this forum.

Should put the word "Tutorial" in the title, imo.

zedd

LordAdef

Nice Caballero!

I foresee a great future for our game corner. Asm used to be the king of games in the good old times. It´s still used a lot and soon I guess people will come here for the "iron taste"

ps: I´ll a good time studying this one!

avcaballero

#3
Finally I found the right way to modify my messages :icon_redface: So this post may be removed, the new zip is already in the first post... Sorry

avcaballero

Hello. I have noticed an odd behaviour with 64 bits programming, common for all the compilers, that I will chase when I have time.

The correct way should be:

invoke    StretchBlt, [hdc], 0, 0, [vdxClient], [vdyClient], [bufDIBDC], 0, 0, cdXSize, cdYSize, SRCCOPY


And works ok for the "floor" demo example for any compiler. Nevertheless, in my computer, only for the template appear vertical dark lines between the flat color. In another computer is ok.

In my computer I have to put the next code if I want to work ok:

invoke    StretchBlt, [hdc], 0, 0, addr vdxClient, addr vdyClient, [bufDIBDC], 0, 0, cdXSize, cdYSize, SRCCOPY


Does anyone noticed anything strange? Has anyone checked anything?

avcaballero


avcaballero

tuto canceled by lack of quorum :bgrin:

LordAdef

No!!


I'm here waiting for it, bring it on!

avcaballero

I have added poasm to the template list and some arrangements

HSE

Fantastic :t

(attached plantilla with some Masm32 macros)
Equations in Assembly: SmplMath

avcaballero

Thank you for your contribution, Héctor :t