#include <windows.h>
#include <d2d1.h>
#include <d2d1helper.h>
#include <dwrite.h>
#pragma comment(lib, "dwrite.lib")
#pragma comment(lib, "d2d1.lib")
HINSTANCE g_hinst;
HWND g_hwnd;
static bool g_flag_app_exit;
ID2D1Factory * g_factory;
ID2D1HwndRenderTarget * g_render_target;
ID2D1SolidColorBrush * g_brush;
ID2D1StrokeStyle * g_stroke_style_list[5];
#define SafeRelease(p) if(p){p->Release(); p = NULL;}
void OnSize(LPARAM lparam)
{
if(g_render_target)
g_render_target->Resize(D2D1::SizeU(LOWORD(lparam),HIWORD(lparam)));
}
bool AppInit()
{
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &g_factory);
RECT rc;
GetClientRect(g_hwnd, &rc);
g_factory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(g_hwnd, D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top) ),
&g_render_target);
g_render_target->CreateSolidColorBrush(D2D1::ColorF(1.0f, 1.0f, 1.0f), &g_brush);
for(int i=0; i<5; i++)
{
g_factory->CreateStrokeStyle(D2D1::StrokeStyleProperties(
D2D1_CAP_STYLE_TRIANGLE,
D2D1_CAP_STYLE_ROUND,
D2D1_CAP_STYLE_ROUND,
D2D1_LINE_JOIN_MITER,
1.0f,
(D2D1_DASH_STYLE)i,
10.0f),
NULL,
0,
&(g_stroke_style_list[i]));
}
return true;
}
void OnPaint()
{
if(!g_render_target)
return;
g_render_target->BeginDraw();
g_render_target->Clear(D2D1::ColorF(0.12f, 0.12f, 0.12f));
const int OFFY = 50.0f;
D2D1_POINT_2F p1 = {20.0f, 40.0f};
D2D1_POINT_2F p2 = {600.0f, 40.0f};
for(int i=0; i<5; i++)
{
g_render_target->DrawLine(p1, p2, g_brush, 8.0f, g_stroke_style_list[i]);
p1.y += OFFY;
p2.y += OFFY;
}
g_render_target->EndDraw();
}
void AppShutdown()
{
//MessageBox(NULL, "Released","Message!", MB_OK);
for(int i=0; i<5; i++)
{
SafeRelease(g_stroke_style_list[i]);
}
SafeRelease(g_brush);
SafeRelease(g_render_target);
SafeRelease(g_factory);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch(msg)
{
case WM_PAINT:
OnPaint();
break;
case WM_SIZE:
OnSize(lparam);
break;
case WM_CLOSE:
g_flag_app_exit = true;
return 1;
default:
return DefWindowProc(hwnd, msg, wparam, lparam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
MSG msg;
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hinst;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
g_hinst = hinst;
g_hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Direct2D Demo",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
480,
NULL, NULL, hinst, NULL);
if(g_hwnd == NULL)
{
MessageBox(NULL, "Failed to create window !","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
if(!AppInit())
{
MessageBox(NULL, "Failed to initialize application !","Error",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
while(!g_flag_app_exit)
{
if(PeekMessage(&msg, g_hwnd, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
AppShutdown();
return msg.wParam;
}