0

I have *.png files and I want to get different 8x8 px parts from textures and place them on bitmap (SDL_Surface, I guess, but maybe not), smth like this:

Image.

Now I'm rendering that without bitmap, i.e. I call each texture and draw part directly on screen each frame, and it's too slow. I guess I need to load each *.png to separate bitmap and use them passing video memory, then call just one big bitmap, but maybe I'm wrong. I need the fastest way of doing that, I need code of this (SDL 2, not SDL 1.3). Also maybe I need to use clear OpenGL here?

Update:
Or maybe I need to load *.png's to int arrays somehow and use them just like usual numbers and place them to one big int array, and then convert it to SDL_Surface/SDL_Texture? It seems this is the best way, but how to write this?

Update 2:
Colors of pixels in each block are not the same as it presented at the picture and also can they be transparent. Picture is just an example.

4
  • Are the 8x8 px parts all solid colours and if so, do you have a fixed amount of colours that they can be? I think you probably need some kind of map representing all the cells in the texture that you're building up from the .png parts. You could use a 2D array maybe.
    – patchwork
    Nov 11, 2013 at 22:36
  • NB. rendering SDL_Textures to the screen in SDL2 should actually be really fast (as compared to rendering SDL_Surfaces SDL 1.2). I think that the rendering of the individual 8x8px parts is probably not what's slowing things down - it's probably the number of checks that you're doing that's causing a bottle neck. It's unlikely that rendering to a single custom texture and then rendering that will speed things up significantly. Can you do any kind of timing tests to figure out what's slowing it down?
    – patchwork
    Nov 11, 2013 at 22:39
  • What do you mean "soild"? Colors can be transparent, RGBA. Now I use this. I make array and send it to surface, this part makes 100 FPS, enough fast. But SDL_UpdateTexture function is very slow (that said in documentation by the way), so I think about clear OpenGL in this place... Nov 14, 2013 at 18:56
  • @Necronomicon, please see my answer below.
    – patchwork
    Nov 15, 2013 at 12:40

3 Answers 3

1

Assumming you already have your bitmaps loaded up as SDL_Texture(s), composing them into a different texture is done via SDL_SetRenderTarget .

SDL_SetRenderTarget(renderer, target_texture);
SDL_RenderCopy(renderer, texture1, ...);
SDL_RenderCopy(renderer, texture2, ...);
...
SDL_SetRenderTarget(renderer, NULL);

Every render operation you perform between setting your Render Target and resetting it (by calling SDL_SetRenderTarget with a NULL texture parameter) will be renderer to the designated texture. You can then use this texture as you would use any other.

1
  • But textures goes in random order, so I need to check if to draw this texture in current cell or not, then next texture, so there are too much checks instead. Nov 7, 2013 at 6:34
1

Ok so, when I asked about "solid colour", I meant - "in that 8x8 pixel area in the .png that you are copying from, do all 64 pixels have the same identical RGB value?" It looks that way in your diagram, so how about this:

How about creating an SDL_Surface, and directly painting 8x8 pixel areas of the memory pointed to by the pixels member of that SDL_Surface with the values read from the original .png.

And then when you're done, convert that surface to an SDL_Texture and render that?

You would avoid all the SDL_UpdateTexture() calls.

Anyway here is some example code. Let's say that you create a class called EightByEight.

  class EightByEight
  {
    public:
      EightByEight( SDL_Surface * pDest, Uint8 r, Uint8 g, Uint8 b):
          m_pSurface(pDest),
          m_red(r),
          m_green(g),
          m_blue(b){}

      void BlitToSurface( int column, int row );

    private:
      SDL_Surface * m_pSurface;
      Uint8 m_red;
      Uint8 m_green;
      Uint8 m_blue;
  };

You construct an object of type EightByEight by passing it a pointer to an SDL_Surface and also some values for red, green and blue. This RGB corresponds to the RGB value taken from the particular 8x8 pixel area of the .png you are currently reading from. You will paint a particular 8x8 pixel area of the SDL_Surface pixels with this RGB value.

So now when you want to paint an area of the SDL_Surface, you use the function BlitToSurface() and pass in a column and row value. For example, if you divided the SDL_Surface into 8x8 pixel squares, BlitToSurface(3,5) means the paint the square at the 4th column, and 5th row with the RGB value that I set on construction.

The BlitToSurface() looks like this:

void EightByEight::BlitToSurface(int column, int row)
{
  Uint32 * pixel = (Uint32*)m_pSurface->pixels+(row*(m_pSurface->pitch/4))+column;

  // now pixel is pointing to the first pixel in the correct 8x8 pixel square 
  // of the Surface's pixel memory. Now you need to paint a 8 rows of 8 pixels, 
  // but be careful - you need to add m_pSurface->pitch - 8 each time 

  for(int y = 0; y < 8; y++)
  {
      // paint a row
      for(int i = 0; i < 8; i++)
      {
        *pixel++ = SDL_MapRGB(m_pSurface->format, m_red, m_green, m_blue);
      }

      // advance pixel pointer by pitch-8, to get the next "row".
      pixel += (m_pSurface->pitch - 8);
   }
}

I'm sure you could probably speed things up further by pre-calculating an RGB value on construction. Or if you're reading a pixel from the texture, you could probably dispense with the SDL_MapRGB() (but it's just there in case the Surface has different pixel format to the .png).

memcpy is probably faster than 8 individual assignments to the RGB value - but I just want to demonstrate the technique. You could experiment.

So, all the EightByEight objects you create, all point to the same SDL_Surface.

And then, when you're done, you just convert that SDL_Surface to an SDL_Texture and blit that.

3
  • "do all 64 pixels have the same identical RGB value?" Ah, no, this picture is just example, there are different colors in one block. If you know Terraria game, imagine one block of any matter. Nov 15, 2013 at 20:00
  • "And then when you're done, convert that surface to an SDL_Texture and render that?" I did like this before I've started to use SDL_UpdateTexture, also too slow. Nov 15, 2013 at 20:11
  • I make my array exactly using memcpy. As I said, calculations are pretty fast, delay is in convertation from SDL_Surface to SDL_Texture. I've tried different variants, it seems I must use clean OpenGL in this part. Nov 15, 2013 at 20:39
1

Thanks to everyone who took part, but we solved it with my friends. So here is an example (source code is too big and unnecessary here, I'll just describe the main idea):

int pitch, *pixels;
SDL_Texture *texture;
...
if (!SDL_LockTexture(texture, 0, (void **)&pixels, &pitch))
{
    for (/*Conditions*/)
        memcpy(/*Params*/);
    SDL_UnlockTexture(texture);
}
SDL_RenderCopy(renderer, texture, 0, 0);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.