Pong game (SFML)

Pages: 12
There are other cross-platform graphics technologies available, two that come to mind are Unity and Unreal. Both have on-demand packages available in Visual Studio. Another package is Cocos (Cocos2dx).

I see more than a few games that were created using Unity or Unreal, and the list keeps growing.

For Win games there is always DirectX, which can also IIRC be used for Xbox games.
Thanks. Yes, I know about Godot, Gamemaker Studio, Unity, Unreal engines and the likes, but I really want to stick with pure C++ after having just enough knowledge to get me into trouble & getting the C++ bug and I need to practice. Later this year I will also do Unreal Engine.

I will delve into openGL some core, because it is quicker to learn & so my next book will be "Game Programming in C++: Creating 3D Games (Game Design) 1st Edition". I already bought my DirectX11 & 12 books, and I only skimmed the first 1-2 chapters because they are harder reads. I need to practice what I learned some more, but I think in another 2-3 months I will be ready.

Ultimately Vulkan would be nice too, but way out of my league for now.

8) QUESTION?
I have not looked deeply into this, and I would also love to create a simple "Hello World" android app or even to draw a simple circle. What would be the best way?

I know there is a NDK for Java to use C++, even though they recommend native Java for simplicity. I know there is an OpenGL for Android/iOS.
Doing graphics/GUI and/or sound/music in ANY form is not "pure C++". Doing it requires 3rd party libraries or an OS like Windows, and a lot of grunt work that is not part of the C++ standard.

I am not remotely suggesting you don't try to dink around with media, just concentrate for now on getting a firm grasp on what the C++ standard does. And doesn't do.

One "weird thing" about graphical games is they for the most part don't use but a fraction of what C/C++ offers. Games go all anal about frame rates and buffering sprites, asynchronously emitting noise/music, etc.

It is entirely possible to create 2D windowed games using Windows GDI and the ancient MS multimedia library. Most of the work is done for proper timing to display the game, get user input via keyboard/mouse/joystick and process it before the next frame and doing the animation smoothly without flickering. That is even before worrying about having computer controlled entities that are AI run.
Thanks George! I know I will have to rely on some external software for graphics for C++, what I was trying to say is that I would rather not deal with something like C# in Unity for now and stick with all things related to C++. Yea, I know I am bad also, as I should be reviewing my first book from the beginning, but I just had to see some graphics and fun stuff after last looking at boring containers.

9) QUESTION:
I have moved on to the next game and have a question regarding VertexArray and sprite sheets.

The game has a 50x200 sprite sheet, with 4 images each image is 50x50...here is a pic of it
https://www.oreilly.com/library/view/beginning-c-game/9781786466198/ch07s02.html

So in the book he shows...he really has those numbers typed in the book:

Image1 goes from 0,0 - 49,49
Image2 goes from 0,50 - 49,99
Image3 goes from 0,100 - 49,149
Image4 goes from 0,150 - 49,199


Here I spell out each corner for the 4 images (according to the book), to make it easier to see:

IMAGE 1:
0,0 49,0

0,49 49,49


IMAGE 2:
0,50 49,50

0,99 49,99


IMAGE 3:
0,100 49,100

0,149 49,149


IMAGE 4:
0,150 49,150

0,199 49,199



BUT THEN, the code actually goes one beyond the last pixel and I printed the output out for each set of vertices. I am surprised it does not crash or something when trying to reach beyond, or maybe he does it on purpose and instead it prints an empty pixel line between the tiles?

[0] = (0, 0)
[1] = (50, 0)
[2] = (50, 50)
[3] = (0, 50)

[4] = (0, 50)
[5] = (50, 50)
[6] = (50, 100)
[7] = (0, 100)

[8] = (0, 100)
[9] = (50, 100)
[10] = (50, 150)
[11] = (0, 150)

[12] = (0, 150)
[13] = (50, 150)
[14] = (50, 200)
[15] = (0, 200)

[16] = (0, 200)
[17] = (50, 200)
[18] = (50, 250)
[19] = (0, 250)

[20] = (0, 250)
[21] = (50, 250)
[22] = (50, 300)
[23] = (0, 300)

[24] = (0, 300)
[25] = (50, 300)
[26] = (50, 350)
[27] = (0, 350)
........................400 total  vertices (for the 10x10 map on screen with Quad vertices (x4) = 400)




Code is:
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
#include "ZombieArena.h"
#include <iostream>			//EXTRA
using namespace sf;
using namespace std;		//EXTRA

int createBackground(VertexArray& rVA, IntRect arena)
{
	const int TILE_SIZE = 50;
	const int TILE_TYPES = 3;	//Really 4 tiles, but is zero based
	const int VERTS_IN_QUAD = 4; //background.setPrimitiveType(Quads)...4 vertices

	int worldWidth = arena.width / TILE_SIZE;
	int worldHeight = arena.height / TILE_SIZE;

	rVA.setPrimitiveType(Quads);

	rVA.resize(worldWidth * worldHeight * VERTS_IN_QUAD);	//Set size of vertex array

	int currentVertex = 0;		//Start at beginning of vertex array


	for (int w = 0; w < worldWidth; w++)
	{
		for (int h = 0; h < worldHeight; h++)
		{
			//Position each vertex in the current quad
			rVA[currentVertex + 0].position = Vector2f(w * TILE_SIZE, h * TILE_SIZE);
			rVA[currentVertex + 1].position = Vector2f((w * TILE_SIZE) + TILE_SIZE, h * TILE_SIZE);
			rVA[currentVertex + 2].position = Vector2f((w * TILE_SIZE) + TILE_SIZE, (h * TILE_SIZE) + TILE_SIZE);
			rVA[currentVertex + 3].position = Vector2f((w * TILE_SIZE), (h * TILE_SIZE) + TILE_SIZE);

			//EXTRA TESTING!!!!!!!!!!!!!!!!!
			cout << "[" << currentVertex + 0 << "] = (" << (w * TILE_SIZE) << ", " <<  h * TILE_SIZE << ")" << endl;
			cout << "[" << currentVertex + 1 << "] = (" << (w * TILE_SIZE) + TILE_SIZE << ", " << (h * TILE_SIZE) << ")" << endl;
			cout << "[" << currentVertex + 2 << "] = (" << (w * TILE_SIZE) + TILE_SIZE << ", " << (h * TILE_SIZE) + TILE_SIZE << ")" << endl;
			cout << "[" << currentVertex + 3 << "] = (" << (w * TILE_SIZE) << ", " << (h * TILE_SIZE) + TILE_SIZE << ")" << endl << endl;

			// Define the position in the Texture for current quad
			// Either grass, stone, bush or wall
			if (h == 0 || h == worldHeight - 1 || w == 0 || w == worldWidth - 1)
			{
				// Use the wall texture
				rVA[currentVertex + 0].texCoords = Vector2f(0, 0 + TILE_TYPES * TILE_SIZE);
				rVA[currentVertex + 1].texCoords = Vector2f(TILE_SIZE, 0 + TILE_TYPES * TILE_SIZE);
				rVA[currentVertex + 2].texCoords = Vector2f(TILE_SIZE, TILE_SIZE + TILE_TYPES * TILE_SIZE);
				rVA[currentVertex + 3].texCoords = Vector2f(0, TILE_SIZE + TILE_TYPES * TILE_SIZE);
			}
			else
			{
				// Use a random floor texture
				srand((int)time(0) + h * w - h);
				int mOrG = (rand() % TILE_TYPES);
				int verticalOffset = mOrG * TILE_SIZE;

				rVA[currentVertex + 0].texCoords = Vector2f(0, 0 + verticalOffset);
				rVA[currentVertex + 1].texCoords = Vector2f(TILE_SIZE, 0 + verticalOffset);
				rVA[currentVertex + 2].texCoords = Vector2f(TILE_SIZE, TILE_SIZE + verticalOffset);
				rVA[currentVertex + 3].texCoords = Vector2f(0, TILE_SIZE + verticalOffset);
			}

			// Position ready for the next for vertices
			currentVertex = currentVertex + VERTS_IN_QUAD;

		} //END for loop h
	} //END for loop w

return TILE_SIZE;

} //END     int createBackground(VertexArray& rVA, IntRect arena)     function 



So I try this small one below on paper to verify the book and he seems correct in book with zero based address access.
3x3 sprite, each dot is pixel, & the sprite goes from 0,0 - 2,2 (with zero base coordinates):

... 0,0 1,0 2,0
... 0,1 1,1 2,1
... 0,2 1,2 2,2
I looked at it again & had to strain a few more brain cells. I think what is going on is that .position= does not really read or affect the sprite sheet and he can therefore go past the number in his formula (so from 49 actual to 50 simulated/programmed).

In the .texCoords = I guess you can do whatever you want, independent of the sprite sheet and so when it reads x = 50 there is nothing there & it prints to sreen an empty pixel....I think????

Wish he would have said that in the book, if that is the case.


I also printed out the coordinates of the tile printout to the screen & what it looks like:
https://static.packt-cdn.com/products/9781786466198/graphics/image_10_005.jpg


[0] = (0, 150)
[1] = (50, 150)
[2] = (50, 200)
[3] = (0, 200)

[4] = (0, 150)
[5] = (50, 150)
[6] = (50, 200)
[7] = (0, 200)

[8] = (0, 150)
[9] = (50, 150)
[10] = (50, 200)
[11] = (0, 200)

[12] = (0, 150)
[13] = (50, 150)
[14] = (50, 200)
[15] = (0, 200)

[16] = (0, 150)
[17] = (50, 150)
[18] = (50, 200)
[19] = (0, 200)

[20] = (0, 150)
[21] = (50, 150)
[22] = (50, 200)
[23] = (0, 200)

[24] = (0, 150)
[25] = (50, 150)
[26] = (50, 200)
[27] = (0, 200)

[28] = (0, 150)
[29] = (50, 150)
[30] = (50, 200)
[31] = (0, 200)

[32] = (0, 150)
[33] = (50, 150)
[34] = (50, 200)
[35] = (0, 200)

[36] = (0, 150)
[37] = (50, 150)
[38] = (50, 200)
[39] = (0, 200)

[40] = (0, 150)
[41] = (50, 150)
[42] = (50, 200)
[43] = (0, 200)

[44] = (0, 0)
[45] = (50, 0)
[46] = (50, 50)
[47] = (0, 50)

[48] = (0, 0)
[49] = (50, 0)
[50] = (50, 50)
[51] = (0, 50)

[52] = (0, 0)
[53] = (50, 0)
[54] = (50, 50)
[55] = (0, 50)

[56] = (0, 0)
[57] = (50, 0)
[58] = (50, 50)
[59] = (0, 50)

[60] = (0, 0)
[61] = (50, 0)
[62] = (50, 50)
[63] = (0, 50)


I should be reviewing my first book from the beginning, but I just had to see some graphics and fun stuff after last looking at boring containers.

HUGE mistake, period. I'd bet a lot of the problems you are experiencing derive from not understanding core concepts explained earlier, coupled with "let's modify this to see how it works."

Games require a solid foundation of the language used, even if they don't use all the features the language offers.

"For want of a nail the war was lost," and you're close to losing the C++ war that would be easier to deal with getting a foundation first. Go back and go through the book from the beginning.

Nothing about C++ is boring when properly understood, applied and used. The more you know about the core language the easier using 3rd party libraries should become.
@protoseepp - is the code above yours or taken from the book? random floor texture is based upon C random functions (and are used wrongly - srand() should only be used once at the very beginning of a program) rather than C++ std::random.

Beginning C++ Game Programming


Note that reviewers have pointed out significant errors in the code examples in the book. I see that this is a Packt book. I've looked at/got several Packt books and IMO they are a mixed bag. Some are good, others are not and should have major surgery by the editors.
Last edited on
Thanks for the direction guys, I appreciate it.

I am actually doing pretty darn good with the game book as I was able to read the book, then write the code, then duplicate the first 2 games without looking. I have not changed any code with this 3rd game (only to cout) and I understand everything that I read. I can already duplicate the double for loop that I posted from above without looking and most of the other parts of this 3rd game and I understand all of what the game is doing and the code that I posted.

The question that I asked is actually more simpler & probably sillier, as the question just boils down to why the VertexArray is accepting coordinates from 0-50 when the book clearly shows that a 50x50 image has the following coordinates in each corner (ranging from 0-49)? As a test I mod'd the sprite to spill over beyond its limits & filled the background with red, but when tiled no lines of red show up between the tiles. So now I am thinking maybe the VertexArray takes in 0-50 for the math convenience but does not use the 50....only to 49 behind the scenes.

[quote]
IMAGE 1:
0,0 49,0

0,49 49,49
[quote]

Just came to read in the last few pages that this game is using a map container to spawn the zombies & it is already just an appetizer for when I go back to containers. What I meant was that I had zipped through the container chapters and needed a temp break from them for now, not that I never want to see them again. It is funny how the brain works sometimes, when you encounter complex problems or too much new material, it is better to switch to another subject and I find that you learn more that way...at least for me.

Trust me, this 3rd game is sooo long and after finishing it, I am going to go back to containers with pleasure.
FYI, new STL C++20 Cookbook is out and I really want a physical book.

https://www.amazon.com/20-STL-Cookbook-Leverage-real-world/dp/1803248718/ref=sr_1_1?crid=29I9CGM29XLLZ&keywords=C%2B%2B20+STL+Cookbook%3A+Leverage+the+latest+features+of+the+STL+to+solve+real-world+problems&qid=1654595216&sprefix=c%2B%2B20+stl+cookbook+leverage+the+latest+features+of+the+stl+to+solve+real-world+problems%2Caps%2C62&sr=8-1

I remember you guys setting me straight on an array deprecating to a pointer when passed to a function. I had no idea where that concept came from as my book only passed an array and did not mention anything about any deprecation and I wonder how long I would have went on without knowing. It did make perfect sense though and I was grateful for all your valuable inputs as I have learned so much faster.
So it seems that the VertexArray does not handle the sprite sheet on a pixel per pixel coordinate. Although it is true that the 1st image in the sprite sheet is 0-49 pixels in width and 0-49 pixels in height the .textCoords acts like a mesh on top of the image and they do this so you can scale up/down the image.

If you did a 1:1 mesh on top, then pixel 0 will fall between 0-1 of the mesh coordinates and the last pixel 49 will fall between 49-50 of the mesh.

myVertexArray[i].textCoord


Below, you can make position any size you want and it will scale the image into position, which works out even better than a pixel per pixel array copy.

myVertexArray[i].position
If you are interested in creating games for Windows, and don't mind using older technology that is part of the WinAPI core, consider using the GDI and multi-media library. A couple of older books showing how to create 2D Win games using the WinAPI:

From 2003: https://www.amazon.com/Sams-Teach-Yourself-Programming-Hours/dp/067232461X/

From 2004: https://www.amazon.com/gp/product/0672326590/

I have both books, the 2nd is mostly a minor update to the first with a couple of different style game ideas at the end, and different graphics used.

Both books have source code available on CD. The main emphasis of the books is crafting a game engine that can be used to create different games without rewriting the core engine. A slick OOP reuse approach.

The WinAPI is not C++, pure or otherwise. The WinAPI is C.

The entry point for a WinAPI is WinMain, not main. Get beyond that and you shouldn't have too many problems that need to be squashed if you get the books.

Since the books are from before C++11 the provided code could use some "clean-up" rewriting using C++11 and later.
Last edited on
Those 2 links are for the same 1st edition book!

The entry point for a WinAPI is WinMain, not main.


Only if you're using gui windows. If you're not coding a gui interface then it's still main()
Last edited on
ooops, my bad, links corrected.

I could say the mistake was because I was dealing with someone who didn't want to listen to advice....

But no, I won't say that. I screwed up. No, ifs, ands or arses. :Þ
Only if you're using gui windows. If you're not coding a gui interface then it's still main()

Using main() IMO is not "true" WnAPI (Desktop) programming.

I make a distinction because of the different ways of dealing with users and I/O with GUI vs. command-line.

Yes, an artificial one, but it does serve a purpose in broad view understanding of dealing with events of most apps.

Full screen games tends to break that mold so it ain't a hard and fast "never to be overturned" rule. M'ok?
Thanks George, I will have to hit the Win32 API eventually as well and the Sam's book seems like relative easy reading. Although, the Win32 names are atrocious to me at a quick glance.

I was going to use SFML any time I needed to make a Window, but it would be nice to know Win32 as well. Other ones I can use are: Allegro 5, ClanLib, Qt, & WxWidgets. I wonder what devs see as the most used library to create windows in the wild?

Win32 API will allow me to create drop boxes & check boxes and things of that sort too.

How often do they update the Win32 API's? Why has Sam's not created an update to that book since 2003?

I chose SFML over SDL based open their similarities and the fact that the SFML game book was rated higher. Here is the SDL book. How do you feel about SDL vs SFML?

https://www.amazon.com/SDL-Game-Development-Black-White/dp/1849696829/ref=sr_1_4?crid=1KJJGX8OD1EE8&keywords=SDL+game+programming&qid=1654761946&sprefix=sdl+game+programming%2Caps%2C54&sr=8-4


From my explanation above, this is what the grid would look like for the
myVertexArray[i].position

Each pixel of the sprite goes inside the cube and the .positions range from 0-50. So there are 51 lines that demark the area of 50 pixels.

How do you insert a picture on this site?
[picture]
https://pnggrid.com/wp-content/uploads/2021/07/50x50-Grid-PNG-50-by-50-Square-Grid.png
[/picture]

Last edited on
Topic archived. No new replies allowed.
Pages: 12