|
Question about locking surfaces to directly access pixels, SDL.
After spending a few days during my SDL hell week, I have a question about the following:
int SDL_LockSurface(SDL_Surface *surface);
In the description it tells me that I use this to lock a surface so I can directly access pixels. (read and write to them) And unlock it after I'm done. Well, it does say I do not need to do this to access the pixels, but why do I need to in the first place? Thats my question? I have vague Ideas as to why I need to, but As shown in the following piece of code, I could access the pixels just fine:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include "SDL.h"
SDL_Surface *screen, *temp_screen;
void fillrect(bool,int x, int y, int w, int h, int r, int g, int b);
void ClipRect(int,int);
int main()
{
if (SDL_Init(SDL_INIT_VIDEO)==-1)
{
printf("SDL_Init failed, %s", SDL_GetError());
exit(-1);
}
screen = SDL_SetVideoMode(640,480,8,SDL_SWSURFACE);
fillrect(false,100,100,100,100,255,255,255); SDL_Delay(1000);
fillrect(true,100,100,100,100,255,255,255); SDL_Delay(1000);
return 0;
}
void fillrect(bool nw, int x, int y, int w, int h, int r, int g, int b)
{
Uint32 color = SDL_MapRGB(screen->format, r, g, b);
SDL_Rect drect;
drect.x = x;
drect.y = y;
drect.w = w;
drect.h = h;
if (nw == false)
{
SDL_FillRect(screen, &drect, color);
SDL_UpdateRect(screen, drect.x, drect.y, drect.w, drect.h);
}
else if (nw == true)
{
SDL_FreeSurface(screen);
SDL_UpdateRect(screen, 0, 0, 0, 0);
}
}
Though, I am still trying to figure out how to free pixels (don't tell me  , and yes I have tried using the SDL_LockSurface/Unlock), I still don't quit see where I could use this function. My question summed up: How and when can/do I use SDL_LockSurface() and SDL_UnlockSurface() for?
Last edited by cable_guy_67 : 19-Jun-2006 at 01:32.
Reason: Changed [CODE] to [C++]
|