Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include <windows.h>
- #include <stdio.h>
- #include <SWI-Prolog.h>
- #include <SDL.h>
- #include <SDL_image.h>
- #include <stdbool.h>
- SDL_Window* gWindow = NULL;
- SDL_Surface* gScreenSurface = NULL;
- SDL_Surface* gHelloWorld = NULL;
- SDL_Surface* loadedSurface = NULL;
- SDL_Renderer* gRenderer = NULL;
- SDL_Texture* gTexture = NULL;
- const int SCREEN_WIDTH = 640;
- const int SCREEN_HEIGHT = 480;
- bool init()
- {
- //Initialization flag
- bool success = true;
- //Initialize SDL
- if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
- {
- printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
- success = false;
- }
- else
- {
- //Set texture filtering to linear
- if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
- {
- printf( "Warning: Linear texture filtering not enabled!" );
- }
- //Create window
- gWindow = SDL_CreateWindow( "Main", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
- if( gWindow == NULL )
- {
- printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
- success = false;
- }
- else
- {
- //
- gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );
- if( gRenderer == NULL )
- {
- printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
- success = false;
- }
- else
- {
- //Initialize renderer color
- SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
- //Initialize PNG loading
- int imgFlags = IMG_INIT_PNG;
- if( !( IMG_Init( imgFlags ) & imgFlags ) )
- {
- printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
- success = false;
- }
- }
- }
- }
- return success;
- }
- static foreign_t pl_say_hello(term_t to)
- {
- printf("test\n");
- if ( 1 )//PL_get_atom_chars(to, &a)
- {
- //MessageBox(NULL, a, "DLL test", MB_OK|MB_TASKMODAL);
- //printf("%s\n",a);
- PL_succeed;
- }
- else
- PL_fail;
- }
- static foreign_t pl_init()
- {
- init();
- //exit(1);
- PL_succeed;
- }/**/
- struct pair
- {
- SDL_Surface* s;
- SDL_Texture* t;
- };
- //typedef pair pair;
- static foreign_t loadImage(term_t t1, int n)
- {
- char *s;
- void *p;
- PL_get_chars(t1,&s,CVT_STRING);
- printf("l;%s;\n",s);
- SDL_Surface* loadedSurface = IMG_Load( s );/*
- if( SDL_Surface == NULL )
- printf( "Unable to create texture from %s! SDL Error: %s\n", s, SDL_GetError() );
- SDL_Texture* newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface );
- if( newTexture == NULL )
- printf( "Unable to create texture from %s! SDL Error: %s\n", s, SDL_GetError() );
- SDL_FreeSurface( loadedSurface );
- //gHelloWorld = SDL_LoadBMP( "hello.bmp" );
- //gHelloWorld = IMG_Load( "hello.bmp" );
- gHelloWorld = IMG_Load( s );
- gTexture=newTexture;
- //loadedSurface = SDL_LoadBMP( s );
- if( loadedSurface == NULL ) {
- printf( "Unable to load image %s! SDL Error: %s\n", s, SDL_GetError() );
- //throw s;
- PL_fail;
- }
- printf("l;%d;%d;\n",newTexture,loadedSurface);*/
- //p={newTexture,loadedSurface};
- term_t a1=PL_new_term_ref();term_t a2=PL_new_term_ref();
- term_t a;
- functor_t img=PL_new_functor(PL_new_atom("image"),2);
- //p=img;
- //p[0]=newTexture; p[1]=loadedSurface;
- //PL_unify_pointer(a1+1,p);
- PL_put_atom_chars(a1, "gnu");
- PL_put_integer(a2, 50);
- PL_cons_functor(a,img,a1,a2);
- PL_unify(t1+1,a);
- PL_succeed;
- }
- static foreign_t pl_check(term_t t, int n)
- {
- printf("-\n");
- term_t a1=PL_new_term_ref();term_t a2=PL_new_term_ref();
- term_t a;
- functor_t img=PL_new_functor(PL_new_atom("image"),2);
- PL_put_atom_chars(a1, "gnu");
- PL_put_integer(a2, 50);
- PL_cons_functor(a,img,a1,a2);
- PL_unify(t,a);
- PL_succeed;
- }
- int x=0,y=0,w=50,h=50;
- void draw(int x,int y) {
- SDL_Rect renderQuad = { x, y, w, h };
- printf("w:%d;\n",w);
- SDL_RenderCopy( gRenderer, gTexture, NULL, &renderQuad );
- }
- void draw_(int x,int y,int w,int h) {
- SDL_Rect renderQuad = { x, y, w, h };
- printf("w:%d;\n",w);
- SDL_RenderCopy( gRenderer, gTexture, NULL, &renderQuad );
- }
- static foreign_t drawImage(term_t a1, int n)
- {
- printf("-\n");
- void *p;
- //PL_get_pointer(a1,&p);
- //printf("d;%d;%d;\n",p.t,p.s);
- //printf("d;%d;%d;\n",p[0],p[1]);
- //printf("d;%d;\n",p);
- //gTexture=p;
- //SDL_BlitSurface( (SDL_Surface*)p, NULL, gScreenSurface, NULL );
- //SDL_BlitSurface( p, NULL, gScreenSurface, NULL );
- //SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
- //Clear screen
- SDL_RenderClear( gRenderer );
- draw(x,y);
- //Update screen
- SDL_RenderPresent( gRenderer );
- PL_succeed;
- }
- static foreign_t update(term_t a){
- SDL_UpdateWindowSurface( gWindow );PL_succeed;
- }
- static foreign_t pl_circle(term_t a){
- //circle(screen,a0,a1,a2,a3);SDL_SetColorKey( loadedSurface, SDL_TRUE, SDL_MapRGB( loadedSurface->format, 0, 0xFF, 0xFF ) );
- PL_succeed;
- }
- /*
- static foreign_t pl_check(term_t t, int n)
- {
- printf("-\n");
- int i;
- //PL_get_integer(a1,&i);
- printf("%d;%d;\n",a1,&a1);
- printf("%d;%d;\n",i,n);
- }*/
- static foreign_t pl_circlefill(term_t a0,term_t a1,term_t a2,term_t a3){
- //circlefill(screen,&a0,&a1,&a2,&a3);
- PL_succeed;
- }
- /*
- */
- static foreign_t pl_exit()
- {
- //SDL_FreeSurface( gHelloWorld ); gHelloWorld = NULL;
- SDL_DestroyWindow( gWindow );
- gWindow = NULL;
- SDL_Quit();
- PL_succeed;
- }
- static foreign_t sdl_delay()
- {
- //readkey();
- SDL_Delay(1000);
- PL_succeed;
- }
- static foreign_t pl_readkey()
- {
- //readkey();
- PL_succeed;
- }
- static PL_extension predicates[] = {
- { "say_hello", 1, pl_say_hello, 0 },
- { "init", 0, pl_init, 0 },
- { "exit", 0, pl_exit, 0 },
- { "sdl_delay", 0, sdl_delay, 0 },
- { "check", 1, pl_check, 0 },
- { "circlefill", 4, pl_circlefill, 0 },
- { "loadImage", 2, loadImage, 0 },
- { "update", 0, update, 0 },
- { "drawImage", 3, drawImage, 0 },
- //{ "readkey", 0, pl_readkey, 0 },
- { NULL, 0, NULL, 0 }
- };
- install_t install_lib()
- { //PL_register_foreign("say_hello", 1, pl_say_hello, 0);
- //PL_register_foreign("init", 1, pl_init, 0);
- PL_register_extensions(predicates);
- }
- //int main(int a,char*s){}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement