• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

This topic is now archived and is closed to further replies.

joelmartinez

C# Bresenham algorithm

4 posts in this topic

Hey all, this is gonna be my first post ... I''ve been lurking here for a bit now and have started experimenting with some basic game/graphics programming. I''ll start off by saying that I specifically want to use GDI+ as opposed to MDX because I want to learn the concepts behind it all first before I move on to a powerhouse platform like MDX or C++ or whatever. Now ... I''ve implemented (ok, copied ;-) ) the C function found here [http://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html] in C# ... only problem is, that it''s rather slow. I''ve got a demo that draws a line between a point, and the location of the mouse. Once it gets longer than about 100 pixels, it starts to visibly lag behind the mouse as it redraws each frame.
void Line(Graphics g, int x1, int y1, int x2, int y2, Pen pen)
{
	int dx = x2 - x1;
	int dy = y2 - y1;
	
	int y = y1;
	int eps = 0;
	Bitmap bmp = new Bitmap(1,1,g);
	bmp.SetPixel(0,0,Color.Black);
			
	for (int x=x1; x<=x2;x++)
	{
		g.DrawImage(bmp,x,y);
				
		eps+=dy;
		if ((eps*2) &rts= dx)
		{
			y++;
			eps-=dx;
		}
	}
				
}
 
The only thing I can guess is causing it to be slow is the use of a Bitmap object to draw the pixel as there''s no way to set an individual pixel using only the Graphics object. I was wondering if anyone else here had enough C#/GDI+ experience to see if there was a way to speed this up. Thanks in advance Joel Martinez http://www.codecube.net/
0

Share this post


Link to post
Share on other sites
That is because you are sort of abusing GDI+ It''s not made for lots of direct pixel access. If you want that, DirectX will be better. If you just want to draw a line, GDI+ has functions for that already...and they are fast.
0

Share this post


Link to post
Share on other sites
Right ... I totally understand that ... but I didn''t want to use the built in line function (you''re right, it is very fast) because I want to understand the underlying concept behind it all before I move on to something like DirectX.

And as far as using GDI+ ... I would assume that a 2ghz machine with 512 megs of ram using GDI+ would outperform a nintendo, and I don''t think drawing a line is beyond nintendo performance. I could be wrong though ;-)

Joel Martinez
http://www.codecube.net/
0

Share this post


Link to post
Share on other sites
Every time you call bmp.SetPixel, you aren''t just doing direct pixel access.

You are first locking the entire surface in memory. SetPixel doesn''t just lock the 1 pixel rectangle you are working on.

Then you are modifying the pixel.

Finally, you are unlocking the surface.

The lock/unlock is what is taking all of the time.

If you want to do this fast, either do it in a backbuffer then blit it to the screen, or lock the buffer once, then use direct memory access to change the pixels, then unlock the buffer.

RomSteady - Test Locally, Test Globally, Test Early, Test Often
0

Share this post


Link to post
Share on other sites
Yeah the constant bitmap locking is killing you, as RomSteady has said.

I''d suggest you create a Bitmap equal to the size of the Graphics''s surface area and then use the Bitmap::LockBits() method to get a handle on the raw bitmap data buffer (see the GDI+ documentation for Bitmap::LockBits and BitmapData class for exact usage information), plot the pixels into that buffer, unlock and THEN DrawImage from the Bitmap to the Graphics object. If done correctly this should be _significantly_ faster than what you are doing now. Doing it this way requires an understanding of pixel formats and surface pitch vs surface width, so if you''re not already familiar with these concepts you may have some reading up to do, but the GDI+ documentation and the examples it includes should be enough to get you going.


0

Share this post


Link to post
Share on other sites