#include #include "opencv2/core/core.hpp" #include "opencv2/imgcodecs.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include using namespace cv; SDL_Surface* background = NULL; SDL_Surface* sprite = NULL; SDL_Surface* backbuffer = NULL; SDL_Surface* asurface = NULL; //pass in an opencv image, get a sureface out //https://wiki.libsdl.org/SDL_CreateRGBSurfaceFrom //http://stackoverflow.com/questions/22702630/converting-cvmat-to-sdl-texture SDL_Surface* MatToSDL(const cv::Mat &frame){ IplImage opencvimg2 = (IplImage)frame; IplImage* opencvimg = &opencvimg2; //Convert to SDL_Surface asurface = SDL_CreateRGBSurfaceFrom( (void*)opencvimg->imageData, opencvimg->width, opencvimg->height, opencvimg->depth*opencvimg->nChannels, opencvimg->widthStep, 0xff0000, 0x00ff00, 0x0000ff, 0); if(asurface == NULL){ printf("Couldn't convert Mat to Surface."); return NULL; } else { printf("SUCCESS! conversion!"); return asurface; } cvReleaseImage(&opencvimg); } bool ProgramIsRunning(); bool LoadImages(); void FreeImages(); int main(int argc, char* args[]){ if(SDL_Init(SDL_INIT_EVERYTHING) < 0) { printf("SDL failed to initialize!\n"); SDL_Quit(); return 0; } backbuffer = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE); SDL_WM_SetCaption("SDL!!!", NULL); if(!LoadImages()){ printf("Images failed to load!\n"); FreeImages(); SDL_Quit(); return 0; } SDL_BlitSurface(background, NULL, backbuffer, NULL ); while(ProgramIsRunning()){ SDL_Rect spritePos; spritePos.x = rand()%800; spritePos.y = rand()%600; SDL_BlitSurface(sprite, NULL, backbuffer, &spritePos); SDL_Flip(backbuffer); SDL_Delay(100); } SDL_Quit(); return 1; } bool ProgramIsRunning(){ SDL_Event event; bool running = true; while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) running = false; } return running; } bool LoadImages(){ if(background == NULL){ printf("Image failed to load!\n"); //SDL_Quit(); return 0; } sprite = SDL_LoadBMP("graphics/sprite.bmp"); if(sprite == NULL) return false; return true; } void FreeImages(){ if(background != NULL){ SDL_FreeSurface(background); background = NULL; } if(sprite != NULL){ SDL_FreeSurface(sprite); sprite = NULL; } }