# putpixel help (SDL)

## Recommended Posts

solinent    124
This code gives me a solid blue line accoss the screen, rather than the expected white line. I am using SDL to try and draw pixels, and only the blue component of the color seems to be working. I got putpixel from elsewhere, so I was wondering if someone could help me out. If someone could tell me what's wrong or put this into a compiler, that would be great.
#include <iostream>
#include "SDL/SDL.h"
#include <cmath>

const int WIDTH=512, HEIGHT=512;
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel);
using namespace std;

void quitExecution (int code) {
SDL_Quit();
exit (code);
}

void handleKeyUp (SDL_keysym* keysym) {
switch (keysym->sym) {
case SDLK_ESCAPE:
quitExecution(0);
break;
default:
break;
}
}

void handleKeyDown (SDL_keysym* keysym) {
switch (keysym->sym) {
case SDLK_ESCAPE:
quitExecution(0);
break;
default:
break;
}
}

SDL_Surface* initSDL () {

SDL_Surface* screen;
/* Initialize SDL for video */
const SDL_VideoInfo* video;
int bpp;

if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr,
"Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}

/* Call this when we quit */
atexit(SDL_Quit);

/* Get video information and then set the video mode for a hardware surface */
video = SDL_GetVideoInfo();
if (!video) {
fprintf(stderr,
"Couldn't get video information: %s\n", SDL_GetError());
exit(1);
}
int flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
bpp = video->vfmt->BitsPerPixel;
cout << bpp << endl;
screen = SDL_SetVideoMode( WIDTH, HEIGHT, bpp, flags);
if( screen == 0 ) {
fprintf(stderr,
"Couldn't set video mode: %s\n", SDL_GetError());
exit(1);
}

return screen;
}

void processEvents () {
SDL_Event event;

while( SDL_PollEvent( &event ) ) {

switch( event.type ) {
case SDL_KEYDOWN:
handleKeyDown( &event.key.keysym );
break;
case SDL_KEYUP:
handleKeyUp( &event.key.keysym );
break;
case SDL_QUIT:
quitExecution( 0 );
break;
}

}

}

void drawScreen (SDL_Surface* screen) {
Uint8 pixelColor;
pixelColor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);

/* First, lock the screen */
if (SDL_MUSTLOCK(screen))
if (SDL_LockSurface(screen) < 0)
cerr << "Couldn't lock screen: " << SDL_GetError() << endl;

putpixel (screen, WIDTH/2, HEIGHT/2, pixelColor);

for (int i=0; i<min(HEIGHT,WIDTH); i++)
putpixel (screen, i, i, pixelColor);

if (SDL_MUSTLOCK(screen))
SDL_UnlockSurface(screen);

SDL_Flip(screen);
}

int main( int argc, char* argv[]) {
SDL_Surface* screen = initSDL ();

while (true) {
drawScreen(screen);
processEvents();
}

return 0;
}

void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

switch(bpp) {
case 1:
*p = pixel;
break;

case 2:
*(Uint16 *)p = pixel;
break;

case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;

case 4:
*(Uint32 *)p = pixel;
break;
}
}
If that's too much code, then simply look a the drawScreen method. I've looked at it alot, so I think there's a problem with how I initialize the code. My goal is to build a simple raytracer (just for kicks) and I need to be able to set a pixel to a certain color. This was supposed to do that, but it doesn't. I'm going to be using C++ (I know, this code isn't OO, but I'm going to use OOP when I actually do the real code).

##### Share on other sites
rip-off    10976
In drawScreen(), the variable "pixelColour" is of type Uint8, which cannot hold all the values of a pixel. If you change this to a Uint32 type, it should work (it works on my machine anyway).

##### Share on other sites
solinent    124
Quote:
 Original post by rip-offIn drawScreen(), the variable "pixelColour" is of type Uint8, which cannot hold all the values of a pixel. If you change this to a Uint32 type, it should work (it works on my machine anyway).

Thanks alot, I knew it was some stupid problem like that.

I spent like 30 mins trying to figure it out, but usually after that, you'll never find the problem until you stop for a while and look at it later.

Thanks, again. There should really be a compiler warning, I don't know why I didn't get any.