Jump to content

  • Log In with Google      Sign In   
  • Create Account

Squared'D's Journal



Latest Updates 2014-01-28 and Base64

Posted by , 27 January 2014 - - - - - - · 690 views
base64
It's been a while since I've written a blog post. I had been spending a lot of time learning and exploring AngelScript. I won't be writing anymore AngelScript articles for Gamedev.net, but from time to time, I'll post blog entries on it.

These days, because of personal reasons of the other team members, Auxnet progress has slowed down a lot, but I hope to get back to it soon. While I wait for everyone to catch up, I've been experimenting with some new engine concepts. I've decided to build a little test engine currently code-named "Engine X" to try out the concepts. It'll be a multiplatform (Windows / Android) 2D tile-based engine that will use AngelScript as the scripting language. I'm not sure how much time I'll be able to devote to it though because it'll go on the back burner once things with Auxnet ramp up again.

I've got the base rendering and user input stuff working on Windows. Later I'll build it for Android. In the meantime, I've been working on adding support for a tile layer. I don't have time to make an editor so I'm going to use Tiled which can be found at mapeditor.org . I want to build a simple loader for it. The format is in XML so it's not overly complicated, but there was one thing that's new to me.

The tile data is stored in Base64. At first, I didn't know what this was so I decided to do a little research. Base64 is an encoding that takes data whether binary or text and encodes it using only 64 characters. This is useful when transmitting data over protocols that may alter the data. As it turns out, the pioneers of the Internet and email weren't forward thinking enough to think that more than 7-bits (thank you ASCII) would be needed for transmitting a text character. Base64 can also be used to store complex data inside things like XML. There are different forms of Base64, but they all work on the same general principle, It's used as a way to store binary data as plain text. It's not a complicated encoding. Basically you take three 8-bit bytes and break into four 6-bit units. The 6-bit units will be from 0-63. To make sure the values will be able to be transmitted without being garbled, the final character for each value is determined by the following table.


Posted Image

The above table is for Multipurpose Internet Mail Extensions (MIME). Other versions of Base64 use a similar table, but in some of them the characters used for values 62 and 63 are different. The above characters are good because they are supported by the majority of text encodings and aren't the same as any XML or HTML tags. For binary data to be converted into base64, the number of bytes should be a multiple of three. If not, it should be padded with zeros. When this padding occurs, and special 65th character, '=', is used instead instead of the normal 0, 'A' character. This means '=' will only appear at the end if there was padding.

Here's an example:

Posted Image

Now, I'll continue working on loading the map. Hope to have a working tiled-based rendering system within the next 7 days.

This entry was originally posted on the Squared Programming Blog: http://squaredprogramming.blogspot.com/2014/01/i-had-been-spending-lot-of-time.html


C++ STL-Style Array That's Compatible with AngelScript

Posted by , 04 January 2014 - - - - - - · 1,850 views
c++, angelscript, stl, array and 1 more...
This is a re-post from my blogspot entry: http://squaredprogramming.blogspot.com/2014/01/c-stl-style-array-thats-compatible-with.html

C++ STL-Style Array That's Compatible with AngelScript
I guess this will be like part 1.5 of my 2 part article on AngelScript. I've been working on a new class that I think will be useful to programmers working with AngelScript. AngelScript is nice because its data types are so similar to C++ that it's easy to bind it and share data. There's one thing that doesn't have an easy one-to-one AngelScript counterpart and that's static arrays. I can understand the reasoning behind this as the script has no mechanism that it can use to garuntee the life time of the array. AngelScript does have a nice add-on for arrays that's generic and can add arrays for any type, but I wanted something easier. I decided to make a template class that would wrap the CScriptArray add-on and give it an interface like std::vector including stl-style iterators.

Using the class.
The CScriptArraySTL class was designed so that the array can be created in C++ and then shared with AngelScript as a registered property, or as the return value of a registered functon. It is a template class that takes two template parameters.

template <class T, class TArrayClass = CScriptArray>
class CScriptArraySTL
The first parameter is the type, and the second parameter is the internal array class that has been registered with AngelScript. The default array class is the CScriptArray add-on that is included in the AngelScript SDK. To use this it, you must also include the CScriptArray add-on in your project and register it with AngelScript using the RegisterScriptArray() function. The internal array class is given as a template argument to allow the programmer to be able to use there own array implementation.

Declaring a variable:
// uses CScriptArray addon internally
// for this to work, std::string should be registered with AngelScript.
// This can be done using the ScriptStdString add-on.
CScriptArraySTL <std::string> string_array;
 
// uses a user-defined array type called CScriptArrayInt
// this type should be a specialized version of CScriptArray that only handles integers
CScriptArraySTL <int, CScriptArrayInt> int_array;
InitArray()
You can create variables using CScriptArraySTL anytime, but you can't use it until after the CScriptArraySTL object has been initialized using
the InitArray() method. Here's the declaration for that function:

// Initializes the array so it can be directly accessed using AngelScript
// This must be called before the array can be used
int InitArray(asIScriptEngine *engine, char *declaration, size_type init_length = 0);
asIScriptEngine *engine
The first parameter is a pointer to the script engine. To maximize compatibility with AngelScript, this class uses creates it's internal data using the types that have been registered with AngelScript.

