Author Topic: [HTML5] How to get started with TinyC5  (Read 30535 times)

0 Members and 1 Guest are viewing this topic.

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
[HTML5] How to get started with TinyC5
« on: November 20, 2011 »

How to get started with TinyC5


Introduction

Welcome to this small tutorial about how to get started with TinyC5 development. This tutorial assumes that you ...:

  • have basic coding knowledge
  • have heard about TinyPTC/Pixeltoaster and/or framebuffer stuff
  • have some basic knowledge about HTML and javascript

Before we start, just another short list of what this tutorial is not/not about:

  • complete
  • bug-free ;-)
  • advanced
  • producing great graphic effects

All files of this tutorial are attached.


Step 01


Let's start off with the most minimal example:

Code: [Select]
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       
        <script type="text/javascript" src="js/TinyC5-0.6.js"></script>
       
        <script type="text/javascript">
            function main() {
                 new TinyC5().start();
            }
        </script>
    </head>
    <body onload="main()">
    </body>
</html>

Assuming that you know the basic structure of a standrad HTML page, let's focus on the important bits, actually only 2 lines.

With the followin line we include the TinyC5 library code:

Code: [Select]
<script type="text/javascript" src="js/TinyC5-0.6.js"></script>

Within the main-function, which is called after the page is successfully loaded, we create a new instance of TinyC5 and immediately call the start method.

Code: [Select]
new TinyC5().start();

Result

We see a black square on the screen with the dimensions of 320x200 pixels, which are the default values. The black square is actually our drawing buffer, ie. canvas which is appended by default to the body element.




Step 02


In the second step we restructure our code. This will show us the different parts of how to setup a TinyC5 instnace. The important bits are as follows. We replace the code of the main funciton with the following lines:

Code: [Select]
var tinyC5;
tinyC5 = new TinyC5();
tinyC5.init();
tinyC5.update = function() {};
tinyC5.start();

Line 1: Declare a variable that will refer to our TinyC5 instance.
Line 2: Assign to our tinyC5 varibale a new instance of TinyC5.
Line 3: Initialize out TinyC5 instance.
Line 4: Overwrite the update property of our instance with a new function, which we leave is empty for now.
Line 5: Start the animation of our tinyC5 instance.

Result

The outcome is the same as in Step 01. But this time we have created a detailed setup of our TinyC5 instance we can build on in future steps.




Step 03


In this step we beautify our application while adding some more CSS/HTML to our code. In addition, we define some arguments which we pass to the TinyC5.init() function to overwrite certain default values.

New CSS/HTML code. No magic in here really.

Include some styles...

Code: [Select]
<link rel="stylesheet" type="text/css" href="css/styles.css" />


Add some more HTML to our body...

Code: [Select]
<div id="container">
    <div id="tinyC5_container">
    </div>
</div>
<div id="footer">
    Tutorial | Created with <a href="http://www.dbfinteractive.com/forum/index.php?topic=5397.0">TinyC5</a>
</div>       

Then we define a new variable that we pass as argumennt to TinyC5.init(). The argument is a simple proterty object which values will configure the TinyC5 instance.

Lines we modified:

Code: [Select]
var tinyC5, args;
tinyC5 = new TinyC5();
args = {
    width: 160,
    height: 100,
    scale: 2,
    title: 'Tutorial | TinyC5',
    bgColor: tinyC5.color( 155, 255, 100, 255 ),
    container: document.getElementById( 'tinyC5_container' )
};
tinyC5.init( args );

Line 01: Added delcaration for args.
Line 03: Assigned property-object to args. Most properties should be pretty self-explanary.
Line 06: The scale property will scale the canvas tag by the given value. In this example the resulting canvas will have a dimension of 320x200 pixels.
Line 07: The title will be set to the windows browser tab.
Line 08: We define a nice background color and overwrite the default value (which is solid black).
Line 09: In the previous steps the output canvas was appended directly beneath the body tag. If we pass another HTML element via the container property we can determine to which HTML node the canvas element is inserted in.
Line 11: We pass the created args as parameter to the TinyC5.init() funciton.

Result

Now the layout changed mostly due to the CSS. Our output should now be in the centre of the screen showing a green square.




Step 04


Alright, let's draw something. Since we calculate the color of each pixel new on each loop, we do not need to set the initial background color anymore.

That's why we drop it:

Code: [Select]
bgColor: tinyC5.color( 155, 255, 100, 255 ),

The main addition in this step is that we actually create some logic in our update method - in this case we draw a sinple checkerboard:

Code: [Select]
tinyC5.update = function() {
    var p, color, index;
    index = 0;
    p = tinyC5.pixels;
    for( var y = 0; y < tinyC5.HEIGHT; y++ ) {
        for( var x=0; x < tinyC5.WIDTH; x++ ) {
            color = 255 * ( ( ( x >> 4 ) & 1 ) ^ ( ( y >> 4 ) & 1 ) );
            p[index++] = color;
            p[index++] = color;
            p[index++] = color;
            p[index++] = 255;
        }
    }
};

This should look actually a bit familiar to you if you have ever worked with tinyPTC/PixelToaster. Some quick notes:

Line 04: We make a local reference to the TinyC5.pixels array. The array holds all the pixel data in the following format [ Red, Green, Blue, Alpha, Red, Green, Blue, Alpha, Red, ... ]
Line 05: TinyC5.HEIGHT is the height value of the pixel buffer. In this case 100.
Line 06: Same with TinyC5.WIDTH. It's 160 pixel in our example.

Result

The result is a simple black/white checkerboard. Guess the FX itself does not require any explanation.




Step 05


This step already indicates the end of the tutorial. In this step we added some movement to our small effect as well as some cracker-colors. Only one TinyC5 related function needs to be explained here.

Code: [Select]
tinyC5.getTime()

This returns the elapsed time in milliseconds since the TinyC5.start() function was called. Use this for timed animation.

Result

Since this is the last step, the code contains detailed comments for further details. Please have a look at the code.



Please feel free to post any comments, thoughts or questions about this tutorial in this thread.

Thanks for reading.

P.S.: You need to be logged in in order to download the tutorial files.


Additional Stuff


ImageLoading with TinyC5
« Last Edit: December 06, 2011 by benny! »
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: [HTML5] How to get started with TinyC5
« Reply #1 on: November 20, 2011 »
Thanks for this tutorial Benny, K+
Shockwave ^ Codigos
Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: [HTML5] How to get started with TinyC5
« Reply #2 on: November 20, 2011 »
Thanks for this tutorial Benny, K+

Thanks for the "K" ... guess most of the coders here don't need that tutorial - but maybe its useful for one or the other. In addition, I hope to pull some non-forum members into this forum (tutorial is also linked from google code). Will see ...
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline relsoft

  • DBF Aficionado
  • ******
  • Posts: 3303
  • Karma: 47
    • View Profile
Re: [HTML5] How to get started with TinyC5
« Reply #3 on: November 21, 2011 »
Awesome tute dude!
Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: [HTML5] How to get started with TinyC5
« Reply #4 on: November 21, 2011 »
Awesome tute dude!

Thanks a lot, mate!
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: [HTML5] How to get started with TinyC5
« Reply #5 on: November 21, 2011 »
Good stuff Benny. It would be nice to have something about texture loading covered as a lot of people will probably like to use that functionality.
raizor

Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: [HTML5] How to get started with TinyC5
« Reply #6 on: November 21, 2011 »
Good stuff Benny. It would be nice to have something about texture loading covered as a lot of people will probably like to use that functionality.

Good call, I will add this topic!
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline relsoft

  • DBF Aficionado
  • ******
  • Posts: 3303
  • Karma: 47
    • View Profile
