Problem blitting on DirectDraw7

Started by
2 comments, last by Cef 20 years, 9 months ago
Hi, If anyone could help me with this problem... Im learning from the book TOTWGPG 2ED and on the happy face clipping example, my screen looks very unsable, like "flashing" in color black. I''ve not found my error, and don''t have the source CD, so I don''t know if this is because of my code or because of my Graphics card. I''d really appreciate any help...
Advertisement
I don't have that book so I'm not sure what example you're talking about, maybe you can post your blitting code?

Also, make sure to clear the backbuffer each frame to your background color (most often black) to get rid of video memory artifacts.

[edited by - BitBlt on July 8, 2003 12:39:02 PM]
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define BIT_DEPTH 8
#include <windows.h>
#include <ddraw.h>
#include <ddrawex.h>
#include <mmsystem.h>
#define WIN32_LEAN_AND_MEAN
#include "T3D.h"


bool window_closed = 0;
HWND g_hWnd = NULL;
LPDIRECTDRAW7 lpDD7 = NULL;
PALETTEENTRY palette[256];
LPDIRECTDRAWPALETTE lpDDpal = NULL;
LPDIRECTDRAWSURFACE7 lpDDSPrimary = NULL, lpDDSBack = NULL;
DDSURFACEDESC2 DDsd;
POINT happy_faces[100];
POINT happy_facesv[100];


int GameInit()
{
DDPIXELFORMAT ddpixel;
if(FAILED(DirectDrawCreateEx(NULL, (void **) &lpDD7, IID_IDirectDraw7, NULL)))
OutputDebugString("Could not initialize COM object \n");
if(FAILED(lpDD7->SetCooperativeLevel(g_hWnd,DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWMODEX | DDSCL_ALLOWREBOOT)))
OutputDebugString("Could not set Coop Level \n");
if(FAILED(lpDD7->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,BIT_DEPTH,0,0)))
OutputDebugString("Could not set Display Mode\n");

memset(&DDsd, 0, sizeof(DDsd));

DDsd.dwSize = sizeof(DDSURFACEDESC2);
DDsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
DDsd.dwBackBufferCount = 1;

DDsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX |DDSCAPS_FLIP;

if(FAILED(lpDD7->CreateSurface(&DDsd, &lpDDSPrimary, NULL)))
OutputDebugString("Could not create primary surface\n");

DDsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;

if(FAILED(lpDDSPrimary->GetAttachedSurface(&DDsd.ddsCaps, &lpDDSBack)))
OutputDebugString("Could not attach secondary surface\n");

memset(&ddpixel, 0, sizeof(ddpixel));
ddpixel.dwSize = sizeof(DDPIXELFORMAT);

if(FAILED(lpDDSPrimary->GetPixelFormat(&ddpixel)))
OutputDebugString("Could not get pixel format\n");

if(ddpixel.dwFlags & DDPF_RGB)
switch(ddpixel.dwRGBBitCount)
{
case 15:
{ OutputDebugString("Pixel format 555");
}break;
case 16:
{ OutputDebugString("Pixel format 565");
}break;
case 32:
{ OutputDebugString("Pixel format 32");
}break;
default: break;
}
//else if(ddpixel.dwFlags & DDPF_PALETTEINDEXED8)
//{
OutputDebugString("Pixel format 256");
PALETTEENTRY palette[256];
LPDIRECTDRAWPALETTE lpddpal = NULL;
for (int color=1; color<255; color++)
{
palette.peRed = rand()%256;<br> palette.peGreen = rand()%256;<br> palette.peBlue = rand()%256;<br> palette.peFlags = PC_NOCOLLAPSE;<br> }<br> <br> palette[0].peRed = 0;<br> palette[0].peGreen = 0;<br> palette[0].peBlue = 0;<br> palette[0].peFlags = PC_NOCOLLAPSE;<br><br> palette[255].peRed = 255;<br> palette[255].peGreen = 255;<br> palette[255].peBlue = 255;<br> palette[255].peFlags = PC_NOCOLLAPSE;<br><br> palette[1].peRed = 255;<br> palette[1].peGreen = 255;<br> palette[1].peBlue = 0;<br> palette[1].peFlags = PC_NOCOLLAPSE;<br> <br> if(FAILED(lpDD7->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256 | DDPCAPS_INITIALIZE, palette, &lpddpal, NULL)))<br> OutputDebugString("Could not create palette");<br> if(FAILED(lpDDSPrimary->SetPalette(lpddpal)))<br> OutputDebugString("Could not set palette");<br> <br> <br> //}<br><br> for(int face = 0; face < 100; face++)<br> {<br> happy_faces[face].x = rand()%SCREEN_WIDTH;<br> happy_faces[face].y = rand()%SCREEN_HEIGHT;<br> happy_facesv[face].x = rand()%4*(-rand()%2);<br> happy_facesv[face].y = rand()%4*(-rand()%2);<br> }<br> <br> return 1;<br>}<br><br>int GameMain()<br>{<br> <br> UCHAR happy_bitmap[64] = {0,0,0,0,0,0,0,0,<br> 0,0,1,1,1,1,0,0,<br> 0,1,0,1,1,0,1,0,<br> 0,1,1,1,1,1,1,0,<br> 0,1,0,1,1,0,1,0,<br> 0,1,1,0,0,1,1,0,<br> 0,0,1,1,1,1,0,0,<br> 0,0,0,0,0,0,0,0};<br> <br> DDBLTFX ddBltFX;<br> //RECT dest_rect;<br> //UCHAR *DoubleBuffer = (UCHAR *)malloc(SCREEN_WIDTH*SCREEN_HEIGHT);<br> UCHAR *BackBuffer = NULL;<br><br> if(window_closed)<br> return 0;<br><br> if(KEYDOWN(VK_ESCAPE))<br> {<br> PostMessage(g_hWnd,WM_CLOSE, 0,0);<br> window_closed = 1;<br> }<br> <br> //memset((void *)DoubleBuffer,0,SCREEN_WIDTH*SCREEN_HEIGHT);<br><br> <br><br> DDRAW_INIT_STRUCT(ddBltFX);<br> ddBltFX.dwFillColor = 0;<br> if(FAILED(lpDDSPrimary->Blt(NULL, NULL, NULL, DDBLT_COLORFILL , &ddBltFX)))<br> {<br> OutputDebugString("Could not blit\n");<br> <br> return 0;<br> }<br><br> //BackBuffer = (UCHAR *)DDsd.lpSurface;<br><br> <br><br><br> DDRAW_INIT_STRUCT(DDsd);<br> DDsd.dwSize = sizeof(DDsd);<br><br> if(FAILED(lpDDSBack->Lock(NULL,&DDsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL)))<br> OutputDebugString("Could not lock surface\n");<br> <br> <br> for(int face = 0; face < 100; face++)<br> Blit_Clipped(happy_faces[face].x, happy_faces[face].y, 8,8, happy_bitmap, (UCHAR *) DDsd.lpSurface, DDsd.lPitch);<br> <br> for(face = 0; face < 100; face++)<br> {<br> happy_faces[face].x += happy_facesv[face].x;<br> happy_faces[face].y += happy_facesv[face].y;<br><br> if(happy_faces[face].x > SCREEN_WIDTH)<br> happy_faces[face].x = -8;<br> if(happy_faces[face].x < 0)<br> happy_faces[face].x = SCREEN_WIDTH;<br> if(happy_faces[face].y > SCREEN_HEIGHT)<br> happy_faces[face].y = -8;<br> if(happy_faces[face].y < -8)<br> happy_faces[face].y = SCREEN_HEIGHT;<br> }<br> <br> if(FAILED(lpDDSBack->Unlock(NULL)))<br> OutputDebugString("Could not unlock surface");<br> <br> <br>while(FAILED(lpDDSPrimary->Flip(NULL, /*DDFLIP_WAIT*/0)));<br><br> <br> return 1;<br>}<br><br>int GameShutdown()<br>{ <br><br> if(lpDDpal){<br> if(FAILED(lpDDpal->Release()))<br> OutputDebugString("Could not realease Palette object\n");<br> lpDDpal = NULL;<br> }<br> if(lpDDSBack){<br> if(FAILED(lpDDSBack->Release()))<br> OutputDebugString("Could not realease Back Surface object\n");<br> lpDDSBack = NULL;<br> }<br> if(lpDDSPrimary){<br> if(FAILED(lpDDSPrimary->Release()))<br> OutputDebugString("Could not realease Surface object\n");<br> lpDDSPrimary = NULL;<br> }<br> if(lpDD7){<br> if(FAILED(lpDD7->Release()))<br> OutputDebugString("Could not realease COM object\n");<br> lpDD7 = NULL;<br> }<br> <br> return 1;<br>}<br><br>LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wparam, LPARAM lparam)<br>{<br> PAINTSTRUCT ps;<br> HDC hDC;<br><br> switch(msg)<br> {<br> case WM_CREATE:<br> {<br> return(0);<br> }<br> break;<br> case WM_PAINT:<br> {<br> hDC = BeginPaint(hWnd, &ps);<br> EndPaint(hWnd, &ps);<br> return(0);<br> }<br> break;<br> case WM_DESTROY:<br> {<br> PostQuitMessage(20);<br> return(0);<br> }<br> break;<br> <br> default: break;<br> }<br>return(DefWindowProc(hWnd, msg, wparam, lparam));<br>}<br><br><br><br>int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)<br>{<br> MSG msg;<br> WNDCLASSEX wc;<br> <br><br> wc.cbClsExtra = NULL;<br> wc.cbSize = sizeof(WNDCLASSEX);<br> wc.cbWndExtra = NULL;<br> wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);<br> wc.hCursor = LoadCursor(NULL, IDC_ARROW);<br> wc.hIcon = LoadIcon(hInstance, "IDI_BART");<br> wc.hIconSm = LoadIcon(hInstance,"IDI_BART");<br> wc.hInstance = hInstance;<br> wc.lpfnWndProc = WndProc;<br> wc.lpszClassName = "WindowClass";<br> wc.lpszMenuName = "Menuppal";<br> wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;<br><br> RegisterClassEx(&wc);<br><br> g_hWnd = CreateWindowEx( NULL, "WindowClass", "Parent Guindow", WS_POPUP | WS_VISIBLE, 0,CW_USEDEFAULT,640,480,NULL,NULL,hInstance,NULL);<br><br> UpdateWindow(g_hWnd);<br> <br> GameInit();<br><br> while(TRUE)<br> {<br> if(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))<br> {<br> if(msg.message==WM_QUIT)<br> {<br> break;<br> }<br> TranslateMessage(&msg);<br> DispatchMessage(&msg);<br> }<br> GameMain();<br> }<br> GameShutdown();<br> return(msg.wParam);<br>}<br>
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code)&0x8000000) ? 1:0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code)&0x8000000) ? 0:1)

#define _RGB16BIT555(r,g,b) ((b&31)+((g&31)<<5)+((r&31)<<10))
#define _RGB16BIT565(r,g,b) ((b&31)+((g&63)<<5)+((r&31)<<11))
#define _RGB32BIT(a,r,g,b) ((b)+ ((g)<<8)+((r)<<16)+((a)<<24))

#define DDRAW_INIT_STRUCT(ddstruct) {memset(&ddstruct,0,sizeof(ddstruct)); ddstruct.dwSize=sizeof(ddstruct); }

//typedef struct _POINT {
// int x;
// int y;
//} POINT;

inline void PlotPixel8(int x, int y, UCHAR index, UCHAR *video_buffer, int lpitch)
{
video_buffer[x+y*lpitch] = index;
}

inline void PlotPixel555(int x, int y,int red, int green, int blue, USHORT *video_buffer, int lpitch)
{
USHORT pixel = _RGB16BIT555(red,green,blue);
video_buffer[x+y*lpitch] = pixel;
}

inline void PlotPixel565(int x, int y,int red, int green, int blue, USHORT *video_buffer, int lpitch)
{
USHORT pixel = _RGB16BIT565(red,green,blue);
video_buffer[x+y*lpitch] = pixel;
}

inline void PlotPixel32(int x, int y, int red, int green, int blue, int alpha, UINT *video_buffer, int lpitch)
{
UINT pixel = _RGB32BIT(alpha, red, green, blue);
video_buffer[x+y*lpitch] = pixel;
}

//En cada modo de video cambia pitch, cambia video_buffer, cambia RGB

void Blit_Clipped(int x, int y, int width, int height, UCHAR *bitmap, UCHAR *video_buffer, int mempitch)
{
if((x>=SCREEN_WIDTH) || (y>= SCREEN_HEIGHT) || (x+width<=0) || (y+height<=0))
return;

int x1 = x;
int y1 = y;
int x2 = x+width-1;
int y2 = y+height-1;

if(x1<0)
x1=0;
if(y1<0)
y1=0;

if(x2>=SCREEN_WIDTH)
x2=SCREEN_WIDTH-1;
if(y2>=SCREEN_HEIGHT)
y2=SCREEN_HEIGHT-1;

int xoff = x1-x;
int yoff = y1-y;

int dx = x2-x1+1;
int dy = y2-y1+1;

video_buffer += (x1 + y1*mempitch);

bitmap += (xoff + yoff*width);

UCHAR pixel;

for(int index_y = 0; index_y < dy; index_y++)
{
for(int index_x = 0; index_x < dx; index_x++)
{
if(pixel = bitmap[index_x])
video_buffer[index_x] = pixel;
}
video_buffer += mempitch;
bitmap += width;
}
}

This topic is closed to new replies.

Advertisement