SDL2.0创建HelloWorld工程


***【在线视频教程】***

好文章,来自【福优学苑@音视频+流媒体】

SDL2.0创建HelloWorld工程

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
 
#include <SDL.h>
#include <SDL_image.h>
 
int main_Helloworld()
{
/// SDL核心对象
SDL_Window *win = SDL_CreateWindow("hello world!", 100, 100, 640, 480, SDL_WINDOW_RESIZABLE);
SDL_Surface *picture = SDL_LoadBMP("./m_bottle.bmp");
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, picture);
 
SDL_FreeSurface(picture);
SDL_RenderClear(ren);
/// 把纹理中数据 复制到  渲染器的视频缓冲区
SDL_RenderCopy(ren, tex, NULL, NULL);
SDL_RenderPresent(ren);//刷新渲染器
SDL_Delay(2000);
SDL_DestroyTexture(tex);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
 
cout << "Success!" << endl;
return 0;
}

1 SDL初始化和SDL退出:

SDL_Init(SDL_INIT_VIDEO);



SDL_Quit();


2创建Window和销毁窗口:

SDL_Window *window = SDL_CreateWindow("YourGame", 

SDL_WINDOWPOS_UNDEFINED, 

SDL_WINDOWPOS_UNDEFINED, 

screen_width, screen_height, 

SDL_WINDOW_SHOWN);


SDL_DestroyWindow(window);


3让窗口显示图片:

SDL_Surface *surface = SDL_GetWindowSurface(window);

SDL_Surface* blackground_surface = SDL_LoadBMP("Hello.bmp");

SDL_BlitSurface(blackground_surface, NULL, surface, NULL);

SDL_UpdateWindowSurface(window)


4让窗口停留3秒:SDL_Delay(3000);


image.png

初始化函数SDL_Init参数的意义: (initialize the SDL library)

Value Description

SDL_INIT_TIMER timer subsystem

SDL_INIT_AUDIO audio subsystem

SDL_INIT_VIDEO video subsystem; automatically initializes the events subsystem

SDL_INIT_JOYSTICK joystick subsystem; automatically initializes the events subsystem

SDL_INIT_HAPTIC haptic (force feedback) subsystem

SDL_INIT_GAMECONTROLLER controller subsystem; automatically initializes the joystick subsystem

SDL_INIT_EVENTS events subsystem

SDL_INIT_EVERYTHING all of the above subsystems

SDL_INIT_NOPARACHUTE compatibility; this flag is ignored

创建函数SDL_CreateWindow参数的意义: (create a window)

Parameter Description

title the title of the window, in UTF-8 encoding

x the x position of the window, SDL_WINDOWPOS_CENTERED, or SDL_WINDOWPOS_UNDEFINED

y the y position of the window, SDL_WINDOWPOS_CENTERED, or SDL_WINDOWPOS_UNDEFINED

w the width of the window, in screen coordinates

h the height of the window, in screen coordinates

flags 0, or one or more SDL_WindowFlags OR’d together; see Remarks for details

Value of flags Description

SDL_WINDOW_FULLSCREEN fullscreen window

SDL_WINDOW_FULLSCREEN_DESKTOP fullscreen window at the current desktop resolution

SDL_WINDOW_OPENGL window usable with OpenGL context

SDL_WINDOW_VULKAN window usable with a Vulkan instance

SDL_WINDOW_HIDDEN window is not visible

SDL_WINDOW_BORDERLESS no window decoration

SDL_WINDOW_RESIZABLE window can be resized

SDL_WINDOW_MINIMIZED window is minimized

SDL_WINDOW_MAXIMIZED window is maximized

SDL_WINDOW_INPUT_GRABBED window has grabbed input focus

SDL_WINDOW_ALLOW_HIGHDPI window should be created in high-DPI mode if supported (>= SDL 2.0.1)


好文章,来自【福优学苑@音视频+流媒体】
***【在线视频教程】***