Recent

Author Topic: sdl2 - how to load spritesheet from memory instead of file?  (Read 3746 times)

billyb123

  • New Member
  • *
  • Posts: 26
sdl2 - how to load spritesheet from memory instead of file?
« on: November 06, 2017, 03:27:23 am »
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  [Select][+][-]
  1. procedure init_spritesheet;
  2. var
  3.   map : UInt32;
  4. begin
  5.   sprite.surf := IMG_Load (spritesht);  (* spritesht = spritesheet filename to load *)
  6.   check (sprite.surf);   (* checks for null pointer *)
  7.   map := SDL_MapRGB (sprite.surf^.format, $ff, $00, $ff);
  8.   SDL_SetColorKey (sprite.surf, 1, map);
  9.   sprite.text := SDL_CreateTextureFromSurface (sdl.rend, sprite.surf);
  10.   check (sprite.text);
  11. end;
  12.  

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

  • Hero Member
  • *****
  • Posts: 1260
Re: sdl2 - how to load spritesheet from memory instead of file?
« Reply #1 on: November 06, 2017, 03:40:21 am »
Quote
(1) how to put entire .png file into memory? do i put it in my var section as one big array of byte?

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?
« Last Edit: November 06, 2017, 03:43:02 am by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: sdl2 - how to load spritesheet from memory instead of file?
« Reply #2 on: November 06, 2017, 07:20:30 am »
@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?
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

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  [Select][+][-]
  1. var
  2.   sdlSpriteResource : TResourceStream;
  3.   sdlSpriteRWops    : PSDL_RWops;
  4. begin
  5.   // Create resource stream
  6.   sdlSpriteResource := TResourceStream.Create(HInstance, 'MYDATA', RT_RCDATA);  //  requires Classes and Windows unit
  7.  
  8.   // create RWops buffer from resource. Note trick to access resource memory directly (use TMemoryStream instead to be safer).
  9.   sdlSpriteRWops:= SDL_RWFromConstMem(sdlSpriteResource.Memory, sdlSpriteResource.Size);
  10.  
  11.   // done using the resource so free on earliest occasion
  12.   sdlSpriteResource.Free;
  13.  
  14.   // check if RWops was created successfully
  15.   if sdlSpriteRWops = nil then HALT;
  16.  
  17.   // Make sure you have this somewhere inside your init in order to add support for png images
  18.   IMG_Init(IMG_INIT_PNG);
  19.  
  20.   // Create a surface from RWops buffer, automatically free RWops buffer
  21.   sdlSpriteSurface := IMG_Load_RW(sdlSpriteRWops, 1);
  22.  
  23.   // Continue to use the surface as you are used to
  24.  
  25.   // Finally: don't forget to eventually free the surface somewhere
  26. end;
  27.  
Note that the name of the resource should match, e.g. change 'MYDATA' to match your chosen name.
« Last Edit: November 06, 2017, 07:39:15 am by molly »

billyb123

  • New Member
  • *
  • Posts: 26
Re: sdl2 - how to load spritesheet from memory instead of file?
« Reply #3 on: November 06, 2017, 03:20:08 pm »
Thanks for the help!

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: sdl2 - how to load spritesheet from memory instead of file?
« Reply #4 on: November 19, 2017, 04:05:21 am »
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

The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018