Author Topic: Get your feet wet into DragonFireSDK and learn the basics (Beginner's Guide)  (Read 11135 times)

zacho

  • Jr. Member
  • **
  • Posts: 81
  • Karma: +2/-0
    • View Profile
Get your feet wet into DragonFireSDK and learn the basics (Guide)

Introduction
Hello. Zacho here. After receiving wonderful and insightful help from the members of this forum, I have been inspired to help others as they have helped me. This thread will be where I will update tidbits of code to accomplish tasks using DragonFireSDK once I have learned the tricks. Most importantly... this thread will be a

COMMUNITY THREAD

in that everyone can contribute code and examples to share with each other. If you have a good example that you found yourself appraising please post it here so our knowledge of DragonFireSDK can continue to grow. Please post code using [ code ] [/ code ] tags.



Topics in thread
  • Creating a push button

To create a button, you must have two things; a function that is called when this button is pressed and two separate images for this button. The two images must have the same name and end with a 1 and a 2.
Button1.png       -       Button2.png
The function can look like something below (feel free to copy the code).

[/list]
Code: [Select]
int OnButton(int id)
{
        //do something...
        return 0;
}

//ButtonAdd("filename", x-cor, y-cor, function, id#)
ButtonAdd("Images/Button", 0, 0, OnButton, 0);


The paramater "id" can be thought of as a case. For example, if three copies of this button were made right next to each other, so that three buttons sat side-by-side onscreen, the id would do something separate for each button. If the far-leftmost button was hit, the id called might be 1. The middle button's id might be 2 and the far-rightmost button's id might be 3.


Code: [Select]
int OnButton(int id)
{
        if( id == 1 )
        //do something...

        if( id == 2 )
        //do something...

        if( id == 3 )
        //do something
        return 0;
}


ButtonAdd("Images/Button", 0, 0, OnButton, 1);
ButtonAdd("Images/Button", 50, 0, OnButton, 2);
ButtonAdd("Images/Button", 100, 0, OnButton 3);
  • Flipping "pages"



m4unot

  • Guest
Good place to share knowledge, because i'm going show how to " detect "  the starting and ending  point of the object's x -coordinate

and detect collision without using code, but simple and basic logic to give someone the idea. I'm not going cover the  y -coordinate part, because it feels

better to do something too, but let start!


Step one:

We start by passing the known data. Like the x, y -coordinate where the object is placed, but most

important the height and width of the object:

Example:

x  = 10 , y  = 10

height =  50 width = 50



Step Two:


Radius of the images width is going help us to find the starting point of  x-coordinate

radius = width/2

While the diameter is going be used to detect the ending point of the x-coordinate

diameter = width


And the negative number down below is going be used to change the direction from positive to

negative and vise versa:

(-1)


Step three:


We start calculating the starting point and ending point of the object from the information we had:

x_start_point = radius * (-1)

x_end_point = diameter


All the x-points between the starting point and ending point is going be stored inside arrays

Points[] = [x_start_point;  x_end_point]

Now you can use if statement to detect if any of the points  stored inside Points[] is getting touched




zacho

  • Jr. Member
  • **
  • Posts: 81
  • Karma: +2/-0
    • View Profile
Here's another example coming your way, flipping pages!

The idea behind flipping pages is you have multiple groups of elements, say, a menu and a choice in that menu to choose a level. To use this, we need at least two containers. A container is like a big package. You can put all of your buttons and images within a container. To flip pages, you turn one container's contents off (temporarily) and turn another's on, creating an illusion that you are flipping pages. Here's the code

Code: [Select]
int container1;
int container2;


int OnButton(int id)
{
     if( id == 1 )
     {
            ContainerSetVisible(container1,0);
            ContainerSetVisible(container2,1);
     }
     else if( id == 2 )
     {
            ContainerSetVisible(container1,1);
            ContainerSetVisible(container2,0);
     }

     return 0;
}

AppMain()
{
     //Create your containers first
     container1 = ContainerAdd(0,0,0);
     container2 = ContainerAdd(0,0,0);

     //Add images or buttons to your container
     ViewAdd = (container1, 0,0, "Image.png");
     ButtonAdd = (container1, 0,0, "Button", OnButton, 1);

     ViewAdd = (container2, 0,0, "Image_two.png");
     ButtonAdd = (container2, 0,0, "Button", OnButton, 2);


     //Set visibility flags for each container
     ContainerSetVisible(container1,1);
     ContainerSetVisible(container2,0);

     
}

The code above creates two pages and one button to swap between them.

zacho

  • Jr. Member
  • **
  • Posts: 81
  • Karma: +2/-0
    • View Profile
Non-hardcoded things in TextAdd boxes

simply do something like this

Code: [Select]
string s = "ola";

TextAdd(x,y, (char *)s.c_str() );


chris

  • Guest
This is a really neat idea, thanks for taking the time to help out new users!

Rob_Jurado

  • Guest
Hello everyone,
I finally bought my copy of DFSDK.  Now I need to learn how to use it. I am reading through the forum and trying to get the hang of it to start coding.  Before I can learn how to use the different UI components ( by the way, I dont see that many).  How do I create forms (pages, layouts, activities... sorry, I am using my Android knowledge).  Anybody has a good example on how to start a simple project?

I think in order to understand how to use this library, I need to create an app with multiple screens, play with buttons to move back and forth, submenus and read some SQL table data.  Also, there is no UI Designer, right?

Thanks in advance.

Robert J.

Nico91

  • Guest
Hi,

to start if I were you I would learn to master the sights, touch and text.
then if you want a specific example you ask and we will show you, on the forum we have already added a lot, for example to zoom or scroll or to view a page that appears to the right dynamically, or animation image.

JB

  • Guest
For an App with multiple screens, use Containers.

Add buttons/images/Touch areas/text areas etc to a specific container.

Set the visibility of the containers depending on the Apps' function.
e.g.
"Button A" in "Container 1" might actually hide Container 1 and make "Container 2" visible, thereby exposing all the components housed in Container 2!

Rob_Jurado

  • Guest
Thanks everyone. 

I am on my way to build my first Iphone App.

Nico91

  • Guest
Good luck.

ciuffolo

  • Guest
about the x points' detecting, can someone make an example on how to get the width and height?
thanks

ciuffolo

  • Guest
here's another one to detect the collision event, is the code on FAQ, but isn't very easy to translate for the beginners:
(it uses the pitagora's theorem)
Code: [Select]
void OnTimer()
{
    // Get the circles moving:
    for (int i=0; i<n.balls; i++)
    {
        xcur=ViewGetx(Balls[i].ViewHandle);//get the current x coord. of the n.0
        ycur=ViewGety(Balls[i].ViewHandle);//get the current y coord. of the n.0
     
         for (int j=i+1; j<MAX_BALLS; j++)//verify the collision for all the balls
        {
            x2cur=ViewGetx(Balls[j].ViewHandle);//get the current x coord. of the ball n.1
            y2cur=ViewGety(Balls[j].ViewHandle);//get the current y coord. of the ball n.1
//then you have now the position of 2 balls, and you can calculate the distance, the for loop will think to calculate all the combinations
            d=sqrtf((x2cur-xcur)*(x2cur-xcur)+(y2cur-ycur)*(y2cur-ycur));//pitagora's theorem calculates the distance between the two balls
           
            if (d>BallWidth || d==0)//control if the distance between the two balls is 0 or less (superimposed the ball),then the collision has occurred
            //What do you want to do after that the collision has occurred
        }


DeadSn0wMan

  • Guest
Or you use Box2D with the R.U.B.E engine/ debug draw for DFSDK (search on the forums for thread) ;)

Nico91

  • Guest
If you need Apple features using dragonfiresdk look at this : http://dragonfiresdk.net/forum/index.php?topic=495.0

zacho

  • Jr. Member
  • **
  • Posts: 81
  • Karma: +2/-0
    • View Profile
EASIER FILE FUNCTIONS::






I made a bunch of file functions, these need to be tested if they work on the device, but here they are:

Returns a handle to the file created (in Debug/Documents)

Just pass in a string of the file and it will create it, ie. "textfile.txt"
Code: [Select]
int createNewFile(std::string fileurl)
{
int temp = FileCreate(toCString(fileurl));

return FileOpen(toCString(fileurl));;
}

Returns file handle for object (is an integer)

ie. int filehandle = getFileHandle("file.txt");
Code: [Select]
int getFileHandle(std::string fileurl)
{
return FileOpen(toCString(fileurl));
}

Reads file contents into a string
-File handle needs to be passed in; ie the variable holding the file
-numOfBytes = number of bytes to be read in, ie. one char is one byte
-startByte = the byte you would like to start reading at, these are indexed (ie, it goes from 0 to length of file)

ie, string s = readFromFile(filehandle,10,0); //Will read the first 10 bytes into string 's'
There is protection in here, if you try to read past file it won't read anything
Code: [Select]
std::string readFromFile(int filehandle, int numOfBytes, int startByte)
{
char* buffer = new char[numOfBytes+1];
int fileLength = FileLength(filehandle);

if( (startByte + numOfBytes) <= fileLength ) //If we try to read out of bounds
{
FileSeek(fileHandle,startByte); //Set 'cursor' for reading bytes

FileRead(fileHandle,buffer,numOfBytes);

buffer[numOfBytes] = '\0'; //Null terminate the character arra
std::string s(buffer);

delete[] buffer;
buffer = 0;

return s;
}
else
{
delete[] buffer;
buffer = 0;

return "";
}

}

Writes a string to a file
Filehandle is needed to be passed in
text - string to write to file

This doesn't write to user-defined bytes in the file, but can be easily changed to do so.

example: writeToFile(filehandle,"I want to write this to the file");
Code: [Select]
void writeToFile(int filehandle, std::string text)
{
char* buffer = new char[text.length()];
for(int i = 0; i < (int)text.length(); i++)
*(buffer+i) = text[i];

FileWrite(filehandle, buffer, (int)text.length());

delete [] buffer;
buffer = 0;

return;
}

Opens file, returns true if file did exist previously

I think this is a useful function

example: if( openFile("file.txt") )
       //do something
Code: [Select]
bool openFile(std::string fileurl)
{
int temp = FileOpen(toCString(fileurl));

return temp != 0 ? true : false; //returns true if temp is not 0
//meaning a file did exist
}

Closes file handle

Can't forget this one
Code: [Select]
void closeFile(int filehandle)
{
FileClose(filehandle);
return;
}






Can't forget the helper functions that makes this easier too, at least for me because I don't like using char* for file things... :P

Pass in a string, get a char*

example: AlertShow(toCString("text"),toCString("more text") );

Code: [Select]
char* toCString(std::string s)
{
char* temp = new char[s.length()];

for(int i = 0; i < (int)s.size(); i++)
temp[i] = s[i];

temp[s.length()] = '\0';

return temp;
}