Author Topic: TCLib update for Patrice  (Read 11903 times)

0 Members and 1 Guest are viewing this topic.

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 672
Re: TCLib update for Patrice
« Reply #15 on: March 09, 2017, 07:45:16 PM »
Patrice,
  You must have an include file somewhere including <stdlib.h> .
I'll try renaming them to see if that helps.
James

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 672
Re: TCLib update for Patrice
« Reply #16 on: March 09, 2017, 07:53:14 PM »
Patrice,
  This is the test code I used for the math functions.

James

Code: [Select]
// *********************************************************************
//  Created with bc9Basic - BASIC To C/C++ Translator (V) 9.2.6.0 (2017/02/05)
//       The bc9Basic translator (bc9.exe) was compiled with
//                           g++ (tdm64-1) 5.1.0
// ----------------------------------------------------------------------
//                 BCX (c) 1999 - 2009 by Kevin Diggins
// *********************************************************************
//              Translated for compiling with the
//           Microsoft (R) C/C++ Optimizing Compiler
//                           On MS Windows
//                    Using TCLib by Fred Harris
// *********************************************************************
#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#define _X(y) y
#include <windows.h>
#define   x64
#include "TCLib\stdio.h"
#include "TCLib\string.h"
#include "TCLib\stdlib.h"
#include "TCLib\memory.h"
#include "TCLib\malloc.h"
#include "TCLib\math.h"
#include "TCLib\tchar.h"
#include "TCLib\Strings.cpp"
typedef String fstring;
FILE* stdin;
FILE* stdout;
FILE* stderr;
//<---UNICODE AWARE
#define SFMT (const char*)"%s\r\n"
//>---UNICODE AWARE

// NO HEADERS START
#ifndef _WINDOWS_
//<---UNICODE AWARE
typedef _TCHAR *PCHAR, *LPCH, *PCH, *NPSTR, *LPSTR, *PSTR;
typedef unsigned long DWORD, *PDWORD, *LPDWORD;
typedef unsigned int UINT;
//>---UNICODE AWARE
#endif
// NO HEADERS END

// ***************************************************
// Compiler Defines
// ***************************************************

// C++
#if defined( __cplusplus )
#define overloaded
#define C_EXPORT EXTERN_C __declspec(dllexport)
#define C_IMPORT EXTERN_C __declspec(dllimport)
#define BEGIN_EXTERN_C extern _T("C") {
#define END_EXTERN_C }
#else
#define C_EXPORT __declspec(dllexport)
#define C_IMPORT __declspec(dllimport)
#endif


// Microsoft VC++
#ifndef DECLSPEC_UUID
#if (_MSC_VER >= 1100) && defined ( __cplusplus )
#define DECLSPEC_UUID(x)    __declspec(uuid(x))
#else
#define DECLSPEC_UUID(x)
#endif
#endif

// ***************************************************
// Compiler Defines
// ***************************************************
#ifndef __cplusplus
#error A C++ compiler is required
#endif

// *************************************************
//        User's GLOBAL ENUM blocks
// *************************************************

// *************************************************
//            System Defined Constants
// *************************************************

typedef const _TCHAR* ccptr;
#define CCPTR const _TCHAR*
#define cfree free
//<---UNICODE AWARE
typedef char _char;
#define _strlen strlen
//>---UNICODE AWARE
#define EQU ==
#define NOT_USED(x) if(x);
#define CTLHNDL(id) GetDlgItem(hWnd,id)
#define cSizeOfDefaultString 2048

// *************************************************
//            User Defined Constants
// *************************************************

#define PI 3.141592653589753
#define TR 57.2957795130831

// *************************************************
//               Standard Prototypes
// *************************************************

_TCHAR*   BCX_TmpStr(size_t, size_t = 0, int = 1);
void    Pause (void);

// *************************************************
//                System Variables
// *************************************************

#define LDOUBLE long double
// *************************************************
//          User Defined Types, Unions and Classes
// *************************************************


// *************************************************
//            User Global Variables
// *************************************************



// *************************************************
//               User Prototypes
// *************************************************

int     _tmain (void);

// *************************************************
//            User Global Initialized Arrays
// *************************************************



// *************************************************
//                 Runtime Functions
// *************************************************

#ifndef BCXTmpStrSize
#define BCXTmpStrSize  2048
#endif
_TCHAR *BCX_TmpStr (size_t Bites, size_t  iPad, int iAlloc)
{
    static int   StrCnt;
    static _TCHAR *StrFunc[BCXTmpStrSize];
    StrCnt = (StrCnt + 1) & (BCXTmpStrSize - 1);
    if(StrFunc[StrCnt]) {
        free (StrFunc[StrCnt]);
        StrFunc[StrCnt] = NULL;
    }
#if defined BCX_MAX_VAR_SIZE
    if(Bites * sizeof(_TCHAR) > BCX_MAX_VAR_SIZE)
    {
        _tprintf(_T("Buffer Overflow caught in BCX_TmpStr - requested space of %d EXCEEDS %d\n"), (int)(Bites * sizeof(_TCHAR)), BCX_MAX_VAR_SIZE);
        abort();
    }
#endif
    if(iAlloc) StrFunc[StrCnt] = (_TCHAR*)calloc(Bites + iPad + 1, sizeof(_TCHAR));
    return StrFunc[StrCnt];
}


void Pause(void)
{
    _tprintf(_T("\n%ls\n"), _T("Press any key to continue..."));
    _getwch();
}



// *************************************************
//       User Subs, Functions and Class Methods
// *************************************************

int _tmain ()
{
    LDOUBLE  ld = {0};
    double   d1 = {0};
    float    f1 = {0};
    int      I = {0};
    ld = sin( 45 / TR);
    _tprintf(_T("%.19LG\n"), (LDOUBLE)ld);
    I = sin( 45 / TR);
    d1 = sin( 45 / TR);
    f1 = sin( 45 / TR);
    _tprintf(_T("%d\n"), (int)I);
    _tprintf(_T("%.15G\n"), (double)d1);
    _tprintf(_T("%.7G\n"), (float)f1);
    ld = cos( 45 / TR);
    _tprintf(_T("%.19LG\n"), (LDOUBLE)ld);
    ld = acos( 45 / TR);
    d1 = sqrt( 144);
    _tprintf(_T("%.15G\n"), (double)d1);
    I = time( NULL);
    _tprintf(_T("%ls%d\n"), _T("I = "), (int)I);
    _tprintf(_T("%ls\n"), _T("Hello There"));
    I = rand();
    _tprintf(_T("%ls%d\n"), _T("I = "), (int)I);
    srand((DWORD)time(NULL));
    d1 = fmod( 5.3, 2);
    _tprintf(_T("%ls%.15G\n"), _T("d1 = "), (double)d1);
    f1 = fmodf( 5.3, 2);
    _tprintf(_T("%ls%.7G\n"), _T("f1 = "), (float)f1);
    Pause();
}





Offline Mike Lobanovsky

  • Jr. Member
  • **
  • Posts: 95
Re: TCLib update for Patrice
« Reply #17 on: March 09, 2017, 08:40:35 PM »
#define RAND_MAX 0x7fff belongs to stdlib.h so it should be defined explicitly elsewhere since stdlib.h is eliminated altogether.
Mike
(3.6GHz Intel Core i5 w/ 16GB RAM, 2 x GTX 650Ti w/ 2GB VRAM, Windows 7 Ultimate Sp1)

Offline Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2003
    • www.zapsolution.com
Re: TCLib update for Patrice
« Reply #18 on: March 09, 2017, 10:02:30 PM »
James

The main difference between you and me is that you don't use the IDE.

I have attached the full VS 2015 community project to create the 64-bit OpenGL bbp_Laserbeam.dll (13312 bytes) to be used with the latest MBox64.exe player.

I shall try to convert the other plugins, once the rand() and srand() problem will be sorted out ;)

Thank you for helping me to produce this tiny visual plugin.
As a matter of comparison the size of the PowerBASIC 32-bit LaserBeam.dll plugin is 35328 bytes, that means 2.65 bigger, i am very happy!
« Last Edit: March 09, 2017, 10:05:49 PM by Patrice Terrier »
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 672
Re: TCLib update for Patrice
« Reply #19 on: March 09, 2017, 11:44:50 PM »
Patrice,
  Try this one.
James

Offline Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2003
    • www.zapsolution.com
Re: TCLib update for Patrice
« Reply #20 on: March 10, 2017, 12:21:09 PM »
Fred

Thanks for the rand and rnd functions, i didn't see your post until today.
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2003
    • www.zapsolution.com
Re: TCLib update for Patrice
« Reply #21 on: March 10, 2017, 02:24:54 PM »
Fred

What do you use to replace the srand function to seed the pseudorandom-number ?
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2003
    • www.zapsolution.com
Re: TCLib update for Patrice
« Reply #22 on: March 10, 2017, 06:03:18 PM »
Amazing

i have been able to reduce the size of most of my plugins by a factor ranging between 4 and 7 !!!
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline Frederick J. Harris

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 941
    • Frederick J. Harris
Re: TCLib update for Patrice
« Reply #23 on: March 10, 2017, 06:08:40 PM »
Patrice, I don't think I have any implementation for srand().  I had only run into the problem of coming up with a replacement for PowerBASIC's Rnd() function in converting a multi-threaded example I posted here some time ago...

http://www.jose.it-berater.org/smfforum/index.php?topic=5058.0

...from PowerBASIC to C++.  As you are probably aware, C's rand produces a rather limited range of integers.  I forget the exact range, but its rather limited.  For my example I needed numbers like PowerBASIC's Rnd() would generate, so I scaled them into an acceptable range.

