MEX wrap around SDL2 library (Simple DirectMedia Layer)

3 views (last 30 days)
I am trying to write a simple wrap around SDL2 library. The code below is suppose to render a white screen on the display. During the window show command the program throws a: "Unexpected unknown exception from MEX file" and the window can not be closed or destroyed. The C code compiles and executes without any problems outside Matlab.
testSDLMex.c
#include <SDL.h>
#include <stdio.h>
#include "mex.h"
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
/* allocate SDL variables */
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Rect display_rect;
int display_count;
/* initialize SDL library */
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0)
{
mexErrMsgTxt("Unable to initialize SDL and its subsystems.\n");
}
/* detect displays count */
display_count = SDL_GetNumVideoDisplays();
/* get position and size of last display */
if(SDL_GetDisplayBounds((display_count-1), &display_rect) != 0)
{
mexErrMsgTxt("SDL could not measure display bounds!\n");
}
/* some test outputs */
printf("Ready: %d!\n",display_count);
printf("x= %d\ny= %d\nw= %d\nh= %d\n",display_rect.x,display_rect.y,display_rect.w,display_rect.h);
/* create window on last display */
window = SDL_CreateWindow("SDL2Window",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
640, 480,
SDL_WINDOW_HIDDEN);
if ( window == NULL )
{
mexErrMsgTxt("Window could not be created!\n");
}
/* get window renderer */
renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_PRESENTVSYNC);
/* show white window */
SDL_ShowWindow(window);
SDL_SetRenderDrawColor( renderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( renderer );
SDL_RenderPresent( renderer );
SDL_Delay(1000);
SDL_HideWindow(window);
/* quit SDL */
SDL_DestroyRenderer( renderer );
SDL_DestroyWindow( window );
SDL_Quit();
return;
}
To compile the MEX file use: testSDLMexCompile.m
mex testSDLMex.c -o testSDLMex -I/usr/local/include/SDL2/ -L/usr/local/lib/ -lSDL2;
testSDLMex;
Any remarks or suggestions are welcome. Thank you in advance! G.
  1 Comment
Image Analyst
Image Analyst on 24 Aug 2014
What's your MATLAB version and operating system. Do you have a mac, like Geoff? Are you sure the "bits" of the DLL matches the bits of the MATLAB code you're running?

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 24 Aug 2014
I grabbed SDL 2.0.3 (for OS X), compiled the above, and observed the same behaviour as you. Though I think that the exception is only being thrown once the code is instructed to hide the window, destroy it, or quit (seems to hang on this last one). I tried this by commenting out the lines
SDL_HideWindow(window);
SDL_DestroyWindow( window );
SDL_Quit();
recompiled, and executed the function. The white screen appeared, then went black (presumably due to the destroyed renderer), and the function exited gracefully. The window was still there but there was no exception. (And I have to exit MATLAB before the window can disappear.)
So running the code did show the white screen on the window, and the renderer could be destroyed, but that was as far as it could go (successfully).
Since the exception is unknown, converting the above code to a cpp file with a try-catch block will not give any more information (I did try this out of curiosity).
Taking the above function, removing the mex friendly calls and placing it in a int main block of a file called main.c, I could compile it with GCC as
gcc main2.c -o main2 -I/Library/Frameworks/SDL2.framework/Headers -framework SDL2
Executing it from the unix command line was successful - there was no crash.
Is there a particular reason you are writing this through MATLAB? I think that you are "safe" to run whatever code you have in mind, there would just be a problem when it came time to the exit/shut down of the window.

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!