SDL window acting weird with diffrent DrawLine & FillTriangle algorithms

Hey,

So here it goes; I am currently studying computer science and i finishing the required algorithms to draw lines to form triangles, calculate bounding boxes & creating the fill triangle function. However, the SDL(SDL code were supplied from the school already) is only partially completing the task, and only when i tab to another window or refresh it with another window over the SDL window, it updates yet again. Here is an example of what i am talking about; SDL window to the left is on originally viewing the SDL window, the one on the right is after just hovering another application over it, for instance mozilla firefox.

http://i108.photobucket.com/albums/n12/vladwow/SDLcomparison.jpg


As you can see the final task is just to squish the DrawTriangle function through an entire Array of triangle data to draw this teapot.

This is what strikes me as odd, since changing seemingly irrelevant things in my code alters the misformed image on the screen. I am not asking for you to fix this, i am just wondering if anyone brightminded people out there could explain to me why this is happending.

Here is the relevant functions assosiated with it, i will not give all the SDL related stuff though;

min and max functions are self-explainatory. Usage of sx and sy coordinates are because cordinates used in the teapot array are on a -500 to 500 cartesian coordinate system.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#define INSIDE_TRIANGLE 0
#define INCREMENT 1

// Scale triangle
void ScaleTriangle(triangle_t *triangle)
{
	triangle->scale = 1.0;

    triangle->sx1 = triangle->x1 * triangle->scale;
    triangle->sx2 = triangle->x2 * triangle->scale;
    triangle->sx3 = triangle->x3 * triangle->scale;
    triangle->sy1 = triangle->y1 * triangle->scale;
    triangle->sy2 = triangle->y2 * triangle->scale;
    triangle->sy3 = triangle->y3 * triangle->scale;
}


// Move triangle to its screen position
void TranslateTriangle(triangle_t *triangle)
{
	triangle->sx1 += triangle->tx;
	triangle->sx2 += triangle->tx;
	triangle->sx3 += triangle->tx;
	triangle->sy1 += triangle->ty;
	triangle->sy2 += triangle->ty;
	triangle->sy3 += triangle->ty;
}


// Calculate triangle bounding box
void CalculateTriangleBoundingBox(triangle_t *triangle)
{


	triangle->bx = min(triangle->sx1, triangle->sx2, triangle->sx3);
	triangle->by = min(triangle->sy1, triangle->sy2, triangle->sy3);
	triangle->bw = max(triangle->sx1, triangle->sx2, triangle->sx3);
	triangle->bh = max(triangle->sy1, triangle->sy2, triangle->sy3);


}
// Fill triangle with a color
void FillTriangle(SDL_Surface *screen, triangle_t *triangle)
{


	int i, j, start, stop, step;

	for(i = triangle->bx; i <= triangle->bw; i++){
		step = INSIDE_TRIANGLE;
		for (j = triangle->by; j <= triangle->bh; j++){
			if (GetPixel(screen, i, j) == TRIANGLE_PENCOLOR && step == INSIDE_TRIANGLE){
				start = stop = j;
				step = INCREMENT;
			}
			if (GetPixel(screen, i, j) == TRIANGLE_PENCOLOR && step == INCREMENT){
				stop = j;
			}

		}
		DrawLine(screen, i, start, i, stop, triangle->fillcolor);
	}

}

// Draw triangle on screen
void DrawTriangle(SDL_Surface *screen, triangle_t *triangle)
{
    int isOK;
    
    // Scale.
    ScaleTriangle(triangle);
    
    // Translate.
    triangle->tx = screen->w/2;
    triangle->ty = screen->h/2;
    TranslateTriangle(triangle);
    
    // Determine bounding box
    CalculateTriangleBoundingBox(triangle);

    // Sanity check that triangle is within screen boundaries.
    isOK = SanityCheckTriangle(screen, triangle);
    if (isOK == 0) {
        PrintTriangle(triangle, "Triangle outside screen boundaries");
        return;
    }


    //3 DrawLine functions to draw the distinct outline of the triangle in the color Red.
	DrawLine(screen, triangle->sx1, triangle->sy1, triangle->sx2, triangle->sy2, TRIANGLE_PENCOLOR);
	DrawLine(screen, triangle->sx1, triangle->sy1, triangle->sx3, triangle->sy3, TRIANGLE_PENCOLOR);
	DrawLine(screen, triangle->sx2, triangle->sy2, triangle->sx3, triangle->sy3, TRIANGLE_PENCOLOR);

    // Fill triangle
    FillTriangle(screen, triangle);

    // Force screen update.  
    SDL_UpdateRect(screen, triangle->bx, triangle->by, triangle->bw, triangle->bh);


}



There are of course assosiated header files to make this bunch work, but i wnated to paste along the functions i had to finish in order to get this DrawTriangle to work. And i tested this with only one triangle aswell, some triangles it draws and fills on the first load, others it needs to "refresh".

I also wonder why on earth it suddently starts drawing entire boxes from y=0 sometimes...

If anyone could shed some light on this, it would be much appreciated. I do understand however, that i might be a too long read & too much work to figure out the problem.

I can post the entire code if you so desire to help me figure out this problem; i am using eclipse IDE, and i am writing in C code. Should not be much of a diffrence..

Sincerly, Vladwow
Last edited on
Topic archived. No new replies allowed.