[GLFW] loading media from DataBuffer

Monkey Forums/Monkey Programming/[GLFW] loading media from DataBuffer

EdzUp(Posted 2015) [#1]
Would it be possible to allow loading of media, graphics and sounds from DataBuffers, if this was added into monkey it would easily bring it on par with max and allow for encryption and other data manipulation :)


Nobuyuki(Posted 2015) [#2]
is there some limit to loading files into a dataBuffer other than available memory? Before I knew about DataBuffers, I used FileStream to get data from arbitrary files... If DataBuffer has a location limit, it may be because it's the canonical "cross-platform compatible" implementation?


impixi(Posted 2015) [#3]


Would it be possible to allow loading of media, graphics and sounds from DataBuffers, if this was added into monkey it would easily bring it on par with max and allow for encryption and other data manipulation :)




To do this "properly" for all the targets would be *challenging*, to say the least. You'd have to modify numerous native code files and possibly make some architectural changes. Wouldn't be worth the effort, IMO.

What specific kinds of "data manipulation" are you wanting to make? Might already be possible through simpler techniques?


EdzUp(Posted 2015) [#4]
Well basically what I would like to do is read an image from a compressed file into a DataBuffer and then load it via LoadImage into monkey form use. Same with audio data, I can already extract files from the compressed file but not through a buffer.


impixi(Posted 2015) [#5]
To give you an idea of what's required to modify monkey/mojo so you can load an Image from a DataBuffer object on GLFW3 target:

(Monkey X Pro V83b)

(If you're crazy enough to try these changes, remember to backup your Monkey installation first).

---

modules\brl\native\databuffer.cpp

Add the following public method to the BBDataBuffer class prototype:


	unsigned char *GetDataPtr(){
		return (unsigned char *) _data; 
	}



---

targets\glfw3\modules\native\glfwgame.cpp

Add the following public method prototype to the BBGlfwGame class prototype:


	virtual unsigned char *LoadImageDataFromMemory(stbi_uc const *buffer, int len, int *width, int *height, int *depth );



Add the implementation somewhere below the class prototype:

unsigned char *BBGlfwGame::LoadImageDataFromMemory(stbi_uc const *buffer, int len, int *width, int *height, int *depth ){

	unsigned char *data = stbi_load_from_memory(buffer, len, width, height, depth, 0 );
	
	if( data ) gc_ext_malloced( (*width)*(*height)*(*depth) );
	
	return data;
}


---

modules\mojo\native\mojo.glfw.cpp

Add the following public method prototypes to the gxtkGraphics class prototype:

virtual bool LoadSurfaceFromMemory__UNSAFE__( gxtkSurface *surface, BBDataBuffer *dataBuffer );
virtual gxtkSurface *LoadSurface( BBDataBuffer *dataBuffer );


Add the implementations somewhere below the class prototype:

bool gxtkGraphics::LoadSurfaceFromMemory__UNSAFE__( gxtkSurface *surface, BBDataBuffer *dataBuffer ){

	if (!dataBuffer) return false;
	if (dataBuffer->Length()<=0) return false;

	int width,height,depth;
	unsigned char *data=BBGlfwGame::GlfwGame()->LoadImageDataFromMemory( dataBuffer->GetDataPtr(), dataBuffer->Length(), &width, &height, &depth );
	if( !data ) return false;
	
	surface->SetData( data,width,height,depth );
	return true;
}

gxtkSurface *gxtkGraphics::LoadSurface( BBDataBuffer *dataBuffer )
{
	gxtkSurface *surf=new gxtkSurface();
	if( !LoadSurfaceFromMemory__UNSAFE__( surf, dataBuffer ) ) return 0;
	surf->Bind();
	return surf;
}


---

modules\mojo\graphicsdevice.monkey

Add the import statement at the top:

	Import brl.databuffer


Add the following method declaration to the GraphicsDevice class declaration:

	Method LoadSurface:Surface(dataBuffer:DataBuffer)


---

modules\mojo\graphics.monkey

Add the import statement at the top:

	Import brl.databuffer


Add the following functions:

	Function LoadImage:Image( data:DataBuffer, frameCount=1, flags=Image.DefaultFlags )
		Local surf:=device.LoadSurface( data )
		If surf Return (New Image).Init( surf,frameCount,flags )
	End

	Function LoadImage:Image( data:DataBuffer, frameWidth, frameHeight, frameCount, flags=Image.DefaultFlags )
		Local surf:=device.LoadSurface( data )
		If surf Return (New Image).Init( surf,0,0,frameWidth,frameHeight,frameCount,flags,Null,0,0,surf.Width,surf.Height )
	End


***

Here's a test program (place a test.jpg image in the data directory):



GOOD LUCK!


EdzUp(Posted 2015) [#6]
Ah thanks will have a look, would be loverly to have these added to the official source.