I could use some help, write a pixel drawing program


#1

Need some help designing a pixel drawing program.
Here the request: Write a pixel drawing program that draws pixels at the current mouse position when the user holds the right mouse button down. Do not clear the screen every frame so that you have a primitive drawing program!

Current code:

#include <SDL/SDL.h>

int PixelX = -100;
int PixelY = -100;

SDL_Surface* Pixel = NULL;
SDL_Surface* Background = NULL;
SDL_Surface* Backbuffer = NULL;

bool LoadFiles();
void FreeFiles();

SDL_Surface* LoadImage(char* fileName);

void DrawImage(SDL_Surface* image, SDL_Surface* destSurface, int x, int y);
bool ProgramIsRunning();

int main(int argc, char* args[])
{
SDL_Init(SDL_INIT_EVERYTHING);

Backbuffer = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);

if(!LoadFiles())
{
    FreeFiles();
    SDL_Quit();

    return 0;
}

while(ProgramIsRunning())
{
    SDL_FillRect(Backbuffer,NULL, 0);

    DrawImage(Background, Backbuffer, 0, 0);
    DrawImage(Pixel, Backbuffer, PixelX - 50, PixelY - 50);


    SDL_Delay(20);
    SDL_Flip(Backbuffer);
}

FreeFiles();

SDL_Quit();

return 1;

}

SDL_Surface* LoadImage(char* fileName)
{
SDL_Surface* imageLoaded = NULL;
SDL_Surface* processedImage = NULL;

imageLoaded = SDL_LoadBMP(fileName);

if(imageLoaded != NULL)
{
    processedImage = SDL_DisplayFormat(imageLoaded);
    SDL_FreeSurface(imageLoaded);

    if( processedImage != NULL )
    {
        Uint32 colorKey = SDL_MapRGB( processedImage->format, 0xFF, 0, 0xFF );
        SDL_SetColorKey( processedImage, SDL_SRCCOLORKEY, colorKey );
    }

}

return processedImage;

}

void DrawImage(SDL_Surface* image, SDL_Surface* destSurface, int x, int y)
{
SDL_Rect destRect;
destRect.x = x;
destRect.y = y;

SDL_BlitSurface( image, NULL, destSurface, &destRect);

}

bool ProgramIsRunning()
{
SDL_Event event;

bool running = true;

while(SDL_PollEvent(&event))
{


    if(event.type == SDL_QUIT)
        running = false;

    if( event.type == SDL_MOUSEBUTTONDOWN)
    {
         if(event.button.button == SDL_BUTTON_RIGHT)
         {
              PixelX = event.button.x;
              PixelY = event.button.y;
         }


    }

    /*if( event.type == SDL_MOUSEMOTION)
    {
        int mouseX = event.motion.x;
        int mouseY = event.motion.y;

        char buffer[64];
        sprintf(buffer, "Mouse X: %d, Mouse Y: %d", mouseX, mouseY);
        SDL_WM_SetCaption(buffer, NULL);
    }
    */
}

return running;

}

bool LoadFiles()
{
Pixel = LoadImage(“image/pixel.bmp”);

if(Pixel == NULL)
    return false;

Background = LoadImage("image/background.bmp");

if(Background == NULL)
    return false;

return true;

}

void FreeFiles()
{
if(Pixel != NULL)
{
SDL_FreeSurface(Pixel);
Pixel = NULL;
}

if(Background != NULL)
{
    SDL_FreeSurface(Background);
    Background = NULL;
}

}