Page 1 of 3

Cannot Find The Problem

Posted: Sun Oct 25, 2009 7:12 pm
by davidthefat

Code: Select all

#include <allegro.h>

int x = 10;
int y = 10;

bool collision = false;
int bass_bb_left = 0;
int bass_bb_top = 0;
int bass_bb_right = 0;
int bass_bb_bottom = 0;

int m_bb_left = 0;
int m_bb_top = 0;
int m_bb_right = 0;
int m_bb_bottom = 0;

int cmap_left[12][16];
int cmap_top[12][16];
int cmap_right[12][16];
int cmap_bottom[12][16];

int map[12][16] = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2},
                   {1,1,1,1,1,1,1,1,1,1,2,3,3,3,3,3},
                   {1,1,1,1,1,1,1,1,1,1,2,3,3,3,3,4},
                   {1,1,1,1,1,1,1,1,1,1,2,3,3,3,3,3},
                   {1,1,1,1,1,1,1,1,1,1,2,3,3,3,3,3}};



BITMAP *my_pic = NULL; 

int startX[18] = {3,19,51,94,143,186,220,255,290,4,52,84,127,189,236,280,315,349};
int startY[18] = {0,0,0,0,0,12,12,12,12,69,69,69,69,69,69,69,69,69};
int endX[18] = {14,27,39,45,39,31,31,31,41,39,29,33,42,45,40,28,32,41};
int endY[18] = {64,64,64,64,64,52,52,52,52,48,48,48,48,48,48,48,48,48};
int frame = 0;

void setup();
void game();
void init();
void deinit();

