先给出一个有趣的例子,它会在窗口标题栏上循环打印不同的标题:
-
#include "SDL.h"
-
-
int main(int argc, char* argv[])
-
{
-
-
SDL_Window *window;
-
SDL_Event e;
-
-
const char *titles[] = { // just for fun, let's make the title animate like a marquee and annoy users
-
"t", "thi", "this w", "this win", "this windo", "this window's", "this window's ti", "this window's title",
-
"chis window's title is", "chih window's title is ", "chih wandnw's title is ", "c h wandnw'g title is ",
-
"c h a nw'g titln is ", "c h a n g i n ig ", "c h a n g i n g!", "",
-
"c h a n g i n g!", "", "c h a n g i n g!", "c h a n g i n g!"
-
};
-
-
SDL_Init(SDL_INIT_VIDEO); // Init SDL2
-
-
// Create a window.
-
window = SDL_CreateWindow(
-
"This will surely be overwritten", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 240, SDL_WINDOW_RESIZABLE
-
);
-
-
// Enter the main loop. Press any key or hit the x to exit.
-
for( ; e.type!=SDL_QUIT&&e.type!=SDL_KEYDOWN; SDL_PollEvent(&e)){
-
static int i = 0, t = 0;
-
-
if(!(++t%9)){ // every 9th frame...
-
SDL_SetWindowTitle(window, titles[i]); // loop through the
-
if(++i >= sizeof(titles)/sizeof(titles[0])) i = 0; // array of titles
-
}
-
-
SDL_Delay(10);
-
-
}
-
-
SDL_DestroyWindow(window);
-
SDL_Quit();
-
return 0;
-
-
}
运行效果图:
以前的代码都是基于1.2版本的,比较旧,而且代码功能上也不及2.0,这个是2.0的一个小框架。算是入门的代码,可以先学习!
2.0在1.2基础上做了很多优化,2.0在显示方面尽可能获得硬件加速支持。官网上的文档写得是非常详细: ,随时可以上去查询。
阅读(6148) | 评论(0) | 转发(0) |