Cannot Find The Problem

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Cannot Find The Problem

Post by Bakkon »

davidthefat wrote:Should I just stick with the bounding box collision or do a pixel to pixel collision?
Looking at all the problems you're having, you probably shouldn't touch pixel perfect. Using bounding box now, finish the major structure of your game, and come back later when you're more comfortable with your design.
User avatar
davidthefat
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 529
Joined: Mon Nov 10, 2008 3:51 pm
Current Project: Fully Autonomous Robot
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: California
Contact:

Re: Cannot Find The Problem

Post by davidthefat »

:x

Code: Select all

//Draw.cpp
#include "Draw.h"

int Draw::DrawMap(int flag, int i, int t)
{
   
            
            if( flag == 1)
            {
                 color = makecol( 128, 255, 255);
            }
            else if( flag == 2) 
            {
                 color = makecol( 255, 128, 0);
            }
            else if( flag == 3) 
            {
                color = makecol( 255, 0, 0);
            }
            else if( flag == 4) 
            {
                 color = makecol( 0, 0, 0);
            }
            rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, color);
}
void Draw::SetBuffer()
{
     Buffer = create_bitmap( 640, 480);
     if(Buffer == NULL)
{
       set_gfx_mode(GFX_TEXT,0,0,0,0);
         allegro_message("Could not create buffer!");
           exit(EXIT_FAILURE);
}
}
BITMAP* Draw::bBuffer()
{
     return Buffer;
}
void Draw::DrawBuffer()
{
      draw_sprite( screen, Buffer, 0, 0);
}

void Draw::DrawSprite(BITMAP* sprite, int x, int y)
{
      masked_blit(sprite, Buffer, 0, 0, x, y, 172,64);
      
}

Code: Select all

//Draw.h
#include <allegro.h>
#ifndef DRAW_H
#define DRAW_H
class Draw
{
      private:
              BITMAP* Buffer;
              int color;
      public:
             int DrawMap(int flag, int i, int t);
             void SetBuffer();
             BITMAP* bBuffer();
             void DrawBuffer();
             void DrawSprite(BITMAP* sprite, int x, int y);
};
#endif

Code: Select all

//Player.h
#include <allegro.h>
#ifndef PLAYER_H
#define PLAYER_H
class Player
{
      private:
              int playerx;
              int playery;
              BITMAP *Player_Sprite;
              int Frame;
      public:
             void SetPlayer();
             BITMAP* PlayerSprite();
             int Player_X();
             int Player_Y();
             int Player_Frame();
};
#endif

Code: Select all

//Player.cpp
#include "Player.h"

void Player::SetPlayer()
{
     Player_Sprite = load_bitmap("Snake.bmp", NULL);
     if(Player_Sprite == NULL)
{
  set_gfx_mode(GFX_TEXT,0,0,0,0); //Set the screen mode for allegro messages
  allegro_message("Could not load Snake.bmp");
  exit(EXIT_FAILURE);
}
     playerx = 0;
     playery = 0;
     Frame = 0;
}
BITMAP* Player::PlayerSprite()
{
       return Player_Sprite;
}
int Player::Player_X()
{
       return playerx;
}
int Player::Player_Y()
{
       return playery;
}
int Player::Player_Frame()
{
       return Frame;
}

Code: Select all

//Main.cpp
#include "Player.h"
#include "Collision.h"
#include "Map.h"
#include "Draw.h"


void init();
void deinit();

int main() 
{
    init();
    Player P;
    Map m;
    Draw D;
    D.SetBuffer();
    P.SetPlayer();
    m.GetMap();
    while (!key[KEY_ESC]) 
    {
          
          clear_keybuf();
        clear_bitmap(D.bBuffer());         
        acquire_screen();
    
    
    for (int r = 0; r < 12; r++)
    {
     
     for( int c = 0; c < 16; c++)
     {
     D.DrawMap(m.MapCoor(c, r), r, c);
     }
     }
     D.DrawSprite(P.PlayerSprite(), P.Player_X(), P.Player_Y());
     D.DrawBuffer();
     release_screen();
     rest(50);
     }
     

   deinit();

}
END_OF_MAIN()