int main() 
{

    setup();

}
END_OF_MAIN()
void setup()
{
     init();

     game();

	deinit();

}
void game()
{
     my_pic = load_bitmap("bass1.bmp", NULL); 
BITMAP *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);
}

     

     	while (!key[KEY_ESC]) 
         {

		clear_keybuf();
        clear_bitmap(Buffer);         
        acquire_screen();

                
                //update bounding Boxes
        bass_bb_left = x;
        bass_bb_top = y;
        bass_bb_right = bass_bb_left + endX[frame];
        bass_bb_bottom = bass_bb_top + endY[frame];
        m_bb_left = mouse_x;
        m_bb_top = mouse_y;
        m_bb_right = m_bb_left + endX[frame];
        m_bb_bottom = m_bb_top + endY[frame];
        
        
        for (int i = 0; i <= 12; i++)
        {
     
        for( int t = 0; t <= 16; t++)
        {
            
            if( map[i][t] == 1)
            {
                 rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 440, makecol( 128, 255, 255));
                 cmap_left[i][t] = 0;
            cmap_top[i][t] = 0;
            cmap_right[i][t] = 0;
            cmap_bottom[i][t] = 0;
            }
            else if( map[i][t] == 2) 
            {
                 rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, makecol( 255, 128, 0));
                 cmap_left[i][t] = t * 40;
            cmap_top[i][t] = i * 40;
            cmap_right[i][t] = (t + 1) * 40;
            cmap_bottom[i][t] = (i + 1) * 40;
            }
            else if( map[i][t] == 3) 
            {
                 rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, makecol( 255, 0, 0));
                 cmap_left[i][t] = 0;
            cmap_top[i][t] = 0;
            cmap_right[i][t] = 0;
            cmap_bottom[i][t] = 0;
            }
            else if( map[i][t] == 4) 
            {
                 rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, makecol( 0, 0, 0));
                 cmap_left[i][t] = 0;
            cmap_top[i][t] = 0;
            cmap_right[i][t] = 0;
            cmap_bottom[i][t] = 0;
                 }
            
            
           }       
        
    }  
       collision = true;
        
        if(bass_bb_bottom < m_bb_top)
        {
        collision = false;
        }
        
        else if(bass_bb_top > m_bb_bottom)
        {
        collision = false;
        }
        
        else if(bass_bb_right < m_bb_left)
        {
        collision = false;
        }
        
        else if(bass_bb_left > m_bb_right)
        {
        collision = false;
        }
        
        //Map Collision
        for (int r = 0; r <= 12; r++){
     
        for( int c = 0; c <= 16; c++){
             if(bass_bb_bottom < cmap_top[r][c] && cmap_top[r][c] != 0)
        {
        collision = false;
        }
        
        else if(bass_bb_top > cmap_bottom[r][c] && cmap_bottom[r][c] != 0)
        {
        collision = false;
        }
        
        else if(bass_bb_right < cmap_left[r][c] && cmap_left[r][c] != 0)
        {
        collision = false;
        }
        
        else if(bass_bb_left > cmap_right[r][c] && cmap_right[r][c] != 0)
        {
        collision = false;
        }
        }
        }

        
        
        //get keyboard
        if (key[KEY_UP]) 
        {
             y -= 3;
             if(frame >= 9 && frame <= 16)
             {
             frame ++;
             }
             else if(frame < 9)
             {
                  frame = 9;
             }
             else if (frame >= 17)
        {
        frame = 9;
        }
        }        
        else if (key[KEY_DOWN]) 
        {
             y += 3;   

             if(frame >= 9 && frame <= 16)
             {
             frame ++;
             }
             else if(frame < 9)
             {
                  frame = 9;
             }
             else if (frame >= 17)
        {
        frame = 9;
        }
        }
        else if (key[KEY_RIGHT])
        {
             x += 3;

             if(frame >= 9 && frame <= 16)
             {
             frame ++;
             }
             else if(frame < 9)
             {
                  frame = 9;
             }
             else if (frame >= 17)
        {
        frame = 9;
        }
        }
        else if (key[KEY_LEFT]) 
        {
             
             x -= 3;

             if(frame >= 9 && frame <= 16)
             {
             frame ++;
             }
             else if(frame < 9)
             {
                  frame = 9;
             }
             else if (frame >= 17)
        {
        frame = 9;
        }
        }
        else 
        {
             frame = 7;
        }
        if(collision == true)
        {
              textout_ex( Buffer, font, "Collision!", 0, 0, makecol( 0, 0, 255), makecol( 0, 0, 0) );
              frame = 5;
              

        }
        

        //sprite
        masked_blit(my_pic, Buffer, startX[frame],startY[frame],x,y,endX[frame],endY[frame]);
        masked_blit(my_pic, Buffer, startX[frame],startY[frame],mouse_x,mouse_y,endX[frame],endY[frame]);
        /*
        line(Buffer, bass_bb_left, bass_bb_top, bass_bb_right, bass_bb_top, makecol(255,0,0));
        line(Buffer, bass_bb_left, bass_bb_bottom, bass_bb_right, bass_bb_bottom, makecol(255,0,0));
        line(Buffer, bass_bb_left, bass_bb_top, bass_bb_left, bass_bb_bottom, makecol(255,0,0));
        line(Buffer, bass_bb_right, bass_bb_top, bass_bb_right, bass_bb_bottom, makecol(255,0,0));

        line(Buffer, m_bb_left, m_bb_top, m_bb_right, m_bb_top, makecol(255,0,0));
        line(Buffer, m_bb_left, m_bb_bottom, m_bb_right, m_bb_bottom, makecol(255,0,0));
        line(Buffer, m_bb_left, m_bb_top, m_bb_left, m_bb_bottom, makecol(255,0,0));
        line(Buffer, m_bb_right, m_bb_top, m_bb_right, m_bb_bottom, makecol(255,0,0));
        */
        
        if (collision == false)
        {
        y += 10;

        }
        if (y >= 480)
        {
              y =0;
        }
        
        
        draw_sprite( screen, Buffer, 0, 0);

        release_screen();
        
        rest(50);


        }
}
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();

}


pretty messy code, I know I should organize them into classes and stuff, but its bugging me, Im trying to draw the map and add collision onto the map, but not working... the collision with the mouse and drawing the map works but when I added the collision stuff for the map, it just makes a black blank screen and exits in like 1 second... I know the problem is here but I see nothing wrong...

Code: Select all

for (int i = 0; i <= 12; i++)
        {
     
        for( int t = 0; t <= 16; t++)
        {
           
            if( map[i][t] == 1)
            {
                 rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 440, makecol( 128, 255, 255));
                 cmap_left[i][t] = 0;
            cmap_top[i][t] = 0;
            cmap_right[i][t] = 0;
            cmap_bottom[i][t] = 0;
            }
            else if( map[i][t] == 2)
            {
                 rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, makecol( 255, 128, 0));
                 cmap_left[i][t] = t * 40;
            cmap_top[i][t] = i * 40;
            cmap_right[i][t] = (t + 1) * 40;
            cmap_bottom[i][t] = (i + 1) * 40;
            }
            else if( map[i][t] == 3)
            {
                 rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, makecol( 255, 0, 0));
                 cmap_left[i][t] = 0;
            cmap_top[i][t] = 0;
            cmap_right[i][t] = 0;
            cmap_bottom[i][t] = 0;
            }
            else if( map[i][t] == 4)
            {
                 rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, makecol( 0, 0, 0));
                 cmap_left[i][t] = 0;
            cmap_top[i][t] = 0;
            cmap_right[i][t] = 0;
            cmap_bottom[i][t] = 0;
                 }
           
           
           }       
       
    }  

Re: Cannot Find The Problem

Posted: Sun Oct 25, 2009 7:30 pm
by ibly31
I'm not sure if this is is:

When you create an array of ints(or whatever) if the declaration is "int blah[12] = {etc,etc}" then that means you have 12 objects (0 through 11). So when you ask for the blah[12] object, it's like "wtf there is none" and exits. Try looping like "for(int i = 0; i <= 11; i++){" and same for the 16.

Basically when your looping from 0 to 12 and 0 to 16, your asking for 13 and 17 different objects respectively.

Re: Cannot Find The Problem

Posted: Sun Oct 25, 2009 7:33 pm
by davidthefat
ibly31 wrote:I'm not sure if this is is:

When you create an array of ints(or whatever) if the declaration is "int blah[12] = {etc,etc}" then that means you have 12 objects (0 through 11). So when you ask for the blah[12] object, it's like "wtf there is none" and exits. Try looping like "for(int i = 0; i <= 11; i++){" and same for the 16.

Basically when your looping from 0 to 12 and 0 to 16, your asking for 13 and 17 different objects respectively.
:) That fixed the drawing and running part, now the collision isnt working, which means my map collision equation if all ****ed up...

Re: Cannot Find The Problem

Posted: Tue Oct 27, 2009 7:35 pm
by MarauderIIC
Most conventions follow

int blah[12];
for (int i = 0;i < 12...)

rather than
for (int i = 0;i <= 11...)

Re: Cannot Find The Problem

Posted: Tue Oct 27, 2009 9:42 pm
by Bakkon
MarauderIIC wrote:Most conventions follow

int blah[12];
for (int i = 0;i < 12...)

rather than
for (int i = 0;i <= 11...)
Reiterating this because its extremely helpful for dynamic uses.

Code: Select all

int* blah = new int[n];
for(int i = 0; i < n; i++) {}
delete blah;

Re: Cannot Find The Problem

Posted: Tue Oct 27, 2009 10:20 pm
by avansc
dont know what the issue with that for loop is...
both of them iterate exactly the same.

mar is right in saying that the convention is < and not <=, but thats just out of a readability point of view
its easy to see its a 12 wide array.. rather than having to introduce the possibility of mistaking it for an 11 wide array.

edit: i just now saw he had <= 12 when having a 12 wide array.. yea that will cause issues.

Re: Cannot Find The Problem

Posted: Tue Oct 27, 2009 11:52 pm
by davidthefat

Code: Select all

 //Map Collision
        
        if(bass_bb_bottom > cmap_top[(y + endY[frame])/40][x/40] && map_col[(y + endY[frame])/40][x/40] == 1)
        {
        collision = true;
        }
        
        else if(bass_bb_top < cmap_bottom[(y - endY[frame])/40][x/40] && map_col[(y - endY[frame])/40][x/40] == 1)
        {
        collision = true;
        }
        
        else if(bass_bb_right > cmap_left[y/40][(x + endX[frame])/40] && map_col[y/40][(x + endX[frame])/40] == 1)
        {
        collision = true;
        }
        
        else if(bass_bb_left < cmap_right[y/40][(x - endX[frame])/40]&& map_col[y/40][(x - endX[frame])/40] == 1)
        {
        collision = true;
        }

:lol: :lol: :lol: :lol: :lol: :roll: :roll: FINALLY I fixed it

now time to go pack it into classes

Re: Cannot Find The Problem

Posted: Wed Oct 28, 2009 9:08 pm
by davidthefat
Ah damn it, its the buggiest collision ever coded... :x I guess start the equation over again

Re: Cannot Find The Problem

Posted: Wed Oct 28, 2009 9:35 pm
by Innerscope
davidthefat wrote:Ah damn it, its the buggiest collision ever coded... :x I guess start the equation over again
You should try refactoring your code. It'll make it easier to debug (and read for that matter), and give you less headache.

Re: Cannot Find The Problem

Posted: Wed Oct 28, 2009 11:12 pm
by Falco Girgis
davidthefat, you need to reconsider your data organization. Storing everything as a global is frowned upon from any point of view. You could at least do something aesthetically pleasing like throwing it all into a bullshit singleton and pretending like you aren't raping the foundations of object oriented design in the ass. ;)

...

But on a more serious note. Have you learned classes yet? Have you learned functions either? I suggest looking into both for the sake of your sanity.

Re: Cannot Find The Problem

Posted: Wed Oct 28, 2009 11:32 pm
by davidthefat
GyroVorbis wrote:davidthefat, you need to reconsider your data organization. Storing everything as a global is frowned upon from any point of view. You could at least do something aesthetically pleasing like throwing it all into a bullshit singleton and pretending like you aren't raping the foundations of object oriented design in the ass. ;)

