Advertisement

Drawing lines

Started by February 01, 2000 03:58 AM
1 comment, last by Digger 24 years, 10 months ago
Last day I made a quick hack to draw text and lines with DirectDraw as I have started to work on a new game. I use the GDI function, I think it is DrawLine or something. It works for a while, but then it doesn't work to draw lines anymore. The whole thing starts when I'm moving the mouse or pressing any keys for a while. It seams to be something with the messageloop but I can't track it. I'm using the function every frame, because I erase the backbuffer every frame. I'm drawing text with TextOut the same way, but that works perfect. What am I doing wrong???????? Edited by - Digger on 2/1/00 7:58:30 AM
It''s hard to tell without actually looking at the code, but here''s a couple of possible problems that can crop up using GDI:

- You''re creating a compatible device context but never deleting it
- You''re creating an HBRUSH, HBITMAP, HICON, or something similar in your drawing function but never deselecting and deleting it

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Advertisement
//***********************//** Normal line://***********************void Prim::Line(int x0, int y0, int x1, int y1){	// Theory: add Dy / Dx to y after every loop	double dy = y1 - y0;	double dx = x1 - x0;	double m;	if(dy < dx)	{		m = dy / dx;		double y = y0;		for(int x = x0; x <= x1; x++)		{			Dest[(int)(((int)y * Pitch) + (x*3))] = (unsigned char) 255;			y += m;		}	}	else	{		double x = x0;		m = dx / dy;		for(int y = y0; y <= y1; y++)		{			Dest[(int)((y * Pitch) + ((int)x * 3))] = (unsigned char) 255;			x += m;		}	}}//*********************//** End normal line//*********************//*********************//** Midpoint algorithm//*********************void Prim::MidPointLine(int x0, int y0, int x1, int y1){	int x, y;	int dx = x1 - x0;	if(dx > 0)	{		x = x0;	}	else	{		dx = -dx;		x = x0;	}	int dy = y1 - y0;	//int x = x0;	y = y0;	int d, IncrE, IncrNE;	Dest[(y * Pitch) + (x*3)] = 255;	if(dy < dx)	{		d =  2 * dy - dx;	    IncrE = 2 * dy;	    IncrNE = 2 * (dy - dx);			while(x < x1)		{			if(d <= 0)			{				d+= IncrE;				x++;			}			else			{				d+=IncrNE;				x++;				y++;			}			Dest[(y * Pitch) + (x*3)] = 255;		}			}	else	{		d =  2 * dx - dy;	    IncrE = 2 * dx;	    IncrNE = 2 * (dx - dy);			while(y < y1)		{			if(d <= 0)			{				d+= IncrE;				y++;			}			else			{				d+=IncrNE;				x++;				y++;			}			Dest[(y * Pitch) + (x*3)] = 255;		}	}}//***********************//** End Midpoint algorithme//***********************//***********************//** Bresenham''s algorithme//***********************void Prim::BressenhamLine(int x0, int y0, int x1, int y1){	int XUnit, YUnit, Offset = y0 * Pitch + (x0*3);	int dy = y1 - y0;	if(dy < 0)	{		dy = -dy;		YUnit = -Pitch;	}	else		YUnit = Pitch;	int dx = x1 - x0;	if(dx < 0)	{		dx = -dx;		XUnit = -3;	}	else XUnit = 3;	int ErrorTerm = 0;	if(dx > dy)	{		for(int i = 0; i <= dx; i++)		{			Dest[Offset] = 255;			Dest[Offset+1] = 0;			Dest[Offset+2] = 0;			Offset += XUnit;			ErrorTerm += dy;			while(ErrorTerm > dx)			{				ErrorTerm -= dx;				Offset += YUnit;			}		}	}	else	{		for(int i = 0; i <= dy; i++)		{			Dest[Offset] = 255;			Dest[Offset+1] = 0;			Dest[Offset+2] = 0;			Offset += YUnit;			ErrorTerm += dx;			while(ErrorTerm > 0)			{				ErrorTerm -= dy;				Offset += XUnit;			}		}	}}//************************//** End Bresenham''s algorithm//************************[/CODE}I know it''s bressenhma, but wrote this code some time ago, and can''t seem to find the time to change it with some enhancements I came up with......    


Dance with me......

http://members.xoom.com/CJdeVos/index.htm

This topic is closed to new replies.

Advertisement