char *declaration
The second parameter is how you would write the type for this array in AngelScript. This allows the class to match its type with AngelScript. For example, if the class holds integers, it should be written "array<int>".

size_t init_length
This is the initial size of the array.

GetRef()
This function returns a pointer to the internal array class and can be registered with AngelScript as a property or returned from a function that has been registered with AngelScript.

Release()
This will release a reference to the array. After this method has been called, the CScriptArraySTL class will no longer be able to access the array data; however, as arrays are reference types in AngelScript, the data may still exist inside AngelScript until the reference count is zero. This method should be called before the script engine is released.

Sample Code
// Create the script engine
asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
 
// Register needed add-ons
RegisterStdString(engine);
RegisterScriptArray(engine, true);
 
// setup our string array
CScriptArraySTL <std::string> string_array;
string_array.InitArray(engine, "array<string>");
 
// register our array as a global variable in the script
r = engine->RegisterGlobalProperty("array<string> string_array", string_array.GetRef()); assert( r >= 0 );
 
// do other things and load and run scripts
...
 
// Release the array
string_array.Release();
 
// Release the script engine
engine->Release();
Get the Source.
The source code and a example project can be found on GitHub. If you copy the "scriptarraystl" folder to the AngelScript SDK add_on folder, the project directory paths should work. Only a Visual Studio 2010 solution is being provided, but you should be able to run it using other compilers. To use this class in your project, all you need to do is include "ScriptArraySTL.h" The class has been implemented entirely in one header file.

GitHub link: https://github.com/squaredprogramming/scriptarraystl

___________________________________________________________

Here's the complete list of the methods that I've implemented for the class.

// Constructors, Destructors and AngelScript initialization -------------------------------
CScriptArraySTL(void);
~CScriptArraySTL(void);
 
// Initializes the array so it can be directly accessed using AngelScript
// This must be called before the array can be used
int InitArray(asIScriptEngine *engine, char *declaration, size_type init_length = 0);
 
// returns a pointer to an array class that can be used in an AS script
TArrayClass *GetRef();
 
// Releases a reference to the array. After this is called, this class can
// no longer access the array data, but the data may still exist inside
// AngelScript until the refernce count is 0.
void Release();
 
// Capacity ----------------------------------------------------------------------------------
 
// returns the number of elements in the array
size_type size() const;
  
// resizes the array, adding unitialized data if the new size is bigger than the old size or
// deleting data if the new size is smaller
void resize(size_type n);
 
// returns true if the array is empty
bool empty() const;
 
// grows the buffer capacity
void reserve(size_type n);
 
 
// iterators ----------------------------------------------------------------------------
// returns an iterator to the begining of the array
iterator begin();
 
// returns an iterator to the end of the array
iterator end();
 
// returns a constant iterator to the begining of the array
const_iterator cbegin() const;
 
// returns a constant iterator to the end of the array
const_iterator cend() const;
 
// returns a constant iterator to the begining of the array
iterator begin() const;
 
// returns a constant iterator to the end of the array
iterator end() const;
 
// returns a reverse iterator to the begining of the array
reverse_iterator rbegin();
 
// returns a reverse iterator to the end of the array
reverse_iterator rend();
 
// returns a constant reverse iterator to the begining of the array
const_reverse_iterator crbegin() const;
 
// returns a constant reverse iterator to the end of the array
const_reverse_iterator crend() const;
 
// returns a constant reverse iterator to the begining of the array
const_reverse_iterator rbegin() const;
 
// returns a constant reverse iterator to the end of the array
const_reverse_iterator rend() const;
 
// Element Access -----------------------------------------------------------------------
 
// returns a reference to an element in the array. This will not throw an out-of-range exception.
// undefined behavior if out of range.
reference operator[](size_type index);
 
// returns a const reference to an element in the array. This will not throw an out-of-range exception.
// undefined behavior if out of range.
const_reference operator[](size_type index) const;
 
// returns a reference to an element in the array. This will throw an out-of-range exception.
reference at(size_type index);
 
// returns a constant reference to an element in the array. This will throw an out-of-range exception.
const_reference at(size_type) const;
 
// returns a reference to the first element
// undefined if empty
reference front();
 
// returns a constant reference to the first element
// undefined if empty
const_reference front() const;
 
// returns a reference to the last element
// undefined if empty
reference back();
 
// returns a constant reference to the last element
// undefined if empty
const_reference back() const;
 
// Modifiers ------------------------------------------------------------------------------------
 
// adds a value to the end of the array
void push_back (const value_type& val);
 
// removes the last element
void pop_back();
 
// assigns new data to the array using iterators.
template <class inputiterator="">
void assign (InputIterator first, InputIterator last);
 
// fills the array 
void assign (size_type n, const value_type& val);
 
// clears the contents of the array
void clear()






Recent Comments

January 2014 »

S M T W T F S
   1234
567891011
12131415161718
19202122232425
262728293031 


PARTNERS