programming

CSharp – Functions

CSharp – Functions

A function allows you to encapsulate a piece of code and call it from other parts of your code.

Syntax:


<visibility> <return type> <method name>(<parameters>)
{
	<function code>
}

Example:

...
public int AddNumbers(int number1, int number2)
{
    int result = number1 + number2;
    return result;
}
...
int result = AddNumbers(10, 5);
Console.WriteLine(result);
...
By |CSharp|Commenti disabilitati su CSharp – Functions

Unity 3D – CSharp – Event Functions – Inizialization – Update – GUI – Mouse – Physics

Unity 3D – CSharp – Event Functions – Inizialization – Update – GUI – Mouse – Physics

To create videogames Unity3D give us special functions to manage special operations as variable inizialization, updating, physic management.


using UnityEngine;
using System.Collections;

public class MyScript : MonoBehaviour
{

        // Initialization Events
        // ******************************************

	void Awake ()
	{
		// first inizialization
		Debug.Log("Awake called.");
	}
	
	
	void Start ()
	{
		// second inizialization
		Debug.Log("Start called.");
	}

        // Regular Update Events
        // ******************************************

	void FixedUpdate ()
	{
		// frame indipendent update
                // use this function to drive physic
		Debug.Log("FixedUpdate time :" + Time.deltaTime);
	}
	
	void Update ()
	{
		// update at every frame
		Debug.Log("Update time :" + Time.deltaTime);
	}

	void LateUpdate () 
	{
		// update at the end of the frame rendering
		Debug.Log("Late Update");
	}

        // GUI Events
        // ******************************************

        void OnGUI() 
        {
                // draw here labels, buttons and other Graphic User Interface elements
        	GUI.Label(labelRect, "Game Over");
        }

        // Mouse Events
        // ******************************************

        void OnMouseOver() 
        {
                // do something if mouse is over
        }

        void OnMouseDown() {
                // do something if mouse is down
        }

        // Physics Events
        // ******************************************

        void OnCollisionEnter(otherObj: Collision) 
        {
                // do stuff if there is a collision
        }

        void OnTriggerEnter(Collider other) 
        {
                // do stuff if there is an object that overlap the trigger
        }

} // END MyScript

My website: http://www.lucedigitale.com

Ref: http://docs.unity3d.com/Manual/EventFunctions.html

By |CSharp, Unity3D, Video Games Development|Commenti disabilitati su Unity 3D – CSharp – Event Functions – Inizialization – Update – GUI – Mouse – Physics

CSharp – Enum

CSharp – Enum

Quando si definisce una variabile di tipo enumerativo, ad essa viene associato un insieme di costanti intere chiamato insieme dell’enumerazione. La variabile può contenere una qualsiasi delle costanti definite.

Syntax:


enum <enum_name> 
{ 
    enumeration list 
};

Example:


using System;
namespace EnumApplication
{
   class EnumProgram
   {
      enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };

      static void Main(string[] args)
      {
         int WeekdayStart = (int)Days.Mon;
         int WeekdayEnd = (int)Days.Fri;
         Console.WriteLine("Monday: {0}", WeekdayStart);
         Console.WriteLine("Friday: {0}", WeekdayEnd);
         Console.ReadKey();
      }
   }
}

The rEsult is

Monday: 1
Friday: 5

By |CSharp|Commenti disabilitati su CSharp – Enum

C++ Unions

C++ Unions

Unions allow one portion of memory to be accessed as different data types.

The code below creates a new union type, identified by type_name, in which all its member elements occupy the same physical space in memory. The size of this type is the one of the largest member element.


// Syntax
union type_name {
  member_type1 member_name1;
  member_type2 member_name2;
  member_type3 member_name3;
  .
  .
} object_names;

// The Sample code...
union mytypes_t {
  char c;
  int i;
  float f;
} mytypes;

// ...declares an object (mytypes) with three members
mytypes.c
mytypes.i
mytypes.f	

For italian people: la peculiarità della union è che tutte le sue variabili membro (in questo caso l’intero non segnato intero e l’array di char caratteri) condividono la stessa area di memoria, a partire dallo stesso indirizzo: in altri termini intero e caratteri occupano gli stessi 4 byte in memoria.
Tale proprietà si rivela molto utile, ad esempio, nella gestione dei colori in un ambiente grafico come X11. In un simile contesto, una union come quella del presente esempio consente di accedere “istantaneamente” alle componenti alpha, rossa, verde e blu del colore di un pixel senza ricorrere ad alcuna operazione di “bit masking” o “scorrimento bit”.

By |C++, Video Games Development|Commenti disabilitati su C++ Unions

C++ – Do While Statement

C++ – Do While Statement

Working sample:


// custom countdown using while
#include <iostream>
using namespace std;

int main ()
{
  int n = 10;

  while (n>0) {
    cout << n << ", ";
    --n;
  }

  cout << "liftoff!\n";
}

The result:

10, 9, 8, 7, 6, 5, 4, 3, 2, 1, liftoff!


// echo machine
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str;
  do {
    cout << "Enter text: ";
    getline (cin,str);
    cout << "You entered: " << str << '\n';
  } while (str != "goodbye");
}

The result:

Enter text: hello
You entered: hello
Enter text: who’s there?
You entered: who’s there?
Enter text: goodbye
You entered: goodbye
Process returned 0

By |C++, Video Games Development|Commenti disabilitati su C++ – Do While Statement