Drawing Program in C

This is a discussion on Drawing Program in C within the C Programming forums, part of the General Programming Boards category; Hey guys, I'm having trouble with a code for an assignment. I've got the base for the assignment working as ...

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    3

    Drawing Program in C

    Hey guys, I'm having trouble with a code for an assignment.

    I've got the base for the assignment working as you can see;

    Basically, there is a boundary drawn, as you move with the keyboard arrows (UP, DOWN, LEFT and RIGHT) the program will draw a "#" and the curser is a "@".

    The program does this successfully but when you press "p", the program should enter a mode where you can move the curser but not draw anything, then when you press "p" again, it starts to draw again.

    The problem I'm facing is that, when I press "p" the program only allows me to enter one space without drawing before returning itself to normal drawing mode.

    It also exits when you press "q"

    Sorry about all the commented out lines, I wrote the two loops (One for drawing, one for moving) separately.

    Here is my code:

    Code:
    #include <curses.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define TRUE 1
    #define FALSE 0
    
    
    #define UP 3
    #define DOWN 2
    #define RIGHT 5
    #define LEFT 4
    #define P 6
    
    
    #define LEN 18
    
    
    void dispMap(int x, int y, char map[][LEN])
    {     
        int row,col;
          
          for(row=0;row<LEN;row++)                                                            
        {
            for(col=0;col<LEN;col++)
            {
                 mvaddch(row+1,col+1,map[row][col]);
            }
          }
          mvaddch(y+1,x+1,'@');
    }
    
    
    int main(void)
    {
                                                                         
    
    
        char map[][LEN]={" ---------------- ",
                         "|                |",
                         "|                |",
                         "|                |",
                         "|                |",
                         "|                |",
                         "|                |",
                         "|                |",
                         "|                |",
                         "|                |",
                         "|                |",
                         "|                |",
                         "|                |",
                         "|                |",
                         "|                |",
                         "|                |",
                         "|                |",                     
                         " ---------------- "
                          };
    
    
        
    
    
        
    
    
       
      
        keypad(initscr(),TRUE);                                                                    
        curs_set(0);
      
        
        int xPos=1;                                                                                
          int yPos=1;
        int p;
          char c='\0';                                                                               
    
    
          mvprintw(20,2,"Drawing Field of Play"); 
        mvprintw(21,2,"Press P to Toggle Pen"); 
        mvprintw(22,2,"Press Q to Quit Drawing"); 
        mvprintw(23,2,"Pen Down");
         
        while(c!='q')                                                                             
        {
            mvprintw(23,2,"Pen Down");
            dispMap(xPos,yPos,map);
            c=getch(); 
            
            while (c=='p')
            {
            
            mvprintw(23,2,"Pen Up  ");
            c=getch();
             
            enum{off,on}p;
    
    
            p==on;
            if (p)
                switch(c)
                {
                    case UP:
                    if (map[yPos-1][xPos]== ' ')
                    {
                        yPos--;
                        c=p;
                    }
                    else if (map[yPos-1][xPos]== '#')
                    {
                        yPos--;
                        c=p;
                    }
                      break;
                    case DOWN:
                      if (map[yPos+1][xPos]== ' ')
                    {
                        yPos++;
                        c=p;
                      }
                    else if (map[yPos+1][xPos]== '#')
                    {
                        yPos++;
                        c=p;
                    }
                      break;
                    case LEFT:
                      if (map[yPos][xPos-1]== ' ')
                    {
                           xPos--;
                        c=p;
                      }
                    else if (map[yPos][xPos-1]== '#')
                    {
                           xPos--;
                        c=p;
                      }
                      break;
                    case RIGHT:
                    if (map[yPos][xPos+1]== ' ')
                    {
                        xPos++;
                        c=p;
                      }
                    else if (map[yPos][xPos+1]== '#')
                    {
                        xPos++;
                        c=p;
                      }
                   }
    //            'p'==off;
    //            if ('p')
    //            {
    //                getchar();
    //            }
            }
    
    
            
    
    
            switch(c)
            {
            
                case UP:
                  if (map[yPos-1][xPos]== ' ')
                {    
                    map[yPos][xPos]='#';
                    yPos--;
                }
                else if (map[yPos-1][xPos]== '#')
                {
                    map[yPos][xPos]='#';
                    yPos--;
                }
                break;
                case DOWN:
                if (map[yPos+1][xPos]== ' ')
                {
                    map[yPos][xPos]='#';
                    yPos++;
                }
                else if (map[yPos+1][xPos]== '#')
                {
                    map[yPos][xPos]='#';
                    yPos++;
                }
                  break;
                case LEFT:
                  if (map[yPos][xPos-1]== ' ')
                {
                    map[yPos][xPos]='#';
                    xPos--;
                  }
                else if (map[yPos][xPos-1]== '#')
                {
                    map[yPos][xPos]='#';
                    xPos--;
                  }
                  break;
                case RIGHT:
                  if (map[yPos][xPos+1]== ' ')
                {
                    map[yPos][xPos]='#';
                    xPos++;
                }
                else if (map[yPos][xPos+1]== '#')
                {
                    map[yPos][xPos]='#';
                    xPos++;
                }
            
              }
            
        }
    
    
    //    while (c=='p')
    //    {
    //
    //        dispMap(xPos,yPos,map);
    //        c=getch();
    //         mvprintw(23,2,"Pen Up  ");
    //
    //        enum button{on,off}p;
    //        {
    //            p=on;
    //            if (p)
    //            {
    //                mvprintw(23,2,"Pen Up  ");
    
    
    //                dispMap(xPos,yPos,map);
    //                c=getch();
    //                mvaddch(yPos,xPos,' ');        
    
    
    //                switch(c)
    //                {
    //                    case UP:
    //                    if (map[yPos-1][xPos]== ' ')
    //                    {
    //                        yPos--;
    //                    }
    
    
    //                      break;
    //                    case DOWN:
    //                      if (map[yPos+1][xPos]== ' ')
    //                    {
    //                        yPos++;
    //                      }
    
    
    //                      break;
    //                    case LEFT:
    //                      if (map[yPos][xPos-1]== ' ')
    //                    {
    //                       xPos--;
    //                      }
    //                      break;
    //                    case RIGHT:
    //                    if (map[yPos][xPos+1]== ' ')
    //                    {
    //                    xPos++;
    //                      }
    //                   }
            
    //            p==off;
    //            if(!p)
    //            {
    //                mvprintw(23,2,"Pen Down");
    //            }
    //            getchar();
    //        
    //        }
    
    
    endwin();
    }
    Cheers!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It would be a lot easier if you used a boolean (or int) variable of pendown (1=down, 0=not down), instead of testing for c=='p', because c gets new chars, all the time, as the program is in use.

    so
    Code:
    int pendown = 0;  //start with pen not down
    if(c=='p')
       pendown=!pendown; //switch state, if pen is down already, switch to up, if up, switch to down state
    while(pendown) {
        //do a drawing keystroke 
        c=getchar();
        switch (c) {
           //handle c==p pendown=!pendown break
          //etc.
        }
    
    }
    Last edited by Adak; 06-12-2013 at 06:51 AM.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    3
    thanks for the reply man, before what was happening is, I'd press p, it would move 1 character and reset to Pen Down, with the change the code, it selects Pen Up, but then I can't move the curser at all;
    Here's what I've done:

    Code:
    while(c!='q') 																			
    	{
    		mvprintw(23,2,"Pen Down");
    		dispMap(xPos,yPos,map);
    		c=getch(); 
    		
    	
    		if (c=='p')
    		{
    			pendown!=pendown;
    			while (pendown=1)
    			{
    			mvprintw(23,2,"Pen Up  ");
    			c=getch();
    
    
    				switch(c)
    				{
    					case UP:
    					if (map[yPos-1][xPos]== ' ')
    					{
    						yPos--;
    						//c=p;
    					}
    					else if (map[yPos-1][xPos]== '#')
    					{
    						yPos--;
    						//c=p;
    					}
    			  		break;
    					case DOWN:
    				  	if (map[yPos+1][xPos]== ' ')
    					{
    						yPos++;
    						//c=p;
    				  	}
    					else if (map[yPos+1][xPos]== '#')
    					{
    						yPos++;
    						//c=p;
    					}
    				  	break;
    					case LEFT:
    				  	if (map[yPos][xPos-1]== ' ')
    					{
    				   		xPos--;
    						//c=p;
    				  	}
    					else if (map[yPos][xPos-1]== '#')
    					{
    				   		xPos--;
    						//c=p;
    				  	}
    				  	break;
    					case RIGHT:
    					if (map[yPos][xPos+1]== ' ')
    					{
    						xPos++;
    						//c=p;
    				  	}
    					else if (map[yPos][xPos+1]== '#')
    					{
    						xPos++;
    						//c=p;
    				  	}
    			   	}
    
    
    			}
    		}
    is that what you meant to do?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    while (pendown==1)

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    3
    Oh yeah, of course.

    Code:
    if (c=='p')
    		{
    			pendown!=pendown;
    			while (pendown==1)
    			{
    			mvprintw(23,2,"Pen Up  ");
    			c=getch();
    and so on,

    now it's not even saying Pen Up, it doesn't seem to register the "p" button.

    I'm a bit confused with the pendown!=pendown.

    If c is equal to p
    pendown does not equal pendown?

    I'm pretty amatuer when it comes to this kinda stuff

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    pendown=!pendown

    Is a toggle switch (light switch), that you are changing. If it's true (anything else but zero), you are switching it to 0. (false). If it's false, you are switching it to true.

    Wax on, wax off,

    The logic is a little fussy. I used this:

    Code:
       int c, moveOK,pendown=0;
    
       while(c!='q') {
          moveOK=0;
          c=_getch();
         
          if(c=='p')
             pendown=!pendown;
          
          if (c == 0 || c == 0xE0) { 
             c = _getch(); //key code has two values-get the second one
             
             switch(c) {
             case UP: 
                if(y>TOP+1) {
                   _gotoxy(x,--y);
                   moveOK=1;
                }  
                break;
             case LEFT: 
                if(x>LBORDER+1) {
                   _gotoxy(--x,y); 
                   moveOK=1;
                }
                break;
             case RIGHT: 
                if(x<RBORDER-1) {
                   _gotoxy(++x,y); 
                   moveOK=1;
                }
                break;
             case DOWN: 
                if(y<BOTTOM-1) {
                   _gotoxy(x,++y);
                   moveOK=1;
                }
                break;
             }
             if(pendown && moveOK)
                putchar('*');
          }
       }
    The above uses conio.h functions, (I don't have curses), but you should be able to translate it, no problem.

    All the capitalized words are #define values. TOP is the top row of the screen for the area, RBORDER is the right border of the area, BOTTOM is the last row of the screen for the area, and LBORDER is the left side of the screen area border.
    Last edited by Adak; 06-12-2013 at 10:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help. Drawing program
    By Cherry65 in forum Windows Programming
    Replies: 4
    Last Post: 12-21-2008, 07:34 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  3. Box Drawing Program
    By donniebb in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2003, 01:40 PM
  4. Try out my drawing program
    By Magos in forum C++ Programming
    Replies: 37
    Last Post: 12-16-2002, 03:47 PM
  5. drawing over the open program.
    By Unregistered in forum Windows Programming
    Replies: 6
    Last Post: 01-23-2002, 02:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21