Jump to content

  • Log In with Google      Sign In   
  • Create Account


Prads

Member Since 27 May 2010
Offline Last Active Jun 22 2013 01:13 AM

#5037042 Writing my own programming language

Posted by Prads on 27 February 2013 - 02:26 AM

Hello, I have been working on this project for some months now and I
want to show it to other people. Basically speaking, it's a programming
language which can be compiled and ran using a virtual machine. Here's a
detailed explanation I wrote in my blog:


    
Quote:

            
                So, in last blog post I gave small introduction to my current
project that I am working on. In this blog I want to release the first
version of my current project. You can grab it from here: http://www.pradsprojects.com/despair.html

So, this piece of software that I wrote is a system to write portable
programs. It has its own programming language and the program that are
compiled can be executed using a Virtual Machine. The system isn’t
mature and lot of things are lacking but I hope to develop it further in
the future.

Let me talk a little about how this system works. You write a program
using its programming language (here’s the documentation of programming
language: http://www.pradsprojects.com/despair...ion/index.html)
then compile it using compiler tool that I have written. The compiler
generates a file with ‘.dbin’ extension which can be executed using the
Virtual Machine.

I have created a small IDE for developing program for this system
which you can download it from the project site (the one that I gave you
on the top). IDE consist of editor to write and manage source code
(which is fairly basic at the moment, no syntax highlighting for the
moment sorry), a compiler to compile the source code and a virtual
machine to run the compiled file.

Alternately, if you don’t want to create programs for the system but
want to run program created by others, you’re going to have to download
the VM from the project site. There are two types of VM for Windows OS:
interpreter version and JIT version (Dynamic Recompiler). The JIT
version is faster than interpreter version though I am still not very
happy with the JIT performance, I think it can be improved more. I will
port the JIT version to Linux later.

I want to create the Virtual Machine for various different platforms
like android, iOS, MacOS etc so that the program written using
despairLanguage can be run in different platforms. Currently the VM
works in Windows and Linux.

P.S: The software is open source and is on github. Links are on the project site.
            
        


So, I just wanted you guys to look at it and play around with it. Some feedbacks (good or bad) is highly appreciated.

Thanks!




#4830992 Which audio library is best to use with DirectX 9.0?

Posted by Prads on 04 July 2011 - 08:47 AM

Here's a class I made for you:

Header:

class waveSound {
private:
        IXAudio2SourceVoice *pSourceVoice;
        XAUDIO2_BUFFER SoundBuffer;
        unsigned char *wBuffer;

	HRESULT FindChunk(HANDLE hFile, DWORD fourcc, DWORD & dwChunkSize, DWORD & dwChunkDataPosition);
	HRESULT ReadChunkData(HANDLE hFile, void * buffer, DWORD buffersize, DWORD bufferoffset);

public:
        waveSound(LPCSTR waveFile, IXAudio2 *xAudio, HRESULT &hr);
        ~waveSound();

        void playSound();
};

Cpp code:

HRESULT waveSound::FindChunk(HANDLE hFile, DWORD fourcc, DWORD & dwChunkSize, DWORD & dwChunkDataPosition)
{
        HRESULT hr = S_OK;
        if( INVALID_SET_FILE_POINTER == SetFilePointer( hFile, 0, NULL, FILE_BEGIN ) )
                return HRESULT_FROM_WIN32( GetLastError() );

        DWORD dwChunkType;
        DWORD dwChunkDataSize;
        DWORD dwRIFFDataSize = 0;
        DWORD dwFileType;
        DWORD bytesRead = 0;
        DWORD dwOffset = 0;

        while (hr == S_OK)
        {
                DWORD dwRead;
                if( 0 == ReadFile( hFile, &dwChunkType, sizeof(DWORD), &dwRead, NULL ) )
                        hr = HRESULT_FROM_WIN32( GetLastError() );

                if( 0 == ReadFile( hFile, &dwChunkDataSize, sizeof(DWORD), &dwRead, NULL ) )
                        hr = HRESULT_FROM_WIN32( GetLastError() );

                switch (dwChunkType)
                {
                case fourccRIFF:
                        dwRIFFDataSize = dwChunkDataSize;
                        dwChunkDataSize = 4;
                        if( 0 == ReadFile( hFile, &dwFileType, sizeof(DWORD), &dwRead, NULL ) )
                                hr = HRESULT_FROM_WIN32( GetLastError() );
                        break;

                default:
                        if( INVALID_SET_FILE_POINTER == SetFilePointer( hFile, dwChunkDataSize, NULL, FILE_CURRENT ) )
                        return HRESULT_FROM_WIN32( GetLastError() );            
                }

                dwOffset += sizeof(DWORD) * 2;
                        
                if (dwChunkType == fourcc)
                {
                        dwChunkSize = dwChunkDataSize;
                        dwChunkDataPosition = dwOffset;
                        return S_OK;
                }

                dwOffset += dwChunkDataSize;
                        
                if (bytesRead >= dwRIFFDataSize) return S_FALSE;

        }   	

        return S_OK;            
}

HRESULT waveSound::ReadChunkData(HANDLE hFile, void * buffer, DWORD buffersize, DWORD bufferoffset)
{
        HRESULT hr = S_OK;
        if( INVALID_SET_FILE_POINTER == SetFilePointer( hFile, bufferoffset, NULL, FILE_BEGIN ) )
                return HRESULT_FROM_WIN32( GetLastError() );
        DWORD dwRead;
        if( 0 == ReadFile( hFile, buffer, buffersize, &dwRead, NULL ) )
                hr = HRESULT_FROM_WIN32( GetLastError() );
        return hr;
}

waveSound::waveSound(LPCSTR waveFile, IXAudio2 *pXAudio2, HRESULT &hr) {
	wBuffer = 0;

	// Open the file
	HANDLE hFile = CreateFile(
		waveFile,
        GENERIC_READ,
        FILE_SHARE_READ,
        NULL,
        OPEN_EXISTING,
        0,
        NULL );

	if( INVALID_HANDLE_VALUE == hFile ) {
		hr = HRESULT_FROM_WIN32( GetLastError() );
		return;
	}

    DWORD dwChunkSize;
    DWORD dwChunkPosition;
	WAVEFORMATEX wfx;

	//check the file type, should be fourccWAVE or 'XWMA'
	hr = FindChunk(hFile,fourccRIFF,dwChunkSize, dwChunkPosition );
	if (hr != S_OK) return;
	DWORD filetype;
	hr = ReadChunkData(hFile,&filetype,sizeof(DWORD),dwChunkPosition);
	if (hr != S_OK) return;

	hr = FindChunk(hFile,fourccFMT, dwChunkSize, dwChunkPosition );
	if (hr != S_OK) return;
	hr = ReadChunkData(hFile, &wfx, dwChunkSize, dwChunkPosition );
	if (hr != S_OK) return;

	hr = FindChunk(hFile,fourccDATA,dwChunkSize, dwChunkPosition );
	if (hr != S_OK) return;
	wBuffer = new unsigned char[dwChunkSize];
	hr = ReadChunkData(hFile, wBuffer, dwChunkSize, dwChunkPosition);
	if (hr != S_OK) return;

	ZeroMemory(&SoundBuffer, sizeof(SoundBuffer));
	SoundBuffer.AudioBytes = dwChunkSize;  //buffer containing audio data
	SoundBuffer.pAudioData = wBuffer;  //size of the audio buffer in bytes
	SoundBuffer.Flags = XAUDIO2_END_OF_STREAM;

	hr = pXAudio2->CreateSourceVoice( &pSourceVoice, &wfx,
		     0, XAUDIO2_DEFAULT_FREQ_RATIO, NULL, NULL, NULL );
	if (hr != S_OK) return;

	hr = S_OK;
}

waveSound::~waveSound() {
	if (wBuffer) delete [] wBuffer;
}

void waveSound::playSound() {
	XAUDIO2_VOICE_STATE xAState;
	pSourceVoice->GetState(&xAState);
	if (xAState.BuffersQueued == 0) {
		pSourceVoice->SubmitSourceBuffer(&SoundBuffer);
		pSourceVoice->Start();
	}
}

First load your sounds like this:

waveSound firstSound("1.wav", pXAudio2, hr);
if (hr != S_OK) {
	//Do some error handling
}

waveSound secondSound("2.wav", pXAudio2, hr);
if (hr != S_OK) {
	//Do some error handling
}

waveSound thirdSound("3.wav", pXAudio2, hr);
if (hr != S_OK) {
	//Do some error handling
}

//so on

Now when you need to play the sound:

firstSound.playSound();

secondSound.playSound();

thirdSound.playSound();

Good Luck!


#4830855 Which audio library is best to use with DirectX 9.0?

Posted by Prads on 04 July 2011 - 03:14 AM

Why are you using TCHAR instead of LPCSTR? Converting from string to LPCSTR is easy, just use string::c_str().
There is a memory leak in your code. I suggest you to redesign your code because loading wave file to buffer from the disk every time you want to play the sound is inefficient and unnecessary. Here's what you can do, create a separate class for sound data like this:

class waveSound {
private:
	IXAudio2SourceVoice *xASourceVoice;
	XAUDIO2_BUFFER xABufferInfo;
	unsigned char *wBuffer;

public:
	waveSound(LPCSTR waveFile, IXAudio2 *xAudio);
	~waveSound();

	void playSound();
};

Now you can load your wave data in wBuffer in the constructor and the delete wBuffer in your destructor which will solve your memory leak issue. When you play sound using playSound() just submit the xABufferInfo and then play it, like this:

xASourceVoice->SubmitSourceBuffer(&xABufferInfo);
xASourceVoice->Start();


I have one more question: How to check if the playing sound has finished?



XAUDIO2_VOICE_STATE xAState;
xASourceVoice->GetState(&xAState);


Now check xAState.BuffersQueued, if it's greater than 0, sound is playing.


#4830648 Newbie Programming Help.

Posted by Prads on 03 July 2011 - 11:22 AM

Learning C++ will take a lot of time specially since you have never programmed before. You need atleast 6 months of practice or even more to start writing some good codes. So, if you have time, learn it. Just take your time to grab the concept of the language but don't expect for any shortcuts 'cause there are non.

If you however want to learn C#, that's ok too. It's somewhat easier and if you choose to use XNA framework, developing game is much much more easier for you. But as I said before, just take it slowly to learn the language first and then only think about developing a game.

Good Luck!


#4830638 Which audio library is best to use with DirectX 9.0?

Posted by Prads on 03 July 2011 - 10:43 AM

Yes, that is one way but you won't be able to play multiple sound at once since when you submit a sound to IXAudio2SourceVoice, it goes through a queue. So, if you want to play multiple sound at once, create different IXAudio2SourceVoice for different sounds.


#4828964 Packing level files

Posted by Prads on 29 June 2011 - 12:09 AM

Do you want compression too? If yes then it's little complicated. But if not then you can just read bytes from files and put them all in one file. And later when you need to read them, just unpack them into a temporary folder and delete them when you don't need them anymore.

But I think it would be more efficient if you just put all your files in one separate folder and read them from there instead of make one huge file by putting them together.


#4770626 Breaking Simple XOR Encryption

Posted by Prads on 06 February 2011 - 03:56 PM

Toony, you owe me an hour. :P The file you attached contains two files b1 and b2, and I thought I was suppose to break both of them. I had successfully figured out some of the plaintext of b1 (You -------ange------- soc------) and now all I had to do was to replace those "-" with actual plaintext which would take like some minutes. But I thought maybe the key has some clue so I figured out the first 4 letters of key (g3ng) and that is when I realized b1 was actually "a" file and I just wasted my 1 hour. You should have named them correctly, b1 = a and b2 = b.

Anyway, I was able to break b2 file too. b1 and b2 contains same plaintext (You are 2 dangerous to society!!). Actually b2 is just encrypted form of b1, you encrypted b1 with 13 letters key (g309jg4320ng2) and just renamed it to b2. To decrypt b2, all you have to do is decrypt it with g309jg4320ng2 first and then decrypt it again with g3n908dj20g.

There you go, both of your files were cracked, that proves how weak simple XOR encryption is when the size of key is less than size of file.


PARTNERS