void init() {
	int depth, res;
	allegro_init();
	depth = desktop_color_depth();
	if (depth == 0) depth = 32;
	set_color_depth(depth);
	res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
	if (res != 0) {
		allegro_message(allegro_error);
		exit(-1);
	}

	install_timer();
	install_keyboard();
	install_mouse();

}

void deinit() {
	clear_keybuf();

}


I think the problem is that Im not loading the Bitmap right...

Code: Select all

void Player::SetPlayer()
{
     Player_Sprite = load_bitmap("Snake.bmp", NULL);
     if(Player_Sprite == NULL)
{
  set_gfx_mode(GFX_TEXT,0,0,0,0); //Set the screen mode for allegro messages
  allegro_message("Could not load Snake.bmp");
  exit(EXIT_FAILURE);
}
That should be working but its not... Or I can be wrong and its the draw function thats wrong

Code: Select all

void Draw::DrawSprite(BITMAP* sprite, int x, int y)
{
      masked_blit(sprite, Buffer, 0, 0, x, y, 172,64);
      
}

:oops: :oops: :x

I think Im gonna just forget Allegro and try SDL... sounds more easier... Unless some one gives me a reason to stay with Allegro...
Dunc
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 6
Joined: Sun Oct 18, 2009 12:18 pm
Current Project: Battleship Variation
Favorite Gaming Platforms: Dreamcast, Saturn, PC, and N64
Programming Language of Choice: C/++
Location: Ohio
Contact:

Re: Cannot Find The Problem

Post by Dunc »

Could you post a screenshot of your map loading. That would help to determine the problem.
User avatar
davidthefat
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 529
Joined: Mon Nov 10, 2008 3:51 pm
Current Project: Fully Autonomous Robot
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: California
Contact:

Re: Cannot Find The Problem

Post by davidthefat »

Dunc wrote:Could you post a screenshot of your map loading. That would help to determine the problem.
its loading fine, its just when I add the sprite draw, its opens fora split second and closes, so the sprite draw is messed up
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Cannot Find The Problem

Post by Bakkon »

Is Buffer initialized before using it? You should add some debug and error checking messages.
User avatar
davidthefat
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 529
Joined: Mon Nov 10, 2008 3:51 pm
Current Project: Fully Autonomous Robot
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: California
Contact:

Re: Cannot Find The Problem

Post by davidthefat »

Bakkon wrote:Is Buffer initialized before using it? You should add some debug and error checking messages.
Yes it is, well IDK I put how to check for stuff and it all points to the loading BITMAP


:lol: :lol: :lol: :lol: :lol: I FIXED it... I made the Player sprite into the Draw class and made a function to draw it... not in the player class

Image

It get cut off so thats the link to full image
http://i34.tinypic.com/2ugdoba.jpg

I really need a way to organize the draw function... Any Ideas?
User avatar
davidthefat
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 529
Joined: Mon Nov 10, 2008 3:51 pm
Current Project: Fully Autonomous Robot
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: California
Contact:

Re: Cannot Find The Problem

Post by davidthefat »

So Im porting the few things I have in Allegro to SDL... I Like the SDL way better than Allegro, IDK why but I just like it better, I think Im sticking with SDL

I think it might be because I have more control over things compared to SDL, like the transparency color and stuff
User avatar
davidthefat
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 529
Joined: Mon Nov 10, 2008 3:51 pm
Current Project: Fully Autonomous Robot
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: California
Contact:

Re: Cannot Find The Problem

Post by davidthefat »

I dont know if Im doing it as efficient It can get... I think Im using too much resources.

Code: Select all

//Main.cpp

#include "Player.h"
#include "Collision.h"
#include "Map.h"
#include "Draw.h"
#include "Keyboard.h"
#include "Timer.h"

//The attributes of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The frame rate
const int FRAMES_PER_SECOND = 30;


//The surfaces that will be used

SDL_Surface *screen = NULL;

void clean_up()
{
    //Quit SDL
    SDL_FreeSurface( screen );
    SDL_Quit();
}

Draw D;
Player P;
Player M;
Keyboard K;
Timer fps;
SDL_Event event;
int main( int argc, char* args[] )
{
    bool quit = false;
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return 1;
    }

    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
    D.SetClip();
    P.SetCoor(0, 0);
    P.SetFrame(0);
    M.SetCoor(P.Player_X() - 3, P.Player_Y() + 15);
    M.SetFrame(3);
    K.ResetVel();
    //Load the images
    D.SetPlayerSprite( D.load_image( "Snake.bmp" ));
    //Makes pink transparent
    SDL_SetColorKey(D.PlayerSprite(), SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 255, 0, 255));

    //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return 1;
    }

    //Set the window caption
    SDL_WM_SetCaption( "Hello World", NULL );
    while (quit == false)
    {
    while (SDL_PollEvent(&event))
    {
          K.Input(event);
          P.SetFrame(K.frame());
          M.SetFrame(K.frame() + 3);
          if( event.type == SDL_QUIT ) 
          {
              quit = true; 
          }
    }
    P.Move(K.xV(), K.yV());
    //Fill the screen white
        SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
        
    M.SetCoor(P.Player_X(), P.Player_Y() + 13);
    //Draws
    D.DrawSprite( M.Player_X(), M.Player_Y(), D.PlayerSprite(), screen , M.Player_Frame());
    D.DrawSprite( P.Player_X(), P.Player_Y(), D.PlayerSprite(), screen , P.Player_Frame());
    
    //Update the screen
    if( SDL_Flip( screen ) == -1 )
    {
        return 1;
    }
    //Cap the frame rate
        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
        {
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
        }


    

    }
    clean_up();

    return 0;
}



