1/0 SDL creation window and color filling

What is SDL?
SDL is actually a function library convenient for game development. SDL provides several functions to control image, sound, output and input, so that developers can develop application software across multiple platforms (Linux, Windows, Mac OS X, etc.) with the same or similar code.

What's the use of learning SDL?
At present, SDL is mostly used to develop multimedia applications such as games, simulators and media players.

What are the difficulties of SDL learning?
Using vs Software, we need to introduce SDL library functions. SDL is used as a writing tool. Our purpose is to flexibly master the use methods of SDL functions, including: the type, number and meaning of passed parameters; Function return value type and its meaning; How to debug if there are errors in the use of functions; Individual functions must have corresponding function aftertreatment after use (functions that open up memory, release memory in order after use, so as to facilitate the recycling of memory)..., we will learn according to this mode.

Configuration of 0/0 SDl project

1. Create a project in vs and create a new main function.

2. Add the SDL header file include to the attachment of the property page.

3. Add SDL static library file to vs project.

1 / 0 display window with SDL

#include "SDL.h"
#include <stdio.h>
#include <Windows.h>
int main(void)
{
    SDL_Window * pWindow = NULL;
  

     // 1 initialize SDL Library
    if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
        printf("SDL initialization failed!%s\n", SDL_GetError());
        return -1;
    }
    printf("SDL Initialization succeeded!\n");

    // 2 create window
    pWindow = SDL_CreateWindow("Lesson01", 
        SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, //In the middle of the display
        400, 400,//400px length and width
        SDL_WINDOW_SHOWN);
    if(pWindow == NULL)
    {
        printf("SDL Window creation failed!\n");
    }
    else
    {
        printf("SDL Window created successfully!\n");
    }

    // Delay 2000 ms for easy observation
    SDL_Delay(2000);

    // 3 destroy window
    SDL_DestroyWindow(pWindow);

    // 4 exit SDL Library
    SDL_Quit();

    return 0;

Two common SDL functions used above:

1. int SDL_Init(Uint32 flags)

Function: initialize the subsystem of SDL library

Parameter: Flag subsystem identifier

       SDL_INIT_TIMER                     Timer subsystem

       SDL_INIT_AUDIO                     Audio subsystem

       SDL_INIT_VIDEO                         Video subsystem

       SDL_INIT_JOYSTICK                      Lever subsystem

       SDL_INIT_HAPTIC                        Touch screen feedback subsystem

       SDL_INIT_GAMECONTROLLER      Controller subsystem

      SDL_INIT_EVENTS                         Event subsystem

      SDL_INIT_EVERYTHING                 All subsystems

       SDL_INIT_NOPARACHUTE            Non capture lethal signal subsystem

Return value: 0 when successful
- 1 on failure

2. SDL_Window SDL_CreateWindow(const char title,
int x,
int y,
int w,
int h,
Uint32 flags)

Function: create window

Parameters:

      title    The title of the window, UTF-8 code.(Chinese is not supported)

      x, y     The position of the screen where the window is located, X Coordinates, Y coordinate

          SDL_WINDOWPOS_CENTERED              Center

      w, h     The width and height of the window

      flags    Type identifier of the window when it was created

          SDL_WINDOW_FULLSCREEN               Window full screen

          SDL_WINDOW_FULLSCREEN_DESKTOP       Window full screen at current desktop resolution

          SDL_WINDOW_OPENGL                   OpenGL Available windows under

          SDL_WINDOW_SHOWN                    The window is visible and the window is displayed

          SDL_WINDOW_HIDDEN                   The window is invisible and hidden

          SDL_WINDOW_BORDERLESS               Windows have no borders

          SDL_WINDOW_RESIZABLE                The window can be resized

          SDL_WINDOW_MINIMIZED                window minimizing

          SDL_WINDOW_MAXIMIZED                window maximizing

          SDL_WINDOW_INPUT_GRABBED            The window has input focus

          SDL_WINDOW_INPUT_FOCUS              The window has input focus

          SDL_WINDOW_MOUSE_FOCUS              The window can get the mouse focus

         SDL_WINDOW_FOREIGN                  Window is not SDL Created

          SDL_WINDOW_ALLOW_HIGHDPI            Windows should be created in high-DPI Mode(>= SDL 2.0.1)

         SDL_WINDOW_MOUSE_CAPTURE            The window can capture the mouse focus(>= SDL 2.0.4)

                                              (And SDL_WINDOW_INPUT_GRABBED irrelevant)

Return value: SDL is returned after the creation is successful_ Window * window pointer
Creation failed, NULL returned

1. Pay attention to the way of debug ging in the code.

Use the return value of the function to judge whether the function successfully completes the task. Use SDL at the same time_ Geterror() function to determine the bug type—— Because the compiler will not tell the SDL function to use the error. This is a different place to study.

2. Functions in pairs (free memory)

Initialize SDL library and exit SDL Library:
SDL_Window* SDL_CreateWindow()
SDL_Destroywindow()
Create and destroy windows:
SDL_CreatWindow()
SDL_DestroyeWindow()

2 / 0 color fill the window

Note: the fill color actually changes the relevant code information stored in the Surface layer of Window.

       SDL_Surface * pSurface = NULL;

       // Get the Surface of the window
        pSurface = SDL_GetWindowSurface(pWindow);

        // Fill the Surface with red
        SDL_FillRect(pSurface, NULL, SDL_MapRGB(pSurface->format,152, 29, 29));

Here, you may think that the color filling is completed, but the window is displayed immediately when it is created successfully. When the color filling code is executed, the window will not be updated automatically. Therefore, we need to add a WindowSurface update function.

       // Update window Surface
        SDL_UpdateWindowSurface(pWindow);

At this time, the goal is completed!

3/0 SDL loads and displays a picture

#include <stdio.h>
#include "SDL.h"

int main(void)
{
    SDL_Window * pWindow = NULL;        // window
    SDL_Renderer * pRenderer = NULL;    // Renderer 
    SDL_Surface * pSurface = NULL;      // layer 
    SDL_Texture * pTexture = NULL;      // texture

    // Initialize SDL Library
    if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
        printf("%s\n", SDL_GetError());
        return -1;
    }
    
    // create a window
    pWindow = SDL_CreateWindow("lesson02", 
        SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
        1000, 600, SDL_WINDOW_SHOWN);

    if(NULL == pWindow)
    {
        printf("%s\n", SDL_GetError());
        return -1;
    }

    // Create renderer
    pRenderer = SDL_CreateRenderer(pWindow, -1, 
        SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

    if(NULL == pRenderer)
    {
        printf("%s\n", SDL_GetError());
        return -1;
    }

    // Load picture generation layer according to picture path
    pSurface = SDL_LoadBMP("../image/HelloWorld.bmp");
    if(NULL == pSurface)
    {
        printf("%s\n", SDL_GetError());
        return -1;
    }

    // Generate textures from layers based on a renderer
    pTexture = SDL_CreateTextureFromSurface(pRenderer, pSurface);
    if(NULL == pTexture)
    {
        printf("%s\n", SDL_GetError());
        return -1;
    }

    // Release layer
    SDL_FreeSurface(pSurface);

    // Copy image to renderer
    SDL_RenderCopy(pRenderer, pTexture, NULL, NULL);

    // Change line renderer
    SDL_RenderPresent(pRenderer);

    // delayed
    SDL_Delay(3000);

    // Destroy renderer
    SDL_DestroyRenderer(pRenderer);

    // Destroy Window 
  // SDL_DestroyWindow(pWindow);

    // Exit SDL Library
    SDL_Quit();

    return 0;
}

SDL_Renderer SDL_CreateRenderer(SDL_Window window,**

                             int         index,

                             Uint32      flags)

Parameters: target window of the window renderer

     index       The index value of the device to be initialized,-1 Indicates that the default rendering device is initialized

     flags       Render mode identifier

      SDL_RENDERER_SOFTWARE       Using software rendering

      SDL_RENDERER_ACCELERATED    Hardware accelerated rendering

      SDL_RENDERER_PRESENTVSYNC   Synchronize with the refresh rate of the display

      SDL_RENDERER_TARGETTEXTURE  The renderer supports rendering textures

**Return value: * * if the creation is successful, the created renderer will be returned
Creation failed, NULL returned
technological process:

Initialize SDL Library - > create Window - > createrenderer - > loadmp (generate layer from Image) - > createtexturefromsurface (assign texture generated by layer) to renderer - > freesurface (free space occupied by layer) - > renderercopy (copy Image by renderer) - > rendererpresent (update renderer) - > destroyrenderer - > d Estroywindow - > quit

Keywords: C Linux sdl

Added by andrew_ww on Sun, 05 Sep 2021 08:15:41 +0300