Lesson 3: Making game in C++
#1
Brick 
Hey, again....

Click HERE for previous lesson.

Today i will tell about text creating in game.

Information

Hm, lets create R_RenderScene() method between 'BeginScene' and 'EndScene'. It will be better.
There's our final code from second lesson.
I changed code a bit and added R_RenderScene method.
Code:
#include <windows.h> #include <windowsx.h> // required #include "stdafx.h" // class LPCWSTR WIN_MAIN = L"MAIN_WINDOW"; // screen data int SCREEN_WIDTH; int SCREEN_HEIGHT; int SCREEN_POS_X; int SCREEN_POS_Y; bool SCREEN_WINDOWED; LPCWSTR WIN_TITLE; // d3d data int D3D_COLOR_R; int D3D_COLOR_G; int D3D_COLOR_B; #include <d3d9.h> #pragma comment (lib, "d3d9.lib") LPDIRECT3D9 d3dinit; // d3d interface init LPDIRECT3DDEVICE9 d3ddevice; // d3d device init // function prototypes void R_Init(HWND hWnd); // Initialize d3d device void R_Frame(void); // rendering void CleanD3D(void); // clear display before showing void R_Prepare(void); void R_RenderScene(void); LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int ShowCmd) { R_Prepare(); // Window creation MSG msg; WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_VREDRAW|CS_HREDRAW|CS_OWNDC, WindowProc, 0, 0, hInstance, NULL, NULL, (HBRUSH)(COLOR_WINDOW+3), NULL, WIN_MAIN, NULL}; RegisterClassEx(&wc); // create new window HWND hMainWnd = CreateWindow(WIN_MAIN, WIN_TITLE, WS_OVERLAPPEDWINDOW, SCREEN_POS_X, SCREEN_POS_Y, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL); // show it ShowWindow(hMainWnd, ShowCmd); // INIT DIRECT3D R_Init(hMainWnd); // Initialize d3d stuff UpdateWindow(hMainWnd); // real time loop while(TRUE) { while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } // game loop can be there if(msg.message == WM_QUIT) break; // game loop can be there // recommended there R_Frame(); } CleanD3D(); return(0); } LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_DESTROY: PostQuitMessage(0); return(0); } return DefWindowProc (hWnd, message, wParam, lParam); } // ===== Prepare stuff ===== // void R_Prepare(void) { // mode SCREEN_WINDOWED = TRUE; // size of window SCREEN_WIDTH = 1024; SCREEN_HEIGHT = 768; // position of window SCREEN_POS_X = 0; SCREEN_POS_Y = 0; // title of the window WIN_TITLE = L"Windowed program ^_^"; // L prefix is required ( C++ basics ) // d3d clear color data // blueness D3D_COLOR_R = 0; D3D_COLOR_G = 40; D3D_COLOR_B = 120; } // ===== Direct3D stuff ===== // // init Direct3D void R_Init(HWND hWnd) { d3dinit = Direct3DCreate9(D3D_SDK_VERSION); // interface creationism // presentation parameters D3DPRESENT_PARAMETERS d3dparams; ZeroMemory(&d3dparams, sizeof(d3dparams)); // clear d3d parameters d3dparams.Windowed = SCREEN_WINDOWED; // window mode toggle d3dparams.SwapEffect = D3DSWAPEFFECT_DISCARD; // available: D3DSWAPEFFECT_FLIP, D3DSWAPEFFECT_COPY d3dparams.hDeviceWindow = hWnd; // get owner and set d3d to the window d3dinit->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, // window owner D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dparams, // presentation parameters &d3ddevice); // initialized device } // Render // this is the function used to render a single frame void R_Frame(void) { // clear current window d3ddevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(D3D_COLOR_R, D3D_COLOR_G, D3D_COLOR_B), 1.0f, 0); d3ddevice->BeginScene(); // starts rendering current scene // your rendering stuff will be there R_RenderScene(); d3ddevice->EndScene(); // stops rendering current scene d3ddevice->Present(NULL, NULL, NULL, NULL); // shows current scene } // Clean d3d void CleanD3D(void) { d3ddevice->Release(); // destroys d3d device d3dinit->Release(); // destroys d3d stuffs } // Render your stuff void R_RenderScene(void) { }

So, lets make font rendering
Firstly, you need to add new header and library files.
Code:
#include <d3dx9.h> // added #pragma comment (lib, "d3dx9.lib") // added

Required for fonts and another stuff.
Let's add two methods, Font_Init and R_DrawString.
Code:
void Font_Init(LPCWSTR fontname); // font name can be Arial, etc void R_DrawString(LPCWSTR string, int x, int y, D3DCOLOR fontcolor);

To create font prototype we will use
Code:
LPD3DXFONT g_font;

