guile-sources
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

the guile part of the Twigg project


From: JOHN GOODWIN
Subject: the guile part of the Twigg project
Date: Sun, 18 Sep 2011 02:18:23 +0100

//***********************************************************************************************************
//  File Name :            learn1_lib.cpp
//  Header    :               none.
//  Author    :               John Goodwin
//  Web site  :               http://www.objobj.webs.com
//  Copyright :               John Goodwin
//  Licence   :               Gnu Free Public licence
//                            (guile) Copyright (C) 1995-2011 Free Software Foundation, Inc.
//  Platform  :               Microsoft Windows XP and later (cygwin)
//  Date      :               20/07/2011 19:00
//-----------------------------------------------------------------------------------------------------------
//  Description/Purpose:
//  ====================
//  A starter program for learning Guile(2.0.1) with MS windows (XP and later).
//  Built for guile 2.0.1 using cygwin.
//  You can independently compile/enhance this library and use twig.exe to run it,
//  without recompiling twig.exe.
//  g++ (c++) compiled (but mainly pure c) UNICODE.
//  (was build origionally with guile 1.8) [commented out depreciated code]
//  Demo program guile (2.0.1) Win32 (XP) C++ g++ ( 4.3.4) cygwin(1.7.7-1) UNICODE tortoise (integrated console)
//  Try to hide as much MS windows as I can (this is about Guile).
//  Try to hide build problems by giving binaries as well as source code.
//  a library containing a guile interface to a MS window.for drawing/painting.
//  cygwin makefile and wxDev-C++ Project provided.
//  I will try to give you the mininal libraries/exe to...
//  try to execute a binary to see what it does,
//  before you need to comile...without breaking licencies/copyright
//-----------------------------------------------------------------------------------------------------------
//  History:
//  ========
//  http://www.gnu.org/software/guile/docs/guile-tut/tutorial.html
//-----------------------------------------------------------------------------------------------------------
//  future:
//  =======
//  Visual C++...working on it....
//  wxWidgets, WTL etc...
//-----------------------------------------------------------------------------------------------------------
//  downloads:
//  ==========
//  http://www.gnu.org/s/guile
//  http://www.cygwin.com
//  http://www.gnu.org
//  (http://www.wxwidgets.org)
//  (http://wxdsgn.sourceforge.net)
//-----------------------------------------------------------------------------------------------------------
//  dependancies:
//  =============
//  you should install guile 2.0 and cygwin BUT....
//  building everything is not as trivial as it could be...like life..
//-----------------------------------------------------------------------------------------------------------
//  Acknowlegments:
//  ===============
//  David Drysdale.....Daniel Kraft, address@hidden
//-----------------------------------------------------------------------------------------------------------
//  Functions:
//  ==========
//  DLLEXPORT BOOL APIENTRY DllMain (HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved);
//  DLLEXPORT int jrg_main(int argc, char **argv);
//  DLLEXPORT void inner_main (void *closure, int argc, char **argv)
//***********************************************************************************************************
//typedef int ssize_t ;
//typedef int pthread_attr_t;
//typedef int pthread_t;
//typedef int pthread_once_t;
//typedef int SCM_CELL_TYPE;
//includes
//========
//#include "stdafx.h"                       // MCF WTL WX
#include <windows.h>                        // Windows
#include <stdio.h>                          //
#include <stdlib.h>                         //
 
#include <guile/2.0/libguile.h>             // Guile 2.0
//#include <guile/gh.h>                     // depreciated
 
#include <wchar.h>                          // UNICODE
#include <math.h>                           // #import works...cool ?
#include <assert.h>                         //
 
#include "resource.h"                       // resource
#include "defines.h"                        // this is MACRO stuff
#include "debug.h"                          // this is MACRO stuff
 
//#include "QuickFill.h"                    // floodfill
 
//defines
//=======
#define  DEGREES_TO_RADIANS                  (3.1415926535897932384626433832795029L/180.0)
//#define  WINDOW_SIZE 500
#define  CONFIGFILENAME                      ".tortoise"
#define  DIRECTORYSEPARATOR                  "/"
 
//declare
//=======
//guile SCM functions
extern "C" void     register_procs           (void);
extern "C" SCM      tortoise_reset           (void);
extern "C" SCM      tortoise_reset_direction (void);
extern "C" SCM      tortoise_cls             (void);
extern "C" SCM      tortoise_pendown         (void);
extern "C" SCM      tortoise_penup           (void);
extern "C" SCM      tortoise_turn            (SCM  s_degrees);
extern "C" SCM      tortoise_move            (SCM  s_steps);
extern "C" SCM      tortoise_color           (SCM  scm_r,SCM  scm_g,SCM  scm_b);
extern "C" SCM      tortoise_set_pixel       (SCM  s_pos_x, SCM  s_pos_y);
extern "C" SCM      tortoise_get_pixel       (SCM  s_pos_x, SCM  s_pos_y);
extern "C" SCM      tortoise_jump_to         (SCM  s_pos_x, SCM  s_pos_y);
extern "C" SCM      floodfill                (SCM  s_pos_x, SCM  s_pos_y);
extern "C" SCM      draw_text                (SCM s_text, SCM  s_pos_x, SCM  s_pos_y);
 
//extern "C" SCM    tortoise_blitt           (SCM  s_pos_x, SCM  s_pos_y, SCM  s_pos_x1, SCM  s_pos_y1, SCM  s_pos_x2, SCM  s_pos_y2);
 
//helper functions
extern "C" void     GetLargestDisplayMode    (int * pcxBitmap, int * pcyBitmap);
void                read_config_file         (void);
//DLLIMPORT void scm_boot_guile (int argc, char **argv,//CM_API
//        void (*main_func) (void *closure,int argc,char **argv),
//        void *closure);
extern "C" void scm_init_guile ();
extern "C" void scm_boot_guile (int argc, char **argv,//CM_API
        void (*main_func) (void *closure,int argc,char **argv),
        void *closure);
/*
#if defined (SCM_IMPORT)
# define SCM_API __declspec (dllimport) extern
#elif defined (SCM_EXPORT) || defined (DLL_EXPORT)
# define SCM_API __declspec (dllexport) extern
#else
# define SCM_API extern
*/
 

//guile C functions (examples..... not called)
void                tortoise_reset1          (void);
void                tortoise_pendown1        (void);
void                tortoise_penup1          (void);
void                tortoise_turn1           (int degrees);
void                tortoise_move1           (int steps);
 
//windows interface functions
//DLLEXPORT int WINAPI   DllMain          (HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved);
DLLEXPORT BOOL APIENTRY  DllMain          (HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved); //dll stuff
 
DLLEXPORT int            jrg_main         (int argc, char **argv);                 // init guile
void                     inner_main       (void *closure, int argc, char **argv);  // init guile
DLLEXPORT void           setHwnd          (HWND ahwnd);                            // window handle
DLLEXPORT void           sethdcMem        (HDC *ahdcMem);                          // memory hardware device context
DLLEXPORT void           setscrlX         (int scrlX);                             // scrolling offset x
DLLEXPORT void           setscrlY         (int scrlY);                             // scrolling offset y
//             __stdcall
BOOL                     CtrlHandler( DWORD fdwCtrlType );
//structures
//==========
 
//Global variables
//================
double                  currentX;             //
double                  currentY;             //
double                  currentDirection;     //
int                     penDown;              //
//int                   acolor;               //
int                     cxBitmap;             //
int                     cyBitmap;             //
 
extern CRITICAL_SECTION cs;                   //
static int              GcxClient ;           //
static int              GcyClient;            //
HDC                     Ghdc ;                //
int                     Gi ;                  //
PAINTSTRUCT             Gps ;                 // paint structure
POINT                   Gapt  [1000] ;        //
POINT                   Gapt1 [1000] ;        //
static POINT            Gapt3[4] ;            // a point
HBRUSH                  GhBrush ;             // brush
RECT                    Grc ;                 // rectangle
TCHAR                   GszBuffer [30] ;      // temporary string buffer for drawing
HPEN                    GlinePen ;            // line pen
int                     r,g,b;                // colour
 
HWND                    hwnd;                 // window handle we can do graphics on
HDC                     hdcMem;               // hardware device context in memory
int                     xscrl;                // see setscrlX
int                     yscrl;                // see setscrlY
HINSTANCE               hmodMSVCRT;           //
 
//***********************************************************************************************************
 
