When i Call SDL_Init passing in SDL_AUDIO_INIT, the function returns a failure.
I called SDL_GetError() but there does not seem to be any message.
I am not having problems getting VIDEO to initialize, only audio. I don't get any errors when compiling. I have linked the SDL mixer library in my makefile.
Here is a minimum reproducible example code:
#include <SDL2/SDL.h>
#include <stdio.h>
#include <string>
int main( int argc, char* args[] )
{
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO < 0))
{
printf( "SDL VIDEO could not initialize! SDL Error: %s\n", SDL_GetError() );
}
else if ( SDL_Init( SDL_INIT_AUDIO) < 0)
{
printf( "SDL AUDIO could not initialize! SDL Error: %s\n", SDL_GetError() );
}
else
{
printf("video and audio initialized");
}
return 0;
}
output: SDL AUDIO could not initialize! SDL Error:
(there is no message for the error)
Here is the makefile
#OBJS specifies which files to compile as part of the project
OBJS = 21_sound_effects_and_music.cpp
#CC specifies which compiler we're using
CC = g++
#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
COMPILER_FLAGS = -g -Wall
#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer
#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = sdl_program
#This is the target that compiles our executable
all : $(OBJS)
$(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
I am using Ubuntu 20.04
install libasound2-dev libpulse-dev
and recompyling SDL2
Audio drivers : disk dummy oss alsa(dynamic) pulse(dynamic) <--- IS OK
Related
I am attempting to set up SDL2 for creating a C++ game for windows. When I run the below make file with just a single source file, I get no errors, and the program can run. When I attempt to run the make file with multiple source files, the compilation goes smoothly, but when I attempt to run the program, i get the error seen in the title. It appears to only happen when I attempt to create an instance of a class in the second source file, and try to access either members or instance functions.
I have tried redownloading and setting up my environment, updating mingw (didnt hurt to try) and triple checking my code. I have included the makefile, as well as the source files I am using. Thank you for any help.
EDIT: It seems that the delete call in the main function causes this error... Not sure if that is a coincidence or if the cause does lie somewhere there...
#OBJS specifies which files to compile as part of the project
OBJS = src/*.cpp src/*.hpp
#CC specifies which compiler we're using
CC = g++
#INCLUDE_PATHS specifies the additional include paths we'll need
INCLUDE_PATHS = -Iinclude/SDL2
#LIBRARY_PATHS specifies the additional library paths we'll need
LIBRARY_PATHS = -Llib
#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
# -Wl,-subsystem,windows gets rid of the console window
COMPILER_FLAGS =
#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lmingw32 -lSDL2main -lSDL2
#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = demoGame
#This is the target that compiles our executable
all : $(OBJS)
$(CC) $(OBJS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
#include <SDL.h>
#include <stdio.h>
#include "grid.hpp"
#include <iostream>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main( int argc, char* args[] )
{
//The window we'll be rendering to
SDL_Window* window = NULL;
//The surface contained by the window
SDL_Surface* screenSurface = NULL;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//Create window
window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( window == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//Get window surface
screenSurface = SDL_GetWindowSurface( window );
//Fill the surface white
SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) );
//Update the surface
SDL_UpdateWindowSurface( window );
//Wait two seconds
SDL_Delay( 2000 );
}
}
Grid* grid = new Grid(1, 2);
// These two lines seem to cause an issue.
std::cout << grid->getRows() << std::endl;
delete grid;
//Destroy window
SDL_DestroyWindow( window );
//Quit SDL subsystems
SDL_Quit();
return 0;
}
class Grid {
public:
Grid(int rows, int columns) {
this->rows = rows;
this->columns = columns;
}
int getRows() {
return this->rows;
}
private:
int rows;
int columns;
};
I'm trying to link to SDL2 statically on Arch Linux in order to produce single, cross-platform executable for a game. SDL2 headers are installed in /usr/include/SDL2, and libraries in /usr/lib. Compiling dynamically with g++ -I/usr/include/SDL2 -lSDL2 hello.cpp works, as ./a.out produces a blank window, but I am unable to link SDL2 to the executable statically.
Contents of hello.cpp:
#include <iostream>
#include "SDL.h"
int main() {
bool quit{false};
SDL_Event inputEvent;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("Hello", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 1280, 0);
while (!quit) {
SDL_WaitEvent(&inputEvent);
switch (inputEvent.type) {
case SDL_QUIT:
quit = true;
break;
}
}
SDL_Quit();
std::cout << "Bye!";
return 0;
}
g++ -I/usr/include/SDL2 -static -lSDL2 hello.cpp returns /usr/bin/ld: cannot find -lSDL2, while g++ -I/usr/include/SDL2 -l:libSDL2 hello.cpp returns /usr/bin/ld: cannot find -l:libSDL2.
It turns out that PKGBUILD of sdl2-hg in the AUR was renaming libSDL2.a to libSDL2main.a, hence the libSDL2.a not found error. Removing the problematic line from the PKGBUILD and recompiling SDL2 alleviated the issue.
I'm trying to compile this code:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
GLFWwindow* window;
#include <glm/glm.hpp>
using namespace glm;
int main( void )
{
// Initialise GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_RESIZABLE,GL_FALSE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window and create its OpenGL context
window = glfwCreateWindow( 1024, 768, "Playground", NULL, NULL);
if( window == NULL ){
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Initialize GLEW
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
// Dark blue background
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
do{
// Draw nothing, see you in tutorial 2 !
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
// Close OpenGL window and terminate GLFW
glfwTerminate();
return 0;
}
It's a code from a tutorial I found about OpenGL, I program mostly in Java when it comes to OpenGL, but I wanted to try something new so I went to try in C++.
I'm using QtCreator for this project.
At first I included GLEW and glfw3 libraries:
And the same for the glfw library file.
And then, when I try compiling the program I get this error:
In text:
$ /home/sapir/Qt/5.4/gcc_64/bin/qmake -spec linux-g++ CONFIG+=debug -o Makefile ../Test/Test.pro
$ g++ -Wl,-rpath,/home/sapir/Qt/5.4/gcc_64 -o Test main.o -L/home/sapir/Dropbox/Development/Computer/openGLTest/Test/../../../../../../../usr/local/lib/ -lglfw3 -lGLEW -lGLEWmx
/usr/bin/ld: /home/sapir/Dropbox/Development/Computer/openGLTest/Test/../../../../../../../usr/local/lib//libglfw3.a(glx_context.c.o): undefined reference to symbol 'glXQueryExtension'
//usr/lib/x86_64-linux-gnu/mesa/libGL.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make: *** [Test] Error 1
23:12:13: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project Test (kit: Desktop Qt 5.4.0 GCC 64bit)
When executing step "Make"
23:12:13: Elapsed time: 00:00.
I tired searching for an answer in forums and here, but I couldn't find anything that solved this problem.
Anybody got any Ideas?
After adding
-lXxf86vm -lXrandr -lGL -lGLU -lXi
to the gcc compiler, I get a different error, which contains:
/usr/bin/ld: /home/sapir/Dropbox/Development/Computer/openGLTest/Test/../../../../../../../usr/local/lib//libglfw3.a(x11_window.c.o): undefined reference to symbol 'XConvertSelection'
This is my make file: http://pastebin.com/xL5Hpwsf
And this is my .pro file: http://pastebin.com/yhkV7nn7
Ok, after some research I found that the DSO error which I got means that the order of the includes I've implemented is incorrect and cause the compilation to fail.
So what I did is I used the command:
pkg-config --static --libs x11 xrandr xi xxf86vm glew glfw3
To get the packages I need for them to run and in the right order.
Then I compiled the project accordingly.
That's it :)
I got same error "undefined reference to symbol 'XConvertSelection'" while compiling example from Irrlicht 3D, solved by adding "-lX11".
Then I got error "undefined reference to symbol 'XF86VidModeGetGamma'", solved by adding "-lXxf86vm"
The program following, is one that creates a window which does nothing except close when you press esc. When I compile it with cygwin, there are no errors. The GLEW I use is from Cygwin Ports, and the SDL2 is version 2.0.3, from their website's SDL2-devel-2.0.3-mingw.tar.gz download. I have SDL2.dll in the directory of the compiled executable.
Links with: -lSDL2 -lSDL2main -lGLEW -lGLU -lGL -lSDL2 -lSDL2main -lGLEW -lGLU -lGL, twice to ensure everything is linked.
Also compiled with: -std=c++11
On my computer, the following program prints out:
OpenGL Vendor: (null)
OpenGL Renderer: (null)
OpenGL Shading Language Version: (null)
OpenGL Extensions: (null)
Error initializing GLEW! Missing GL version
The program appears to work otherwise. The main problem is that if I try to call, for example glGenVertexArrays, the program will crash with STATUS_ACCESS_VIOLATION. (See the crashing code here. I think this has something to do with GLEW's error Missing GL version.
#include <cstdio>
#include <chrono>
#include <thread>
#include <SDL2/SDL.h>
#include <GL/glew.h>
#include <SDL2/SDL_opengl.h>
#include <GL/glu.h>
const int width = 1000;
const int height = 500;
bool Running = true;
#undef main
int main (int argc, char *argv[]) {
FILE* cdebug = fopen("cdebug.txt", "w");
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(cdebug, "SDL could not initialize! SDL Error: %s\n", SDL_GetError()); fflush(cdebug);
}
#define setAttr(attr, value) \
if (SDL_GL_SetAttribute(attr, value) < 0) { \
fprintf(cdebug, "SDL failed to set %s to %s, SDL Error: %s\n", #attr, #value, SDL_GetError()); fflush(cdebug);\
}
setAttr(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
setAttr(SDL_GL_CONTEXT_MINOR_VERSION, 3);
setAttr(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
setAttr(SDL_GL_RED_SIZE, 8);
setAttr(SDL_GL_GREEN_SIZE, 8);
setAttr(SDL_GL_BLUE_SIZE, 8);
setAttr(SDL_GL_DEPTH_SIZE, 24);
setAttr(SDL_GL_DOUBLEBUFFER, 1);
#undef setAttr
/*
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
*/
SDL_Window *window = SDL_CreateWindow(
"test",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
640, 480,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
);
if (window == NULL) {
fprintf(cdebug, "Window could not be created! SDL Error: %s\n", SDL_GetError()); fflush(cdebug);
}
SDL_GLContext GLContext = SDL_GL_CreateContext(window);
if (GLContext == NULL) {
fprintf(cdebug, "OpenGL context could not be created! SDL Error: %s\n", SDL_GetError()); fflush(cdebug);
}
if (SDL_GL_MakeCurrent(window, GLContext) < 0) {
fprintf(cdebug, "OpenGL context could not be made current! SDL Error: %s\n", SDL_GetError()); fflush(cdebug);
}
fprintf(cdebug, "OpenGL Vendor: %s\n", glGetString(GL_VENDOR));
fprintf(cdebug, "OpenGL Renderer: %s\n", glGetString(GL_RENDERER));
fprintf(cdebug, "OpenGL Shading Language Version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
fprintf(cdebug, "OpenGL Extensions: %s\n", glGetString(GL_EXTENSIONS));
fflush(cdebug);
glewExperimental = GL_TRUE;
{
GLenum glewError = glewInit();
if (glewError != GLEW_OK) {
fprintf(cdebug, "Error initializing GLEW! %s\n", glewGetErrorString(glewError)); fflush(cdebug);
}
}
SDL_Event event;
while (Running) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYUP: {
switch (event.key.keysym.scancode) {
case SDL_SCANCODE_ESCAPE:
Running = false;
break;
}
break;
}
}
}
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(window);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
SDL_GL_DeleteContext(GLContext);
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
return 0;
}
You are mixing cygwin and mingw in ways in which you shouldn't.
If you use cygwin's toolchain and -lGL and so on, you link against cygwin's OpenGL - which is not the native OpenGL lib on windows, but the one provided by cygwin's X server, implementing the GLX protocol.
The mingw version of SDL will use the native GL lib (opengl32.dll) on windows, using the wgl API. So SDL might even create a context for you, but the GL functions your programm are calling belong to a completely different GL implementation - for which your program never created a GL context.
The solution is to stick with one or the other: Completely use cygwin, and a cygwin version of SDL, and a cygwin X server. However, that is not the path I would recommend. I don't know if that would even get you some HW acceleration at all.
The more useful solution would be to not use cygwin, but mingw, for the whole project, with a mingw version of GLEW. That will result in a completely native windows binrary which will use the native OpenGL library with all features provided by the driver and not require cygwin's dlls and especially not cygwin's X server.
I managed to get things working in a weird way.
I am using a self compiled version of SDL2 but the SDL2-devel-2.0.3-mingw.tar.gz provided by the SDL website seems to work as well and using a combination of them (such as mingw version's libs and self-compiled .dll) seem to work as well.
For GLEW, I am using my own compiled version. To compile this, I used their website's source glew-1.11.0.zip and extracted this. Then I edited glew-1.11.0/Makefile and edited line 24 to SYSTEM = cygming. Then in glew-1.11.0/config/Makefile.cygming on line's 7 and 8, I removed the -mno-cygwin flag (so the line's are CC := gcc and LD := gcc) and added -D_WIN32 to line 10 (so the line becomes CFLAGS.SO = -DGLEW_BUILD -D_WIN32). Then in glew-1.11.0, I ran make all and let it compile. After that, I copied glew-1.11.0/include/GL to my includes directory. Next, I copied glew-1.11.0/lib/libglew32.dll.a to my libs folder. I also copied glew-1.11.0/lib/glew32.dll to my .exe's folder. Then to get it to not produce a linker error, I had to place a #define _WIN32 before my #include <GL/glew.h>.
To link everything, I managed to compile it with a minimum of -lSDL2 -lSDL2main -lglew32.dll -lopengl32.
I install SDL via brew on my mac but I can't include it!
Here is my too easy code:
#include <SDL.h>
int main(){
return 0;
}
when I compile it with cc, CC could not find SDL.h
I found that brew install SDL in Cellar but cc did not check this folder
Could you help me?
I know this post has 9 months old but if someone, somewhere in the internet try to find out how to use SDL with mac, just follow this.
DL .dmg file on the SDL website (V2).
Put SDL2.framework in /Library/Frameworks
In your code :
#include <SDL.h>
and compile with those flags :
`sdl-config --cflags --libs`
Ex :
gcc test.c `sdl-config --cflags --libs`
Use this simple code to see it working :
#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
int main( int argc, char *argv[ ] )
{
SDL_Surface *screen;
if( SDL_Init( SDL_INIT_VIDEO ) == -1 )
{
printf( "Can't init SDL: %s\n", SDL_GetError( ) );
return EXIT_FAILURE;
}
atexit( SDL_Quit );
screen = SDL_SetVideoMode( 640, 480, 16, SDL_HWSURFACE );
if( screen == NULL )
{
printf( "Can't set video mode: %s\n", SDL_GetError( ) );
return EXIT_FAILURE;
}
SDL_Delay( 3000 );
return EXIT_SUCCESS;
}