/*
Name: Pong
Copyright: None what so ever.
Author: Joe Bates
Date:
Description: Testing my abilitites as a programmer.
*/
//Headers
#include <stdlib.h>
#include <SDL\SDL.h>
#include <SDL\SDL_gfx.h>
#include <SDL\SDL_image.h>
//Main
int main(int argc, char *argv[])
{
//Initialize SDL and the screen.
SDL_Init(SDL_INIT_VIDEO ); atexit(SDL_Quit);
SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF );
SDL_WM_SetCaption("Pong", NULL);
//Initialize the framerate.
FPSmanager *framerate;
SDL_initFramerate(framerate);
SDL_setFramerate(framerate, 100);
//Initialize images.
SDL_Surface *lpaddle = IMG_Load("L_Paddle.png");
SDL_Surface *rpaddle = IMG_Load("R_Paddle.png");
SDL_Surface *ballimg = IMG_Load( "Ball.png");
//Initialize variables.
SDL_Rect lrect;
lrect.x = 5; lrect.y = 200;
SDL_Rect rrect;
rrect.x = 615; rrect.y = 200;
SDL_Rect ball ;
ball.x = 310; ball.y = 230;
float ballmx = 4;
float ballmy = 0;
int wait = 1;
//Loop start.
bool done = false;
while(done == false)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = true; }
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = true; }
}
}
//Clear the screen.
SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,0));
//Get keys.
Uint8 *keys = SDL_GetKeyState(NULL);
//Respond to keys.
if (keys[SDLK_LSHIFT]) lrect.y-=4;
else if(keys[SDLK_LCTRL] ) lrect.y+=4;
if (keys[SDLK_UP] ) rrect.y-=4;
else if(keys[SDLK_DOWN]) rrect.y+=4;
//Block paddles.
if(lrect.y < 0) lrect.y = 0;
if(lrect.y > 400) lrect.y = 400;
if(rrect.y < 0) rrect.y = 0;
if(rrect.y > 400) rrect.y = 400;
//Left paddle bounce detection.
if
(
(ball.x <= 15)
&&
((ball.y + 20) >= lrect.y)
&&
( ball.y <= (lrect.y + 80))
)
{
ballmy = (((ball.y + 10) - (lrect.y + 40)) /10);
if (ballmy < 0) ballmx = 4 + ballmy;
else if(ballmy > 0) ballmx = 4 - ballmy;
else ballmx = 4;
}
//Right paddle bounce detection.
if
(
(ball.x >= 605)
&&
((ball.y +20) >= rrect.y)
&&
( ball.y <= (rrect.y + 80))
)
{
ballmy = (((ball.y + 10) - (rrect.y + 40)) /10);
if (ballmy < 0) ballmx = -4 - ballmy;
else if(ballmy > 0) ballmx = -4 + ballmy;
else ballmx = -4;
}
//Wall bounce detection.
if((ball.y >= 460) || (ball.y <= 0)) ballmy = -ballmy;
//Score detection.
if((ball.x <= 0) || (ball.x >= 620))
{
ball.x = 310;
ball.y = 230;
ballmx = 4;
ballmy = 0;
wait = 1;
}
//Move ball.
if(wait == 0)
{
ball.x += ballmx;
ball.y += ballmy;
}
//Ball regeneration wait.
if(wait != 0)wait++;
if(wait == 150)wait = 0;
//Draw
if((wait > 50) || (wait == 0)) SDL_BlitSurface(ballimg, NULL, screen, &ball);
SDL_BlitSurface(lpaddle, NULL, screen, &lrect);
SDL_BlitSurface(rpaddle, NULL, screen, &rrect);
rectangleRGBA(screen, 0, 0, 639, 479, 255, 255, 255, 255);
//Synchronize the screen.
SDL_Flip(screen);
SDL_framerateDelay(framerate);
}
}
I got Fatal signal: Segmentation Fault (SDL Parachute Deployed) but don't know why!
I got Fatal signal: Segmentation Fault (SDL Parachute Deployed) in stderr but don't know why! I did the SDL_INIT_NOPARACHUTE thing and the problem seems to happen when the program exits because it either freezes up there or it puts the error in stderr. Take a look. The program runs fine when the parachute is there, but I feel bad seeing it. BTW, I made the SDL_gfx header so I wouldn't have to include individual parts of SDL_gfx, incase u thought that was the problem.
so it only crash's when you try to exit the game? i only glanced at your code, and i don't know the problem. step through your code with a debugger, and it should bring you to the exact line that is crashing the game. a few things:
-your not free'ing any of your surfaces when the game is over. this is a memory leak your causing! the OS will clean it up for you, but this is just a bad programming practice
-god invented functions for a reason, man. [smile]
-your not free'ing any of your surfaces when the game is over. this is a memory leak your causing! the OS will clean it up for you, but this is just a bad programming practice
-god invented functions for a reason, man. [smile]
FTA, my 2D futuristic action MMORPG
You should probably run it in a debugger to see exactly where it's crashing, and step up the callstack to see what the bad decision was.
I used the exit(0) function and it worked fine, but the files still appear, just empty. Any way to have like it ot make those files?
I will make it with functiosn later.
I will make it with functiosn later.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement