Convert memory for surface

Started by
8 comments, last by GameDevCorn 11 months ago

Screenshot data is stored like this:

BYTE* memory = 0;

...other declarations.

int bytes = (((bits_per_pixel * bitmap_dx + 31) & (~31))/8) * bitmap_dy;

file.write((const *)Memory1, bytes);

What do I need to cast memory to, to load it to sdl_surface?

I tried making a black surface.

Locking and casting the surface pixels to BYTE* tPix.

memcpy Memory1 to tPix and unlocking the surface.

Image isn't showing.

Advertisement

Your text is far from complete. You're showing a line about “memory” and never use it. Instead you use “Memory1” without defining what it is or what you do with it. Similarly, “tPix” comes out of the blue.

I'd suggest you find an SDL2 example or tutorial that loads a screen from bytes.

Thanks for replying.

The memory declaration is a typo. It's memory1.

Also, I posted only the pertinent code to keep down having to read code that didn't matter.

The tPix variable comes after I create A SDLSurface named tmpSurf.

It gets loaded from IMG_LOAD("black.png"). The tmpSurf has blk pixels.

It's at this point I do the surfacelock.

BYTE *tPix = (BYTE *)tmpSur→pixels

Then i've tried 2 options:

  1. tPix = memory1; then unlocked the surface. I created a texture from that surface and displayed with rendercopy and renderpresent. Result = Black image displayed
  2. Or, tried memcpy(tPix, memory1, sizeof(memory1)). I created a texture from that surface and displayed with rendercopy and renderpresent. Result = segment fault.

The segment fault is funny since they are the same size.

Hope that clears up things

GameDevCorn said:
BYTE* memory = 0;

Well, fair enough. so this is "memory1". I don't know what you think this does, but in CPP this sets “memory1” to be a null-pointer. That is, it points to nothing.

GameDevCorn said:
A SDLSurface named tmpSurf. It gets loaded from IMG_LOAD("black.png"). The tmpSurf has blk pixels.

It's at this point I do the surfacelock.

BYTE *tPix = (BYTE *)tmpSur→pixels

So tmpSurf is allocated and managed by SDL, and you setup a pointer into that space.

GameDevCorn said:
tPix = memory1

Then you change the pointer to point at whatever memory 1 points (ie nothing). That's fine.

You don't show how you did unlocking etc here. I assume you used tmpSurf. That of course works. That is, setting up a pointer to some address and then pointing it to some other address doesn't do anything with the previously pointed-at spot.

If you used tPix here rather than tmpSurf, perhaps SDL has a special case if the supplied pointer in null? (due to the previous assignment)

GameDevCorn said:
Or, tried memcpy(tPix, memory1, sizeof(memory1)).

This won't fly for 2 reasons. First “memory1” points at null, and you can't copy data from “I point at nothing”, so a crash is the logical answer if you attempt it anyway. Secondly, “sizeof(memory1)” queries the size of the pointer, which is either 4 or 8 bytes on most machines. It's likely not enough to contain all pixels in the loaded image file.

In particular a pointer is exactly just a pointer (a finger pointing at a memory address). It doesn't know what it points at, it has no size of the thing it points at. Basically, it's a simple integer, as explained in https://www.freecodecamp.org/news/pointers-in-c-are-not-as-difficult-as-you-think/

In addition, it also doesn't handle memory allocation, it's your job to make sure it points into space that the program is allowed to access. Read may perhaps work (or it may not), write will definitely cause a crash due to the OS that guards processes not to try modifying memory that they don't own.

@Alberth I'm not sure if you're trying to be combative but check your writing tone.

Memory1 starts out as null but is written to with bmp data.

file.write((const *)Memory1, bytes);

I know it contains data because I can write it to disk with lodepng with the name ScreenShot.png.

The main point is to bypass writing to disk and load the data directly to the surface.

This will speed my program up.

Then you change the pointer to point at whatever memory 1 points (ie nothing). That's fine

Again, it's has data.

Now, if you actually have constructive help I'll be glad to read it.

However, if your style is only to be a combative obstinate pretentious anal orifices.

Then don't reply.

I was going to try to help, but I thought Alberth's tone was perfectly appropriate, so you probably don't want my help either.

The topic here is afaik what code is needed to do what you want it to do.

I was explaining what I think the CODE is doing so we can find the spots with different views and resolve them.

If you however consider a different view on what CODE is doing to be a personal attack, I cannot help you.

GameDevCorn said:

@Alberth I'm not sure if you're trying to be combative but check your writing tone.

Memory1 starts out as null but is written to with bmp data.

file.write((const *)Memory1, bytes);

I know it contains data because I can write it to disk with lodepng with the name ScreenShot.png.

The main point is to bypass writing to disk and load the data directly to the surface.

This will speed my program up.

Then you change the pointer to point at whatever memory 1 points (ie nothing). That's fine

Again, it's has data.

Now, if you actually have constructive help I'll be glad to read it.

However, if your style is only to be a combative obstinate pretentious anal orifices.

Then don't reply.

Alberth's tone was fine. The code you posted is missing important bits (like how/where memory was allocated for “memory1”, or where/how you create/lock the surface to write data to it, etc).

I think you should post the entire code (if possible), rather than trying to post the “relevant bits”.

Edit: I find it strange that you are writing data to a const pointer, this tells me that the "file.write" function is trying to write data to the file and not to the “memory1” location. I am not sure if this is your intention or not.

I used memcpy to get what I needed done.

SDL_LockSurface(surf1);
  memcpy(surf1→pixels, memory, bytes);
SDL_UnlockSurface(surf1);

Hopefully that'll help someone searching later.

This topic is closed to new replies.

Advertisement