Advertisement

Pixel and Bitmap problems!!

Started by April 01, 2002 10:31 AM
6 comments, last by Mullvad 22 years, 7 months ago
OK now I getting very Mad why doesnt this code worke??
  
#include <windows.h>
#include <windowsx.h>
#include <ddraw.h>
#include "resource.h"

#pragma comment(lib,"winmm.lib")
#pragma comment(lib,"dxguid.lib")    

#define INIT_DXSTRUCT(dxstruct) { ZeroMemory(&dxstruct, sizeof(dxstruct)); dxstruct.dwSize = sizeof(dxstruct); }

int LoadBitmapResource(LPDIRECTDRAWSURFACE7 lpddsPrimary, int xDest, int yDest, int nResID);

const char g_szClassName[] = "myWindowClass";

LPDIRECTDRAW7 lpdd7 = NULL;
LPDIRECTDRAWSURFACE7 lpddsPrimary = NULL;  // primary surface

LPDIRECTDRAWSURFACE7 lpddsBack = NULL;  // back buffer

LPDIRECTDRAWSURFACE7 lpddsOffscreen = NULL;  // offscreen buffer


DDSURFACEDESC2 ddsd, ddsd2;

//Window stuff

HWND hwnd;
WNDCLASSEX wc;
MSG Msg;

int hej;
long unsigned LastTime;

