Thread: Trying to start a roguelike

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Well, here are two functions which I think will come in handy -
    Code:
    // Sets the position of the text-cursor, starts at 0, 0 on the 
    // top left
    void GotoXY	( int  x, int  y ) 
    { 
        COORD Cursor_Position; 
    
        Cursor_Position.X = x; 
        Cursor_Position.Y = y; 
    
        SetConsoleCursorPosition( GetStdHandle ( STD_OUTPUT_HANDLE ), Cursor_Position ); 
    } 
    
    // Gets the current position of the text-cursor
    void GetXY	( int &x, int &y )
    {
    	CONSOLE_SCREEN_BUFFER_INFO XYInfo;
    
    	GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &XYInfo );
    
    	x = XYInfo.dwCursorPosition.X;
    	y = XYInfo.dwCursorPosition.Y;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    What do I need to include to use those functions?

    EDIT: Never mind, needed windows.h. I've got an empty room with a moving @ now! Woo, haha.
    Last edited by Dark_Oppressor; 09-17-2006 at 11:17 PM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    Ok, now I am using the x and y coordinates of my @ to determine where GotoXY should center, then drawing the @. Here's my current code regarding this:
    Code:
    void drawscreen()
    {
         clrscr();
         GotoXY(p_x,p_y);
         cout<<"@";
         GotoXY(p_x,p_y);
         while ((ch=get_code())){
               switch (ch){
               case KP_UP:
                    if (p_y>0)
                    p_y=p_y-1;
                    break;
               case KP_DOWN:
                    if (p_y<23)
                    p_y=p_y+1;
                    break;
               case KP_LEFT:
                    if (p_x>0)
                    p_x=p_x-1;
                    break;
               case KP_RIGHT:
                    if (p_x<79)
                    p_x=p_x+1;
                    break;
               case ARROW_UP:
                    if (p_y>0)
                    p_y=p_y-1;
                    break;
               case ARROW_DOWN:
                    if (p_y<23)
                    p_y=p_y+1;
                    break;
               case ARROW_LEFT:
                    if (p_x>0)
                    p_x=p_x-1;
                    break;
               case ARROW_RIGHT:
                    if (p_x<79)
                    p_x=p_x+1;
                    break;
               case KP_UPLEFT:
                    if (p_y>0)
                    p_y=p_y-1;
                    if (p_x>0)
                    p_x=p_x-1;
                    break;
               case KP_UPRIGHT:
                    if (p_y>0)
                    p_y=p_y-1;
                    if (p_x<79)
                    p_x=p_x+1;
                    break;
               case KP_DOWNLEFT:
                    if (p_y<23)
                    p_y=p_y+1;
                    if (p_x>0)
                    p_x=p_x-1;
                    break;
               case KP_DOWNRIGHT:
                    if (p_y<23)
                    p_y=p_y+1;
                    if (p_x<79)
                    p_x=p_x+1;
                    break;
               case KEY_ESC:
                    return;
               }
               clrscr();
               GotoXY(p_x,p_y);
               cout<<"@";
               GotoXY(p_x,p_y);
         }
    }
    As you can see, I have determined the edges of the empty room based on the edges of my console window, but I assume that will be insufficient, because other console windows will be different sizes. Is there a way I can control the size of the console window at the beginning of the program, so all consoles will be resized to fit with my program? Or is there a better way to go about this?
    Also, I suppose I need a way to determine if a tile my @ is moving to is already occupied by a wall. Is there a way to simply determine if a tile has an ascii symbol such as \ / | or - on it?(I'll use these for walls for now I think)
    Last edited by Dark_Oppressor; 09-18-2006 at 12:16 AM.

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Since you're using Win 32 API here's a simple func you can use to resize the console buffer size.
    You should be aware that nothing stops the user from manually resizing the console buffer size after you run this function.
    I searched for a boolean flag or a setter function that allows the programmer to lock the size but I was unable to find anything.
    If anyone else knows of a way, I'd be happy to hear it.

    Code:
    #include <windows.h>
    
    int main()
    {
        setConsoleSize(80, 25);
        system("pause");
    }
    
    int setConsoleSize(int width, int height)
    {
        HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
        COORD scr_dim;
        
        scr_dim.X = width;
        scr_dim.Y = height;
    
        SetConsoleScreenBufferSize(hOut, scr_dim);
    }
    Last edited by MacNilly; 09-18-2006 at 08:44 AM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    Thanks, that'll do for now, anyway. Here is part of the code I'm working on to randomly generate the dungeon:
    Code:
    void Dungeon_Gen()
    {
         int i;
         int j;
         int k;
         int l;
         int mem_i;
         int mem_j;
         int mem_k;
         int mem_l;
         int o;
         int p;
         
         //Fill screen with walls
         for (i=0;i<80;i++){
             for (j=0;j<24;j++){
                 dungeon[i][j]=1;
                 }
                 }
         //i and j are the coordinates of corner 1, k and l are the coorinates of corner 2, the room should be filled in between them
         i=random_range(2,70);
         j=random_range(2,14);
         mem_i=i;
         mem_j=j;
         o=random_range(3,6);
         p=random_range(3,6);
         k=i+o;
         l=j+p;
         mem_k=k;
         mem_l=l;
         for (i;i<k;i++){
             dungeon[i][j]=0;
             }
             i=mem_i;
         for (j;j<l;j++){
             dungeon[i][j]=0;
             }
         j=mem_j;
         mem_k=k;
         mem_l=l;
         for (k;k>=i;k--){
             dungeon[k][l]=0;
             }
         k=mem_k;
         for (l;l>=j;l--){
             dungeon[k][l]=0;
             }
         dungeon[mem_k-1][mem_l-1]=2;
         p_x=mem_k-2;
         p_y=mem_l-2;
    }
    What this does, currently, is fill the screen with walls, hollow out a rectangle(but only edges, it doesn't fill in the room with floor, which is my current problem), and stick in a stair up and the @. I need to find a way to make the for loops that hollow out the edges of the rectangle hollow out a full room instead. Maybe it's because I've been sitting here working on this all day, because it seems like this should be a simple thing to do.
    Last edited by Dark_Oppressor; 09-18-2006 at 04:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  3. C++ gui for windows where to start
    By prixone in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2006, 11:48 PM
  4. GNOME Desktop won't start (Mandriva)
    By psychopath in forum Tech Board
    Replies: 10
    Last Post: 07-19-2006, 01:21 PM
  5. Where do I start?
    By Unregistered in forum Game Programming
    Replies: 2
    Last Post: 04-08-2002, 12:42 AM