-1

Is it possible to draw a rectangle on top of an image without creating a pop up window? I was able to draw a rectangle on top of an image with following code, but I don't want the window to be popped up, is it possible?

What I want is load jpg image -> draw rectangles -> save image as jpg.

int main(int argc, char** argv)
{
        bool quit = false;
        SDL_Event event;

        SDL_Init(SDL_INIT_VIDEO);
        IMG_Init(IMG_INIT_JPG);

        SDL_Window* window = SDL_CreateWindow("Test",
                SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
        SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
        SDL_Surface* image = IMG_Load("test.jpg");

        if (!image) {
                printf("Image NULL\n");
                return -1;
        }

        SDL_Texture* texture = SDL_CreateTexture(renderer,
                SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC,
                image->w, image->h);
        image = SDL_ConvertSurfaceFormat(image, SDL_PIXELFORMAT_ARGB8888, 0);

        while (!quit)
        {
                SDL_UpdateTexture(texture, NULL, image->pixels,
                        image->w * sizeof(Uint32));

                SDL_WaitEvent(&event);

                switch (event.type)
                {
                case SDL_QUIT:
                        quit = true;
                        break;
                }

                SDL_SetRenderDrawColor(renderer, 136, 8, 8, 255);

                SDL_Rect dstrect = { 20, 10, 320, 145};
                SDL_RenderCopy(renderer, texture, NULL, NULL);
                SDL_RenderDrawRect(renderer, &dstrect);
                SDL_RenderPresent(renderer);
        }

        SDL_DestroyTexture(texture);
        SDL_FreeSurface(image);
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        IMG_Quit();
        SDL_Quit();

        return 0;
}
lifang
  • 1,485
  • 3
  • 16
  • 23

1 Answers1

1

You can use SDL2's software renderer to draw to surface, then save this surface to a file.

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
#include <stdbool.h>

int main(int argc, char** argv)
{
    SDL_Init(SDL_INIT_VIDEO);
    IMG_Init(IMG_INIT_JPG);

    // load input image
    SDL_Surface* image = IMG_Load("test.jpg");
    if (!image) {
        printf("Image NULL\n");
        return -1;
    }

    // create target surface
    SDL_Surface *output_surface = SDL_CreateRGBSurface(0,
            image->w, image->h, 32,
            0, 0, 0, 0);

    // create software renderer that renders to target surface
    SDL_Renderer *renderer = SDL_CreateSoftwareRenderer(output_surface);

    // create texture from input surface
    SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, image);

    SDL_SetRenderDrawColor(renderer, 136, 8, 8, 255);

    SDL_Rect dstrect = { 20, 10, 320, 145};
    SDL_RenderCopy(renderer, texture, NULL, NULL);
    SDL_RenderDrawRect(renderer, &dstrect);
    SDL_RenderPresent(renderer);

    // save target surface to JPEG file
    IMG_SaveJPG(output_surface, "test_output.jpg", 90);

    SDL_DestroyTexture(texture);
    SDL_FreeSurface(image);
    SDL_DestroyRenderer(renderer);
    SDL_FreeSurface(output_surface);

    IMG_Quit();
    SDL_Quit();

    return 0;
}

Other way would be to create SDL_Surface and use functions like SDL_BlitSurface to copy your image on top of it, and SDL_FillRect to draw filled rectangle (e.g. draw 4 sides of not-filled rectangle).

keltar
  • 17,711
  • 2
  • 37
  • 42