跳至正文
wyaaaattwhoAI RESEARCH & ENGINEERING
← 返回首页

SDL

🔥
SDL运作逻辑:软件—>renderer—>texture(surface)—>window—>硬件

Initiation

用以下语句初始化
创建窗口
关闭窗口

Surface(Outdated)

创建Surface
初始化图片
释放Surface(实测内存占用是巨大的)

Video

👉
补充一个点,窗口的x,y轴原点在左上角

SDL_Window

SDL_Window is the struct that holds all info about the Window itself: size, position, full screen, borders etc.

SDL_Renderer

SDL_Renderer is a struct that handles all rendering. It is tied to a SDL_Window so it can only render within that SDL_Window. It also keeps track the settings related to the rendering. There are several important functions tied to the SDL_Renderer
  • SDL_SetRenderDrawColor(renderer, r, g, b, a);
    • This sets the color you clear the screen to ( see below )
  • SDL_RenderClear(renderer);
    • This clears the rendering target with the draw color set above
  • SDL_RenderCopy(
    • This is probably the function you'll be using the most, it's used for rendering a SDL_Texture and has the following parameters :
    • SDL_Renderer* renderer,The renderer you want to use for rendering.
    • SDL_Texture* texture,The texture you want to render.
    • const SDL_Rect* srcrect, The part of the texture you want to render, NULL if you want to render the entire texture
    • const SDL_Rect* dstrect)Where you want to render the texture in the window. If the width and height of this SDL_Rect is smaller or larger than the dimensions of the texture itself, the texture will be stretched according to this SDL_Rect
  • SDL_RenderPresent(renderer);The other SDL_Render* functions draws to a hidden target. This function will take all of that and draw it in the window tied to the renderer.
    • 👉
      Render is presented in order you have when you give it textures

SDL_Textures and SDL_Surface

The SDL_Renderer renders SDL_Texture, which stores the pixel information of one element. It's the new version of SDL_Surface which is much the same. The difference is mostly that SDL_Surface is just a struct containing pixel information, while SDL_Texture is an efficient, driver-specific representation of pixel data.
You can convert an SDL_Surface* to SDL_Texture using
After this, the SDL_Surface should be freed using
Another important difference is that SDL_Surface uses software rendering (via CPU) while SDL_Texture uses hardware rendering (via GPU).

SDL_Rect

The simplest struct in SDL. It contains only four shorts. x, y which holds the position and w, h which holds width and height.
It's important to note that 0, 0 is the upper-left corner in SDL. So a higher y-value means lower, and the bottom-right corner will have the coordinate x + w, y + h

Event

💡
在SDL里,event均被写为一系列的结构体,使用前要先声明 SDL_Event click;
执行event
退出
按键盘
鼠标

Lib

SDL_GetWindowSurface
extern SDL_Surface *SDL_GetWindowSurface(SDL_Window *window) Get the SDL surface associated with the window. A new surface will be created with the optimal format for the window, if necessary. This surface will be freed when the window is destroyed. Do not free this surface. This surface will be invalidated if the window is resized. After resizing a window this function must be called again to return a valid surface. You may not combine this with 3D or the rendering API on this window. This function is affected by SDL_HINT_FRAMEBUFFER_ACCELERATION.
SDL_PollEvent
extern int SDL_PollEvent(SDL_Event *event) Poll for currently pending events. If event is not NULL, the next event is removed from the queue and stored in the SDL_Event structure pointed to by event. The 1 returned refers to this event, immediately stored in the SDL Event structure -- not an event to follow. If event is NULL, it simply returns 1 if there is an event in the queue, but will not remove it from the queue. As this function may implicitly call SDL_PumpEvents(), you can only call this function in the thread that set the video mode. SDL_PollEvent() is the favored way of receiving system events since it can be done from the main loop and does not suspend the main loop while waiting on an event to be posted. The common practice is to fully process the event queue once every frame, usually as a first step before updating the game's state: c while (game_is_still_running) { SDL_Event event; while (SDL_PollEvent(&event)) { // poll until all events are handled! // decide what to do with this event. } // update game state, draw the current frame }