Starting a project
This is how to start a project from scratch. Here are the basic parts and why they are needed.
- OpenGL
This provides access to the graphics hardware for rendering. It does nothing else, so it needs a lot of supporting libraries. OpenGL support is provided by the graphics driver, so the version supported will vary a lot. - SFML
This provides access to events, I/O, and windowing interfaces. This library is designed for media-rich interactive programs, so it provides a lot more than we really need. - GL3W
Since the OpenGL version varies a lot, there are helper tools that query the driver and expose support. There are many of these tools: GLEW, GLee, GLFW. - GLM
GLUT was an OpenGL helper library that was very popular many years ago. It provided easy ways to create view and projection matrices. GLUT does not work with modern OpenGL and GLM was developed to provide similar functionality.
Starting a project
- Download SFML, GLM, and GL3W.
- GLM is designed as a header-only library and needs to be placed at a linkable library path.
- Put it in your dev path and include it at compile time, or:
- Modify it to be locally included by replacing all
<glm/...>
with
"glm/..."
- Run the GL3W python script and get the resulting code/header.
- Place them on your dev path and build the .c file into a library, or:
- Add the .c file to your project and modify all includes to be local by replacing all gl3w includes with local ones. For example:
- gl3w.c :
<GL/gl3w.h>
to"gl3w.h"
- gl3w.h :
<GL/glcorearb.h>
to"glcorearb.h"
- glcorearb.h : all KHR references to local
- and so on...
- gl3w.c :
- Or, get my previously created versions of these libraries:
Windows
-
Windows has no file-system structure for dev libraries, so put SFML somewhere reasonable.
-
Create a new project following the the tutorial on the SFML website. Create an empty project. Follow the setup instructions, but not the code. We will use different base code.
https://www.sfml-dev.org/tutorials/2.5/start-vc.php -
If your program has precompiled headers, you need to remove them. Go to Project > 'program' Properties > C/C++ > Precompiled Headers and disable it there. You can then delete all the stdafx and targetver files. Then, fix the dependencies.
-
The tutorial tells you to add several SFML dependencies. These ones are the ones you need:
- sfml-window.lib
- sfml-system.lib
- opengl32.lib
- winmm.lib
- gdi32.lib
-
Copy the SFML dlls to the project working directory. This is probably the build output directory of your application:
PROJECT/Debug/
.
OS X (Xcode)
- Create a new project following the the tutorial on the SFML website. Follow the setup instructions, but not the code. We will use different base code.
https://www.sfml-dev.org/tutorials/2.5/start-osx.php - Add Cocoa framework as dependancy
Linux
- Create a new project following the the tutorial on the SFML website. Follow the setup instructions, but not the code. We will use different base code.
https://www.sfml-dev.org/tutorials/2.5/start-linux.php - You will only need to add
sfml-window
as a dependency.
Rubric
Running green/blue project | 0 : Incomplete | 10 : Complete |
Here is the base code we will use:
#include <SFML/Window.hpp>
//TODO: Add include for GL3W here
int main(int argc, char const** argv)
{
// Get OpenGL context
sf::VideoMode mode(512, 512, 32);
sf::ContextSettings settings(32, 0, 0, 3, 3, sf::ContextSettings::Core);
// Create the main window
sf::Window window(mode, "Project", sf::Style::Default, settings);
// Init GL3W
if (gl3wInit()) {
fprintf(stderr, "failed to initialize OpenGL\n");
}
// If you want to do graphics setup, this is a good place to do it
// Resolving resource paths and other system settings can be hard!
bool toggle = true;
// Start the game loop
while (window.isOpen())
{
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
if(toggle)
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
toggle = !toggle;
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
window.close();
}
}
// Update the window
window.display();
}
return EXIT_SUCCESS;
}