Graphic programming

Pages: 1234
Well my actual error while setting up Ganado is that I can’t find the SFML library


Use the VS 2017 download links here: https://www.sfml-dev.org/download/sfml/2.5.1/
You can then unpack the SFML archive wherever you like. Copying headers and libraries to your installation of Visual Studio is not recommended, it's better to keep libraries in their own separate location, especially if you intend to use several versions of the same library, or several compilers.

Previously versions of VS like 2010 were not compatible with VS 2013 and beyond, but VS 2017 and VS 2019 are compatible.

Then, follow the rest of the tutorial: https://www.sfml-dev.org/tutorials/2.5/start-vc.php
"Creating and configuring a SFML project"

I just downloaded it myself and built it and ran the green circle example myself. The tutorial works.

One specific part of the tutorial that is slightly off: The "Additional Include Directories" is now found under Properties --> VC++ Directories --> Include Directories
But just as the tutorial says, you would put the path to the /include folder of the SFML package you downloaded.
e.g.
C:\libraries\SFML-2.5.1-windows-vc15-32-bit\SFML-2.5.1\include
(I extracted the zip file in a centralized C:\libraries folder. You can put it anywhere.)

e.g. for the Linker's Additional Library Directories:
C:\libraries\SFML-2.5.1-windows-vc15-32-bit\SFML-2.5.1\lib


- Make sure that if you are building as debug, you set the Linker's Additional Dependencies to be the -d lib files, e.g.
sfml-graphics-d.lib
sfml-window-d.lib
sfml-system-d.lib


Don't use the optional SFML_STATIC. I mean, you can, but it will force you to configure additional dependencies. Just make sure you copy the -d DLL files into the folder where your exe is built.
e.g. inside the folder where your exe is, copy:
sfml-graphics-d-2.dll
sfml-system-d-2.dll
sfml-window-d-2.dll

The DLL files are found in the /bin folder of the downloaded zip.


Last edited on
I’ll give that a go Ganado, I’ll let you know how it goes and thank you all for your help
Well I agree Ganado, I think I need a better understanding of c++ before I start making a game. But I’m confused where should I start from, what should I start learning first? What is the best for me to learn at this stage
Learning C++ while creating simple console-mode games is a fun way to learn:

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
// Pirate Musketeer Game
// tells a pirate story

#include <iostream>

int main()
{
   std::cout << "You are a pirate and are walking along in the crime filled\n"
      << "city of Havana (in 1789).  How many of your pirate buddies\n"
      << "do you bring along? (lots)\n";

   // records the amount of friends you bring along
   int buddies { };
   std::cin >> buddies;

   // calculates the amount of pirates left after the battle.
   int afterBattle { 1 + buddies - 10 };

   std::cout << "Suddenly 10 musketeers jump out from the local tavern and\n"
      << "draw their swords. 10 musketeers and 10 pirates die in the\n"
      << "battle.  There are only " << (buddies + 1 - 10) << " pirates left.\n\n";

   std::cout << "They drop 107 gold coins.  That is " << (107 / afterBattle) << " gold coins each.\n";

   std::cout << "There is a big drunken brawl for the last " << (107 % afterBattle) << " coins.\n";
}

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
// The Weapon Store Game

#include <iostream>
#include <string>
#include <cmath> // for truncf() function

int main()
{
   std::string name;

   std::cout << "Welcome to the weapon store, noble knight.  Come to equip the army again?\n"
      << "What is your name? ";
   std::cin >> name;

   std::cout << "\nWell then, Sir " << name << ", Let's get shopping!\n";

   float gold                 { 50.0f };
   int silver                 { 8 };
   const float SILVERPERGOLD  { 6.7f };
   const float BROADSWORDCOST { 3.6f };
   unsigned short broadswords { };

   std::cout << "You have " << gold << " gold pieces and " << silver << " silver.\nThat is equal to ";
   gold += silver / SILVERPERGOLD;
   std::cout << gold << " gold.\n";

   std::cout << "How many broadswords would you like to buy? (3 gold, 4 silver each) ";
   std::cin >> broadswords;

   gold = gold - broadswords * BROADSWORDCOST;

   std::cout << "\nThank you. You have " << gold << " left.";

   silver = static_cast<int> ((gold - truncf(gold)) * SILVERPERGOLD);
   gold   = truncf(gold);

   std::cout << "\nThat is equal to " << gold << " gold and " << silver << " silver.\n"
      << "Thank you for shopping at the Weapon Store.  Have a nice day, Sir " << name << '\n';
}

Two simple 'games' that use basic features of C++ (with a bit of C thrown in).

A more complicated game:
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// The Snail Racing Game

#include <iostream>
#include <random>

// function declarations
int race(int, int);
void race();
int menu();
int placeBet(int);
void ini();

// variables
int money { 200 };

std::random_device                 ran_dev;
std::uniform_int_distribution<int> dist(1, 3);

// the main function
int main()
{
   ini();

   int userResponse;

   std::cout << "Welcome to the snail races!!!" << '\n';

   while (userResponse = menu())
   {
      switch (userResponse)
      {
      case 1:
      case 2:
      case 3:
         ::money += race(placeBet(userResponse), userResponse);
         break;

      case 4: // the user did not bet
         race();
         break;
      }
   }
}

// displays the main menu and returns the user's selection
int menu()
{
   int userResponse;

   std::cout << "You have " << money << " dollars.\n";

   do
   {
      std::cout << "Races Menu\n"
         << "1) Bet on snail 1\n"
         << "2) Bet on snail 2\n"
         << "3) Bet on snail 3\n"
         << "4) Just Watch\n"
         << "0) leave the races\n"
         << ": ";
      std::cin >> userResponse;
   }
   while (userResponse < 0 && userResponse > 4);
   std::cout << '\n';

   return userResponse;
}

// decides how much a person will bet on the snail
int placeBet(int userResponse)
{
   int betAmount;

   std::cout << "Snail " << userResponse << " is a good choice!\n";
   std::cout << "How much would you like to bet on your snail " << userResponse << "? ";
   std::cin >> betAmount;
   std::cout << '\n';

   return betAmount;
}

// if they are just watching the race
void race()
{
   race(0, 0);
}

// if they are betting money
int race(int money, int userResponse)
{
   // pick and store the random number
   // int winner = ran_dev() % 3 + 1;
   int winner { dist(ran_dev) };

   std::cout << "And the snails are off\n"
      << "Look at them GO!!!\n"
      << "The winner is snail " << winner << "!\n";

   if (winner == userResponse)
   {
      std::cout << " You Win!\n\n";
      return 2 * money;
   }

   std::cout << " You lose " << money << " dollars.\n\n";
   return -1 * money;
}

// handles program initializations
void ini()
{
   // 'warm up' the random number generator
   // discard the value
   unsigned temp { ran_dev() };
}

Have fun learning to C++.
These looks great Furry, i’ll get to learn a lot by this. I was wondering did you just make them by your self right now?
I also had one more question. What is the main difference between Javascript and C++?
Instead of using
std::cout & std::cin

Can I add
using namespace std;

Which then I can make it
cout <<
Ganado for the Green circle I tried it out. I followed all steps and did them correctly, I placed the code but it didn't work and i'm not sure why
What is the main difference between Javascript and C++?

I think a better question would be "Do they have anything in common?"
They are totally different beasts, the only thing they have in common is lots of { ( [ ] ) }

If you want to know more:
https://duckduckgo.com/?t=ffsb&q=difference+between+Javascript+and+C%2B%2B%3F+&ia=web
Can I add
using namespace std;
Which then I can make it
cout <<

You could for some quick throw-away code.
It's considered bad practice, though even B. Stroustrup and N.Josuttis use it in their books.
Can I add using namespace std;

You can, I don't use it.
https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

As your understanding of C++ grows you will learn about namespaces, and what problem(s) they were added to C++ to solve.
https://www.learncpp.com/cpp-tutorial/2-9-naming-collisions-and-an-introduction-to-namespaces/

I don't have using namespace std; in my code because my brain and fingers have typed std:: it is almost automatic.

If typing std::cout and other commonly and frequently used parts of the C++ standard library is too burdensome consider having using declarations, using std::cout; for example, is a better choice instead of using namespace std;.
https://www.learncpp.com/cpp-tutorial/using-statements/

I personally don't use using declarations or directives*, I use the explicit namespace prefixes. For me knowing I am dealing with something from whatever C++ library enhances readability and understanding.

*The Boost library is for me an exception, sometimes. The designers add a lot of namespace layers to their components, and they have using declarations strewn throughout their example code. When fully qualifying a Boost component requires typing 15-20 or more characters each time, then an occasional using declaration is helpful.
Last edited on
I placed the code but it didn't work and i'm not sure why
Saying "it didn't work" doesn't help me help you. You have to be specific as to what the issue is. What error messages does Visual Studio show? Do you have the project settings correctly pointing to your include/lib directories? Does the application run but then crash? etc.

But I’m confused where should I start from, what should I start learning first?
I would just get a good book, and do the practice problems in them at the end of each chapter. Make small programs to practice one thing at at time.
https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
Last edited on
@Tezzy, I threw a lot of code at you earlier. I'll bet you want to test that code without having to type it manually. You are using Visual Studio 2019 so a tutorial on setting up projects is here:
Compiling your first program - https://www.learncpp.com/cpp-tutorial/writing-your-first-program/

One difference is when I create new console-mode projects I DO NOT use the "Windows Desktop Wizard," I use the "Empty Project" option. No files added, no precompiled headers BS, I can then easily add new or existing files when I want.

You want to test that code? Create a new project, and add a new C++ file. Copy'n'paste and compile/run. If you copied the code correctly it should run.

I never use precompiled headers, they can cause problems when repeatedly editing/compiling code. For small projects, ones that take several seconds or a couple of minutes to build, precompiled headers are more hassle than they are worth IMO.

The code I put up earlier requires at best C++11 (the snail race), VS 2019 can compile the code without requiring any setting changes.
I was wondering did you just make them by your self right now?

Those code snippets I found years ago, sometime in 2001. I've made some changes to what I originally found; formatting, minor changes such as uniform initialization of variables, etc.

The biggest changes are with the snail race game. Using the <random> library for generating random numbers instead of using C's rand/srand.

The bell has tolled for rand() | Explicit C++ - https://web.archive.org/web/20180123103235/http://cpp.indi.frih.net/blog/2014/12/the-bell-has-tolled-for-rand/
Last edited on
If you want to know more:
https://duckduckgo.com/?t=ffsb&q=difference+between+Javascript+and+C%2B%2B%3F+&ia=web

Alr Thomas thanks, i’ll read that up as well!

Saying "it didn't work" doesn't help me help you. You have to be specific as to what the issue is. What error messages does Visual Studio show? Do you have the project settings correctly pointing to your include/lib directories? Does the application run but then crash? etc

I’ll write step by step instructions of what I did,
1. Downloaded SFML file which came out as a WinZip file.
2. Opened VS 2019 > Console application
3. I right clicked on the ‘Console Application’ on solution bar > properties and changed all files as it said to,
4. Made a new item from source and placed code there
I got the error message
SFML/graphics hpp directary not found & lots of a ‘;’ is required before the token

These error messges aren’t accurate but it was like this.
Last edited on
I'll bet you want to test that code without having to type it manually.

Honestly sometimes I type codes out, it gives me a better understanding of what i’m doing and helps improve on my C++ knowledge
SFML/graphics hpp directary not found
You have to add the Include directory for SFML, and also the library directory.

After downloading the SFML file, unzip to a known location.
Look inside it. You'll see a directory called "include". This is the directory Visual Studio needs to know about.

The "Additional Include Directories" is now found under Properties --> VC++ Directories --> Include Directories
But just as the tutorial says, you would put the path to the /include folder of the SFML package you downloaded.
e.g.
C:\libraries\SFML-2.5.1-windows-vc15-32-bit\SFML-2.5.1\include
(I extracted the zip file in a centralized C:\libraries folder. You can put it anywhere.)

e.g. for the Linker's Additional Library Directories:
C:\libraries\SFML-2.5.1-windows-vc15-32-bit\SFML-2.5.1\lib
Alr i’ll give that a go when i’m on desktop, THANK YOU SO MUCH!!
After downloading the SFML file, unzip to a known location.
Look inside it. You'll see a directory called "include". This is the directory Visual Studio needs to know about.


I unziped the file, but I'm unsure what to do for when you said "Visual studio needs to know about"?
Pages: 1234