Advertisement
Guest User

Untitled

a guest
Aug 19th, 2022
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.96 KB | None | 0 0
  1. //#include <windows.h>
  2. #include <stdio.h>
  3. #include <SWI-Prolog.h>
  4. #include <SDL.h>
  5. #include <SDL_image.h>
  6. #include <stdbool.h>
  7.  
  8. SDL_Window* gWindow = NULL;
  9. SDL_Surface* gScreenSurface = NULL;
  10. SDL_Surface* gHelloWorld = NULL;
  11. SDL_Surface* loadedSurface = NULL;
  12. SDL_Renderer* gRenderer = NULL;
  13. SDL_Texture* gTexture = NULL;
  14.  
  15. const int SCREEN_WIDTH = 640;
  16. const int SCREEN_HEIGHT = 480;
  17.  
  18. bool init()
  19. {
  20.     //Initialization flag
  21.     bool success = true;
  22.  
  23.     //Initialize SDL
  24.     if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  25.     {
  26.         printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
  27.         success = false;
  28.     }
  29.     else
  30.     {
  31.         //Set texture filtering to linear
  32.         if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
  33.         {
  34.             printf( "Warning: Linear texture filtering not enabled!" );
  35.         }
  36.  
  37.         //Create window
  38.         gWindow = SDL_CreateWindow( "Main", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
  39.         if( gWindow == NULL )
  40.         {
  41.             printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
  42.             success = false;
  43.         }
  44.         else
  45.         {
  46.             //
  47.             gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );
  48.             if( gRenderer == NULL )
  49.             {
  50.                 printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
  51.                 success = false;
  52.             }
  53.             else
  54.             {
  55.                 //Initialize renderer color
  56.                 SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
  57.                
  58.                 //Initialize PNG loading
  59.                 int imgFlags = IMG_INIT_PNG;
  60.                 if( !( IMG_Init( imgFlags ) & imgFlags ) )
  61.                 {
  62.                     printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
  63.                     success = false;
  64.                 }
  65.             }
  66.         }
  67.     }
  68.  
  69.     return success;
  70. }
  71.  
  72.  
  73. static foreign_t pl_say_hello(term_t to)
  74. {
  75.     printf("test\n");
  76.     if ( 1 )//PL_get_atom_chars(to, &a)
  77.     {
  78.         //MessageBox(NULL, a, "DLL test", MB_OK|MB_TASKMODAL);
  79.         //printf("%s\n",a);
  80.         PL_succeed;
  81.     }
  82.     else
  83.         PL_fail;
  84. }
  85.  
  86. static foreign_t pl_init()
  87. {
  88.     init();
  89.     //exit(1);
  90.     PL_succeed;
  91. }/**/
  92. struct pair
  93. {
  94.     SDL_Surface* s;
  95.     SDL_Texture* t;
  96. };
  97. //typedef pair pair;
  98. static foreign_t loadImage(term_t t1, int n)
  99. {
  100.     char *s;
  101.     void *p;
  102.     PL_get_chars(t1,&s,CVT_STRING);
  103.     printf("l;%s;\n",s);
  104.     SDL_Surface* loadedSurface = IMG_Load( s );/*
  105.     if( SDL_Surface == NULL )
  106.         printf( "Unable to create texture from %s! SDL Error: %s\n", s, SDL_GetError() );
  107.     SDL_Texture* newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface );
  108.     if( newTexture == NULL )
  109.         printf( "Unable to create texture from %s! SDL Error: %s\n", s, SDL_GetError() );
  110.     SDL_FreeSurface( loadedSurface );
  111.     //gHelloWorld = SDL_LoadBMP( "hello.bmp" );
  112.     //gHelloWorld = IMG_Load( "hello.bmp" );
  113.     gHelloWorld = IMG_Load( s );
  114.     gTexture=newTexture;
  115.     //loadedSurface = SDL_LoadBMP( s );
  116.     if( loadedSurface == NULL ) {
  117.         printf( "Unable to load image %s! SDL Error: %s\n", s, SDL_GetError() );
  118.         //throw s;
  119.         PL_fail;
  120.     }
  121.     printf("l;%d;%d;\n",newTexture,loadedSurface);*/
  122.     //p={newTexture,loadedSurface};
  123.     term_t a1=PL_new_term_ref();term_t a2=PL_new_term_ref();
  124.     term_t a;
  125.     functor_t img=PL_new_functor(PL_new_atom("image"),2);
  126.     //p=img;
  127.     //p[0]=newTexture; p[1]=loadedSurface;
  128.     //PL_unify_pointer(a1+1,p);
  129.     PL_put_atom_chars(a1, "gnu");
  130.     PL_put_integer(a2, 50);
  131.     PL_cons_functor(a,img,a1,a2);
  132.     PL_unify(t1+1,a);
  133.     PL_succeed;
  134. }
  135. static foreign_t pl_check(term_t t, int n)
  136. {
  137.     printf("-\n");
  138.     term_t a1=PL_new_term_ref();term_t a2=PL_new_term_ref();
  139.     term_t a;
  140.     functor_t img=PL_new_functor(PL_new_atom("image"),2);
  141.     PL_put_atom_chars(a1, "gnu");
  142.     PL_put_integer(a2, 50);
  143.     PL_cons_functor(a,img,a1,a2);
  144.     PL_unify(t,a);
  145.     PL_succeed;
  146. }
  147.  
  148. int x=0,y=0,w=50,h=50;
  149. void draw(int x,int y) {
  150.     SDL_Rect renderQuad = { x, y, w, h };
  151.     printf("w:%d;\n",w);
  152.     SDL_RenderCopy( gRenderer, gTexture, NULL, &renderQuad );
  153. }
  154. void draw_(int x,int y,int w,int h) {
  155.     SDL_Rect renderQuad = { x, y, w, h };
  156.     printf("w:%d;\n",w);
  157.     SDL_RenderCopy( gRenderer, gTexture, NULL, &renderQuad );
  158. }
  159. static foreign_t drawImage(term_t a1, int n)
  160. {
  161.     printf("-\n");
  162.     void *p;
  163.     //PL_get_pointer(a1,&p);
  164.     //printf("d;%d;%d;\n",p.t,p.s);
  165.     //printf("d;%d;%d;\n",p[0],p[1]);
  166.     //printf("d;%d;\n",p);
  167.     //gTexture=p;
  168.     //SDL_BlitSurface( (SDL_Surface*)p, NULL, gScreenSurface, NULL );
  169.     //SDL_BlitSurface( p, NULL, gScreenSurface, NULL );
  170.     //SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
  171.     //Clear screen
  172.     SDL_RenderClear( gRenderer );
  173.     draw(x,y);
  174.     //Update screen
  175.     SDL_RenderPresent( gRenderer );
  176.     PL_succeed;
  177. }
  178.    
  179. static foreign_t update(term_t a){
  180.     SDL_UpdateWindowSurface( gWindow );PL_succeed;
  181. }
  182. static foreign_t pl_circle(term_t a){
  183.     //circle(screen,a0,a1,a2,a3);SDL_SetColorKey( loadedSurface, SDL_TRUE, SDL_MapRGB( loadedSurface->format, 0, 0xFF, 0xFF ) );
  184.     PL_succeed;
  185. }
  186. /*
  187. static foreign_t pl_check(term_t t, int n)
  188. {
  189.     printf("-\n");
  190.     int i;
  191.     //PL_get_integer(a1,&i);
  192.     printf("%d;%d;\n",a1,&a1);
  193.     printf("%d;%d;\n",i,n);
  194. }*/
  195. static foreign_t pl_circlefill(term_t a0,term_t a1,term_t a2,term_t a3){
  196.     //circlefill(screen,&a0,&a1,&a2,&a3);
  197.     PL_succeed;
  198. }
  199. /*
  200. */
  201.  
  202. static foreign_t pl_exit()
  203. {
  204.     //SDL_FreeSurface( gHelloWorld ); gHelloWorld = NULL;
  205.     SDL_DestroyWindow( gWindow );
  206.     gWindow = NULL;
  207.  
  208.     SDL_Quit();
  209.     PL_succeed;
  210. }
  211. static foreign_t sdl_delay()
  212. {
  213.     //readkey();
  214.     SDL_Delay(1000);
  215.     PL_succeed;
  216. }
  217. static foreign_t pl_readkey()
  218. {
  219.     //readkey();
  220.     PL_succeed;
  221. }
  222.  
  223. static PL_extension predicates[] = {
  224. { "say_hello", 1, pl_say_hello, 0 },
  225. { "init", 0, pl_init, 0 },
  226. { "exit", 0, pl_exit, 0 },
  227. { "sdl_delay", 0, sdl_delay, 0 },
  228. { "check", 1, pl_check, 0 },
  229. { "circlefill", 4, pl_circlefill, 0 },
  230. { "loadImage", 2, loadImage, 0 },
  231. { "update", 0, update, 0 },
  232. { "drawImage", 3, drawImage, 0 },
  233. //{ "readkey", 0, pl_readkey, 0 },
  234. { NULL,         0,      NULL,   0 }
  235. };
  236. install_t install_lib()
  237. { //PL_register_foreign("say_hello", 1, pl_say_hello, 0);
  238. //PL_register_foreign("init", 1, pl_init, 0);
  239. PL_register_extensions(predicates);
  240. }
  241. //int main(int a,char*s){}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement