#include <iostream>
#include <SDL/SDL.h>
#include "Draw.h"
using namespace std;
int main(int argc, char* argv[])
{
if(SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0)
{
cerr << "Error initailizing SDL: " << SDL_GetError();
return -1;
}
SDL_Surface* screen;
screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE |
SDL_DOUBLEBUF);
if(screen = NULL)
{
cerr << "Could not set video mode, 640 x 480, 32 bbps: "
<< SDL_GetError();
SDL_Quit();
return -1;
}
SDL_Event event;
bool done = false;
while(done == false)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
done = true;
else if(event.type == SDL_KEYDOWN)
if(event.key.keysym.sym == SDLK_ESCAPE)
done = true;
}
LockSurface(screen);
DrawScene(screen);
UnlockSurface(screen);
}
SDL_Quit();
return 0;
}
Draw.h
/////////////////////////////////////////////////////
//Draw.h - Drawing Related Functions
/////////////////////////////////////////////////////
#ifndef _GAURD_DRAW_H_
#define _GAURD_DRAW_H_
#include <iostream>
#include <SDL/SDL.h>
//-------------------------------------------
//Surface Locking/Unlocking Functions
//-------------------------------------------
void LockSurface(SDL_Surface* screen);
void UnlockSurface(SDL_Surface* screen);
//-----------------------------------------------
//Pixel Drawing Functions
//-----------------------------------------------
void DrawPixel(SDL_Surface* screen, int x, int y,
Uint8 R, Uint8 G, Uint8 B);
void DrawScene(SDL_Surface* screen);
#endif
Draw.cpp
/////////////////////////////////////////////////////
//Draw.cpp - Drawing Related Functions
/////////////////////////////////////////////////////
#include "Draw.h"
//-------------------------------------------
//Surface Locking/Unlocking Functions
//-------------------------------------------
void LockSurface(SDL_Surface* screen)
{
if(SDL_MUSTLOCK(screen))
if(SDL_LockSurface(screen) < 0)
{
std::cerr << "Error Locking Screen: "
<< SDL_GetError();
return;
}
}
void UnlockSurface(SDL_Surface* screen)
{
if(SDL_MUSTLOCK(screen))
SDL_UnlockSurface(screen);
}
//-----------------------------------------------
//Pixel Drawing Functions
//-----------------------------------------------
void DrawPixel(SDL_Surface* screen, int x, int y,
Uint8 R, Uint8 G, Uint8 B)
{
Uint32 color = SDL_MapRGB(screen->format, R, G, B);
Uint8 bpp = screen->format->BytesPerPixel;
Uint32* pxp = (Uint32*)screen->pixels + y * screen->pitch
+ x * bpp;
*pxp = color;
}
void DrawScene(SDL_Surface* screen)
{
for(int x = 0; x < 640; ++x)
for(int y = 0; y < 480; ++y)
DrawPixel(screen, x, y, x, y, x + y % 256);
}
I'm using VC++ .Net 2002 Standard Edition to complie it.
PS: I also have a question about the adress of the pixel. Does the (SDL_Surface*)->pitch * y give the pixel down and x * BitsPerPixel give the pixel right. And does that mean (SDL_Surface*)->pixels) give the adress of the top left pixel. I just read info about what the pitch is(scanlines I think) so I'm not use if it's correct. I got that equation from a the SDL site and it's different than from the tutorial. So not sure if it's the best way to do it but I understand it better.
[edited by - Unidentified on January 4, 2004 5:31:13 PM]
C++ Language Error? Pixel Drawing - SDL
First I copied the code from a tutorial and converted it to C++, it didn't work. So i copied and pasted it and it worked. I re wrote it in C++ again today and it doesn't work. I checked the tutorial and it seems fine. The error is a runtime error. I'm thinking from the main function since all I had to do before was copy the tutorials main code. I don't know what the deal is. Does it have to do with C++ or not? Do I have to do something different when I'm programming in C++ with SDL than C?
Here's the code if it doesn't have to do with
main.cpp
quote:
PS: I also have a question about the adress of the pixel. Does the (SDL_Surface*)->pitch * y give the pixel down and x * BitsPerPixel give the pixel right. And does that mean (SDL_Surface*)->pixels) give the adress of the top left pixel.
Yup.
The error is Unhandled exception at 0x00411376 in SDL_1.exe: 0xC0000005: Access violation reading location 0x00000018. Which is located in the line containing SDL_MUSTLOCK(screen) in the LockSurface() function, according to the debugger. I replace it with the tutorial's code and it's the exact same error.
Ok thanks, I wasn't sure since there were different forms of the equation.
[edited by - Unidentified on January 4, 2004 5:34:46 PM]
[edited by - Unidentified on January 4, 2004 5:40:10 PM]
quote:Original post by stroquote:
PS: I also have a question about the adress of the pixel. Does the (SDL_Surface*)->pitch * y give the pixel down and x * BitsPerPixel give the pixel right. And does that mean (SDL_Surface*)->pixels) give the adress of the top left pixel.
Yup.
Ok thanks, I wasn't sure since there were different forms of the equation.
[edited by - Unidentified on January 4, 2004 5:34:46 PM]
[edited by - Unidentified on January 4, 2004 5:40:10 PM]
Just comment out the "if(SDL_MUSTLOCK(screen))" lines in the two functions.
They should work perfectly without it. Try it out and tell us what happens.
They should work perfectly without it. Try it out and tell us what happens.
Now the error is Unhandled exception at 0x100210f5 in SDL_1.exe: 0xC0000005: Access violation reading location 0x0000002c. Which is at the call to the LockSurface function. I can''t figure out why.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement