Add SDL to my path - sdl

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

Related

multiple definition of `main' working with SDL_main

Basically im trying to create a game with SDL2 and Opengl, but I cant get passed this error. I've tried changing SDL_main(int argc, char *argv[]) to regular main but nothing changes. I know this error is due to the SDL_main library but I don't know how to fix it. I've been searching info for some hours now but can't figure it out, the only thing related to this problem I've found is this post, which didn't fix my problem.
Common.h
#ifndef _COMMON_H
#define _COMMON_H
/*=======STANDARD TEMPLATE LIBRARY=======*/
#include <iostream>
#include <string>
#include <cstdlib>
#include <memory>
#include <ctime>
#include <vector>
using namespace std;
/*=======GLU/GLEW=======*/
#include <GL/glew.h>
#include <GL/glu.h>
#include <GL/gl.h>
/*=======SDL2=======*/
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
/*=======CLASS DEFINITIONS=======*/
#endif
Main.cpp
#include "common.h"
bool isRunning = true;
int SDL_main(int argc, char *argv[])
{
//Error Checking/Initialisation
if (SDL_Init(SDL_INIT_NOPARACHUTE & SDL_INIT_EVERYTHING) != 0) {
SDL_Log("Unable to initialize SDL: %s\n", SDL_GetError());
return -1;
}
else
{
//Specify OpenGL Version (4.2)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_Log("SDL Initialised");
}
//Create Window Instance
SDL_Window* window = SDL_CreateWindow(
"Game Engine",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640,
480,
SDL_WINDOW_OPENGL
);
//Check that the window was succesfully created
if(window == NULL)
{
//Print error, if null
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
else
SDL_Log("Window Successful Generated");
//Map OpenGL Context to Window
SDL_GLContext glContext = SDL_GL_CreateContext(window);
//Render
//Swap Render Buffers
SDL_GL_SwapWindow(window);
//Free up resources
SDL_GL_DeleteContext(glContext);
SDL_Quit();
return 0;
}
Error output
./src/lib/SDL2main.lib(x64/Release/SDL_windows_main.obj):(.text[main]+0x0): multiple definition of `main'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x0): first defined here
Makefile
all:
g++ -I ./src/include -L ./src/lib -o ./Builds/Win_Build/engine main.cpp -lmingw32 -lSDL2main -lSDL2 -lmingw32 -lopengl32 -lglew32 -lSDL2main -lSDL2 -lSDL2_image -lSDL2_ttf --verbose
Lib folder
EDIT:
I fixed it by redownloading the libraries and using the .a extension ones. And also I changed the version of Glew to 64 bits. My new lib folder:

failure to initialize SDL audio

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

SDL2 on Raspberry Pi without X?

I'm hoping to develop some code that uses SDL2 to display graphics on the 7" RPi touchscreen, but I'd rather not install a full desktop OS. I've got Raspbian Buster Lite installed. Some simple test code gets an error when I try to run it:
user#rpi4:~/01_hello_SDL $ ./hw
Window could not be created! SDL_Error: Could not initialize EGL
user#rpi4:~/01_hello_SDL $ sudo ./hw
error: XDG_RUNTIME_DIR not set in the environment.
Window could not be created! SDL_Error: Could not initialize EGL
I'm trying to create the window with
SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL )
I found a post that referenced instructions on how to build SDL2 without X, but I was hoping someone could educate me a bit more about how SDL finds the display in various environments, and if it’s even possible to do what I want to do.
A few years ago I used SDL 1.2 to do full-screen graphics on a Beaglebone Black running a version of Debian, but I seem to have lost that installation, and don’t remember how it was set up. I vaguely recall some issues around fbdev and it being non-accelerated graphics, but that didn’t matter at the time (and while I’d like to get accelerated graphics now, it’s not critical).
Example code:
/*This source code copyrighted by Lazy Foo' Productions (2004-2019)
and may not be redistributed without written permission.*/
//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>
//Screen dimension constants
const int SCREEN_WIDTH = 800;
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_FULLSCREEN | SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL );
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 );
}
}
//Destroy window
SDL_DestroyWindow( window );
//Quit SDL subsystems
SDL_Quit();
return 0;
}
Alrighty, got it working on my Raspberry Pi 3 with 2019-07-10-raspbian-buster-lite.img, both with the default Broadcom blobs & the KMS/DRM backend:
Install SDL2 build dependencies:
# install everything Debian uses to build SDL
sudo apt build-dep libsdl2
# needed for the KMSDRM backend:
sudo apt install libdrm-dev libgbm-dev
Grab the latest stable SDL source tarball or tag (release-2.0.10) from Git and extract it somewhere like ~/sdl-src
Run SDL's configure script:
cd ~/sdl-src
./configure --enable-video-kmsdrm
Here's my configure summary:
SDL2 Configure Summary:
Building Shared Libraries
Building Static Libraries
Enabled modules : atomic audio video render events joystick haptic sensor power filesystem threads timers file loadso cpuinfo assembly
Assembly Math :
Audio drivers : disk dummy oss alsa(dynamic) pulse(dynamic) sndio(dynamic)
Video drivers : dummy rpi x11(dynamic) kmsdrm(dynamic) opengl opengl_es1 opengl_es2 vulkan wayland(dynamic)
X11 libraries : xcursor xdbe xinerama xinput2 xinput2_multitouch xrandr xscrnsaver xshape xvidmode
Input drivers : linuxev linuxkd
Using libsamplerate : YES
Using libudev : YES
Using dbus : YES
Using ime : YES
Using ibus : YES
Using fcitx : YES
Note the rpi and kmsdrm(dynamic) entries in the Video drivers list:
Video drivers : dummy rpi x11(dynamic) kmsdrm(dynamic) opengl opengl_es1 opengl_es2 vulkan wayland(dynamic)
^^^ ^^^^^^^^^^^^^^^
Build & install SDL; took ~4.5 minutes on my Rpi3:
make -j4 && sudo make install
Build test program:
g++ main.cpp `pkg-config --cflags --libs sdl2`
(Optional) Enable the "Full KMS" driver if you want to use the KMSDRM backend instead of the default OpenGL ES blobs:
$ sudo raspi-config
select '7 Advanced Options'
select 'A7 GL Driver'
select 'G3 GL (Full KMS)'
reboot
Run test program:
$ ./a.out
Testing video drivers...
The path /dev/dri/ cannot be opened or is not available
The path /dev/dri/ cannot be opened or is not available
SDL_VIDEODRIVER available: x11 wayland KMSDRM RPI dummy
SDL_VIDEODRIVER usable : RPI
The path /dev/dri/ cannot be opened or is not available
The path /dev/dri/ cannot be opened or is not available
SDL_VIDEODRIVER selected : RPI
SDL_RENDER_DRIVER available: opengl opengles2 opengles software
SDL_RENDER_DRIVER selected : opengles2
You can use environment variables to override the default video/render driver selection:
SDL_VIDEODRIVER=KMSDRM SDL_RENDER_DRIVER=software ./a.out
I had to hold SDL's hand a bit with envvars to get the KMSDRM backend to load:
# no envvars, fails:
$ ./a.out
Testing video drivers...
SDL_VIDEODRIVER available: x11 wayland KMSDRM RPI dummy
SDL_VIDEODRIVER usable : KMSDRM
SDL_VIDEODRIVER selected : KMSDRM
SDL_CreateWindow(): Could not initialize OpenGL / GLES library
# with envvars, succeeds:
$ SDL_VIDEO_EGL_DRIVER=libEGL.so SDL_VIDEO_GL_DRIVER=libGLESv2.so ./a.out
Testing video drivers...
SDL_VIDEODRIVER available: x11 wayland KMSDRM RPI dummy
SDL_VIDEODRIVER usable : KMSDRM
SDL_VIDEODRIVER selected : KMSDRM
SDL_RENDER_DRIVER available: opengl opengles2 opengles software
SDL_RENDER_DRIVER selected : opengl
Here's the test program I've been using:
// g++ main.cpp `pkg-config --cflags --libs sdl2`
#include <SDL.h>
#include <iostream>
#include <vector>
int main( int argc, char** argv )
{
SDL_Init( 0 );
std::cout << "Testing video drivers..." << '\n';
std::vector< bool > drivers( SDL_GetNumVideoDrivers() );
for( int i = 0; i < drivers.size(); ++i )
{
drivers[ i ] = ( 0 == SDL_VideoInit( SDL_GetVideoDriver( i ) ) );
SDL_VideoQuit();
}
std::cout << "SDL_VIDEODRIVER available:";
for( int i = 0; i < drivers.size(); ++i )
{
std::cout << " " << SDL_GetVideoDriver( i );
}
std::cout << '\n';
std::cout << "SDL_VIDEODRIVER usable :";
for( int i = 0; i < drivers.size(); ++i )
{
if( !drivers[ i ] ) continue;
std::cout << " " << SDL_GetVideoDriver( i );
}
std::cout << '\n';
if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
{
std::cerr << "SDL_Init(): " << SDL_GetError() << '\n';
return EXIT_FAILURE;
}
std::cout << "SDL_VIDEODRIVER selected : " << SDL_GetCurrentVideoDriver() << '\n';
SDL_Window* window = SDL_CreateWindow
(
"SDL2",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
640, 480,
SDL_WINDOW_SHOWN
);
if( nullptr == window )
{
std::cerr << "SDL_CreateWindow(): " << SDL_GetError() << '\n';
return EXIT_FAILURE;
}
std::cout << "SDL_RENDER_DRIVER available:";
for( int i = 0; i < SDL_GetNumRenderDrivers(); ++i )
{
SDL_RendererInfo info;
SDL_GetRenderDriverInfo( i, &info );
std::cout << " " << info.name;
}
std::cout << '\n';
SDL_Renderer* renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED );
if( nullptr == renderer )
{
std::cerr << "SDL_CreateRenderer(): " << SDL_GetError() << '\n';
return EXIT_FAILURE;
}
SDL_RendererInfo info;
SDL_GetRendererInfo( renderer, &info );
std::cout << "SDL_RENDER_DRIVER selected : " << info.name << '\n';
bool running = true;
unsigned char i = 0;
while( running )
{
SDL_Event ev;
while( SDL_PollEvent( &ev ) )
{
if( ( ev.type == SDL_QUIT ) ||
( ev.type == SDL_KEYDOWN && ev.key.keysym.sym == SDLK_ESCAPE ) )
{
running = false;
}
}
SDL_SetRenderDrawColor( renderer, i, i, i, SDL_ALPHA_OPAQUE );
SDL_RenderClear( renderer );
SDL_RenderPresent( renderer );
i++;
}
SDL_DestroyRenderer( renderer );
SDL_DestroyWindow( window );
SDL_Quit();
return 0;
}

Undefined reference when using glew and mingw?

I am using Eclipse, I had originally downloaded the binary from the website until someone pointed out that I needed to build it from source to make it work with mingw, so I did and I got these files: glew32.dll, libglew32.a, and libglew32.dll.a
I dropped the glew32.dll into the debug folder, and linked the libraries but it did not work.
The weird part: GLenum status = glewInit(); works but glClearColorand glClear do not work and I get a undefined reference to error when I try to call them.
Please see these screenshots: http://imgur.com/a/L8iNb and http://imgur.com/a/nYoWD
C++.cpp
#include <iostream>
#include "classHeaders\display.h"
#include "GL\glew.h"
int main(int argv, char** args){
display x(800,600,"something");
while(!x.isClosed()){
glClearColor(0.0f,0.15f,0.3f,1.0f); //undefined reference to ERROR here
glClear(GL_COLOR_BUFFER_BIT); //undefined reference to ERROR here
x.Update();
}
return 0;
}
display.cpp
#include "classHeaders\display.h"
#include "GL\glew.h"
#include <iostream>
display::display(int width, int height, const std::string& title){
SDL_Init(SDL_INIT_EVERYTHING);
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_ALPHA_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
m_window = SDL_CreateWindow(title.c_str(),SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height, SDL_WINDOW_OPENGL);
m_glContext = SDL_GL_CreateContext(m_window);
GLenum status = glewInit(); //NO ERRORS OCCUR
if(status != GLEW_OK){
std::cerr << "glew failed to initialize" << std::endl;
}
m_isClosed = false;
}
display::~display(){
SDL_GL_DeleteContext(m_glContext);
SDL_DestroyWindow(m_window);
SDL_Quit();
}
bool display::isClosed(){
return m_isClosed;
}
void display::Update(){
SDL_GL_SwapWindow(m_window);
SDL_Event e;
while(SDL_PollEvent(&e)){
if(e.type == SDL_QUIT){
m_isClosed = true;
}
}
}
display.h
#ifndef DISPLAY_H_
#define DISPLAY_H_
#include <string>
#include "SDL2\SDL.h"
#undef main /*need to put this in or else it gives me "undefined reference to WinMain" ERROR*/
class display{
public:
display(int width, int height, const std::string& title);
void Update();
bool isClosed();
virtual ~display();
private:
display(const display& other){}
display& operator=(const display& other){}
SDL_Window* m_window;
SDL_GLContext m_glContext;
bool m_isClosed;
};
#endif /* DISPLAY_H_ */
To set up GLEW, a current OpenGL Context is needed (see Creating an OpenGL Context (WGL) for more information).
Create OpenGL context and window
A OpenGL Context and a window can easily created by SDL, GLFW or GLUT (see Initializing GLEW for more information).
Initilize SDL
If you are using SDL you have to create the window and you have to create the OpenGL context.
SDL_Window *window = SDL_CreateWindow(""OGL window", SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL );
SDL_GLContext glContext = SDL_GL_CreateContext( window );
Note, you should check for errors with SDL_GetError.
The OpenGL context has to become the current context before you use it. Use SDL_GL_MakeCurrent therefor.
SDL_GL_MakeCurrent( window, glContext );
Initilize GLUT
To set up GLUT you have to use glutInit and can follow the instructions of initializing glew.
glutInit(&argc, argv);
glutCreateWindow("OGL window");
Initilize GLFW
Note, glfwInit returns GLFW_TRUE if succeded:
if ( glfwInit() != GLFW_TRUE )
return;
GLFWwindow *wnd = glfwCreateWindow( width, height, "OGL window", nullptr, nullptr );
if ( wnd == nullptr )
{
glfwTerminate();
return;
}
glfwMakeContextCurrent( wnd );
After you have created an OpenGL Context and you have made it become the current context, you have to initialize glew.
Set up GLEW
Note that glewInit returns GLEW_OK if succeded:
if ( glewInit() != GLEW_OK )
return;
To link the GLEW library correctly you have to set up proper preprocessor definitions:
On Windows, you also need to define the GLEW_STATIC preprocessor token when building a static library or executable, and the GLEW_BUILD preprocessor token when building a dll
See also the answer to GLEW Linker Errors (undefined reference to `__glewBindVertexArray')
So basically to solve this problem you want to download the source from the glew website and compiler it yourself. You use the command prompt to get in the directory of the folder you downloaded and execute these commands line by line in order:
gcc -DGLEW_NO_GLU -O2 -Wall -W -Iinclude -DGLEW_BUILD -o src/glew.o -c src/glew.c
gcc -nostdlib -shared -Wl,-soname,libglew32.dll -Wl,--out-implib,lib/libglew32.dll.a -o lib/glew32.dll src/glew.o -L/mingw/lib -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32
and finnally:
gcc-ar cr lib/libglew32.a src/glew.o (though the "gcc-" may not be needed, it was for me)
Once you're done with that left click on your project and go to Properties, then under C/C++ Build go to settings, then under MinGW C++ Linker click in Libraries. Once you're there make sure your Library search path is correct (the place where Eclipse looks for your libraries) then in Libraries enter these one by one: glew32 opengl32 glu32 glew32.dll SDL2 SDL2main SDL2_test
Also when you compiled from source there should be a glew32 with a .dll not a .a extension in your lib folder inside the glew folder you downloaded from the website, drop that in into your debug (where your .exe is created). Do the same for the .dllnot the .dll.a for SDL and also make sure you have your include folders for both glew and SDL set up under the GCC C++ Compiler (also under your settings for the C/C++ Builder). It should work now.

Compile and Run SDL On Terminal

Ok, so I compiled SDL/C++ on terminal easily by just doing:
g++ main.cpp -o main.out -pthread -std=c++11 `sdl2-config --cflags --libs` .
And then this is my main.cpp:
// #include <SDL/SDL.h>
#include "SDL2.0.4/SDL2-2.0.4/include/SDL.h"
int main(int argc, char** argv)
{
SDL_Window *__window;
//Init SDL
SDL_Init(SDL_INIT_EVERYTHING);
//Creation Window
__window = SDL_CreateWindow(
"Engine",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640,
430,
SDL_SWSURFACE
);
while(true){
}
return 0;
}
Well when I do ./main.out to run it, it just stops iterating over while loop. There is no window popping up. I am using Ubuntu Linux. And as I said I want to run and compile sdl on terminal, I'm pretty sure I passed the compiling stage, it's the running that's irritating me.