Problems implementing Intel Realsense and SDL2 - c++

as title, I'm having problems trying to make SDL2 display frames from realsesense pipeline. I have no clue how to extract data from the frameset and process it in order to make it works with SDL2. I'm programming in C++. I've read all the documentation on Intel SDK website and tried a lot of different approches, nothing worked so far.
Here's some code I've wrote on a new project:
int main(int argc, char * argv[])
{
window window(1280, 720, "Tecnos Depth Cam Demo");
rs2::colorizer color_map;
rs2::pipeline pipe;
pipe.start();
while (true)
{
rs2::frameset data = pipe.wait_for_frames().apply_filter(color_map);
// Process data to work with SDL2 renderer (my problem)
}
}

At the end I managed to do it. Simply create a texture with SDL_CreateTexture and update it with the pixels data from get_data() function of Realsense library each frame. The tricky part is that you have to define a pixel format with:
SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_PACKED8, SDL_PACKEDORDER_XBGR, SDL_PACKEDLAYOUT_8888, BITS_PER_PIXEL, BYTES_PER_PIXEL)
Here a little code demo that I wrote on porpuse to show you:
#include <iostream>
#define SDL_MAIN_HANDLED
#include <SDL.h>
#include <librealsense2/rs.hpp>
#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 720
#define BITS_PER_PIXEL 24
#define BYTES_PER_PIXEL 3
int main(int argc, char* argv[])
{
// Init
SDL_Init(SDL_INIT_EVERYTHING);
// Declarative section
SDL_Window* window = SDL_CreateWindow("Tutorial", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
SDL_Texture* textureCamera = SDL_CreateTexture(renderer, SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_PACKED8, SDL_PACKEDORDER_XBGR, SDL_PACKEDLAYOUT_8888, BITS_PER_PIXEL, BYTES_PER_PIXEL), SDL_TEXTUREACCESS_TARGET, WINDOW_WIDTH, WINDOW_HEIGHT);
rs2::pipeline camera;
rs2::frameset frames;
// Executive section
camera.start();
// Loop
while (true)
{
// Do whatever you like there
SDL_RenderClear(renderer);
// ...
frames = camera.wait_for_frames();
Uint8* pixels = (Uint8*)(frames.get_color_frame().get_data());
SDL_UpdateTexture(textureCamera, NULL, pixels, frames.get_color_frame().get_bytes_per_pixel() * WINDOW_WIDTH);
SDL_RenderCopy(renderer, textureCamera, NULL, NULL);
SDL_RenderPresent(renderer);
}
return EXIT_SUCCESS;
}
I hope I helped someone in trouble like me. For any question just ask me, I'll be more than happy to help! Lastly thanks to the stackoverflow community to always help even when it doesn't!

Related

game displayed in the bottom-left quarter of my screen in SDL2

I recently updated my mac and the code that was normally running stopped functioning.
When I now run my program (a game) the window still has the right dimensions but the elements are displayed in the bottom-left quarter of my screen.
I don't understand where it comes from.
here is the .cpp that I used in the exemple:
#include "../../include/game.h"
int main(int argc, char **argv)
{
SDL_Window *window;
SDL_Renderer *render;
int w;
int h;
(void)argc;
(void)argv;
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
window = SDL_CreateWindow("lul", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_W, WINDOW_H, 0);
render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_GetWindowSize(window, &w, &h);
SDL_Surface *surface;
SDL_Texture *texture;
surface = IMG_Load("FondHome2.jpg");
texture = SDL_CreateTextureFromSurface(render, surface);
SDL_Event event;
bool close_requested = false;
while (!close_requested)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
close_requested = true;
}
}
SDL_RenderCopy(render, texture, NULL, NULL);
SDL_RenderPresent(render);
}
return(0);
}
and here is the .h used:
#ifndef GAME_H
# define GAME_H
# include <SDL2/SDL.h>
# include <SDL2/SDL_timer.h>
# include <SDL2/SDL_image.h>
# include <SDL2/SDL_ttf.h>
# include <fstream>
# include <iostream>
# include <string>
# include <vector>
# include <stdarg.h>
# include <math.h>
# define WINDOW_W 400
# define WINDOW_H 300
#endif
See content related to High-DPI differences when working with Retina displays on Mac: SDL_GL_GetDrawableSize. Pass the SDL_WINDOW_ALLOW_HIGHDPI flag to SDL_CreateWindow, and the SDL_Render API should do the right thing.
The problem is that without high DPI enabled, the actual width and height of your SDL_Window correspond to more pixels than you passed to SDL_CreateWindow (2X width and height, in this case). It seems like SDL_RenderCopy still thinks the target framebuffer is 400x300, which is correct when the DPI scaling factor is 1. When the image is drawn, only 1/4 of the physical space is covered.
I can't tell you exactly where the scaling issue happens without reading the SDL2 source, since it could be happening with an internal framebuffer or during the final blit, but the issue is the assumption of a DPI scaling factor of 1. Passing the SDL_WINDOW_ALLOW_HIGHDPI flag during window creation tells SDL to query the actual monitor DPI to be used.
Related issue: SDL 2.0 retina mac

main already defined in main.obj

I'm trying to set up an SDL project with Visual Studio 2019 using this article:
https://www.wikihow.com/Set-Up-SDL-with-Visual-Studio
but the compiler is throwing me the errors 'one or more multiply defined symbols found' and
'_main already defined in main.obj'.
main.obj is a file in the debug folder of my project but when I try deleting it or the entire debug folder, VS recreates it when I run the project.
I've read that c++ can't have more than one main function but I can't open the main.obj file and I don't really want to delete the one in main.cpp
Here's the code I'm running and thanks for your help!
#include "SDL.h"
#include <stdio.h>
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow
("An SDL2 window", // window's title
10, 25, // coordinates on the screen, in pixels, of the window's upper left corner
640, 480, // window's length and height in pixels
SDL_WINDOW_OPENGL);
SDL_Delay(3000); // window lasts 3 seconds
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Glad to know it works now. Maybe you had a messy file structure with your previous SDL installation. Anyways, I think it might be interesting to show how the SDL dependency can be removed from your main.cpp file, to avoid such problems in the future.
First, let's consider the example in your question. The example is not very useful in practice, but I'll show a better version after.
The main idea is hiding everything that has to do with SDL from your main.cpp and headers.
1. Simple Example
// MySDL.h - NO SDL STUFF
class MySDL
{
public:
MySDL() = default; // or whatever you need
void runtest() const; // here we'll run the 3sec window
};
Now, we can put all our SDL stuff in the cpp file:
// MySDL.cpp
#include "MySDL.h"
#include "SDL.h" // HERE WE INCLUDE SDL
void MySDL::runtest()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("yee haw", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 400, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_Delay(3000);
}
No SDL included in main.cpp, we just include our SDL interface MySDL.h.
// Now you can use your SDL interface like this
int main(int, char* [])
{
MySDL sdl;
sdl.runtest();
return 0;
}
2. Better Version
However, you would typically want something more sofisticated than a window which disappears in 3 seconds. Therefore, you might want to store class members which depends on SDL. But then, you would have to #include "SDL.h" in your MySDL.h header file, which would give you the same problems as described in your question and comments. To remove this dependency, we can use the pimpl idiom.
The header file now includes a pointer to our SDL implementation. This SDL implementation will be defined in the cpp file in order to remove the SDL dependency.
// MySDL.h
class MySDL
{
public:
MySDL() = default; // or whatever you need
~MySDL();
void doSmthWithYourWindow(/*args*/);
private:
// pointer to our SDLImplementation (defined in cpp file)
class SDLImplementation;
std::unique_ptr<SDLImplementation> _sdl;
};
In our cpp file, we define the SDLImplementation, and MySDL has access to that implementation through the _sdl pointer.
// MySDL.cpp
#include "MySDL.h"
#include "SDL.h"
// here we can store class members which depend on SDL
struct MySDL::SDLImplementation
{
SDLImplementation()
{
SDL_Init(SDL_INIT_EVERYTHING);
_window = SDL_CreateWindow("yee haw", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 400, SDL_WINDOW_SHOWN);
_renderer = SDL_CreateRenderer(_window, -1, 0);
SDL_SetRenderDrawColor(_renderer, 0, 255, 0, 255);
SDL_RenderClear(_renderer);
SDL_RenderPresent(_renderer);
}
// functionality of your SDL implementation
void turnWindowUpsideDown() { /* _window->turnUpsideDown(); */ }
// members depending on SDL
SDL_Window* _window;
SDL_Renderer* _renderer;
};
MySDL::~MySDL() = default;
void MySDL::doSmthWithYourWindow(/*args*/)
{
// here we have access to our SDL implementation
_sdl->turnWindowUpsideDown();
}
Just like before, we only include our MySDL.h interface in the main.cpp file.
int main(int, char* [])
{
MySDL sdl;
sdl.doSmthWithYourWindow();
return 0;
}
So I ended up deleting SDL and completely restarting with a different tutorial linked here:
https://www.youtube.com/watch?v=QQzAHcojEKg
Not really sure what the difference was but it worked. Anyways, thanks for your help and I'll put the new code here.
#include "SDL.h"
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("yee haw", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 400, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_Delay(3000);
return 0;
}

Why is SDL2 rectangle and line drawing not pixel perfect?

