Reputation: 8991
Im trying to cover small rectangles within an SDL_Surface *
by using SDL_FillRect()
with the following code:
int display(SDL_Surface * screen, Uint16 tile_size){
if (!screen)
return 1;
std::cout << x << " " << y << std::endl;
SDL_Rect pos = {(Sint16) (x * tile_size), (Sint16) ((y - 2) * tile_size), tile_size, tile_size};
std::cout << pos.x << " " << pos.y << std::endl;
for(uint8_t Y = 0; Y < 4; Y++){
pos.x = x * tile_size;
for(uint8_t X = 0; X < 4; X++){
// bit mask check to see which bits should be displayed.
// not relevant to question
// if (shape[orientation][Y] & (1U << (3 - X))){
std::cout << pos.x << " " << pos.y << " -> ";
SDL_FillRect(screen, &pos, color);
std::cout << pos.x << " " << pos.y << std::endl;
// }
pos.x += tile_size;
}
pos.y += tile_size;
}
std::cout << std::endl << std::endl;
return 0;
}
x ranges from 0 to 9, y ranges from 0 to 21, tile_size is small (25 right now).
When this code is run, the output is the following:
3 0 // x y
75 -50 // x*tile_size y*tile_size
75 -50 -> 75 0 // what in the world?
100 0 -> 100 0
125 0 -> 125 0
150 0 -> 150 0
75 25 -> 75 25
100 25 -> 100 25
125 25 -> 125 25
150 25 -> 150 25
75 50 -> 75 50
100 50 -> 100 50
125 50 -> 125 50
150 50 -> 150 50
75 75 -> 75 75
100 75 -> 100 75
125 75 -> 125 75
150 75 -> 150 75
The third line (first displayed rectangle) is somehow moved from being at -50 to 0. It is throwing off my display calculations. What is wrong? Am I missing something obvious?
Upvotes: 1
Views: 345
Reputation: 1903
I saw this when I was looking through the documentation: "If there is a clip rectangle set on the destination (set via SDL_SetClipRect
) then this function will clip based on the intersection of the clip rectangle and the dstrect
rectangle and the dstrect
rectangle will be modified to represent the area actually filled."
This indicates that there is a clip rectangle causing the issue.
That clip rectangle is a property contained in your SDL_Surface
, and probably starts at (0,0). That would make any negative numbers become 0 after the SDL_FillRect()
call is made.
You could fix this by passing a reference to a copy of the dstrect
.
Source: http://www.libsdl.org/docs/html/sdlfillrect.html
Upvotes: 3