SDL not printing to console - c++

I'm trying to use SDL with Visual Studio 2019 but my programs are only showing an empty console. At the moment I just want to be able to compile my program with the SDL libraries.
#include <iostream>
#include <SDL.h>
int main(int argc, char** argv)
{
std::cout << "yee haw!" << std::endl;
return 0;
}
This code is just giving me a console with the text:
(process 32) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
Where I would want 'yee haw!' preceding it.
It works fine when I take out the #include <SDL.h> (but I want the SDL.h)
I've heard that SDL now redirects to a stdout.txt file but I couldn't find that anywhere. I've also tried displaying a window with code from a tutorial I found, but that also gives me the empty console.
I'm using Visual Studio 2019 on Windows and SDL 2.0.9
Thanks!

By default, SDL uses a macro hack to replace the main function. The user defined main function must be in the following format:
int main(int argc, char** argv)
{
// whatever
return 0;
}
Alternatively, if you don't want this behaviour you can use SDL_SetMainReady.
#define SDL_MAIN_HANDLED
#include <SDL.h>
int main()
{
SDL_SetMainReady();
// whatever
return 0;
}

Related

SDL ignores std::cin

I started learing SDL using visual studio 2015, and I am following a video tutorial.
I already have everything setup.
This tutorial is all about drawing a blank window. After drawing the window the author adds a std::cin to take in an integer so that the window does not quit immediately.
The problem is that I have done the same exact thing but my window quits instantly.
This is my main.cpp:
#include"SDL.h"
#include<iostream>
#include"MainGame.h"
int main(int argc, char *args[]) {
using namespace std;
MainGame maingame;
maingame.run();
cout<<"Enter any key: "
int a;
cin>>a;
return 0;
}
This same thing works in the video. What am I doing wrong here?
It seems like visual studio isn't told to pay attention to the console, so it just throws away std::cin.
Try replacing it with:
SDL_Delay(1000); // do nothing for 1 second

OpenCV: Reading the frames of a video sequence

Anyone help me ,I am trying to run code to read frames from video in folder its success in building but when debugging there isn't any output
* I am using Visual studio 2012 ,opencv 2.4.11 version
the code is :
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
int main()
{
// Open the video file
cv::VideoCapture capture("C:/Users/asus/Desktop/A.mp4");
// check if video successfully opened
if (!capture.isOpened())
return 1;
// Get the frame rate
int rate= capture.get(CV_CAP_PROP_FPS);
bool stop(false);
cv::Mat frame; // current video frame
cv::namedWindow("Extracted Frame");
// Delay between each frame in ms
// corresponds to video frame rate
int delay= 1000/rate;
// for all frames in video
while (!stop) {
// read next frame if any
if (!capture.read(frame))
break;
cv::imshow("Extracted Frame",frame);
// introduce a delay
// or press key to stop
if (cv::waitKey(delay)>=0)
stop= true;
}
// Close the video file.
// Not required since called by destructor
capture.release();
}
Your main() function is never executed. The only thing, that gets executed is _tmain(), which does nothing and returns immediately.
I haven't done much Windows programming in a while, but if I remember correctly this is how it works:
When Unicode is enabled for your compiler
int _tmain(int argc, _TCHAR* argv[])
gets compiled as
int wmain(int argc, wchar * argv[])
which is then used as the program entry point.
Since you seem not to be using any Windows-APIs in your code I would ignore the Microsoft specific way of doing multibyte character strings, which is non-portable, and simply use plain ASCII strings as you did in the main() function, that you intended to use.
So to solve your problem simply throw out the _tmain() function. Maybe you also need to disable Unicode in your project settings if you get linker errors.

Run .exe file of C code using Eclipse

I've make a very simple basic code in C language using Eclipse IDE.
Eclipse details:
Eclipse IDE for C/C++ Developers
Version: Juno Service Release 2
Build id: 20130225-0426
Here is the code:
#include <stdio.h>
int main( int argc, char ** argv ) {
printf("Hello, World!\n");
return 0;
}
Runs successfully in Eclipse, it generates .exe & .o file in Project/Debug directory. I am trying to run that .exe file but it is not working.
Eclipse acts as if it ran the program very quickly and then terminates it. Window appears, but nothing happens when I run the .exe. It just looks like a flash of a dialogue box.
What should be the problem? I don't want to change the IDE and I've already tried these two things:
Eclipse CDT won't run compiled exe files
eclipse won't run my exe file
run the program from the command prompt window. If you simple double click the exe file .. it just runs the program and shuts out in an instant. You won't have any time to see it.
Run it through a cmd window by navigating to the directory and doing ./yourexefile
Or a terrible way to do this with double click is to do this :
#include <stdio.h>
int main( int argc, char ** argv ) {
int n;
printf("Hello, World!\n");
scanf("%d",&n); // this will force the execution window to stay open until you put in some input
return 0;
}
You could also do:
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char ** argv ) {
printf("Hello, World!\n");
system("pause");
return 0;
}
Another way ( more aesthetic ):
#include <stdio.h>
int main( int argc, char ** argv ) {
printf("Hello, World!\n");
printf(" Press enter to exit\n");
getchar();
return 0;
}
Windows is opening a command prompt for your program, running your print statement, and then closing the window (because your function is over).
If you want to see the result of your program, run the command prompt, navigate to the directory of your .exe file, and run it from there.
You should also try this:
Right click on your project name in the Project Explorer view, then go to Run As and click on Local C/C++ Application

Very slow startup using SDL 2 on OS X 10.8

Even using the most basic SDL test, when I run the output file after compiling, I get a pinwheel for about 8 seconds, and then the program starts.
This doesn't happen if I don't use SDL.
I have tried both clang and g++ with the same results.
#include <iostream>
#include <SDL2/SDL.h>
int main(int argc, char **argv){
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Quit();
return 0;
}
Is this normal, or is there a way to fix this? It's really annoying for quickly testing :(
I've found initializing the joysticks tends to take a long time across multiple platforms.
My solution is to initialize just the video to begin with, then later initialize other stuff separately.
SDL_Init(SDL_INIT_VIDEO);
// You can create your window and display a splash screen here
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
SDL_InitSubSystem(SDL_INIT_AUDIO);

C++ CMD Window Output with SDL

Is it possible to get the output of COUT in the cmd window, even though you are using SDL? If so, how would I go about doing so?
Solution #1:
FAQ_Console
Solution #2:
add
#undef main before int main(int argc, char** argv), and don't link to SDLmain (SDLMain == libSDLMain.a or SDLmain.lib).