如何在 SDL 中在屏幕上绘制矩形?

问题描述 投票:0回答:3

当我已经渲染了纹理时,我不知道如何填充矩形。当我使用

SDL_RenderClear(renderer);
时 矩形消失了,但是当我使用
SDL_UpdateWindowSurface(window);
时,纹理消失了。 我尝试过使用
SDL_CreateRGBSurface
方法将矩形渲染为纹理,但这也不起作用。输出只是一个黑色的全屏窗口,其中渲染了图像。按箭头键使图像移动。

这是我的代码:

 #include <SDL.h>
    #include <SDL_ttf.h>
    #include <SDL_image.h>
    #include <stdio.h>

    SDL_Rect sr;
    SDL_Rect dr;
    SDL_Rect dr2;
    SDL_Renderer * renderer;
    SDL_Texture * texture;

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


        SDL_Init(SDL_INIT_VIDEO);
        IMG_Init(IMG_INIT_JPG);
        TTF_Init();
        sr.x = sr.y = sr.h = sr.w = 40;
        dr.x = 20;
        dr.y = 20;
        dr.h = 100;
        dr.w = 100;

        dr2.x = 80;
        dr2.y = 80;
        dr2.h = 100;
        dr2.w = 100;


        SDL_Window *window = SDL_CreateWindow("Game window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1000, 1000, SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN_DESKTOP);
        SDL_Surface *screen = SDL_GetWindowSurface(window);



        SDL_UpdateWindowSurface(window);
        renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
        Uint32 color2 = SDL_MapRGB(screen->format, 255, 0, 0);

        SDL_UpdateWindowSurface(window);
        SDL_Surface * image= IMG_Load("p.jpg");
        if (image== NULL){ printf(SDL_GetError()); }
        texture = SDL_CreateTextureFromSurface(renderer,
        message);
        SDL_Surface *cr = SDL_CreateRGBSurface(NULL, 1000, 1000, 8, 255, 0, 0, 255);

        SDL_Texture *texture2 = SDL_CreateTextureFromSurface(renderer, cr);
        SDL_RenderClear(renderer);
        SDL_RenderCopy(renderer, texture2, NULL, &dr2);
        SDL_RenderCopy(renderer, texture, NULL, &dr);
        SDL_RenderPresent(renderer);


        while (!quit)
        {
            while (SDL_PollEvent(&event)!=0){

                if (event.type == SDL_QUIT){ quit = true; }
                else if (event.type==SDL_KEYDOWN)
                {
                    switch (event.key.keysym.sym){


                        if (event.key.keysym.sym == SDLK_LEFT && event.key.keysym.sym == SDLK_UP){
                            dr.x -= 30;
                            dr.y -= 30;
                            SDL_RenderClear(renderer);
                            SDL_RenderCopy(renderer, texture, NULL, &dr2);
                            SDL_RenderCopy(renderer, texture2, NULL, &dr);
                            SDL_RenderPresent(renderer); }

                    case SDLK_LEFT: 
                        if (dr.x <= (-20)){ dr.x = ((screen->w) + 20); }
                        dr.x -=30;
                        SDL_RenderClear(renderer);
                        SDL_RenderCopy(renderer, texture, NULL, &dr2);
                        SDL_RenderCopy(renderer, texture2, NULL, &dr);
                        SDL_RenderPresent(renderer);
                        break;

                    case SDLK_UP:
                        if (dr.y <= 0){ dr.y = screen->h; }
                        dr.y -= 30;
                        SDL_RenderClear(renderer);
                        SDL_RenderCopy(renderer, texture, NULL, &dr2);
                        SDL_RenderCopy(renderer, texture2, NULL, &dr2);
                        SDL_RenderPresent(renderer);
                        break;

                    case SDLK_RIGHT:
                        if (dr.x >= (screen->w)){ dr.x = -1 * 20; }
                        dr.x += 30;
                        SDL_RenderClear(renderer);
                        SDL_RenderCopy(renderer, texture, NULL, &dr2);
                        SDL_RenderCopy(renderer, texture2, NULL, &dr);
                        SDL_RenderPresent(renderer);
                        break;

                    case SDLK_DOWN:
                        if (dr.y >= (screen->h)){ dr.y = -20; }
                        dr.y += 30;
                        SDL_RenderClear(renderer);
                        SDL_RenderCopy(renderer, texture, NULL, &dr2);
                        SDL_RenderCopy(renderer, texture2, NULL, &dr);
                        SDL_RenderPresent(renderer);
                        break;

                    case SDLK_ESCAPE: 
                        quit = true;
                        break;

                    }




                }
        }


        }

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

        return 0;
    }
c++ visual-studio sdl sdl-2
3个回答
2
投票

请勿将屏幕表面模拟与新的 SDL_Render API 混合使用。 也就是说,根本不要使用

SDL_GetWindowSurface

另外,不要每次绘制内容时都清除渲染器。 在绘制所有内容之前,每帧仅清除一次。 使用

SDL_RenderClear
SDL_RenderCopy
SDL_RenderPresent
将重复的块移出事件循环。


1
投票

在屏幕上绘制一个简单的矩形:

SDL_Window* window;
SDL_Renderer* renderer;
SDL_Rect box;
//define box attributes
box.w = 10; 
box.h = 10; 
box.x = 0;
box.y = 0; 

window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 250, 250, NULL);
renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(renderer, 0,0,0,0);
SDL_RenderClear(renderer);
//outline rect
SDL_SetRenderDrawColor(renderer, r , g , b, a);
SDL_RenderDrawRect(renderer, &box);
SDL_SetRenderDrawColor(renderer, r , g , b, a);
//fill up rectangle with color
SDL_RenderFillRect(renderer, &box);

0
投票

首先,考虑一下您希望如何在 SDL 中绘制:通过 CPU 还是 GPU?如果是 GPU,您应该使用渲染器,而不是表面。

SDL_Renderer* screenRenderer = SDL_CreateRenderer(window, 0, 0); //Get Renderer from window

SDL_SetRenderDrawColor(screenRenderer, r, g, b, alpha) //Set color you will draw

SDL_RenderClear(screenRenderer); //Fill screen by one color

SDL_RenderDrawRect(screenRenderer, &(SDL_Rect) {x, y, width, height}); //Draw rect

SDL_RenderFillRect(screenRenderer, &(SDL_Rect) {x, y, width, height}); //Fill rect

SDL_Texture* yourTexture = SDL_CreateTextureFromSurface(yourImageSurface); //Get Texture from Surface

SDL_RenderCopy(screenRenderer, yourTexture, NULL, destRect); //Draw texture to Renderer

SDL_RenderPresent(screenRenderer); //Presents All from renderer to screen
© www.soinside.com 2019 - 2024. All rights reserved.