• 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.
Sign in to follow this  
Followers 0
derek_of_bodom@hotmail.com

Line drawing? (C#)

7 posts in this topic

I'm working on a 2D game, and I need to make a map editor. I've made map editors in the past, but there has always been a problem. When you're drawing, if you swipe the mouse across the screen, it will skip large chunks of space. So what I want to know how to do, essentially, is draw a line.

I want to be able to take 2 points and fill every point that touches the line between those two points. Once I know how to do that, I can implement it in my map editor. Does anyone know any good resources that tell how to do this, or maybe some sample code?
0

Share this post


Link to post
Share on other sites
[quote name='Nyxenon' timestamp='1327355592' post='4905581']
I'm working on a 2D game, and I need to make a map editor. I've made map editors in the past, but there has always been a problem. When you're drawing, if you swipe the mouse across the screen, it will skip large chunks of space. So what I want to know how to do, essentially, is draw a line.

I want to be able to take 2 points and fill every point that touches the line between those two points. Once I know how to do that, I can implement it in my map editor. Does anyone know any good resources that tell how to do this, or maybe some sample code?
[/quote]

Most 2D APIs will have functions to draw lines given two points, what API are you using for the editor ? (For normal C# forms you can use the DrawLine method in System.Drawing.Graphics)
0

Share this post


Link to post
Share on other sites
[quote name='SimonForsman' timestamp='1327356535' post='4905585']
[quote name='Nyxenon' timestamp='1327355592' post='4905581']
I'm working on a 2D game, and I need to make a map editor. I've made map editors in the past, but there has always been a problem. When you're drawing, if you swipe the mouse across the screen, it will skip large chunks of space. So what I want to know how to do, essentially, is draw a line.

I want to be able to take 2 points and fill every point that touches the line between those two points. Once I know how to do that, I can implement it in my map editor. Does anyone know any good resources that tell how to do this, or maybe some sample code?
[/quote]

Most 2D APIs will have functions to draw lines given two points, what API are you using for the editor ? (For normal C# forms you can use the DrawLine method in System.Drawing.Graphics)
[/quote]

Actually, [s]I'm not using an API[/s]. I'm using Xna, and I need to set tiles along a line between two points on the map. I figured it out though.

[CODE]
private void DrawTileLine(Point p1, Point p2,PointU16 tile)
{
Vector2 dir = new Vector2(p2.X - p1.X, p2.Y - p1.Y);
float len = dir.Length();
Vector2 st = new Vector2(p1.X, p1.Y);
Point cur = new Point(p1.X, p1.Y);
dir.Normalize();
float i = 0f;
while (i <= len)
{
cur.X = (int)st.X;
cur.Y = (int)st.Y;
tileMap.SetTile(cur.X, cur.Y, tile);
st += dir;
i++;
}
}
[/CODE]
0

Share this post


Link to post
Share on other sites
To plot a points along a line segment is fairly easy with linear interpolation:

lerp(a, b, t) : a + t(b - a)

stepwise instructions:
* calcualte and store that vectors length :: ?x² + y²
* then in a loop that goes from 0 to the vectors length rounded to the nearest integer create whatever needs to be plotted along the line and position them using the linear interpolation function, like so;
x = lerp(p1.x, p2.x, (float)i / length);
y = lerp(p1.y, p2.y, (float)i / length);

something like that.

Regards
0

Share this post


Link to post
Share on other sites
This is what I've used in the past for my tile level editors. Just modify it with your own map variables.

This is based in a modified version of Bresenham's classic line drawing code.
[source]

#define ABS(x) (x < 0 ? -x : x)
#define SIGN(x) ((x) < 0 ? -1 : ((x) > 0 ? 1 : 0))

void LineTool(TileMap *map,POINT p1,POINT p2,long tile_ID)
{
long y,x;
long dy,dx;
long sx,sy;
POINT start,end;
long accum;

start.x = p1.x;
start.y = p1.y;
end.x = p2.x;
end.y = p2.y;

dx = end.x - start.x;
dy = end.y - start.y;

sx = SIGN(dx);
sy = SIGN(dy);

dx = ABS(dx);
dy = ABS(dy);

end.x += sx;
end.y += sy;

if(dx > dy)
{
accum = dx >> 1;
do{
map->map[start.y][start.x] = tile_ID;

accum -= dy;
if(accum < 0)
{
accum += dx;
start.y += sy;
}
start.x += sx;
}while(start.x != end.x);
}else{
accum = dy >> 1;
do{
map->map[start.y][start.x] = tile_ID;

accum -= dx;
if(accum < 0)
{
accum += dy;
start.x += sx;
}
start.y += sy;
}while(start.y != end.y);
}
}
[/source]
1

Share this post


Link to post
Share on other sites
[quote name='Nyxenon' timestamp='1327594499' post='4906456']
That's a great method, but I noticed that your x and y variables are unused.
[/quote]

lol! I hadn't noticed that. Through several revisions and changes, the x/y coords got replaced with start.x/y. In fact, I'm not too sure why I was doing that![img]http://public.gamedev.net//public/style_emoticons/default/blink.png[/img]

Let's try this again...

[source]
#define ABS(x) (x < 0 ? -x : x)
#define SIGN(x) ((x) < 0 ? -1 : ((x) > 0 ? 1 : 0))

void LineTool(TileMap *map,POINT p1,POINT p2,long tile_ID)
{
long y,x;
long dy,dx;
long sx,sy;
long accum;

x = p1.x;
y = p1.y;

dx = p2.x - p1.x;
dy = p2.y - p1.y;

sx = SIGN(dx);
sy = SIGN(dy);

dx = ABS(dx);
dy = ABS(dy);

p2.x += sx;
p2.y += sy;

if(dx > dy)
{
accum = dx >> 1;
do{
map->map[y][x] = tile_ID;

accum -= dy;
if(accum < 0)
{
accum += dx;
y += sy;
}
x += sx;
}while(x != p2.x);
}else{
accum = dy >> 1;
do{
map->map[y][x] = tile_ID;

accum -= dx;
if(accum < 0)
{
accum += dy;
x += sx;
}
y += sy;
}while(y != p2.y);
}
}
[/source]
0

Share this post


Link to post
Share on other sites
I still managed to get the code to work how I need it to. However, I had an unusual error. When I ported the code to my game, I messed up a variable name:

[CODE]
while(start.x != end.x);
[/CODE]

Became:

[CODE]
while(start.x != end.y);
[/CODE]

Needless to say, my game froze up, and it took me a while to figure out what it was. Then I decided to step through my code when I noticed that the x value was WAY too high. I got it working though, so for that, I thank you. I probably could have written myself given enough thought, but I prefer to see how other people do things rather than inventing way (like I did above), that may or may not be efficient.
0

Share this post


Link to post
Share on other sites

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  
Followers 0