Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn OpenGL

You're reading from   Learn OpenGL Beginner's guide to 3D rendering and game development with OpenGL and C++

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher Packt
ISBN-13 9781789340365
Length 208 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Frahaan Hussain Frahaan Hussain
Author Profile Icon Frahaan Hussain
Frahaan Hussain
Arrow right icon
View More author details
Toc

Setting up OpenGL using SDL on Windows

In this section, we'll discuss how to set up OpenGL on a Windows machine using SDL and GLEW. SDL stands for Simple DirectMedia Layer, and it allows us to create a render window and provides access to input devices through OpenGL. SDL is prominently used to code games and other media applications that run on various operating systems. It's a cross-platform multimedia library written in the C language. GLEW (OpenGL Extension Wrangler), as seen in the previous sections, allows us to easily use extensions and non-core OpenGL functionality.

Downloading the SDL library

We'll begin the setup by downloading the essential libraries. Let's first download the SDL library by following these steps:

  1. Visit http://libsdl.org/index.php, go to Download, and click on the latest version; at the time of writing this book, SDL 2.0 was the latest version.
  2. Once you have clicked on the latest version, you may either want to download the development libraries or the runtime libraries. For this project, it is recommended that you download Development Libraries.
  3. We'll go for the Visual C++ one, which is SDL2-devel-2.0.8-VC.zip. Click on the filename and download it.
  4. Once you've downloaded the file, unzip it and put it in the SDL folder inside the OpenGL folder that we created in previous sections.
  5. After downloading the SDL library, we move on to downloading the GLEW libraries, but as we've already downloaded them in the previous sections, you can just refer to that.
If you want a quick review on downloading GLEW, you can refer to the Downloading the essential libraries section at the start of the chapter.

Setting up OpenGL using SDL and GLEW with absolute linking

Follow these steps to set up the environment in Visual Studio for OpenGL using SDL and GLEW with absolute linking:

  1. Open up Visual Studio and click on Create new Project... in the home page window.
  2. Go to Visual C++ | Windows Desktop | Windows Console Application, name your project SDLOpenGL, and then click OK.
  3. Next, right-click on the project in the Solution Explorer window. Click on Properties.
  4. A Property Pages window will pop up, click on C/C++ | General, and then go to Additional Include Directories. Click on the dropdown, then click on Edit, and you will get a pop up window.
  5. Click on the New button, and then click on the three dots. And now, you want to go to SDL in the OpenGL folder. Select include and then click on the Select Folder button. Repeat the same process for including GLEW files. Once both the files have been included, click on the OK button.
  6. Now, again in the Property Pages window, we'll go to Linker | General, and then go to Additional Library Directories. Click on the dropdown, then click on Edit, and you will get a pop-up window.
  7. In the window, click on the New button, then click on the three dots, and go to the SDL folder. Open the lib folder, go to x86 (which is a 32-bit file, actually), and then click on the Select Folder button.
  8. Repeat the same process for including GLEW libraries. Open the lib folder, then double-click on the Release folder, select Win32, and then click on the Select Folder button. Once you have added both the libraries, click on the OK button.
  9. Next, we'll go to Linker | Input, and then go to Additional Dependencies. Click on the dropdown, then click on Edit, and type opengl32.lib. Then, we'll type glew32s.lib. If you don't want to statically link the library, you can just remove the s. Next, we'll type SDL2.lib and SDL2main.lib, and then click on OK.
  10. Then, click on the Apply button.

Setting up OpenGL using SDL and GLEW with relative linking

In this section, we'll take a look at how to set up OpenGL using SDL and GLEW as the provider for creating a render window with relative linking. Follow these steps:

  1. Click on Create new project... and go to Visual C++. Select Windows Console Application and name it something like SDLApp.
  2. Then, in the New Project window, click on the Browse... button. Go to the OpenGL folder that you created on the desktop and placed the downloaded libraries into External Libraries. Just select the folder and then click OK.
  3. Now, we'll right-click on the project in the Solution Explorer window. Go to Add | New Item, and you will get an Add New Item window. Select C++ File, as this will be our main entry point; let's name it main.cpp and then click on the Add button.
  4. Next, again right-click on the project in the Solution Explorer window. Click on Properties.
  5. A Property Pages window will pop up. Click on C/C++ | General and then go to Additional Include Directories. Click on the dropdown, and then click on Edit.
  6. Then, click on the New button and type $(SolutionDir) in the textbox. This command refers to the folder that contains our .sln file. So if we were to specify a folder in the path, and whenever we do something new in the project, it'd be relatively linked to wherever that file is located.
  1. To link up the include files, add the paths, as shown in the following screenshot:
  1. Next, we'll link up the libraries. So, go to Linker | General, and then go to Additional Library Directories. Click on the dropdown and then click on Edit. Click on New and add the paths, as shown in the following screenshot, then click OK, and click Apply:
  1. Next, we'll link up the .lib files. So, go to the dropdown and click Edit. Now, just type in opengl32.lib. Then, we'll type glew32s.lib. Next, we'll type SDL2.lib and SDL2main.lib, and then click on OK.
  2. Then, click on the Apply button.

Adding a DLL file to the project

As we saw in the previous sections, before completing the setup, we'll have to copy the dynamic link library into our project. Follow these steps to do that:

  1. Go to C:\OpenGL\SDL\lib\x86 and copy the SDL2.dll dynamic link library, as seen in the following screenshot:
SDL2.dll dynamic link library
  1. Now, go to the location in your system where the main.cpp file of your project is located and paste the dynamic link library there. We'll also have to copy and paste the glew32.dll file here from the bin folder of the GLEW folder.
You have been reading a chapter from
Learn OpenGL
Published in: Aug 2018
Publisher: Packt
ISBN-13: 9781789340365
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime