Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to draw several rectangles that grow and shrink constantly at once using SDL3 in Win32 C++

1 Answer

0 votes
#include <SDL3/SDL.h>

#if _WIN32 // PLATFORM_WINDOWS 

#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPWSTR lpCmdLine,
    _In_ int nCmdShow) {

    if (!SDL_Init(SDL_INIT_VIDEO)) {
        // SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError());
        MessageBoxA(0, SDL_GetError(), "Couldn't initialize SDL", MB_OK);
        return 3;
    }

    SDL_Window* window;
    SDL_Renderer* renderer;
    if (!SDL_CreateWindowAndRenderer("SDL3 Draw and fill a bunch of rectangles at once", WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
        // SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window and renderer: %s", SDL_GetError());
        MessageBoxA(0, SDL_GetError(), "Couldn't create window and renderer", MB_OK);
        return 3;
    }

    SDL_Event event;

    while (1) {
        SDL_PollEvent(&event);
        if (event.type == SDL_EVENT_QUIT) {
            break;
        }

        const Uint64 now = SDL_GetTicks();

        // a rectangle that grows and shrinks constantly 
        const float direction = ((now % 2000) >= 1000) ? 1.0f : -1.0f;
        const float scale = ((float)(((int)(now % 1000)) - 500) / 500.0f) * direction;

        // clear the windows with a black color
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);  
        SDL_RenderClear(renderer); // start with a blank canvas

        /*
            typedef struct SDL_FRect
            {
                float x;
                float y;
                float w;
                float h;
            } SDL_FRect;
        */

        SDL_FRect rects[4] = { 0 };

        // Draw and fill several rectangles at once
        for (int i = 0; i < SDL_arraysize(rects); i++) {
            const float w = (float)(WINDOW_WIDTH / SDL_arraysize(rects));
            const float h = (i + 1) * 95.0f;
            rects[i].x = i * w;
            rects[i].y = WINDOW_HEIGHT - h;
            rects[i].w = w;
            rects[i].h = h;
        }
        SDL_SetRenderDrawColor(renderer, 0, 0, 255, SDL_ALPHA_OPAQUE); // blue
        SDL_RenderFillRects(renderer, rects, SDL_arraysize(rects));


        SDL_SetRenderDrawColor(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE); // green
        // Draw 4 rectangles at once as frame to filled rectangles
        SDL_RenderRects(renderer, rects, 4); 

        SDL_RenderPresent(renderer); // put it all on the screen
    }

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);

    SDL_Quit();

    return 0;
}

#endif

 



answered Jan 2, 2025 by avibootz
edited Jan 3, 2025 by avibootz
...