So, lets create functions and text rendering!

Text rendering, preparing

Lets create Font_Init method. It's just one line.
Code:
D3DXCreateFont( d3ddevice, FONT_HEIGHT, FONT_WIDTH, FW_BOLD, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, fontname, &g_font );
d3ddevice - Our current D3D Device
FONT_HEIGHT and FONT_WIDTH are integers, i included them in my code.
Code:
int FONT_HEIGHT; int FONT_WIDTH;
fontname - Name of font which will be used to draw text. I will use Arial.
g_font - out for g_font global variable.
Another parameters are not so important.
That what you must get
Code:
int FONT_HEIGHT; int FONT_WIDTH; // create fonts void Font_Init(LPCWSTR fontname) { FONT_HEIGHT = 15; // crete new d3d font D3DXCreateFont( d3ddevice, FONT_HEIGHT, FONT_WIDTH, FW_BOLD, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, fontname, &g_font ); }
Font_Init initializes in R_Init, AFTER ALL ( since it is using d3ddevice, which is initializing )

This line creates d3d font, but it won't show. Now we need to create font itself and set font data. ( Position, color etc )
We will use R_DrawString method Rectangle class.
Let me explain you, how to make it.

Lets create rectangle first
Code:
RECT rectangle;
Now set some parameters

Code:
rectangle.left = x; // X position rectangle.right = SCREEN_HEIGHT; // set right position for text rectangle.top = y; // Y position rectangle.bottom = rectangle.top + 30; // just more space
x and y are parameters in R_DrawString

The following line of code is drawing text with data.
Code:
// render our text g_font->DrawText(NULL, string, -1, &rectangle, 0, fontcolor );
string is our text in parameters.

