   Help with pixels in SDL?
  I followed the tutorial from http://sol.gfxile.net/gp/ch02.html and I came up with this:#include "SDL/SDL.h"const int WINDOW_WIDTH = 640;const int WINDOW_HEIGHT = 480;const char* WINDOW_TITLE = "SDL START";SDL_Surface *screen;void putpixel(int x, int y, int color){unsigned int *ptr = (unsigned int*)screen-&gt;pixels;int lineoffset = y * (screen-&gt;pitch / 4);ptr[lineoffset + x] = color;}void render(){putpixel(10, 9, 0xff0000);putpixel(10, 10, 0xff0000);putpixel(10, 11, 0xff0000);putpixel(10, 12, 0xff0000);putpixel(10, 13, 0xff0000);putpixel(10, 14, 0xff0000);putpixel(10, 15, 0xff0000);putpixel(10, 16, 0xff0000);putpixel(11, 9, 0xff0000);putpixel(11, 10, 0xff0000);putpixel(11, 11, 0xff0000);putpixel(11, 12, 0xff0000);putpixel(11, 13, 0xff0000);putpixel(11, 14, 0xff0000);putpixel(11, 15, 0xff0000);putpixel(11, 16, 0xff0000);}int main(int argc, char **argv){//Start SDLSDL_Init( SDL_INIT_EVERYTHING );screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );  SDL_WM_SetCaption( WINDOW_TITLE, 0 );   SDL_Event event;bool gamerunning = true;while(gamerunning){if (SDL_PollEvent(&event)){if (event.type == SDL_QUIT){gamerunning = false;}}}//Quit SDLSDL_Quit();return 0;}I know I didn't EXACTLY follow the tutorial but I was completely confused with some of it.I'm using the SDL Linux compiler for the terminal.For some reason the pixels won't draw. (putpixel function) It'd be cool if someone could tell me what I've done wrong.Thanks, Logan   
  You're not calling your render function from anywhere in your code.  I don't know SDL so I can't say exactly how your game loop should look, but try this:while(gamerunning){if (SDL_PollEvent(&event)){if (event.type == SDL_QUIT){gamerunning = false;}}else{render();}};Quote:Original post by SeymourClearlyYou're not calling your render function from anywhere in your code.  I don't know SDL so I can't say exactly how your game loop should look, but try this:*** Source Snippet Removed ***Oh... Hehe I feel stupid now. Thanks a bunch.Edit:Hmm... I thought that would work, but for some reason it still doesn't. If anyone has any other advice I'd be grateful. :)   ;  You aren't calling render() in your game loop. You also need to call SDL_Flip() or SDL_UpdateRect() to display the changed contents on the screen.The code inside render() could itself use a loop:void render(){for(int x = 10 ; x < 12 ; ++x){for(int y = 9 ; y < 17 ; ++y){putpixel(x, y, 0xff0000);}}}Or we could realise that we are drawing a rectangle, and SDL has a function that does just that:void render(){SDL_Rect rect;rect.x = 10;rect.y = 9;rect.w = 2;rect.h = 8;SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0xff, 0x00, 0x00));}Drawing pixel by pixel is usually the slowest way of doing things.Another two important points: your "putpixel" routine assumes a particular surface format (SDL supports many). A format independent version is here. Also, your code will not work if the surface is a hardware surface, and you explicitly request one (although you often will not get one).   ;Quote:Original post by rip-offYou aren't calling render() in your game loop. You also need to call SDL_Flip() or SDL_UpdateRect() to display the changed contents on the screen.The code inside render() could itself use a loop:*** Source Snippet Removed ***Or we could realise that we are drawing a rectangle, and SDL has a function that does just that:*** Source Snippet Removed ***Drawing pixel by pixel is usually the slowest way of doing things.Another two important points: your "putpixel" routine assumes a particular surface format (SDL supports many). A format independent version is here. Also, your code will not work if the surface is a hardware surface, and you explicitly request one (although you often will not get one).I tried using SDL_Flip(screen) and it works great. I am a bit confused with what your saying, though. What do you mean by a "hardware surface" ?Sorry to be a bit noobish.   ;Quote:Sorry to be a bit noobish.Don't be. [smile]Quote:What do you mean by a "hardware surface" ?When you pass "SDL_HWSURFACE" to SDL_SetVideoMode, you are requesting that the surface be accelerated by the graphics hardware, if possible/available. Often, this might mean that the memory used by the surface is placed in graphics card memory and therefore not directly accessible to you.It is getting hard to actually get hardware surfaces in SDL of late, under Windows you have to manually request the DirectDraw backend, rather than the default GDI version.To access the raw pixels of a hardware surface, you must first lock it. This will make the surface available in system memory. I believe that RLE encoded surfaces must similarly be "locked". You can use the macro SDL_MUSTLOCK() to determine whether a surface needs to be locked. You can then use SDL_LockSurface() and SDL_UnlockSurface() to obtain and release a pointer to the raw pixels. Failure to do so can result in a crash, memory corruption or worse.For more information, you can read Bob Pendleton's articles on SDL.May I ask: why do you want to work at the pixel level?   ;Quote:Original post by rip-offQuote:Sorry to be a bit noobish.Don't be. [smile]:DQuote:May I ask: why do you want to work at the pixel level?I just kind of wanted to start basic, and didn't know what else to do.   ;Quote:Original post by logank9Quote:May I ask: why do you want to work at the pixel level?I just kind of wanted to start basic, and didn't know what else to do.That is ok. Just be aware that plotting individual pixels for anything other than stylistic reasons tends to be the hardest way of doing things. SDL makes loading and blitting images relatively easy. You can use auxiliary libraries like SDL_gfx for drawing lines and non-rectangular shapes.   