How to use SDL with OGRE? - c++

When I go to use OGRE with SDL (as described in this article), I seem to be having trouble with a second window that appears behind my main render window. Basically, the code I'm using is this:
SDL_init(SDL_INIT_VIDEO);
SDL_Surface *screen = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
Ogre::Root *root = new Ogre::Root();
root->restoreConfig();
root->initialise(false);
Ogre::NameValuePairList windowSettings;
windowSettings["currentGLContext"] = Ogre::String("True");
Ogre::RenderWindow *window = root->createRenderWindow("MainRenderWindow", 640, 480, false, &windowSettings);
window->setVisible(true);
The question is, how do I get rid of the extra window?
Just for posterity, I'm using OGRE 1.6.4, Mac OS X 10.6.2, and SDL 1.2.14.

I ended up figuring this out on my own. The problem ends up being that OGRE's Mac GL backend does not honor the currentGLContext option, so the best solution is to change to SDL 1.3 (directly from Subversion, as of time of writing) and use the SDL_CreateWindowFrom call to start getting events from a window created by OGRE. It should also be noted that the OGRE window needs to have the macAPI set to cocoa, or else SDL won't recognize the window handle.

I see that you already solved your problem, but not all users will be content with downgrading SDL to 1.3. You can use SDL2 and an SDL2 window created via SDL_CreateWindow with OGRE. The code would look something like this:
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
OGRE_EXCEPT(Ogre::Exception::ERR_INTERNAL_ERROR, "Cannot initialize SDL2!",
"BaseApplication::setup");
}
Ogre::Root *root = new Ogre::Root();
root->restoreConfig();
root->initialise(false);
Ogre::NameValuePairList params; // ogre window / render system params
SDL_Window *sdlWindow = SDL_CreateWindow("myWindow", posX, posY, width, height, vflags);
// see SDL_CreateWindow docs / examples for how to populate posX, posY, width, height, and vflags according to your needs
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
if (SDL_GetWindowWMInfo(sdlWindow, &wmInfo) == SDL_FALSE) {
OGRE_EXCEPT(Ogre::Exception::ERR_INTERNAL_ERROR,
"Couldn't get WM Info! (SDL2)",
"BaseApplication::setup");
}
params.insert(std::make_pair("macAPI", "cocoa"));
params.insert(std::make_pair("macAPICocoaUseNSView", "true"));
// grab a string representing the NSWindow pointer
Ogre::String winHandle = Ogre::StringConverter::toString((unsigned long)wmInfo.info.cocoa.window);
// assign the NSWindow pointer to the parentWindowHandle parameter
params.insert(std::make_pair("parentWindowHandle", winHandle));
Ogre::RenderWindow *ogreWindow = root->createRenderWindow("myWindowTitle", width, height, isFullscreen, &params);
// see OGRE documentation on how to populate width, height, and isFullscreen to suit your needs
// create OGRE scene manager, camera, viewports, etc

Related

Having trouble initializing bgfx (with an sdl2 window)

I'm attempting to create a window using SDL2 and initialize BGFX to use it. My current test is just to set the window to purple using a clear color.
I tried creating a window using CreateWindowEx as well, and was also unable to update the window with the clear color I specified in my call to bgfx::setViewClear. I've been scouring open source projects as well as the docs and samples, and I can't figure out what step of bgfx initialization/update I could be missing. Please help! I've attached my current approach as a small test main.
int main(int, char**) {
SDL_InitSubSystem(SDL_INIT_VIDEO);
const int width = 800;
const int height = 600;
SDL_Window* window = nullptr;
HWND nativeWindow;
// sdl2
{
window = SDL_CreateWindow(
"test_window",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
width, height,
0
);
SDL_SysWMinfo windowManInfo;
SDL_VERSION(&windowManInfo.version);
if (SDL_GetWindowWMInfo(window, &windowManInfo)) {
nativeWindow = windowManInfo.info.win.window;
}
}
// bgfx
{
bgfx::PlatformData platformData;
platformData.ndt = nullptr;
platformData.nwh = nativeWindow;
bgfx::setPlatformData(platformData);
// prevent creation of a renderer thread
bgfx::renderFrame();
bgfx::Init init;
init.type = bgfx::RendererType::Count;
init.resolution.width = width;
init.resolution.height = height;
init.resolution.reset = BGFX_RESET_VSYNC;
bgfx::init(init);
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x443355FF /*purple*/, 1.f, 0);
}
while (1) {
// sdl events
{
SDL_Event _event;
while (SDL_PollEvent(&_event) > 0);
}
bgfx::frame();
}
bgfx::shutdown();
SDL_Quit();
return 0;
}
After asking around work/etc.. I finally got a solution, and there were actually a couple of things that I was missing.
Because I wasn't adding any render work to the frame, bgfx is 'smart' and doesn't actually do anything. Adding a call to bgfx::touch will add an empty primitve for rendering. After I added this I could see a small dot in the top-left of my window, which leads to the other call I was missing.
I never set my view! I was also only rendering to one pixel of my window. By adding a call to bgfx::setViewRect I was able to set a size for my window view and the clear color finally took.

glfwCreateWindow does not return null, yet new window does not show up

I'm trying to use ofxProjectorKinectV2Calibration to calibrate my Kinect v2 and projector. It's an add-on to openFrameworks, and the set-up is relatively complicated.
Anyway, ofxProjectorKinectV2Calibration uses another add-on, ofxSecondWindow, to create a second window to display a chessboard. My problem is I do not see this second window at all. It's not even shown on the taskbar.
Here is the code from ofxSecondWindow to create the second window:
void ofxSecondWindow::setup(const char *name, int xpos, int ypos, int width, int height, bool undecorated) {
this->width = width;
this->height = height;
glfwWindowHint(GLFW_DECORATED, !undecorated);
mainWindow = glfwGetCurrentContext();
auxWindow = glfwCreateWindow(width, height, name, NULL, mainWindow);
glfwSetWindowPos(auxWindow, xpos, ypos);
/* enable alpha blending by default */
glfwMakeContextCurrent(auxWindow);
glEnable(GL_BLEND);
#ifndef TARGET_OPENGLES
glBlendEquation(GL_FUNC_ADD);
#endif
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glfwMakeContextCurrent(mainWindow);
hidden = false;
}
I am sure that width and height are correct, turning undecorated on or off does not change anything, and glfwCreateWindow does return some handle that is not null.
Environment: Windows 10 64-bit, Visual Studio 2015 32-bit build target, projector (1024x768) is display 1 and PC screen is display 2. openFrameworks version 0.9.3, add-ons:
ofxOpenCv
ofxXmlSettings
ofxCv
ofxKinect2ProjectorCalibration
ofxKinectV2
ofxSecondWindow
ofxUI
Ideas?
Turns out I had to call show(), or in implementation, glfwShowWindow, on the newly created window.

dyld: Library not loaded...reason image not found?

I'm new to c++ and XCode, I'm using sdl2 to create a window but when i compile it, it crashes giving me a thread.I have included opengl.h , stdio.h and SDL2.h. There are questions about
dlyd:library not loaded but their different.
Error Message:
dyld: Library not loaded: #rpath/SDL2.framework/Versions/A/SDL2 Referenced from:
/Users/shayanrazavi/Library/Developer/Xcode/DerivedData/c++_code-bbdyozxqxxdxosbxuyhcrqobxrkd/Build/Products/Debug/c++
code
Reason: image not found
This is the code I used i couldn't get int main to be inside the code block for some reason but anyway and I got this code from https://wiki.libsdl.org/SDL_CreateWindow.
int main(int argc, char* argv[]) {
SDL_Window *window; // Declare a pointer
SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2
// Create an application window with the following settings:
window = SDL_CreateWindow(
"An SDL2 window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);
// Check that the window was successfully made
if (window == NULL) {
// In the event that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
// The window is open: enter program loop (see SDL_PollEvent)
SDL_Delay(3000); // Pause execution for 3000 milliseconds, for example
// Close and destroy the window
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
return 0;
}
I figured out why this was happening I was meant to put the framework in /Library/Frameworks folder before using it in XCode because when you download SDL it gives you a read me file and the read me file says to put it in that folder.
I should try reading all the text in read me files next time I guess. But if I try running this in XCode it will crash for some reason. (Makes sense because it says dyld: Library not loaded and we just put it in /Library/Frameworks)

cannot display texture in SDL 2

I'm trying to display a texture onto the screen but all I'm getting is a black window.
No SDL Errors are being reported. There's a good chance that I'm missing something stupid, but I can't see it. Hopefully another set of eyes will help. Feel free to ask for more code/info.
main.cpp
SDL_Window * window;
SDL_Renderer * renderer;
SDL_Texture * grass;
SDL_Rect g_dst;
SDL_Event event;
Game app;
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow("tmp", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
g_dst.x = g_dst.y = 0;
g_dst.w = 640;
g_dst.h = 480;
grass = IMG_LoadTexture(renderer, "grass.bmp");
while (app.isRunning()) {
app.pollEvents(&event);
app.render_init();
app.render(grass, NULL, &g_dst);
app.render_end();
}
//SDL_Quit() is handled by the Game class' destructor
Game.cpp
//Only functions used for rendering are shown
void render_init(Uint8 red=0, Uint8 green=0, Uint8 blue=0, Uint8 alpha=255)
{
SDL_SetRenderDrawColor(renderer, red, green, blue, alpha);
SDL_RenderClear(renderer);
}
void render(SDL_Texture * texture, SDL_Rect * src, SDL_Rect * dest) {
SDL_RenderCopy(renderer, texture, src, dest);
}
void render_end() { SDL_RenderPresent(renderer); }
First of all, you're initializing everything? please don't do that frequently, mind you that you're also initializing MANY unnecessary stuffs like for game controllers, etc. if the app gets bigger then the efficiency and the possibility of this app running at a smoot speed is at stake.
I also noticed that you are declaring variables in the .cpp file, do that in the header file and just recall the header to the cpp file that will be using it.
You want to render the grass right? and render it as much as the screens size.
(I'll just assume that you used this in the game.cpp part, which is the very first file, thus, not regarding any classes made)
int winWidth = 680; //The reason for this is just in case you make the window resizable
int winHeight = 480; //then the texture would also resize along with the window
SDL_Window *window = window = SDL_CreateWindow("The Space Project", 100, 100, winWidth, winHeight, SDL_WINDOW_SHOWN);
SDL_Renderer *renderer = NULL; //I've set this to NULL so that we can know if
the reason as to why your image is not rendering is because the renderer is not properly working.
renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED);
if(renderer == NULL)
{
cout >> "Renderer is not working" >> endl;
//This shows a line at the command prompt that your renderer doesn't have any output, thus, only having a NULL as an equivalent
}
SDL_Texture* grass= NULL;
grass= IMG_LoadTexture(renderer, "grass.bmp"); //As you can see, I've set the grass to Null again
if(grass == NULL)
{
cout >> "Grass have failed to initialize" >> endl;
/*I don't normally do this but it's very important if you really need trouble shooting guides
but this time, were here to check IF the grass.bmp entered the SDL_Texture grass, so if the system can't find the .bmp file then it would show this error
since the grass (SDL_Texture) still doesn't have anything inside it (NULL)*/
}
SDL_Rect grass_rect;
grass_rect.x = 0;
grass_rect.y = 0;
grass_rect.w = winWidth;
grass_rect.h = winHeight;
//Loop part, I'll skip some of it
while (!quit && mainEvent->type != SDL_QUIT) //!quit is just an imaginary Boolean I've typed)
{
SDL_PollEvent(mainEvent); //Let's say you created the event already
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, grass, NULL, &grass_rect);
//The NULL part is also similar to a rect but it's a limiting type, we didn't assign anything to it
since I assumed that you wanted the whole image to be rendered
SDL_RenderPresent(renderer);
}
I revised the code and made it more efficient since your code called for useless extras which might result in lower performance.
I also notice that you tried calling for color changes?
use this
SDL_SetTextureColorMod(texture, red-value, green-value, blue-value);
and put it in the loops part under the render present of the same texture.
SDL_SetTextureColorMod(grass, 250, 250, 250);
Doing this would set all color values to 250, thus, having a white color, this change your texture color to white.
You're also wasting space on making the app.is running(), you could easily replace it with a boolean, which consumes much less space or you could omit it if you don't have an exit button inside the application and just make your loop read the SDL_QUIT, this saves space for the file, mind the efficiency.
If this still doesn't work then try replacing the image your using, make a simple one on paint name it something like "grass.png" or anything then try it again.
Don't forget to put the file in the proper folder, in the DEBUG folder if you haven't specified a folder, and also put it in the app folder so it would also read it when it executes as an .exe file and not as part of the debug command.

How to draw pixels in SDL 2.0?

How does one draw with pixels in SDL2.0?
I'm trying to get familiar with C++, but this is very difficult to do without pretty pictures, so I'm trying to get a very basic graphics display thing running. All I really want it to do is give me a window, let me draw rgbα pixels on it, and access information about those pixels. There may be other things I want that I'm not aware of, but that's all that's on my list right now. My research on this has lead me to try using SDL, the current version being 2.0.
Almost all my graphics experience comes from using JavaScript on a <canvas>. Most of the other bit comes from my calculator, which has this really awesome Pxl-On() command, so easy.
I'm using MinGW for my C++, if it matters. Also, if there's something better** out there than SDL2.0 for what I need, advice welcome.
** "better" means "contains what functionality I need, but less total functionality than SDL2.0, and/or has a more intuitive/less complex*** API than SDL2.0."
*** Less lines of code to accomplish the same task.
Runnable example
Draws a diagonal red line pixel by pixel on the screen using SDL_RenderDrawPoint.
main.c
#include <stdlib.h>
#include <SDL2/SDL.h>
#define WINDOW_WIDTH 600
int main(void) {
SDL_Event event;
SDL_Renderer *renderer;
SDL_Window *window;
int i;
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_WIDTH, 0, &window, &renderer);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
for (i = 0; i < WINDOW_WIDTH; ++i)
SDL_RenderDrawPoint(renderer, i, i);
SDL_RenderPresent(renderer);
while (1) {
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
break;
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}
GitHub upstream.
Compile and run:
gcc -std=c89 -Wextra -pedantic-errors -o main.out main.c -lSDL2
./main.out
If you want to set a large rectangle of pixels at once, e.g. the entire screen or a sprite, use SDL_Texture + SDL_RenderCopy and possibly SDL_TEXTUREACCESS_STREAMING, as that will be much faster. Examples at:
What is a Blit in SDL?
Rendering pixels from array of RGB values in SDL 1.2?
Tested on libsdl 2.0.2, Ubuntu 15.10.
I don't know how your code is structured. Assuming you have a SDL_Window and a SDL_Renderer, you just have to call SDL_RenderDrawPoint(renderer, x, y).
If you don't have a renderer nor window, you can create both with SDL_CreateWindowAndRenderer(). For example:
SDL_Window *window;
SDL_Renderer *renderer;
SDL_CreateWindowAndRenderer(800, 600, 0, &window, &renderer);
//Probably on a loop
SDL_RenderDrawPoint(renderer, 400, 300); //Renders on middle of screen.
SDL_RenderPresent(renderer);
This should draw a pixel on the middle of screen. To read a pixel is a little more complicated. You can use SDL_RenderReadPixels(), it is made for read an area, but you can always specify an area of 1x1. Read the wiki page if you really need it.
If you are having much trouble with SDL2 a recommend you to read the Lazy Foo tutorials. The SDL2 section still a work in progress, but there is enough material to begin learning.
I find Python + PySDL2 more easy to prototype with. Debug is also funny, because it is veeeery slooow for pixel graphics. =) Here is the complete code:
"""
The code is placed into public domain
by anatoly techtonik <techtonik#gmail.com>
"""
import sdl2
import sdl2.ext
sdl2.ext.init()
window = sdl2.ext.Window('', size=(300, 100))
window.show()
renderer = sdl2.ext.Renderer(window)
renderer.draw_point([10,10], sdl2.ext.Color(255,255,255))
renderer.present()
running = True
while running:
for e in sdl2.ext.get_events():
if e.type == sdl2.SDL_QUIT:
running = False
break
if e.type == sdl2.SDL_KEYDOWN:
if e.key.keysym.sym == sdl2.SDLK_ESCAPE:
running = False
break