Sign in to follow this  

Jumping blinking

This topic is 4480 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all! I made my first SDL game! I made mario and luigi jump, but at the end for some reason they blink?! I'm using Dev-CPP with C++. Heres my code:
#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
#include "SDL_image.h"

SDL_Surface *screen;

void Draw(int x, int y, SDL_Surface *sprite);

int main(int nArgs, char **args)
{
    Uint8* keys;
    
    bool falling = false, falling2 = false;
    
    SDL_Surface *bg;
    bool isRunning = true;
    
    int height = 383, width = 0;
    int height2 = 381, width2 = 780;

    SDL_Init(SDL_INIT_VIDEO);
    atexit(SDL_Quit);

    screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    if (screen == NULL)
        return 0;
        
    bg = IMG_Load("bg.bmp");
    if (bg == NULL)
        return 0;
        
    SDL_Surface *mario1;
    mario1 = IMG_Load("mario1.bmp");
    if (mario1 == NULL)
        return 0;
        
    SDL_Surface *mario2;
    mario2 = IMG_Load("mario2.bmp");
    if (mario2 == NULL)
        return 0;
        
    SDL_Surface *luigi1;
    luigi1 = IMG_Load("luigi1.bmp");
    if (luigi1 == NULL)
        return 0;
        
    SDL_Surface *luigi2;
    luigi2 = IMG_Load("luigi2.bmp");
    if (luigi2 == NULL)
        return 0;
        
    bool MARIO1 = true, LUIGI1 = false;
    bool LuigiJump = false, MarioJump = false;
    
    SDL_SetColorKey(mario1, SDL_SRCCOLORKEY, SDL_MapRGB(mario1->format, 0, 255, 255));
    SDL_SetColorKey(luigi1, SDL_SRCCOLORKEY, SDL_MapRGB(luigi1->format, 0, 255, 255));
    SDL_SetColorKey(mario2, SDL_SRCCOLORKEY, SDL_MapRGB(mario2->format, 0, 255, 255));
    SDL_SetColorKey(luigi2, SDL_SRCCOLORKEY, SDL_MapRGB(luigi2->format, 0, 255, 255));

    while(isRunning)
    {
        SDL_Event event;
        SDL_PollEvent(&event);

        switch (event.type)
        {
            case SDL_QUIT:
                isRunning = false;
            break;
            case SDL_KEYDOWN:
                 switch (event.key.keysym.sym)
                 {
                     case SDLK_ESCAPE:
                         isRunning = false;
                     break;
                 }
            break;
        }
        
        keys = SDL_GetKeyState(NULL);
        
        if (keys[SDLK_UP])
        {
            MarioJump = true;
        }
        if (keys[SDLK_DOWN]);
        if (keys[SDLK_LEFT]) 
        { 
            if (width > 0)
                width -= 5;
                
            MARIO1 = false;
        }
        if (keys[SDLK_RIGHT]) 
        { 
            if (width < 780)
                width += 5;
                
            MARIO1 = true;
        }
        
        if (keys[SDLK_KP8]) 
        { 
            LuigiJump = true;
        }
        if (keys[SDLK_KP5]);
        if (keys[SDLK_KP4]) 
        { 
            if (width2 > 0)
                width2 -= 4;
                
            LUIGI1 = false;
        }
        if (keys[SDLK_KP6]) 
        { 
            if (width2 < 780)
                width2 += 4;
                
            LUIGI1 = true;
        }
        
        SDL_FillRect(screen, 0, 0);
        
        Draw(0, 0, bg);
        
        if (MarioJump == false)
        {
            if (MARIO1)
                Draw(width, height, mario1);
            else
                Draw(width, height, mario2);
        }
        else
        {
            if (height > 301 && falling == false)
            {
                height -= 4;
                
                if (MARIO1)
                    Draw(width, height, mario1);
                else
                    Draw(width, height, mario2);
            }
            else
            {
                falling = true;
                if (height < 381)
                {
                    height += 4;
                    
                    if (MARIO1)
                        Draw(width, height, mario1);
                    else
                        Draw(width, height, mario2);
                }
                else
                {       
                    MarioJump = false;
                    falling = false;
                }
            }
        }
        
        if (LuigiJump == false)
        {    
            if (LUIGI1)
                Draw(width2, height2, luigi1);
            else
                Draw(width2, height2, luigi2);
        }
        else
        {
            if (height2 > 301 && falling2 == false)
            {
                height2 -= 4;
                
                if (LUIGI1)
                    Draw(width2, height2, luigi1);
                else
                    Draw(width2, height2, luigi2);
            }
            else
            {
                falling2 = true;
                if (height2 < 381)
                {
                    height2 += 4;
                    
                    if (LUIGI1)
                        Draw(width2, height2, luigi1);
                    else
                        Draw(width2, height2, luigi2);
                }   
                else
                {
                    LuigiJump = false;
                    falling2 = false;
                }
            }
        }
        
        SDL_Flip(screen);
    }
    
    SDL_FreeSurface(bg);
    SDL_FreeSurface(mario1);
    SDL_FreeSurface(luigi1);
    SDL_FreeSurface(mario2);
    SDL_FreeSurface(luigi2);

    return 0;
}

void Draw(int x, int y, SDL_Surface *sprite)
{
    SDL_Rect source;
    SDL_Rect dest;

    source.x = 0;
    source.y = 0;
    source.w = sprite->w;
    source.h = sprite->h;

    dest.x = x;
    dest.y = y;
    dest.w = source.w;
    dest.h = source.h;

    SDL_BlitSurface(sprite, &source, screen, &dest);

    return;
}


Thanks for any help, cuz I dont want them to blink like that always. The pics are simply 17x28 surfaces... there are four cuz its luigi in two directions and mario in two direction. Thanks in advance. EDIT: Fixed it by drawing the sprites after the else's execute. [Edited by - agi_shi on September 5, 2005 8:48:27 PM]

Share this post


Link to post
Share on other sites

This topic is 4480 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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