Touchscreen bug ?

Post Reply
Daimie
Posts: 1
Joined: Mon Nov 30, 2009 11:15 pm

Touchscreen bug ?

Post by Daimie » Mon Nov 30, 2009 11:35 pm

Hello,

is there a known bug with libnds that causes randomly lines pop out of nowhere when you draw with stylus? It works well most of the time but like on every 10th - 20th draw the bug occurs and an extra random line is drawn on the screen in addition to the one you draw with stylus. Anyone experienced anything similar/is there something wrong with the code? Thanks for any suggestions.

The relevant part of code from some tutorial:

Code: Select all

void Review::DrawLine(int x1, int y1, int x2, int y2)
{
    int yStep = SCREEN_WIDTH;
    int xStep = 1;
    int xDiff = x2 - x1;
    int yDiff = y2 - y1;
    int color = RGB15(0,0,0) | BIT(15);
 
    int errorTerm = 0;
    int offset = y1 * SCREEN_WIDTH + x1;
    int i;
    
    if (yDiff < 0) {
        yDiff = -yDiff;
        yStep = -yStep;
    }
    
    if (xDiff < 0) {
        xDiff = -xDiff;
        xStep = -xStep;
    }
 
    if (xDiff > yDiff) {
        for (i = 0; i < xDiff + 1; i++) {
            for(int x = -1; x < 1; ++x) {
                for(int y = -1; y < 1; ++y) {
                    int nx = offset%256 + x;
                    int ny = offset/256 + y;
                    videobuf_sub_stats[nx + ny * SCREEN_WIDTH] = color;
                }
            }
            offset += xStep;
            errorTerm += yDiff;
 
            if (errorTerm > xDiff) {
                errorTerm -= xDiff;
                offset += yStep;
          }
       }
    }

    else  {
        for(i = 0; i < yDiff + 1; i++)  {
            for(int x = -1; x < 1; ++x) {
                for(int y = -1; y < 1; ++y) {
                    int nx = offset%256 + x;
                    int ny = offset/256 + y;
                    videobuf_sub_stats[nx + ny * SCREEN_WIDTH] = color;
                }
            }
            offset += yStep;
            errorTerm += xDiff;

            if (errorTerm > yDiff) {
               errorTerm -= yDiff;
               offset += xStep;
            }
        }
    }
}
and the other function:

Code: Select all

void Review::TouchScreen()
{
    static int tp_thresh = 256;
    int down = keysDown();
    int held = keysHeld();
    int up = keysUp();
    
    if (down & (KEY_L|KEY_R)) {
        ClearScreen(STAT);
        DrawScreen(STAT);
    }
    if (down & KEY_TOUCH) {
        tp_thresh = 30;
        touchPosition touch;
        touchRead(&touch);
        
        oldx = touch.px;
        oldy = touch.py;
    }
    if (held & KEY_TOUCH) {
        touchPosition touch;
        touchRead(&touch);
        
        if (touch.px < 12 && touch.py < 12) {
            ClearScreen(STAT);
            DrawScreen(STAT);
        }
        // attempt to fix the bug here
        int d = sqrt(((oldx -touch.px)*(oldx-touch.px)) + ((oldy -touch.py)*(oldy-touch.py)));
        
        if (d < tp_thresh) {
            DrawLine(touch.px, touch.py, oldx, oldy);
            oldx = touch.px;
            oldy = touch.py;
        }
    }
    if (up & KEY_TOUCH) {
        tp_thresh = 256;
    }
}

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests