英文:
SDL2 draw a rectangle in an image without window
问题
我能否在图像上方绘制一个矩形而不创建弹出窗口?我能够使用以下代码在图像上方绘制一个矩形,但我不想弹出窗口,这可能吗?
我想要的是加载jpg图像 -> 绘制矩形 -> 将图像保存为jpg。
英文:
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;
}
答案1
得分: 1
你可以使用SDL2的软件渲染器来绘制到表面,然后将这个表面保存到文件中。
#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);
// 加载输入图像
SDL_Surface* image = IMG_Load("test.jpg");
if (!image) {
printf("Image NULL\n");
return -1;
}
// 创建目标表面
SDL_Surface *output_surface = SDL_CreateRGBSurface(0,
image->w, image->h, 32,
0, 0, 0, 0);
// 创建渲染器,用于渲染到目标表面
SDL_Renderer *renderer = SDL_CreateSoftwareRenderer(output_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);
// 将目标表面保存为JPEG文件
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;
}
另一种方法是创建SDL_Surface并使用SDL_BlitSurface
等函数将图像复制到其上,以及使用SDL_FillRect
来绘制填充的矩形(例如,绘制未填充矩形的四边)。
英文:
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论