Jump to content

You're Making your Visual Studio C++ Projects Wrong

Admin

2,160 views

Visual Studio gives two options for creating C++ applications. Console applications use printed text to communicate with the user, harkening back to the pre-GUI days of DOS. The other option is a windowed application with a GUI interface, simply called "Win32 Project" in the Visual Studio project creation dialog.

 

vs.jpg

 

A console application will use the regular old main function you know and love:

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

 

This is cross-platform compatible and runs on any operating system. A "Win32 Project", however, will use a special WinMain() function that only works on Windows:

WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR, int) 

 

So if you don't want a black rectangle printing a bunch of lines of text, you have to use the WinMain() function. But, there is a way to fix this. We can change a console application to a windowed application in the Linker settings in the Visual Studio project settings. Change the Linker > System > Subsystem property to a windowed application.

 

console.jpg

 

There's also a way to force the program to use the cross-platform main() function. Change the setting Linker > Options > All Options > Entry Point to mainCRTStartup.

 

main.jpg

 

Although the application will no longer show a black text box, the printed output is still detectable by a parent application like the Leadwerks script editor.

 

scripteditor.jpg

 

However, if you create a .bat file to run your application, you can only pipe the output to a text file. The text will not be shown when the .bat file runs:

MyGame.exe > output.txt

 

Of course, any Leadwerks game will automatically write it's output to a text log already, so this is sort of redundant. Another consideration is you will not be able to see the output when you run your application from Visual Studio. Strange that VS doesn't support one of the most basic needs of an IDE, and has no known method of viewing the printed output of your program when you run it. However, if you want to get rid of that annoying black popup box, and you want your code to be cross-platform compatible, this is the way to do it.



4 Comments


Recommended Comments

But I like to print debug text through Console. :(

 

Nifty article though. I could put some of the techniques here to use.

 

I'm curious, I use System::Print to print some debug info to console, the text doesn't get logged into appdata. Why? If you use System:Print in Lua, it's logged to appdata log text file.

Share this comment


Link to comment

You can also use this at the top of App.cpp

 

#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")

 

Running it from Visual Studio will still create the window but won't print anything to it. Running straight from the EXE won't create the window at all.

Share this comment


Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Blog Entries

    • By jen in jen's Blog 3
      I thought I would share my experience on this; if you're working on Multiplayer, you will need to protect your packets. The solution is simple, let's go through how we can achieve this by implementing what Valve calls "challenge codes". (Some reading on the topic from Valve here: https://developer.valvesoftware.com/wiki/Master_Server_Query_Protocol#Challenge_response).
      Disclaimer: this doesn't cover other security techniques like authoritative server or encryption.
      So, I've worked on Border Recon last year (I think) and I needed a way to protect my server/client packets. There was no need for me to re-invent the wheel, I just had to copy what Valve has had for a  long time - challenge  codes.
      The idea behind challenge codes is similar to Captcha, but not exactly. Think of it like this: for every packet submitted to the server, it must be verified - how? By requiring the client to solve challenges our server provides.
      To implement this we need to have the following:
      A randomised formula in the server i.e.: a = b * c / d + e or a = b / c + d - e, be creative - it can be any combination of basic arithmetic or some fancy logic you like and can be however long as you want - do consider that the longer the formula, the more work your server has to do to make the computation.  Copy the same formula to the client. A random number generator.  So the idea here is:
      (Server) Generate a random number (see 3 above) of which the result would become the challenge code, (Server) run it through our formula and record the result. (Client) And then, we hand over the challenge code to the client to solve (an authentic client would have the same formula implemented in its program as we have on the server). For every packet received from the player, a new challenge code is created (and the player is notified of this change by the server in response). For every other packet, a new challenge code is created. (Client) Every packet sent to the server by the client must have a challenge code and its answer embedded.  (Server receives the packet) Run the challenge code again to our formula and compare the result to the answer embedded on the client's packet. (Server) If the answers are different, reject the packet, no changes to the player's state. The advantage(s) of this strategy in terms of achieving the protection we need to secure our server:
      - For every packet sent, new challenge code is created. Typically, game clients (especially FPS) will update its state in a matter of ms so even if a cheater is successful at sniffing the answer to a challenge code it would be invalidated almost instantaneously. 
      - Lightweight solution. No encryption needed. 
      Disadvantage(s):
      - The formula to answering the challenge code is embedded to the client, a cheater can de-compile the client and uncover the formula. Luckily, we have other anti-cheat solutions for that; you can implement another anti-cheat solution i.e. checking file checksums to verify the integrity of your game files and more (there are third-party anti cheat solutions out there that you can use to protect your game files).
       
       
       
    • By Josh in Josh's Dev Blog 4
      New commands in Turbo Engine will add better support for multiple monitors. The new Display class lets you iterate through all your monitors:
      for (int n = 0; n < CountDisplays(); ++n) { auto display = GetDisplay(n); Print(display->GetPosition()); //monitor XY coordinates Print(display->GetSize()); //monitor size Print(display->GetScale()); //DPI scaling } The CreateWindow() function now takes a parameter for the monitor to create the window on / relative to.
      auto display = GetDisplay(0); Vec2 scale = display->GetScale(); auto window = CreateWindow(display, "My Game", 0, 0, 1280.0 * scale.x, 720.0 * scale.y, WINDOW_TITLEBAR | WINDOW_RESIZABLE); The WINDOW_CENTER style can be used to center the window on one display.
      You can use GetDisplay(DISPLAY_PRIMARY) to retrieve the main display. This will be the same as GetDisplay(0) on systems with only one monitor.
    • By Josh in Josh's Dev Blog 1
      A huge update is available for Turbo Engine Beta.
      Hardware tessellation adds geometric detail to your models and smooths out sharp corners. The new terrain system is live, with fast performance, displacement, and support for up to 255 material layers. Plugins are now working, with sample code for loading MD3 models and VTF textures. Shader families eliminate the need to specify a lot of different shaders in a material file. Support for multiple monitors and better control of DPI scaling. Notes:
      Terrain currently has cracks between LOD stages, as I have not yet decided how I want to handle this. Tessellation has some "shimmering" effects at some resolutions. Terrain may display a wire grid on parts. Directional lights are supported but cast no shadows. Tested in Nvidia and AMD, did not test on Intel. Subscribers can get the latest beta in the private forum here.

       
       
×
×
  • Create New...