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 a box inside a shape with lines and different colors using SDL3 in Win32 C++

1 Answer

0 votes
#include <SDL3/SDL.h>

#if _WIN32 // PLATFORM_WINDOWS 

#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 a box inside a shape with lines and different colors", 640, 480, 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;
        }

        // Set backround color
        SDL_SetRenderDrawColor(renderer, 33, 33, 33, SDL_ALPHA_OPAQUE); // dark gray
        SDL_RenderClear(renderer); //Start with a blank dark gray canvas

        static const SDL_FPoint line_points[] = {
                { 100, 354 }, { 220, 230 }, { 140, 230 }, { 320, 100 }, { 500, 230 },
                { 420, 230 }, { 540, 354 }, { 400, 354 }, { 100, 354 }
        };

        // bool SDLCALL SDL_RenderLines(SDL_Renderer * renderer, const SDL_FPoint * points, int count);

        // set shape lines colors
        SDL_SetRenderDrawColor(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE);
        // Draw a series of connected lines in a single batch to create a shape
        SDL_RenderLines(renderer, line_points, SDL_arraysize(line_points));

        // bool SDLCALL SDL_RenderLine(SDL_Renderer *renderer, float x1, float y1, float x2, float y2);

        // Set box lines colors
        SDL_SetRenderDrawColor(renderer, 38, 103, 155, SDL_ALPHA_OPAQUE);

        // Draw the box with lines, one at a time
        SDL_RenderLine(renderer, 240, 170, 400, 170); // top line -
        SDL_RenderLine(renderer, 240, 345, 400, 345); // bottom line -
        SDL_RenderLine(renderer, 400, 170, 400, 345); // right line |
        SDL_RenderLine(renderer, 240, 170, 240, 345); // left line |
        
        SDL_RenderPresent(renderer); // put all on the screen
    }

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);

    SDL_Quit();

    return 0;
}

#endif

 



answered Dec 30, 2024 by avibootz
edited Jan 1, 2025 by avibootz
...