Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Looking for suggestions on a game that I am porting from Javascript to C++  (Read 1052 times)

0 Members and 1 Guest are viewing this topic.

DaveLembke

    Topic Starter


    Sage
  • Inventor of the Magna-Broom 3000 =)
  • Thanked: 590
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 7
There was a game that I made a ways back when I was taking JavaScript Programming in college 20 years ago. I was thinking the other day of porting that by rewriting it for C++ from scratch to be a stand alone game that runs on C++ and ran into the challenges of graphics. So I remember in one of my old Borland C++ books it had bitmap rendering and all sorts of stuff on bitmaps.

So I skipped over looking this up in my old book from the late 1990s and went on to make the 120 different game states as 120 differently named bitmaps in Microsoft Paint. The idea is to call to the bitmap that displays the game state to display that game state without having to specify every bitmap pixel or sprite location, just call to the image and display the image that shows the game state which I thought would make it way simple than having to piece together pixels.

My book is somewhere, possibly lost in the move, and searching google I dont see any exact matches to what i am trying to do and they all seem to call to an outside viewer to display the image which for a game to have a viewer instance called, the only way that that could work for this game is if there was a refresh loop that was looking at a target location and display any bitmap at that location so as the game changes the viewer changes the image result of whatever the player is doing. Additionally there is the problem with the fact that you now have 2 windows open. One that displays the image for the game state and the other for the user input.

I'd like to get this all into the same window vs calling to outside viewer and its looking like I need to do away with console C++ programming and switch to programming for Windows. However once again I hit a wall looking for code examples to learn from to piece together my program to make this work.

The game title was Dark Room Deathmatch, the concept of 2 or more players placed into a dark confined space room with a knife and only 1 player survives, and they can not hear or see each other as they are all quiet in moving but when they bump into someone they stab them and then get to flea so who they are attacking cant just target the same location they are at. It starts off where you can't see your opponent initially. And after a certain number of moves the room shrinks in space by blocks in strategic areas no longer in play and so they get greyed out, and if a player is located at one of these blocks that is no longer in play they get relocated to the nearest block that is still in play. As the game progresses further if someone hasnt died yet then hint of blood is enabled and so you have an idea of where your opponent is sort of like hot or cold hot for closer to opponent and cold for further away but it doesnt give exact location away it makes blocks glow pink around a player but the player might not necessarily be at the center of the glowing blocked area they could be located on the edge of the glowing pink blocks. I also added special moves to the game that allowed players to teleport themselves to any block that is still in play but it was a limited use feature. If someone felt like they were being cornered they could use it to jump to far end of map or any location still in play. This feature of teleporting though wasnt enabled until a certain number of moves so that someone couldnt use it as their opening move to exploit the person going first landing on the starting location of their opponent which is known at the start.

Because I am not an artist by any means I chose to go low tech with just a map that shows your location in relation to the play area. Its about as low tech as a board game with a piece that moves on the board. The game is a combination of other game concepts mashed together. For the fact that your playing in the game environment of in the dark there is no need to display anything that impressive other then a map with a block location to indicate where you are as well as any changes to the map.

With javascript for this game it was pretty easy as for the browser environment allowed for user input and a refresh of the image to show the change in the game state. An array was used to keep track of where the player was, and if the array value was 5 for the players location, the image for map5.bmp would display to reflect the location of the player in regards to the other player. The game was like battleship in that you didnt know where the other player was but that when you found them they could flea. So unlike battleship where once you hit the opponent you just need to determine the orientation of fixed objects, in my game the opponent can go in 8 different directions to flea in a 30 block area laid out as 2 blocks, 4 blocks, 6 blocks, 6 blocks, 6 blocks, 4 blocks, and 2 blocks with each player starting on opposite ends of the map. It starts out as an open area and no idea where each other is until you land on them to strike a hit. After so many plays in the game blocks in specific locations turn from white to grey as they are barriers that are placed into the game to make the odds of bumping into each other increased as there are lesser blocks to move to. Then as the game goes on for even more turns by each player blocks in an area will turn pink indicating that your enemy is near and so each person knows a hot or cold close or far away from each other to further increase the odds of each other striking each other by landing on the block that they are at. After a player is struck 10 times they are dead. This javascript game was single player because making it 2 or more players would have required a database and forms to pass the data to the database etc and web server side dynamic HTML etc and time was limited.

I was thinking if I could get this working with C++ ( or just about any other programming language out there ) that I can get the multiplayer aspect of this to work. The game would play on 2 computers that have access to a network share. The network share has text files for the game state information to be passed between the program running on each of the computers. One of the text files is a read/write lock which keeps control of Player #1 vs Player #2's turn. The game has a time delay loop in it that tests the value within the text file to see whos turn it is. If the value is 1 then its player 1's turn. If the value is 2 then its player 2's turn.

I was talking to a friend at this years game con and he suggested that I should bring the game there next year with 2 computers since I have plenty of spare working computers that I could leave set up for people to sit down and play against each other etc. Even though its a low tech game and no impressive graphics he said it sounds like it would make for some fun for people looking for a break from the other games.

If I can get it working as 2 player then my goal is to then make it 4 player turn based with a larger map. But not going to go that route until I can get the 2 player version of this hammered out and working.

the reason why i am not going with a database to keep track of game state is because its just a few players. Its never going to be a game that is large scale that you have multiple instances of it going on, and a network share to text files with game states is simplistic and I have used that method before in the past for 2 player card game that was C++ console based to play War with a network connected friend to a network share back in college.

Javascripts inability to read/write to files locally as designed due to security reasons is the main reason why i didnt just take what I had in javascript and make it 2 player.

Part of me is feeling a tug to go with dynamic HTML vs beating my head against the C++ wall and just make the game run off of an apache web server with php for example and go the database route for keeping track of game state and progress. This method would make use of the bitmap game states very simple to implement. But I havent caved to that just yet and am sort of determined on a way to pull this off with C++ and avoiding the use of a database and instead just text files that each computer uses to keep track of turn and game progress.

I looked for a C++ code example that has a button and displays one bitmap image and then click on that button and it displays a new image as for once I know how to achieve this I can replicate the code and add the logic guts to it to make the rest of it come together I feel.

Additionally I refuse to just go the route of displaying ASCII characters for a display for the user with a console window open and something that looks like this with X as the players location
          [  ] [  ]
     [  ] [  ] [  ] [  ]
[  ] [  ] [  ] [  ] [  ] [  ]
[  ] [  ] [  ] [  ] [  ] [  ]
[  ] [  ] [  ] [  ] [  ] [  ]
      [  ] [  ] [  ] [  ]
           [X] [  ]
To show location of the player on a map with the slightly better than Zork method of gaming.  ::) ;D

Seeing a picture vs ascii just looks more appealing and at least gives it an Atari 2600 feel of complexity which is more visually appealing.  ;D
Additionally I feel that I need to get over this roadblock and learn from it vs going an alternate route to achieve this unless its more realistic to go the route of the apache web server with people connecting to it with dynamic HTML, php, and form data passed to database to make it 2 to 4 players.

The up to 4 player version would be like this, but as a clean looking bitmap image in paint with some eye appeal and maybe a nice background too. Only the player themselves location is displayed to them. They cant see the other player or players. The numbers 1,2,3,4 indicating player start locations and all of then free to move in any direction and land on each other to land hits and able to move 1 or 2 places per turn or skip moving and stay put if foolish to wait to be landed on or stay out of the mix in the middle in hopes to wait it out around the outside perimeter until 2 players are eliminated and they can then focus on just 1 opponent as everyone went after each other towards the middle of the play area using the demolition derby method of staying out of the initial fight wih heavy damage to then wait it out to see who survives it and then go after them in hopefully better shape than they are in leaving the fight but taking some damage from others who failed.  ::) ;D

               [  ] [3]
          [  ] [  ] [  ] [  ]
     [  ] [  ] [  ] [  ] [  ] [  ]
[2] [  ] [  ] [  ] [  ] [  ] [  ] [  ]
[  ] [  ] [  ] [  ] [  ] [  ] [  ] [4]
     [  ] [  ] [  ] [  ] [  ] [  ]
           [  ] [  ] [  ] [  ]
                 [1] [  ]

Players health Bars can be displayed or not displayed by choice. If not displayed for actual health bar to be known by all then its just an alive or dead indication.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1074
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 8
Re: Looking for suggestions on a game that I am porting from Javascript to C++
« Reply #1 on: December 02, 2017, 03:10:46 PM »
If you want to create a GUI in C++ you are going to have to either learn to construct and manage Windows and Device Contexts directly via the Windows API, or use a framework to manage it for you.

Visual Studio has a C++ template for a empty Desktop Application. But you will need to be familiar with the Win32 API.


I think saving every possible board state seems excessive. As you add features to the game you will need additional board states, and the bitmaps are also going to be relatively large. Since it's a simple grid it isn't difficult to just draw squares. Saving every board state is only feasible for simple, "solved" games like tic-tac-toe.

The basis for the entire thing seems to be about movement within a grid.

I don't think a multidimensional array is needed. Instead, you can have the number of rows and columns in the grid and then store the positions of any appropriate things such as players, or information about blocks that aren't available anymore and evaluate things that way. It also opens up additional possible features, such as pits that cause you to lose a turn, or finding "power ups" if they were to be added (perhaps a single-shot gun that can shoot in one of the 8 cardinal directions and will eliminate players in that path, or a flashlight with a low battery that let's you illuminate a triangle in a particular direction and see the location of players, etc.

The grid-based movement is the basis for the entire idea and is relatively straightforward, of course I'm going to go out on a limb and say you have no idea how to even create a Windows Application in C++, let alone how to draw anything in one. I mean, you wouldn't be posting this question otherwise :P.

I've never created a GUI C++ Application either that I recall, but I've worked with the Win32 API a lot and was able to use some boilerplate for the standard stuff. I made a thrown together, very rough example which allows moving within a larger grid-based play area. This is *very* thrown together, and shouldn't really be used as an example of anything; I threw everything into a single source file. It still has a bunch of unnecessary stuff like the menu and about box and stuff which need to be stripped out.

Fundamentally it stores information about the Play area (number of columns, number of rows) and the position of a single player. it draws the board in a very basic way with filled and outlined squares, and will draw the square with the first player in it differently. It responds to arrow keys by changing the first player's position and forcing things to repaint.

A version that plays over the network should use Sockets properly. How many games have you played which save text files to a shared drive for multiplayer? WoW doesn't make you use Hamachi and have a shared drive. Mind you I might not be much help with that one, my experience with networking via Sockets is largely with our Server program on Windows which mimics a much older server program that ran on a much older OS and it takes the requests and commands which would typically deal with ancient ISAM database files, and translates them to instead read/write Postgres tables. I didn't write it and have only made bugfixes and a few additions and it's not tended to be anywhere near where the actual network socket communication occurs.
I was trying to dereference Null Pointers before it was cool.

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 961
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows XP
Re: Looking for suggestions on a game that I am porting from Javascript to C++
« Reply #2 on: December 02, 2017, 03:16:58 PM »
Why?    :)

(Rhetorical question. I don't need any answer. You need to think about your objective.)

In general, programs that are interpreted should stay that way. The interpreted program ca resolve  dependencies at run time, something the compiler can not do.


DaveLembke

    Topic Starter


    Sage
  • Inventor of the Magna-Broom 3000 =)
  • Thanked: 590
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 7
Re: Looking for suggestions on a game that I am porting from Javascript to C++
« Reply #3 on: December 02, 2017, 08:13:51 PM »
Quote
The grid-based movement is the basis for the entire idea and is relatively straightforward, of course I'm going to go out on a limb and say you have no idea how to even create a Windows Application in C++, let alone how to draw anything in one. I mean, you wouldn't be posting this question otherwise


 ;D Other than the books that come with lab projects and a CD or DVD in the back of them to follow and make programs in Visual C# or Visual C++ for Visual Studio Express 2010 for example, I have mainly programmed for console vs Windows. If push comes to shove I play around with Windows based programming and digging through pages for code examples, but my comfort zone in which i dont have to fumble through pages of a book to program has been down and dirty console programs that do what I need because I am the end user and it doesnt need to be pretty just need to work with exception to the fact that console programming doesnt have attractive user interfaces and no graphics or sounds other than BEEP  :P etc.

Many many years ago I did some drawing of lines, and pixel sprite manipulation etc with Basic. Basic at least had color and sound tone / duration built into it. My biggest problem back then was trying to get the refresh right so that the object moving on the display wouldn't flicker as my refresh was a loop spamming the CLS clear screen to clean up the old display state. There was likely better ways to refresh, but the clear screen spamming was my method and I had seen it done with some other people code examples. ::) There was code in a magazine back in the days when programs would be in monthly magazines (1980s) that showed how to set it up to use arrow keys and space bar and I used that code from the magazine for my game, however proper rendering of objects was pretty ugly and I was playing a round with adding time delays in the refresh in order to get game objects to display properly. This slowed down the game execution as well since it was single threaded so the program was on a pause state until  the for loop counted from X=1 to 100 and moved on then reset the value to 1 so on next iteration X=1 and it would loop to 100 and repeat. When not using code sections for myself I would program it as seen in magazine and then correct for typos and when the program ( mostly games ) ran correctly I then would mess around with it all and trial and error learn what happens if you mess with this or that.

Sadly when I was in college ( late 1990s ) the professor taught VC++ 5.0 and everything was in console. No Windows programming at all. I asked why not be teaching how to make Windows programs since DOS type of games and programs are a thing of the past. His statement was that if I wanted to make Windows games that I should be taking the Visual Basic course as for the C++ courses they offer are all programming in console and that the type of C++ programming that is taught here gives a good foundation for writing programs that large companies would use for business with databases etc and not games. Knowing from reading about how the games out there are made mostly in C++ instead of Visual Basic, I chose the path of sticking with C++ because I felt it was the better path to go with programming for all platforms vs being stuck with only programming for Windows OS computers as well as while the teacher was telling me if I want to program Windows games I should have taken Visual Basic, I knew that that wasnt the programming language to be in for game programming as for greater than 80% of the games at the time were created using C++.  ::)

At some point I should probably take an up to date college level programming course that doesnt program in console programming and have the hands on learning with a teacher who can look over my shoulder and point out this or that. For now though I have books and follow lab examples but I find myself copy pasting sections of code that will do something and so if I needed a specific function I know copy/pasting it in and editing it as needed to do whatever I need, but picking it apart I might only know half of what is there as to what it does the other half I know is necessary but not sure exactly why etc. And doing this sometimes means that there is creep code. Portions of code that came from elsewhere that doesnt flag as a warning that its unnecessary when compiling but a skilled programmer would be like... why is that there it makes no sense... its harmless in that it doesnt affect the execution of the program but it stands out to someone who is a skilled coder that it doesnt belong. When explaining that I copy/pasted code from elsewhere and then tweaked it to what i needed it for and didnt quite understand every part of what is going on, it then makes sense as to why its there and that it wasnt added intentional for any extra feature in my code, but instead is unnecessary code that came from elsewhere. Usually though compiling there are warnings and I can remove stuff that isnt necessary and then get a clean compile without warnings, but sometimes the compiler doesnt catch it as unnecessary.  :P

Regarding the use of the shared text file... yes that is an odd way to slap it together and make it work. Not complex but very different, and not a method that would make any sense if designed to be distributed etc. WoW uses databases for keeping track of everything which is the best method especially with at one point 10 million copies sold and probably half that active users.  :)

Thanks for the reply to this.  Going to look into Win32 API also thanks for linking your game snippet going to check that out after I click post.  8)

*Update* ... Got a 404 page error for that link you provided.

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 961
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows XP
Re: Looking for suggestions on a game that I am porting from Javascript to C++
« Reply #4 on: December 02, 2017, 09:24:08 PM »
Possible reference that might help a little.
https://www.leaningtech.com/cheerp/buy/
Cheep
Quote
Cheerp is distributed as a Free and Open Source Software under the University of Illinois/NCSA Open Source License (core compiler) and under the GNU General Public License v2 (libraries).
For commercial uses, Cheerp is also available with a non-copyleft proprietary license, private support, and premium features. Three licenses are available:

