📜  sdl draw Rectf - C 编程语言(1)

📅  最后修改于: 2023-12-03 14:47:20.981000             🧑  作者: Mango

SDL Draw Rectf - C Programming Language

SDL is a powerful tool for game developers and anyone looking to create interactive software programs. One of the many functions that SDL offers is the ability to draw rectangles on the screen or window. In this article, we'll go over how to use the SDL_DrawRectF function to draw rectangles in your C programming language projects.

What is SDL?

SDL is a popular cross-platform software development library designed to provide low-level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D. SDL is written in C, but works with a variety of programming languages including C++, Python, Lua, and many others.

Rectangles in SDL

Rectangles are a basic shape used in computer graphics to simplify the process of drawing objects on the screen. In SDL, rectangles are defined by their x and y position, as well as their width and height. SDL offers several functions for drawing rectangles, including the SDL_DrawRectF function.

SDL_DrawRectF function

The SDL_DrawRectF function is used to draw a filled rectangle on the screen at a specific position and size. Here is the function signature:

int SDL_DrawRectF(SDL_Renderer* renderer, const SDL_FRect* rect);

The parameters are as follows:

  • renderer: The renderer to draw on.
  • rect: The rectangle to draw.

With SDL_DrawRectF, you can draw a filled rectangle on the screen by passing the renderer and the rectangle you want to draw as arguments. Here's an example:

#include <SDL2/SDL.h>

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

    SDL_Window* window = SDL_CreateWindow("SDL Rectangles", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    SDL_FRect rect = { 50.0f, 50.0f, 100.0f, 100.0f };
    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    SDL_RenderDrawRectF(renderer, &rect);

    SDL_RenderPresent(renderer);

    SDL_Delay(5000);

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

In this example, we create a window and a renderer, and then draw a rectangle on the screen using SDL_RenderDrawRectF. The rectangle's position and size are defined by the SDL_FRect struct. Finally, we present the renderer and delay the program for 5 seconds before destroying the renderer and window and quitting SDL.

Conclusion

The SDL_DrawRectF function is a powerful tool for drawing filled rectangles on the screen in SDL. With some basic knowledge of C programming and SDL, you can use this function to create dynamic and engaging graphics in your projects.