C++ 3D Game Tutorial 1: Creating a Window with Win32 API

preview_player
Показать описание


DESCRIPTION:
Hello everyone! In this first tutorial we'll see how to create the window in C/C++ with Win32 API, in which we will draw all the 3d objects that will compose our game world!
This is the first of a series of video tutorials about the creation of a 3D Game in C++ from scratch!

For any doubts, questions, suggestions and feedbacks about the tutorial, please add them in the comment section below!
I'll answer you as soon as I can!

All the code written in this tutorial is available at the following address:

I hope you'll enjoy this tutorial!
----------------------------------------------------------------------------

Chapters:
0:45 Requirements
1:25 Design
3:47 Implementation
14:00 WARNING: Once you've finished this tutorial, apply the improvements explained in the pinned comment
16:00 Implementation
21:52 Result
22:15 End
Рекомендации по теме
Комментарии
Автор

WARNING - Enable the subtitles in order to show the written explanation.
WARNING 2 - CODE IMPROVEMENT:
In Window.cpp file, the pointer Window* window has been declared as global pointer in order to be accessible in the global Window Procedure WndProc.
But, there is a better way to handle this use-case.

It is possible to pass the window instance pointer to the Window Procedure through the CreateWindowEx function.
It is also possible to store the window instance pointer inside a user data structure related to a specific window identified by the handle HWND.

The User Data related to a specific Window identified by the HWND can be:
- modified through the Win32 function: SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)window);
- retrieved through the Win32 function: Window* window = (Window*) GetWindowLongPtr(hwnd, GWLP_USERDATA);

Here is the necessary steps to apply the improvements:

In Window.cpp file:

| comment (or delete) the Window* window global pointer.

| in the init() method:
- replace the last parameter of CreateWindowEx with "this" (that is the pointer to the Window class instance):

m_hwnd=::CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "MyWindowClass", "DirectX Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 1024, 768,
NULL, NULL, NULL, this);

- delete the following code snippet:

if (!window)
window = this;

| in the Window Procedure WndProc:
- replace the WM_CREATE case with the following code snippet:

case WM_CREATE:
{
// Event fired when the window is created
// collected here..
Window* window =
// .. and then stored for later lookup
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)window);
window->onCreate();
break;
}

- replace the WM_DESTROY case with the following code snippet:

case WM_DESTROY:
{
// Event fired when the window is destroyed
Window* window =(Window*) GetWindowLongPtr(hwnd, GWLP_USERDATA);
window->onDestroy();
::PostQuitMessage(0);
break;
}

| in the broadcast() method:
- replace window->onUpdate(); with this->onUpdate();


All the code improvements are already available on GitHub!

PardCode
Автор

What an awesome human being this guy is, sharing his immense knowledge with the rest of us dedicating his precious time.

WastingTime
Автор

The easiest tutorial that i found on youtube teaching how to create a window in the most minimalist way!!! thanks dude !!!! keep it amazing!!!!

luismendes
Автор

Nice Work. I am watching this 4 years in the future so I'm not sure if these things have changed, but two things I had to do to get this program running were:
1) around 8:32, in the Window.cpp file:
add "L" to wc.lpszClassName = L"MyWindowClass"
and then later add "L" to m_hwnd = ::CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, L"MyWindowClass", L"DirectX Application", ...

2) in Window.cpp file, change the default return value of the switch statement in the WndProc function to from DefWindowProcA to DefWindowProcW:
return ::DefWindowProcW(hwnd, msg, wparam, lparam);

I don't know how these will impact the engine in future development though

nivedmorts
Автор

Always good to see engine programming from scratch. Thanks for the content!

danielecarbone
Автор

This is the most exciting white box that i have ever seen. It is so rewarding to see a little box appear on the screen. Great tutorial

phicoding
Автор

this is the indian guy that can teach better than the teacher

bytelaw
Автор

Man!!!! Where were you when I was looking for a tutorial like this for goddamn months!!!! Just love this tut!!!

mathgenius
Автор

if you are using a newer version of windows, you will need to put L infront of some of your strings now to get them to work, L"MyWindowCLas"; instead. if you're confused and still want to follow along.

joshuablackmon
Автор

I am so glad i found this tutorial because almost all other ones i saw were so badly explained but you explain things very good, keep up the good work!

drzeldaglitch
Автор

Eclipse C++ user here!


Took me about an hour and a half or so to get through this video and translate what he's doing in Visual Studio over to Eclipse. After I understood everything, it wasn't that hard! Here's some pointers on how to translate the tutorial in this video over to Eclipse C++ IDE:


1. At the beginning when he says to delete the default stuff and start from scratch, make sure not to delete the folders called Binaries, Includes, and Debug.
2. You can create the App folder and do everything in it without much trouble, but you MUST create the Engine folder as a Source Folder, otherwise the system won't recognize when you're calling for Window.h!
3. Since Eclipse can't do the automatic function creations to transfer from the header files to the cpp files, make sure that you type out what you see in the video manually when he uses the automatic transfer.
4. You might get some warnings, but that's OK! Your build will still work as long as you have followed the tutorial correctly.


Hope this helps you Eclipse users out there!

akdreamer
Автор

there aren't enough good tutorials on youtube, keep it up.

timdoozy
Автор

I've hoped for a tutorial like this one for about 5 years now

toraneko
Автор

Coming from an OpenGL background trying to diversify my knowledge. Awesome series and very helpful and code explanatory. 👍👍

quickscopesheep
Автор

absolutely amazing. i'm using visual studio 2019 and a few things might be different, but WORKS LIKE A CHARM :)

Roundbeargames
Автор

you explain very well, you need more likes and views dude

Spectral-fbrg
Автор

Underrated channel that deserves many more subs.

oleksijm
Автор

Man, i am very new at programming and this video was like chinese for me, but im glad it so complete, you have used a lot of important tools of programming that i can now look individually to understand and learn, Thank you so much. If i get somewhere ill update.
(Sorry bad english)

ulisesferreira
Автор

Man you are just amazing !!!!
3 cheers for you
hip hip pardcode!
hip hip pardcode!!
hip hip pardcode!!!

shirshprasad
Автор

5:59 That scared the hell out of me. I thought your voice would have been fully replaced by the ambient music.

white_cloth