This has been really annoying, but for some reason division bugs out.
Basically, the code in main declares the variables (i is X, j is y, k is z) and attempts a simple "projection" onto the screen using the z coordinate. For some reason, this causes a bunch of different glitches. For one thing, on every other button press, the line disappears. Press a valid button again and the line will come back. Another thing is that changing the value of k sends the endpoints of the line way off into infinity. If you change the value back, then the line is stuck in that portion of the screen until you clear the program. However, commenting out the division makes the whole thing work properly again, so project_x and project_y are definitely the problem. Is division just not defined?
Code: #include <display.h>
#include <keyboard_syscalls.h>
#include <SYSTEM_syscalls.h>
#include <keyboard.hpp>
#include <display_syscalls.h>
//
//draws a point of color color at (x0, y0)
void plot(int x0, int y0, int color) {
char* VRAM = (char*)0xA8000000;
VRAM += 2*(y0*LCD_WIDTH_PX + x0);
*(VRAM++) = (color&0x0000FF00)>>8;
*(VRAM++) = (color&0x000000FF);
return;
}
//Uses the Bresenham line algorithm
void drawLine(int x1, int y1, int x2, int y2, int color) {
signed char ix;
signed char iy;
// if x1 == x2 or y1 == y2, then it does not matter what we set here
int delta_x = (x2 > x1?(ix = 1, x2 - x1):(ix = -1, x1 - x2)) << 1;
int delta_y = (y2 > y1?(iy = 1, y2 - y1):(iy = -1, y1 - y2)) << 1;
plot(x1, y1, color);
if (delta_x >= delta_y) {
int error = delta_y - (delta_x >> 1); // error may go below zero
while (x1 != x2) {
if (error >= 0) {
if (error || (ix > 0)) {
y1 += iy;
error -= delta_x;
} // else do nothing
} // else do nothing
x1 += ix;
error += delta_y;
plot(x1, y1, color);
}
} else {
int error = delta_x - (delta_y >> 1); // error may go below zero
while (y1 != y2) {
if (error >= 0) {
if (error || (iy > 0)) {
x1 += ix;
error -= delta_y;
} // else do nothing
} // else do nothing
y1 += iy;
error += delta_x;
plot(x1, y1, color);
}
}
}
int project_x(int i0,int j0, int k0) {
return i0/k0;
}
int project_y(int i4,int j4,int k4) {
return (-j4)/k4;
}
int main(void) {
int x = 1;
int y = 1;
int key;
int color = 0x000000FF;
int mode = 0;
int iContinue = 1;
int i1 = 1;
int i2 = 50;
int i3 = 60;
int j1 = 1;
int j2 = 50;
int j3 = 60;
int k1 = 0;
int k2 = 1;
int k3 = 1;
int xRadius = 20;
int yRadius = 10;
char* array = "--- ";
array[1] = 0xE5;
array[2] = 0xE2;
array[3] = 0xE3;
int background = 0x01;
while (iContinue) {
Bdisp_AllCr_VRAM();
i2 = project_x(i2,j2,k2);
j2 = project_y(i2,j2,k2);
i3 = project_x(i3,j3,k3);
j3 = project_y(i3,j3,k3);
drawLine(i2, j2, i3, j3, color);
Bdisp_PutDisp_DD();
GetKey(&key);
switch (key) {
case KEY_CTRL_EXIT:
iContinue = 0;
break;
case KEY_CTRL_UP:
j2 = j2 == 1 ? 8 : j2-1;
break;
case KEY_CTRL_DOWN:
j2 = j2 == 8 ? 1 : j2+1;
break;
case KEY_CTRL_LEFT:
i2 = i2 == 0 ? 21 : i2-1;
break;
case KEY_CTRL_RIGHT:
i2 = i2 == 21 ? i2=1 : i2+1;
break;
case KEY_CHAR_PLUS:
k2 = k2 == 7 ? 0 : k2+1;
break;
case KEY_CHAR_MINUS:
if (k2>0) {
k2 = k2 == 0 ? 7 : k2-1;
}
break;
case KEY_CHAR_8:
j3 = j3 == 1 ? 8 : j3-1;
break;
case KEY_CHAR_2:
j3 = j3 == 8 ? 1 : j3+1;
break;
case KEY_CHAR_4:
i3 = i3 == 0 ? 21 : i3-1;
break;
case KEY_CHAR_6:
i3 = i3 == 21 ? i3=1 : i3+1;
break;
case KEY_CHAR_0:
k3 = k3 == 7 ? 0 : k3+1;
break;
case KEY_CHAR_EXP:
if (k3>0) {
k3 = k3 == 0 ? 7 : k3-1;
}
break;
}
}
return 0;
}