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

andrew
Chaos Rift Regular
Chaos Rift Regular
Posts: 121
Joined: Mon Dec 08, 2008 2:12 pm

Re: Cannot Find The Problem

Post by andrew »

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

Code: Select all

using namespace std;
ifstream lives in the std namespace.
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 »

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

Code: Select all

using namespace std;
ifstream lives in the std namespace.
:nono: I feel so stupid...

thanks BTW
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

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


void init();
void deinit();

int main() 
{
    init();
    Map m;
    Draw D;
    D.SetBuffer();
    m.GetMap();
    while (!key[KEY_ESC]) 
    {
          
          clear_keybuf();
        clear_bitmap(D.bBuffer());         
        acquire_screen();
    
    
    for (int c = 0; c < 12; c++)
    {
     
     for( int r = 0; r < 16; r++)
     {
     D.DrawMap(m.MapCoor(c, r), c, r);
     }
     }
     D.DrawBitmap(D.bBuffer());
     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();

}

Code: Select all

//Player.h
#ifndef PLAYER_H
#define PLAYER_H
class Player
{
      
};
#endif

Code: Select all

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

Code: Select all

//Collision.cpp
#include "Collision.h"

Code: Select all

//Collision.h
#ifndef COLLISION_H
#define COLLISION_H
class Collision
{
      
};
#endif

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();
             int MapCoor(int c, int r);
};

#endif

Code: Select all

//Map.cpp

#include "Map.h"

void Map::GetMap()
{
std::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();

}

int Map::MapCoor(int c, int r)
{
    return map[c][r];
}   
      

Code: Select all

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

Code: Select all

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

int Draw::DrawMap(int flag, int t, int i)
{
   
            
            if( flag == 1)
            {
                 rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 440, makecol( 128, 255, 255));
            }
            else if( flag == 2) 
            {
                 rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, makecol( 255, 128, 0));
            }
            else if( flag == 3) 
            {
                 rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, makecol( 255, 0, 0));
            }
            else if( flag == 4) 
            {
                 rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, makecol( 0, 0, 0));
                 }
}
void Draw::SetBuffer()
{
     Buffer = create_bitmap( 640, 480);
}
BITMAP* Draw::bBuffer()
{
     return Buffer;
}
void Draw::DrawBitmap(BITMAP* draw)
{
      draw_sprite( screen, Buffer, 0, 0);
}

:oops: :roll: :| My Brain Hurts... Just leaving it here to check if I got any bad habits, bad syntax and ect... That took me like 2-3 hours... Wow, still the map doesnt draw correctly

BTW anyone know how to open a console window and print stuff out while running an allegro window for debugging usage? I dont think the map is getting read corectly or the draw function is wrong
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 »

Code: Select all

int Draw::DrawMap(int flag, int t, int i)
{
   
           
            if( flag == 1)
            {
                 rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 440, makecol( 128, 255, 255)); //I think this is the problem is y2?
            }
I've never used allegro, but I'm guessing you pass a rectfill(bitmap,x1,y1,x2,y2,color). Anyway I think there are more elegant ways of writing this, like using a switch statement. I would probably do something like this:

Code: Select all

int Draw::DrawMap(int flag, int t, int i) {
     Color mColor; //not sure about color types in allegro...
     mColor = getColor(flag);
     rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, mColor);
}
I'd probably have it return a color from a different function ("getColor"-which would probably use a switch instead of "if" statements). Which may or may not be to your liking.
I also like using named constants so I might do:

Code: Select all

enum {
		kCyan,
		kOrange,
		kRed,
		kBlack
	};
to represent the flag values.
BTW anyone know how to open a console window and print stuff out while running an allegro window for debugging usage? I dont think the map is getting read corectly or the draw function is wrong
Can you not just use cout?
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 »

Innerscope wrote:
BTW anyone know how to open a console window and print stuff out while running an allegro window for debugging usage? I dont think the map is getting read corectly or the draw function is wrong
Can you not just use cout?
Nope, I doubt it. He just has an application window.

You have two options. I don't know what IDE you're using.

1) Make a command-line project so that you have one window for Allegro+graphics, and one console in the background for printing things.
2) Make a cute little debug equivalent of printf/cout that writes to a debug.txt for you to read.
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 »

GyroVorbis wrote:
Innerscope wrote:
BTW anyone know how to open a console window and print stuff out while running an allegro window for debugging usage? I dont think the map is getting read corectly or the draw function is wrong
Can you not just use cout?
Nope, I doubt it. He just has an application window.
edit: Yea, my fault, I misread his question. Your 1st method.
Current Project: Gridbug
Website (under construction) : http://www.timcool.me
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 »

Code: Select all

int Draw::DrawMap(int flag, int t, int i)
{            
            if( flag == 1)
            {
                 rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 440, makecol( 128, 255, 255));
            }
            else if( flag == 2) 
            {
                 rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, makecol( 255, 128, 0));
            }
            else if( flag == 3) 
            {
                 rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, makecol( 255, 0, 0));
            }
            else if( flag == 4) 
            {
                 rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, makecol( 0, 0, 0));
                 }
}
See all the repetition? This means you're doing something wrong :)
If you make one change and have to change it in like four places... maintainability becomes a huge issue.

I agree with innerscope's solution of using another function, but at that point you'll want to use a pointer, so that you don't make a copy of the object... and now we're into dynamically allocated memory territory.
You could do a switch in the function itself as long as you don't need to reuse your "get color" code...

Code: Select all

drawMap(...) {
    Color color;
    switch (flag) {
        case 1:
            color = makecol(128, 255, 255);
            break;
        case 2:
        ....
    }
    rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, color);
}
Doing this kind of thing will make your problem easier to find.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
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 »

Didnt look at the code for more than 10 seconds, but

using namespace std;

??? after the includes
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
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 »

We solved that problem.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
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:Didnt look at the code for more than 10 seconds, but

using namespace std;

??? after the includes
Wouldnt that be a problem in the long run? Like that namespace might interfere with another?
GyroVorbis wrote:
1) Make a command-line project so that you have one window for Allegro+graphics, and one console in the background for printing things.
Just did that, easy as just going to project option and clicking window console ;)
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 »

davidthefat wrote:
ibly31 wrote:Didnt look at the code for more than 10 seconds, but

using namespace std;

??? after the includes
Wouldnt that be a problem in the long run? Like that namespace might interfere with another?
Not really, no. Unless you plan on naming your functions cout and your classes fstream.
If that does happen, you can always omit using namespace std; from the appropriate files and prefix your stuff with std::cout, mynamespace::cout, etc.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
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 »

Fixed my map loading and drawing... Found out that I formatted the map file wring, I put commas... :roll:
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 »

MarauderIIC wrote:
I agree with innerscope's solution of using another function, but at that point you'll want to use a pointer, so that you don't make a copy of the object... and now we're into dynamically allocated memory territory.
You could do a switch in the function itself as long as you don't need to reuse your "get color" code...
Elaborate on the pointers please, from the books I read, it only taught me HOW to use it, not WHY or WHERE to use it... :(
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 »

davidthefat wrote:
MarauderIIC wrote:
I agree with innerscope's solution of using another function, but at that point you'll want to use a pointer, so that you don't make a copy of the object... and now we're into dynamically allocated memory territory.
You could do a switch in the function itself as long as you don't need to reuse your "get color" code...
Elaborate on the pointers please, from the books I read, it only taught me HOW to use it, not WHY or WHERE to use it... :(
http://elysianshadows.com/phpBB3/viewto ... t=pointers

i'll finish that up. kinda forgot about it.
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 »

Should I just stick with the bounding box collision or do a pixel to pixel collision?
Post Reply