GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Jun-2006, 22:16
george89 george89 is offline
New Member
 
Join Date: Jun 2006
Posts: 21
george89 is on a distinguished road

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:

CPP / C++ / C Code:
#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++]
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:35.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.