Sign in to follow this  
chennaiaras

surface - while loop error

Recommended Posts

Hi all,

I am creating a demo program with surface, where one horizontal line goes from left to right repeatedly and another line from bottom to top. Supposed to write a CD where the two lines crosses.

This is my coding :
-------------------

#pragma once

#include <windows.h>
#include "declarations.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int lCmdShow) {
// create the window
if(!initWindow(hInstance))
return false;

// create DirectX object and device
if(!initDirect3d())
return false;

// create surface and load it with images
createSurface();

MSG msg;
ZeroMemory(&msg, sizeof(msg));
while(msg.message!=WM_QUIT) {
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else {
// the main functionality comes here
render();
}
}
shutDown();
return (int) msg.wParam;
}

// register window class and create the window
bool initWindow(HINSTANCE hInstance) {
WNDCLASSEX wcex;
// fill the window class variables
wcex.cbClsExtra =0;
wcex.cbSize =sizeof(WNDCLASSEX);
wcex.cbWndExtra =0;
wcex.hbrBackground =(HBRUSH)(COLOR_WINDOW+1);
wcex.hCursor =LoadCursor(NULL, IDC_ARROW);
wcex.hIcon =0;
wcex.hIconSm =0;
wcex.hInstance =hInstance;
wcex.lpfnWndProc =(WNDPROC)WndProc;
wcex.lpszClassName ="Window";
wcex.lpszMenuName ="WindowSample";
wcex.style =CS_HREDRAW | CS_VREDRAW;

// register the window class
if(!RegisterClassEx(&wcex)) {
MessageBox(NULL,"Window registration failed", "Error!", MB_ICONEXCLAMATION | MB_OK);
return false;
}

// create the window
wndHandle =CreateWindow("window","WindowSample", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, S_WIDTH, S_HEIGHT, NULL, NULL,
hInstance, NULL);

if(!wndHandle) {
MessageBox(NULL,"Window creation failed", "Error!", MB_ICONEXCLAMATION | MB_OK);
return false;
}

ShowWindow(wndHandle, SW_SHOW);
UpdateWindow(wndHandle);
return true;
}

// callback function for the window
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch(message) {
case WM_DESTROY :
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}

// initialize directX
bool initDirect3d(void) {
d3d = NULL;
d3dDevice = NULL;

// creating d3d object
d3d = Direct3DCreate9(D3D_SDK_VERSION);
if(d3d == NULL) {
MessageBox(NULL,"could not initialize D3D object", "Error!", MB_ICONEXCLAMATION | MB_OK);
return false;
}

// initiate present parameters
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferHeight = S_HEIGHT;
d3dpp.BackBufferWidth = S_WIDTH;
d3dpp.hDeviceWindow = wndHandle;

// creating d3d device
d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wndHandle, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp,
&d3dDevice);

if(d3dDevice == NULL) {
MessageBox(NULL,"could not initialize D3D device", "Error!", MB_ICONEXCLAMATION | MB_OK);
return false;
}

return true;
}

// create surface
void createSurface() {
// create surface
hResult = d3dDevice->CreateOffscreenPlainSurface(100,100,D3DFMT_X8R8G8B8,D3DPOOL_DEFAULT,&surface,NULL);
if(FAILED(hResult))
return;

// load the surface with the hzline
hzsrc.left=10; hzsrc.right=79; hzsrc.top=0; hzsrc.bottom=6;
hResult = D3DXLoadSurfaceFromFile(surface,NULL,&hzsrc,"hz.bmp",NULL,D3DX_DEFAULT,0,NULL);

// load the surface with the hzline
vrsrc.left=10; vrsrc.right=79; vrsrc.top=0; vrsrc.bottom=6;
hResult = D3DXLoadSurfaceFromFile(surface,NULL,&vrsrc,"vr.bmp",NULL,D3DX_DEFAULT,0,NULL);
if(FAILED(hResult))
return;
}

// the main functionality
void render() {
// clear the screen with black color
d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f,0);

// get backbuffer
d3dDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO, &backbuffer);

while(hzdest.right<=S_WIDTH) {
//get the hzline in surface to the backbuffer
hzdest.left=0; hzdest.right=69; hzdest.top=70; hzdest.bottom=76;
d3dDevice->StretchRect(surface,&hzsrc,backbuffer,&hzdest,D3DTEXF_NONE);
hzdest.left+=69;
hzdest.right+=69;

//get the hzline in surface to the backbuffer
vrdest.left=500; vrdest.right=506; vrdest.top=S_HEIGHT-63; vrdest.bottom=S_HEIGHT;
d3dDevice->StretchRect(surface,&vrsrc,backbuffer,&vrdest,D3DTEXF_NONE);
vrdest.top-=63;
vrdest.bottom-=63;
}

// present the backbuffer to the frontbuffer (screen)
d3dDevice->Present(NULL,NULL,NULL,NULL);
}

// free the objects and devices and close everything
void shutDown() {
// release directx device
if( d3dDevice != NULL) {
d3dDevice->Release();
d3dDevice = NULL;
}
// release directx object
if( d3d != NULL) {
d3d->Release();
d3d = NULL;
}
}

The problem is, in the render() function.. when i give the 'while' loop the system hangs... otherwise it shows two lines in a black window.

what is the solution?.. Is there any error in my coding?..

Thanks,
aras

Share this post


Link to post
Share on other sites
Well, have you thought about how the loop should exit?

In any case, you are approaching this the wrong way. The render() function should render a single frame. Some external loop should control where the lines are rendered.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this