what's Wrong In this Code PLZ PLZ
/*--------------------------------------------------------------------------*/
// ARAB SPACE
//
// By Ahmed Saleh Mohammed Tolba
//
// CopyRight(c) 2001
//
//
//
/*--------------------------------------------------------------------------*/
//------ Include Files ------//
#include "stdafx.h"
#define INITGUID
#include
#include
#include
#include
#include
#include "wave.h"
//------ Window Class Information ------//
static char szClass[] = "Arab Space";
static char szCaption[] = "ARAB SPACE";
//------ Global Interface Pointers ------//
LPDIRECTDRAW7 lpDD=NULL;
LPDIRECTDRAWSURFACE7 lpDDSPrimary=NULL;
LPDIRECTDRAWSURFACE7 lpDDSBack=NULL;
//------ Define Number of Sounds ------//
#define NUMSOUNDS 1
//------ DirectSound Object and Buffers ------//
LPDIRECTSOUND lpDS;
LPDIRECTSOUNDBUFFER lpDSBSounds[NUMSOUNDS];
LONG lSirenPan;
LONG lSirenPanInc;
//------Define number of images and set up list of file names ------//
#define IMAGE_COUNT 5
char file_names[IMAGE_COUNT][256] = { "start.bmp",
"load.bmp",
"cerdit.bmp",
"exit.bmp",
};
//------ DirectDraw Surfaces for Image Storage ------//
LPDIRECTDRAWSURFACE7 lpSlides[IMAGE_COUNT];
//------ current image displayed------//
int cur_image=0;
//------ Error Return String ------//
const char *ErrStr=NULL;
//------ Error Messages ------//
const char Err_Reg_Class[] = "Error Registering Window Class";
const char Err_Create_Win[] = "Error Creating Window";
const char Err_DirectDrawCreate[] = "DirectDrawCreate FAILED";
const char Err_Coop[] = "SetCooperativeLevel FAILED";
const char Err_CreateSurf[] = "CreateSurface FAILED";
const char Err_GetBack[] = "Error Retrieving Back Buffer";
const char Err_LoadBMP[] = "Error Loading Image";
const char Err_DispMode[] = "Error Setting Display Mode";
const char Err_LoadImage[] = "Error Loading Image";
const char Err_DirectSoundCreate[] = "DirectSoundCreate FAILED";
const char Err_CreateBuff[] = "CreateBuffer FAILED";
const char Err_LoadWAV[] = "Error Loading Sound";
//------ Function to Load a Bitmap into a DirectDraw Surface ------//
LPDIRECTDRAWSURFACE7 bitmap_surface(LPCTSTR file_name,RECT *dims=NULL)
{
HDC hdc;
HBITMAP bit;
LPDIRECTDRAWSURFACE7 surf=NULL;
// load the bitmap
bit=(HBITMAP) LoadImage(NULL,file_name,IMAGE_BITMAP,0,0,
LR_DEFAULTSIZE|LR_LOADFROMFILE);
if (!bit)
// failed to load, return failure to caller
return NULL;
// get bitmap dimensions
BITMAP bitmap;
GetObject( bit, sizeof(BITMAP), &bitmap );
int surf_width=bitmap.bmWidth;
int surf_height=bitmap.bmHeight;
// create surface
HRESULT ddrval;
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize = sizeof(DDSURFACEDESC2);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT ;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = surf_width;
ddsd.dwHeight = surf_height;
// attempt to create surface
ddrval=lpDD->CreateSurface(&ddsd,&surf,NULL);
// created ok?
if (ddrval!=DD_OK) {
// no, release the bitmap and return failure to caller
DeleteObject(bit);
return NULL;
} else {
// yes, get a DC for the surface
surf->GetDC(&hdc);
// generate a compatible DC
HDC bit_dc=CreateCompatibleDC(hdc);
// blit the interface to the surface
SelectObject(bit_dc,bit);
BitBlt(hdc,0,0,surf_width,surf_height,bit_dc,0,0,SRCCOPY);
// release the DCs
surf->ReleaseDC(hdc);
DeleteDC(bit_dc);
// save the dimensions if rectangle pointer provided
if (dims) {
dims->left=0;
dims->top=0;
dims->right=surf_width;
dims->bottom=surf_height;
}
}
// clear bitmap
DeleteObject(bit);
// return pointer to caller
return surf;
}
//------ Cleanup Function to Release Objects ------//
#define SafeRelease(x) if (x) { x->Release(); x=NULL;}
void Cleanup()
{
// release loaded image surfaces
for (int i=0;i);
// release DirectDraw interfaces
SafeRelease(lpDDSPrimary);
SafeRelease(lpDD);
SafeRelease(lpDS);
// display error if one thrown
if (ErrStr) {
MessageBox(NULL, ErrStr, szCaption, MB_OK);
ErrStr=NULL;
}
}
//------ Function to Draw a Slide ------//
void draw_slide()
{
// make sure we have the current image, don''t draw if we fail
if (!lpSlides[cur_image]) {
lpSlides[cur_image]=bitmap_surface(file_names[cur_image]);
if (!lpSlides[cur_image])
return;
}
// draw the object to the screen
lpDDSBack->BltFast(0,0,lpSlides[cur_image],NULL,DDBLTFAST_WAIT);
// draw instructions for slide show
HDC hdc;
if (DD_OK==lpDDSBack->GetDC(&hdc)) {
TextOut(hdc,320,400,"Arab Space Ver 0,1",18);
SetTextColor(hdc,0x0000ffff);
lpDDSBack->ReleaseDC(hdc);
}
// flip to the primary surface
lpDDSPrimary->Flip(0,DDFLIP_WAIT);
// make sure we have the next and previous image
// this insures that our next selection is quickly
// available, while we only need to load one image
// when the program starts.
int next_slide=(cur_image>=IMAGE_COUNT-1) ? 0 : cur_image+1;
if (!lpSlides[next_slide])
lpSlides[next_slide]=bitmap_surface(file_names[next_slide]);
int prev_slide=(cur_image<1) ? IMAGE_COUNT-1 : cur_image-1;
if (!lpSlides[prev_slide])
lpSlides[prev_slide]=bitmap_surface(file_names[prev_slide]);
}
//------ Windows Message Handler ------//
LRESULT CALLBACK
WindowProc(HWND hWnd, unsigned uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
Cleanup();
PostQuitMessage(0);
break;
case WM_KEYDOWN:
switch (wParam)
{
case VK_UP:
// Process the LEFT ARROW key.
cur_image--;
if (cur_image<0)
cur_image=IMAGE_COUNT-1;
draw_slide();
break;
case VK_DOWN:
// Process the RIGHT ARROW key.
cur_image++;
if (cur_image>IMAGE_COUNT-1)
cur_image=0;
draw_slide();
break;
case VK_ESCAPE:
// exit the program on escape
DestroyWindow(hWnd);
break;
// Process other non-character keystrokes.
default:
break;
}
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0L;
}
BOOL load_sounds()
{
// Initialize waves
CWave waves[NUMSOUNDS];
waves[0].Create("Siren.wav");
// Initialize secondary DS buffers
for (int i = 0; i < NUMSOUNDS; i++) {
// Get the wave information
DWORD dwDataLen = waves.GetDataLen();
WAVEFORMATEX wfFormat;
waves.GetFormat(wfFormat);
// Setup the DS buffer description
DSBUFFERDESC dsbdDesc;
ZeroMemory(&dsbdDesc, sizeof(DSBUFFERDESC));
dsbdDesc.dwSize = sizeof(DSBUFFERDESC);
dsbdDesc.dwFlags = DSBCAPS_STATIC;
dsbdDesc.dwBufferBytes = dwDataLen;
dsbdDesc.lpwfxFormat = &wfFormat;
// Create the DS buffer
if (lpDS->CreateSoundBuffer(&dsbdDesc,
&lpDSBSounds, NULL) != DS_OK)
{
ErrStr = Err_CreateBuff;
return FALSE;
}
// Lock the DS buffer
BYTE* pDSBuffData;
if (lpDSBSounds->Lock(0, dwDataLen, (VOID **)&pDSBuffData,
&dwDataLen, NULL, 0, 0) != DS_OK)
{
ErrStr = Err_LoadWAV;
return FALSE;
}
// Write wave data to the DS buffer
dwDataLen = waves.GetData(pDSBuffData, dwDataLen);
// Unlock the DS buffer
if (lpDSBSounds->Unlock(pDSBuffData, dwDataLen, NULL, 0) !=
DS_OK)
{
ErrStr = Err_LoadWAV;
return FALSE;
}
}
return TRUE;
}
//—— Function to Initialize DirectDraw and the Application ——//
static BOOL Init(HINSTANCE hInstance, int nCmdShow)
{
WNDCLASS wc;
HRESULT ddrval;
// Set up and register window class
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = sizeof(DWORD);
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = szClass;
if (!RegisterClass(&wc)) {
ErrStr=Err_Reg_Class;
return FALSE;
}
// Get dimensions of display
int ScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int ScreenHeight = GetSystemMetrics(SM_CYSCREEN);
// Create a window and display
HWND hWnd;
hWnd = CreateWindow(szClass, // class
szCaption, // caption
WS_VISIBLE|WS_POPUP, // style
0, // left
0, // top
ScreenWidth, // width
ScreenHeight, // height
NULL, // parent window
NULL, // menu
hInstance, // instance
NULL); // parms
if (!hWnd) {
ErrStr=Err_Create_Win;
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// clear image pointers
ZeroMemory(lpSlides,sizeof(lpSlides));
// Create the main DirectDraw object
ddrval = DirectDrawCreateEx(NULL, (VOID **) &lpDD, IID_IDirectDraw7, NULL);
if (ddrval != DD_OK) {
ErrStr=Err_DirectDrawCreate;
return FALSE;
}
// Set our cooperative level
ddrval = lpDD->SetCooperativeLevel(hWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );
if (ddrval != DD_OK) {
ErrStr=Err_Coop;
return FALSE;
}
// Set the display mode
ddrval = lpDD->SetDisplayMode( 640, 480, 16, 0, 0);
if (ddrval !=DD_OK) {
ErrStr=Err_DispMode;
return FALSE;
}
// Create the primary surface with 1 back buffer
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
DDSCAPS_FLIP |
DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount = 1;
ddrval = lpDD->CreateSurface( &ddsd, &lpDDSPrimary, NULL );
if (ddrval!=DD_OK) {
ErrStr=Err_CreateSurf;
return FALSE;
}
// Fetch back buffer interface
DDSCAPS2 ddscaps;
ZeroMemory(&ddscaps,sizeof(ddscaps));
ddscaps.dwCaps=DDSCAPS_BACKBUFFER;
ddrval=lpDDSPrimary->GetAttachedSurface(&ddscaps,&lpDDSBack);
if (ddrval!=DD_OK) {
ErrStr=Err_GetBack;
return FALSE;
}
// load the first image and display it
lpSlides[0]=bitmap_surface(file_names[0]);
if (!lpSlides[0])
return FALSE;
draw_slide();
// return success to caller
return TRUE;
}
// Create the DS object
if (DirectSoundCreate(NULL, &lpDS, NULL) != DS_OK)
{
return FALSE;
}
// Set the cooperation level for the DS object
if (lpDS->SetCooperativeLevel(hWnd, DSSCL_PRIORITY) != DS_OK)
{
return FALSE;
}
// Initialize the DS buffers
if (!load_sounds())
{
return FALSE;
}
//—— Application Loop ——//
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// initialize the application, exit on failure
if (!Init(hInstance, nCmdShow)) {
Cleanup();
return FALSE;
}
// run till completed
MSG msg;
while (GetMessage(&msg,NULL,0,0)) {
// dispatch the message
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// exit returning final message
return (msg.wParam);
}
code]
</i>
You will need to be a little more specific as to what the problem is. It would be quite a chore reading through that code looking for unknown errors.
Seeya
Krippy
Seeya
Krippy
Also, make sure you format the code using the appropriate GameDev tags. Read the FAQ for help. You can''t see code sections where greater than or less than signs are used.
Think to self: "Don''t people preview or edit their post?"
Jinushaun
Think to self: "Don''t people preview or edit their post?"
Jinushaun
Actually I don''t usually preview my posts (that''s why they''re so incoherent), but if I was posting this much code (somebody slap me if I do) then I would.
in response to Ahmed Saleh:
The problem with your code is that it''s too long. Try to find out where the error you must have (otherwise why would you post?) is, and only post that portion. Then people will be more willing to look it over for you.
in response to Ahmed Saleh:
The problem with your code is that it''s too long. Try to find out where the error you must have (otherwise why would you post?) is, and only post that portion. Then people will be more willing to look it over for you.
Or read your other thread on this forum where I told you what the problem was... lol
But next time ATLEAST use code formatting in your post otherwise I''m not gonna even try... lol
Seeya
Krippy
But next time ATLEAST use code formatting in your post otherwise I''m not gonna even try... lol
Seeya
Krippy
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement