Page 2 of 3

Re: Cannot Find The Problem

Posted: Thu Oct 29, 2009 8:34 pm
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.

Re: Cannot Find The Problem

Posted: Thu Oct 29, 2009 8:42 pm
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

Re: Cannot Find The Problem

Posted: Thu Oct 29, 2009 9:35 pm
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

Re: Cannot Find The Problem

Posted: Fri Oct 30, 2009 12:31 am
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?

Re: Cannot Find The Problem

Posted: Fri Oct 30, 2009 12:41 am
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.

Re: Cannot Find The Problem

Posted: Fri Oct 30, 2009 2:34 am
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.

Re: Cannot Find The Problem

Posted: Fri Oct 30, 2009 9:24 am
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.

Re: Cannot Find The Problem

Posted: Fri Oct 30, 2009 6:59 pm
by ibly31
Didnt look at the code for more than 10 seconds, but

using namespace std;

??? after the includes

Re: Cannot Find The Problem

Posted: Fri Oct 30, 2009 8:04 pm
by MarauderIIC
We solved that problem.

Re: Cannot Find The Problem

Posted: Fri Oct 30, 2009 11:46 pm
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 ;)

Re: Cannot Find The Problem

Posted: Sun Nov 01, 2009 5:01 pm
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.

Re: Cannot Find The Problem

Posted: Sun Nov 01, 2009 5:58 pm
by davidthefat
Fixed my map loading and drawing... Found out that I formatted the map file wring, I put commas... :roll:

Re: Cannot Find The Problem

Posted: Mon Nov 02, 2009 8:59 pm
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... :(

Re: Cannot Find The Problem

Posted: Mon Nov 02, 2009 9:06 pm
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.

Re: Cannot Find The Problem

Posted: Tue Nov 03, 2009 9:03 pm
by davidthefat
Should I just stick with the bounding box collision or do a pixel to pixel collision?