1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
| #define WIN32_LEAN_AND_MEAN
#define INITGUID
#pragma comment(linker,"/NODEFAULTLIB:strmbasd.lib ")
#include <Windows.h>
#include <mmsystem.h>
#include <d3dx9.h>
#include <d3d9.h>
#pragma comment ( lib, "d3dx9.lib" )
#pragma comment ( lib, "d3d9.lib" )
#pragma comment ( lib, "dxguid.lib")
const int X=1920;
const int Y=1080;
LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
LPD3DXSPRITE g_Sprite = NULL;
LPDIRECT3DTEXTURE9 g_Texture0 = NULL;
LPDIRECT3DTEXTURE9 g_Texture1 = NULL;
LPDIRECT3DTEXTURE9 g_Texture2 = NULL;
LPDIRECT3DSURFACE9 back;
HRESULT loadimage(char *name,LPD3DXSPRITE &Spr,LPDIRECT3DTEXTURE9 &Texture);
void DrawImage(LPD3DXSPRITE Spr,LPDIRECT3DTEXTURE9 Texture,float X0 ,float Y0, float X1 ,float Y1, float Ang);
void clear(int r,int g,int b);
void Cleanup();
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------
void main2d()
{
for(int i=0; i<8; i++)
for(int j=0; j<6; j++)
{
DrawImage(g_Sprite,g_Texture0,i*100,j*100,100,100,0.0);
}
DrawImage(g_Sprite,g_Texture1,100,100,100,100,0.0);
DrawImage(g_Sprite,g_Texture2,100,100,100,100,0.0);
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------
void initload()
{
loadimage("0.png",g_Sprite,g_Texture0);
loadimage("1.png",g_Sprite,g_Texture1);
loadimage("2.png",g_Sprite,g_Texture2);
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------
HRESULT loadimage(char *name,LPD3DXSPRITE &Spr,LPDIRECT3DTEXTURE9 &Texture)
{
D3DXCreateSprite(g_pd3dDevice, &Spr);
D3DXCreateTextureFromFile(g_pd3dDevice, name, &Texture);
return S_OK;
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------
void DrawImage(LPD3DXSPRITE Spr,LPDIRECT3DTEXTURE9 Texture,float X0 ,float Y0, float X1 ,float Y1, float Ang)
{
RECT r;
D3DXMATRIX Matrix;
D3DXVECTOR2 Translation(X0,Y0);
D3DSURFACE_DESC desc; Texture->GetLevelDesc(0, &desc);
float X=desc.Width;
float Y=desc.Height;
r.left=0;
r.top=0;
r.right=X;
r.bottom=Y;
D3DXVECTOR2 spriteCentre=D3DXVECTOR2(X/2,Y/2);
D3DXVECTOR2 scaling(X1/X,Y1/Y);
Spr->Begin(D3DXSPRITE_ALPHABLEND);
D3DXMatrixTransformation2D(&Matrix, NULL, 0.0, &scaling, &spriteCentre, Ang, &Translation);
Spr->SetTransform(&Matrix);
Spr->Draw(Texture, &r, NULL, NULL, 0xFFFFFFFF);
Spr->End();
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------
void Cleanup()
{
if(g_Sprite) g_Sprite->Release();
if( g_pd3dDevice != NULL ) g_pd3dDevice->Release();
if( g_pD3D != NULL ) g_pD3D->Release();
CoUninitialize();
ShowCursor( TRUE );
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------
void Render()
{
g_pd3dDevice->BeginScene() ;
main2d();
g_pd3dDevice->EndScene();
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
if( GetAsyncKeyState( VK_ESCAPE ) ) PostQuitMessage(0);
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd ,int dx,int dy)
{
D3DPRESENT_PARAMETERS d3dpp;
D3DDISPLAYMODE d3ddm;
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
d3dpp.BackBufferWidth = dx;
d3dpp.BackBufferHeight = dy;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; //32 bp
d3dpp.BackBufferCount = 1;//
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
d3dpp.hDeviceWindow = hWnd;//
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.FullScreen_RefreshRateInHz= D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
d3dpp.Flags= D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
g_pD3D->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice );
g_pd3dDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &back );
DWORD dwRenderWidth = dx;
DWORD dwRenderHeight = dy;
D3DVIEWPORT9 vp = { 0, 0, dwRenderWidth, dwRenderHeight, 0.0f, 1.0f };
g_pd3dDevice->SetViewport( &vp );
ShowCursor( TRUE );
SetCursor(LoadCursor(NULL, IDC_ARROW));
srand( GetTickCount() );
clear(0,0,0);
return S_OK;
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
CoInitialize( NULL );
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL,
( HBRUSH )( GetStockObject( BLACK_BRUSH ) ),
NULL, "2D", NULL };
RegisterClassEx( &wc );
HWND hWnd = CreateWindow( "2D", "enZine", WS_POPUP,
0, 0,
GetSystemMetrics( SM_CXSCREEN ),
GetSystemMetrics( SM_CYSCREEN ),
GetDesktopWindow(), NULL, wc.hInstance, NULL );
ShowWindow( hWnd, SW_SHOW);
UpdateWindow( hWnd );
InitD3D( hWnd ,X,Y);
initload();
MSG msg;
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
Render();
}
UnregisterClass( "Bye!", wc.hInstance );
return 0;
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------
void clear(int r,int g,int b)
{
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(r,g,b), 1.0f, 0 );
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
Partager