Skip to content

Instantly share code, notes, and snippets.

@cr1901
Created May 17, 2015 04:23
Show Gist options
  • Save cr1901/3665216785051b968e48 to your computer and use it in GitHub Desktop.
Save cr1901/3665216785051b968e48 to your computer and use it in GitHub Desktop.
Bresenham for VB
#include "libgccvb/libgccvb.h"
#include "gamectl.h"
#include <limits.h>
void draw_line(short start_x, short start_y, short start_z, \
short end_x, short end_y, short end_z);
void inline plot_point(short x, short y, short z);
/* void inline convert_octant(short * dx, short * dy, short * z); */
int sign(int i);
int iabs(int i);
//int swap(short * x, short * y);
void triangle_mainproc()
{
WA[31].head = WRLD_END;
vbDisplayShow();
while(!(vbPadKeyDown()))
{
//int i;
/* for(i = 0; i < 384; i++)
{
plot_point(i, i, i/12);
} */
//Synchronize with display
//Draw lines until VIP says that "need to draw"
draw_line(0, 0, 0, 383, 223, 0);
draw_line(0, 0, 0, 383, 223, 0);
draw_line(0, 0, 0, 383, 223, 0);
//draw_line(100, 100, 0, 150, 200, 0);
//draw_line(150, 200, 0, 150, 150, 0);
//draw_line(150, 150, 0, 100, 100, 0);
//draw_line(250, 50, 0, 100, 100, 0);
//draw_line(200, 75, 0, 100, 100, 0);
//draw_line(0, 150, 0, 100, 100, 0);
//draw_line(100, 100, 0, 125, 220, 0);
//draw_line(200, 150, 0, 100, 100, 0);
//draw_line(100, 100, 0, 50, 200, 0);
//draw_line(0, 25, 0, 200, 150, 0);
//draw_line(50, 150, 0, 50, 200, 0);
}
vbDisplayHide();
jump_to_reset();
}
//Use Bresenham's algorithm
void draw_line(short start_x, short start_y, short start_z, \
short end_x, short end_y, short end_z)
{
int count;
int do_swap = 0;
long error = 0;
short dx;
short dy;
short di; /* Change in independent var */
short dd; /* Change in dependent var */
short curr_dv, start_iv, end_iv, start_dv, end_dv;
//short * curr_x, * curr_y;
dx = iabs(end_x - start_x);
dy = iabs(end_y - start_y);
/* If y changes at a faster rate than y, do all calculations relative to
y (i.e. y is independent var), and reverse vars when plotting. */
if(dx < dy)
{
do_swap = 1;
di = dy;
dd = dx;
start_iv = start_y;
end_iv = end_y;
start_dv = start_x;
end_dv = end_x;
curr_dv = start_x;
}
else
{
do_swap = 0;
di = dx;
dd = dy;
start_iv = start_x;
end_iv = end_x;
start_dv = start_y;
end_dv = end_y;
curr_dv = start_y;
}
error = 2*dd - di;
plot_point(start_x, start_y, 0);
if(end_iv > start_iv)
{
for(count = (start_iv + 1); count <= end_iv; count++)
{
if(error > 0)
{
if(end_dv > start_dv)
{
curr_dv = curr_dv + 1;
}
else
{
curr_dv = curr_dv - 1;
}
error = error + (2*dd - 2*di);
}
else
{
error = error + 2*dd;
}
if(do_swap)
{
plot_point(curr_dv, count, 0);
}
else
{
plot_point(count, curr_dv, 0);
}
}
}
else if(end_iv < start_iv)
{
for(count = (start_iv - 1); count >= end_iv; count--)
{
if(error > 0)
{
if(end_dv > start_dv)
{
curr_dv = curr_dv + 1;
}
else
{
curr_dv = curr_dv - 1;
}
error = error + (2*dd - 2*di);
}
else
{
error = error + 2*dd;
}
if(do_swap)
{
plot_point(curr_dv, count, 0);
}
else
{
plot_point(count, curr_dv, 0);
}
}
}
else /* Vertical or Horizontal Line. */
{
for(count = start_dv; count <= end_dv; count++)
{
if(do_swap)
{
plot_point(count, start_iv, 0);
}
else
{
plot_point(start_iv, count, 0);
}
}
}
}
void inline plot_point(short x, short y, short z)
{
short y_bias = 2*(y & 0x0F); /* Multiply by 2 b/c 2 bits == 1 pixel */
short rx_offs = 16*(x + z); /* 64 bytes (half-words) per column == 16 longs */
short lx_offs = 16*(x - z); /* 64 bytes (half-words) per column == 16 longs */
short y_offs = y >> 4; /* 16 pixels per 4 bytes (doubleword). */
short lframe_pos = lx_offs + y_offs;
short rframe_pos = rx_offs + y_offs;
(* (L_FRAME0 + lframe_pos)) |= 0x0003uL << y_bias;
(* (L_FRAME1 + lframe_pos)) |= 0x0003uL << y_bias;
(* (R_FRAME0 + rframe_pos)) |= 0x0003uL << y_bias;
(* (R_FRAME1 + rframe_pos)) |= 0x0003uL << y_bias;
/* (* (L_FRAME0 + lframe_pos)) = 0x0003uL << y_bias;
(* (L_FRAME1 + lframe_pos)) = 0x0003uL << y_bias;
(* (R_FRAME0 + rframe_pos)) = 0x0003uL << y_bias;
(* (R_FRAME1 + rframe_pos)) = 0x0003uL << y_bias; */
}
int sign(int i)
{
return i > 0 ? 1 : -1;
}
/* Relies on twos-complement arithmetic. */
int iabs(int i)
{
return i >= 0 ? i : -i;
}
/* void inline convert_octant(short * dx, short * dy, short * z)
{
if(
} */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment