Property definitions

egl $ EGLDisplay :: defaultinit
extern class EGLDisplay `{ EGLDisplay `}
	new current `{ return eglGetCurrentDisplay(); `}
	new(handle: Pointer) `{ return eglGetDisplay(handle); `}

	fun is_valid: Bool `{ return self != EGL_NO_DISPLAY; `}

	fun initialize: Bool `{
		return eglInitialize(self, NULL, NULL);
	`}

	fun major_version: Int `{
		EGLint val;
		eglInitialize(self, &val, NULL);
		return val;
	`}
	fun minor_version: Int `{
		EGLint val;
		eglInitialize(self, NULL, &val);
		return val;
	`}

	# TODO if useful
	# Returns all configs supported by the hardware
	#fun get_configs: nullable Array[EGLConfig] import Array[EGLConfig].with_native `{

	# Returns some configs compatible with the specified `attributes`
	fun choose_configs(attribs: Array[Int]): nullable Array[EGLConfig] import Array[Int].length, Array[Int].[], Array[EGLConfig], Array[EGLConfig].add, Array[EGLConfig].as nullable, report_egl_error `{
		EGLConfig *configs;
		int n_configs;
		int n_attribs = Array_of_Int_length(attribs);
		int i;
		int *c_attribs = malloc(sizeof(int)*n_attribs);
		for (i = 0; i < n_attribs; i ++) {
			c_attribs[i] = Array_of_Int__index(attribs, i);
		}

		// get number of configs
		EGLBoolean r = eglChooseConfig(self, c_attribs, NULL, 0, &n_configs);

		if (r == EGL_FALSE) {
			EGLDisplay_report_egl_error(self, "failed to get number of available configs.");
			return null_Array_of_EGLConfig();
		} else if (n_configs == 0) {
			EGLDisplay_report_egl_error(self, "no config available.");
			return null_Array_of_EGLConfig();
		}

		configs = (EGLConfig*)malloc(sizeof(EGLConfig)*n_configs);

		r = eglChooseConfig(self, c_attribs, configs, n_configs, &n_configs);

		if (r == EGL_FALSE) {
			EGLDisplay_report_egl_error(self, "failed to load config.");
			return null_Array_of_EGLConfig();
		} else {
			Array_of_EGLConfig array = new_Array_of_EGLConfig();
			for (i=0; i < n_configs; i++)
				Array_of_EGLConfig_add(array, configs[i]);

			return Array_of_EGLConfig_as_nullable(array);
		}
	`}

	private fun report_egl_error(cmsg: CString)
	do
		var msg = cmsg.to_s
		print "libEGL error: {msg}"
	end

	# Can be used directly, but it is preferable to use a `EGLConfigAttribs`
	fun config_attrib(config: EGLConfig, attribute: Int): Int `{
		EGLint val;
		EGLBoolean r = eglGetConfigAttrib(self, config, attribute, &val);
		if (r == EGL_FALSE)
			return -1;
		else
			return val;
	`}

	fun terminate: Bool `{
		return eglTerminate(self) == EGL_TRUE;
	`}

	fun create_window_surface(config: EGLConfig, native_window: Pointer, attribs: Array[Int]): EGLSurface `{
		EGLSurface surface = eglCreateWindowSurface(self, config, (EGLNativeWindowType)native_window, NULL);
		return surface;
	`}

	# TODO add share_context
	fun create_context(config: EGLConfig): EGLContext `{
		EGLint context_attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE}; // TODO move out!
		EGLContext context = eglCreateContext(self, config, EGL_NO_CONTEXT, context_attribs);
		return context;
	`}

	fun make_current(draw, read: EGLSurface, context: EGLContext): Bool `{
		return eglMakeCurrent(self, draw, read, context);
	`}

	# Can be used directly, but it is preferable to use a `EGLSurfaceAttribs`
	fun query_surface(surface: EGLSurface, attribute: Int): Int `{
		int val;
		EGLBoolean r = eglQuerySurface(self, surface, attribute, &val);
		if (r == EGL_FALSE)
			return -1;
		else
			return val;
	`}

	fun destroy_context(context: EGLContext): Bool `{
		return eglDestroyContext(self, context);
	`}

	fun destroy_surface(surface: EGLSurface): Bool `{
		return eglDestroySurface(self, surface);
	`}

	fun error: EGLError `{ return eglGetError(); `}

	# Utility method for easier debugging
	fun assert_no_egl_error
	do
		var error = self.error
		if not error.is_success then
			print "EGL error: {error}"
			abort
		end
	end

	private fun query_string(name: Int): String import CString.to_s `{
		return CString_to_s((char *)eglQueryString(self, name));
	`}

	fun vendor: String do return query_string(0x3053)

	fun version: String do return query_string(0x3054)

	fun extensions: Array[String] do return query_string(0x3055).trim.split_with(" ")

	fun client_apis: Array[String] do return query_string(0x308D).split_with(" ")

	fun swap_buffers(surface: EGLSurface) `{ eglSwapBuffers(self, surface); `}
end
lib/egl/egl.nit:38,1--180,3