00001
#include "draw_line.h"
00002
#include "dxutil.h"
00003
00004
00005
00006 void Draw_Line(
CSurface * p_surface ,
cPoint2D & start,
cPoint2D & goal,
unsigned char color )
00007 {
00008
00009
int sdx,sdy,dx,dy,dx2,dy2,error,x_inc,y_inc;
00010
int Px,Py;
00011
bool arrived =
false;
00012
00013
if( p_surface == NULL )
return;
00014
00015
00016 sdx = goal.
X - start.
X;
00017 sdy = goal.
Y - start.
Y;
00018 dx = goal.
X - start.
X;
00019 dy = goal.
Y - start.
Y;
00020
00021 Px = start.
X;
00022 Py = start.
Y;
00023
00024
if(dx<0)
00025 {
00026 x_inc = -1;
00027 dx=-dx;
00028 }
00029
else
00030 x_inc=1;
00031
00032
if(dy<0)
00033 {
00034 y_inc = -1;
00035 dy=-dy;
00036 }
00037
else
00038 y_inc = 1;
00039
00040
00041 dx2=dx*2;
00042 dy2=dy*2;
00043
00044
if(dx>dy)
00045 {
00046
00047 error=dy2-dx;
00048 }
00049
else
00050 error=dx2-dy;
00051
00052
00053 DDSURFACEDESC2 ddsd;
00054 HRESULT hr;
00055
00056
00057 ddsd.dwSize =
sizeof(ddsd);
00058 hr = p_surface->
GetDDrawSurface()->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL );
00059
if( hr != DD_OK)
00060 {
00061
00062
_DbgOut(
"display.cpp",137,hr,
"void Draw_Line(CSurface * p_surface , cPoint2D & start,cPoint2D & goal)" );
00063
return;
00064 }
00065
00066
00067
while( !arrived )
00068 {
00069
if(dx>dy)
00070 {
00071
00072
00073
if(error>=0)
00074 {
00075 error-=dx2;
00076
00077 Py+=y_inc;
00078
00079
00080 }
00081
00082 error+=dy2;
00083
00084 Px+=x_inc;
00085
00086
00087 }
00088
else
00089 {
00090
00091
if(error>=0)
00092 {
00093 error-=dy2;
00094
00095 Px+=x_inc;
00096
00097 }
00098
00099 error+=dx2;
00100
00101 Py+=y_inc;
00102
00103 }
00104
00105 ((
unsigned char *)ddsd.lpSurface)[Px + Py * ddsd.lPitch] = color;
00106
00107
00108
00109
if( sdx >=0 && sdy>= 0 && Px >= goal.
X && Py >= goal.
Y )
00110 arrived=
true;
00111
if( sdx >=0 && sdy<0 && Px >= goal.
X && Py <= goal.
Y )
00112 arrived=
true;
00113
if( sdx <0 && sdy>= 0 && Px <= goal.
X && Py >= goal.
Y )
00114 arrived=
true;
00115
if( sdx <0 && sdy< 0 && Px <= goal.
X && Py <= goal.
Y )
00116 arrived=
true;
00117 }
00118
00119 p_surface->
GetDDrawSurface()->Unlock(NULL);
00120
00121 }
00122
00123
00124
00125 void Draw_Line(
CDisplay * g_pDisplay,
cPoint2D & start,
cPoint2D & goal,
unsigned char color )
00126 {
00127
00128
int sdx,sdy,dx,dy,dx2,dy2,error,x_inc,y_inc;
00129
int Px,Py;
00130
bool arrived =
false;
00131
00132
00133
00134 sdx = goal.
X - start.
X;
00135 sdy = goal.
Y - start.
Y;
00136 dx = goal.
X - start.
X;
00137 dy = goal.
Y - start.
Y;
00138
00139 Px = start.
X;
00140 Py = start.
Y;
00141
00142
if(dx<0)
00143 {
00144 x_inc = -1;
00145 dx=-dx;
00146 }
00147
else
00148 x_inc=1;
00149
00150
if(dy<0)
00151 {
00152 y_inc = -1;
00153 dy=-dy;
00154 }
00155
else
00156 y_inc = 1;
00157
00158
00159 dx2=dx*2;
00160 dy2=dy*2;
00161
00162
if(dx>dy)
00163 {
00164
00165 error=dy2-dx;
00166 }
00167
else
00168 error=dx2-dy;
00169
00170
00171 DDSURFACEDESC2 ddsd;
00172 HRESULT hr;
00173
00174
00175 ddsd.dwSize =
sizeof(ddsd);
00176 hr =
g_pDisplay->
GetBackBuffer()->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL );
00177
if( hr != DD_OK)
00178 {
00179
00180
_DbgOut(
"display.cpp",249,hr,
"void Draw_Line( cPoint2D & start,cPoint2D & goal)" );
00181
return;
00182 }
00183
00184
00185
while( !arrived )
00186 {
00187
if(dx>dy)
00188 {
00189
00190
00191
if(error>=0)
00192 {
00193 error-=dx2;
00194
00195 Py+=y_inc;
00196
00197
00198 }
00199
00200 error+=dy2;
00201
00202 Px+=x_inc;
00203
00204
00205 }
00206
else
00207 {
00208
00209
if(error>=0)
00210 {
00211 error-=dy2;
00212
00213 Px+=x_inc;
00214
00215 }
00216
00217 error+=dx2;
00218
00219 Py+=y_inc;
00220
00221 }
00222
00223 ((
unsigned char *)ddsd.lpSurface)[Px + Py * ddsd.lPitch] = color;
00224
00225
00226
00227
if( sdx >=0 && sdy>= 0 && Px >= goal.
X && Py >= goal.
Y )
00228 arrived=
true;
00229
if( sdx >=0 && sdy<0 && Px >= goal.
X && Py <= goal.
Y )
00230 arrived=
true;
00231
if( sdx <0 && sdy>= 0 && Px <= goal.
X && Py >= goal.
Y )
00232 arrived=
true;
00233
if( sdx <0 && sdy< 0 && Px <= goal.
X && Py <= goal.
Y )
00234 arrived=
true;
00235 }
00236
00237
g_pDisplay->
GetBackBuffer()->Unlock(NULL);
00238
00239 }