Hi,
I’m just getting started in graphics programming using the SDL api
library and also currently reading the online book, programming games for
linux found at:
http://www.overcode.net/~overcode/writing/plg/local/release/plg-second-
printing-update.pdf
I’m currently up to chapter 4 now and I’m a bit unclear about drawing
stuff to the surface. More specifically, I’m not understanding the code
that’s used to draw individual pixels to the screen that’s found on page
79 of that book:
Uint16 CreateHicolorPixel(SDL_PixelFormat * fmt, Uint8 red,
Uint8 green, Uint8 blue)
{
Uint16 value;
/* This series of bit shifts uses the information from the
SDL_Format structure to correctly compose a 16-bit pixel
value from 8-bit red, green, and blue data. */
value = ((red >> fmt->Rloss) << fmt->Rshift) +
((green >> fmt->Gloss) << fmt->Gshift) +
((blue >> fmt->Bloss) << fmt->Bshift);
return value;
}
I understand the code but at the same time I don’t understand the code.
I’ve used C++ long enough to know syntactically what is happening line by
line but I don’t understand what is going on as far as the bigger picture
is concerned. I don’t see why it’s doing bitshift for the different red,
green, and blue values and then adding them together. I’m mainly having
trouble visualizing what it’s really doing and why it works.
Also the main() that follows after this code segment, specifically in the
nested for loops I’m not completely understanding either:
for (x = 0; x < 256; x++)
{
for (y = 0; y < 256; y++)
{
Uint16 pixel_color;
int offset;
pixel_color = CreateHicolorPixel(screen->format, x, 0, y);
offset = (screen->pitch / 2 * y + x);
raw_pixels[offset] = pixel_color;
}
}
I get the looping ranges, it goes line by line until it covers the entire
dimensions of the window but I don’t quite get what’s happening inside
the loop itself. Like what exactly is offset storing, and why do we need
it? I also don’t quite see why the author suggested using the screen
pitch for calculation over the screen width itself.
One could probably get away with not knowing how to do this part(drawing
individual pixels to a screen) but I rather not take that route; I don’t
think I’m doing myself any favors with that kind of mentality. So if
anyone can help clarify these things for me or take a different approach
in explaining it that’ll help me understand I’d really appreciate it.
I also noticed he mentioned some terms in the earlier chapters of the
book that I’m not familar with. More precisely, can anyone tell me what
this business with little indy and big indy is all about? There were some
other terms that I wasn’t familar with but it escapes me at the moment.
Thanks