Hi
I have a source code with an algorit for a line, but when a try use it
the program say: Fatal signal: Segmentation Fault (SDL Parachute Deployed)
always, if i delete the line that draw a line, the program run great but not
draw a line (obvious), i don’t know why, the program is very simple only i need
draw a pixel and a line, i put my code, Thanks.
ej.cpp
#include <stdlib.h>
#include <math.h>
#include “video.h”
int main()
{
int err;
vid res;
res.resx = 640;
res.resy = 480;
err = iniciovideo(res);
/* Code to set a yellow pixel at the center of the screen */
int x, y;
long yellow;
/* Map the color yellow to this display (R=0xff, G=0xFF, B=0x00)
Note: If the display is palettized, you must set the palette first.
*/
yellow = SDL_MapRGB(screen->format, 0xff, 0xff, 0x00);
x = screen->w / 2;
y = screen->h / 2;
// Main loop: loop forever.
while (1)
{
/* Lock the screen for direct access to the pixels */
if ( SDL_MUSTLOCK(screen) ) {
if ( SDL_LockSurface(screen) < 0 ) {
fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
return(0);
}
}
putpixel(x, y, yellow);
diblinea(0,0,screen->w,screen->h,SDL_MapRGB(screen->format,0xff, 0x00, 0xff));
// Poll for events, and handle the ones we care about.
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
break;
case SDL_KEYUP:
// If escape is pressed, return (and thus, quit)
if (event.key.keysym.sym == SDLK_ESCAPE)
return 0;
break;
case SDL_QUIT:
return(0);
}
}
if ( SDL_MUSTLOCK(screen) )
{
SDL_UnlockSurface(screen);
}
/* Update just the part of the display that we've changed */
SDL_UpdateRect(screen, x, y, 1, 1);
}
return(0);
}
video.h this code take a algoritmo for line (didline)
//Librerias----------------------------------------------------------------
#include <stdlib.h>
#if defined(_MSC_VER)
#include “SDL.h”
#else
#include “SDL/SDL.h”
#endif
//Personal---------------------------------------------------------------
//Boleanos
#define TRUE 1
#define FALSE 0
//Errores
#define EINI 2
#define ERES 3
//datos
SDL_Surface *screen;
typedef struct r
{
int resx,resy;
}vid;
//Funciones
int initvideo(vid res);
void putpixel(int x, int y, Uint32 pixel);
Uint32 getpixel(int x, int y);
int SGN( int a );
void dibline(int x0, int y0, int x1, int y1, Uint32 color);
static int iabs(int i);
static inline int incdir(int a,int b);
static inline void swap(int *a, int *b);
//---------------------------------------------------------------------
int initvideo(vid res)
{
if( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
return(EINI);
}
atexit(SDL_Quit);
screen = SDL_SetVideoMode(res.resx, res.resy,32, SDL_SWSURFACE);
if ( screen == NULL )
{
return(ERES);
}
return (VERDAD);
}
//----------------------------------------------------------------------
void putpixel(int x, int y, Uint32 pixel)
{
int bpp = screen->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)screen->pixels + y * screen->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;
}
}
//-------------------------------------------------------------------
Uint32 getpixel(int x, int y)
{
int bpp = screen->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)screen->pixels + y * screen->pitch + x * bpp;
switch(bpp) {
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
//------------------------------------------------------------------
void dibline(int x0, int y0, int x1, int y1, Uint32 color)
{
// absolute values of dx and dy, so as to not screw up calculation
int dx=iabs(x1-x0),dy=iabs(y1-y0),x=x0,y=y0;
// When true, the loop will iterate through y instead of x
int reverse=dy>dx;
// These record which direction the line should go
int xdir=incdir(x0,x1),ydir=incdir(y0,y1);
int d,incrE,incrNE;
// Swap dx and dy if reversed, so as to not fubar equation
if(reverse) swap(&dy,&dx);
// Initialize. If
d=(dy2)-dx;
incrE=dy2;
incrNE=(dy-dx)*2;
// Draw first pixel
putpixel(x,y,color);
if(reverse)
while(y!=y1) // Iterate through y
{
y+=ydir;
if(d<=0)
d+=incrE;
else
{
x+=xdir;
d+=incrNE;
}
// Draw next pixel
putpixel(x,y,color);
}
else
while(x!=x1) // Iterate through x
{
x+=xdir;
if(d<=0)
d+=incrE;
else
{
y+=ydir;
d+=incrNE;
}
// Draw pixel
putpixel(x,y,color);
}
}
//-------------------------------------------------------------
int SGN( int a )
{
if (a>0) return (+1) ;
if (a<0) return (-1) ;
return (0);
}
//-------------------------------------------------------------
static int iabs(int i) { if(i<0) return(-i); else return(i); }
//-------------------------------------------------------------
static inline int incdir(int a,int b)
{
if(a>b) return(-1);
else return(1);
}
//------------------------------------------------------------
static inline void swap(int *a, int *b)
{
int tmp=*a;
*a=*b;
*b=tmp;
}