Ejemplo n.º 1
0
	//====================
	// Methods
	//==================== 
	////////////////////////////////////////////////////////////
	bool Window::create(const std::string& title, const Vector2i& position, const Vector2i& size, const ContextSettings_t& settings)
	{
		m_title = title;
		m_position = position;
		m_size = size;

		if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO))
		{
			log.error(log.function(__FUNCTION__, title, position, size), "SDL failed to initialize:", SDL_GetError());
			return false;
		}

		if (IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG) == 0)
		{
			log.error(log.function(__FUNCTION__, title, position, size), "SDL image failed to initialize:", IMG_GetError());
			return false;
		}

		m_pWindow = SDL_CreateWindow(m_title.c_str(),
			                         m_position.x,
			                         m_position.y,
			                         m_size.x,
			                         m_size.y,
			                         SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);

		if (!m_pWindow)
		{
			log.error(log.function(title, position, size), "Failed to create window:", SDL_GetError());
			return false;
		}

		m_context = SDL_GL_CreateContext(m_pWindow);

		SDL_GL_SetSwapInterval(1);
		SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
		SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
		SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
		SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, m_settings.depthBits);
		SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, m_settings.stencilBits);
		SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, m_settings.majorVersion);
		SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, m_settings.minorVersion);

		m_running = true;
		if (!m_pMain)
		{
            glewExperimental = GL_TRUE; 
			GLenum error = glewInit();

			if (error != GLEW_OK)
			{
				log.error(log.function(__FUNCTION__, title, position, size), "GLEW failed to initialize:", glewGetErrorString(error));
				return false;
			}

			glEnable(GL_BLEND);
			glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

            log.debug(log.function(__FUNCTION__, title, position, size), "Main window set.");
			m_pMain = this;
		}

		glEnable(GL_DEPTH_TEST);

		log.debug(log.function(__FUNCTION__, title, position, size), "Created successfully.");
		return true;
	}