Post

Computer Graphics 2D Rasterization Setup

This guide will provide step-by-step instructions on how to create a Visual Studio C++ project for the Computer Graphics course. This project will use SDL to provide 2D rasterization. 2D rasterization is the process of converting geometric shapes, such as lines, circles, and polygons, into pixels on a screen. It involves taking mathematical descriptions of these shapes and determining which pixels should be illuminated to represent them accurately.

Create C++ Solution/Project

  • Open Visual Studio and select Create a new project
Project
  • Create an Empty Project C++
Project
  • Configure your new project
    • Project Name: 2D
    • Solution Name: GAT350
    • Uncheck Place solution and project in the same directory
    • Press Create
Project
  • The solution is now created
Project
  • Add Main.cpp
    • Right-click on the project in Solution Explorer, select Add>New Item
    • Create Main.cpp and set the folder to a “Source” folder (the folder will be created if it doesn’t exist)
    • Main.cpp will now be shown in the Solution Explorer
Project
  • Add the main() function to Main.cpp
  • Include the iostream and display the string "Hello, World!" in main()
  • Run the Solution and ensure that “Hello, World!” is displayed
Project

Download SDL

  • Go to the SDL website https://www.libsdl.org/
    • Go to the Download SDL Releases
    • Download the latest release SDL2-devel-####-VC.zip
Version
  • Create a folder in the Solution directory called “ThirdParty”
    • ThirdParty will contain libraries from external developers to be used in the project
ThirdParty Folder
  • Move the downloaded .zip file into the ThirdParty folder and extract it
    • Rename the extracted folder to “SDL2” to make is easier to reference the folder
    • Once extracted, the .zip file can be deleted
Unzip

Add SDL to the Solution Project

Add SDL Include

  • Open the Project Properties that SDL will be used in
    • Right-click the Project and select Properties
Project Properties

In the Project Properties, make sure that the Configuration is set to All Configurations and Platform is set to All Platforms.

Configurations
  • Add the directory of the SDL include folder to the Additional Include Directories.
    • Additional Include Directories is located in C/C++>General.
    • Add $(SolutionDir)ThirdParty\SDL2\include
1
$(SolutionDir)ThirdParty\SDL2\include
Include
  • Once the directory is added to the list of included directories, the SDL header can be add to the files.
    • Add the following to a file #include <SDL.h>
    • Update the main() function, SDL requires the arguments
1
int main(int argc, char* argv[])

Add SDL Library

  • Open the Project Properties that SDL will be used in
    • Right-click the Project and select Properties
Project Properties
  • Add the directory of the SDL library folder in Project Properties.
    • Additional Library Directories is located in Librarian>General or Linker>Input
    • Add $(SolutionDir)ThirdParty\SDL2\lib\$(PlatformTarget)
1
$(SolutionDir)ThirdParty\SDL2\lib\$(PlatformTarget)
Library
  • Add the SDL .lib files that the project needs to function.
    • Additional Dependencies is located in Librarian>General or Linker>General
    • Add sdl2.lib and sdl2main.lib
1
2
sdl2.lib
sdl2main.lib
Library

If the SDL library was properly added, building the project (CTRL+B) will result in no errors.

  • Create a folder in the Solution directory called “Build”.
    • The Build folder will contain the SDL dll (dynamic link library) files.
Build
  • Copy the sdl2.dll file from the ThirdParty\sdl\lib\x64 directory to the Build folder.
    • The project is a x64 project (64-bit application).
DLL DLL

Set the Solution Working Directory

  • Set the Working Directory that the project will be run from in Project Properties.
    • Working Directory is located in Debugging.
    • Add $(SolutionDir)Build
1
$(SolutionDir)Build
Working

Create SDL Window

  • In main() add the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <SDL.h>
#include <iostream>

int main(int argc, char* argv[])
{
    // initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        std::cerr << "Error initializing SDL: " << SDL_GetError() << std::endl;
        return 1;
    }

    // create window
    // returns pointer to window if successful or nullptr if failed
    SDL_Window* window = SDL_CreateWindow("Game Engine",
        SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
        800, 600,
        SDL_WINDOW_SHOWN);
    if (window == nullptr)
    {
        std::cerr << "Error creating SDL window: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return 1;
    }

    // create renderer
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);

    while (true)
    {
        // clear screen
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
        SDL_RenderClear(renderer);

        // show screen
        SDL_RenderPresent(renderer);
    }

    return 0;
}
This post is licensed under CC BY 4.0 by the author.