Преглед на файлове

Refactored to use SDLVideo module instead of glue.
Now also captures window resize events.

woollybah преди 7 години
родител
ревизия
2c69cee3ee
променени са 3 файла, в които са добавени 155 реда и са изтрити 17 реда
  1. 10 2
      sdlgraphics.mod/common.bmx
  2. 142 15
      sdlgraphics.mod/sdlgraphics.bmx
  3. 3 0
      sdlsystem.mod/glue.c

+ 10 - 2
sdlgraphics.mod/common.bmx

@@ -94,7 +94,7 @@ Extern
 '	Function bmx_SDL_WaitEvent()
 	
 End Extern
-
+Rem
 Const SDL_WINDOW_FULLSCREEN:Int = $00000001         ' fullscreen window
 Const SDL_WINDOW_OPENGL:Int = $00000002             ' window usable with OpenGL context
 Const SDL_WINDOW_SHOWN:Int = $00000004              ' window is visible
@@ -109,6 +109,14 @@ Const SDL_WINDOW_MOUSE_FOCUS:Int = $00000400        ' window has mouse focus
 Const SDL_WINDOW_FULLSCREEN_DESKTOP:Int = SDL_WINDOW_FULLSCREEN | $00001000
 Const SDL_WINDOW_FOREIGN:Int = $00000800            ' window Not created by SDL
 Const SDL_WINDOW_ALLOW_HIGHDPI:Int = $00002000       ' window should be created in high-DPI Mode If supported
-
+End Rem
 Const GRAPHICS_RPI_TV_FULLSCREEN:Int = $1000
 Const GRAPHICS_WIN32_DX:Int = $1000000
+
+Const SDL_GRAPHICS_BACKBUFFER:Int    = $00800000
+Const SDL_GRAPHICS_ALPHABUFFER:Int   = $01000000
+Const SDL_GRAPHICS_DEPTHBUFFER:Int   = $02000000
+Const SDL_GRAPHICS_STENCILBUFFER:Int = $04000000
+Const SDL_GRAPHICS_ACCUMBUFFER:Int   = $08000000
+
+Const SDL_GRAPHICS_NATIVE:Int        = $40000000

+ 142 - 15
sdlgraphics.mod/sdlgraphics.bmx

@@ -35,10 +35,29 @@ Import "-lbcm_host"
 ?
 
 Import SDL.SDLSystem
+Import SDL.SDLVideo
 Import BRL.Graphics
 
 Import "common.bmx"
 
+Private
+Global _currentContext:TGraphicsContext
+Public
+
+Type TGraphicsContext
+	Field mode:Int
+	Field width:Int
+	Field height:Int
+	Field depth:Int
+	Field hertz:Int
+	Field flags:Int
+	Field sync:Int
+	
+	Field window:TSDLWindow
+	Field context:TSDLGLContext
+	Field info:Byte Ptr
+End Type
+
 Type TSDLGraphics Extends TGraphics
 
 	Method Driver:TSDLGraphicsDriver()
@@ -48,28 +67,38 @@ Type TSDLGraphics Extends TGraphics
 	
 	Method GetSettings( width:Int Var,height:Int Var,depth:Int Var,hertz:Int Var,flags:Int Var )
 		Assert _context
-		Local w:Int,h:Int,d:Int,r:Int,f:Int
-		bbSDLGraphicsGetSettings _context,w,h,d,r,f
-		width=w
-		height=h
-		depth=d
-		hertz=r
-		flags=f
+		'Local w:Int,h:Int,d:Int,r:Int,f:Int
+		'bbSDLGraphicsGetSettings _context,w,h,d,r,f
+		width=_context.width
+		height=_context.height
+		depth=_context.depth
+		hertz=_context.hertz
+		flags=_context.flags
 	End Method
 	
 	Method Close()
 		If Not _context Return
-		bbSDLGraphicsClose( _context )
-		_context=0
+		'bbSDLGraphicsClose( _context )
+		If _currentContext = _context Then
+			_currentContext = Null
+		End If
+		If _context.context Then
+			_context.context.Free()
+		End If
+		If _context.window Then
+			_context.window.Destroy()
+		End If
+		_context=Null
 	End Method
 
 	Method GetHandle:Byte Ptr()
 		If _context Then
-			Return bbSDLGraphicsGetHandle(_context)
+			' FIXME
+			'Return bbSDLGraphicsGetHandle(_context)
 		End If
 	End Method
 	
-	Field _context:Byte Ptr
+	Field _context:TGraphicsContext
 	
 End Type
 
@@ -121,21 +150,119 @@ Type TSDLGraphicsDriver Extends TGraphicsDriver
 	
 	Method CreateGraphics:TSDLGraphics( width:Int,height:Int,depth:Int,hertz:Int,flags:Int )
 		Local t:TSDLGraphics=New TSDLGraphics
-		t._context=bbSDLGraphicsCreateGraphics( width,height,depth,hertz,flags )
+		t._context=SDLGraphicsCreateGraphics( width,height,depth,hertz,flags )
 		Return t
 	End Method
 	
