02-04-2013, 15:21
Hey, again....
Click HERE for previous lesson.
Today i will tell about text creating in game.
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.
So, lets make font rendering
Firstly, you need to add new header and library files.
Required for fonts and another stuff.
Let's add two methods, Font_Init and R_DrawString.
To create font prototype we will use
So, lets create functions and text rendering!
Lets create Font_Init method. It's just one line.
d3ddevice - Our current D3D Device
FONT_HEIGHT and FONT_WIDTH are integers, i included them in my code.
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
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
Now set some parameters
x and y are parameters in R_DrawString
The following line of code is drawing text with data.
string is our text in parameters.
There's final function
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:
Press Compile, Run it, and you will see that:
Final code:
Well, it's just 2D stuff, in next tutorial i will tell about 3D stuff and more.
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 );
FONT_HEIGHT and FONT_WIDTH are integers, i included them in my code.
Code:
int FONT_HEIGHT;
int FONT_WIDTH;
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 );
}
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;
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
The following line of code is drawing text with data.
Code:
// render our text
g_font->DrawText(NULL, string, -1, &rectangle, 0, fontcolor );
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:
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.