Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <d3d9.h>
- #include <d3dx9.h>
- #include <iostream>
- #include <stdio.h>
- #include <io.h>
- #include <fcntl.h>
- #include <string>
- #include "SpriteBatcher.h"
- #pragma comment (lib, "d3d9.lib")
- #pragma comment (lib, "d3dx9.lib")
- #define SCREEN_WIDTH 800
- #define SCREEN_HEIGHT 600
- //Global Vars
- HINSTANCE hInstance;
- HWND winHandle;
- RECT windowSize;
- LPDIRECT3D9 d3d;
- LPDIRECT3DDEVICE9 device;
- //---------------------------------
- LPDIRECT3DTEXTURE9 tex;
- LPDIRECT3DTEXTURE9 tex2;
- SpriteBatcher batcher;
- float x;
- //Prototypes
- LRESULT CALLBACK winProc(HWND winHandle, UINT message, WPARAM wParam, LPARAM lParam);
- void createDebugConsole();
- bool initWindow(HINSTANCE hInstance);
- bool initDirectX();
- void render();
- void cleanUp();
- //Create the main entry point
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
- {
- //Set up the console
- createDebugConsole();
- //Create the window
- if(!(initWindow(hInstance)))
- return false;
- //Init Direct X
- if(!(initDirectX()))
- return false;
- //Prepare the game loop
- MSG msg;
- ZeroMemory(&msg, sizeof(MSG));
- //While there is no quit message run the game loop
- while(msg.message != WM_QUIT)
- {
- //Handle window messages
- while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- //Game Code
- render();
- }
- //Clean up clean up
- cleanUp();
- return msg.wParam;
- }
- #pragma region Window Creation and call back
- bool initWindow(HINSTANCE hInstance)
- {
- //Set up the window properties
- WNDCLASSEX wc;
- ZeroMemory(&wc, sizeof(WNDCLASSEX));
- //Window properties
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = winProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = 0;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
- wc.lpszMenuName = NULL;
- wc.lpszClassName = "window";
- wc.hIconSm = 0;
- //Register the window class
- RegisterClassEx(&wc);
- //Set the save/size of the window
- windowSize.top = 0;
- windowSize.left = 0;
- windowSize.right = SCREEN_WIDTH;
- windowSize.bottom = SCREEN_HEIGHT;
- AdjustWindowRectEx(&windowSize, WS_OVERLAPPEDWINDOW, false, NULL);
- //Create the window
- winHandle = CreateWindowEx(NULL,
- "window",
- "My Window",
- WS_OVERLAPPEDWINDOW,
- 500,
- 150,
- windowSize.right - windowSize.left,
- windowSize.bottom - windowSize.top,
- NULL,
- NULL,
- hInstance,
- NULL);
- if(winHandle == false)
- return false;
- //Show the window
- ShowWindow(winHandle, SW_SHOW);
- return true;
- }
- //Call back funciton for the window
- LRESULT CALLBACK winProc(HWND winHandle, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch(message)
- {
- case WM_DESTROY:
- {
- PostQuitMessage(0);
- }
- break;
- }
- return DefWindowProc(winHandle, message, wParam, lParam);
- }
- #pragma endregion
- //Setup direct x
- bool initDirectX()
- {
- //Set up all the direct x properties
- d3d = NULL;
- device = NULL;
- if(NULL == (d3d = Direct3DCreate9(D3D_SDK_VERSION)))
- return false;
- D3DPRESENT_PARAMETERS d3dpp;
- ZeroMemory(&d3dpp, sizeof(d3dpp));
- RECT rect;
- GetClientRect(winHandle, &rect);
- std::cout<<"Win Width:"<<rect.right - rect.left<<std::endl;
- std::cout<<"Win Height: "<<rect.bottom - rect.top<<std::endl;
- d3dpp.Windowed = true;
- d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
- d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
- d3dpp.BackBufferCount = 1;
- d3dpp.BackBufferWidth = rect.right - rect.left;
- d3dpp.BackBufferHeight = rect.bottom - rect.top;
- d3dpp.hDeviceWindow = winHandle;
- //Create the direct x device
- if(FAILED(d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, winHandle, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &device)))
- return false;
- //Texture items
- device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
- device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
- device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
- std::string texture = "img.png";
- std::string texture2 = "img2.png";
- HRESULT hr = D3DXCreateTextureFromFile(device, texture.c_str(), &tex);
- if(FAILED(hr))
- std::cout<<"Failed to load texture! "<<texture<<std::endl;
- std::cout<<"Texture 1:"<<tex<<std::endl;
- hr = D3DXCreateTextureFromFile(device, texture2.c_str(), &tex2);
- if(FAILED(hr))
- std::cout<<"Failed to load texture2! "<<texture2<<std::endl;
- std::cout<<"Texture 2:"<<tex2<<std::endl;
- //Set the device to use for the sprite batcher
- batcher.setBatcherDevice(device);
- x = 10.0f;
- return true;
- }
- //Draw the things that need to be drawn
- void render()
- {
- //Clear the screen
- device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);
- device->BeginScene();
- //Draw
- batcher.beginBatch();
- batcher.draw(250.0f, 50.0f, 64.0f, 64.0f, D3DCOLOR_XRGB(0,0,255), tex);
- batcher.draw(200.0f, 200.0f, 64.0f, 64.0f, D3DCOLOR_XRGB(0,0,255), tex);
- batcher.endBatch();
- device->EndScene();
- device->Present(NULL, NULL, NULL, NULL);
- }
- //Clean up
- void cleanUp()
- {
- //Clean up the D3D device
- if(device != NULL)
- {
- device->Release();
- device = NULL;
- }
- //Clean up the D3D object
- if(d3d != NULL)
- {
- d3d->Release();
- d3d = NULL;
- }
- }
- void createDebugConsole()
- {
- AllocConsole();
- HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
- int hCrt = _open_osfhandle((long) handle_out, _O_TEXT);
- FILE* hf_out = _fdopen(hCrt, "w");
- setvbuf(hf_out, NULL, _IONBF, 1);
- *stdout = *hf_out;
- HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
- hCrt = _open_osfhandle((long) handle_in, _O_TEXT);
- FILE* hf_in = _fdopen(hCrt, "r");
- setvbuf(hf_in, NULL, _IONBF, 128);
- *stdin = *hf_in;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement