当前位置:网站首页>Sdl2 concise tutorial (2): create an empty window
Sdl2 concise tutorial (2): create an empty window
2022-07-22 04:46:00 【Helplessness of mustard】
List of articles
SDL2 A concise tutorial ( One ): Use Cmake and Conan structure SDL2 Programming environment
SDL2 A concise tutorial ( Two ): Create an empty window
List of articles
Create an empty window
In the last article , We learned through CMake + Conan introduce SDL2 Library programming . Today we go on SDL2 teaching , Learn how to create an empty window .
Creating an empty window is very simple , Just a few lines of code :
#if defined(__cplusplus)
extern "C" {
#endif
#include <SDL.h>
#if defined(__cplusplus)
};
#endif
int main(int argc, char ** argv)
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window * screen = SDL_CreateWindow("My SDL Empty Window",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
SDL_Quit();
return 0;
}
In the above code :
SDL_Init
initialization SDL, It tells SDL Which subsystems are needed , In this caseSDL_INIT_VIDEO
It means that video is what we need- Call at the end of the code
SDL_Quit
sign out SDL, Clean up related memory and resources SDL_CreateWindow
Used to create a window , We will the title of the window , Initialization position , The width and height of the window and flags tell SDL, It is responsible for generating a window
If you run the above code , It works , But the window stay time is very short , Cease ~ It just disappeared . We can add some code , Keep the window until it is actively closed :
#if defined(__cplusplus)
extern "C" {
#endif
#include <SDL.h>
#if defined(__cplusplus)
};
#endif
int main(int argc, char *argv[]) {
bool quit = false;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *screen = SDL_CreateWindow("My SDL Empty window",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640, 480, 0);
for (; !quit;) {
SDL_WaitEvent(&event);
switch (event.type) {
case SDL_QUIT: {
quit = true;
break;
}
}
}
SDL_Quit();
return 0;
}
for(;!quit;)
Indicates that the code is looping indefinitely until the exit condition is met .
We use SDL_WaitEvent()
To wait for an event to happen ( For example, press the keyboard ), At the same time, we will have a SDL_Event
The structure is put into it . You can still use it SDL_PollEvent()
To continuously acquire events , But it will consume a lot of CPU cycle .(SDL_WaitEvent()
Basically, I'm sleeping , Know that an event happened , In general, it is lighter ).
Events tell you what happened , It may be a key 、 Roller sliding 、 Mouse movement 、 Touch interaction and so on . In our example , We are only right SDL_QUIT
Events of interest , This means that the user clicked the close button of the form .
OK, Now run the code , You can get an empty form , It will exist until you close it :
Ha ha ha , It's very simple . You can start with this , Start drawing things in your window . Have a good time , Come back to more tutorials !
summary
This article explains how to use it SDL2 Create an empty window , You can specify the position of the window , Size and other information . To keep the window alive , We introduce an infinite loop until the exit condition is satisfied , Close the window and trigger SDL_QUIT
event , adopt SDL_WaitEvent
Wait for this event , Then close the window .
The source code of this article can be found in sdl2_tutorial find .
边栏推荐
猜你喜欢
从底层了解JVM的volatile实现,CPU Cache、缓存一致性MESI、Store BufferI、nvalidate Queue等知识
软件供应链攻击的新形式
【FPGA教程案例33】通信案例3——基于FPGA的BPSK调制信号产生,通过matlab测试其星座图
深入理解Istio流量管理的超时时间设置
300. Longest increasing subsequence
05mysql的锁分析
Medical cell image segmentation
【BSP视频教程】BSP视频教程第20期:串口专题之玩转HAL库,LL库和寄存器方式实现方法以及参考手册几个关键时序图学习(2022-07-16)
Welcome to the CSDN markdown editor template
年中盘点 | 2022年,PaaS 再升级
随机推荐
322. Change
Step by step introduction to the development framework based on sqlsugar (12) -- split the content of the page module into components to realize the division and rule processing
Number和Math类的常用方法
go-系统监控
马斯克热搜体质无疑,称已将大脑上传云端,却遭网友热议!
学界VS工业界:深度学习究竟能不能打破视频编解码天花板
软考 系统架构设计师 简明教程 | 软件开发方法
多态,抽象类,接口
深入理解Istio流量管理的超时时间设置
A simple exploration of Suricata
HOT100 word split
Nacos相关概念小总结
【BSP视频教程】BSP视频教程第20期:串口专题之玩转HAL库,LL库和寄存器方式实现方法以及参考手册几个关键时序图学习(2022-07-16)
风控文章资源合集
When the neural network model is not as good as the decision tree
4.基本类型和引用类型?
steam API
Mysql数据库入门
Implement printf function by yourself (serial port printing)
How to recover the problems that appear when you first write a blog - use the CSDN markdown editor template