Monday, 30 November 2015

SDL: Surface and texture

Teemu is running again, but as expected graphics were problematic. Fonts had reversed R and B components I think and when I tried to include texture as component in Image_Data class with surface it just didn't work.

That's why I created Texture class for static textures only and then Image_Data for SDL surface. That way I can manipulate the surface by first creating it and then adjusting stuff. For example fonts are created from only one template color which is much easier than use paint program to draw all colors. Surface class can unify all graphics data to RGBA model, because colorkey doesn't matter I guess. In texture the transparency is done with alpha blending value so alpha channel can be reconstructed in SDL surface using the colorkey.

If software drawing is an option then Image_Data can be used as it is. The texture is just a static copy of a surface data.

Other than that I think moving to SDL 2.0 was relatively easy. Keyboard has still some problems, because I had unicode routines receive ASCII for now which only works for letters. If the routine can't be fixed to return all ASCII codes (from keycode data) then I have to rewrite that, but it's a minor problem.

Saturday, 28 November 2015

Moving from SDL 1.2 to 2.0

I've been sick for couple of days so I decided to do some less demanding programming and since there is some kind of problem with latest "legacy" SDL 1.2.15 I decided to jump to 2.0. I'm in a middle of the process and there is lot of guessing going on, but here are some thoughts.

The first project for 2.0 is Teemu, because it has the most light-weight GUI you can imagine. It has only graphical ascii letters with some background tiles and a title logo image. There are some classes with simple color data, then a surface class and gui for the window etc.

SDL 2.0 has some major changes in keyboard handling and in graphics. If the keyboard codes work as I think then unicode is not needed anymore, in fact there is no unicode field anymore in the keyboard data, only keycode and scancode. Some keyboard codes have changed, for example keypad codes are now ...KP_1 rather than KP1. I don't yet know how keyboard events works since keyboard repeat has been removed or moved and there is a new event for keyboard which is SDL_TEXTINPUT. It could be more suitable for turn based input, who knows?

New features in graphics are window, renderer and textures. I've always worked only with software rendering so I don't have experience from textures, but as far as I can understand textures are now the only way to display graphics. Surfaces still exists, but they are only data for images and allow pixel based operations where textures are static. Renderer is the canvas where textures are displayed and you can create textures from surfaces. I don't know how colorkey works with textures. It may have to be replaced with alpha blend value, but I guess it's quite easy to create alpha images from colorkey information by manipulating the alpha value of a surface.

So my plan is to replace destination surface in surface class with renderer. Then I guess the slow way to display images is create the texture before it's displayed and fast way is create and store the texture during the constructor of the class. I guess(?) textures are copied in the GPU which makes them faster, but the copy process itself is slow. And that there is of course limited memory for textures in GPU.

All this is speculation, but it shouldn't be too long to see what happens.