Advertisement
j0h

Untitled

j0h
Sep 4th, 2016
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. #include <SDL/SDL.h>
  2. #include "opencv2/core/core.hpp"
  3. #include "opencv2/imgcodecs.hpp"
  4. #include "opencv2/imgproc/imgproc.hpp"
  5. #include "opencv2/highgui/highgui.hpp"
  6. #include <opencv2/imgproc/imgproc.hpp>
  7. using namespace cv;
  8.  
  9. SDL_Surface* background = NULL;
  10. SDL_Surface* sprite = NULL;
  11. SDL_Surface* backbuffer = NULL;
  12. SDL_Surface* asurface = NULL;
  13. //pass in an opencv image, get a sureface out
  14. //https://wiki.libsdl.org/SDL_CreateRGBSurfaceFrom
  15. //http://stackoverflow.com/questions/22702630/converting-cvmat-to-sdl-texture
  16. SDL_Surface* MatToSDL(const cv::Mat &frame){
  17.     IplImage opencvimg2 = (IplImage)frame;
  18.     IplImage* opencvimg = &opencvimg2;
  19.  
  20.      //Convert to SDL_Surface
  21.     asurface = SDL_CreateRGBSurfaceFrom(
  22.                          (void*)opencvimg->imageData,
  23.                          opencvimg->width, opencvimg->height,
  24.                          opencvimg->depth*opencvimg->nChannels,
  25.                          opencvimg->widthStep,
  26.                          0xff0000, 0x00ff00, 0x0000ff, 0);
  27.  
  28.     if(asurface == NULL){
  29.         printf("Couldn't convert Mat to Surface.");
  30.         return NULL;
  31.     }    else    {
  32.         printf("SUCCESS! conversion!");
  33.         return asurface;
  34.     }
  35.  
  36.     cvReleaseImage(&opencvimg);
  37.  
  38. }
  39. bool ProgramIsRunning();
  40. bool LoadImages();
  41. void FreeImages();
  42.  
  43. int main(int argc, char* args[]){
  44.     if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
  45.         printf("SDL failed to initialize!\n");
  46.         SDL_Quit();
  47.         return 0;
  48.     }
  49.  
  50.     backbuffer = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
  51.     SDL_WM_SetCaption("SDL!!!", NULL);
  52.  
  53.     if(!LoadImages()){
  54.         printf("Images failed to load!\n");
  55.         FreeImages();
  56.         SDL_Quit();
  57.  
  58.         return 0;
  59.     }
  60.  
  61.     SDL_BlitSurface(background, NULL, backbuffer, NULL );
  62.  
  63.     while(ProgramIsRunning()){
  64.         SDL_Rect spritePos;
  65.         spritePos.x = rand()%800;
  66.         spritePos.y = rand()%600;
  67.  
  68.         SDL_BlitSurface(sprite, NULL, backbuffer, &spritePos);
  69.  
  70.         SDL_Flip(backbuffer);
  71.  
  72.         SDL_Delay(100);
  73.     }
  74.     SDL_Quit();
  75.  
  76.     return 1;
  77. }
  78.  
  79. bool ProgramIsRunning(){
  80.     SDL_Event event;
  81.  
  82.     bool running = true;
  83.  
  84.     while(SDL_PollEvent(&event))
  85.     {
  86.         if(event.type == SDL_QUIT)
  87.             running = false;
  88.     }
  89.  
  90.     return running;
  91. }
  92.  
  93. bool LoadImages(){
  94.     if(background == NULL){
  95.         printf("Image failed to load!\n");
  96.         //SDL_Quit();
  97.         return 0;
  98.     }
  99.  
  100.  
  101.     sprite = SDL_LoadBMP("graphics/sprite.bmp");
  102.  
  103.     if(sprite == NULL)
  104.         return false;
  105.  
  106.     return true;
  107. }
  108.  
  109. void FreeImages(){
  110.     if(background != NULL){
  111.         SDL_FreeSurface(background);
  112.         background = NULL;
  113.     }
  114.  
  115.     if(sprite != NULL){
  116.         SDL_FreeSurface(sprite);
  117.         sprite = NULL;
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement