Change resolution during the game

Blitz3D Forums/Blitz3D Programming/Change resolution during the game

RepeatUntil(Posted 2003) [#1]
Hello,

I would like to put a menue accessible during the game. In this menue, it's possible to change the screen resolution (going from 640X480X16 to 800X600X16 for example). But how can you change the resolution when the game is already started ??
I tried to put a new Graphics3D, but it seems that it reset every 3D objects and that everything has to be reloaded!!
Do you know how to change this resolution after the game is begun ????
Many thanks to all Blitzers !


Beaker(Posted 2003) [#2]
You have to free everything and re-load everything. You either use a large function to re-load and/or keep a log of everything in type objects.


Yappy(Posted 2003) [#3]
yeah - wot he said ;)

calling Graphics or Graphics3D deletes all images and GFX data (textures etc)

so you need to reload them.


Physt(Posted 2003) [#4]
hmmm... I just wrote a small resource manager that keeps track of everything loaded in types. Before I change graphics modes, I have it free all the resources then I change the mode and load everything back in.

The problems are that the cameras and lights are no longer valid and the position of the 3d objects are reset. I suppose I could add cameras and lights to the resource manager so that they would be recreated on a reload. As for the position of the 3d objects, I guess I could take a snapshot of thier positions and rotations before I change graphics modes then set them on reload. Does that makes sense?

On another note, is audio data also lost when the screen mode is reset? I hope not.


Hotcakes(Posted 2003) [#5]
Wouldn't think so. Audio stuff goes thru FMOD, not DirectSound/Music.

Your resource manager sounds pretty handy. Will it be going public? ;]


Michael Reitzenstein(Posted 2003) [#6]
Toby - it isn't hard at all to write one.


Physt(Posted 2003) [#7]
I'll post it to the code archives when it's complete but just to give you something to look at here it is. This also backs up Michael's statement. A resource manager isn't hard to right. This is simple at the moment. I was just trying to solve the mode switch problem. I'm not sure the resource manager will help with that. What do the pro games written in Blitz do? Does anyone know? Do they destroy then reload ***everything*** on mode change?



; Resource Manager 
; Kenneth "Physt" Lemieux 
; kenlem@...
; 4/27/2003
; No Rights Reserved


; CONSTANTS
Const RT_MESH=1
Const RT_ANIMMESH=2
Const RT_TEXTURE=3  


; TYPES
Type ResourceEntry
	Field name$
	Field filename$
	Field resourceHandle
	Field resourceType%
	Field flags%
	Field x#
	Field y#
	Field z#
	Field yaw#
	Field pitch#
	Field roll#
End Type


;---------------------------------------------------------------------------------------------
;
; RM_FreeAllResources
;		
;			Free all currently loaded resources. Used at the End of a program
;
; 		Params
;			None
;
;		Returned
;			None
;---------------------------------------------------------------------------------------------
Function RM_FreeAllResources() 

	; free the resource for each entry
	RM_UnloadAllResources()
	
	; free all the entries
	Delete Each ResourceEntry

End Function 

;---------------------------------------------------------------------------------------------
;
; RM_UnloadAllResources
;		
;			Walks the list of loaded resources and frees each one. The entry in the resourse 
;			table is not deleted.
;
; 		Params
;			None
;
;		Returned
;			None
;---------------------------------------------------------------------------------------------
Function RM_UnloadAllResources()

	For res.ResourceEntry = Each ResourceEntry
		Select (res\resourceType%)
			Case RT_MESH
				;SavePosistion(res)	
				FreeEntity(res\resourceHandle)
			Case RT_ANIMMESH
				;SavePosistion(res)				
				FreeEntity(res\resourceHandle)
			Case RT_TEXTURE
				FreeTexture(res\resourceHandle)
		End Select
	Next
	
End Function

;---------------------------------------------------------------------------------------------
;
; RM_ReloadAllResources
;		
;			Walks the resourse table and loads each resource 
;
; 		Params
;			None
;
;		Returned
;			None
;---------------------------------------------------------------------------------------------
Function RM_ReloadAllResources()

	For res.ResourceEntry = Each ResourceEntry
		res\resourceHandle = RM_LoadResourceFromDisk(res\filename$, res\resourceType%, res\flags%)
		;If (res\resourceType = RM_MESH) Or (res\resourceType = RM_ANIMMESH) Then
		;	RestorePosition(res)	
		;EndIf 
	Next

End Function 



;---------------------------------------------------------------------------------------------
;
; RM_AddResourceEntry
;		
;			Loads resource from disk and adds an entry to the resource table
;
; 		Params
;			argFile
;
;		Returned
;			None
;---------------------------------------------------------------------------------------------
Function RM_LoadResource(argName$, argFilename$, argType%, argFlags%)
	
	resHandle = RM_LoadResourceFromDisk(argFilename$, argType%, argFlags%)	
	If resHandle <> 0 Then 
		res.ResourceEntry = New ResourceEntry
		res\name$ = argName$
		res\filename$ = argFilename$
		res\resourceType% = argType%
		res\flags% = argFlags%
		res\resourceHandle = resHandle
		DebugLog "Loading resource: " + argFilename$
	Else
		DebugLog "Can not load resource: " + argFilename$
	End If 
	
	Return resHandle

End Function 

;---------------------------------------------------------------------------------------------
;
; RM_LoadResourceFromDisk
;		
;			Loads resource
;
; 		Params
;			argFile
;
;		Returned
;			handle to resource
;---------------------------------------------------------------------------------------------
Function RM_LoadResourceFromDisk(argFilename$, argType%, argFlags%)
	
	Local resHandle
	
	Select argType%
		Case RT_MESH
			resHandle = LoadMesh(argFilename$)
		Case RT_ANIMMESH
			resHandle = LoadAnimMesh(argFilename$)
		Case RT_TEXTURE
			resHandle = LoadTexture(argFilename$, argFlags%)
		Default
	End Select
	
	Return resHandle
		
End Function 


;---------------------------------------------------------------------------------------------
;
; RM_FindResourceByName
;		
;			Loads resource
;
; 		Params
;			argResourceName
;
;		Returned
;			handle to resource
;---------------------------------------------------------------------------------------------
Function RM_FindResourceByName(argName$)
	
	Local retVal

	For res.ResourceEntry = Each ResourceEntry
		If (res\name$ = argName$)
			retVal = res\resourceHandle
		End If 
	Next

	Return retVal

End Function 


Function SavePosistion(argRes.ResourceEntry)

	entity = argRes\resourceHandle	

	argRes\x# = EntityX(entity)
	argRes\y# = EntityX(entity)
	argRes\x# = EntityX(entity)
	
	argRes\roll# = EntityRoll(entity)
	argRes\pitch# = EntityPitch(entity)
	argRes\yaw# = EntityYaw(entity)

End Function 

Function RestorePosition(argRes.ResourceEntry)

	entity = argRes\resourceHandle	

	PositionEntity(entity, argRes\x#, argRes\y#, argRes\x#)
	EntityRoll(entity, argRes\roll#) 
	EntityPitch(entity, argRes\pitch#) 
	EntityYaw(entity, argRes\yaw#) 
	
End Function 




Michael Reitzenstein(Posted 2003) [#8]
Juno only allows the user to change the res in the launcher before it is launched.


RepeatUntil(Posted 2003) [#9]
Thank you all!!
I decided finally to take the easiest solution: the player can change the resolution only in the main menue before the game is started... I definitely don't like the idea that everything has to be reloaded for a resolution change during the game. It can be sooooo long!!
Anyway, thanks again!


Hotcakes(Posted 2003) [#10]
I'm aware it's a simple task, but it's also a fidgety one, one that I'm sure most ppl would prefer to just include one that's allready written =]


M2PLAY(Posted 2003) [#11]
Donīt forget erase all types before graphics3D function like particle system, fire, etc...


Physt(Posted 2003) [#12]
Pardon? Are you saying changing the graphics mode clears all types? I don't think that is the case.

I've come to the conclusion that the right way to change the screen mode mid game is to basically save the game, free everything and do a game load. That should work.


jhocking(Posted 2003) [#13]
I know that changing the graphics mode does NOT clear types because I've had some bizarre bugs happen when I clear the graphics without clearing types. On one memorable occasion random objects in my game started behaving like monsters because the enemy types from previous plays of the game were not cleared and thus pointed to random parts of memory. "Aargh! I'm being attacked by a tree!" Incidentally, his reminder to clear all types implies that types aren't cleared when the graphics mode is changed.

I allow the user to change graphics mode from the main menu of the game (specifically, the game's settings menu.) Loading stuff for the menu is a neatly parceled function anyway so I just call the loading function immediately after changing the graphics mode. This means I don't have to keep track of reloading everything back to the same state while in the middle of a level but the player can change resolution without having to exit and restart the game. A good compromise I think. I'ld be pretty annoyed if I couldn't change the resolution after having started the game (you have to play the game at least once to know what is the optimal resolution to use on your computer) but being able to change it on the fly with a level loaded isn't too important.