Forum > General

sdl2 - how to load spritesheet from memory instead of file?

(1/1)

billyb123:
hi, i wrote a simple sdl2 pascal game, next thing to do is clean it up a little which means put the .png spritesheet file in memory and load it from there.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure init_spritesheet;var  map : UInt32;begin  sprite.surf := IMG_Load (spritesht);  (* spritesht = spritesheet filename to load *)  check (sprite.surf);   (* checks for null pointer *)  map := SDL_MapRGB (sprite.surf^.format, $ff, $00, $ff);  SDL_SetColorKey (sprite.surf, 1, map);  sprite.text := SDL_CreateTextureFromSurface (sdl.rend, sprite.surf);  check (sprite.text);end; 
i guess i have two questions:
(1) how to put entire .png file into memory? do i put it in my var section as one big array of byte?
(2) how to load it from memory? google says img_load_rw (), but i could not find usable examples

Mike.Cornflake:

--- Quote ---(1) how to put entire .png file into memory? do i put it in my var section as one big array of byte?
--- End quote ---

I'd compile your file into a resource and compile it into the exe.
http://wiki.freepascal.org/Lazarus_Resources

Of course, your question (2) now becomes "How do I load a resource into SDL..."  Sorry, dunno.

UPDATE:  If you can't find a way to load a TFileStream into SDL, then you can at least use the last example on the wiki to save the resource as a file into a temp folder, then load it into SDL from that temp file?

molly:
@billyb123:

--- Quote ---(1) how to put entire .png file into memory? do i put it in my var section as one big array of byte?

--- End quote ---
One of the solutions (as shown above) is to use resources.

In order to accomplish that, add the png image to your resources inside your project options. Make sure your project gets saved correctly before compiling (otherwise errors will occur at runtime). Return to resources project options to verify that the resource was added to your project correctly. Also note the name you've given the resource.


--- Quote ---(2) how to load it from memory? google says img_load_rw (), but i could not find usable examples

--- End quote ---

Something like the following should be able to solve your problem and loads the project's resource into a surface. It might be you need to tweak some more for your specifics.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  sdlSpriteResource : TResourceStream;  sdlSpriteRWops    : PSDL_RWops;begin  // Create resource stream  sdlSpriteResource := TResourceStream.Create(HInstance, 'MYDATA', RT_RCDATA);  //  requires Classes and Windows unit   // create RWops buffer from resource. Note trick to access resource memory directly (use TMemoryStream instead to be safer).  sdlSpriteRWops:= SDL_RWFromConstMem(sdlSpriteResource.Memory, sdlSpriteResource.Size);    // done using the resource so free on earliest occasion  sdlSpriteResource.Free;   // check if RWops was created successfully  if sdlSpriteRWops = nil then HALT;   // Make sure you have this somewhere inside your init in order to add support for png images  IMG_Init(IMG_INIT_PNG);   // Create a surface from RWops buffer, automatically free RWops buffer  sdlSpriteSurface := IMG_Load_RW(sdlSpriteRWops, 1);   // Continue to use the surface as you are used to   // Finally: don't forget to eventually free the surface somewhereend; Note that the name of the resource should match, e.g. change 'MYDATA' to match your chosen name.

billyb123:
Thanks for the help!

jamie:
You can also use Constant arrays of your data that will be loaded in memory when your app loads..

 these arrays maybe tricky to create but they are doable.

Const
   Sprites :Array[0..10,0..10] of TRgb = ( (RGBValue), (RGBValue).....

 I think you get the idea

Navigation

[0] Message Index

Go to full version