// Step 4: the Window Procedure

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
		/*case WM_LBUTTONDOWN:
			PostQuitMessage(0);
*/
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{

    

    //Step 1: Registering the Window Class

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window

    hwnd = CreateWindowEx(
        0,
        g_szClassName,
        "The title of my window",
        WS_POPUP | WS_VISIBLE,
        0, 0, 640, 480,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

// initialize COM

if (FAILED(CoInitialize(NULL)))
{
	MessageBox(hwnd, "Couldn`t initialize COM", "Error", MB_OK);
	return(FALSE);
}

// create DirectDraw object

if (FAILED(CoCreateInstance(CLSID_DirectDraw, NULL, CLSCTX_ALL,
							IID_IDirectDraw7, (void**)&lpdd7)))
{
	MessageBox(hwnd, "Couldn`t create DirectDraw object", "Error", MB_OK);
	return(FALSE);
}

// initialize DirectDraw object

if (FAILED(lpdd7->Initialize(NULL)))
{
	MessageBox(hwnd, "Couldn`t initialize DirectDraw object", "Error", MB_OK);
	return(FALSE);
}

//Set cooperative level

if (FAILED(lpdd7->SetCooperativeLevel(hwnd, DDSCL_ALLOWREBOOT | 
	DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN)))
{
	MessageBox(hwnd, "Couldn`t set cooperative level", "Error", MB_OK);
	return(FALSE);
}

//Set display mode to 640x480/16

if(FAILED(lpdd7->SetDisplayMode(640, 480, 16, 0, 0)))
{
	MessageBox(hwnd, "Couldn`t set display mode", "Error", MB_OK);
	return(FALSE);
}

// set up primary drawing surface

INIT_DXSTRUCT(ddsd);                              // initialize ddsd

ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;  // valid flags

ddsd.dwBackBufferCount = 1;                       // one back buffer

ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |    // primary surface

                      DDSCAPS_COMPLEX |           // back buffer is chained

                      DDSCAPS_FLIP |              // allow page flipping

                      DDSCAPS_VIDEOMEMORY;        // create in video memory

        


// set up offscreen surface

INIT_DXSTRUCT(ddsd2);                                  // initialize ddsd

ddsd2.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;  // valid flags

ddsd2.dwWidth = 400;                                   // set width

ddsd2.dwHeight = 300;                                  // set height

ddsd2.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN |        // offscreen buffer

                      DDSCAPS_VIDEOMEMORY;            // video memory



//create primary surface

if(FAILED(lpdd7->CreateSurface(&ddsd, &lpddsPrimary, NULL)))
{
	MessageBox(hwnd, "Couldn`t create primary surface", "Error", MB_OK);
	return(FALSE);
}

// get the attached surface

ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
if (FAILED(lpddsPrimary->GetAttachedSurface(&ddsd.ddsCaps, &lpddsBack)))
{
	MessageBox(hwnd, "Couldn`t create attached surface", "Error", MB_OK);
	return(FALSE);
}

// create offscreen buffer

if (FAILED(lpdd7->CreateSurface(&ddsd2, &lpddsOffscreen, NULL)))
{
	MessageBox(hwnd, "Couldn`t create offscreen surface", "Error", MB_OK);
	return(FALSE);
}

ShowCursor(FALSE);
 
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    
	MessageBox(hwnd, "hhhhhhhhhhhhhhhh", "Error", MB_OK);
	int LoadBitmapResource(LPDIRECTDRAWSURFACE7 lpddsPrimary, int xDest, int yDest, int nResID);
	MessageBox(hwnd, "aaaaaaaaaaaaaaaa", "Error", MB_OK);

	while(TRUE)
	{
		// check the message queue

		if (PeekMessage(&Msg,NULL,0,0,PM_REMOVE))
		{
			if (Msg.message == WM_QUIT) // exit loop on quit

				break;

			// send the message on its merry way

			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}
	}

    return Msg.wParam;
	
	if (lpddsOffscreen)
	{
		// kill the primary surface!

		lpddsOffscreen->Release();
		lpddsOffscreen = NULL;
	}


	if (lpddsBack)
	{
		// kill the primary surface!

		lpddsBack->Release();
		lpddsBack = NULL;
	}


	if (lpddsPrimary)
	{
		// kill the primary surface!

		lpddsPrimary->Release();
		lpddsPrimary = NULL;
	}

	if (lpdd7)
	{
		// blow away DirectDraw!

		lpdd7->Release();
		lpdd7 = NULL;
	}

	CoUninitialize();

}



int LoadBitmapResource(LPDIRECTDRAWSURFACE7 lpddsPrimary, int xDest, int yDest, int nResID)
{
  HDC hSrcDC;           // source DC - memory device context

  HDC hDestDC;          // destination DC - surface device context

  HBITMAP hbitmap;      // handle to the bitmap resource

  BITMAP bmp;           // structure for bitmap info

  int nHeight, nWidth;  // bitmap dimensions


  // first load the bitmap resource

  if ((hbitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(nResID),
                                    IMAGE_BITMAP, 0, 0,
                                    LR_CREATEDIBSECTION)) == NULL)
  {
	MessageBox(hwnd, "Couldn`t Load Image", "Error", MB_OK);
	return(FALSE);
  }

  // create a DC for the bitmap to use

  if ((hSrcDC = CreateCompatibleDC(NULL)) == NULL)
  {
	MessageBox(hwnd, "Couldn`t create a DC for the bitmap to use", "Error", MB_OK);
    return(FALSE);
  }

  // select the bitmap into the DC

  if (SelectObject(hSrcDC, hbitmap) == NULL)
  {
	MessageBox(hwnd, "Couldn`t select the bitmap into the DC", "Error", MB_OK);
    DeleteDC(hSrcDC);
    return(FALSE);
  }

  // get image dimensions

  if (GetObject(hbitmap, sizeof(BITMAP), &bmp) == 0)
  {
	MessageBox(hwnd, "Couldn`t get image dimensions", "Error", MB_OK);
    DeleteDC(hSrcDC);
    return(FALSE);
  }

  nWidth = bmp.bmWidth;
  nHeight = bmp.bmHeight;
  xDest = 50;
  yDest = 50;

  // retrieve surface DC

  if (FAILED(lpddsPrimary->GetDC(&hDestDC)))
  {
	MessageBox(hwnd, "Couldn`t retrieve surface DC", "Error", MB_OK);
    DeleteDC(hSrcDC);
    return(FALSE);
  }

  // copy image from one DC to the other

  if (BitBlt(hDestDC, xDest, yDest, nWidth, nHeight, hSrcDC, 0, 0,
             SRCCOPY) == NULL)
  {
	MessageBox(hwnd, "Couldn`t copy image DC too surface DC", "Error", MB_OK);
    lpddsPrimary->ReleaseDC(hDestDC);
    DeleteDC(hSrcDC);
    return(FALSE);
  }

  // kill the device contexts

  lpddsPrimary->ReleaseDC(hDestDC);
  DeleteDC(hSrcDC);

  // return success

  return(TRUE);
}

  
or why does this pixel on working when I have a MessageBox befor it but It doesnt work when I doesnt have it in front of the funktion!
  
#include <windows.h>
#include <windowsx.h>
#include <ddraw.h>

#pragma comment(lib,"winmm.lib")
#pragma comment(lib,"dxguid.lib")    

#define RGB_16BIT565(r, g, b)  ((r << 11) | (g << 5) | (b))
#define RGB_15BIT555(r, g, b)  ((r << 10) | (g << 5) | (b))


#define INIT_DXSTRUCT(dxstruct) { ZeroMemory(&dxstruct, sizeof(dxstruct)); dxstruct.dwSize = sizeof(dxstruct); }

inline void PlotPixel16(int x, int y, USHORT color, USHORT* buffer, int nPitch);

const char g_szClassName[] = "myWindowClass";

LPDIRECTDRAW7 lpdd7 = NULL;
LPDIRECTDRAWSURFACE7 lpddsPrimary = NULL;  // primary surface

LPDIRECTDRAWSURFACE7 lpddsBack = NULL;  // back buffer

LPDIRECTDRAWSURFACE7 lpddsOffscreen = NULL;  // offscreen buffer


DDPIXELFORMAT ddpf;

DDSURFACEDESC2 ddsd, ddsd2;

USHORT nPitch, *buffer;
USHORT white_pixel_565 = RGB_16BIT565(1, 1, 1);
USHORT white_pixel_555 = RGB_15BIT555(1, 1, 1);

int hej;
long unsigned LastTime;

