Search code examples
c++pointerssdl

SDL is sent me on a bad trip with FillRect


    SDL_Rect r3;
    r3.h = 10;
    r3.w = 10;
    r3.x = 10;
    r3.y = 10;

    SDL_Rect r4;
    r3.h = 10;
    r3.w = 10;
    r3.x = 20;
    r3.y = 20;

    SDL_FillRect(surface, &r3, SDL_MapRGB(surface->format, 100, 0 ,100));
    SDL_FillRect(surface, &r4, SDL_MapRGB(surface->format, 100, 100 ,0));

So the crazy thing, and it very weird. maybe I don't know something but R4 is R3. No I am not crazy, it will draw r3 and r4 in the same place.

I commented out SDL_FillRect(surface, &r4, SDL_MapRGB(surface->format, 100, 100 ,0)); and it still draws the rectangle at r4 cords. This drives me crazy guys.

   SDL_Rect r3;
    r3.h = 10;
    r3.w = 10;
    r3.x = 10;
    r3.y = 10;

    SDL_Rect r4;
    r3.h = 10;
    r3.w = 10;
    r3.x = 200;
    r3.y = 200;

    //SDL_FillRect(surface, &r3, SDL_MapRGB(surface->format, 100, 0 ,100));
    SDL_FillRect(surface, &r4, SDL_MapRGB(surface->format, 100, 100 ,0));

Now R4 is not drawn at all

    SDL_Rect r;
    r.w = n.left->width;
    r.h = n.left->height;
    r.x = n.left->position.x;
    r.y = n.left->position.y;

    
    SDL_Rect r2;
    r.w = (int)n.right->width;
    r.h = (int)n.right->height;
    r.x = (int)n.right->position.x;
    r.y = (int)n.right->position.y;

    SDL_Rect r3;
    r3.h = 10;
    r3.w = 10;
    r3.x = 10;
    r3.y = 10;

    SDL_Rect r4;
    r3.h = 10;
    r3.w = 10;
    r3.x = 200;
    r3.y = 200;

    //SDL_FillRect(surface, &r3, SDL_MapRGB(surface->format, 100, 0 ,100));
    SDL_FillRect(surface, &r3, SDL_MapRGB(surface->format, 100, 100 ,0));

So basically r is drown where r2 should be drawn, r2 is not drawn, r3 is drawn where r4 should be and r4 is not drawn. wtf.


Solution

  • After declaring the SDL_rect r4, you set the coordinates for r3.

    SDL_Rect r4;
    r3.h = 10;
    r3.w = 10;
    r3.x = 200;
    r3.y = 200;
    

    This may result in uninitialized coordinate values for r4. This should work:

    SDL_Rect r3;
    r3.h = 10;
    r3.w = 10;
    r3.x = 10;
    r3.y = 10;
    
    //Set r4's coordinates instead of r3's
    SDL_Rect r4;
    r4.h = 10;
    r4.w = 10;
    r4.x = 200;
    r4.y = 200;
    
    SDL_FillRect(surface, &r3, SDL_MapRGB(surface->format, 100, 0 ,100));
    SDL_FillRect(surface, &r4, SDL_MapRGB(surface->format, 100, 100 ,0))