Math 155A (Winter 2019) Project #0 – Basic Introduction to using Microsoft Visual C++ 2017.

Task for project #0: Do all the steps below.  Fill out the google form at https://goo.gl/forms/hkaTD3UEnQbHhrUw2 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 2017 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 2017.  (There is no need to “sign in” when prompted.) 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 (but General will work too).

POTENTIAL PROBLEM: If you get an error message when starting Visual Studio, it probably means it is incorrectly installed. If so, please make a note of the machine’s ID number, and email ACMS and the Professor Buss to let them know so it can be restalled.  Then try using a different computer in the lab.

Step 1: Create the project/solution:

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

-        Set the project name, for instance HelloWorld.

-        Set a location for the project/solution folder.  For instance “My155AProjects”, will cause Visual Studio 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 left-hand side of the window that opens. Then choose 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. (A similar method works to open a .h header file.)

-        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 Cntl-Shift-B the F7 key. (Or maybe the F7 key depending on your setup.)

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

-        Fix any compile errors and repeat.  There are two windows listing error messages, an “Error List” window and an “Output” window. You may find it easier to use the “Output” window.

-        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? [This is a slightly tricky question. 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 can add multiple files at once, by selecting them together.

-        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). Click the file names to open a source file.

-        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.  Just type in the desired value!

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

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

Reopening a Visual Studio project: The main project file is called a “Visual Studio Solution” file, a.sln file.  You can use this is reopen the project.  Alternately, use the File menu in Visual Studio to open the file.

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.  In fact, this is one of the main reasons to use a good IDE like Visual Studio instead of a command line environment.

There is an enormous range of options and features in debugging.  You may wish to explore these on your own as needed; or ask one of the TAs 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 – TRYING OUT AN OPENGL PROGRAM --- SimpleDrawModern

Now try making a new Visual Studio solution (project) with a sample OpenGL program:

Step 1. Download SimpleDrawModern.zip from http://www.math.ucsd.edu/~sbuss/MathCG2/OpenGLsoft/SimpleDrawModern/. Extract the files from the zip file, and place them in a new 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. Create a new Visual Studio C++ project, following the same instructions as before.  Add all three sources files from that you just downloaded.  Do not add the executable file to the project however. 

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 or maybe 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).  In this course, we never use stdin.

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

-        Hit the space bar to cycle through five different images: with line segments, then a “line strip”, then a “line loop”, then dots, 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.

-        If everything worked OK, go onto to Step 4.

POTENTIAL PROBLEMS: If there are error messages about missing GLFW and GLEW files, your systems is misconfigured.  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 4.  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. Double click on file names to open the file.

-        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 changing the line loop’s color from yellow to cyan.  For this, find the appropriate glVertexAttrib3f command on line 208 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 color to BROWN.  (Brown can be a difficult color to match.) You may wish to google for “RGB color” or “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? (The best brown color might not be named “brown”.)

-        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? What was it?

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?