OpenGL的SDL2接入程序

SDL2做为OpenGL的窗口程序,其接入程序为:

#include 
#include 
#include 
#include 
#include 
#include 
#include 

// 控制显示的帧渲染
bool quit; 

SDL_Window* window;
SDL_GLContext glContext;
SDL_Event sdlEvent;

// 窗口的尺寸
const GLuint WIDTH = 1280, HEIGHT = 720;

int main(int argc, char *argv[])
{
	quit = false;
	//将openGL设置为3.1 core
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

	SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
	SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);

	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		// Display error message
		printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
		return -1;
	}
	else
	{
		// Create window
		window = SDL_CreateWindow("Hello World!", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
		if (window == NULL)
		{
			// Display error message
			printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
			return -1;
		}
		else
		{
			// Create OpenGL context
			glContext = SDL_GL_CreateContext(window);

			if (glContext == NULL)
			{
				// Display error message
				printf("OpenGL context could not be created! SDL Error: %s\n", SDL_GetError());
				return -1;
			}

		}
	}
	//初始化glew,相当于初始化OpenGL
	glewExperimental = GL_TRUE;
	glewInit();

	glViewport(0, 0, WIDTH, HEIGHT);
	glEnable(GL_DEPTH_TEST);

	//文本输出(还不知道怎么输入)
	SDL_StartTextInput();
	//帧循环显示
	while (!quit)
	{
		while (SDL_PollEvent(&sdlEvent) != 0)
		{
			if (sdlEvent.type == SDL_QUIT)
			{
				quit = true;
			}
		}
		glClearColor(0.39f, 0.58f, 0.93f, 1.f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		//刷新屏幕
		SDL_GL_SwapWindow(window);
	}
	//清空窗口
	SDL_DestroyWindow(window);
	window = NULL;
	SDL_Quit();
	SDL_StopTextInput();
	return 0;
}



你可能感兴趣的:(OpenGL的SDL2接入程序)