There's final function
Code:
void R_DrawString(LPCWSTR string, int x, int y, D3DCOLOR fontcolor) { RECT rectangle; rectangle.left = x; // x pos rectangle.right = SCREEN_HEIGHT; // right coords rectangle.top = y; // y pos rectangle.bottom = rectangle.top + 30; // just more space // render our text g_font->DrawText(NULL, string, -1, &rectangle, 0, fontcolor ); }

So, uh, we made font stuff. Lets show it!
As you remember, i've created R_RenderScene. Put R_DrawString in this method.
Example of using:
Code:
// Render your stuff void R_RenderScene(void) { // draw string // 255 255 255 - white // // string x y color R_DrawString(L"Hello ItsMods!", 10, 10, D3DCOLOR_ARGB(255,255,255,255)); }

Press Compile, Run it, and you will see that:
[Image: CONQF.png]

Final code:
Code:
#include <windows.h> #include <windowsx.h> // required #include "stdafx.h" // class LPCWSTR WIN_MAIN = L"MAIN_WINDOW"; // screen data int SCREEN_WIDTH; int SCREEN_HEIGHT; int SCREEN_POS_X; int SCREEN_POS_Y; bool SCREEN_WINDOWED; LPCWSTR WIN_TITLE; // d3d data int D3D_COLOR_R; int D3D_COLOR_G; int D3D_COLOR_B; #include <d3d9.h> #include <d3dx9.h> // added #pragma comment (lib, "d3dx9.lib") // added #pragma comment (lib, "d3d9.lib") LPDIRECT3D9 d3dinit; // d3d interface init LPDIRECT3DDEVICE9 d3ddevice; // d3d device init // function prototypes void R_Init(HWND hWnd); // Initialize d3d device void R_Frame(void); // rendering void CleanD3D(void); // clear display before showing void R_Prepare(void); void R_RenderScene(void); // font stuff void Font_Init(LPCWSTR fontname); void R_DrawString(LPCWSTR string, int x, int y, D3DCOLOR fontcolor); LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int ShowCmd) { R_Prepare(); // Window creation MSG msg; WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_VREDRAW|CS_HREDRAW|CS_OWNDC, WindowProc, 0, 0, hInstance, NULL, NULL, (HBRUSH)(COLOR_WINDOW+3), NULL, WIN_MAIN, NULL}; RegisterClassEx(&wc); // create new window HWND hMainWnd = CreateWindow(WIN_MAIN, WIN_TITLE, WS_OVERLAPPEDWINDOW, SCREEN_POS_X, SCREEN_POS_Y, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL); // show it ShowWindow(hMainWnd, ShowCmd); // INIT DIRECT3D R_Init(hMainWnd); // Initialize d3d stuff UpdateWindow(hMainWnd); // real time loop while(TRUE) { while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } // game loop can be there if(msg.message == WM_QUIT) break; // game loop can be there // recommended there R_Frame(); } CleanD3D(); return(0); } LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_DESTROY: PostQuitMessage(0); return(0); } return DefWindowProc (hWnd, message, wParam, lParam); } // ===== Prepare stuff ===== // void R_Prepare(void) { // mode SCREEN_WINDOWED = TRUE; // size of window SCREEN_WIDTH = 1024; SCREEN_HEIGHT = 768; // position of window SCREEN_POS_X = 0; SCREEN_POS_Y = 0; // title of the window WIN_TITLE = L"Windowed program ^_^"; // L prefix is required ( C++ basics ) // d3d clear color data // blueness D3D_COLOR_R = 0; D3D_COLOR_G = 40; D3D_COLOR_B = 120; } // ===== Direct3D stuff ===== // // init Direct3D void R_Init(HWND hWnd) { d3dinit = Direct3DCreate9(D3D_SDK_VERSION); // interface creationism // presentation parameters D3DPRESENT_PARAMETERS d3dparams; ZeroMemory(&d3dparams, sizeof(d3dparams)); // clear d3d parameters d3dparams.Windowed = SCREEN_WINDOWED; // window mode toggle d3dparams.SwapEffect = D3DSWAPEFFECT_DISCARD; // available: D3DSWAPEFFECT_FLIP, D3DSWAPEFFECT_COPY d3dparams.hDeviceWindow = hWnd; // get owner and set d3d to the window d3dinit->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, // window owner D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dparams, // presentation parameters &d3ddevice); // initialized device // initialize fonts Font_Init(L"Arial"); } // Render // this is the function used to render a single frame void R_Frame(void) { // clear current window d3ddevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(D3D_COLOR_R, D3D_COLOR_G, D3D_COLOR_B), 1.0f, 0); d3ddevice->BeginScene(); // starts rendering current scene // your rendering stuff will be there R_RenderScene(); d3ddevice->EndScene(); // stops rendering current scene d3ddevice->Present(NULL, NULL, NULL, NULL); // shows current scene } // Clean d3d void CleanD3D(void) { d3ddevice->Release(); // destroys d3d device d3dinit->Release(); // destroys d3d stuffs } // =================================================================== // // font LPD3DXFONT g_font; int FONT_HEIGHT; int FONT_WIDTH; // create fonts void Font_Init(LPCWSTR fontname) { FONT_HEIGHT = 30; // crete new d3d font D3DXCreateFont( d3ddevice, FONT_HEIGHT, FONT_WIDTH, FW_BOLD, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, fontname, &g_font ); } // render fonts void R_DrawString(LPCWSTR string, int x, int y, D3DCOLOR fontcolor) { RECT rectangle; rectangle.left = x; // x pos rectangle.right = SCREEN_HEIGHT; // right coords rectangle.top = y; // y pos rectangle.bottom = rectangle.top + 30; // just more space // render our text g_font->DrawText(NULL, string, -1, &rectangle, 0, fontcolor ); } // =================================================================== // // Render your stuff void R_RenderScene(void) { // draw string // 255 255 255 - white R_DrawString(L"Hello ItsMods!", 10, 10, D3DCOLOR_ARGB(255,255,255,255)); }


Well, it's just 2D stuff, in next tutorial i will tell about 3D stuff and more.
C++/Obj-Cdeveloper. Neko engine wip
Steam: Click
Reply

#2
when will lesson 4 come?
Reply

#3
Maybe i will do opengl lessons
C++/Obj-Cdeveloper. Neko engine wip
Steam: Click
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  What game have you bought in the last week? RaZ 12 12,275 12-05-2013, 16:29
Last Post: Nekochan
  Making an apocalyptic themed Minecraft map AZUMIKKEL 54 41,225 08-31-2013, 02:54
Last Post: AZUMIKKEL
  [GAME]The Letter Game Bandarigoda123 65 43,065 08-08-2013, 21:05
Last Post: AZUMIKKEL
  [Release] AntiRage for Infected Game Mode yokai134 17 19,859 08-04-2013, 22:22
Last Post: yokai134
  ESTRANGED best indie game I've seen so far Arteq 0 3,847 08-03-2013, 14:03
Last Post: Arteq
  [HELP] bo2 - FD1 - dis-attach the console from the game masis 8 7,982 07-17-2013, 23:01
Last Post: surtek
Smile [Release] Map & Game Type Changer Plugin (Fixed) 30mba 31 29,042 07-10-2013, 16:27
Last Post: 26hz
  Selling steam account for a game! Strentin 3 5,736 06-18-2013, 07:49
Last Post: xfxtroll
  Making a Heli's angles match my angles? akillj 1 4,125 06-16-2013, 15:01
Last Post: Yamato
  Help Using chat outside in game Bandarigoda123 1 4,185 06-09-2013, 12:46
Last Post: Nekochan

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum Powered By MyBB, Theme by © 2002-2026 Melroy van den Berg.