//--------------------------Dll's INFO--------------------------------------------------------------------------
/*
In Win32, all DLLs might contain an optional entry-point function
(usually called DllMain)
that is called for both initialization and termination.
This gives you an opportunity to allocate or release additional resources as needed.
Windows calls the entry-point function in four situations:
process attach, process detach, thread attach, and thread detach
The C run-time library provides an entry-point function called _DllMainCRTStartup, and it calls DllMain
 
When building DLLs in Visual C++, _DllMainCRTStartup is linked in automatically
and you do not need to specify an entry-point function using the /ENTRY: linker option
 
DLL type                  Where to add initialization and termination code
========                  ================================================
Regular DLL               In the DLL's CWinApp object's InitInstance and ExitInstance.
                          Because regular DLLs have a CWinApp object,
                          they should perform their initialization and termination tasks
                          in the same location as an MFC application:
                          in the InitInstance and ExitInstance member functions of the DLL's CWinApp-derived class
                          A regular DLL can keep track of multiple threads by calling TlsAlloc and TlsGetValue
                          in its InitInstance function.
                          These functions allow the DLL to track thread-specific data.
 

Extension DLL             In the DllMain function generated by the MFC DLL Wizard.
                          Because MFC provides a DllMain function that is called by _DllMainCRTStartup
                          for PROCESS_ATTACH and PROCESS_DETACH, you should not write your own DllMain function.
                          The MFC-provided DllMain function calls InitInstance when your DLL is loaded
                          and it calls ExitInstance before the DLL is unloaded.
 
Non-MFC DLL               In a function called DllMain that you provide.
 
*/
//--------------------------DllMain--------------------------------------------------------------------------
DLLEXPORT BOOL APIENTRY DllMain(HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
 switch( ul_reason_for_call )
 {
         case DLL_PROCESS_ATTACH:
//           hmodMSVCRT = LoadLibrary(L"MSVCRT.DLL");
//     JRGTRACE(L"DLL_PROCESS_ATTACH\n");
//           DPRINTF("DllMain DLL_PROCESS_ATTACH\n");//howhere to write to !
           break;
 
         case DLL_THREAD_ATTACH:
//           hmodMSVCRT = LoadLibrary(L"MSVCRT.DLL");
//     JRGTRACE(L"DLL_THREAD_ATTACH\n");
//           DPRINTF("DllMain DLL_THREAD_ATTACH\n");//howhere to write to !
           break;
 
         case DLL_THREAD_DETACH:
     JRGTRACE(L"DLL_THREAD_DETACH\n");
//     DPRINTF("DllMain DLL_THREAD_DETACH\n");
           break;
 
         case DLL_PROCESS_DETACH:
     JRGTRACE(L"DLL_PROCESS_DETACH\n");
//           DPRINTF("DllMain DLL_PROCESS_DETACH\n");
           break;
 
         default:
            break;
    }
    return TRUE;
}//DllMain
//--------------------------DllMain--------------------------------------------------------------------------
/*
DLLEXPORT int WINAPI DllMain (HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
{
 //allocate or release additional resources as needed
 
 switch( fdwReason )
 {
         case DLL_PROCESS_ATTACH:
//           DPRINTF("DllMain DLL_PROCESS_ATTACH\n");//howhere to write to !
           break;
 
         case DLL_THREAD_ATTACH:
//           DPRINTF("DllMain DLL_THREAD_ATTACH\n");//howhere to write to !
           break;
 
         case DLL_THREAD_DETACH:
           DPRINTF("DllMain DLL_THREAD_DETACH\n");
           break;
 
         case DLL_PROCESS_DETACH:
           DPRINTF("DllMain DLL_PROCESS_DETACH\n");
           break;
 
         default:
            break;
    }
 return TRUE;
}//DllMain
*/
//--------------------------jrg_main-------------------------------------------------------------------------
DLLEXPORT int jrg_main(int argc, char **argv)
{
    JRGTRACE(L"jrg_main inside dll\n");
/*
static HWND                hCon;
HANDLE                     hStdout;
HANDLE                     hStdin;
HANDLE                     hStderr;
 
             hStdin = GetStdHandle(STD_INPUT_HANDLE);  // ((DWORD)-10)
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);// ((DWORD)-11)
             hStderr = GetStdHandle(STD_ERROR_HANDLE); // ((DWORD)-12)
             AllocConsole();
    freopen("CONIN$", "r", stdin);
    freopen("CONOUT$", "w", stdout);
    freopen("CONERR$", "w", stderr);
             ShowWindow(GetConsoleWindow(), SW_SHOW);// Redraw the application window.
//HMODULE h = LoadLibrary(L"C:\\cygwin\\bin\\cygwin1.dll");
//void (*pFn)();
//pFn = (void (__cdecl *)(void )) (FARPROC)  GetProcAddress(h, "cygwin_dll_init");
//pFn(); // init cygwin1
*/
   scm_init_guile ();
 
    JRGTRACE(L"jrg_main  scm_init_guile(done) inside dll\n");
// DPRINTF("scm_boot_guile ...\n");
 scm_boot_guile (argc, argv, inner_main, 0);
 
// DPRINTF("gh_enter ...\n");
//  gh_enter(argc, argv, inner_main);
return 0;
}//jrg_main
//------------------------inner_main-------------------------------------------------------------------------
void inner_main (void *closure, int argc, char **argv)
{
SCM func_symbol;
SCM func;
 
//    printf("inner_main ...[argc=%d],[argv[0]=%s]\n",argc,argv[0]);
   JRGTRACE(L"inner_main Test of TrAcE()[argc=%d],[argv[0]=%s]\n",argc,argv[0]);
   register_procs();
   r= 255;g=255;b=255;
    GetLargestDisplayMode (&cxBitmap, &cyBitmap) ;
 // Load the scheme function definitions
// scm_c_primitive_load ("script.scm");
// scm_c_primitive_load ("C:\\Documents and Settings\\john\\guile\\learn1\\learn1\\Debug\\script.scm");
 scm_c_primitive_load ("C:/Documents and Settings/john/guile/learn1/learn1/Debug/script.scm");
 
 func_symbol = scm_c_lookup("do-hello");
 func = scm_variable_ref(func_symbol);
 
 scm_call_0 (func);
 
// for fun, evaluate some simple Scheme expressions here
//DEPRECIATED gh_eval_str USE scm_c_eval_string
//http://www.gnu.org/software/guile/docs/docs-1.8/guile-ref/Transitioning-away-from-GH.html
    scm_c_eval_string("(define (square x) (* x x))");
    scm_c_eval_string("(define (fact n) (if (= n 1) 1 (* n (fact (- n 1)))))");
    scm_c_eval_string("print (square 9)\n");
 
//jrg***    read_config_file();
//DEPRECIATED gh_repl USE scm_shell
 scm_shell ( argc, argv);
 
 exit(EXIT_SUCCESS);
}//inner_main
//-------------------------setHwnd---------------------------------------------------------------------------
DLLEXPORT void  setHwnd (HWND ahwnd)
{
// DPRINTF_P("setHwnd[%p]\n",ahwnd);
 hwnd = ahwnd;
}//setHwnd
//-------------------------sethdcMem-------------------------------------------------------------------------
DLLEXPORT void  sethdcMem (HDC *ahdcMem)
{
// DPRINTF_P("sethdcMem[%p]\n",*ahdcMem);
 hdcMem = *ahdcMem;
}//sethdcMem
//-------------------------setscrlX--------------------------------------------------------------------------
DLLEXPORT void  setscrlX (int scrlX)
{
 xscrl = scrlX;
}//setscrlX
//-------------------------setscrlY--------------------------------------------------------------------------
DLLEXPORT void  setscrlY (int scrlY)
{
 yscrl = scrlY;
}//setscrlY
//----------------------------tortoise_reset-----------------------------------------------------------------
extern "C" SCM tortoise_reset(void)
{
    DPRINTF("(tortoise-reset)");
    currentX = cxBitmap/2;
    currentY = cyBitmap/2;
    currentDirection = 0;
    penDown = 1;
    return SCM_UNSPECIFIED;
}//tortoise_reset
//-----------------------------------------------------------------------------------------------------------
extern "C" SCM tortoise_reset_direction(void)
{
    DPRINTF("(tortoise_reset_direction)");
//    currentX = currentY = WINDOW_SIZE/2;
    currentDirection = 0;
    penDown = 1;
    return SCM_UNSPECIFIED;
}//tortoise_reset_direction
//----------------------------tortoise_cls-------------------------------------------------------------------
extern "C" SCM tortoise_cls(void)
{
//    DPRINTF("(tortoise-cls)");
//    currentX = currentY = WINDOW_SIZE/2;
//    currentDirection = 0;
//    penDown = 1;
 
/*
HDC                hdc ;
static int        xBitmap, cyBitmap;
static HBITMAP    hBitmap ;
 
//hdcMem reset
                 DeleteDC (hdcMem) ;
 
//               GetLargestDisplayMode (&cxBitmap, &cyBitmap) ;
                 cxBitmap=1000;
                 cyBitmap=1000;
        hdc = GetDC (hwnd) ;
     hBitmap = CreateCompatibleBitmap (hdc, cxBitmap, cyBitmap) ;
     hdcMem  = CreateCompatibleDC (hdc) ;
 
     if (!hBitmap)       // no memory for bitmap
     {
          DeleteDC (hdcMem) ;
          return SCM_UNSPECIFIED ;
     }
     SelectObject (hdcMem, hBitmap) ;
        ReleaseDC (hwnd, hdc) ;
 */
 SendMessage(hwnd,IDM_EDIT_CLS,NULL,(LPARAM)NULL);
// SendMessage(hwnd,WM_PAINT,NULL,NULL);
 
    return SCM_UNSPECIFIED;
}//tortoise_cls
//-----------------------------tortoise_pendown--------------------------------------------------------------
extern "C" SCM tortoise_pendown(void)
{
    DPRINTF("(tortoise-pendown)");
    int prevValue = penDown;
    penDown = 1;
    return (prevValue ? SCM_BOOL_T: SCM_BOOL_F);
}//tortoise_pendown
//-----------------------------tortoise_penup----------------------------------------------------------------
 extern "C" SCM tortoise_penup(void)
{
    DPRINTF("(tortoise-penup)");
    int prevValue = penDown;
    penDown = 0;
    return (prevValue ? SCM_BOOL_T: SCM_BOOL_F);
}//tortoise_penup
//----------------------------tortoise_turn------------------------------------------------------------------
extern "C" SCM tortoise_turn(SCM  s_degrees)
{
//    int prevValue = (int)currentDirection;
//    double degrees;
//    SCM_ASSERT(SCM_NUMBERP(s_degrees), s_degrees, SCM_ARG1, "tortoise-turn");
//    degrees = scm_to_double(s_degrees);
//    currentDirection += degrees;
//    int degrees = SCM_INUM(s_degrees);
//    currentDirection += (double)degrees;
 
//    DPRINTF_F("(tortoise-turn )",degrees);
//    return SCM_EOL;
 
int prevValue = (int)currentDirection;
double degrees;
 
 SCM_ASSERT(SCM_NUMBERP(s_degrees), s_degrees, SCM_ARG1, "tortoise-turn");
 //DEPRECIATED scm_num2dbl USE
// degrees = scm_num2dbl(s_degrees,0);
 degrees = scm_to_double(s_degrees);
 currentDirection += degrees;
//    return SCM_MAKINUM(prevValue);
    return scm_from_int(prevValue);
}//tortoise_turn
//-----------------------------tortoise_color----------------------------------------------------------------
extern "C" SCM tortoise_color(SCM  scm_r,SCM  scm_g,SCM  scm_b)
{
 int ar;
 int ag;
 int ab;
    int prevValuer = (int)ar;
 
    int prevValueg = (int)ag;
 
    int prevValueb = (int)ab;
 
    SCM_ASSERT((scm_r), scm_r, SCM_ARG1, "tortoise-color");
    SCM_ASSERT((scm_g), scm_g, SCM_ARG2, "tortoise-color");
    SCM_ASSERT((scm_b), scm_b, SCM_ARG3, "tortoise-color");
    ar = (int)scm_to_int(scm_r);
    ag = (int)scm_to_int(scm_g);
    ab = (int)scm_to_int(scm_b);
    r= ar;
    g = ag;
    b = ab;
    DPRINTF_D("(tortoise_color r)",ar);
    DPRINTF_D("(tortoise_color g)",ag);
    DPRINTF_D("(tortoise_color b)",ab);
//    return SCM_MAKINUM(prevValuer);
    return scm_from_int(prevValuer);
 
}//tortoise_color
//---------------------------tortoise_move-------------------------------------------------------------------
extern "C" SCM tortoise_move(SCM  s_steps)
{
double oldX = currentX;
double oldY = currentY;
double newX, newY;
double steps;
 
    SCM_ASSERT(SCM_NUMBERP(s_steps), s_steps, SCM_ARG1, "tortoise-move");
    steps = scm_to_double(s_steps);
    DPRINTF_F("(tortoise_move )",steps);
//  first work out the new endpoint
    newX = currentX + sin(currentDirection*DEGREES_TO_RADIANS)*steps;
    newY = currentY - cos(currentDirection*DEGREES_TO_RADIANS)*steps;
//  if the pen is down, draw a line
//  if (penDown)
//  {
         DPRINTF("tortoise_move with pen down");
 
//        DPRINTF_P("draw hwnd",hwnd);
          Ghdc = BeginPaint (hwnd, &Gps) ;
          Ghdc = GetDC (hwnd) ;
//        DPRINTF_P("draw hdc",Ghdc);
//        DPRINTF_P("draw ps",&Gps);
          GetClientRect (hwnd, &Grc) ;
//          r= (rand () % 156+100);g=(rand () % 156+100);b=(rand () % 156+100);
//          r= 255;g=255;b=255;
          SetCapture (hwnd) ;
          SetTextColor(Ghdc, RGB (0, 0, 0)) ;
          SetTextColor(hdcMem, RGB (0, 0, 0)) ;
          GlinePen = CreatePen( PS_SOLID, 0, RGB (r, g, b)) ;//PS_DOT
          SelectObject(Ghdc, GlinePen) ;
          SelectObject(hdcMem, GlinePen) ;
          //SelectObject (hdc, GetStockObject (GRAY_BRUSH)) ;
          GhBrush = CreateSolidBrush (RGB (r, g, b)) ;
          SelectObject(Ghdc, GhBrush) ;
          SelectObject(hdcMem, GhBrush) ;
//        DPRINTF_R("draw line",(int)currentX,(int)currentY,(int)newX,(int)newY);
          wsprintf (GszBuffer, TEXT ("(0,0)")) ;
          DrawText (Ghdc, GszBuffer, -1, &Grc, DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;//JRG **need to add xscrl and yscrl
          DrawText (hdcMem, GszBuffer, -1, &Grc, DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
          MoveToEx (Ghdc,   (int)currentX+xscrl, (int)currentY +yscrl, NULL) ;
          MoveToEx (hdcMem,   (int)currentX, (int)currentY , NULL) ;
          LineTo   (Ghdc,   (int)newX+xscrl, (int)newY +xscrl) ;         //
          LineTo   (hdcMem,   (int)newX, (int)newY ) ;       //
          SetCapture (NULL) ;
          EndPaint (hwnd, &Gps) ;
          ReleaseDC (hwnd, Ghdc) ;
          DeleteObject (GhBrush) ;
          DeleteObject (GlinePen) ;
//  }
// in either case, move the tortoise
    currentX = newX;
    currentY = newY;
//    InvalidateRect (hwnd, NULL, TRUE) ;
//    SendMessage(hwnd,WM_PAINT,NULL,NULL);
//  DEPRECIATED gh_double2scm   USE  scm_make_real
//DEPRECIATED gh_cons USE scm_cons
//    return     scm_cons(scm_make_real (oldX) , scm_cons(scm_make_real (oldY), SCM_EOL));
    return     scm_cons(scm_from_double (oldX) , scm_cons(scm_from_double (oldY), SCM_EOL));
 
}//tortoise_move
//----------------------------------tortoise_jump_to---------------------------------------------------------
extern "C" SCM  tortoise_jump_to(SCM  s_pos_x, SCM  s_pos_y)
{
double newX, newY;
 
 SCM_ASSERT(SCM_NUMBERP(s_pos_x), s_pos_x, SCM_ARG1, "tortoise_jump_to");
    newX = scm_to_double(s_pos_x);
    SCM_ASSERT(SCM_NUMBERP(s_pos_y), s_pos_x, SCM_ARG2, "tortoise_jump_to");
    newY = scm_to_double(s_pos_y);
    currentX = newX;
    currentY = newY;
    return SCM_UNSPECIFIED;
}//tortoise_jump_to
//--------------------------------tortoise_set_pixel---------------------------------------------------------
extern "C" SCM  tortoise_set_pixel(SCM  s_pos_x,SCM  s_pos_y)
{
double newX, newY;
 
 SCM_ASSERT(SCM_NUMBERP(s_pos_x), s_pos_x, SCM_ARG1, "tortoise_set_pixel");
    newX = scm_to_double(s_pos_x);
    SCM_ASSERT(SCM_NUMBERP(s_pos_y), s_pos_x, SCM_ARG2, "tortoise_set_pixel");
    newY = scm_to_double(s_pos_y);
 
//    Ghdc = GetDC (hwnd) ;
//    SetPixel(
//      Ghdc ,
//      newX,
//      newY,
//      RGB(r,g,b)
//    );
 
    Ghdc = GetDC (hwnd) ;
    SetPixel(
      hdcMem ,
      newX,
      newY,
      RGB(r,g,b)
    );
 
    ReleaseDC (hwnd, Ghdc) ;
    currentX = newX;
    currentY = newY;
 return SCM_UNSPECIFIED;
}//tortoise_set_pixel
//-----------------------------tortoise_get_pixel------------------------------------------------------------
extern "C" SCM tortoise_get_pixel (SCM  s_pos_x,SCM  s_pos_y)
{
double newX, newY;
COLORREF  pixelColor;
 
 SCM_ASSERT(SCM_NUMBERP(s_pos_x), s_pos_x, SCM_ARG1, "tortoise_get_pixel");
    newX = scm_to_double(s_pos_x);
    SCM_ASSERT(SCM_NUMBERP(s_pos_y), s_pos_x, SCM_ARG2, "tortoise_get_pixel");
    newY = scm_to_double(s_pos_y);
 
    Ghdc = GetDC (hwnd) ;
 pixelColor = GetPixel(
   Ghdc,
   newX,
   newY
 );
 ReleaseDC (hwnd, Ghdc) ;
 r = GetRValue(pixelColor);
 g = GetGValue(pixelColor);
    b = GetBValue(pixelColor);
    printf("colour is r[%d],g[%d],b[%d]\n",r,g,b);
 
    GetLargestDisplayMode (&cxBitmap, &cyBitmap) ;
    printf("largest is x[%d],y[%d]\n",cxBitmap,cyBitmap);
//    currentX = newX;
//    currentY = newY;
 return SCM_UNSPECIFIED;
}//tortoise_get_pixel
//-------------------------------------register_procs--------------------------------------------------------
extern "C" void register_procs(void)
{
    DPRINTF("register_procs\n");
//  DEPRECIATED gh_new_procedure USE  scm_c_define_gsubr
/*
    gh_new_procedure("tortoise-reset"  ,                             tortoise_reset  , 0, 0, 0);
    gh_new_procedure("tortoise-cls"    ,                             tortoise_cls    , 0, 0, 0);
    gh_new_procedure("tortoise-pendown",                             tortoise_pendown, 0, 0, 0);
    gh_new_procedure("tortoise-penup"  ,                             tortoise_penup  , 0, 0, 0);
    gh_new_procedure("tortoise-turn"   ,   (scm_unused_struct*(*)()) tortoise_turn   , 1, 0, 0);
    gh_new_procedure("tortoise-move"   ,   (scm_unused_struct*(*)()) tortoise_move   , 1, 0, 0);
    gh_new_procedure("tortoise-color"  ,   (scm_unused_struct*(*)()) tortoise_color  , 3, 0, 0);
//alias
        gh_new_procedure("r"           ,                             tortoise_reset  , 0, 0, 0);
     gh_new_procedure("x"           ,                             tortoise_cls    , 0, 0, 0);
     gh_new_procedure("d"           ,                             tortoise_pendown, 0, 0, 0);
     gh_new_procedure("u"           ,                             tortoise_penup  , 0, 0, 0);
     gh_new_procedure("t"           ,   (scm_unused_struct*(*)()) tortoise_turn   , 1, 0, 0);
     gh_new_procedure("m"           ,   (scm_unused_struct*(*)()) tortoise_move   , 1, 0, 0);
        gh_new_procedure("c"           ,   (scm_unused_struct*(*)()) tortoise_color  , 3, 0, 0);
*/
 

//-------------- C function name  -------------------------------------+
//                                                                     |
//-------------- rest arguments ---------------------------+           |
//                                                         |           |
//-------------- optinal arguments ---------------------+  |           |
//                                                      |  |           |
//-------------- number of SCM arguments ------------+  |  |           |
//                                                   |  |  |           |
//                                                   V  V  V           V
    scm_c_define_gsubr("tortoise-reset"            , 0, 0, 0, (void *) tortoise_reset);
    scm_c_define_gsubr("tortoise-cls"              , 0, 0, 0, (void *) tortoise_cls);
    scm_c_define_gsubr("tortoise-pendown"          , 0, 0, 0, (void *) tortoise_pendown);
    scm_c_define_gsubr("tortoise-penup"            , 0, 0, 0, (void *) tortoise_penup);
    scm_c_define_gsubr("tortoise-turn"             , 1, 0, 0, (void *) tortoise_turn);
    scm_c_define_gsubr("tortoise-move"             , 1, 0, 0, (void *) tortoise_move);
    scm_c_define_gsubr("tortoise-color"            , 3, 0, 0, (void *) tortoise_color);
    scm_c_define_gsubr("tortoise-jump-to"          , 2, 0, 0, (void *) tortoise_jump_to);
    scm_c_define_gsubr("tortoise-reset-direction"  , 0, 0, 0, (void *) tortoise_reset_direction);
    scm_c_define_gsubr("tortoise-set-pixel"        , 2, 0, 0, (void *) tortoise_set_pixel);
    scm_c_define_gsubr("tortoise-get-pixel"        , 2, 0, 0, (void *) tortoise_get_pixel);
    scm_c_define_gsubr("floodfill"                 , 2, 0, 0, (void *) floodfill);
 
    scm_c_define_gsubr("draw-text"                 , 3, 0, 0, (void *) draw_text);
//    draw_text(SCM s_text, SCM  s_pos_x, SCM  s_pos_y)
 
//alias
        scm_c_define_gsubr("r"           , 0, 0, 0, (void *) tortoise_reset);
     scm_c_define_gsubr("x"           , 0, 0, 0, (void *) tortoise_cls);
     scm_c_define_gsubr("d"           , 0, 0, 0, (void *) tortoise_pendown);
     scm_c_define_gsubr("u"           , 0, 0, 0, (void *) tortoise_penup);
     scm_c_define_gsubr("t"           , 1, 0, 0, (void *) tortoise_turn);
     scm_c_define_gsubr("m"           , 1, 0, 0, (void *) tortoise_move);
        scm_c_define_gsubr("c"           , 3, 0, 0, (void *) tortoise_color);
     scm_c_define_gsubr("j"           , 2, 0, 0, (void *) tortoise_jump_to);
     scm_c_define_gsubr("rd"          , 0, 0, 0, (void *) tortoise_reset_direction);
        scm_c_define_gsubr("sp"          , 2, 0, 0, (void *) tortoise_set_pixel);
        scm_c_define_gsubr("gp"          , 2, 0, 0, (void *) tortoise_get_pixel);
        scm_c_define_gsubr("f"           , 2, 0, 0, (void *) floodfill);
        scm_c_define_gsubr("dt"          , 3, 0, 0, (void *) draw_text);
 
    return;
}//register_procs
//---------------------------------read_config_file----------------------------------------------------------
void read_config_file(void)
{
char *homedir;
char *filename;
 
//  build the $HOME/.tortoise filename
    homedir = getenv("HOME");
//  DPRINTF_S("read_config_file","homedir [%s]\n",homedir);
    if (homedir == NULL) return;
    filename = (char *)malloc(strlen(homedir) + strlen(CONFIGFILENAME) + strlen(DIRECTORYSEPARATOR) + 1);
    if (filename == NULL) return;
    sprintf(filename, "%s%s%s", homedir, DIRECTORYSEPARATOR, CONFIGFILENAME);
 
//    get Guile to do all of the work
//    DPRINTF_S("filename \n",filename);
//    DPRINTF((const char *)filename);
printf("filename[%s]\n",filename);
    DPRINTF("scm_cons ");
//  DEPRECIATED gh_eval_file USE scm_cons
//  gh_eval_file ("C:/Dev-Cpp/Examples/twig/.tortoise");
//  scm_cons ((scm_unused_struct*)"C:/Dev-Cpp/Examples/twig/.tortoise",NULL);
//  scm_cons ((scm_unused_struct*)"../.tortoise",NULL);
//  Load the scheme function definitions
// scm_c_primitive_load ("../.tortoise");
    free(filename);
 
//  How to get the value of a Scheme variable from C (hint: use gh_lookup)
//  How to prevent an incorrect .tortoise file from terminating your program (hint: use gh_catch)
}//read_config_file
//-------------------------------tortoise_reset1-------------------------------------------------------------
void tortoise_reset1(void)
{
    DPRINTF("(tortoise_reset1)");
    currentX = cxBitmap/2;
    currentY = cyBitmap/2;
    currentDirection = 0.0;
    penDown = 1;
}//tortoise_reset1
//-----------------------------tortoise_pendown1-------------------------------------------------------------
void tortoise_pendown1(void)
{
    DPRINTF("(tortoise_pendown1)");
    penDown = 1;
}//tortoise_pendown1
//------------------------------tortoise_penup1--------------------------------------------------------------
void tortoise_penup1(void)
{
    DPRINTF("(tortoise_penup1)");
    penDown = 0;
}//tortoise_penup1
//------------------------------tortoise_turn1---------------------------------------------------------------
void tortoise_turn1(int degrees)
{
    currentDirection += (double)degrees;
}//tortoise_turn1
//-------------------------------tortoise_move1--------------------------------------------------------------
 void tortoise_move1(int steps)
{
double newX, newY;
// first work out the new endpoint
    newX = currentX + sin(currentDirection*DEGREES_TO_RADIANS)*(double)steps;
    newY = currentY - cos(currentDirection*DEGREES_TO_RADIANS)*(double)steps;
// if the pen is down, draw a line
//    if (penDown)
//   {
          DPRINTF("pen down");
//        XDrawLine(theDisplay, theWindow, theGC, (int)currentX, (int)currentY, (int)newX, (int)newY);
 
          DPRINTF_P("draw hwnd",hwnd);
          Ghdc =  GetDC (hwnd) ;
          GetClientRect (hwnd, &Grc) ;
          r = (rand () % 156+100);g=(rand () % 156+100);b=(rand () % 156+100);
          SetTextColor(Ghdc, RGB (r, g, b)) ;
//        SetTextColor(hdcMem, RGB (r, g, b)) ;
          GlinePen = CreatePen( PS_DOT, 0, RGB (r, g, b)) ;
          SelectObject(Ghdc, GlinePen) ;
//        SelectObject(hdcMem, GlinePen) ;
//        SelectObject (hdc, GetStockObject (GRAY_BRUSH)) ;
          GhBrush = CreateSolidBrush (RGB (r, g, b)) ;
          SelectObject(Ghdc, GhBrush) ;
//        SelectObject(hdcMem, GhBrush) ;
//        DPRINTF_R("draw line",(int)currentX,(int)currentY,(int)newX,(int)newY);
          wsprintf (GszBuffer, TEXT ("(1,1)")) ;
//        SetCapture (hwnd) ;
          DrawText (Ghdc, GszBuffer, -1, &Grc, DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
//        DrawText (hdcMem, GszBuffer, -1, &Grc, DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
          MoveToEx (Ghdc,   (int)currentX, (int)currentY , NULL) ;
//        MoveToEx (hdcMem, (int)currentX, (int)currentY , NULL) ;
          LineTo   (Ghdc,   (int)newX, (int)newY ) ;
//        LineTo   (hdcMem, (int)newX, (int)newY ) ;
//        SetCapture (NULL) ;
 
          ReleaseDC (hwnd, Ghdc) ;
          DeleteObject (GhBrush) ;
          DeleteObject (GlinePen) ;
//   }
// in either case, move the tortoise
    currentX = newX;
    currentY = newY;
}//tortoise_move1
//-------------------------------------draw_text-------------------------------------------------
extern "C" SCM draw_text(SCM s_text, SCM  s_pos_x, SCM  s_pos_y)
{
double newX, newY;
//COLORREF  pixelColor;
TCHAR  tchBuffer1[200] ;                        // buffer for expanded string
char * buff;
 
//char string[150] = "How are you?";
//TCHAR wstring[150];
//MultiByteToWideChar(CP_ACP, 0, string, -1,  wstring, 150);
 

 SCM_ASSERT(SCM_STRINGP(s_text), s_text, SCM_ARG1, "draw_text");
// SCM_ASSERT_STRING (s_text, SCM_ARG1, "draw_text");
    buff = scm_to_locale_string (s_text);//scm_to_locale_string scm_to_string
    MultiByteToWideChar(CP_ACP, 0, buff, -1,  tchBuffer1, 150);
 
 SCM_ASSERT(SCM_NUMBERP(s_pos_x), s_pos_x, SCM_ARG2, "draw_text");
    newX = scm_to_double(s_pos_x);
    SCM_ASSERT(SCM_NUMBERP(s_pos_y), s_pos_y, SCM_ARG3, "draw_text");
    newY = scm_to_double(s_pos_y);
    Ghdc =  GetDC (hwnd) ;
 
//    wsprintf (tchBuffer1,  ( const WCHAR *)buff) ;
    TextOut (Ghdc, newX - xscrl, newY - yscrl, tchBuffer1,wcslen(tchBuffer1)) ;
    TextOut (hdcMem, newX , newY, tchBuffer1,wcslen(tchBuffer1)) ;
    ReleaseDC (hwnd, Ghdc) ;
 
 return SCM_UNSPECIFIED;
}//draw_text
//--------------------------CtrlHandler----------------------------------------------------------------------
BOOL CtrlHandler( DWORD fdwCtrlType )
{
  switch( fdwCtrlType )
  {
    // Handle the CTRL-C signal.
    case CTRL_C_EVENT:
      printf( "Ctrl-C event\n\n" );
      Beep( 750, 300 );
      return( TRUE );
 
    // CTRL-CLOSE: confirm that the user wants to exit.
    case CTRL_CLOSE_EVENT:
      Beep( 600, 200 );
      printf( "Ctrl-Close event\n\n" );
      return( TRUE );
 
    // Pass other signals to the next handler.
    case CTRL_BREAK_EVENT:
      Beep( 900, 200 );
      printf( "Ctrl-Break event\n\n" );
      return FALSE;
 
    case CTRL_LOGOFF_EVENT:
      Beep( 1000, 200 );
      printf( "Ctrl-Logoff event\n\n" );
      return FALSE;
 
    case CTRL_SHUTDOWN_EVENT:
      Beep( 750, 500 );
      printf( "Ctrl-Shutdown event\n\n" );
      return FALSE;
 
    default:
      return FALSE;
  }
}//CtrlHandler
//-------------------------------------GetLargestDisplayMode-------------------------------------------------
void GetLargestDisplayMode (int * pcxBitmap, int * pcyBitmap)
{
     DEVMODE devmode ;
     int     iModeNum = 0 ;
 
     * pcxBitmap = * pcyBitmap = 0 ;
 
     ZeroMemory (&devmode, sizeof (DEVMODE)) ;
     devmode.dmSize = sizeof (DEVMODE) ;
 
     while (EnumDisplaySettings (NULL, iModeNum++, &devmode))
     {
          * pcxBitmap = MAX (* pcxBitmap, (int) devmode.dmPelsWidth) ;
          * pcyBitmap = MAX (* pcyBitmap, (int) devmode.dmPelsHeight) ;
     }
}//GetLargestDisplayMode
//----------------------------floodfill----------------------------------------------------------------------
extern "C" SCM  floodfill(SCM  s_pos_x, SCM  s_pos_y)// cheap and cheerful fill
{
int newX, newY;
register int il,ir,it,ib;
 
 SCM_ASSERT(SCM_NUMBERP(s_pos_x), s_pos_x, SCM_ARG1, "floodfill");
    newX = scm_to_int(s_pos_x);
    SCM_ASSERT(SCM_NUMBERP(s_pos_y), s_pos_x, SCM_ARG2, "floodfill");
    newY = scm_to_int(s_pos_y);
 
  printf("start[%d][%d]\n",newX,newY);
  Ghdc = GetDC (hwnd) ;
//  Ghdc = BeginPaint (hwnd, &Gps) ;
 
  if(GetRValue(GetPixel(Ghdc,newX,newY)) == 0 && GetGValue(GetPixel(Ghdc,newX,newY)) == 0 && GetBValue(GetPixel(Ghdc,newX,newY)) == 0)
  {
   for (ib=newY+1; ib<cyBitmap; ib++)
   {
  if(GetRValue(GetPixel(Ghdc,newX,ib)) != 0 || GetGValue(GetPixel(Ghdc,newX,ib)) != 0 || GetBValue(GetPixel(Ghdc,newX,ib)) != 0) break;
 
    if(GetRValue(GetPixel(Ghdc,newX,ib)) != 0 || GetGValue(GetPixel(Ghdc,newX,ib)) != 0 || GetBValue(GetPixel(Ghdc,newX,ib)) != 0) break;
 for (ir=newX+1; ir<cxBitmap; ir++)
 {
      if(GetRValue(GetPixel(Ghdc,ir,ib)) == 0 && GetGValue(GetPixel(Ghdc,ir,ib)) == 0 && GetBValue(GetPixel(Ghdc,ir,ib)) == 0)
      {
//   printf("set[%d][%d] ",ir,it);
//     SetPixel(Ghdc ,ir,ib,RGB(255,255,0));
     SetPixel(hdcMem ,ir,ib,RGB(255,255,0));
      }
     else
     {
//      printf("ir break[%d]\n",ir);
     break;
     }
 }//for ir
    for (il=newX; il>0; il--)
    {
      if(GetRValue(GetPixel(Ghdc,il,ib)) == 0 && GetGValue(GetPixel(Ghdc,il,ib)) == 0 && GetBValue(GetPixel(Ghdc,il,ib)) == 0)
      {
//   printf("set[%d][%d] ",il,it);
//     SetPixel(Ghdc ,il,ib,RGB(255,255,0));
     SetPixel(hdcMem ,il,ib,RGB(255,255,0));
      }
     else
     {
//      printf("il break[%d]\n",il);
     break;
     }
    }//for il
   }//for ib
 
   for (it=newY; it>0; it--)
   {
    if(GetRValue(GetPixel(Ghdc,newX,it)) != 0 || GetGValue(GetPixel(Ghdc,newX,it)) != 0 || GetBValue(GetPixel(Ghdc,newX,it)) != 0) break;
 for (ir=newX+1; ir<cxBitmap; ir++)
 {
      if(GetRValue(GetPixel(Ghdc,ir,it)) == 0 && GetGValue(GetPixel(Ghdc,ir,it)) == 0 && GetBValue(GetPixel(Ghdc,ir,it)) == 0)
      {
//   printf("set[%d][%d] ",ir,it);
//     SetPixel(Ghdc ,ir,it,RGB(255,255,0));
     SetPixel(hdcMem ,ir,it,RGB(255,255,0));
      }
     else
     {
//      printf("ir break[%d]\n",ir);
     break;
     }
 }//for ir
    for (il=newX; il>0; il--)
    {
      if(GetRValue(GetPixel(Ghdc,il,it)) == 0 && GetGValue(GetPixel(Ghdc,il,it)) == 0 && GetBValue(GetPixel(Ghdc,il,it)) == 0)
      {
//   printf("set[%d][%d] ",il,it);
//     SetPixel(Ghdc ,il,it,RGB(255,255,0));
     SetPixel(hdcMem ,il,it,RGB(255,255,0));
      }
     else
     {
//      printf("il break[%d]\n",il);
     break;
     }
    }//for il
   }//for it
  }//if
//+++++++++++++++
  if(GetRValue(GetPixel(Ghdc,newX,newY)) == 0 && GetGValue(GetPixel(Ghdc,newX,newY)) == 0 && GetBValue(GetPixel(Ghdc,newX,newY)) == 0)
  {
   for (ib=newX+1; ib<cxBitmap; ib++)
   {
  if(GetRValue(GetPixel(Ghdc,ib,newY)) != 0 || GetGValue(GetPixel(Ghdc,ib,newY)) != 0 || GetBValue(GetPixel(Ghdc,ib,newY)) != 0) break;
 
    if(GetRValue(GetPixel(Ghdc,ib,newY)) != 0 || GetGValue(GetPixel(Ghdc,ib,newY)) != 0 || GetBValue(GetPixel(Ghdc,ib,newY)) != 0) break;
 for (ir=newY+1; ir<cyBitmap; ir++)
 {
      if(GetRValue(GetPixel(Ghdc,ib,ir)) == 0 && GetGValue(GetPixel(Ghdc,ib,ir)) == 0 && GetBValue(GetPixel(Ghdc,ib,ir)) == 0)
      {
//   printf("set[%d][%d] ",ir,it);
//     SetPixel(Ghdc ,ib,ir,RGB(255,0,255));
     SetPixel(hdcMem ,ib,ir,RGB(255,0,255));
      }
     else
     {
//      printf("ir break[%d]\n",ir);
     break;
     }
 }//for ir
    for (il=newY; il>0; il--)
    {
      if(GetRValue(GetPixel(Ghdc,ib,il)) == 0 && GetGValue(GetPixel(Ghdc,ib,il)) == 0 && GetBValue(GetPixel(Ghdc,ib,il)) == 0)
      {
//   printf("set[%d][%d] ",il,it);
//     SetPixel(Ghdc ,ib,il,RGB(255,0,255));
     SetPixel(hdcMem ,ib,il,RGB(255,0,255));
      }
     else
     {
//      printf("il break[%d]\n",il);
     break;
     }
    }//for il
   }//for ib
 
   for (it=newX; it>0; it--)
   {
    if(GetRValue(GetPixel(Ghdc,it,newY)) != 0 || GetGValue(GetPixel(Ghdc,it,newY)) != 0 || GetBValue(GetPixel(Ghdc,it,newY)) != 0) break;
 for (ir=newY+1; ir<cyBitmap; ir++)
 {
      if(GetRValue(GetPixel(Ghdc,it,ir)) == 0 && GetGValue(GetPixel(Ghdc,it,ir)) == 0 && GetBValue(GetPixel(Ghdc,it,ir)) == 0)
      {
//   printf("set[%d][%d] ",ir,it);
//     SetPixel(Ghdc ,it,ir,RGB(255,0,255));
     SetPixel(hdcMem ,it,ir,RGB(255,0,255));
      }
     else
     {
//      printf("ir break[%d]\n",ir);
     break;
     }
 }//for ir
    for (il=newY; il>0; il--)
    {
      if(GetRValue(GetPixel(Ghdc,it,il)) == 0 && GetGValue(GetPixel(Ghdc,it,il)) == 0 && GetBValue(GetPixel(Ghdc,it,il)) == 0)
      {
//   printf("set[%d][%d] ",il,it);
//     SetPixel(Ghdc ,it,il,RGB(255,0,255));
     SetPixel(hdcMem ,it,il,RGB(255,0,255));
      }
     else
     {
//      printf("il break[%d]\n",il);
     break;
     }
    }//for il
   }//for it
  }//if
//    EndPaint (hwnd, &Gps) ;
 ReleaseDC (hwnd, Ghdc);
 InvalidateRect (hwnd, NULL, TRUE) ;
 return SCM_UNSPECIFIED;
}//floodfill
//**************************end of file**********************************************************************

reply via email to

[Prev in Thread] Current Thread [Next in Thread]