Code: [Select]
// The cheerp/clientlib.h header contains declarations
// for all the browser APIs.
#include <cheerp/clientlib.h>

// webMain is the entry point for web applications written in Cheerp.
void webMain()
{
    // client is a C++ namespace that contains all browser APIs
    client::console.log("Hello World Wide Web!");
}
You can compile this program using the following command line:
Code: [Select]
/opt/cheerp/bin/clang++ -target cheerp example.cpp -o example.js
Source for above:
https://github.com/leaningtech/cheerp-meta/wiki/Cheerp-Tutorial


BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1074
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 8
Re: Looking for suggestions on a game that I am porting from Javascript to C++
« Reply #5 on: December 03, 2017, 03:58:25 AM »
Fixed, Apparently Private pages/posts require login to show, I thought they just needed the URL. Probably because of some security modules I installed recently. I've made it public so the link should work now. I'll probably just put it at the end of this post anyways :P

Many many years ago I did some drawing of lines, and pixel sprite manipulation etc with Basic. Basic at least had color and sound tone / duration built into it. My biggest problem back then was trying to get the refresh right so that the object moving on the display wouldn't flicker as my refresh was a loop spamming the CLS clear screen to clean up the old display state. There was likely better ways to refresh, but the clear screen spamming was my method and I had seen it done with some other people code examples. ::)

Yes, you wanted page flipping. If memory serves it was done with certain arguments to the SCREEN statement in BASIC which switched to graphics mode. Certain Screen modes supported page flipping, and you used different parameters to SCREEN to change the active buffer as well as change the active one. Buffer 0 would be on screen and you would draw to the second buffer, then when finished, instantly flip to the second buffer, then you would switch to the first buffer as active, clear it, and draw there, then when finished flip back to the first buffer, etc. the CLS and drawing would be effectively off-screen. It can also be done with non page-flipped modes with a memory buffer but that is more involved.

Quote
His statement was that if I wanted to make Windows games that I should be taking the Visual Basic course as for the C++ courses they offer are all programming in console and that the type of C++ programming that is taught here gives a good foundation for writing programs that large companies would use for business with databases etc and not games.

That doesn't make much sense to me! Business software for both small and large companies aren't usually console programs. The software I work on is for Inventory management, invoices, sales, Marina reservations, Work orders, etc. And they are all Windows GUI Applications. The only Console program in the entire thing is something I threw together as part of our source code build script, so it is only on our build server. Even in the mid to late 90's using Windows console software wasn't really a thing. Maybe on a *nix system but mostly with ancient mainframe terminal software, and businesses don't usually want to upgrade when there is going to be no real change.

Quote
Knowing from reading about how the games out there are made mostly in C++ instead of Visual Basic, I chose the path of sticking with C++ because I felt it was the better path to go with programming for all platforms vs being stuck with only programming for Windows OS computers as well as while the teacher was telling me if I want to program Windows games I should have taken Visual Basic, I knew that that wasnt the programming language to be in for game programming as for greater than 80% of the games at the time were created using C++.  ::)

AAA Games are also written by massive teams with a deep skillset in the language and associated frameworks, which makes C++ realistic to use. Most independent games are written in other languages which are more tenable for single programmers, (even if they are seeing somebody). Otherwise it is doable for simpler game ideas and/or more experienced C++ programmers.

It's also not the only cross-platform language. Even C# can be run on Linux and OSX; I had my Arkanoid Clone running on Linux through Mono, with only a few changes despite never writing it for Linux.

Quote
Regarding the use of the shared text file... yes that is an odd way to slap it together and make it work. Not complex but very different, and not a method that would make any sense if designed to be distributed etc. WoW uses databases for keeping track of everything which is the best method especially with at one point 10 million copies sold and probably half that active users.  :)
WOW uses databases in the back end, but Clients communicate with the Server entirely using sockets, not with shared network drives. For a simple game a Peer-to-peer network connection would probably be best.
I was trying to dereference Null Pointers before it was cool.