Very slow startup using SDL 2 on OS X 10.8 - c++

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);

Related

Is there a way to change color without outputting the ANSI escape sequence in C++?

I am on android, using termux and clang. I am trying to do something like the echo command but with colored output (for fun.)
I have this code, and the normal outputting works, but I'm confused on how to change color without the ANSI escape sequence being outputted with the text?
This is my code:
#define RESET "\033[0m"
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
for (int i = 2; i < argc; ++i) {
cout << argv[1] << argv[i] << " " << RESET;
}
}
You will need to use a library that understands how to write to the terminal.
You could look at ncurses.
Its been a while since I did any of this stuff but if you want to draw (term used loosely) on the terminal this is a simple library that abstracts away particular terminal devices.
It is used by people writing OS installers who don't know what graphics software is available and so must write user interfaces that work in the terminal.
Here is a getting stared guide:
Hello World
ncurses Colored Text
Getting Started Guid

OpenCV imread could not open or find image

I'm doing a project with OpenCV. I am trying to run a simple code:
#include<opencv2/core.hpp>
#include<opencv2/videoio.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/opencv.hpp>
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
cv::Mat img2 = cv::imread("test.jpg", 1);
if (!img2.data) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl;
cv::waitKey(5000);
return -1;
}
else {
cout << "Working" << endl;
cv::waitKey(5000);
}
return 0;
}
My project is a lot more bigger then this, however something simple like this isn't working for me. I have tried many things such as full path names // and \\, even tried an IplImage and convert it into Mat still no luck.
I have tried many different file types as well. If it helps I have coded it to stop the application after hitting any button, that does not work as it isn't taking any input from my keyboard.
How can I determine what is wrong?
I had this exact issue including the part about it only working in release mode. I found out I was using the release libraries for both release and debug. Because I had followed the tutorial on the OpenCV website for visual studio (incorrectly albeit), I changed the .lib folder to the correct d.lib and that fixed it.

SDL not printing to console

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;
}

Unicode Windows console application (WxDev-C++/minGW 4.6.1)

I'm trying to make simple multilingual Windows console app just for educational purposes. I'm using c++ lahguage with WxDev-C++/minGW 4.6.1 and I know this kind of question was asked like million times. I'v searched possibly entire internet and seen probably all forums, but nothing really helps.
Here's the sample working code:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
/* English version of Hello world */
wchar_t EN_helloWorld[] = L"Hello world!";
wcout << EN_helloWorld << endl;
cout << "\nPress the enter key to continue...";
cin.get();
return 0;
}
It works perfectly until I try put in some really wide character like "Ahoj světe!". The roblem is in "ě" which is '011B' in hexadecimal unicode. Compiler gives me this error: "Illegal byte sequence."
Not working code:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
/* Czech version of Hello world */
wchar_t CS_helloWorld[] = L"Ahoj světe!"; /* error: Illegal byte sequence */
wcout << CS_helloWorld << endl;
cout << "\nPress the enter key to continue...";
cin.get();
return 0;
}
I heard about things like #define UNICODE/_UNICODE, -municode or downloading wrappers for older minGW. I tried them but it doesn't work. May be I don't know how to use them properly. Anyway I need some help. In Visual studio it's simple task.
Big thanks for any response.
Apparently, using the standard output streams for UTF-16 does not work in MinGW.
I found that I could either use Windows API, or use UTF-8. See this other answer for code samples.
Here is an answer, not sure this will work for minGW.
Also there are some details specific to minGW here

GLEW functions can't be found

I am on Ubuntu, writing an application using OpenGL, GLEW and GLFW. I am using Eclipse as my IDE, and am having problems when trying to use functions such as glGenRenderbuffer. I have a very similar structure set up under Windows but can't get it to work on Linux.
I am linking to the libraries glfw and GLEW. I am able to compile and run parts of my application and can initialize both GLFW and GLEW. From these test runs I learn the following:
OpenGL version: 4.2.0 NVIDIA 304.64
Using GLEW 1.6.0
However, I can't get the function glGenRenderbuffer (or other framebuffer functions) to compile. I have tried adding both EXT and ARB to the name, as well as playing around with glex.h without any luck. As far as I know, the purpose of GLEW is to handle all extension issues, right?
The source file (VolumeRenderer.cpp) includes are done like this:
#include <glm/gtc/matrix_transform.hpp>
#include <VolumeRenderer.h>
#include <GL/glew.h>
#include <GL/glfw.h>
Further down, I am trying to use the function like this (only partially complete function, of course):
bool VolumeRenderer::InitFramebuffers()
{
glGenRenderbuffers(1, &renderBufferObject_);
return true;
}
All I get is "function 'glGenRenderBuffers' could not be resolved".
Looking in the GL/glew.h header I am including, I found the following lines among others:
#define glGenRenderbuffers GLEW_GET_FUN(__glewGenRenderbuffers)
#define glGenRenderbuffersEXT GLEW_GET_FUN(__glewGenRenderbuffersEXT)
But the compiler still can't find the function. Is there anything I have forgotten, or maybe some tests and checks I can run to see what is happening?
Alright, so I got it to work by basically copying my Eclipse project file by file into a new, empty project. However, I did some other things before that. First, I moved around the includes as suggested in the helpful comments. I also removed things like glm. No success! Then I made a simple project in Eclipse with one single .cpp file, and linked to glfw and GLEW:
#include <GL/glew.h>
#include <GL/glfw.h>
#include <iostream>
#include <cstdlib>
int main()
{
if (glfwInit() != GL_TRUE) {
std::cout << "glfwInit() failed\n";
exit(1);
}
if (!glfwOpenWindow(800, 600, 8 , 8, 8, 8, 8, 8, GLFW_WINDOW)) {
std::cout << "glfwOpenWindow() failed\n";
exit(1);
}
char *glVersion = (char*)glGetString(GL_VERSION);
if (glVersion) {
std::cout << "OpenGL version: " << glVersion << "\n";
}
GLenum err = glewInit();
if (err != GLEW_OK) {
std::cout << "GLEW init failed: " << glewGetErrorString(err) << "\n";
exit(1);
} else {
std::cout << "Using GLEW " << glewGetString(GLEW_VERSION) << "\n";
}
GLuint buf;
glGenRenderbuffers(1, &buf);
exit(0);
}
That worked like a charm. I then tried to recreate my old problem by making another project with a similar file and include structure, and it seemed to work fine too. I then started a third, empty project where I simply made exact copies of the files that were originally giving me trouble. Now I have an exact copy of the original project (that now had the include order changed) with the exact same libraries linked to it, and it works. I still don't know what happened but I suspect that my project in Eclipse got messed up somehow, and never recovered even though I made some changes.