Later, when I attempted to get that example working with my TCLib, I ran into the same problem you did, that is, I hadn't included in TCLib an implementation of rand() or srand().  So I hunted around on the internet a bit and found what I did for rand() and that by itself met my simple needs at the time.  The implementation seems to use integer overflow to get crazy numbers. 

Just off the top of my head, I think there are two solutions possible if you need rand() or srand().  First, you could hunt around the internet for an implementation of srand().  My guess is it wouldn't be too hard to come up with something.

The other possibility would be to simply include it within TCLib itself in the manner Jim seems to have done with the trig and other Math functions.  Its a fairly straightforward process.  As I've said before, the C Runtime is loaded into every GUI process.  All the C Runtime functions are there, including, I'm assuming, rand() and srand().  Its just a matter of calling LoadLibrary() to get the dll HINSTANCE, and calling GetProcAddress() to get a pointer to the function.  In many cases the function pointer can be named exactly the same as the function itself, and the usage is the same. 

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 672
Re: TCLib update for Patrice
« Reply #24 on: March 10, 2017, 06:21:55 PM »
Patrice,
  I noticed in your bbp_Laserbeam.cpp source you are not including TCLib\tchar.h.
This is a vital component that eliminates the duplicate definitions for rand srand.

James

« Last Edit: March 10, 2017, 06:23:35 PM by James C. Fuller »

Offline Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2003
    • www.zapsolution.com
Re: TCLib update for Patrice
« Reply #25 on: March 10, 2017, 06:27:34 PM »
Code: [Select]
LoadLibrary() to get the dll HINSTANCE, and calling GetProcAddress()
OF COURSE, that could be the easiest solution!
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 672
Re: TCLib update for Patrice
« Reply #26 on: March 10, 2017, 06:34:15 PM »
Fred,
  I have found that renaming the functions in the TCLib and then using tchar.h for the translation seems to work fine.
For rand and srand I just prefaced with an underscore but I am going to go back and preface everything with tcl_.

I found I needed fflush so :

tcl_fflush = (int (__cdecl*)(FILE* fp)) GetProcAddress(hLib,"fflush");

Then in tchar.h:
#define fflush tcl_fflush

James


Offline Mike Lobanovsky

  • Jr. Member
  • **
  • Posts: 95
Re: TCLib update for Patrice
« Reply #27 on: March 10, 2017, 08:43:36 PM »
... C's rand produces a rather limited range of integers.  I forget the exact range, but its rather limited.

The ISO standard declares a compatible C rand() function's exact range to be entirely compiler implementation dependent but states that it be at least 0 thru 32767 inclusive. As it happens, both VC and GCC define their RAND_MAX value as 0x7FFF, which is exactly 32767 decimal.
Mike
(3.6GHz Intel Core i5 w/ 16GB RAM, 2 x GTX 650Ti w/ 2GB VRAM, Windows 7 Ultimate Sp1)

Offline Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2003
    • www.zapsolution.com
Re: TCLib update for Patrice
« Reply #28 on: March 10, 2017, 08:55:38 PM »
Here is what i came with to simulate the PowerBASIC:
RANDOMIZE TIMER
Code: [Select]
typedef void (*FuncWithParams)(DWORD seed);
void RandoMize(IN DWORD seed) {
    HMODULE hModule = LoadLibrary(L"MSVCRT");
    if (hModule) {
        FuncWithParams hProc;
        hProc = (FuncWithParams) GetProcAddress(hModule, "srand");
        if (hProc) { hProc(seed); }
    }
}

void RandomizeTimer() {
//    //static long done;
//    //if (!done) { srand((DWORD) time(NULL)); done = !done; }
    //srand((DWORD) time(NULL));
    RandoMize((DWORD) time(NULL));
}
i shall post the source code of all the new TCLib plugins on www.objreader.com to render in Mbox64.exe

...
« Last Edit: March 10, 2017, 09:01:46 PM by Patrice Terrier »
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2003
    • www.zapsolution.com
Re: TCLib update for Patrice
« Reply #29 on: March 10, 2017, 10:02:02 PM »
i was missing the log2 function.
here is what i came with

Code: [Select]
#define M_LOG2E 1.44269504088896340736
#define double_proc typedef double (__stdcall *zProc)

HMODULE MSVCRT() {
    static HMODULE hModule;
    if (hModule == 0) { hModule = LoadLibrary(L"MSVCRT"); }
    return hModule;
}

double log2(IN double X) {
    double l2 = 0;
    HMODULE hModule = MSVCRT();
    if (hModule) {
        double_proc (double);
        zProc hProc = (zProc) GetProcAddress(hModule, "log");
        if (hProc) { l2 = hProc(X) * M_LOG2E; }
    }
    return l2;
}
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com