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 render green rectangle with red outline on a window with SDL2 in C

1 Answer

0 votes
#include <stdio.h>
#include <stdbool.h>
#include <SDL.h>

static SDL_Window* window = NULL;
static SDL_Renderer* renderer = NULL;

#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600

bool init() {
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("SDL_Init Error: %s\n", SDL_GetError());
        return false;
    }
    else {
        window = SDL_CreateWindow("SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                                         SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_RESIZABLE);

        if (window == NULL) {
            printf("SDL_CreateWindow Error: %s\n", SDL_GetError());
            return false;
        }
    }

    return true;
}

void close() {
    SDL_DestroyRenderer(renderer);
    SDL_Quit();

    SDL_DestroyWindow(window);
    window = NULL;

    SDL_Quit();
}

int main(int argc, char* argv[]) {

    init();

    renderer = SDL_CreateRenderer(window, -1, 0);

   SDL_Event event;
    bool quit = false;
    while (!quit) {
        while (SDL_PollEvent(&event) != 0) {
            if (event.type == SDL_QUIT)
                quit = true;
        }

        SDL_Rect rectangle = { SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4, 
                               SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 };
        SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
        SDL_RenderFillRect(renderer, &rectangle);

        SDL_Rect outline = { SCREEN_WIDTH / 6, SCREEN_HEIGHT / 6, 
                             SCREEN_WIDTH * 2 / 3, SCREEN_HEIGHT * 2 / 3 };
        SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
        SDL_RenderDrawRect(renderer, &outline);

        SDL_RenderPresent(renderer);

        SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
        SDL_RenderClear(renderer);   
    }

    close();

    return 0;
}

 



answered Aug 5, 2023 by avibootz

Related questions

...