When using SDL2 (2.0.8), drawing a 2D rectangle or a line with SDL_RenderDrawRect or SDL_RenderDrawLine is not pixel perfect. There are some artifacts. Why is that? And is there a way to prevent that?
Code example:
#ifdef WIN32
#include "SDL.h"
#else
#include "SDL2/SDL.h"
#endif
int main(int argc, char* args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1000, 800, SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, 0,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_Event event;
bool quit = false;
while (!quit)
{
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_QUIT:
{
quit = true;
break;
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderDrawLine(renderer, 40, 10, 60, 10);
SDL_Rect rect{10, 10, 20, 20};
SDL_RenderDrawRect(renderer, &rect);
SDL_RenderPresent(renderer);
}
return 0;
}
Environment:
Acer Laptop, Intel HD Graphics, NVIDIA GEFORCE 940m, Windows 10
In my Windows 10 display settings "Scale and Layout" was set to "125%". After Setting it to "100%" the rendering was pixel perfect.
Windows 10 display settings
Though this is just a workaraound. The real solution is to call the Windows API function "SetProcessDpiAwareness". I found the answer and code to do that on different Windows versions in this link:
SetProcessDpiAwareness
There are different methods to set the DPI awareness depending on the Windows OS version. It can be set programmatically or by Application Manifest. See the
Microsoft docs.
Setting "Scale and Layout" back to "125%" and calling that function at the begin of my application gives the expected result.
this dude is right. But SetProcessDpiAwareness is undefined in my case.
SetProcessDPIAware() work for me.
#ifdef _WIN32
#include <windows.h>
#endif
int main(int, char**)
{
#ifdef _WIN32
SetProcessDPIAware();// Fix DPI settings
#endif
//some code
}
I hope this code helps.
Take a look at this:
https://wiki.libsdl.org/SDL_HINT_RENDER_SCALE_QUALITY
Make sure to disable this and set it to 0. You may also wish to ensure anti-aliasting is disabled in your video drivers/NVIDIA Control Panel.
There are many upstream factors that may cause this.

Why does SDL program not show BMP picture?

I've got following code from Lazy:
#include <iostream>
#include "SDL/SDL.h"
using namespace std;
int main()
{
//Start SDL
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface *hello = NULL;
SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
hello = SDL_LoadBMP("hello.bmp");
SDL_BlitSurface(hello, NULL, screen, NULL);
SDL_Flip(screen);
SDL_Delay(3000);
SDL_FreeSurface(hello);
//Quit SDL
SDL_Quit();
return 0;
}
From time to time the picture is shown, but most of time it's just a black window (with slim string of this picture). And I've got the BMP file with name "hello.bmp" in the same directory.
PS.I've got ArchLinux.
Before displaying your image you should convert it to a format compatible to the video mode you selected.
Therefore you should implement something like this:
SDL_Surface *imagef;
imagef = SDL_DisplayFormat(image);
Before bliting your BMP and use imagef for all of your operations.

SDL loading my image messed up

I'm attemting to load an image that I exported from flash CS3 it's a very cute face but it loads very weird it loads on a blueish way this is the code for the two files:
//main.cpp
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "test.hpp"
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
// Activamos modo de video
screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE | SDL_DOUBLEBUF);
image = IMG_Load("face.bmp");
dest.x = 200;
dest.y = 200;
//Main Loop
while(Abierto)
{
//We Draw
Draw();
//Events
while( SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
Abierto = false;
}
}
// We free the image
SDL_FreeSurface(image);
SDL_Quit();
return 0;
}
Now the other one the;
//test.hpp
DL_Surface *image = NULL, *screen = NULL;
SDL_Rect dest;
SDL_Event event;
bool Abierto = true;
float PlaneX = 300, PlaneY = 200;
float velX = 0.1, velY = 0.1;
void Draw()
{
Uint32 color;
// Black Background is created
color = SDL_MapRGB (screen -> format, 0, 0, 0);
SDL_FillRect (screen, NULL, color);
SDL_DisplayFormatAlpha(image);
SDL_BlitSurface(image, NULL, screen, &dest);
// Flip the working image buffer with the screen buffer
SDL_Flip (screen);
}
I need help with this please Im not that experienced on SDL stuff oh and if you want to take a closer look I uplaoded the project here.
Oh my bad I must add the image is 32 pixels with alpha according to flash exporting options
According to docs, SDL_DisplayFormatAlpha returns a new image and keeps the original intact.
So, try in the first part, when you load the image:
SDL_Surface *origImage = IMG_Load("face.bmp");
image = SDL_DisplayFormatAlpha(origImage);
SDL_FreeSurface(origImage)
As there is no need to call SDL_DisplayFormatAlpha each frame.
Then in the second part, just blit image, without calling SDL_DisplayFormatAlpha.
UPDATE
I've just checked your picture, and it looks like it is a weird bmp. I've seen that before: BMP format is such a mess that if you don't keep to the basics chances are that different programs will interpret the data differently.
In your case:
display face.bmp shows correctly.
gthumb face.bmp shows nothing.
eog face.bmp says "bogus header data".
I strongly recommend using PNG files for all your game cartoon-like pictures and JPG for all the photo-like ones.
So run
$ convert face.bmp face.png
And use the PNG file. I'll will work better and you will have a file 20% the size of the original.