Sign in to follow this  
nooblet

Screen shows and closes

Recommended Posts

Hey, my screen open's then closes, what is wrong?
#include "SDL/SDL.h"
#include "SDL/SDL_Image.h"

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

const int DOT_WIDTH = 20;
const int DOT_HEIGHT = 20;

/* -------------------------------------------- */

SDL_Event event;

SDL_Surface *screen = NULL;
SDL_Surface *dot = NULL;

int x = 0;
int y = 0;

int xVel = 0;
int yVel = 0;

bool done = false;

/* -------------------------------------------- */

class Dot
{
    private:

    int xVel, yVel;

    public:

    Dot();
    void handle_input();
    void show();
};

Dot::Dot()
{
    int x = 0;
    int y = 0;

    int xVel = 0;
    int yVel = 0;

    dot = SDL_LoadBMP("dot.bmp");
}

void apply_surface( int x, int y, SDL_Surface *source, SDL_Surface *destination )
{
    SDL_Rect offset;

    offset.x = x;
    offset.y = y;

    SDL_BlitSurface( source, NULL, destination, &offset );
}


void Dot::handle_input()
{
    if( event.type == SDL_KEYDOWN )
    {
        switch(event.key.keysym.sym)
        {
            case SDLK_UP: yVel -= DOT_HEIGHT / 2; break;
            case SDLK_DOWN: yVel += DOT_HEIGHT / 2; break;
            case SDLK_LEFT: xVel -= DOT_WIDTH / 2; break;
            case SDLK_RIGHT: xVel += DOT_WIDTH / 2; break;
        }
    }

    else if( event.type == SDL_KEYUP )
    {
        switch(event.key.keysym.sym)
        {
            case SDLK_UP: yVel += DOT_HEIGHT / 2; break;
            case SDLK_DOWN: yVel -= DOT_HEIGHT / 2; break;
            case SDLK_LEFT: xVel += DOT_WIDTH / 2; break;
            case SDLK_RIGHT: xVel -= DOT_WIDTH / 2; break;
        }
    }
}

void Dot::show()
{
    x += xVel;

    if( x < 0 || ( x + DOT_WIDTH > SCREEN_WIDTH ) )
    {
        x -= xVel;
    }

    y += yVel;

    if( y < 0 || ( y + DOT_HEIGHT > SCREEN_HEIGHT ) )
    {
        y -= yVel;
    }

    apply_surface( x, y, dot, screen );
}

bool init()
{
    if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
    {
        return false;
    }

    SDL_Surface *screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
    if(!screen)
    {
        return false;
    }

    SDL_WM_SetCaption("Fighter! BETA Version", NULL);

    return true;
}

int main(int argc, char* argv[])
{
    init();

    Dot myDot;

    while( done == false )
    {
        while( SDL_PollEvent(&event) )
        {
            myDot.handle_input();

            if(event.type == SDL_QUIT)
            {
                done = true;
            }

            myDot.show();

            SDL_Flip(screen);
        }
    }

    SDL_FreeSurface(dot);
    SDL_FreeSurface(screen);

    SDL_Quit();

    return 1;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by nooblet
I fixed the screen not showing up, but now it won't move the dot :(


Ok, I tried your code and had to make a good few modifications to get it to work.


#include "SDL/SDL.h"
#include "SDL/SDL_Image.h"
// used for some error reporting
#include <iostream>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

const int DOT_WIDTH = 20;
const int DOT_HEIGHT = 20;

/* -------------------------------------------- */
SDL_Event event;

SDL_Surface *screen = NULL;
SDL_Surface *dot = NULL;

// i removed these as they are not needed.
/*
int x = 0;
int y = 0;

int xVel = 0;
int yVel = 0;
*/

// this can be a local variable inside main()
// local varaibles are preffered.
// bool done = false;

/* -------------------------------------------- */

class Dot
{
private:
// surely the position is as much a property of the
// dot as the velocity
int x,y;
int xVel, yVel;

public:

Dot();
void handle_input();
void show();
};

// this peculiar looking code sets all the member varaibles of the
// dots to zero. its called an initialiser list, google it if
// you havent come across it.
Dot::Dot()
:
x(0),
y(0),
xVel(0),
yVel(0)
{
// these lines created local variables and set them to zero
// these variables are distinct from both the global x,y,xVel and yVel
// and the class versions.
//int x = 0;
//int y = 0;

//int xVel = 0;
//int yVel = 0;

dot = SDL_LoadBMP("dot.bmp");
if( !dot ) { std::cout << "error " << SDL_GetError() << std::endl; }
}

void apply_surface( int x, int y, SDL_Surface *source, SDL_Surface *destination )
{
SDL_Rect offset;

offset.x = x;
offset.y = y;

SDL_BlitSurface( source, NULL, destination, &offset );
}


void Dot::handle_input()
{
if( event.type == SDL_KEYDOWN )
{
switch(event.key.keysym.sym)
{
case SDLK_UP: yVel -= DOT_HEIGHT / 2; break;
case SDLK_DOWN: yVel += DOT_HEIGHT / 2; break;
case SDLK_LEFT: xVel -= DOT_WIDTH / 2; break;
case SDLK_RIGHT: xVel += DOT_WIDTH / 2; break;
}
}

else if( event.type == SDL_KEYUP )
{
switch(event.key.keysym.sym)
{
case SDLK_UP: yVel += DOT_HEIGHT / 2; break;
case SDLK_DOWN: yVel -= DOT_HEIGHT / 2; break;
case SDLK_LEFT: xVel += DOT_WIDTH / 2; break;
case SDLK_RIGHT: xVel -= DOT_WIDTH / 2; break;
}
}
}

void Dot::show()
{
x += xVel;

if( x < 0 || ( x + DOT_WIDTH > SCREEN_WIDTH ) )
{
x -= xVel;
}

y += yVel;

if( y < 0 || ( y + DOT_HEIGHT > SCREEN_HEIGHT ) )
{
y -= yVel;
}

apply_surface( x, y, dot, screen );
}

// we should report errors, your code didnt do anything if there was an error
bool init()
{
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
std::cout << "error " << SDL_GetError() << std::endl;
return false;
}
//
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
if(!screen)
{
std::cout << "error " << SDL_GetError() << std::endl;
return false;
}

SDL_WM_SetCaption("Fighter! BETA Version", NULL);

return true;
}

int main(int argc, char* argv[])
{
init();

Dot myDot;
bool done = false;
while( !done )
{
while( SDL_PollEvent(&event) )
{
myDot.handle_input();

if(event.type == SDL_QUIT)
{
done = true;
}
// these will be moved outside the input loop
// we want to draw the dot even while the user isnt pressing
// a key
//myDot.show();
//SDL_Flip(screen);
}
myDot.show();
SDL_Flip(screen);
}

SDL_FreeSurface(dot);
// dont call FreeSurface on the surface that you get
// from SDL_SetVideoMode. SDL_Quit or SDL_QuitSubSystem(SDL_INIT_VIDEO)
// will free it for you.
//SDL_FreeSurface(screen);

SDL_Quit();

return 1;
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this