#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
SDL_Surface *screen;
SDL_Surface *back;
SDL_Surface *image;
int xpos=100,ypos=300;
void init(SDL_Surface *screen)
{
SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
back = SDL_LoadBMP("bg.bmp");
image = SDL_LoadBMP("img.bmp");
atexit(SDL_Quit);
}
void drawimg(SDL_Surface *screen, int x, int y, SDL_Surface *blitto)
{
Uint8* keys;
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_UP] ) { y -= 1; }
if ( keys[SDLK_DOWN] ) { y += 1; }
if ( keys[SDLK_LEFT] ) { x -= 1; }
if ( keys[SDLK_RIGHT] ) { x += 1; }
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(screen, NULL, blitto, &dest);
}
int drawIMG(SDL_Surface *img, int x, int y, int x2, int y2, int w, int h)
{
SDL_Rect dest;
dest.x=x;
dest.y=y;
SDL_Rect dest2;
dest2.x=x2;
dest2.y=y2;
dest.w=w;
dest2.h=h;
SDL_BlitSurface(img,&dest,screen,&dest2);
}
int drawit()
{
drawimg(image,0,0,screen);
drawIMG(back,xpos,ypos,xpos-2,ypos-2,132,132);
SDL_Flip(screen);
}
int main(int argc, char *argv[])
{
Uint8 *keys;
init(screen);
int done=0;
while(done ==0)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
}
}
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_UP] ) { ypos -= 1; }
if ( keys[SDLK_DOWN] ) { ypos += 1; }
if ( keys[SDLK_LEFT] ) { xpos -= 1; }
if ( keys[SDLK_RIGHT] ) { xpos += 1; }
done = 0;
drawit();
}
return 0;
}
🔒 Locked
sdl help
Advertisement
Original Post
the init went fine, it still flashes on, displays nothing then goes. the images did load, I put a check on that.
stderr says
Fatal signal: Segmentation Fault (SDL Parachute Deployed)
Fatal signal: Segmentation Fault (SDL Parachute Deployed)
It looks like something is messing up right around your drawit() function...commenting out SDL_Flip(screen); has a very interesting effect.
For the lazy, the effect is that it doesn't crash, but instead draws a blank screen. I'd say something's wrong with your drawing code...what, are you doublebuffering?
For the lazy, the effect is that it doesn't crash, but instead draws a blank screen. I'd say something's wrong with your drawing code...what, are you doublebuffering?
I've pointed this out before, and no one's contradicted me - yet. I also haven't tested it myself, but why does everyone always leave the w and h members of SDL_Rect uninitialized?
I assume the assumption is that dest.w and dest.h will equal the width and height of the surface, but in reality they're prob initialized to some garbage that just happens to be bigger than the width and height of the surface. Thus the blit works - but at what cost? Drakkcon is doing the same thing and is also complaining about crashing.
SDL_Rect dest;dest.x = x;dest.y = y;SDL_BlitSurface(screen, NULL, blitto, &dest);I assume the assumption is that dest.w and dest.h will equal the width and height of the surface, but in reality they're prob initialized to some garbage that just happens to be bigger than the width and height of the surface. Thus the blit works - but at what cost? Drakkcon is doing the same thing and is also complaining about crashing.
#include <stdio.h>#include <stdlib.h>#include <SDL/SDL.h>SDL_Surface *screen;SDL_Surface *back;SDL_Surface *image;int xpos=100,ypos=300;void init(){ back = SDL_LoadBMP("back.bmp"); if(back == NULL) { printf("cant load back"); } image = SDL_LoadBMP("ball.bmp"); if(image == NULL) { printf("cant load the image"); } atexit(SDL_Quit);}void drawimg(SDL_Surface *screen, int x, int y,int w, int h, SDL_Surface *blitto){ SDL_Rect dest; dest.x = x; dest.y = y; dest.h=h; dest.y=y; SDL_BlitSurface(screen, NULL, blitto, &dest);}int drawIMG(SDL_Surface *img, int x, int y,int x2, int y2, int w, int h){ SDL_Rect dest; dest.x=x; dest.y=y; SDL_Rect dest2; dest2.x=x2; dest2.y=y2; dest.w=w; dest2.h=h; SDL_BlitSurface(img,&dest,screen,&dest2);} int drawit(){ xpos=xpos+1; drawimg(image, 50, 50,xpos,xpos,back); drawimg(back, 0, 0,800,600,screen); SDL_Flip(screen); SDL_Flip(back);}int main(int argc, char *argv[]){ if(SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0){ printf("unable to init", SDL_GetError());} screen = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE|SDL_DOUBLEBUF); if(screen == NULL) { printf("cant set video mode"); } Uint8 *keys; init(); drawimg(back, 0, 0,800,600,screen); int done=0; while(done ==0) { SDL_Event event; while ( SDL_PollEvent(&event) ) { if ( event.type == SDL_QUIT ) { done = 1; } if ( event.type == SDL_KEYDOWN ) { if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; } } } keys = SDL_GetKeyState(NULL); if ( keys[SDLK_UP] ) { ypos -= 1; } if ( keys[SDLK_DOWN] ) { ypos += 1; } if ( keys[SDLK_LEFT] ) { xpos -= 1; } if ( keys[SDLK_RIGHT] ) { xpos += 1; } done = 0; drawit(); } return 0;} the image will drag and I have to go to the task manager to quit it, know why?
You should only be calling SDL_Flip for the main surface. The program's not quitting because you're setting done back to 0 every game loop (look above the drawit() call)
Whazzat?
Quote:
Original post by PnP Bios
You would know right away if SDL supported hardware surface stretch blits...
Whazzat?
I did, there wher some changes.
this is the code right now
it just drags now
#include <stdio.h>#include <stdlib.h>#include <SDL/SDL.h>SDL_Surface *screen;SDL_Surface *back;SDL_Surface *image;int xpos,ypos;void init(){ back = SDL_LoadBMP("back1.bmp"); if(back == NULL) { printf("cant load back"); } image = SDL_LoadBMP("ball.bmp"); if(image == NULL) { printf("cant load the image"); } atexit(SDL_Quit);}void drawimg(SDL_Surface *screen, int x, int y,int w, int h, SDL_Surface *blitto){ SDL_Rect dest; dest.x = x; dest.y = y; dest.h=h; dest.y=y; SDL_BlitSurface(screen, NULL, blitto, &dest);}int hitwall(int xpos ,int ypos ,int wall1 , int wall2){ if(xpos>wall1) { xpos=0; } if(xpos<0) { xpos=800; } if(ypos>wall2) { ypos = 0; } if(ypos<0) { ypos = 600; }} int drawIMG(SDL_Surface *img, int x, int y,int w, int h,int x2,int y2){ SDL_Rect dest; dest.x=x; dest.y=y; SDL_Rect dest2; dest2.x=x2; dest2.y=y2; dest.w=w; dest2.h=h; SDL_BlitSurface(img,&dest,screen,&dest2);} int drawit(){ hitwall(xpos,ypos,800,600); drawIMG(back, xpos-2, ypos-2, 52, 52, xpos-2, ypos-2); drawimg(image, xpos, ypos,500,50,screen); SDL_Flip(screen);}int main(int argc, char *argv[]){ if(SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0){ printf("unable to init", SDL_GetError());} screen = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE|SDL_DOUBLEBUF); if(screen == NULL) { printf("cant set video mode"); } Uint8 *keys; init(); int done=0; while(done ==0) { SDL_Event event; while ( SDL_PollEvent(&event) ) { if ( event.type == SDL_QUIT ) { done = 1; } if ( event.type == SDL_KEYDOWN ) { if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; } } } keys = SDL_GetKeyState(NULL); if ( keys[SDLK_UP] ) { ypos -= 10; } if ( keys[SDLK_DOWN] ) { ypos += 10; } if ( keys[SDLK_LEFT] ) { xpos -= 10; } if ( keys[SDLK_RIGHT] ) { xpos += 10; } drawit(); } return 0;} it just drags now
thank you very much
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement