Sign in to follow this  

I'm trying to plot a pixel.... but it's not working out.

This topic is 4809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to get a pixel plotted on the screen in a 320x240x8 resolution. Problem is, it's not showing up. I think I'm getting somewhere because the program's actually stayed up a few times. But, here's my code. Hopefully someone can tell me what I'm doing wrong, and hopefully it'll be a silly overlooked problem too. :) ----------------------------------------------------------------
#include <stdio.h>
#include <ddraw.h>
#define WINDOW_CLASS_NAME "WindowClass"

LRESULT CALLBACK WinProc(HWND hwnd, unsigned int msg, WPARAM wparam, LPARAM lparam)
{
	switch(msg)
	{
	case WM_DESTROY:
		{
			PostQuitMessage(0);
			return(0);
		}
	}
	return(DefWindowProc(hwnd, msg, wparam, lparam));
}



int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, LPSTR lpcmd, int nshowcmd)
{
	WNDCLASS wndclass;
	HWND hwnd;
	MSG msg;
	LPDIRECTDRAW7 lpdd;
	LPDIRECTDRAWSURFACE7 lpdds;
	DDSURFACEDESC2 ddsd;
	unsigned char* video_buffer = NULL;

	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hInstance = hinstance;
	wndclass.lpfnWndProc = WinProc;
	wndclass.lpszClassName = WINDOW_CLASS_NAME;
	wndclass.lpszMenuName = NULL;
	wndclass.style = CS_HREDRAW || CS_VREDRAW || CS_OWNDC;

	RegisterClass(&wndclass);
	hwnd = CreateWindow(WINDOW_CLASS_NAME, "Window", WS_POPUP | WS_VISIBLE, 0, 0, 320, 240, NULL, NULL, hinstance, NULL);

	DirectDrawCreateEx(NULL, (void**)&lpdd, IID_IDirectDraw7, NULL);
	lpdd->SetCooperativeLevel(hwnd, DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT);
	lpdd->SetDisplayMode(320, 240, 8, 0, 0);
	memset(&ddsd, 0, sizeof(ddsd));
	ddsd.dwSize = sizeof(ddsd);
	ddsd.dwFlags = DDSD_CAPS;
	ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
	lpdd->CreateSurface(&ddsd, &lpdds, NULL);

	lpdds->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL);

	video_buffer = (unsigned char*)ddsd.lpSurface;

	video_buffer[5*2+ddsd.lPitch*5] = 60;

	lpdds->Unlock(NULL);
	
	

	while(1)
	{
		if(PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE)!=NULL)
		{
			if(msg.message==WM_QUIT)
			{
				PostQuitMessage(0);
				break;
			}
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return(0);
		
}
----------------------------------------------------------------

Share this post


Link to post
Share on other sites
I'll hazard a guess here, having never done
this in directx...

Shouldn't you set the pixel format for the *surface*
to 8 bits? Right now you're just setting the screen
resolution to 320x240x8, which may have no bearing on the surface.

You may also want to set the surface width and height to 320x240,
instead of leaving them zeroed.

Share this post


Link to post
Share on other sites
To jog my memory* about ModeX I looked at the docs: http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/ddraw7/directdraw7/ddover_82bb.asp?frame=true

Quote:
Windows 95 and Windows NT/Windows 2000 do not natively support Mode X modes; therefore, when your application is in a Mode X mode, you cannot use the IDirectDrawSurface7::Lock or IDirectDrawSurface7::Blt methods to lock or blit to the primary surface.



So... if you're using Windows XP / Windows 2000 / Windows NT (the docs were written before XP was released, but it's based on the NT kernel and follows that lineage) or Windows 95 (not sure about 98 and ME - it could be a doc bug though), then although you're able to SetDisplayMode() to ModeX, you won't be able to Lock() the primary surface to write to it whilst still being on the desktop.

The Lock() call still succeeds, it's just the memory you're writing doesn't seem to be the same as the ModeX display is pointing to.

ModeX was a bit of an ugly stepchild from the days when people were porting legacy DOS apps to Windows anyway...


[*: do you *really* require ModeX ?!]

Share this post


Link to post
Share on other sites
Quote:
Original post by Rockuman
But, I went ahead and changed the resolution to 640x480x16, and I got the pixel down just fine. Thanks. :)


Yep - because when you select a 16-bit display mode, the ModeX flag is ignored and you should get a normal 16-bit RGB surface. (ModeX is 8-bit, designed to emulate DOS Mode 13).

Share this post


Link to post
Share on other sites

This topic is 4809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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