Search code examples
c++visual-studiodownloadsdlsdl-2

How to add SDL2_gfxPrimitives to my visual studio 2019?


I am trying to create a graphics project in visual studio 2019 using SDL2. I managed to connect it with my visual studio using this tutorial: https://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/msvc2019/index.php

I want circles, lines, rectangles etc. as my output, so need SDL_gfx for that. The problem is I cannot connect it with my project. I downloaded it from here: https://www.ferzkopp.net/wordpress/2016/01/02/sdl_gfx-sdl2_gfx/

Could someone help me out with connecting it with my visual studio?

Here is a simple example I found to start with:

#include <SDL.h>
#include <SDL2_gfxPrimitives.h>
#include <math.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
    SDL_Log("SDl cant start %s", SDL_GetError());
    exit(1);
}
SDL_Window *window = SDL_CreateWindow("SDL example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 440, 360, 0);
if (window == NULL) {
    SDL_Log("Window cant be generated %s", SDL_GetError());
    exit(1);
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
if (renderer == NULL) {
    SDL_Log("Renderer cannot be generated %s", SDL_GetError());
    exit(1);
}
SDL_RenderClear(renderer);


int x, y, r;
r = 50;


x = 100;
y = 100;
circleRGBA(renderer, x, y, r, 255, 0, 0, 255);
circleRGBA(renderer, x + r, y, r, 0, 255, 0, 255);
circleRGBA(renderer, x + r * cos(3.1415 / 3), y - r * sin(3.1415 / 3), r, 0, 0, 255, 255);


x = 280;
y = 100;
aacircleRGBA(renderer, x, y, r, 255, 0, 0, 255);
aacircleRGBA(renderer, x + r, y, r, 0, 255, 0, 255);
aacircleRGBA(renderer, x + r * cos(3.1415 / 3), y - r * sin(3.1415 / 3), r, 0, 0, 255, 255);


x = 100;
y = 280;
filledCircleRGBA(renderer, x, y, r, 255, 0, 0, 255);
filledCircleRGBA(renderer, x + r, y, r, 0, 255, 0, 255);
filledCircleRGBA(renderer, x + r * cos(3.1415 / 3), y - r * sin(3.1415 / 3), r, 0, 0, 255, 255);

x = 280;
y = 280;
filledCircleRGBA(renderer, x, y, r, 255, 0, 0, 96);
filledCircleRGBA(renderer, x + r, y, r, 0, 255, 0, 96);
filledCircleRGBA(renderer, x + r * cos(3.1415 / 3), y - r * sin(3.1415 / 3), r, 0, 0, 255, 96);


stringRGBA(renderer, 110, 350, "Press the x to exit", 255, 255, 255, 255);

SDL_RenderPresent(renderer);


SDL_Event ev;
while (SDL_WaitEvent(&ev) && ev.type != SDL_QUIT) {
    /* SDL_RenderPresent(renderer); - in case of MacOS Mojave  */
}

SDL_Quit();

return 0;

}


Solution

  • As fa as I'm concerned you should link the SDL library and SDL2_gfx Library to your project.

    First of all, you should find the files that you have downloaded. And the follow the following steps:

    1,Add the path to the header file to the Additional Include Directories(property - >c/c++ -> General -> Additional Include Directories)

    2,Add the path to the .lib file to the Additional Library Directories (property -> linker -> General -> Additional Library Directories)

    3,Add the name of the .lib file with its extension to Additional Dependencies (property -> linker -> input -> Additional Dependencies)