Math 155A Project #0 – Basic Introduction to using Microsoft Visual C++ 2015.

Task for project #0: Do all the steps below.  Fill out the form at https://goo.gl/forms/9KayYX1doBWUp6uT2 as instructed in the main assignment sheet for Project #0 (available at the course web page).

This document gives basic information on how to run Microsoft Visual Studio 2015 for C++ programs, on the PC lab computers.  You will need to know how to do this, even if you program mostly on other computers, in order to turn in your assignments.

PART I.  CREATING A NEW PROJECT.   CREATING AND ADDING FILES.   SIMPLE DEBUGGING.

Creating a new C++ project (also called a “solution”) from scratch.  Visual C++ groups source files into a “project”, and groups projects into “solutions”.  We do not need much of this infrastructure for our course applications.

Step 0: Start Visual C++.

-          Run the program

-          From Start Menu -> All Programs, run Visual Studio 2015.  When prompted, select the Visual C++ settings. (and wait for Visual Studio to start up: it may take a couple minutes).  When it asks for Development Settings, pick Visual C++ from the dropdown menu (instead of General).

Step 1: Create the project/solution:

-          From menu bar, select File…, then New, then Project…, then select Visual C++, and then  Empty Project.

-          Set the project name, for instance HelloWorld.

-          Set a location for the project/solution folder.  For instance “My155AProjects”, will then create or use the folder “My155AProjects/HelloWorld”.   To make this happen, it is usually a good idea to uncheck the “Create directory for solution” box. 

-          Important: All your files should be placed somewhere your personal folder under \\acsnfs4.ucsd.edu\CifsHomes\099. Files placed are the desktop are subject to deletion overnight without warning.  (Yes, this really happens once in a while!)

Step 2.  Add new source files(s).  Usually these are .c or .cpp or .h files.  This is only for brand new files.  If you instead have an existing file that you want to include in the project, see Step 6 below.

-          From the menu bar: File, then New, then File….  

-          Choose Visual C++ templates on the lefthand side, then C++ File (.cpp) or other file type as desired.  Select Open, and you will get a window showing a blank file, temporarily named Source1.cpp

-          Type your code into the source window. For instance

o    #include <stdio.h>

o    int main ()

o    {

o           printf( "Hello World.\n" );

o           getc(stdin);

o    }

-          Save the file (File  then Save Source1.cpp As...) preferably into the folder( named “HelloWorld” for instance) where your project files are placed.  While doing this, rename the file to be something more useful than “Source1.cpp”, for instance, “HelloWorld.cpp”.

Alternate method for first item: Use Menu bar Project…, then Add New Item….

Step 3. Make the file part of the project/solution.  (A slightly silly step, but needed because of the infrastructure of Visual C++.)

-          Have the cursor focus in the file to be added to the project, for instance HelloWorld.cpp.

-          From menu bar: File, then Move HelloWorld.cpp into ..., then HelloWorld (or whatever your project is named).

Alternate method: After file has been saved (say, in Step 2), use Menu bar Project, then Add Existing Item….  This is also discussed in Step 6 below.

Step 4. Compile the program.

-          From Menu bar Build, then Build Solution.  Or press the F7 key. (Or maybe Cntl-Alt-B depending on your setup.)

-          Check the compile output window at the bottom of the screen. 

-          Fix any compile errors and repeat.

-          Once the output window ends with Build: 1 succeeded, … it is compiled without errors. It is recommended, but usually not required, to fix all warnings.

-          Extra tip: Sometime you need to use Rebuild Solution instead of Build Solution if the compiler gets confused about file creation times.

Step 5.  Run the program.

-          From the menu bar: Debug, then Start Debugging.  Or press the F5 key.

-          See what happens.  You should see a console window with “Hello World.” Press ENTER/RETURN to end the HelloWorld program shown above.

Question A: What happens if you omit the getc(stdin); command?  Why? [Hint: This commands reads a character from the console window.]

Step 6. Add existing files to the project.  If you have an existing file you want to start with, instead of typing your code from scratch.

-          Consider whether you want to make a new copy and place it in your project directory.  This is almost always a good idea!!!

-          From the menu bar: Project, then Add Existing Item…, then select the files you wish to include in the project (usually only .c, .cpp, and .h files).  Please note this does not make a copy of the file.  It just includes it from wherever it is.  (See the previous bullet item.)

-          You should now be able to see the files in the Solution Explorer window on the left (or right) side of your Visual Studio window (under Header Files or Source Files).

-          An alternate way to add files: Open the file for editing, then under the menu bar File menu, use the Move … into option.

Step 7.  Debugging example.

-          Update the HelloWorld program to include a simple loop:

o    #include <stdio.h>

o    int main ()

o    {

o           printf( "Hello World.\n" );

o           int i = 0;

o           for ( int j=0; j<7; j++ ) {

o                  i += j*j;

o           }

o           printf( "i = %d.\n", i );

o           getc(stdin);

o    }

-          Compile the program; fix any problems.  If you get compile errors, click on the error message to go to the line with the error.

-          Now in the window for the source code, find the grey column to the left side of the window.  Click there in that column on the line “int i = 0;”, to put a red circle inside that column.  This is a breakpoint.  Click again if you want to remove the breakpoint. A breakpoint will cause the program to stop there for you debug.

-          Insert breakpoints on the line “int i = 0;” and on the line “getc(stdin);”.

-          Run the program again.  It will start running the program, and stop at the first breakpoint, showing with a yellow arrow what line the code is at.

-          Note that below the source code, there is a panel entitled “Autos” showing the value of the variable i.  

-          Use the F10 command (or menu bar, Debug, then Step Over) to single step the program execution to the next line.  Note the value of the variable  i  changes in the Autos panel, and the value of  j  is added. 

-          Continue single stepping: Note how the values of i and j change, and how the arrow moves to the next line each time.

-          It is also possible the change the values of i and j in the Autos panel.

-          Press F5 again: the program continues running to the next breakpoint.

-          You may use Shift-F5 to terminate the program.

Question B: Does debugging, and single-stepping, and watching variables’ values change make sense to you?

Other useful things to know about debugging.  When you have function calls, use F11 and Shift+F11 to step into a function call (instead of the next line in the current routine), and out of a function call.

It is strongly recommended that you get good at the basic features of debugging: it can greatly simplify programming!  This kind of interactive debugging can be an enormous help in finding and fixing errors in your code. It is recommended that you make a habit of single-stepping like this through all new pieces of code. This can be invaluable in finding errors in code.

There is an enormous range of options and features in debugging.  You may wish to explore these on your own as needed; or ask the TA Srivastava or Professor Buss for pointers.  The commands above take care of most basic needs however, and are a great help in debugging your code.

PART II – STARTING WITH AN EXISTING Visual C++ PROJECT. 

Often you will want to start a new Visual C++ project/solution using a previous one as a base.   IMPORTANT: To begin, make a completely new copy of all your files, in order to avoid destroying your old files.  These files should be put in a separate directory.

The basic method is to then just open the .sln file, that is, the solution file.      

To try this out, use the steps below:

Step 1. Download SimpleDrawModern.zip.The full URL for this file is http://www.math.ucsd.edu/~sbuss/CourseWeb/Math155A_2018Winter/Project0/SimpleDrawModern.zip.  Extract the files from the zip file, and place them in a folder (say under your “MyProjects” folder, where your HelloWorld project is). It is important to extract the files, and not try to use them while they are still inside the zip file.

Step 2. Open the file.  Do one of the following methods:

-          Double click on SimpleDrawModern.sln. (A “Microsoft Visual Studio Solution” file.)

-          Alternately, in Visual C++, choose from the menu bar choose File, then Open …, then Project/Solution, and browse to find the .sln file.

Step 3.   Run the program.  (See the “workaround” below if the program will not compile due to missing headers.)

-          Build and then run the program.  (Use Cntl-Shift-B and F5, or use the dropdown menus for Build and Debug).  This open two windows: a graphics window and a console window. The graphics window holds all the output of OpenGL commands. The console window holds text input and output (stdin, stdout, and stderr).

-          For the SimpleDrawModern program, you should initially see three overlapping shaded (colored) triangles. 

-          Hit the space bar to cycle through five different images: with dots, then line segments, then a “line strip”, then a “line loop”, and finally the three triangles again.  Also note what happens when you resize the window, say by making it tall and skinny, or short and wide.

You may not be able to compile (or link) your program due ACS not yet installed the GLFW and GLEW header files and libraries.  If so, here is a work-around:

-          Download the GLFW and GLEW files from the dropbox link https://www.dropbox.com/sh/tn5hzmn6hw2bab4/AADNwXoNzO-rZ7yohp71LZOca?dl=0

-          At that location, there are three static libraries (.lib) files, and two folders (GL and GLFW).

-          Place all of these in the same folder as your Solution file (.sln file).

-          In each of the two .cpp files, SimpleDrawModern.cpp and ShaderMgrSDM.cpp, there are two #include lines: #include <GL/glew.h>  and #include <GLFW/glfw3.h>. Change these to be #include "GL/glew.h" and #include "GLFW/glfw3.h" instead. (Changing angle brackets to double quotes.) 

-          The program should be able to compile and run now. ACMS should install the files soon, and after that this workaround will no longer needed.

If you not at the lab computers, you may not be able to compile and run SimpleDrawModern.cpp due to not having the needed OpenGL and GLFW and GLEW libraries installed.  If so, refer to the course web page for more information on how to do install these items.

Step 5.  Play with the SimpleDrawModern program a bit.

-          Examine the SimpleDrawModern.cpp and ShaderMgrSDM.cpp source files.  (To open the source code files, use the Solution Explorer window, which will be on the right hand or left hand of the Visual C++ windows.)

-          Note from the comments at the beginning that it takes space key as input.  This space key is to be typed into the graphics window, not the console window. 

-          You may also use the Escape key or “X” or “x” to exit.  All these are typed into the graphics window.

-          Try line loop’s color from yellow to cyan.  For this, find the appropriate glVertexAttrib3f command on line 199 in SimpleDrawModern.cpp with color arguments (1.0, 1.0, 0.2) and replace them with the RGB values (0.0, 1.0, 1.0) for Cyan.  Recompile the program, and see if it worked!

-          Try the same again, but now changing the green color to BROWN.  (Brown can be a difficult color to match.) You mish to google for “RGB code for brown”.  You will most find suggested R/G/B values ranging between 0 and 255. Scale them into the range 0.0 to 1.0 by dividing by 255 to use them in the SimpleDrawModern program. Are you able to obtain a good brown color?

-          Take a look at how the code handles key strokes with the routine key_callback in SimpleDrawModern.cpp.  This is a kind of interrupt-driven, or callback-based interface; it may be rather different from the way you are used to programs receiving input.  This is setup by the function setup_callbacks using the OpenGL/GLEW routine glfwSetKeyCallback.

Question C: Were you able to successfully change the green lines to cyan? And to brown?  Did you find a good brown color?

Question D: Does the code for receiving keystrokes as input make sense to you? 

PART III:  REVIEW THE ABOVE MATERIAL. 

Final Question:  Do you have any questions about how or why things work in the above steps?  Did you any particular difficulties?  Were there any steps you could not make work successfully?