+	Method SDLGraphicsCreateGraphics:TGraphicsContext(width:Int,height:Int,depth:Int,hertz:Int,flags:Int)
+		Local context:TGraphicsContext = New TGraphicsContext
+
+		Local windowFlags:UInt = SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_OPENGL
+		Local gFlags:UInt
+		
+		If flags & SDL_GRAPHICS_NATIVE Then
+		
+			flags :~ SDL_GRAPHICS_NATIVE
+			
+			gFlags = flags & (SDL_GRAPHICS_BACKBUFFER | SDL_GRAPHICS_ALPHABUFFER | SDL_GRAPHICS_DEPTHBUFFER | SDL_GRAPHICS_STENCILBUFFER | SDL_GRAPHICS_ACCUMBUFFER)
+			
+			flags :~ (SDL_GRAPHICS_BACKBUFFER | SDL_GRAPHICS_ALPHABUFFER | SDL_GRAPHICS_DEPTHBUFFER | SDL_GRAPHICS_STENCILBUFFER | SDL_GRAPHICS_ACCUMBUFFER)
+
+			windowFlags :| flags
+
+			If gFlags & SDL_GRAPHICS_BACKBUFFER Then SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)
+			If gFlags & SDL_GRAPHICS_ALPHABUFFER Then SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 1)
+			If gFlags & SDL_GRAPHICS_DEPTHBUFFER Then SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24)
+			If gFlags & SDL_GRAPHICS_STENCILBUFFER Then SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1)
+
+		Else
+
+			If depth Then
+				windowFlags :| SDL_WINDOW_FULLSCREEN
+				' mode = MODE_DISPLAY
+			Else
+				If flags & $80000000 Then
+					windowFlags :| SDL_WINDOW_FULLSCREEN_DESKTOP
+				End If
+				' mode = MODE_WINDOW
+			End If
+
+			gFlags = flags & (GRAPHICS_BACKBUFFER | GRAPHICS_ALPHABUFFER | GRAPHICS_DEPTHBUFFER | GRAPHICS_STENCILBUFFER | GRAPHICS_ACCUMBUFFER)
+
+			If gFlags & GRAPHICS_BACKBUFFER Then SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)
+			If gFlags & GRAPHICS_ALPHABUFFER Then SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 1)
+			If gFlags & GRAPHICS_DEPTHBUFFER Then SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24)
+			If gFlags & GRAPHICS_STENCILBUFFER Then SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1)
+		
+		End If
+		
+
+		'End If
+		
+		context.window = TSDLWindow.Create(AppTitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, windowFlags)
+		SDL_GL_SetSwapInterval(-1)
+		
+		context.context = context.window.GLCreateContext()
+		
+		context.width = width
+		context.height = height
+		context.depth = depth
+		context.hertz = hertz
+		context.flags = flags
+		
+		AddHook EmitEventHook,GraphicsHook,context,0
+		
+		Return context
+	End Method
+	
 	Method SetGraphics( g:TGraphics )
-		Local context:Byte Ptr
+		Local context:TGraphicsContext
 		Local t:TSDLGraphics=TSDLGraphics( g )
 		If t context=t._context
-		bbSDLGraphicsSetGraphics context
+		'bbSDLGraphicsSetGraphics context
+		If context Then
+			context.window.GLMakeCurrent(context.context)
+		End If
+		_currentContext = context
 	End Method
 	
 	Method Flip( sync:Int )
-		bbSDLGraphicsFlip sync
+		'bbSDLGraphicsFlip sync
+		If Not _currentContext Then
+			Return
+		End If
+		
+		If sync <> _currentContext.sync Then
+			_currentContext.sync = sync
+			_currentContext.context.SetSwapInterval(sync)
+		End If
+		
+		_currentContext.window.GLSwap()
 	End Method
 	
+	Function GraphicsHook:Object( id,data:Object,context:Object )
+		Local ev:TEvent=TEvent(data)
+		If Not ev Return data
+
+		Select ev.id
+			Case EVENT_WINDOWSIZE
+				Local ctxt:TGraphicsContext = TGraphicsContext(context)
+				If ctxt Then
+					If ctxt.window.GetID() = ev.data Then
+						ctxt.width = ev.x
+						ctxt.height = ev.y
+						GraphicsResize(ev.x, ev.y)
+					End If
+				End If
+		End Select
+		
+		Return data
+	End Function
+
+	Method CanResize:Int()
+		Return True
+	End Method
+
 End Type
 
 Rem

+ 3 - 0
sdlsystem.mod/glue.c

@@ -175,6 +175,9 @@ void bmx_SDL_EmitSDLEvent( SDL_Event *event, BBObject *source ) {
 				case SDL_WINDOWEVENT_FOCUS_LOST:
 					bbSDLSystemEmitEvent(BBEVENT_APPSUSPEND, source, 0, 0, 0, 0, &bbNullObject);
 					return;
+				case SDL_WINDOWEVENT_RESIZED:
+					bbSDLSystemEmitEvent(BBEVENT_WINDOWSIZE, source, event->window.windowID, 0, event->window.data1, event->window.data2, &bbNullObject);
+					return;
 			}
 	}