...

But on a more serious note. Have you learned classes yet? Have you learned functions either? I suggest looking into both for the sake of your sanity.
Yea Learned classes and functions, just dont like adding them unless I know what Im doing, like collision is new ground for me, so I figure that out before putting it into classes so I know its not the classes that are wrong but the actual function is wrong

so instead of collision true or false, I can make a class with a function that checks the collision and returns it as true or false. I know, I just dont apply :lol:

Re: Cannot Find The Problem

Posted: Thu Oct 29, 2009 12:45 am
by Innerscope
davidthefat wrote:
GyroVorbis wrote:davidthefat, you need to reconsider your data organization. Storing everything as a global is frowned upon from any point of view. You could at least do something aesthetically pleasing like throwing it all into a bullshit singleton and pretending like you aren't raping the foundations of object oriented design in the ass. ;)

...

But on a more serious note. Have you learned classes yet? Have you learned functions either? I suggest looking into both for the sake of your sanity.
Yea Learned classes and functions, just dont like adding them unless I know what Im doing, like collision is new ground for me, so I figure that out before putting it into classes so I know its not the classes that are wrong but the actual function is wrong

so instead of collision true or false, I can make a class with a function that checks the collision and returns it as true or false. I know, I just dont apply :lol:
You should really break this up into classes. Also, your using a lot more resources than necessary. Is there any particular reason you have so many arrays? When I look at the collision portion of the code I see your doing some psuedo-pixel level collision by using what I presume is the characters top-most pixel coordinate at a particular frame in its animation. This isn't the way you should handle this. Although I could be completely off on what I just said, because I don't see any particular naming convention in your code, which is another problem. If you'd like more help, it would be a good idea if you explained what every variable was supposed to do. (it would also help you figure some things out)
Hope that made some sense...

Re: Cannot Find The Problem

Posted: Thu Oct 29, 2009 7:05 pm
by davidthefat
Innerscope wrote:
davidthefat wrote:
GyroVorbis wrote:davidthefat, you need to reconsider your data organization. Storing everything as a global is frowned upon from any point of view. You could at least do something aesthetically pleasing like throwing it all into a bullshit singleton and pretending like you aren't raping the foundations of object oriented design in the ass. ;)

...

But on a more serious note. Have you learned classes yet? Have you learned functions either? I suggest looking into both for the sake of your sanity.
Yea Learned classes and functions, just dont like adding them unless I know what Im doing, like collision is new ground for me, so I figure that out before putting it into classes so I know its not the classes that are wrong but the actual function is wrong

so instead of collision true or false, I can make a class with a function that checks the collision and returns it as true or false. I know, I just dont apply :lol:
You should really break this up into classes. Also, your using a lot more resources than necessary. Is there any particular reason you have so many arrays? When I look at the collision portion of the code I see your doing some psuedo-pixel level collision by using what I presume is the characters top-most pixel coordinate at a particular frame in its animation. This isn't the way you should handle this. Although I could be completely off on what I just said, because I don't see any particular naming convention in your code, which is another problem. If you'd like more help, it would be a good idea if you explained what every variable was supposed to do. (it would also help you figure some things out)
Hope that made some sense...
1 class for the player with the x and y coordinates and the frames and ect
1 class for collision
1 class for the loading of the map
1 class for drawing everything
Is that enough for now? BRB coding

Re: Cannot Find The Problem

Posted: Thu Oct 29, 2009 7:38 pm
by Innerscope
davidthefat wrote:1 class for the player with the x and y coordinates and the frames and ect
1 class for collision
1 class for the loading of the map
1 class for drawing everything
Is that enough for now? BRB coding
So your classes are: Player, Collision Handler, Map Loader, Surface. That sounds good to me.

Re: Cannot Find The Problem

Posted: Thu Oct 29, 2009 8:15 pm
by davidthefat
OMG, now the simplest crap isnt working... ifstream is not working...

Code: Select all

//Map.h
#ifndef MAP_H
#define MAP_H
#include <iostream>
#include <fstream>
class Map
{
      private:
              int map[12][16];
      public:
             void GetMap();
};
#endif

Code: Select all

//Map.cpp

#include "Map.h"

void Map::GetMap()
{
ifstream infile("Default.map");
                   
for (int j=0; j < 12; j++)
{
for (int i=0; i < 16; i++)
{
int temp;
infile >> temp;
map[i][j] = temp;
}
}
                   
infile.close();

}
      
      

:x :oops: The error is that ifstream is undeclared, it should work but no, the compiler is choosing to be gay...