Thread: Text with Allegro

  1. #1
    Registered User godly 20's Avatar
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    29

    Question Text with Allegro

    Hi. I'm currently programming a small 2d game usign the allegro library. I need the game to print out a few messages. I tried with the "textout()" function but apparently it the text is drawn undea all the other levels and i only see it for a fraction of second when I close the app.
    How can I fix this?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Draw after all the other! Isn't it simple?
    Devoted my life to programming...

  3. #3
    Registered User godly 20's Avatar
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    29
    If it was that easy I wouldn't have posted this question here don't you think? I have tried to write it on anything but it still doesnt work. Atm I have a buffer, a backbuffer and the screen. And it doesnt work on any of these.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Could you show the relevant code, then? ( Please use code tags )
    Devoted my life to programming...

  5. #5
    Registered User godly 20's Avatar
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    29
    Code:
    #include <allegro.h>
    #include <iostream>
    using namespace std;
    BITMAP* ground;
    BITMAP* water;
    BITMAP* tree;
    BITMAP* rock;
    BITMAP* character;
    BITMAP* buffer;
    BITMAP *backbuffer; 
    BITMAP *tree2;
    
    //These will hold the player's position
    //instead of using pixels, we will use the position within the 2d array
    float x, y;
    int grounds, waters, trees, tree2s, rocks;
    
    //This will be our background, 1 = grass, 3 = water.
    int map[24][32] = {{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,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,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,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},
                       {3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
    
    //This will contain all the objects, 100 = player, 101 = tree, 102 = rock.
    int objMap[24][32] = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,102,0,103,0,0,0,103,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,101,0,0,0,0,101,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,101,0,0,0,0,101,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
    
    void moveChar()
    {
         while( !key[KEY_ESC])
         {
    		  blit(backbuffer, screen, 0, 0, 0, 0, buffer->w, buffer->h);
    		     
        		draw_sprite(backbuffer, buffer, 0,0) ;
    
    			draw_sprite(backbuffer, character, x, y) ;
    			
                if(key [KEY_LEFT]) x-=0.5 ;
                if((key [KEY_LEFT]) && (key [KEY_R])) x-=1;
                if(key [KEY_RIGHT]) x+=0.5;
                if((key [KEY_RIGHT]) && (key [KEY_R])) x+=1;      
                if(key [KEY_DOWN]) y+=0.5; 
                if((key [KEY_DOWN]) && (key [KEY_R])) y+=1;       
                if(key [KEY_UP]) y-=0.5;
                if((key [KEY_UP]) && (key [KEY_R])) y-=1; 
    
              if((x<=176)&&(y>=250)) //collision with map borders & objects
               {
                                      
                            y-=2;
                            
               }
               if((x<=176)&&(y>=250))
               {
                                      
                            x+=2;
                            
               }
                  if((x>=428)&&(y<=122))
               {
                                      
                            y+=2;
                            
               }
                  if((x>=428)&&(y<=122))
               {
                                      
                            x-=2;
                            
               }            
                                        
                
               if((x>=410)&&(y>=199))
               {
                                        x-=2;
                            
                            
               }       
               if((x>=410)&&(y>=199))
               {
                                        
                            y-=2;
                            
               }       
                              
                if(y<=0)
                y+=3;
                if(x<=0)
                x+=3;
                if(y>=417)
                y-=3;
                if(x>=583)
                x-=3;
                } 
        
        destroy_bitmap(buffer);
        destroy_bitmap(backbuffer);
        destroy_bitmap(character);
        release_screen();
        rest(100);
    
    } 
    
    void setupGame()
    {
        buffer = create_bitmap( 640, 480);
    	backbuffer = create_bitmap( 640, 480);
    	
        ground = load_bitmap( "ground2.bmp", NULL);
        water = load_bitmap( "water(2).bmp", NULL);
        tree = load_bitmap( "sprite-0001.bmp", NULL);
        rock = load_bitmap( "Immagine.bmp", NULL);
        character = load_bitmap( "anim2.bmp", NULL);
        tree2 = load_bitmap( "albero2.bmp", NULL);
        grounds = 1;
        waters = 3;
        trees = 101;
        tree2s = 103;
        rocks = 102;
    
      //  draw_sprite( screen, buffer, 0, 0);
              
    
    	for (int i = 0; i <= 24; i++){
         
            for( int t = 0; t <= 32; t++){
                
                if( map[i][t] == 1) draw_sprite( buffer, ground, t * 20, i * 20);
                else if( map[i][t] == 3) draw_sprite( buffer, water, t * 20, i * 20);
                
               }       
            
        }  
        
        //draw_sprite( screen, buffer, 0, 0);
        
            for (int i = 0; i <= 24; i++){
         
            for( int t = 0; t <= 32; t++){
                
                if( objMap[i][t] == 101) draw_sprite( buffer, tree, t * 20, i * 20);
                else if( objMap[i][t] == 102) draw_sprite( buffer, rock, t * 20, i * 20);
                else if( objMap[i][t] == 103) draw_sprite( buffer, tree2, t * 20, i * 20);
                
               }       
            
        }
       	
    }  
    
    int main()
    {    
        allegro_init();
        install_keyboard();
        set_color_depth(32);
        set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
        
        BITMAP* bmp = create_bitmap(640, 480);
        blit(bmp, screen, 0, 0, 0, 0, 0, 0);
        setupGame();    
        moveChar();
        destroy_bitmap(bmp);
        
        cout <<"Posizione x: "<<x<<endl;
        cout <<"Posizione y: "<<y<<endl;
          
        return 0;
    }
    END_OF_MAIN();

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I think some object oriented code would certainly help a bit here.

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    you can't use cout..you would have to use a console screen that works with allegro..
    in the while loop
    try using
    textprintf_ex but the buffer would be backbuffer not screen
    Code:
    textprintf_ex(backbuffer, font, 10, 10, makecol(255, 100, 200),
    		    -1, "Score: %d", player_score);
    You ended that sentence with a preposition...Bastard!

  8. #8
    Registered User godly 20's Avatar
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    29
    The cout at the bottom of the main work perfectly. They're just there because they print on a separate dos widnow the position of the character in the x and y coordinates.
    Ok, I put the code you gave me at the top of the while loop and it worked. Thanks. Now, how can i make it display the x and y coordinates so that they change when the player moves?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-05-2010, 02:43 PM
  2. RichEditCtrl & unicode formatted text
    By ddonate in forum Windows Programming
    Replies: 0
    Last Post: 03-26-2010, 10:50 AM
  3. Need Help in Compiling errors
    By sick in forum C Programming
    Replies: 2
    Last Post: 01-21-2010, 03:26 AM
  4. Replies: 1
    Last Post: 07-13-2002, 05:45 PM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM

Tags for this Thread