From 4a34bc515cc328601946c60f140fee74fabb8de1 Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Wed, 27 Jun 2018 07:05:34 -0700 Subject: [PATCH] Finish first pass on refactor render --- docs/api/ImportAll.hx | 1 - lime/_backend/html5/HTML5GLRenderContext.hx | 3 +- lime/_backend/html5/HTML5Window.hx | 27 +- lime/_backend/native/NativeApplication.hx | 42 +- lime/_backend/native/NativeCFFI.hx | 68 ++-- lime/_backend/native/NativeGLRenderContext.hx | 9 +- lime/_backend/native/NativeRenderer.hx | 265 ------------- lime/_backend/native/NativeWindow.hx | 242 ++++++++++++ lime/graphics/GLES2RenderContext.hx | 3 + lime/graphics/GLES3RenderContext.hx | 5 +- lime/graphics/GLRenderContext.hx | 3 +- lime/graphics/RenderContext.hx | 12 +- lime/graphics/WebGL2RenderContext.hx | 5 +- lime/graphics/opengl/GL.hx | 4 +- lime/graphics/opengl/GLContextType.hx | 10 - project/Build.xml | 1 - project/include/graphics/Renderer.h | 42 -- project/include/ui/Window.h | 60 +-- project/src/ExternalInterface.cpp | 357 ++++++++--------- project/src/backend/sdl/SDLRenderer.cpp | 365 ------------------ project/src/backend/sdl/SDLRenderer.h | 44 --- project/src/backend/sdl/SDLWindow.cpp | 365 +++++++++++++++++- project/src/backend/sdl/SDLWindow.h | 16 + 23 files changed, 875 insertions(+), 1074 deletions(-) delete mode 100644 lime/_backend/native/NativeRenderer.hx delete mode 100644 lime/graphics/opengl/GLContextType.hx delete mode 100644 project/include/graphics/Renderer.h delete mode 100644 project/src/backend/sdl/SDLRenderer.cpp delete mode 100644 project/src/backend/sdl/SDLRenderer.h diff --git a/docs/api/ImportAll.hx b/docs/api/ImportAll.hx index e334a92cf..c5f315992 100644 --- a/docs/api/ImportAll.hx +++ b/docs/api/ImportAll.hx @@ -36,7 +36,6 @@ import lime.graphics.opengl.GL; import lime.graphics.opengl.GLActiveInfo; import lime.graphics.opengl.GLBuffer; import lime.graphics.opengl.GLContextAttributes; -import lime.graphics.opengl.GLContextType; import lime.graphics.opengl.GLES2Context; import lime.graphics.opengl.GLES3Context; import lime.graphics.opengl.GLFramebuffer; diff --git a/lime/_backend/html5/HTML5GLRenderContext.hx b/lime/_backend/html5/HTML5GLRenderContext.hx index ac32075d5..55b9be2c6 100644 --- a/lime/_backend/html5/HTML5GLRenderContext.hx +++ b/lime/_backend/html5/HTML5GLRenderContext.hx @@ -7,6 +7,7 @@ import js.html.webgl.RenderingContext in WebGLRenderingContext; import js.html.CanvasElement; import js.Browser; import lime.graphics.opengl.*; +import lime.graphics.RenderContextType; import lime.utils.ArrayBuffer; import lime.utils.ArrayBufferView; import lime.utils.BytePointer; @@ -652,7 +653,7 @@ class HTML5GLRenderContext { public var drawingBufferWidth (get, never):Int; #end - public var type:GLContextType; + public var type:RenderContextType; public var version (default, null):Int; private var __context:WebGL2RenderingContext; diff --git a/lime/_backend/html5/HTML5Window.hx b/lime/_backend/html5/HTML5Window.hx index 69f3ff03b..6b0f87680 100644 --- a/lime/_backend/html5/HTML5Window.hx +++ b/lime/_backend/html5/HTML5Window.hx @@ -18,6 +18,7 @@ import js.Browser; import lime.app.Application; import lime.graphics.opengl.GL; import lime.graphics.utils.ImageCanvasUtil; +import lime.graphics.GLRenderContext; import lime.graphics.Image; import lime.graphics.RenderContext; import lime.math.Rectangle; @@ -31,7 +32,9 @@ import lime.ui.Touch; import lime.ui.Window; +@:access(lime._backend.html5.HTML5GLRenderContext) @:access(lime.app.Application) +@:access(lime.graphics.opengl.GL) @:access(lime.graphics.RenderContext) @:access(lime.ui.Gamepad) @:access(lime.ui.Joystick) @@ -329,15 +332,23 @@ class HTML5Window { webgl = untyped WebGLDebugUtils.makeDebugContext (webgl); #end - // #if ((js && html5) && !display) - // GL.context = new GLRenderContext (cast webgl); - // parent.context = OPENGL (GL.context); - // #else - // parent.context = OPENGL (new GLRenderContext ()); - // #end + #if ((js && html5) && !display) + var gl = new GLRenderContext (cast webgl); + context.gl = gl; + context.webgl = gl; + if (isWebGL2) context.webgl2 = gl; + #else + var gl = new GLRenderContext (); + context.gl = gl; + context.webgl = gl; + #end + + if (GL.context == null) { + + GL.context = gl; + + } - // context.webgl = cast webgl; - // if (isWebGL2) context.webgl2 = cast webgl; context.type = WEBGL; context.version = isWebGL2 ? "2" : "1"; diff --git a/lime/_backend/native/NativeApplication.hx b/lime/_backend/native/NativeApplication.hx index de7c324b9..ddd31ccf9 100644 --- a/lime/_backend/native/NativeApplication.hx +++ b/lime/_backend/native/NativeApplication.hx @@ -10,7 +10,6 @@ import lime.graphics.opengl.GL; import lime.graphics.ConsoleRenderContext; import lime.graphics.GLRenderContext; import lime.graphics.RenderContext; -import lime.graphics.Renderer; import lime.math.Rectangle; import lime.system.Clipboard; import lime.system.Display; @@ -35,7 +34,7 @@ import lime.ui.Window; @:access(haxe.Timer) @:access(lime._backend.native.NativeCFFI) @:access(lime._backend.native.NativeGLRenderContext) -@:access(lime._backend.native.NativeRenderer) +@:access(lime._backend.native.NativeWindow) @:access(lime.app.Application) @:access(lime.graphics.opengl.GL) @:access(lime.graphics.GLRenderContext) @@ -436,24 +435,26 @@ class NativeApplication { private function handleRenderEvent ():Void { - for (renderer in parent.renderers) { + // TODO: Allow windows to render independently + + for (window in parent.windows) { - if (renderer == null) continue; + if (window == null) continue; - parent.renderer = renderer; + // parent.renderer = renderer; switch (renderEventInfo.type) { case RENDER: - if (renderer.context != null) { + if (window.context != null) { - renderer.render (); - renderer.onRender.dispatch (); + window.backend.render (); + window.onRender.dispatch (); - if (!renderer.onRender.canceled) { + if (!window.onRender.canceled) { - renderer.flip (); + window.backend.contextFlip (); } @@ -461,13 +462,14 @@ class NativeApplication { case RENDER_CONTEXT_LOST: - if (renderer.backend.useHardware && renderer.context != null) { + if (window.backend.useHardware && window.context != null) { - switch (renderer.context) { + switch (window.context.type) { - case OPENGL (gl): + case OPENGL, OPENGLES, WEBGL: #if (lime_cffi && lime_opengl && !display) + var gl = window.context.gl; (gl:NativeGLRenderContext).__contextLost (); if (GL.context == gl) GL.context = null; #end @@ -476,23 +478,23 @@ class NativeApplication { } - renderer.context = null; - renderer.onContextLost.dispatch (); + window.context = null; + window.onContextLost.dispatch (); } case RENDER_CONTEXT_RESTORED: - if (renderer.backend.useHardware) { + if (window.backend.useHardware) { #if lime_console - renderer.context = CONSOLE (ConsoleRenderContext.singleton); + // renderer.context = CONSOLE (ConsoleRenderContext.singleton); #else - GL.context = new GLRenderContext (); - renderer.context = OPENGL (GL.context); + // GL.context = new GLRenderContext (); + // window.context.gl = GL.context; #end - renderer.onContextRestored.dispatch (renderer.context); + window.onContextRestored.dispatch (); } diff --git a/lime/_backend/native/NativeCFFI.hx b/lime/_backend/native/NativeCFFI.hx index 9cde7f200..d01cd60b9 100644 --- a/lime/_backend/native/NativeCFFI.hx +++ b/lime/_backend/native/NativeCFFI.hx @@ -151,15 +151,6 @@ class NativeCFFI { @:cffi private static function lime_neko_execute (module:String):Void; @:cffi private static function lime_png_decode_bytes (data:Dynamic, decodeData:Bool, buffer:Dynamic):Dynamic; @:cffi private static function lime_png_decode_file (path:String, decodeData:Bool, buffer:Dynamic):Dynamic; - @:cffi private static function lime_renderer_create (window:Dynamic):Dynamic; - @:cffi private static function lime_renderer_flip (handle:Dynamic):Void; - @:cffi private static function lime_renderer_get_context (handle:Dynamic):Float; - @:cffi private static function lime_renderer_get_scale (handle:Dynamic):Float; - @:cffi private static function lime_renderer_get_type (handle:Dynamic):Dynamic; - @:cffi private static function lime_renderer_lock (handle:Dynamic):Dynamic; - @:cffi private static function lime_renderer_make_current (handle:Dynamic):Void; - @:cffi private static function lime_renderer_read_pixels (handle:Dynamic, rect:Dynamic, imageBuffer:Dynamic):Dynamic; - @:cffi private static function lime_renderer_unlock (handle:Dynamic):Void; @:cffi private static function lime_render_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void; @:cffi private static function lime_sensor_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void; @:cffi private static function lime_system_get_allow_screen_timeout ():Bool; @@ -185,17 +176,25 @@ class NativeCFFI { @:cffi private static function lime_touch_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void; @:cffi private static function lime_window_alert (handle:Dynamic, message:String, title:String):Void; @:cffi private static function lime_window_close (handle:Dynamic):Void; + @:cffi private static function lime_window_context_flip (handle:Dynamic):Void; + @:cffi private static function lime_window_context_lock (handle:Dynamic):Dynamic; + @:cffi private static function lime_window_context_make_current (handle:Dynamic):Void; + @:cffi private static function lime_window_context_unlock (handle:Dynamic):Void; @:cffi private static function lime_window_create (application:Dynamic, width:Int, height:Int, flags:Int, title:String):Dynamic; @:cffi private static function lime_window_focus (handle:Dynamic):Void; + @:cffi private static function lime_window_get_context (handle:Dynamic):Float; + @:cffi private static function lime_window_get_context_type (handle:Dynamic):Dynamic; @:cffi private static function lime_window_get_display (handle:Dynamic):Int; @:cffi private static function lime_window_get_display_mode (handle:Dynamic):Dynamic; @:cffi private static function lime_window_get_enable_text_events (handle:Dynamic):Bool; @:cffi private static function lime_window_get_height (handle:Dynamic):Int; @:cffi private static function lime_window_get_id (handle:Dynamic):Int; + @:cffi private static function lime_window_get_scale (handle:Dynamic):Float; @:cffi private static function lime_window_get_width (handle:Dynamic):Int; @:cffi private static function lime_window_get_x (handle:Dynamic):Int; @:cffi private static function lime_window_get_y (handle:Dynamic):Int; @:cffi private static function lime_window_move (handle:Dynamic, x:Int, y:Int):Void; + @:cffi private static function lime_window_read_pixels (handle:Dynamic, rect:Dynamic, imageBuffer:Dynamic):Dynamic; @:cffi private static function lime_window_resize (handle:Dynamic, width:Int, height:Int):Void; @:cffi private static function lime_window_set_borderless (handle:Dynamic, borderless:Bool):Bool; @:cffi private static function lime_window_set_display_mode (handle:Dynamic, displayMode:Dynamic):Dynamic; @@ -305,15 +304,6 @@ class NativeCFFI { private static var lime_neko_execute = new cpp.Callablecpp.Void> (cpp.Prime._loadPrime ("lime", "lime_neko_execute", "sv", false)); private static var lime_png_decode_bytes = new cpp.CallableBool->cpp.Object->cpp.Object> (cpp.Prime._loadPrime ("lime", "lime_png_decode_bytes", "oboo", false)); private static var lime_png_decode_file = new cpp.CallableBool->cpp.Object->cpp.Object> (cpp.Prime._loadPrime ("lime", "lime_png_decode_file", "sboo", false)); - private static var lime_renderer_create = new cpp.Callablecpp.Object> (cpp.Prime._loadPrime ("lime", "lime_renderer_create", "oo", false)); - private static var lime_renderer_flip = new cpp.Callablecpp.Void> (cpp.Prime._loadPrime ("lime", "lime_renderer_flip", "ov", false)); - private static var lime_renderer_get_context = new cpp.CallableFloat> (cpp.Prime._loadPrime ("lime", "lime_renderer_get_context", "od", false)); - private static var lime_renderer_get_scale = new cpp.CallableFloat> (cpp.Prime._loadPrime ("lime", "lime_renderer_get_scale", "od", false)); - private static var lime_renderer_get_type = new cpp.Callablecpp.Object> (cpp.Prime._loadPrime ("lime", "lime_renderer_get_type", "oo", false)); - private static var lime_renderer_lock = new cpp.Callablecpp.Object> (cpp.Prime._loadPrime ("lime", "lime_renderer_lock", "oo", false)); - private static var lime_renderer_make_current = new cpp.Callablecpp.Void> (cpp.Prime._loadPrime ("lime", "lime_renderer_make_current", "ov", false)); - private static var lime_renderer_read_pixels = new cpp.Callablecpp.Object->cpp.Object->cpp.Object> (cpp.Prime._loadPrime ("lime", "lime_renderer_read_pixels", "oooo", false)); - private static var lime_renderer_unlock = new cpp.Callablecpp.Void> (cpp.Prime._loadPrime ("lime", "lime_renderer_unlock", "ov", false)); private static var lime_render_event_manager_register = new cpp.Callablecpp.Object->cpp.Void> (cpp.Prime._loadPrime ("lime", "lime_render_event_manager_register", "oov", false)); private static var lime_sensor_event_manager_register = new cpp.Callablecpp.Object->cpp.Void> (cpp.Prime._loadPrime ("lime", "lime_sensor_event_manager_register", "oov", false)); private static var lime_system_get_allow_screen_timeout = new cpp.CallableBool> (cpp.Prime._loadPrime ("lime", "lime_system_get_allow_screen_timeout", "b", false)); @@ -339,17 +329,25 @@ class NativeCFFI { private static var lime_touch_event_manager_register = new cpp.Callablecpp.Object->cpp.Void> (cpp.Prime._loadPrime ("lime", "lime_touch_event_manager_register", "oov", false)); private static var lime_window_alert = new cpp.CallableString->String->cpp.Void> (cpp.Prime._loadPrime ("lime", "lime_window_alert", "ossv", false)); private static var lime_window_close = new cpp.Callablecpp.Void> (cpp.Prime._loadPrime ("lime", "lime_window_close", "ov", false)); + private static var lime_window_context_flip = new cpp.Callablecpp.Void> (cpp.Prime._loadPrime ("lime", "lime_window_context_flip", "ov", false)); + private static var lime_window_context_lock = new cpp.Callablecpp.Object> (cpp.Prime._loadPrime ("lime", "lime_window_context_lock", "oo", false)); + private static var lime_window_context_make_current = new cpp.Callablecpp.Void> (cpp.Prime._loadPrime ("lime", "lime_window_context_make_current", "ov", false)); + private static var lime_window_context_unlock = new cpp.Callablecpp.Void> (cpp.Prime._loadPrime ("lime", "lime_window_context_unlock", "ov", false)); private static var lime_window_create = new cpp.CallableInt->Int->Int->String->cpp.Object> (cpp.Prime._loadPrime ("lime", "lime_window_create", "oiiiso", false)); private static var lime_window_focus = new cpp.Callablecpp.Void> (cpp.Prime._loadPrime ("lime", "lime_window_focus", "ov", false)); + private static var lime_window_get_context = new cpp.CallableFloat> (cpp.Prime._loadPrime ("lime", "lime_window_get_context", "od", false)); + private static var lime_window_get_context_type = new cpp.Callablecpp.Object> (cpp.Prime._loadPrime ("lime", "lime_window_get_context_type", "oo", false)); private static var lime_window_get_display = new cpp.CallableInt> (cpp.Prime._loadPrime ("lime", "lime_window_get_display", "oi", false)); private static var lime_window_get_display_mode = new cpp.Callablecpp.Object> (cpp.Prime._loadPrime ("lime", "lime_window_get_display_mode", "oo", false)); private static var lime_window_get_enable_text_events = new cpp.CallableBool> (cpp.Prime._loadPrime ("lime", "lime_window_get_enable_text_events", "ob", false)); private static var lime_window_get_height = new cpp.CallableInt> (cpp.Prime._loadPrime ("lime", "lime_window_get_height", "oi", false)); private static var lime_window_get_id = new cpp.CallableInt> (cpp.Prime._loadPrime ("lime", "lime_window_get_id", "oi", false)); + private static var lime_window_get_scale = new cpp.CallableFloat> (cpp.Prime._loadPrime ("lime", "lime_window_get_scale", "od", false)); private static var lime_window_get_width = new cpp.CallableInt> (cpp.Prime._loadPrime ("lime", "lime_window_get_width", "oi", false)); private static var lime_window_get_x = new cpp.CallableInt> (cpp.Prime._loadPrime ("lime", "lime_window_get_x", "oi", false)); private static var lime_window_get_y = new cpp.CallableInt> (cpp.Prime._loadPrime ("lime", "lime_window_get_y", "oi", false)); private static var lime_window_move = new cpp.CallableInt->Int->cpp.Void> (cpp.Prime._loadPrime ("lime", "lime_window_move", "oiiv", false)); + private static var lime_window_read_pixels = new cpp.Callablecpp.Object->cpp.Object->cpp.Object> (cpp.Prime._loadPrime ("lime", "lime_window_read_pixels", "oooo", false)); private static var lime_window_resize = new cpp.CallableInt->Int->cpp.Void> (cpp.Prime._loadPrime ("lime", "lime_window_resize", "oiiv", false)); private static var lime_window_set_borderless = new cpp.CallableBool->Bool> (cpp.Prime._loadPrime ("lime", "lime_window_set_borderless", "obb", false)); private static var lime_window_set_display_mode = new cpp.Callablecpp.Object->cpp.Object> (cpp.Prime._loadPrime ("lime", "lime_window_set_display_mode", "ooo", false)); @@ -462,15 +460,6 @@ class NativeCFFI { private static var lime_neko_execute = CFFI.load ("lime", "lime_neko_execute", 1); private static var lime_png_decode_bytes = CFFI.load ("lime", "lime_png_decode_bytes", 3); private static var lime_png_decode_file = CFFI.load ("lime", "lime_png_decode_file", 3); - private static var lime_renderer_create = CFFI.load ("lime", "lime_renderer_create", 1); - private static var lime_renderer_flip = CFFI.load ("lime", "lime_renderer_flip", 1); - private static var lime_renderer_get_context = CFFI.load ("lime", "lime_renderer_get_context", 1); - private static var lime_renderer_get_scale = CFFI.load ("lime", "lime_renderer_get_scale", 1); - private static var lime_renderer_get_type = CFFI.load ("lime", "lime_renderer_get_type", 1); - private static var lime_renderer_lock = CFFI.load ("lime", "lime_renderer_lock", 1); - private static var lime_renderer_make_current = CFFI.load ("lime", "lime_renderer_make_current", 1); - private static var lime_renderer_read_pixels = CFFI.load ("lime", "lime_renderer_read_pixels", 3); - private static var lime_renderer_unlock = CFFI.load ("lime", "lime_renderer_unlock", 1); private static var lime_render_event_manager_register = CFFI.load ("lime", "lime_render_event_manager_register", 2); private static var lime_sensor_event_manager_register = CFFI.load ("lime", "lime_sensor_event_manager_register", 2); private static var lime_system_get_allow_screen_timeout = CFFI.load ("lime", "lime_system_get_allow_screen_timeout", 0); @@ -496,17 +485,25 @@ class NativeCFFI { private static var lime_touch_event_manager_register = CFFI.load ("lime", "lime_touch_event_manager_register", 2); private static var lime_window_alert = CFFI.load ("lime", "lime_window_alert", 3); private static var lime_window_close = CFFI.load ("lime", "lime_window_close", 1); + private static var lime_window_context_flip = CFFI.load ("lime", "lime_window_context_flip", 1); + private static var lime_window_context_lock = CFFI.load ("lime", "lime_window_context_lock", 1); + private static var lime_window_context_make_current = CFFI.load ("lime", "lime_window_context_make_current", 1); + private static var lime_window_context_unlock = CFFI.load ("lime", "lime_window_context_unlock", 1); private static var lime_window_create = CFFI.load ("lime", "lime_window_create", 5); private static var lime_window_focus = CFFI.load ("lime", "lime_window_focus", 1); + private static var lime_window_get_context = CFFI.load ("lime", "lime_window_get_context", 1); + private static var lime_window_get_context_type = CFFI.load ("lime", "lime_window_get_context_type", 1); private static var lime_window_get_display = CFFI.load ("lime", "lime_window_get_display", 1); private static var lime_window_get_display_mode = CFFI.load ("lime", "lime_window_get_display_mode", 1); private static var lime_window_get_enable_text_events = CFFI.load ("lime", "lime_window_get_enable_text_events", 1); private static var lime_window_get_height = CFFI.load ("lime", "lime_window_get_height", 1); private static var lime_window_get_id = CFFI.load ("lime", "lime_window_get_id", 1); + private static var lime_window_get_scale = CFFI.load ("lime", "lime_window_get_scale", 1); private static var lime_window_get_width = CFFI.load ("lime", "lime_window_get_width", 1); private static var lime_window_get_x = CFFI.load ("lime", "lime_window_get_x", 1); private static var lime_window_get_y = CFFI.load ("lime", "lime_window_get_y", 1); private static var lime_window_move = CFFI.load ("lime", "lime_window_move", 3); + private static var lime_window_read_pixels = CFFI.load ("lime", "lime_window_read_pixels", 3); private static var lime_window_resize = CFFI.load ("lime", "lime_window_resize", 3); private static var lime_window_set_borderless = CFFI.load ("lime", "lime_window_set_borderless", 2); private static var lime_window_set_display_mode = CFFI.load ("lime", "lime_window_set_display_mode", 2); @@ -623,15 +620,6 @@ class NativeCFFI { // @:cffi private static function lime_neko_execute (module:String):Void; @:hlNative("lime", "lime_png_decode_bytes") private static function lime_png_decode_bytes (data:Bytes, decodeData:Bool, buffer:ImageBuffer):ImageBuffer { return null; } @:hlNative("lime", "lime_png_decode_file") private static function lime_png_decode_file (path:String, decodeData:Bool, buffer:ImageBuffer):ImageBuffer { return null; } - @:hlNative("lime", "lime_renderer_create") private static function lime_renderer_create (window:CFFIPointer):CFFIPointer { return null; } - @:hlNative("lime", "lime_renderer_flip") private static function lime_renderer_flip (handle:CFFIPointer):Void {} - @:hlNative("lime", "lime_renderer_get_context") private static function lime_renderer_get_context (handle:CFFIPointer):Float { return 0; } - @:hlNative("lime", "lime_renderer_get_scale") private static function lime_renderer_get_scale (handle:CFFIPointer):Float { return 0; } - @:hlNative("lime", "lime_renderer_get_type") private static function lime_renderer_get_type (handle:CFFIPointer):hl.Bytes { return null; } - @:hlNative("lime", "lime_renderer_lock") private static function lime_renderer_lock (handle:CFFIPointer, object:Dynamic):Dynamic { return null; } - @:hlNative("lime", "lime_renderer_make_current") private static function lime_renderer_make_current (handle:CFFIPointer):Void {} - @:hlNative("lime", "lime_renderer_read_pixels") private static function lime_renderer_read_pixels (handle:CFFIPointer, rect:Rectangle, imageBuffer:ImageBuffer):Dynamic { return null; } - @:hlNative("lime", "lime_renderer_unlock") private static function lime_renderer_unlock (handle:CFFIPointer):Void {} @:hlNative("lime", "lime_render_event_manager_register") private static function lime_render_event_manager_register (callback:Void->Void, eventObject:RenderEventInfo):Void {} @:hlNative("lime", "lime_sensor_event_manager_register") private static function lime_sensor_event_manager_register (callback:Void->Void, eventObject:SensorEventInfo):Void {} @:hlNative("lime", "lime_system_get_allow_screen_timeout") private static function lime_system_get_allow_screen_timeout ():Bool { return false; } @@ -657,17 +645,25 @@ class NativeCFFI { @:hlNative("lime", "lime_touch_event_manager_register") private static function lime_touch_event_manager_register (callback:Void->Void, eventObject:TouchEventInfo):Void {} @:hlNative("lime", "lime_window_alert") private static function lime_window_alert (handle:CFFIPointer, message:String, title:String):Void {} @:hlNative("lime", "lime_window_close") private static function lime_window_close (handle:CFFIPointer):Void {} + @:hlNative("lime", "lime_window_context_flip") private static function lime_window_context_flip (handle:CFFIPointer):Void {} + @:hlNative("lime", "lime_window_context_lock") private static function lime_window_context_lock (handle:CFFIPointer, object:Dynamic):Dynamic { return null; } + @:hlNative("lime", "lime_window_context_make_current") private static function lime_window_context_make_current (handle:CFFIPointer):Void {} + @:hlNative("lime", "lime_window_context_unlock") private static function lime_window_context_unlock (handle:CFFIPointer):Void {} @:hlNative("lime", "lime_window_create") private static function lime_window_create (application:CFFIPointer, width:Int, height:Int, flags:Int, title:String):CFFIPointer { return null; } @:hlNative("lime", "lime_window_focus") private static function lime_window_focus (handle:CFFIPointer):Void {} + @:hlNative("lime", "lime_window_get_context") private static function lime_window_get_context (handle:CFFIPointer):Float { return 0; } + @:hlNative("lime", "lime_window_get_context_type") private static function lime_window_get_context_type (handle:CFFIPointer):hl.Bytes { return null; } @:hlNative("lime", "lime_window_get_display") private static function lime_window_get_display (handle:CFFIPointer):Int { return 0; } @:hlNative("lime", "lime_window_get_display_mode") private static function lime_window_get_display_mode (handle:CFFIPointer):Dynamic { return null; } @:hlNative("lime", "lime_window_get_enable_text_events") private static function lime_window_get_enable_text_events (handle:CFFIPointer):Bool { return false; } @:hlNative("lime", "lime_window_get_height") private static function lime_window_get_height (handle:CFFIPointer):Int { return 0; } @:hlNative("lime", "lime_window_get_id") private static function lime_window_get_id (handle:CFFIPointer):Int { return 0; } + @:hlNative("lime", "lime_window_get_scale") private static function lime_window_get_scale (handle:CFFIPointer):Float { return 0; } @:hlNative("lime", "lime_window_get_width") private static function lime_window_get_width (handle:CFFIPointer):Int { return 0; } @:hlNative("lime", "lime_window_get_x") private static function lime_window_get_x (handle:CFFIPointer):Int { return 0; } @:hlNative("lime", "lime_window_get_y") private static function lime_window_get_y (handle:CFFIPointer):Int { return 0; } @:hlNative("lime", "lime_window_move") private static function lime_window_move (handle:CFFIPointer, x:Int, y:Int):Void {} + @:hlNative("lime", "lime_window_read_pixels") private static function lime_window_read_pixels (handle:CFFIPointer, rect:Rectangle, imageBuffer:ImageBuffer):Dynamic { return null; } @:hlNative("lime", "lime_window_resize") private static function lime_window_resize (handle:CFFIPointer, width:Int, height:Int):Void {} @:hlNative("lime", "lime_window_set_borderless") private static function lime_window_set_borderless (handle:CFFIPointer, borderless:Bool):Bool { return false; } @:hlNative("lime", "lime_window_set_display_mode") private static function lime_window_set_display_mode (handle:CFFIPointer, displayMode:DisplayMode):DisplayMode { return null; } diff --git a/lime/_backend/native/NativeGLRenderContext.hx b/lime/_backend/native/NativeGLRenderContext.hx index 3a3795138..8deedb863 100644 --- a/lime/_backend/native/NativeGLRenderContext.hx +++ b/lime/_backend/native/NativeGLRenderContext.hx @@ -12,7 +12,6 @@ import lime.graphics.opengl.ext.*; import lime.graphics.opengl.GLActiveInfo; import lime.graphics.opengl.GLBuffer; import lime.graphics.opengl.GLContextAttributes; -import lime.graphics.opengl.GLContextType; import lime.graphics.opengl.GLFramebuffer; import lime.graphics.opengl.GLProgram; import lime.graphics.opengl.GLRenderbuffer; @@ -21,9 +20,7 @@ import lime.graphics.opengl.GLShaderPrecisionFormat; import lime.graphics.opengl.GLTexture; import lime.graphics.opengl.GLUniformLocation; import lime.graphics.opengl.GL; -import lime.system.System; -import lime.utils.ArrayBuffer; -import lime.utils.ArrayBufferView; +import lime.graphics.RenderContextType; import lime.utils.DataPointer; import lime.utils.Float32Array; import lime.utils.Int32Array; @@ -686,7 +683,7 @@ class NativeGLRenderContext { public var TIMEOUT_IGNORED = -1; public var MAX_CLIENT_WAIT_TIMEOUT_WEBGL = 0x9247; - public var type (default, null):GLContextType; + public var type (default, null):RenderContextType; public var version (default, null):Float; private var __arrayBufferBinding:GLBuffer; @@ -710,7 +707,7 @@ class NativeGLRenderContext { #if (lime_cffi && lime_opengl && !macro) var versionString:String = getParameter (VERSION); if (versionString.indexOf ("OpenGL ES") > -1) { - type = GLES; + type = OPENGLES; } else { type = OPENGL; } diff --git a/lime/_backend/native/NativeRenderer.hx b/lime/_backend/native/NativeRenderer.hx deleted file mode 100644 index 0bd570389..000000000 --- a/lime/_backend/native/NativeRenderer.hx +++ /dev/null @@ -1,265 +0,0 @@ -package lime._backend.native; - - -import haxe.io.Bytes; -import lime.graphics.cairo.Cairo; -import lime.graphics.cairo.CairoFormat; -import lime.graphics.cairo.CairoImageSurface; -import lime.graphics.cairo.CairoSurface; -import lime.graphics.CairoRenderContext; -import lime.graphics.ConsoleRenderContext; -import lime.graphics.GLRenderContext; -import lime.graphics.Image; -import lime.graphics.ImageBuffer; -import lime.graphics.Renderer; -import lime.graphics.opengl.GL; -import lime.math.Rectangle; -import lime.utils.UInt8Array; - -#if !lime_debug -@:fileXml('tags="haxe,release"') -@:noDebug -#end - -@:access(lime._backend.native.NativeCFFI) -@:access(lime._backend.native.NativeGLRenderContext) -@:access(lime.graphics.cairo.Cairo) -@:access(lime.graphics.opengl.GL) -@:access(lime.graphics.GLRenderContext) -@:access(lime.ui.Window) - - -class NativeRenderer { - - - public var handle:Dynamic; - - private var parent:Renderer; - private var useHardware:Bool; - - #if lime_cairo - private var cacheLock:Dynamic; - private var cairo:Cairo; - private var primarySurface:CairoSurface; - #end - - - public function new (parent:Renderer) { - - this.parent = parent; - - } - - - public function create ():Void { - - #if (!macro && lime_cffi) - handle = NativeCFFI.lime_renderer_create (parent.window.backend.handle); - - parent.window.__scale = NativeCFFI.lime_renderer_get_scale (handle); - - #if lime_console - - useHardware = true; - parent.context = CONSOLE (ConsoleRenderContext.singleton); - parent.type = CONSOLE; - - #else - - #if hl - var type = @:privateAccess String.fromUTF8 (NativeCFFI.lime_renderer_get_type (handle)); - #else - var type:String = NativeCFFI.lime_renderer_get_type (handle); - #end - - switch (type) { - - case "opengl": - - var context = new GLRenderContext (); - - useHardware = true; - parent.context = OPENGL (context); - parent.type = OPENGL; - - if (GL.context == null) { - - GL.context = context; - - } - - default: - - useHardware = false; - - #if lime_cairo - render (); - parent.context = CAIRO (cairo); - #end - parent.type = CAIRO; - - } - - #end - #end - - } - - - private function dispatch ():Void { - - - - } - - - public function flip ():Void { - - #if (!macro && lime_cffi) - if (!useHardware) { - - #if lime_cairo - if (cairo != null) { - - primarySurface.flush (); - - } - #end - NativeCFFI.lime_renderer_unlock (handle); - - } - - NativeCFFI.lime_renderer_flip (handle); - #end - - } - - - public function readPixels (rect:Rectangle):Image { - - var imageBuffer:ImageBuffer = null; - - switch (parent.context) { - - case OPENGL (gl): - - var windowWidth = Std.int (parent.window.__width * parent.window.__scale); - var windowHeight = Std.int (parent.window.__height * parent.window.__scale); - - var x, y, width, height; - - if (rect != null) { - - x = Std.int (rect.x); - y = Std.int ((windowHeight - rect.y) - rect.height); - width = Std.int (rect.width); - height = Std.int (rect.height); - - } else { - - x = 0; - y = 0; - width = windowWidth; - height = windowHeight; - - } - - var data = new UInt8Array (width * height * 4); - - gl.readPixels (x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, data); - - #if !js // TODO - - var rowLength = width * 4; - var srcPosition = (height - 1) * rowLength; - var destPosition = 0; - - var temp = Bytes.alloc (rowLength); - var buffer = data.buffer; - var rows = Std.int (height / 2); - - while (rows-- > 0) { - - temp.blit (0, buffer, destPosition, rowLength); - buffer.blit (destPosition, buffer, srcPosition, rowLength); - buffer.blit (srcPosition, temp, 0, rowLength); - - destPosition += rowLength; - srcPosition -= rowLength; - - } - - #end - - imageBuffer = new ImageBuffer (data, width, height, 32, RGBA32); - - default: - - #if (!macro && lime_cffi) - #if !cs - imageBuffer = NativeCFFI.lime_renderer_read_pixels (handle, rect, new ImageBuffer (new UInt8Array (Bytes.alloc (0)))); - #else - var data:Dynamic = NativeCFFI.lime_renderer_read_pixels (handle, rect, null); - if (data != null) { - imageBuffer = new ImageBuffer (new UInt8Array (@:privateAccess new Bytes (data.data.length, data.data.b)), data.width, data.height, data.bitsPerPixel); - } - #end - #end - - if (imageBuffer != null) { - - imageBuffer.format = RGBA32; - - } - - } - - if (imageBuffer != null) { - - return new Image (imageBuffer); - - } - - return null; - - } - - - public function render ():Void { - - #if (!macro && lime_cffi) - NativeCFFI.lime_renderer_make_current (handle); - - if (!useHardware) { - - #if lime_cairo - var lock:Dynamic = NativeCFFI.lime_renderer_lock (handle #if hl, { width: 0, height: 0, pixels: 0., pitch: 0 } #end); - - if (lock != null && (cacheLock == null || cacheLock.pixels != lock.pixels || cacheLock.width != lock.width || cacheLock.height != lock.height)) { - - primarySurface = CairoImageSurface.create (lock.pixels, CairoFormat.ARGB32, lock.width, lock.height, lock.pitch); - - if (cairo != null) { - - cairo.recreate (primarySurface); - - } else { - - cairo = new Cairo (primarySurface); - - } - - } - - cacheLock = lock; - #else - parent.context = NONE; - #end - - } - #end - - } - - -} \ No newline at end of file diff --git a/lime/_backend/native/NativeWindow.hx b/lime/_backend/native/NativeWindow.hx index b3b5378e0..00efe90f9 100644 --- a/lime/_backend/native/NativeWindow.hx +++ b/lime/_backend/native/NativeWindow.hx @@ -1,16 +1,29 @@ package lime._backend.native; +import haxe.io.Bytes; import lime._backend.native.NativeCFFI; import lime.app.Application; +import lime.graphics.cairo.Cairo; +import lime.graphics.cairo.CairoFormat; +import lime.graphics.cairo.CairoImageSurface; +import lime.graphics.cairo.CairoSurface; +import lime.graphics.opengl.GL; +import lime.graphics.CairoRenderContext; +import lime.graphics.ConsoleRenderContext; +import lime.graphics.GLRenderContext; import lime.graphics.Image; import lime.graphics.ImageBuffer; +import lime.graphics.RenderContext; +import lime.math.Rectangle; import lime.math.Vector2; import lime.system.Display; import lime.system.DisplayMode; import lime.system.JNI; import lime.system.System; import lime.ui.Window; +import lime.utils.UInt8Array; + #if !lime_debug @:fileXml('tags="haxe,release"') @@ -18,7 +31,12 @@ import lime.ui.Window; #end @:access(lime._backend.native.NativeCFFI) +@:access(lime._backend.native.NativeGLRenderContext) @:access(lime.app.Application) +@:access(lime.graphics.cairo.Cairo) +@:access(lime.graphics.opengl.GL) +@:access(lime.graphics.GLRenderContext) +@:access(lime.graphics.RenderContext) @:access(lime.system.DisplayMode) @:access(lime.ui.Window) @@ -31,6 +49,13 @@ class NativeWindow { private var closing:Bool; private var displayMode:DisplayMode; private var parent:Window; + private var useHardware:Bool; + + #if lime_cairo + private var cacheLock:Dynamic; + private var cairo:Cairo; + private var primarySurface:CairoSurface; + #end public function new (parent:Window) { @@ -144,6 +169,93 @@ class NativeWindow { parent.id = NativeCFFI.lime_window_get_id (handle); } + + parent.__scale = NativeCFFI.lime_window_get_scale (handle); + + var context = new RenderContext (); + + #if lime_console + + useHardware = true; + // parent.context = CONSOLE (ConsoleRenderContext.singleton); + // parent.type = CONSOLE; + + #else + + #if hl + var contextType = @:privateAccess String.fromUTF8 (NativeCFFI.lime_window_get_context_type (handle)); + #else + var contextType:String = NativeCFFI.lime_window_get_context_type (handle); + #end + + switch (contextType) { + + case "opengl": + + var gl = new GLRenderContext (); + + useHardware = true; + context.gl = gl; + context.gles2 = gl; + context.webgl = gl; + context.type = gl.type; + context.version = Std.string (gl.version); + + if (gl.type == OPENGLES && gl.version >= 3) { + + context.gles3 = gl; + context.webgl2 = gl; + + } + + if (GL.context == null) { + + GL.context = gl; + + } + + default: + + useHardware = false; + + #if lime_cairo + context.cairo = cairo; + context.type = CAIRO; + context.version = ""; + + parent.context = context; + render (); + #end + context.type = CAIRO; + + } + + #end + + parent.context = context; + + #end + + } + + + public function contextFlip ():Void { + + #if (!macro && lime_cffi) + if (!useHardware) { + + #if lime_cairo + if (cairo != null) { + + primarySurface.flush (); + + } + #end + NativeCFFI.lime_window_context_unlock (handle); + + } + + NativeCFFI.lime_window_context_flip (handle); #end } @@ -249,6 +361,136 @@ class NativeWindow { } + public function readPixels (rect:Rectangle):Image { + + var imageBuffer:ImageBuffer = null; + + switch (parent.context.type) { + + case OPENGL: + + var gl = parent.context.gl; + var windowWidth = Std.int (parent.__width * parent.__scale); + var windowHeight = Std.int (parent.__height * parent.__scale); + + var x, y, width, height; + + if (rect != null) { + + x = Std.int (rect.x); + y = Std.int ((windowHeight - rect.y) - rect.height); + width = Std.int (rect.width); + height = Std.int (rect.height); + + } else { + + x = 0; + y = 0; + width = windowWidth; + height = windowHeight; + + } + + var data = new UInt8Array (width * height * 4); + + gl.readPixels (x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, data); + + #if !js // TODO + + var rowLength = width * 4; + var srcPosition = (height - 1) * rowLength; + var destPosition = 0; + + var temp = Bytes.alloc (rowLength); + var buffer = data.buffer; + var rows = Std.int (height / 2); + + while (rows-- > 0) { + + temp.blit (0, buffer, destPosition, rowLength); + buffer.blit (destPosition, buffer, srcPosition, rowLength); + buffer.blit (srcPosition, temp, 0, rowLength); + + destPosition += rowLength; + srcPosition -= rowLength; + + } + + #end + + imageBuffer = new ImageBuffer (data, width, height, 32, RGBA32); + + default: + + #if (!macro && lime_cffi) + #if !cs + imageBuffer = NativeCFFI.lime_window_read_pixels (handle, rect, new ImageBuffer (new UInt8Array (Bytes.alloc (0)))); + #else + var data:Dynamic = NativeCFFI.lime_window_read_pixels (handle, rect, null); + if (data != null) { + imageBuffer = new ImageBuffer (new UInt8Array (@:privateAccess new Bytes (data.data.length, data.data.b)), data.width, data.height, data.bitsPerPixel); + } + #end + #end + + if (imageBuffer != null) { + + imageBuffer.format = RGBA32; + + } + + } + + if (imageBuffer != null) { + + return new Image (imageBuffer); + + } + + return null; + + } + + + public function render ():Void { + + #if (!macro && lime_cffi) + NativeCFFI.lime_window_context_make_current (handle); + + if (!useHardware) { + + #if lime_cairo + var lock:Dynamic = NativeCFFI.lime_window_context_lock (handle #if hl, { width: 0, height: 0, pixels: 0., pitch: 0 } #end); + + if (lock != null && (cacheLock == null || cacheLock.pixels != lock.pixels || cacheLock.width != lock.width || cacheLock.height != lock.height)) { + + primarySurface = CairoImageSurface.create (lock.pixels, CairoFormat.ARGB32, lock.width, lock.height, lock.pitch); + + if (cairo != null) { + + cairo.recreate (primarySurface); + + } else { + + cairo = new Cairo (primarySurface); + + } + + parent.context.cairo = cairo; + + } + + cacheLock = lock; + #else + parent.context = null; + #end + + } + #end + + } + + public function resize (width:Int, height:Int):Void { if (handle != null) { diff --git a/lime/graphics/GLES2RenderContext.hx b/lime/graphics/GLES2RenderContext.hx index 0c378afe2..ad876be9d 100644 --- a/lime/graphics/GLES2RenderContext.hx +++ b/lime/graphics/GLES2RenderContext.hx @@ -1,6 +1,9 @@ package lime.graphics; +import lime.graphics.opengl.*; + + @:forward(ACTIVE_ATTRIBUTES, ACTIVE_TEXTURE, ACTIVE_UNIFORMS, ALIASED_LINE_WIDTH_RANGE, ALIASED_POINT_SIZE_RANGE, ALPHA, ALPHA_BITS, ALWAYS, ARRAY_BUFFER, ARRAY_BUFFER_BINDING, ATTACHED_SHADERS, BACK, BLEND, BLEND_COLOR, BLEND_DST_ALPHA, BLEND_DST_RGB, BLEND_EQUATION, diff --git a/lime/graphics/GLES3RenderContext.hx b/lime/graphics/GLES3RenderContext.hx index 627a9b4c3..b49c7619e 100644 --- a/lime/graphics/GLES3RenderContext.hx +++ b/lime/graphics/GLES3RenderContext.hx @@ -3,6 +3,7 @@ package lime.graphics; import haxe.Int64; import haxe.io.Bytes; +import lime.graphics.opengl.*; import lime.utils.DataPointer; import lime.utils.Float32Array; import lime.utils.Int32Array; @@ -639,7 +640,7 @@ abstract GLES3RenderContext(GLRenderContext) from GLRenderContext to GLRenderCon public var TIMEOUT_IGNORED (get, never):Int; public var MAX_CLIENT_WAIT_TIMEOUT_WEBGL (get, never):Int; - public var type (get, never):GLContextType; + public var type (get, never):RenderContextType; public var version (get, never):Float; private inline function get_EXTENSIONS ():Int { return 0x1F03; } @@ -940,7 +941,7 @@ abstract GLES3RenderContext(GLRenderContext) from GLRenderContext to GLRenderCon private inline function get_CONTEXT_LOST_WEBGL ():Int { return this.CONTEXT_LOST_WEBGL; } private inline function get_UNPACK_COLORSPACE_CONVERSION_WEBGL ():Int { return this.UNPACK_COLORSPACE_CONVERSION_WEBGL; } private inline function get_BROWSER_DEFAULT_WEBGL ():Int { return this.BROWSER_DEFAULT_WEBGL; } - private inline function get_type ():GLContextType { return this.type; } + private inline function get_type ():RenderContextType { return this.type; } private inline function get_version ():Float { return this.version; } private inline function get_READ_BUFFER ():Int { return this.READ_BUFFER; } diff --git a/lime/graphics/GLRenderContext.hx b/lime/graphics/GLRenderContext.hx index 04f787805..8939708bc 100644 --- a/lime/graphics/GLRenderContext.hx +++ b/lime/graphics/GLRenderContext.hx @@ -13,7 +13,6 @@ import haxe.Int64; import lime.graphics.opengl.GLActiveInfo; import lime.graphics.opengl.GLBuffer; import lime.graphics.opengl.GLContextAttributes; -import lime.graphics.opengl.GLContextType; import lime.graphics.opengl.GLFramebuffer; import lime.graphics.opengl.GLProgram; import lime.graphics.opengl.GLQuery; @@ -628,7 +627,7 @@ class GLRenderContext { private function get_drawingBufferWidth () { return 0; } #end - public var type (default, null):GLContextType; + public var type (default, null):RenderContextType; public var version (default, null):Float; private function new () {} diff --git a/lime/graphics/RenderContext.hx b/lime/graphics/RenderContext.hx index 32db530cb..53ab5e230 100644 --- a/lime/graphics/RenderContext.hx +++ b/lime/graphics/RenderContext.hx @@ -8,17 +8,17 @@ import lime.ui.Window; class RenderContext { - // public var cairo (default, null):CairoRenderContext; + public var cairo (default, null):CairoRenderContext; public var ctx (default, null):CanvasRenderContext; public var element (default, null):DOMRenderContext; - // public var gl (default, null):GLRenderContext; - // public var gles2 (default, null):GLES2RenderContext; - // public var gles3 (default, null):GLES3RenderContext; + public var gl (default, null):GLRenderContext; + public var gles2 (default, null):GLES2RenderContext; + public var gles3 (default, null):GLES3RenderContext; public var sprite (default, null):FlashRenderContext; public var type (default, null):RenderContextType; public var version (default, null):String; - // public var webgl (default, null):WebGLRenderContext; - // public var webgl2 (default, null):WebGL2RenderContext; + public var webgl (default, null):WebGLRenderContext; + public var webgl2 (default, null):WebGL2RenderContext; private function new () { diff --git a/lime/graphics/WebGL2RenderContext.hx b/lime/graphics/WebGL2RenderContext.hx index c50faaa6f..84a821747 100644 --- a/lime/graphics/WebGL2RenderContext.hx +++ b/lime/graphics/WebGL2RenderContext.hx @@ -2,6 +2,7 @@ package lime.graphics; import haxe.Int64; +import lime.graphics.opengl.*; import lime.utils.ArrayBuffer; import lime.utils.ArrayBufferView; import lime.utils.BytePointer; @@ -637,7 +638,7 @@ abstract WebGL2RenderContext(GLRenderContext) from GLRenderContext to GLRenderCo public var TIMEOUT_IGNORED (get, never):Int; public var MAX_CLIENT_WAIT_TIMEOUT_WEBGL (get, never):Int; - public var type (get, never):GLContextType; + public var type (get, never):RenderContextType; public var version (get, never):Float; private inline function get_DEPTH_BUFFER_BIT ():Int { return this.DEPTH_BUFFER_BIT; } @@ -935,7 +936,7 @@ abstract WebGL2RenderContext(GLRenderContext) from GLRenderContext to GLRenderCo private inline function get_CONTEXT_LOST_WEBGL ():Int { return this.CONTEXT_LOST_WEBGL; } private inline function get_UNPACK_COLORSPACE_CONVERSION_WEBGL ():Int { return this.UNPACK_COLORSPACE_CONVERSION_WEBGL; } private inline function get_BROWSER_DEFAULT_WEBGL ():Int { return this.BROWSER_DEFAULT_WEBGL; } - private inline function get_type ():GLContextType { return this.type; } + private inline function get_type ():RenderContextType { return this.type; } private inline function get_version ():Float { return this.version; } private inline function get_READ_BUFFER ():Int { return this.READ_BUFFER; } diff --git a/lime/graphics/opengl/GL.hx b/lime/graphics/opengl/GL.hx index b5b0bc855..5de33c54c 100644 --- a/lime/graphics/opengl/GL.hx +++ b/lime/graphics/opengl/GL.hx @@ -656,7 +656,7 @@ class GL { public static inline var MAX_CLIENT_WAIT_TIMEOUT_WEBGL = 0x9247; public static var context (default, null):GLRenderContext; - public static var type (get, null):GLContextType; + public static var type (get, null):RenderContextType; public static var version (get, null):Float; private static var __currentProgram:GLProgram; @@ -3113,7 +3113,7 @@ class GL { } - private static function get_type ():GLContextType { return context.type; } + private static function get_type ():RenderContextType { return context.type; } private static function get_version ():Float { return context.version; } diff --git a/lime/graphics/opengl/GLContextType.hx b/lime/graphics/opengl/GLContextType.hx deleted file mode 100644 index 59fd4eca7..000000000 --- a/lime/graphics/opengl/GLContextType.hx +++ /dev/null @@ -1,10 +0,0 @@ -package lime.graphics.opengl; - - -enum GLContextType { - - OPENGL; - GLES; - WEBGL; - -} \ No newline at end of file diff --git a/project/Build.xml b/project/Build.xml index 5e3b0e52a..8d6abfa4b 100644 --- a/project/Build.xml +++ b/project/Build.xml @@ -247,7 +247,6 @@ - diff --git a/project/include/graphics/Renderer.h b/project/include/graphics/Renderer.h deleted file mode 100644 index cc1a662c9..000000000 --- a/project/include/graphics/Renderer.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef LIME_GRAPHICS_RENDERER_H -#define LIME_GRAPHICS_RENDERER_H - - -#include -#include -#include -#include - - -namespace lime { - - - class Renderer { - - - public: - - virtual ~Renderer () {}; - - virtual void Flip () = 0; - virtual void* GetContext () = 0; - virtual double GetScale () = 0; - virtual void* Lock (bool useCFFIValue, void* object) = 0; - virtual void MakeCurrent () = 0; - virtual void ReadPixels (ImageBuffer *buffer, Rectangle *rect) = 0; - virtual const char* Type () = 0; - virtual void Unlock () = 0; - - Window* currentWindow; - - - }; - - - Renderer* CreateRenderer (Window* window); - - -} - - -#endif \ No newline at end of file diff --git a/project/include/ui/Window.h b/project/include/ui/Window.h index 75b382eca..556882a78 100644 --- a/project/include/ui/Window.h +++ b/project/include/ui/Window.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -16,57 +17,6 @@ namespace lime { - // struct HL_Window { - - // hl_type* t; - // HL_Application* application; - // bool borderless; - // vdynamic* config; - // vdynamic* display; - // int id; - // vdynamic* onActivate; - // vdynamic* onClose; - // vdynamic* onCreate; - // vdynamic* onDeactivate; - // vdynamic* onDropFile; - // vdynamic* onEnter; - // vdynamic* onFocusIn; - // vdynamic* onFocusOut; - // vdynamic* onFullscreen; - // vdynamic* onKeyDown; - // vdynamic* onKeyUp; - // vdynamic* onLeave; - // vdynamic* onMinimize; - // vdynamic* onMouseDown; - // vdynamic* onMouseMove; - // vdynamic* onMouseMoveRelative; - // vdynamic* onMouseUp; - // vdynamic* onMouseWheel; - // vdynamic* onMove; - // vdynamic* onResize; - // vdynamic* onRestore; - // vdynamic* onTextEdit; - // vdynamic* onTextInput; - // HL_Renderer* renderer; - // double scale; - // vdynamic* stage; - - // vdynamic* backend; - // bool __borderless; - // bool __fullscreen; - // int __height; - // bool __maximized; - // bool __minimized; - // bool __resizable; - // double __scale; - // vbyte* __title; - // int __width; - // int __x; - // int __y; - - // }; - - class Window { @@ -76,16 +26,24 @@ namespace lime { virtual void Alert (const char* message, const char* title) = 0; virtual void Close () = 0; + virtual void ContextFlip () = 0; + virtual void* ContextLock (bool useCFFIValue, void* object) = 0; + virtual void ContextMakeCurrent () = 0; + virtual void ContextUnlock () = 0; virtual void Focus () = 0; + virtual void* GetContext () = 0; + virtual const char* GetContextType () = 0; virtual int GetDisplay () = 0; virtual void GetDisplayMode (DisplayMode* displayMode) = 0; virtual bool GetEnableTextEvents () = 0; virtual int GetHeight () = 0; virtual uint32_t GetID () = 0; + virtual double GetScale () = 0; virtual int GetWidth () = 0; virtual int GetX () = 0; virtual int GetY () = 0; virtual void Move (int x, int y) = 0; + virtual void ReadPixels (ImageBuffer *buffer, Rectangle *rect) = 0; virtual void Resize (int width, int height) = 0; virtual bool SetBorderless (bool borderless) = 0; virtual void SetDisplayMode (DisplayMode* displayMode) = 0; diff --git a/project/src/ExternalInterface.cpp b/project/src/ExternalInterface.cpp index f75eaa022..a8fc1e644 100644 --- a/project/src/ExternalInterface.cpp +++ b/project/src/ExternalInterface.cpp @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include @@ -113,22 +112,6 @@ namespace lime { } - void gc_renderer (value handle) { - - Renderer* renderer = (Renderer*)val_data (handle); - delete renderer; - - } - - - void hl_gc_renderer (HL_CFFIPointer* handle) { - - Renderer* renderer = (Renderer*)handle->ptr; - delete renderer; - - } - - void gc_text_layout (value handle) { #ifdef LIME_HARFBUZZ @@ -2694,167 +2677,6 @@ namespace lime { } - value lime_renderer_create (value window) { - - Renderer* renderer = CreateRenderer ((Window*)val_data (window)); - return CFFIPointer (renderer, gc_renderer); - - } - - - HL_PRIM HL_CFFIPointer* hl_lime_renderer_create (HL_CFFIPointer* window) { - - Renderer* renderer = CreateRenderer ((Window*)window->ptr); - return HLCFFIPointer (renderer, (hl_finalizer)hl_gc_renderer); - - } - - - void lime_renderer_flip (value renderer) { - - ((Renderer*)val_data (renderer))->Flip (); - - } - - - HL_PRIM void hl_lime_renderer_flip (HL_CFFIPointer* renderer) { - - ((Renderer*)renderer->ptr)->Flip (); - - } - - - double lime_renderer_get_context (value renderer) { - - Renderer* targetRenderer = (Renderer*)val_data (renderer); - return (uintptr_t)targetRenderer->GetContext (); - - } - - - HL_PRIM double hl_lime_renderer_get_context (HL_CFFIPointer* renderer) { - - Renderer* targetRenderer = (Renderer*)renderer->ptr; - return (uintptr_t)targetRenderer->GetContext (); - - } - - - double lime_renderer_get_scale (value renderer) { - - Renderer* targetRenderer = (Renderer*)val_data (renderer); - return targetRenderer->GetScale (); - - } - - - HL_PRIM double hl_lime_renderer_get_scale (HL_CFFIPointer* renderer) { - - Renderer* targetRenderer = (Renderer*)renderer->ptr; - return targetRenderer->GetScale (); - - } - - - value lime_renderer_get_type (value renderer) { - - Renderer* targetRenderer = (Renderer*)val_data (renderer); - const char* type = targetRenderer->Type (); - return type ? alloc_string (type) : alloc_null (); - - } - - - HL_PRIM vbyte* hl_lime_renderer_get_type (HL_CFFIPointer* renderer) { - - Renderer* targetRenderer = (Renderer*)renderer->ptr; - return (vbyte*)targetRenderer->Type (); - - } - - - value lime_renderer_lock (value renderer) { - - return (value)((Renderer*)val_data (renderer))->Lock (true, NULL); - - } - - - HL_PRIM vdynamic* hl_lime_renderer_lock (HL_CFFIPointer* renderer, vdynamic* object) { - - return (vdynamic*)((Renderer*)renderer->ptr)->Lock (false, object); - - } - - - void lime_renderer_make_current (value renderer) { - - ((Renderer*)val_data (renderer))->MakeCurrent (); - - } - - - HL_PRIM void hl_lime_renderer_make_current (HL_CFFIPointer* renderer) { - - ((Renderer*)renderer->ptr)->MakeCurrent (); - - } - - - value lime_renderer_read_pixels (value renderer, value rect, value imageBuffer) { - - Renderer* targetRenderer = (Renderer*)val_data (renderer); - ImageBuffer buffer (imageBuffer); - - if (!val_is_null (rect)) { - - Rectangle _rect = Rectangle (rect); - targetRenderer->ReadPixels (&buffer, &_rect); - - } else { - - targetRenderer->ReadPixels (&buffer, NULL); - - } - - return buffer.Value (imageBuffer); - - } - - - HL_PRIM ImageBuffer* hl_lime_renderer_read_pixels (HL_CFFIPointer* renderer, Rectangle* rect, ImageBuffer* imageBuffer) { - - Renderer* targetRenderer = (Renderer*)renderer->ptr; - - if (rect) { - - targetRenderer->ReadPixels (imageBuffer, rect); - - } else { - - targetRenderer->ReadPixels (imageBuffer, NULL); - - } - - return imageBuffer; - - } - - - void lime_renderer_unlock (value renderer) { - - ((Renderer*)val_data (renderer))->Unlock (); - - } - - - HL_PRIM void hl_lime_renderer_unlock (HL_CFFIPointer* renderer) { - - ((Renderer*)renderer->ptr)->Unlock (); - - } - - void lime_sensor_event_manager_register (value callback, value eventObject) { SensorEvent::callback = new ValuePointer (callback); @@ -3481,6 +3303,62 @@ namespace lime { } + void lime_window_context_flip (value window) { + + ((Window*)val_data (window))->ContextFlip (); + + } + + + HL_PRIM void hl_lime_window_context_flip (HL_CFFIPointer* window) { + + ((Window*)window->ptr)->ContextFlip (); + + } + + + value lime_window_context_lock (value window) { + + return (value)((Window*)val_data (window))->ContextLock (true, NULL); + + } + + + HL_PRIM vdynamic* hl_lime_window_context_lock (HL_CFFIPointer* window, vdynamic* object) { + + return (vdynamic*)((Window*)window->ptr)->ContextLock (false, object); + + } + + + void lime_window_context_make_current (value window) { + + ((Window*)val_data (window))->ContextMakeCurrent (); + + } + + + HL_PRIM void hl_lime_window_context_make_current (HL_CFFIPointer* window) { + + ((Window*)window->ptr)->ContextMakeCurrent (); + + } + + + void lime_window_context_unlock (value window) { + + ((Window*)val_data (window))->ContextUnlock (); + + } + + + HL_PRIM void hl_lime_window_context_unlock (HL_CFFIPointer* window) { + + ((Window*)window->ptr)->ContextUnlock (); + + } + + value lime_window_create (value application, int width, int height, int flags, HxString title) { Window* window = CreateWindow ((Application*)val_data (application), width, height, flags, title.c_str ()); @@ -3529,6 +3407,39 @@ namespace lime { } + double lime_window_get_context (value window) { + + Window* targetWindow = (Window*)val_data (window); + return (uintptr_t)targetWindow->GetContext (); + + } + + + HL_PRIM double hl_lime_window_get_context (HL_CFFIPointer* window) { + + Window* targetWindow = (Window*)window->ptr; + return (uintptr_t)targetWindow->GetContext (); + + } + + + value lime_window_get_context_type (value window) { + + Window* targetWindow = (Window*)val_data (window); + const char* type = targetWindow->GetContextType (); + return type ? alloc_string (type) : alloc_null (); + + } + + + HL_PRIM vbyte* hl_lime_window_get_context_type (HL_CFFIPointer* window) { + + Window* targetWindow = (Window*)window->ptr; + return (vbyte*)targetWindow->GetContextType (); + + } + + int lime_window_get_display (value window) { Window* targetWindow = (Window*)val_data (window); @@ -3613,6 +3524,22 @@ namespace lime { } + double lime_window_get_scale (value window) { + + Window* targetWindow = (Window*)val_data (window); + return targetWindow->GetScale (); + + } + + + HL_PRIM double hl_lime_window_get_scale (HL_CFFIPointer* window) { + + Window* targetWindow = (Window*)window->ptr; + return targetWindow->GetScale (); + + } + + int lime_window_get_width (value window) { Window* targetWindow = (Window*)val_data (window); @@ -3677,6 +3604,46 @@ namespace lime { } + value lime_window_read_pixels (value window, value rect, value imageBuffer) { + + Window* targetWindow = (Window*)val_data (window); + ImageBuffer buffer (imageBuffer); + + if (!val_is_null (rect)) { + + Rectangle _rect = Rectangle (rect); + targetWindow->ReadPixels (&buffer, &_rect); + + } else { + + targetWindow->ReadPixels (&buffer, NULL); + + } + + return buffer.Value (imageBuffer); + + } + + + HL_PRIM ImageBuffer* hl_lime_window_read_pixels (HL_CFFIPointer* window, Rectangle* rect, ImageBuffer* imageBuffer) { + + Window* targetWindow = (Window*)window->ptr; + + if (rect) { + + targetWindow->ReadPixels (imageBuffer, rect); + + } else { + + targetWindow->ReadPixels (imageBuffer, NULL); + + } + + return imageBuffer; + + } + + void lime_window_resize (value window, int width, int height) { Window* targetWindow = (Window*)val_data (window); @@ -4026,15 +3993,6 @@ namespace lime { DEFINE_PRIME1v (lime_neko_execute); DEFINE_PRIME3 (lime_png_decode_bytes); DEFINE_PRIME3 (lime_png_decode_file); - DEFINE_PRIME1 (lime_renderer_create); - DEFINE_PRIME1v (lime_renderer_flip); - DEFINE_PRIME1 (lime_renderer_get_context); - DEFINE_PRIME1 (lime_renderer_get_scale); - DEFINE_PRIME1 (lime_renderer_get_type); - DEFINE_PRIME1 (lime_renderer_lock); - DEFINE_PRIME1v (lime_renderer_make_current); - DEFINE_PRIME3 (lime_renderer_read_pixels); - DEFINE_PRIME1v (lime_renderer_unlock); DEFINE_PRIME2v (lime_render_event_manager_register); DEFINE_PRIME2v (lime_sensor_event_manager_register); DEFINE_PRIME0 (lime_system_get_allow_screen_timeout); @@ -4062,18 +4020,26 @@ namespace lime { DEFINE_PRIME2v (lime_touch_event_manager_register); DEFINE_PRIME3v (lime_window_alert); DEFINE_PRIME1v (lime_window_close); + DEFINE_PRIME1v (lime_window_context_flip); + DEFINE_PRIME1 (lime_window_context_lock); + DEFINE_PRIME1v (lime_window_context_make_current); + DEFINE_PRIME1v (lime_window_context_unlock); DEFINE_PRIME5 (lime_window_create); DEFINE_PRIME2v (lime_window_event_manager_register); DEFINE_PRIME1v (lime_window_focus); + DEFINE_PRIME1 (lime_window_get_context); + DEFINE_PRIME1 (lime_window_get_context_type); DEFINE_PRIME1 (lime_window_get_display); DEFINE_PRIME1 (lime_window_get_display_mode); DEFINE_PRIME1 (lime_window_get_enable_text_events); DEFINE_PRIME1 (lime_window_get_height); DEFINE_PRIME1 (lime_window_get_id); + DEFINE_PRIME1 (lime_window_get_scale); DEFINE_PRIME1 (lime_window_get_width); DEFINE_PRIME1 (lime_window_get_x); DEFINE_PRIME1 (lime_window_get_y); DEFINE_PRIME3v (lime_window_move); + DEFINE_PRIME3 (lime_window_read_pixels); DEFINE_PRIME3v (lime_window_resize); DEFINE_PRIME2 (lime_window_set_borderless); DEFINE_PRIME2 (lime_window_set_display_mode); @@ -4216,15 +4182,6 @@ namespace lime { // DEFINE_PRIME1v (lime_neko_execute); DEFINE_HL_PRIM (_TIMAGEBUFFER, lime_png_decode_bytes, _TBYTES _BOOL _TIMAGEBUFFER); DEFINE_HL_PRIM (_TIMAGEBUFFER, lime_png_decode_file, _STRING _BOOL _TIMAGEBUFFER); - DEFINE_HL_PRIM (_TCFFIPOINTER, lime_renderer_create, _TCFFIPOINTER); - DEFINE_HL_PRIM (_VOID, lime_renderer_flip, _TCFFIPOINTER); - DEFINE_HL_PRIM (_F64, lime_renderer_get_context, _TCFFIPOINTER); - DEFINE_HL_PRIM (_F64, lime_renderer_get_scale, _TCFFIPOINTER); - DEFINE_HL_PRIM (_BYTES, lime_renderer_get_type, _TCFFIPOINTER); - DEFINE_HL_PRIM (_DYN, lime_renderer_lock, _TCFFIPOINTER _DYN); - DEFINE_HL_PRIM (_VOID, lime_renderer_make_current, _TCFFIPOINTER); - DEFINE_HL_PRIM (_DYN, lime_renderer_read_pixels, _TCFFIPOINTER _TRECTANGLE _TIMAGEBUFFER); - DEFINE_HL_PRIM (_VOID, lime_renderer_unlock, _TCFFIPOINTER); DEFINE_HL_PRIM (_VOID, lime_render_event_manager_register, _FUN (_VOID, _NO_ARG) _TRENDER_EVENT); DEFINE_HL_PRIM (_VOID, lime_sensor_event_manager_register, _FUN (_VOID, _NO_ARG) _TSENSOR_EVENT); DEFINE_HL_PRIM (_BOOL, lime_system_get_allow_screen_timeout, _NO_ARG); @@ -4252,18 +4209,26 @@ namespace lime { DEFINE_HL_PRIM (_VOID, lime_touch_event_manager_register, _FUN (_VOID, _NO_ARG) _TTOUCH_EVENT); DEFINE_HL_PRIM (_VOID, lime_window_alert, _TCFFIPOINTER _STRING _STRING); DEFINE_HL_PRIM (_VOID, lime_window_close, _TCFFIPOINTER); + DEFINE_HL_PRIM (_VOID, lime_window_context_flip, _TCFFIPOINTER); + DEFINE_HL_PRIM (_DYN, lime_window_context_lock, _TCFFIPOINTER _DYN); + DEFINE_HL_PRIM (_VOID, lime_window_context_make_current, _TCFFIPOINTER); + DEFINE_HL_PRIM (_VOID, lime_window_context_unlock, _TCFFIPOINTER); DEFINE_HL_PRIM (_TCFFIPOINTER, lime_window_create, _TCFFIPOINTER _I32 _I32 _I32 _STRING); DEFINE_HL_PRIM (_VOID, lime_window_event_manager_register, _FUN (_VOID, _NO_ARG) _TWINDOW_EVENT); DEFINE_HL_PRIM (_VOID, lime_window_focus, _TCFFIPOINTER); + DEFINE_HL_PRIM (_F64, lime_window_get_context, _TCFFIPOINTER); + DEFINE_HL_PRIM (_BYTES, lime_window_get_context_type, _TCFFIPOINTER); DEFINE_HL_PRIM (_I32, lime_window_get_display, _TCFFIPOINTER); DEFINE_HL_PRIM (_DYN, lime_window_get_display_mode, _TCFFIPOINTER); DEFINE_HL_PRIM (_BOOL, lime_window_get_enable_text_events, _TCFFIPOINTER); DEFINE_HL_PRIM (_I32, lime_window_get_height, _TCFFIPOINTER); DEFINE_HL_PRIM (_I32, lime_window_get_id, _TCFFIPOINTER); + DEFINE_HL_PRIM (_F64, lime_window_get_scale, _TCFFIPOINTER); DEFINE_HL_PRIM (_I32, lime_window_get_width, _TCFFIPOINTER); DEFINE_HL_PRIM (_I32, lime_window_get_x, _TCFFIPOINTER); DEFINE_HL_PRIM (_I32, lime_window_get_y, _TCFFIPOINTER); DEFINE_HL_PRIM (_VOID, lime_window_move, _TCFFIPOINTER _I32 _I32); + DEFINE_HL_PRIM (_DYN, lime_window_read_pixels, _TCFFIPOINTER _TRECTANGLE _TIMAGEBUFFER); DEFINE_HL_PRIM (_VOID, lime_window_resize, _TCFFIPOINTER _I32 _I32); DEFINE_HL_PRIM (_BOOL, lime_window_set_borderless, _TCFFIPOINTER _BOOL); DEFINE_HL_PRIM (_TDISPLAYMODE, lime_window_set_display_mode, _TCFFIPOINTER _TDISPLAYMODE); diff --git a/project/src/backend/sdl/SDLRenderer.cpp b/project/src/backend/sdl/SDLRenderer.cpp deleted file mode 100644 index 6caed85af..000000000 --- a/project/src/backend/sdl/SDLRenderer.cpp +++ /dev/null @@ -1,365 +0,0 @@ -#include "SDLApplication.h" -#include "SDLWindow.h" -#include "SDLRenderer.h" -// #include "SDL_syswm.h" -#include "../../graphics/opengl/OpenGL.h" -#include "../../graphics/opengl/OpenGLBindings.h" - - -namespace lime { - - - SDLRenderer::SDLRenderer (Window* window) { - - currentWindow = window; - sdlWindow = ((SDLWindow*)window)->sdlWindow; - sdlTexture = 0; - sdlRenderer = 0; - context = 0; - - width = 0; - height = 0; - - int sdlFlags = 0; - - if (window->flags & WINDOW_FLAG_HARDWARE) { - - sdlFlags |= SDL_RENDERER_ACCELERATED; - - // if (window->flags & WINDOW_FLAG_VSYNC) { - - // sdlFlags |= SDL_RENDERER_PRESENTVSYNC; - - // } - - // sdlRenderer = SDL_CreateRenderer (sdlWindow, -1, sdlFlags); - - // if (sdlRenderer) { - - // context = SDL_GL_GetCurrentContext (); - - // } - - context = SDL_GL_CreateContext (sdlWindow); - - if (context) { - - if (window->flags & WINDOW_FLAG_VSYNC) { - - SDL_GL_SetSwapInterval (1); - - } else { - - SDL_GL_SetSwapInterval (0); - - } - - OpenGLBindings::Init (); - - #ifndef LIME_GLES - - int version = 0; - glGetIntegerv (GL_MAJOR_VERSION, &version); - - if (version == 0) { - - float versionScan = 0; - sscanf ((const char*)glGetString (GL_VERSION), "%f", &versionScan); - version = versionScan; - - } - - if (version < 2 && !strstr ((const char*)glGetString (GL_VERSION), "OpenGL ES")) { - - SDL_GL_DeleteContext (context); - context = 0; - - } - - #elif defined(IPHONE) || defined(APPLETV) - - // SDL_SysWMinfo windowInfo; - // SDL_GetWindowWMInfo (sdlWindow, &windowInfo); - // OpenGLBindings::defaultFramebuffer = windowInfo.info.uikit.framebuffer; - // OpenGLBindings::defaultRenderbuffer = windowInfo.info.uikit.colorbuffer; - glGetIntegerv (GL_FRAMEBUFFER_BINDING, &OpenGLBindings::defaultFramebuffer); - glGetIntegerv (GL_RENDERBUFFER_BINDING, &OpenGLBindings::defaultRenderbuffer); - - #endif - - } - - } - - if (!context) { - - sdlFlags &= ~SDL_RENDERER_ACCELERATED; - sdlFlags &= ~SDL_RENDERER_PRESENTVSYNC; - - sdlFlags |= SDL_RENDERER_SOFTWARE; - - sdlRenderer = SDL_CreateRenderer (sdlWindow, -1, sdlFlags); - - } - - if (context || sdlRenderer) { - - ((SDLApplication*)currentWindow->currentApplication)->RegisterWindow ((SDLWindow*)currentWindow); - - } else { - - printf ("Could not create SDL renderer: %s.\n", SDL_GetError ()); - - } - - } - - - SDLRenderer::~SDLRenderer () { - - if (sdlRenderer) { - - SDL_DestroyRenderer (sdlRenderer); - - } else if (context) { - - SDL_GL_DeleteContext (context); - - } - - } - - - void SDLRenderer::Flip () { - - if (context && !sdlRenderer) { - - SDL_GL_SwapWindow (sdlWindow); - - } else if (sdlRenderer) { - - SDL_RenderPresent (sdlRenderer); - - } - - } - - - void* SDLRenderer::GetContext () { - - return context; - - } - - - double SDLRenderer::GetScale () { - - if (sdlRenderer) { - - int outputWidth; - int outputHeight; - - SDL_GetRendererOutputSize (sdlRenderer, &outputWidth, &outputHeight); - - int width; - int height; - - SDL_GetWindowSize (sdlWindow, &width, &height); - - double scale = double (outputWidth) / width; - return scale; - - } else if (context) { - - int outputWidth; - int outputHeight; - - SDL_GL_GetDrawableSize (sdlWindow, &outputWidth, &outputHeight); - - int width; - int height; - - SDL_GetWindowSize (sdlWindow, &width, &height); - - double scale = double (outputWidth) / width; - return scale; - - } - - return 1; - - } - - - void* SDLRenderer::Lock (bool useCFFIValue, void* object) { - - if (sdlRenderer) { - - int width; - int height; - - SDL_GetRendererOutputSize (sdlRenderer, &width, &height); - - if (width != this->width || height != this->height) { - - if (sdlTexture) { - - SDL_DestroyTexture (sdlTexture); - - } - - sdlTexture = SDL_CreateTexture (sdlRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height); - - } - - void *pixels; - int pitch; - - if (useCFFIValue) { - - value result = alloc_empty_object (); - - if (SDL_LockTexture (sdlTexture, NULL, &pixels, &pitch) == 0) { - - alloc_field (result, val_id ("width"), alloc_int (width)); - alloc_field (result, val_id ("height"), alloc_int (height)); - alloc_field (result, val_id ("pixels"), alloc_float ((uintptr_t)pixels)); - alloc_field (result, val_id ("pitch"), alloc_int (pitch)); - - } - - return result; - - } else { - - const int id_width = hl_hash_utf8 ("width"); - const int id_height = hl_hash_utf8 ("height"); - const int id_pixels = hl_hash_utf8 ("pixels"); - const int id_pitch = hl_hash_utf8 ("pitch"); - - // TODO: Allocate a new object here? - - vdynamic* result = (vdynamic*)object; - - if (SDL_LockTexture (sdlTexture, NULL, &pixels, &pitch) == 0) { - - hl_dyn_seti (result, id_width, &hlt_i32, width); - hl_dyn_seti (result, id_height, &hlt_i32, height); - hl_dyn_setd (result, id_pixels, (uintptr_t)pixels); - hl_dyn_seti (result, id_pitch, &hlt_i32, pitch); - - } - - return result; - - } - - } else { - - if (useCFFIValue) { - - return alloc_null (); - - } else { - - return 0; - - } - - } - - } - - - void SDLRenderer::MakeCurrent () { - - if (sdlWindow && context) { - - SDL_GL_MakeCurrent (sdlWindow, context); - - } - - } - - - void SDLRenderer::ReadPixels (ImageBuffer *buffer, Rectangle *rect) { - - if (sdlRenderer) { - - SDL_Rect bounds = { 0, 0, 0, 0 }; - - if (rect) { - - bounds.x = rect->x; - bounds.y = rect->y; - bounds.w = rect->width; - bounds.h = rect->height; - - } else { - - SDL_GetWindowSize (sdlWindow, &bounds.w, &bounds.h); - - } - - buffer->Resize (bounds.w, bounds.h, 32); - - SDL_RenderReadPixels (sdlRenderer, &bounds, SDL_PIXELFORMAT_ABGR8888, buffer->data->buffer->b, buffer->Stride ()); - - } else if (context) { - - // TODO - - } - - } - - - const char* SDLRenderer::Type () { - - if (context) { - - return "opengl"; - - } else if (sdlRenderer) { - - SDL_RendererInfo info; - SDL_GetRendererInfo (sdlRenderer, &info); - - if (info.flags & SDL_RENDERER_SOFTWARE) { - - return "software"; - - } else { - - return "opengl"; - - } - - } - - return "none"; - - } - - - void SDLRenderer::Unlock () { - - if (sdlTexture) { - - SDL_UnlockTexture (sdlTexture); - SDL_RenderClear (sdlRenderer); - SDL_RenderCopy (sdlRenderer, sdlTexture, NULL, NULL); - - } - - } - - - Renderer* CreateRenderer (Window* window) { - - return new SDLRenderer (window); - - } - - -} \ No newline at end of file diff --git a/project/src/backend/sdl/SDLRenderer.h b/project/src/backend/sdl/SDLRenderer.h deleted file mode 100644 index ba605d5aa..000000000 --- a/project/src/backend/sdl/SDLRenderer.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef LIME_SDL_RENDERER_H -#define LIME_SDL_RENDERER_H - - -#include -#include - - -namespace lime { - - - class SDLRenderer : public Renderer { - - public: - - SDLRenderer (Window* window); - ~SDLRenderer (); - - virtual void Flip (); - virtual void* GetContext (); - virtual double GetScale (); - virtual void* Lock (bool useCFFIValue, void* object); - virtual void MakeCurrent (); - virtual void ReadPixels (ImageBuffer *buffer, Rectangle *rect); - virtual const char* Type (); - virtual void Unlock (); - - SDL_Renderer* sdlRenderer; - SDL_Texture* sdlTexture; - SDL_Window* sdlWindow; - - private: - - SDL_GLContext context; - int width; - int height; - - }; - - -} - - -#endif \ No newline at end of file diff --git a/project/src/backend/sdl/SDLWindow.cpp b/project/src/backend/sdl/SDLWindow.cpp index 04b3febde..1e7b71ed5 100644 --- a/project/src/backend/sdl/SDLWindow.cpp +++ b/project/src/backend/sdl/SDLWindow.cpp @@ -1,5 +1,7 @@ #include "SDLWindow.h" #include "SDLApplication.h" +#include "../../graphics/opengl/OpenGL.h" +#include "../../graphics/opengl/OpenGLBindings.h" #ifdef HX_WINDOWS #include @@ -16,20 +18,27 @@ namespace lime { SDLWindow::SDLWindow (Application* application, int width, int height, int flags, const char* title) { + sdlTexture = 0; + sdlRenderer = 0; + context = 0; + + contextWidth = 0; + contextHeight = 0; + currentApplication = application; this->flags = flags; - int sdlFlags = 0; + int sdlWindowFlags = 0; - if (flags & WINDOW_FLAG_FULLSCREEN) sdlFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP; - if (flags & WINDOW_FLAG_RESIZABLE) sdlFlags |= SDL_WINDOW_RESIZABLE; - if (flags & WINDOW_FLAG_BORDERLESS) sdlFlags |= SDL_WINDOW_BORDERLESS; - if (flags & WINDOW_FLAG_HIDDEN) sdlFlags |= SDL_WINDOW_HIDDEN; - if (flags & WINDOW_FLAG_MINIMIZED) sdlFlags |= SDL_WINDOW_MINIMIZED; - if (flags & WINDOW_FLAG_MAXIMIZED) sdlFlags |= SDL_WINDOW_MAXIMIZED; + if (flags & WINDOW_FLAG_FULLSCREEN) sdlWindowFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP; + if (flags & WINDOW_FLAG_RESIZABLE) sdlWindowFlags |= SDL_WINDOW_RESIZABLE; + if (flags & WINDOW_FLAG_BORDERLESS) sdlWindowFlags |= SDL_WINDOW_BORDERLESS; + if (flags & WINDOW_FLAG_HIDDEN) sdlWindowFlags |= SDL_WINDOW_HIDDEN; + if (flags & WINDOW_FLAG_MINIMIZED) sdlWindowFlags |= SDL_WINDOW_MINIMIZED; + if (flags & WINDOW_FLAG_MAXIMIZED) sdlWindowFlags |= SDL_WINDOW_MAXIMIZED; #ifndef EMSCRIPTEN - if (flags & WINDOW_FLAG_ALWAYS_ON_TOP) sdlFlags |= SDL_WINDOW_ALWAYS_ON_TOP; + if (flags & WINDOW_FLAG_ALWAYS_ON_TOP) sdlWindowFlags |= SDL_WINDOW_ALWAYS_ON_TOP; #endif #if defined (HX_WINDOWS) && defined (NATIVE_TOOLKIT_SDL_ANGLE) @@ -53,11 +62,11 @@ namespace lime { if (flags & WINDOW_FLAG_HARDWARE) { - sdlFlags |= SDL_WINDOW_OPENGL; + sdlWindowFlags |= SDL_WINDOW_OPENGL; if (flags & WINDOW_FLAG_ALLOW_HIGHDPI) { - sdlFlags |= SDL_WINDOW_ALLOW_HIGHDPI; + sdlWindowFlags |= SDL_WINDOW_ALLOW_HIGHDPI; } @@ -121,7 +130,7 @@ namespace lime { } - sdlWindow = SDL_CreateWindow (title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, sdlFlags); + sdlWindow = SDL_CreateWindow (title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, sdlWindowFlags); #if defined (IPHONE) || defined (APPLETV) if (sdlWindow && !SDL_GL_CreateContext (sdlWindow)) { @@ -129,7 +138,7 @@ namespace lime { SDL_DestroyWindow (sdlWindow); SDL_GL_SetAttribute (SDL_GL_CONTEXT_MAJOR_VERSION, 2); - sdlWindow = SDL_CreateWindow (title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, sdlFlags); + sdlWindow = SDL_CreateWindow (title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, sdlWindowFlags); } #endif @@ -140,8 +149,6 @@ namespace lime { } - //((SDLApplication*)currentApplication)->RegisterWindow (this); - #ifdef HX_WINDOWS HINSTANCE handle = ::GetModuleHandle (nullptr); @@ -168,6 +175,98 @@ namespace lime { #endif + int sdlRendererFlags = 0; + + if (flags & WINDOW_FLAG_HARDWARE) { + + sdlRendererFlags |= SDL_RENDERER_ACCELERATED; + + // if (window->flags & WINDOW_FLAG_VSYNC) { + + // sdlRendererFlags |= SDL_RENDERER_PRESENTVSYNC; + + // } + + // sdlRenderer = SDL_CreateRenderer (sdlWindow, -1, sdlRendererFlags); + + // if (sdlRenderer) { + + // context = SDL_GL_GetCurrentContext (); + + // } + + context = SDL_GL_CreateContext (sdlWindow); + + if (context) { + + if (flags & WINDOW_FLAG_VSYNC) { + + SDL_GL_SetSwapInterval (1); + + } else { + + SDL_GL_SetSwapInterval (0); + + } + + OpenGLBindings::Init (); + + #ifndef LIME_GLES + + int version = 0; + glGetIntegerv (GL_MAJOR_VERSION, &version); + + if (version == 0) { + + float versionScan = 0; + sscanf ((const char*)glGetString (GL_VERSION), "%f", &versionScan); + version = versionScan; + + } + + if (version < 2 && !strstr ((const char*)glGetString (GL_VERSION), "OpenGL ES")) { + + SDL_GL_DeleteContext (context); + context = 0; + + } + + #elif defined(IPHONE) || defined(APPLETV) + + // SDL_SysWMinfo windowInfo; + // SDL_GetWindowWMInfo (sdlWindow, &windowInfo); + // OpenGLBindings::defaultFramebuffer = windowInfo.info.uikit.framebuffer; + // OpenGLBindings::defaultRenderbuffer = windowInfo.info.uikit.colorbuffer; + glGetIntegerv (GL_FRAMEBUFFER_BINDING, &OpenGLBindings::defaultFramebuffer); + glGetIntegerv (GL_RENDERBUFFER_BINDING, &OpenGLBindings::defaultRenderbuffer); + + #endif + + } + + } + + if (!context) { + + sdlRendererFlags &= ~SDL_RENDERER_ACCELERATED; + sdlRendererFlags &= ~SDL_RENDERER_PRESENTVSYNC; + + sdlRendererFlags |= SDL_RENDERER_SOFTWARE; + + sdlRenderer = SDL_CreateRenderer (sdlWindow, -1, sdlRendererFlags); + + } + + if (context || sdlRenderer) { + + ((SDLApplication*)currentApplication)->RegisterWindow (this); + + } else { + + printf ("Could not create SDL renderer: %s.\n", SDL_GetError ()); + + } + } @@ -180,6 +279,16 @@ namespace lime { } + if (sdlRenderer) { + + SDL_DestroyRenderer (sdlRenderer); + + } else if (context) { + + SDL_GL_DeleteContext (context); + + } + } @@ -226,6 +335,128 @@ namespace lime { } + void SDLWindow::ContextFlip () { + + if (context && !sdlRenderer) { + + SDL_GL_SwapWindow (sdlWindow); + + } else if (sdlRenderer) { + + SDL_RenderPresent (sdlRenderer); + + } + + } + + + void* SDLWindow::ContextLock (bool useCFFIValue, void* object) { + + if (sdlRenderer) { + + int width; + int height; + + SDL_GetRendererOutputSize (sdlRenderer, &width, &height); + + if (width != contextWidth || height != contextHeight) { + + if (sdlTexture) { + + SDL_DestroyTexture (sdlTexture); + + } + + sdlTexture = SDL_CreateTexture (sdlRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height); + + contextWidth = width; + contextHeight = height; + + } + + void *pixels; + int pitch; + + if (useCFFIValue) { + + value result = alloc_empty_object (); + + if (SDL_LockTexture (sdlTexture, NULL, &pixels, &pitch) == 0) { + + alloc_field (result, val_id ("width"), alloc_int (contextWidth)); + alloc_field (result, val_id ("height"), alloc_int (contextHeight)); + alloc_field (result, val_id ("pixels"), alloc_float ((uintptr_t)pixels)); + alloc_field (result, val_id ("pitch"), alloc_int (pitch)); + + } + + return result; + + } else { + + const int id_width = hl_hash_utf8 ("width"); + const int id_height = hl_hash_utf8 ("height"); + const int id_pixels = hl_hash_utf8 ("pixels"); + const int id_pitch = hl_hash_utf8 ("pitch"); + + // TODO: Allocate a new object here? + + vdynamic* result = (vdynamic*)object; + + if (SDL_LockTexture (sdlTexture, NULL, &pixels, &pitch) == 0) { + + hl_dyn_seti (result, id_width, &hlt_i32, contextWidth); + hl_dyn_seti (result, id_height, &hlt_i32, contextHeight); + hl_dyn_setd (result, id_pixels, (uintptr_t)pixels); + hl_dyn_seti (result, id_pitch, &hlt_i32, pitch); + + } + + return result; + + } + + } else { + + if (useCFFIValue) { + + return alloc_null (); + + } else { + + return 0; + + } + + } + + } + + + void SDLWindow::ContextMakeCurrent () { + + if (sdlWindow && context) { + + SDL_GL_MakeCurrent (sdlWindow, context); + + } + + } + + + void SDLWindow::ContextUnlock () { + + if (sdlTexture) { + + SDL_UnlockTexture (sdlTexture); + SDL_RenderClear (sdlRenderer); + SDL_RenderCopy (sdlRenderer, sdlTexture, NULL, NULL); + + } + + } + + void SDLWindow::Focus () { SDL_RaiseWindow (sdlWindow); @@ -233,6 +464,41 @@ namespace lime { } + void* SDLWindow::GetContext () { + + return context; + + } + + + const char* SDLWindow::GetContextType () { + + if (context) { + + return "opengl"; + + } else if (sdlRenderer) { + + SDL_RendererInfo info; + SDL_GetRendererInfo (sdlRenderer, &info); + + if (info.flags & SDL_RENDERER_SOFTWARE) { + + return "software"; + + } else { + + return "opengl"; + + } + + } + + return "none"; + + } + + int SDLWindow::GetDisplay () { return SDL_GetWindowDisplayIndex (sdlWindow); @@ -298,6 +564,45 @@ namespace lime { } + double SDLWindow::GetScale () { + + if (sdlRenderer) { + + int outputWidth; + int outputHeight; + + SDL_GetRendererOutputSize (sdlRenderer, &outputWidth, &outputHeight); + + int width; + int height; + + SDL_GetWindowSize (sdlWindow, &width, &height); + + double scale = double (outputWidth) / width; + return scale; + + } else if (context) { + + int outputWidth; + int outputHeight; + + SDL_GL_GetDrawableSize (sdlWindow, &outputWidth, &outputHeight); + + int width; + int height; + + SDL_GetWindowSize (sdlWindow, &width, &height); + + double scale = double (outputWidth) / width; + return scale; + + } + + return 1; + + } + + int SDLWindow::GetWidth () { int width; @@ -341,6 +646,38 @@ namespace lime { } + void SDLWindow::ReadPixels (ImageBuffer *buffer, Rectangle *rect) { + + if (sdlRenderer) { + + SDL_Rect bounds = { 0, 0, 0, 0 }; + + if (rect) { + + bounds.x = rect->x; + bounds.y = rect->y; + bounds.w = rect->width; + bounds.h = rect->height; + + } else { + + SDL_GetWindowSize (sdlWindow, &bounds.w, &bounds.h); + + } + + buffer->Resize (bounds.w, bounds.h, 32); + + SDL_RenderReadPixels (sdlRenderer, &bounds, SDL_PIXELFORMAT_ABGR8888, buffer->data->buffer->b, buffer->Stride ()); + + } else if (context) { + + // TODO + + } + + } + + void SDLWindow::Resize (int width, int height) { SDL_SetWindowSize (sdlWindow, width, height); diff --git a/project/src/backend/sdl/SDLWindow.h b/project/src/backend/sdl/SDLWindow.h index 2c7257ca5..9df3bcba9 100644 --- a/project/src/backend/sdl/SDLWindow.h +++ b/project/src/backend/sdl/SDLWindow.h @@ -19,16 +19,24 @@ namespace lime { virtual void Alert (const char* message, const char* title); virtual void Close (); + virtual void ContextFlip (); + virtual void* ContextLock (bool useCFFIValue, void* object); + virtual void ContextMakeCurrent (); + virtual void ContextUnlock (); virtual void Focus (); + virtual void* GetContext (); + virtual const char* GetContextType (); virtual int GetDisplay (); virtual void GetDisplayMode (DisplayMode* displayMode); virtual bool GetEnableTextEvents (); virtual int GetHeight (); virtual uint32_t GetID (); + virtual double GetScale (); virtual int GetWidth (); virtual int GetX (); virtual int GetY (); virtual void Move (int x, int y); + virtual void ReadPixels (ImageBuffer *buffer, Rectangle *rect); virtual void Resize (int width, int height); virtual bool SetBorderless (bool borderless); virtual void SetDisplayMode (DisplayMode* displayMode); @@ -40,8 +48,16 @@ namespace lime { virtual bool SetResizable (bool resizable); virtual const char* SetTitle (const char* title); + SDL_Renderer* sdlRenderer; + SDL_Texture* sdlTexture; SDL_Window* sdlWindow; + private: + + SDL_GLContext context; + int contextHeight; + int contextWidth; + };