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
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:

Cannot Find The Problem

Post 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;
                 }
           
           
           }       
       
    }  
User avatar
ibly31
Chaos Rift Junior
Chaos Rift Junior
Posts: 312
Joined: Thu Feb 19, 2009 8:47 pm
Current Project: Like... seven different ones
Favorite Gaming Platforms: Xbox 360, Gamecube
Programming Language of Choice: C++, ObjC
Location: New Jersey.

Re: Cannot Find The Problem

Post 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.
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
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 »

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...
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Cannot Find The Problem

Post by MarauderIIC »

Most conventions follow

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

rather than
for (int i = 0;i <= 11...)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
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 »

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;
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Cannot Find The Problem

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
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 »

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
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 »

Ah damn it, its the buggiest collision ever coded... :x I guess start the equation over again
User avatar
Innerscope
Chaos Rift Junior
Chaos Rift Junior
Posts: 200
Joined: Mon May 04, 2009 5:15 pm
Current Project: Gridbug
Favorite Gaming Platforms: NES, SNES
Programming Language of Choice: Obj-C, C++
Location: Emeryville, CA
Contact:

Re: Cannot Find The Problem

Post 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.
Current Project: Gridbug
Website (under construction) : http://www.timcool.me
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Cannot Find The Problem

Post 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.
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 »

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:
User avatar
Innerscope
Chaos Rift Junior
Chaos Rift Junior
Posts: 200
Joined: Mon May 04, 2009 5:15 pm
Current Project: Gridbug
Favorite Gaming Platforms: NES, SNES
Programming Language of Choice: Obj-C, C++
Location: Emeryville, CA
Contact:

Re: Cannot Find The Problem

Post 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...
Current Project: Gridbug
Website (under construction) : http://www.timcool.me
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 »

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
User avatar
Innerscope
Chaos Rift Junior
Chaos Rift Junior
Posts: 200
Joined: Mon May 04, 2009 5:15 pm
Current Project: Gridbug
Favorite Gaming Platforms: NES, SNES
Programming Language of Choice: Obj-C, C++
Location: Emeryville, CA
Contact:

Re: Cannot Find The Problem

Post 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.
Current Project: Gridbug
Website (under construction) : http://www.timcool.me
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 »

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...
Post Reply