Code: Select all

//Draw.cpp
#include "Draw.h"

SDL_Surface *Draw::load_image( std::string filename )
{
    //Temporary storage for the image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = SDL_LoadBMP( filename.c_str() );

    //If nothing went wrong in loading the image
    if( loadedImage != NULL )
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old image
        SDL_FreeSurface( loadedImage );
    }

    //Return the optimized image
    return optimizedImage;
}

void Draw::DrawSprite( int x, int y, SDL_Surface* source, SDL_Surface* destination, int frame)
{
    //Make a temporary rectangle to hold the offsets
    SDL_Rect offset;
    SDL_Rect *clip = &sFrame[frame];

    //Give the offsets to the rectangle
    offset.x = x;
    offset.y = y;

    //Blit the surface
    SDL_BlitSurface( source, clip, destination, &offset );
}
void Draw::SetClip()
{
     sFrame[ 0 ].x = 0; 
     sFrame[ 0 ].y = 0; 
     sFrame[ 0 ].w = 48; 
     sFrame[ 0 ].h = 32; 
     for (int c = 1; c < 23 ; c++)
     {
         sFrame[ c ].x = sFrame[ c -1 ].x + 48;
         sFrame[ c ].y = 0;
         sFrame[ c ].w = 48; 
         sFrame[ c ].h = 32;
         if ( c >= 12)
         {
              sFrame[ 12 ].x = 0;
              sFrame[ c ].y = 32;
         }
     }
}
SDL_Surface *Draw::PlayerSprite()
{
            return Player;
}
SDL_Surface *Draw::SetPlayerSprite(SDL_Surface *Temp)
{
            Player = Temp;
            return Player;
}

Code: Select all

//Draw.h
#include "SDL/SDL.h" 
#include <string>
#ifndef DRAW_H
#define DRAW_H
class Draw
{
     private:
             SDL_Surface* loadedImage;
             SDL_Surface* optimizedImage;
             SDL_Surface *Player;
             SDL_Rect sFrame[ 22 ]; 
     public:
            SDL_Surface *load_image( std::string filename );
            void DrawSprite( int x, int y, SDL_Surface* source, SDL_Surface* destination, int frame);
            void SetClip();
            SDL_Surface *PlayerSprite();
            SDL_Surface *SetPlayerSprite(SDL_Surface *Temp);
};
#endif

Code: Select all

//Player.cpp
#include "Player.h"



void Player::SetCoor(int x, int y)
{
     playerx = x;
     playery = y;
}

int Player::Player_X()
{
       return playerx;
}
int Player::Player_Y()
{
       return playery;
}
int Player::Player_Frame()
{
       return Frame;
}
void Player::SetFrame(int temp)
{
     Frame = temp;
}
void Player::Move(int xVel, int yVel)
{
     playerx += xVel;
     playery += yVel;
}

Code: Select all

//Player.h

#ifndef PLAYER_H
#define PLAYER_H
class Player
{
      private:
              int playerx;
              int playery;
              int Frame;
      public:
             void SetCoor(int x, int y);
             void SetFrame(int temp);
             void Move(int xVel, int yVel);
             int Player_X();
             int Player_Y();
             int Player_Frame();
             
              
                 
};
#endif

Code: Select all

//Keyboard.cpp

#include "Keyboard.h"

void Keyboard::Input(SDL_Event event)
{
     if( event.type == SDL_KEYDOWN ) 
     {
         switch( event.key.keysym.sym ) 
         {
                 case SDLK_UP: yVel -= 1; break; 
                 case SDLK_DOWN: yVel += 1; break; 
                 case SDLK_LEFT: xVel -= 2; tFrame = 0; break; 
                 case SDLK_RIGHT: xVel += 2; tFrame = 12; break; 
         }
     }
     else if( event.type == SDL_KEYUP ) 
     {
          switch( event.key.keysym.sym ) 
         {
                 case SDLK_UP: yVel += 1; break; 
                 case SDLK_DOWN: yVel -= 1; break; 
                 case SDLK_LEFT: xVel += 2; break; 
                 case SDLK_RIGHT: xVel -= 2; break; 
         }
     }
     
}
void Keyboard::ResetVel()
{
     xVel = 0;
     yVel = 0;
}
int Keyboard::xV()
{
     return xVel;
}
int Keyboard::yV()
{
     return yVel;
}

int Keyboard::frame()
{
     return tFrame;
}


Code: Select all

//Keyboard.h

#include "SDL/SDL.h" 
#ifndef KEYBOARD_H
#define KEYBOARD_H

class Keyboard
{
      private:
              int xVel;
              int yVel;
              int tFrame;
              
      public:
             void ResetVel();
             void Input(SDL_Event event);
             int xV();
             int yV();
             int frame();
};

#endif

Code: Select all

#include "SDL/SDL.h" 
#ifndef TIMER_H
#define TIMER_H
class Timer
{
    private:
    //The clock time when the timer started
    int startTicks;

    //The ticks stored when the timer was paused
    int pausedTicks;

    //The timer status
    bool paused;
    bool started;

    public:
    //Initializes variables
    Timer();

    //The various clock actions
    void start();
    void stop();
    void pause();
    void unpause();

    //Gets the timer's time
    int get_ticks();

    //Checks the status of the timer
    bool is_started();
    bool is_paused();
};
#endif

Code: Select all

#include "Timer.h"

Timer::Timer()
{
    //Initialize the variables
    startTicks = 0;
    pausedTicks = 0;
    paused = false;
    started = false;
}

void Timer::start()
{
    //Start the timer
    started = true;

    //Unpause the timer
    paused = false;

    //Get the current clock time
    startTicks = SDL_GetTicks();
}

void Timer::stop()
{
    //Stop the timer
    started = false;

    //Unpause the timer
    paused = false;
}

void Timer::pause()
{
    //If the timer is running and isn't already paused
    if( ( started == true ) && ( paused == false ) )
    {
        //Pause the timer
        paused = true;

        //Calculate the paused ticks
        pausedTicks = SDL_GetTicks() - startTicks;
    }
}

void Timer::unpause()
{
    //If the timer is paused
    if( paused == true )
    {
        //Unpause the timer
        paused = false;

        //Reset the starting ticks
        startTicks = SDL_GetTicks() - pausedTicks;

        //Reset the paused ticks
        pausedTicks = 0;
    }
}

int Timer::get_ticks()
{
    //If the timer is running
    if( started == true )
    {
        //If the timer is paused
        if( paused == true )
        {
            //Return the number of ticks when the timer was paused
            return pausedTicks;
        }
        else
        {
            //Return the current time minus the start time
            return SDL_GetTicks() - startTicks;
        }
    }

    //If the timer isn't running
    return 0;
}

bool Timer::is_started()
{
    return started;
}

bool Timer::is_paused()
{
    return paused;
}

Image
Post Reply