Re: [HTML5] How to get started with TinyC5
« Reply #7 on: December 01, 2011 »
Is there an input functionality? I wanna maek gmaes!
 :goodpost:
Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: [HTML5] How to get started with TinyC5
« Reply #8 on: December 01, 2011 »
Is there an input functionality? I wanna maek gmaes!
 :goodpost:

Hey relsoft,

"natively" the library supports mouse events. Refer to Example 06. Getting the output canvas with TinyC5.getCanvas() you should be able to bind any event you want to the canvas and use it.

Let me know if you need any help here.

Best,
benny!
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline LittleWhite

  • Senior Member
  • Amiga 1200
  • ********
  • Posts: 418
  • Karma: 31
  • It's me!
    • View Profile
Re: [HTML5] How to get started with TinyC5
« Reply #9 on: December 03, 2011 »
Ah, it's an HTML5 demo ... hum ... will be harder to start my framework :D
I hope to play with it :)
The demoscene will never die, never!

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: [HTML5] How to get started with TinyC5
« Reply #10 on: December 06, 2011 »

Introduction


This tutorial was done on request. It explains briefly how to load and use image data with TinyC5. This tutorial shows just one possible way. There are other ways to achieve the same, I guess.


Step 01


To avoid security issues which might arise when loading external images, let's do it the oldschool way and embed the imagedata in our product ;-). So first, we need to convert our image data to base64 format. There are numerous converters around, let's try the following:

http://www.greywyvern.com/code/php/binary2base64

Just point to the image to convert it, e.g. http://www.dbfinteractive.com/forum/Themes/nocturno205/images/img/logo.png.

After it was successful created, let's copy and paste the image data in an asset javascript object:

Code: [Select]
var Assets = {
    dbflogo: "...ENCODED_DATA..."
};

Ok, now you have the imagedata embedded into your javascript code.


Step 02


So, to actually use the encoded data, we creaete an Image object in javascript and assign our encoded data to its source property. The Image object knows how to decode/handle that data ;-)

Code: [Select]
// Declare a var for our image
var dbfLogo;

// We wrap the image creation into a load function
function load() {
    dbfLogo = new Image();
    dbfLogo.src = Assets.dbfLogo;
    dbfLogo.onload = function(e) { main(); };
}

Note

This might look a bit too complex you might think. We added an onload-event-handler to the Image, although we are not really loading an image. Nevertheless, javascript is asynchronous and assigning the data to the src property, decoding it takes a while.

Unlike other languages, the programm execution does not stop. That's why it is necessary to add that handler to make sure we does not make use the dbgLogo image before it is not fully initnialized.


Step 03


First, let's include the Utils library of the TinyC5 package:

Code: [Select]
<script type="text/javascript" src="js/TinyC5Utils-0.7.js"></script>

Then, we actually can get the pure image data from our Image with the following line:

Code: [Select]
// Get imageData
var logoImageData = TinyC5Utils.getImagedataFromImage( dbfLogo );


Result


I wrapped this in a small TinyC5 example. The result is as follows:



[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline combatking0

  • JavaScript lives!
  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4569
  • Karma: 235
  • Retroman!
    • View Profile
    • Combat King's Barcode Battler Home
Re: [HTML5] How to get started with TinyC5
« Reply #11 on: December 06, 2011 »
Is it possible to draw a line with one command, similar to

Code: [Select]
canvas.strokeStyle="#ff7f00";
canvas.moveTo(x,y);
canvas.lineTo(xx,yy)
canvas.stroke();

Or should each pixel along the line be set individually? It seems as though using the above four commands would be faster from a programming point of view, but if this is outside of the spirit of TinyC5, then I will work on making my code conform.
« Last Edit: December 06, 2011 by Raizor »
You are our 9001st visitor.
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: [HTML5] How to get started with TinyC5
« Reply #12 on: December 07, 2011 »
What IDE's would you recommend?
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: [HTML5] How to get started with TinyC5
« Reply #13 on: December 07, 2011 »
Is it possible to draw a line with one command, similar to

Code: [Select]
canvas.strokeStyle="#ff7f00";
canvas.moveTo(x,y);
canvas.lineTo(xx,yy)
canvas.stroke();

Or should each pixel along the line be set individually? It seems as though using the above four commands would be faster from a programming point of view, but if this is outside of the spirit of TinyC5, then I will work on making my code conform.

Well, the library itself is focussing on pushing pixels and that's it. TinyC5 is just an really low-level abstraction layer of the canvas element to have direct pixel access. So, I guess the main FX should be based on a pixel-by-pixel basis. But if you are doing general Canvas programming I would recommend using the inbuild canvas stuff, like the commands you mentioned.

Nevertheless, it is still possible to additionally add normal canvas operation on the resulting output canvas. Refer to Example 06 and have a look how the text is drawn on the canvas. It's all handled in the tinyC5.postUpdate().

What IDE's would you recommend?

Good question. Personally I am currently using Netbeans since the Intellisense is comparably good for javascript. I also liked working with Eclipse - but since the most tools comes with the browser (e.g. debugging, network trace, console...) it is really a matter of taste, what you prefer.

What kind of tools do other use?
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: [HTML5] How to get started with TinyC5
« Reply #14 on: December 07, 2011 »
What kind of tools do other use?

Notepad++ at the moment. I think it's pretty cool.
raizor

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: [HTML5] How to get started with TinyC5
« Reply #15 on: December 07, 2011 »
Notepad++ is really great.  I use it all the time for 100% of my web stuff.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline jace_stknights

  • Amiga 1200
  • ****
  • Posts: 399
  • Karma: 32
  • PEEK & POKE are not MOVEM!
    • View Profile
    • ST Knights WebSite
Re: [HTML5] How to get started with TinyC5
« Reply #16 on: December 07, 2011 »
 
To avoid security issues which might arise when loading external images, let's do it the oldschool way and embed the imagedata in our product ;-). So first, we need to convert our image data to base64 format. There are numerous converters around, let's try the following:

http://www.greywyvern.com/code/php/binary2base64


pfewwww hopefully yhere is this technique... I'm getting mad with this!
Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: [HTML5] How to get started with TinyC5
« Reply #17 on: December 07, 2011 »
To avoid security issues which might arise when loading external images, let's do it the oldschool way and embed the imagedata in our product ;-). So first, we need to convert our image data to base64 format. There are numerous converters around, let's try the following:

http://www.greywyvern.com/code/php/binary2base64


pfewwww hopefully yhere is this technique... I'm getting mad with this!

Ehm? Doesn't this work for you ?
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline jace_stknights

  • Amiga 1200
  • ****
  • Posts: 399
  • Karma: 32
  • PEEK & POKE are not MOVEM!
    • View Profile
    • ST Knights WebSite
Re: [HTML5] How to get started with TinyC5
« Reply #18 on: December 07, 2011 »
Yep it runs fine! I have tried to read pixel from context.getImageData and got the famous security error! With this technique of including binary data in the javascript var lets you work without problems  :D

(Already made tunnel, plasma, zoomer effects  ;D)

Huhu back to my code... Hum at the end, I will produce a mega demo  :bfuck2:
Challenge Trophies Won:

Offline combatking0

  • JavaScript lives!
  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4569
  • Karma: 235
  • Retroman!
    • View Profile
    • Combat King's Barcode Battler Home
Re: [HTML5] How to get started with TinyC5
« Reply #19 on: December 07, 2011 »
Nevertheless, it is still possible to additionally add normal canvas operation on the resulting output canvas. Refer to Example 06 and have a look how the text is drawn on the canvas. It's all handled in the tinyC5.postUpdate().

Thanks Benny,

I will make the effect I was thinking of alongside a genuine TinyC5 pixel effect.
You are our 9001st visitor.
Challenge Trophies Won: