Whats wrong with my code
Ok we are in class and cant figure out how to make the rings transparent. We are using the code from the book
"The Zen of Direct 3D Game Programming.
The space background shows up, and the rings show up and are animated but the color 0,255,0,255 on the rings background are not transparent. here is the code anyone have any idea?
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>
#define MAX_SPRITES 1
#include <d3d8.h>
#include <d3dx8.h>
#include "Engine.h"
int GameInit();
int GameLoop();
int GameShutdown();
int Render();
//////////GLOBAL/////
CSprite g_RingSprites[MAX_SPRITES];
LPDIRECT3DSURFACE8 g_RingSurface = 0;
LPDIRECT3DSURFACE8 pSpaceSurf = 0;
/////////////////////////////////////////////
HWND g_hWndMain;
//the window procedure to handle events
long CALLBACK WndProc ( HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
{
// Switch the windows message to figure out what it is
switch ( uMessage )
{
case WM_CREATE: // The CreateWindow() function was just called
{
// One Time Initialization
return 0;
}
case WM_PAINT: // The window needs to be redrawn
{
ValidateRect( hWnd, NULL);
return 0;
}
case WM_DESTROY: // The window is about to be closed
{
// Our main window is closing
// This means we want our app to exit.
// Tell windows to put a WM_QUIT message in our message queue
PostQuitMessage ( 0 );
return 0;
}
default: // Some other message
{
// Let windows handle this message
return DefWindowProc( hWnd, uMessage, wParam, lParam);
}
}
}
// The window entry point. The application will start executing here
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE HPrevInstance, PSTR pstrCmdLine, int iCmdShow)
{
HWND hWnd; // The handle to our main window
MSG msg; // The message windows is sending us
WNDCLASSEX wc; // The window class used to create our window
// The name of our class and also the title to our window
static char strAppName[] = "Windowed Direct3D Initialization";
// Fill in the window class with the attributes for our main window
// The size of this structure in bytes
wc.cbSize = sizeof (WNDCLASSEX );
// The style of the window.
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
// Useless information just set to zero
wc.cbClsExtra = 0;
// Useless information just set to zero
wc.cbWndExtra = 0;
// The name of our event handler
wc.lpfnWndProc = WndProc;
// A handle to the application instance
wc.hInstance = hInstance;
// The handle to the brush to use for the window background
wc.hbrBackground = (HBRUSH) GetStockObject (DKGRAY_BRUSH);
// A handle to the icon to use for the window
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
// A handle to a smaller version of the apps icon
wc.hIconSm = LoadIcon( NULL, IDI_HAND);
// A handle to the cursor to use while the mouse is over our window
wc.hCursor = LoadCursor (NULL, IDC_CROSS);
// A handle to the resource to use as our menue
wc.lpszMenuName = NULL;
// The human readable name for this class
wc.lpszClassName = strAppName;
// Register the class with windows
RegisterClassEx (&wc);
// Create the window based on the previous class
hWnd = CreateWindowEx( NULL, //Advanced style settings
strAppName, // The name of the class
strAppName, // The window caption
WS_OVERLAPPEDWINDOW, // The window style
CW_USEDEFAULT, // The initial x position
CW_USEDEFAULT, // The initial y position
512, 512, // The initial width / height
NULL, // Handle to parent window
NULL, // Handle to the menu
hInstance, // Handle to the apps instance
NULL); // Advanced context
g_hWndMain = hWnd; // Display the window we just created
ShowWindow(hWnd, iCmdShow); // Draw the window contents for the first time
UpdateWindow ( hWnd);
if (FAILED (GameInit() ))
{
SetError ( "Initialization Failed" );
GameShutdown();
return E_FAIL;
}
// Start the message loop - this is where the Game Code Starts
while (TRUE)
{
// check if a message is waiting for processing
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
//Check if the message is to quit the application
if(msg.message == WM_QUIT)
// Exit the message loop
break;
// Change the format of certain messages
TranslateMessage (&msg);
// Pass the message to WndProc() for processing
DispatchMessage (&msg);
}
else
{
GameLoop();
}
}
GameShutdown(); // Return control to windows with the exit code
return msg.wParam;
}
//******* GAME FUNCTIONS *************//
LPDIRECT3DSURFACE8 pTransSurf = 0; // Global Variable
//LPDIRECT3DSURFACE8 pSpaceSurf = 0; // Global Variable
int GameInit()
{
HRESULT r = 0;
//Acquire a pointer to IDirect3D8
g_pD3D = Direct3DCreate8 (D3D_SDK_VERSION);
if( g_pD3D == NULL)
{
SetError( "Could not create IDirect3D8 object");
return E_FAIL;
}
// create the device
r = InitDirect3DDevice (g_hWndMain, 640, 480, FALSE, D3DFMT_X8R8G8B8, g_pD3D, &g_pDevice);
if (FAILED (r) )
{
SetError( "Initialization of the device failed");
return E_FAIL;
}
//Clear the back buffer
g_pDevice->Clear(0,0,D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,55), 1.0f, 0);
//Get a Pointer to the back buffer and save it in a global variable
r = g_pDevice->GetBackBuffer (0, D3DBACKBUFFER_TYPE_MONO, &g_pBackSurface);
if(FAILED( r ) )
{
SetError("Couldnt get backbuffer" );
return E_FAIL;
}
srand (GetTickCount() );
InitTiming();
LoadBitmapToSurface ("Space.bmp", &pSpaceSurf, g_pDevice);
LoadBitmapToSurface ("rings.bmp", &g_RingSurface, g_pDevice);
for (int SpriteCount = 0; SpriteCount < MAX_SPRITES; SpriteCount++)
{
g_RingSprites [SpriteCount].Initialize (g_RingSurface, 128, 128, 9);
g_RingSprites [SpriteCount].SetVelocity(rand()%8, rand()%8);
g_RingSprites [SpriteCount].SetPosition(rand()%(g_DeviceWidth -128), rand()%(g_DeviceHeight -128));
}
return S_OK;
}
int GameLoop()
{
FrameCount();
Render();
if( GetAsyncKeyState (VK_ESCAPE ) )
PostQuitMessage ( 0 );
return S_OK;
}
int GameShutdown()
{
pTransSurf ->Release();
pSpaceSurf ->Release();
g_RingSurface ->Release();
g_RingSurface = 0;
// Release the pointer to the back surface
if( g_pBackSurface)
g_pBackSurface ->Release();
// Release the pointer to IDirect3Device8
if(g_pDevice)
g_pDevice ->Release();
//Release the pointer to IDirect3D8
if(g_pD3D)
g_pD3D ->Release();
return S_OK;
}
int Render()
{
HRESULT r = 0;
g_pDevice ->Clear ( 0, 0, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,25), 1.0f,0);
//Make sure the device is valid
if(!g_pDevice)
{
SetError( "Cannot render because there is no device" );
return E_FAIL;
}
//Return if the device is not ready;
r = ValidateDevice();
if(FAILED (r) )
{
return E_FAIL;
}
/////////////////////RING SPRITE CODE////////////////////////
static BOOL bIncrease = TRUE;
static int CurrentSprite[MAX_SPRITES];
CopySurfaceToSurface (0, pSpaceSurf, 0, g_pBackSurface, 0, D3DCOLOR_ARGB(0, 0, 0, 0) );
for (int SpriteCount = 0; SpriteCount<MAX_SPRITES; SpriteCount++)
{
if(CurrentSprite [SpriteCount] > 9)
bIncrease = FALSE;
if(CurrentSprite [SpriteCount] < 0)
bIncrease = TRUE;
if(bIncrease)
CurrentSprite [SpriteCount]++;
else
CurrentSprite [SpriteCount] --;
g_RingSprites [ SpriteCount].SelectActiveSprite(CurrentSprite[SpriteCount]);
int VelX, VelY, PosX, PosY;
g_RingSprites[SpriteCount].GetPosition(&PosX, &PosY);
g_RingSprites[SpriteCount].GetVelocity(&VelX, &VelY);
if(PosX > 639 - 128)
VelX = -VelX;
if(PosY > 479 - 128)
VelY = -VelY;
if(PosX < 0)
VelX = -VelX;
if (PosY < 0)
VelY = -VelY;
g_RingSprites [SpriteCount].SetVelocity (VelX, VelY);
g_RingSprites [SpriteCount].Update();
g_RingSprites [SpriteCount].Render(g_pBackSurface);
}
/////////////End Ring Code/////////////////////
D3DLOCKED_RECT Locked;
// CopySurfaceToSurface ( 0, pSpaceSurf, 0, g_pBackSurface, 0, D3DCOLOR_ARGB( 0, 255, 0, 255) );
POINT Point = {100, 100};
CopySurfaceToSurface (0, pTransSurf, &Point, g_pBackSurface, TRUE, D3DCOLOR_ARGB( 0,255,0,255) );
g_pBackSurface ->LockRect(&Locked, 0, 0);
g_pBackSurface -> UnlockRect();
r = g_pDevice -> Present (NULL, NULL, NULL, NULL );
return S_OK;
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement