May The Force Be With You

• May. 22, 2006 - C++

Posted in Information

I've been programing in C++ lately. I am going to be a game programmer, and there's no use waiting.

 

Anyway, the program I just programed can do (currently) three types of geometry: the circumference of a circle, the area of a square, and the area of a triangle. It also has color, but if you have a C++ compiler and paste the below code into it, it won't work.

 

This is why: the #include "msoft.h" is a header that was created, meaning it's not stored in the compiler's "list" of headers it can understand. Here's how to run the program with color:

 

(This is for Microsoft Visual C++ 6.0)

1. Create a new workspace by clicking "File" on the menu.

2. Click new.

3. Select Win32 Console Application and type in a progect name. Click new.

4. Choose "An empty project" and click finish. Click OK. 

5. Go to "File" and click on new again.

6. Choose C/C++ Header File. Type in a file name and click OK.

7. Copy (Ctrl + C) the "msoftcon.h" code from below and paste (Ctrl + V) in in the text area.

8. Go to "File" and click on new AGAIN.

9. Choose C++ Source File. Type the file name and click OK.

10. Copy the "msoftcon.cpp" code from below and paste it in the text area.

11. Repeat steps 8 and 9.

12. Copy the "program.cpp" code from below and paste it in the text area.

 

OK, now click the red exclamation mark under the "Build" menu to execute the program.

Click yes.

 

 

msofcon.h:

 

//msoftcon.h
//declarations for Lafore's console graphics functions
//uses Window's console functions

#ifndef _INC_WCONSOLE    //don't let this file be included
#define _INC_WCONSOLE    //twice in the same source file

#include      //for Windows console functions
#include        //for kbhit(), getche()
#include         //for sin, cos

enum fstyle { SOLID_FILL, X_FILL,      O_FILL,
              LIGHT_FILL, MEDIUM_FILL, DARK_FILL };

enum color {
   cBLACK=0,     cDARK_BLUE=1,    cDARK_GREEN=2, cDARK_CYAN=3,
   cDARK_RED=4,  cDARK_MAGENTA=5, cBROWN=6,      cLIGHT_GRAY=7,
   cDARK_GRAY=8, cBLUE=9,         cGREEN=10,     cCYAN=11,
   cRED=12,      cMAGENTA=13,     cYELLOW=14,    cWHITE=15 };
//--------------------------------------------------------------
void init_graphics();
void set_color(color fg, color bg = cBLACK);
void set_cursor_pos(int x, int y);
void clear_screen();
void wait(int milliseconds);
void clear_line();
void draw_rectangle(int left, int top, int right, int bottom);                   
void draw_circle(int x, int y, int rad);
void draw_line(int x1, int y1, int x2, int y2);
void draw_pyramid(int x1, int y1, int height);
void set_fill_style(fstyle);
#endif /* _INC_WCONSOLE */

 

 

mosoftcon.cpp:

 

//msoftcon.cpp
//provides routines to access Windows console functions

//compiler needs to be able to find this file
//in MCV++, /Tools/Options/Directories/Include/type path name

#include "msoftcon.h"
HANDLE hConsole;         //console handle
char fill_char;          //character used for fill
//--------------------------------------------------------------
void init_graphics()
   {
   COORD console_size = {80, 25};
   //open i/o channel to console screen
   hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
                   FILE_SHARE_READ | FILE_SHARE_WRITE,
                   0L, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0L);
   //set to 80x25 screen size
   SetConsoleScreenBufferSize(hConsole, console_size);
   //set text to white on black
   SetConsoleTextAttribute( hConsole, (WORD)((0 << 4) | 15) );

   fill_char = '\xDB';  //default fill is solid block
   clear_screen();
   }
//--------------------------------------------------------------
void set_color(color foreground, color background)
   {
   SetConsoleTextAttribute( hConsole,
                        (WORD)((background << 4) | foreground) );
   }  //end setcolor()

/* 0  Black          8  Dark gray
   1  Dark blue      9  Blue
   2  Dark green     10 Green
   3  Dark cyan      11 Cyan
   4  Dark red       12 Red
   5  Dark magenta   13 Magenta
   6  Brown          14 Yellow
   7  Light gray     15 White
*/
//--------------------------------------------------------------
void set_cursor_pos(int x, int y)
   {
   COORD cursor_pos;              //origin in upper left corner
   cursor_pos.X = x - 1;          //Windows starts at (0, 0)
   cursor_pos.Y = y - 1;          //we start at (1, 1)
   SetConsoleCursorPosition(hConsole, cursor_pos);
   }
//--------------------------------------------------------------
void clear_screen()
   {
   set_cursor_pos(1, 25);
   for(int j=0; j<25; j++)
      putch('\n');
   set_cursor_pos(1, 1);
   }
//--------------------------------------------------------------
void wait(int milliseconds)
   {
   Sleep(milliseconds);
   }
//--------------------------------------------------------------
void clear_line()                    //clear to end of line
   {                                 //80 spaces
   //.....1234567890123456789012345678901234567890
   //.....0........1.........2.........3.........4
   cputs("                                        ");
   cputs("                                        ");
   }
//--------------------------------------------------------------
void draw_rectangle(int left, int top, int right, int bottom)
   {
   char temp[80];
   int width = right - left + 1;

   for(int j=0; j      temp[j] = fill_char;  
   temp[j] = 0;                    //null

   for(int y=top; y<=bottom; y++)  //stack of strings
      {
      set_cursor_pos(left, y);
      cputs(temp);
      }
   }
//--------------------------------------------------------------
void draw_circle(int xC, int yC, int radius)
   {
   double theta, increment, xF, pi=3.14159;
   int x, xN, yN;

   increment = 0.8 / static_cast(radius);
   for(theta=0; theta<=pi/2; theta+=increment)  //quarter circle
      {
      xF = radius * cos(theta); 
      xN = static_cast(xF * 2 / 1); //pixels not square
      yN = static_cast(radius * sin(theta) + 0.5);
      x = xC-xN;
      while(x <= xC+xN)          //fill two horizontal lines
         {                       //one for each half circle
         set_cursor_pos(x,   yC-yN); putch(fill_char);  //top
         set_cursor_pos(x++, yC+yN); putch(fill_char);  //bottom
         }
      }  //end for
   }
//--------------------------------------------------------------
void draw_line(int x1, int y1, int x2, int y2)
   {

   int w, z, t, w1, w2, z1, z2;
   double xDelta=x1-x2, yDelta=y1-y2, slope;
   bool isMoreHoriz;

   if( fabs(xDelta) > fabs(yDelta) ) //more horizontal
      {
      isMoreHoriz = true;
      slope = yDelta / xDelta;
      w1=x1; z1=y1; w2=x2, z2=y2;    //w=x, z=y
      }
   else                              //more vertical
      {
      isMoreHoriz = false;
      slope = xDelta / yDelta;
      w1=y1; z1=x1; w2=y2, z2=x2;    //w=y, z=x
      }

   if(w1 > w2)                       //if backwards w
      {
      t=w1; w1=w2; w2=t;             //   swap (w1,z1)
      t=z1; z1=z2; z2=t;             //   with (w2,z2)
      }
   for(w=w1; w<=w2; w++)           
      {
      z = static_cast(z1 + slope * (w-w1));
      if( !(w==80 && z==25) )        //avoid scroll at 80,25
         {
         if(isMoreHoriz)
            set_cursor_pos(w, z);
         else
            set_cursor_pos(z, w);
         putch(fill_char);
         }
      }
   }
//--------------------------------------------------------------
void draw_pyramid(int x1, int y1, int height)
   {
   int x, y;
   for(y=y1; y      {
      int incr = y - y1;
      for(x=x1-incr; x<=x1+incr; x++)
         {
         set_cursor_pos(x, y);
         putch(fill_char);
         }
      }
   }
//--------------------------------------------------------------
void set_fill_style(fstyle fs)
   {
   switch(fs)
      {
      case SOLID_FILL:  fill_char = '\xDB'; break;
      case DARK_FILL:   fill_char = '\xB0'; break;
      case MEDIUM_FILL: fill_char = '\xB1'; break;
      case LIGHT_FILL:  fill_char = '\xB2'; break;
      case X_FILL:      fill_char = 'X';    break;
      case O_FILL:      fill_char = 'O';    break;
      }
   }
//--------------------------------------------------------------

 

 

 

program.cpp:

 

#include
#include "msoftcon.h"

int main()
{

start:

 int where;
 
 init_graphics();
 
 cout <<"\n\n1---Circumfrence of a circle";
 cout <<"\n2---Area of a square";
 cout <<"\n3---Area of a triangle";
 cout <<"\n\nWhat type of goemetric math do you want to do? ";
 set_color(cGREEN);
 cin >> where;

 switch(where)
 {
 case 1:
  goto circum;
  break;
 case 2:
  goto areasquare;
  break;
 case 3:
  goto areatrin;
  break;
 }

circum:

 float radius, circum;
 int yn;

 init_graphics();
 
 cout <<"\n\n\nPlease enter the radius of the circle: ";
 set_color(cYELLOW);
 cin >> radius;

 init_graphics();

 circum = (2 * 3.1416 * radius);
 cout <<"\n\nThe circumfrence of the circle is " << circum << ".\n\n";
 set_color(cRED);

 init_graphics();
 
 cout <<"Do you want to restart the program? \n(1 for yes or 0 for no) ";
 set_color(cBLUE);
 cin >> yn;
 switch (yn)
 {
 case 1:
  goto start;
  break;
 }
 
areasquare:
 
 float leangth, width, area;

 init_graphics();
 
 cout <<"\n\n\nPlease enter the length and the the width of the rectangle: ";
 set_color(cRED);
 cin >> leangth >> width;

 init_graphics();
 
 area = (leangth * width);
 cout <<"\nThe area of the square is "<< area <<".\n\n";
 set_color(cWHITE);

 init_graphics();
 
 cout <<"Do you want to restart the program? \n(1 for yes or 0 for no) ";
 set_color(cYELLOW);
 cin >> yn;
 switch (yn)
 {
 case 1:
  goto start;
  break;
 }


areatrin:

 float base, height, triarea;

 init_graphics();
 
 cout <<"\nPlease enter the length of the base and the height: ";
 set_color(cGREEN);
 cin >> base >> height;

 init_graphics();
 
 triarea = (base * height / 2);
 set_color(cBLUE);
 cout <<"The area of the triangle is "<< area <<".\n\n";
 
 init_graphics();
 
 cout <<"Do you want to restart the program? \n(1 for yes or 0 for no) ";
 set_color(cRED);
 cin >> yn;
 switch (yn)
 {
 case 1:
  goto start;
  break;
 }
 
 return 0;
}


 

 

 

 

It should work! 

 

 

Post A Comment! :: Send to a Friend!

• May. 22, 2006 - Whew!

Posted by AmoScribo
Between reading this programming and the music I think I just lost my mind.
Permanent Link

• May. 23, 2006 - Hi

Posted by Al
I hope you're enjoying C++
Permanent Link

About Me

I like to communicate on the Web with other teenagers. My favorite hobbies include computers, reading, drawing, the outdoors, and making money.

Links

Home
View my profile
Archives
Friends
My Blog's RSS
War of the Ring
My Family's Farm Blog
My Photo Album
My Bug Blog
Free ECommerce Website
Lucas Arts
Star Wars

Family

Dad
Mom
2nd In Line
3nd In Line
4th In Line

Friends

JoeM
LeviSuarez
GalacticBlogger
DarthYxpu
JediBlogger
randomchic93
luvinmusic
Al
Hawk
AmoScribo
Inklings
Will7
Mom112041
TheEntomologist
Kekoa
Jocelyndixon
Ringbearer

antelopehead
Lukelee
riverthomas
Isaiahstorm
Guitarist2Him
Trapdude
ScottCosta
Chris
EyesonAslan

DeKampf

Categories

Game Review
Video Reviews
Music Reviews
Chatter
The Outdoors
Debate
Fun Stuff
Books I'm Reading
Information

Favorite Music


•12 Stones
•P.O.D.
•Switchfoot
•Relient K
•KJ-52
•T-Bone
•Thousand Foot Crutch
•Toby Mac

Sunset Photo

This site posts a
sunset photo everyday
Click here

Get your own calendar

Favorite Books


•Lord of the Rings
•Redwall Series
•Jack London's Books
•Narnia
•And just about everything else!

My Favorite Zane Greys & How Many Times I've Read Them


•Shadow on the Trail(6)
•The Code of the West(3 or 4)
•The UP Trail (3 or 4)
•The Drift Fence(3)
•The Hash Knife Gang(3)
•Twin Sombreros (2)
•Lost Pueblo(3)
•Raiders of the Spanish Peaks(1)
•To the Last Man(2)
•The Man of the Forest(2)
The Arizona Gang (1)
The Dude Ranger (1)
I have read other Zane Greys, but these are my favorites.
What is the Highest Computer Software Rating that Someone Should Play?
  
Free polls from Pollhost.com

Visitors since 9/22/06


View My Stats
Online Reference
Dictionary, Encyclopedia & more
Word:
Look in: Dictionary & thesaurus
Computing Dictionary
Medical Dictionary
Legal Dictionary
Financial Dictionary
Acronyms
Idioms
Wikipedia Encyclopedia
Columbia Encyclopedia
by:

I am nerdier than 34% of all people. Are you nerdier? Click here to find out!


Word of the Day

Article of the Day

This Day in History

Today's Birthday

In the News

Quotation of the Day

Match Up
Match each word in the left column with its synonym on the right. When finished, click Answer to see the results. Good luck!

 

Hangman
Entry 39 of 71
Last Page | Next Page