// Step 4: the Window Procedure

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
		/*case WM_LBUTTONDOWN:
			PostQuitMessage(0);
*/
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{

    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window

    hwnd = CreateWindowEx(
        0,
        g_szClassName,
        "The title of my window",
        WS_POPUP | WS_VISIBLE,
        0, 0, 640, 480,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

// initialize COM

if (FAILED(CoInitialize(NULL)))
{
	MessageBox(hwnd, "Couldn`t initialize COM", "Error", MB_OK);
	return(FALSE);
}

// create DirectDraw object

if (FAILED(CoCreateInstance(CLSID_DirectDraw, NULL, CLSCTX_ALL,
							IID_IDirectDraw7, (void**)&lpdd7)))
{
	MessageBox(hwnd, "Couldn`t create DirectDraw object", "Error", MB_OK);
	return(FALSE);
}

// initialize DirectDraw object

if (FAILED(lpdd7->Initialize(NULL)))
{
	MessageBox(hwnd, "Couldn`t initialize DirectDraw object", "Error", MB_OK);
	return(FALSE);
}

//Set cooperative level

if (FAILED(lpdd7->SetCooperativeLevel(hwnd, DDSCL_ALLOWREBOOT | 
	DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN)))
{
	MessageBox(hwnd, "Couldn`t set cooperative level", "Error", MB_OK);
	return(FALSE);
}

//Set display mode to 640x480/16

if(FAILED(lpdd7->SetDisplayMode(640, 480, 16, 0, 0)))
{
	MessageBox(hwnd, "Couldn`t set display mode", "Error", MB_OK);
	return(FALSE);
}

// set up primary drawing surface

INIT_DXSTRUCT(ddsd);                              // initialize ddsd

ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;  // valid flags

ddsd.dwBackBufferCount = 1;                       // one back buffer

ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |    // primary surface

                      DDSCAPS_COMPLEX |           // back buffer is chained

                      DDSCAPS_FLIP |              // allow page flipping

                      DDSCAPS_VIDEOMEMORY;        // create in video memory

        


// set up offscreen surface

INIT_DXSTRUCT(ddsd2);                                  // initialize ddsd

ddsd2.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;  // valid flags

ddsd2.dwWidth = 400;                                   // set width

ddsd2.dwHeight = 300;                                  // set height

ddsd2.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN |        // offscreen buffer

                      DDSCAPS_VIDEOMEMORY;            // video memory



//create primary surface

if(FAILED(lpdd7->CreateSurface(&ddsd, &lpddsPrimary, NULL)))
{
	MessageBox(hwnd, "Couldn`t create primary surface", "Error", MB_OK);
	return(FALSE);
}

// get the attached surface

ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
if (FAILED(lpddsPrimary->GetAttachedSurface(&ddsd.ddsCaps, &lpddsBack)))
{
	MessageBox(hwnd, "Couldn`t create attached surface", "Error", MB_OK);
	return(FALSE);
}

// create offscreen buffer

if (FAILED(lpdd7->CreateSurface(&ddsd2, &lpddsOffscreen, NULL)))
{
	MessageBox(hwnd, "Couldn`t create offscreen surface", "Error", MB_OK);
	return(FALSE);
}

//Lock surface

if (FAILED(lpddsPrimary->Lock(NULL, &ddsd, DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR, NULL)))
{
	MessageBox(hwnd, "Couldn`t lock primary surface", "Error", MB_OK);
	return(FALSE);
}
lpddsPrimary->Unlock(NULL);


// now convert the pointer and the pitch

USHORT* buffer = (USHORT*)ddsd.lpSurface;
USHORT nPitch = ddsd.lPitch/2;

ShowCursor(FALSE);

MessageBox(hwnd, "hej", "hej", MB_OK);
lpddsPrimary->Lock(NULL, &ddsd, DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR, NULL);
PlotPixel16(200, 200, white_pixel_555, buffer, nPitch);
PlotPixel16(201, 200, white_pixel_555, buffer, nPitch);
PlotPixel16(200, 201, white_pixel_555, buffer, nPitch);
PlotPixel16(201, 201, white_pixel_555, buffer, nPitch);

lpddsPrimary->Unlock(NULL);

 
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

	while(TRUE)
	{
		// check the message queue

		if (PeekMessage(&Msg,NULL,0,0,PM_REMOVE))
		{
			if (Msg.message == WM_QUIT) // exit loop on quit

				break;

			// send the message on its merry way

			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}
	}

    return Msg.wParam;


	if (lpddsOffscreen)
	{
		// kill the primary surface!

		lpddsOffscreen->Release();
		lpddsOffscreen = NULL;
	}


	if (lpddsBack)
	{
		// kill the primary surface!

		lpddsBack->Release();
		lpddsBack = NULL;
	}


	if (lpddsPrimary)
	{
		// kill the primary surface!

		lpddsPrimary->Release();
		lpddsPrimary = NULL;
	}

	if (lpdd7)
	{
		// blow away DirectDraw!

		lpdd7->Release();
		lpdd7 = NULL;
	}

	CoUninitialize();

}

inline void PlotPixel16(int x, int y, USHORT color, USHORT* buffer, int nPitch)
{
	buffer[y*nPitch + x] = color;
}
  
Mullvad
Please I really need you help now guys!!!
This is very irritating!!!!

Mullvad
Mullvad
Advertisement
What error(s) does it give?

------------------------------
Omnipotent_Q
"Poor people are crazy. I''m eccentric."
------------------------------Omnipotent_Q"Poor people are crazy. I'm eccentric."
The errors are pointing at the Bitmap funktion and saying I aint have enough ) and unexpected end of file!!

Mullvad
Mullvad
If you don't know where you're missing your }, then try starting over and building the program one step at a time. First, all it does is create a ddraw interface, then have it create a primary surface, then have it lock the surface.

BTW... if you don't have MessageBox in your if statement, and you don't have any brackets, you're going to have a problem. The best solution would be to leave the message boxes. If you really don't want them, then replace them with something else, or take out the if statements.

[edited by - Forcas on April 4, 2002 2:46:06 PM]
-Forcaswriteln("Does this actually work?");
But both this error points at the LoadBitmapResource(); funktion whys that??

Mullvad
Mullvad
Advertisement
int LoadBitmapResource(LPDIRECTDRAWSURFACE7 lpddsPrimary, int xDest, int yDest, int nResID);

this line should be at the beginning of your cpp file or in the header file (called prototyping) and the definition of this function should be somethere within your program with all code. When you call this function you do not include the varible types.

use:

LoadBitmapResource(lpddsPrimary,xDest,yDest,nResID);


Also when you use the return command in a function it returns command to the parent (caller) of that function, so anything after a return command will not be invoked.

return Msg.wParam;

if (lpddsOffscreen)
{
.........etc

The if statement will never be invoked because of the return in front of it.

,Jay
Thanks for the last time you helped me Jay thanks for the mail and thanks for this time you are the best!!

Mullvad
Mullvad

This topic is closed to new replies.

Advertisement