• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

jayakody2000lk

Members
  • Content count

    0
  • Joined

  • Last visited

Community Reputation

271 Neutral

About jayakody2000lk

  • Rank
    Newbie

Personal Information

  1. This is a simple project to demonstrate the USB custom hardware interfacing with Unity3D game engine on top of Microsoft Windows operating system(s). The custom hardware unit used in this demo is build around Microchip's PIC18F2550 MCU. This custom USB controller consists of 4 push buttons and a linear potentiometer. In the supplied demo, the user needs to control the aircraft with those buttons and the potentiometer. According to the game logic 4 buttons are used to control the flying direction and flying angle of the aircraft and the potentiometer is used to control the speed of the aircraft. As illustrated in figure above, the host environment consists of 2 main applications such as Session Controller and Unity3D game. Session Controller is responsible for USB communication and data conversions. It's a native application written using Delphi and it gets started with Unity game project. Communication between Session controller and Unity game project is happening through an OS level shared memory location. In this demo both Session Controller and Unity game project are heavily dependent on Windows API functions, and also both the applications require administrative privileges to execute. In this demo project MCU firmware is developed using MikroC PRO 5.0. Session controller is developed using Embarcadero Delphi XE3 and all the Unity scripts are in C#. HID interface of this project is based around J.W. Beunder's Delphi HID library. The microcontroller firmware consists of a simple port scanner and ADC (Analog to Digital Converter) scanner. When the scanner dectects some change in input, it transmits all the "port values" and "ADC value" to the USB HID buffer. Microcontroller firmware is listed below and it is specially designed for PIC18F2550 MCU, but it can be used with PIC18F2455, PIC18F4455 and PIC18F4550 MCUs with slight modifications. #define USB_BUFFER_SIZE 64 #define USB_LINK_SIGNATURE 0x3E #define ADC_NOISE_OFFSET 5 unsigned char usb_readbuff[USB_BUFFER_SIZE] absolute 0x500; unsigned char usb_writebuff[USB_BUFFER_SIZE] absolute 0x540; unsigned char button_buffer = 0x0; unsigned int speed_val, speed_buffer = 0x0; //handle MCU interrupts void interrupt() { USB_Interrupt_Proc(); } //function to clear USB write buffer void clear_write_buffer() { unsigned char wpos; for(wpos = 0; wpos < USB_BUFFER_SIZE; wpos++) usb_writebuff[wpos] = 0x0; usb_writebuff[0] = USB_LINK_SIGNATURE; } void init_system() { clear_write_buffer(); //enable MCU's USB connectivity and init HID module. HID_Enable(&usb_readbuff, &usb_writebuff); ADC_Init(); //setup microcontroller I/O configuration INTCON2 = 0x0; ADCON1 = 0xE; PORTB = 0; TRISB = 0x0F; PORTA = 0; TRISA = 0x1; Delay_ms(10); } //function to write scanned port values and ADC value to USB data buffer void tx_usr_inputs() { usb_writebuff[1] = button_buffer; usb_writebuff[2] = (speed_val & 0xFF); usb_writebuff[3] = (speed_val >> 8); while(!HID_Write(&usb_writebuff, 64)); asm nop; } void main() { init_system(); while(1) { speed_val = ADC_Get_Sample(0); //check for port or ADC value changes if((button_buffer != (PORTB & 0xF)) || (abs(speed_val - speed_buffer) > ADC_NOISE_OFFSET)) { //port or ADC value is changed... button_buffer = (PORTB & 0xF); speed_buffer = speed_val; tx_usr_inputs(); } } } As described earlier, interface between the game and USB HID peripheral is made using a Delphi application. This application creates named shared memory and writes all the processed data to that space. Because of this techinique multiple game instances can read the USB controller's data and it also reduces the synchronization issues between the game and hardware device. This interface code is listed below: unit ufMain; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, uCommon, HIDctrlIntf, Vcl.ExtCtrls; const USB_CNTLR_VID = $8462; USB_CNTLR_PID = $0004; USB_CNTLR_SIGNATURE_CODE = $3E; SPEED_ADC_MIN = $08C; SPEED_ADC_MAX = $384; type TfrmMain = class(TForm) tmrUSB: TTimer; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure tmrUSBTimer(Sender: TObject); private IsDevInUse: Boolean; IPCPntr: PIPCDataset; MemMapHandler: THandle; USBDevList: THIDdeviceList; procedure InitIPCDataSet(); public procedure InitUSBDeviceScan(); procedure TxHIDData(BtnCode: Byte; ADCInput: Word); end; var frmMain: TfrmMain; ADCSpeedPos: Word; implementation {$R *.dfm} //capture USB library events (including USB attach and deattach events) procedure OnUSBEvent; stdcall; begin TfrmMain(Application.MainForm).InitUSBDeviceScan; end; //function to read data from HID buffer procedure OnHIDRead(Data: THIDbuffer); stdcall; begin if((SizeOf(THIDbuffer) > 3) and (Data[0] = USB_CNTLR_SIGNATURE_CODE)) then begin //recreate 10bit ADC value ADCSpeedPos := Data[2] + (Data[3] shl 8); if(ADCSpeedPos < SPEED_ADC_MIN) then ADCSpeedPos := 0 else ADCSpeedPos := Round(((ADCSpeedPos - SPEED_ADC_MIN)/SPEED_ADC_MAX) * 100); TfrmMain(Application.MainForm).TxHIDData((not Data[1]) and $0F, ADCSpeedPos); end; end; //module's init point procedure TfrmMain.FormCreate(Sender: TObject); begin try USBsetEventHandler(@OnUSBEvent); HIDsetEventHandler(@OnHIDRead); IsDevInUse := false; //create shared memory space MemMapHandler := CreateFileMapping(INVALID_HANDLE_VALUE, nil, PAGE_READWRITE, 0, $100, COMLINK_NAME); Win32Check(MemMapHandler > 0); IPCPntr := MapViewOfFile(MemMapHandler, FILE_MAP_ALL_ACCESS, 0, 0, $100); Win32Check(Assigned(IPCPntr)); InitIPCDataSet(); //check for USB game controller... InitUSBDeviceScan; except MessageBox(0, 'Unable to create shared memory to initiate the communication link'#10#10'Is this application running with administrative privileges?', Pchar(Application.Title), MB_OK + MB_ICONHAND); if(MemMapHandler > 0) then CloseHandle(MemMapHandler); Application.Terminate; end; end; procedure TfrmMain.FormDestroy(Sender: TObject); begin if(MemMapHandler > 0) then CloseHandle(MemMapHandler); end; procedure TfrmMain.InitIPCDataSet(); begin IPCPntr^.SignatureCode := COMLINK_SIGNATURE; TxHIDData(0, 0); end; procedure TfrmMain.TxHIDData(BtnCode: Byte; ADCInput: Word); begin if(MemMapHandler > 0) then begin IPCPntr^.ControlInputs := BtnCode; IPCPntr^.SpeedInput := ADCInput; end; end; procedure TfrmMain.InitUSBDeviceScan(); var USBDevCount : Byte; begin //Searching for USB game controller... HIDscanForDevices(USBDevList, USBDevCount, USB_CNTLR_VID, USB_CNTLR_PID); if((USBDevCount > 0) and (not IsDevInUse)) then tmrUSB.Enabled := true else begin try HIDcloseDevice(USBDevList[0]); finally IsDevInUse := false; end; end; end; //timer module is used to avoid multipal high frequency USB events procedure TfrmMain.tmrUSBTimer(Sender: TObject); begin tmrUSB.Enabled := false; IsDevInUse := HIDopenDevice(USBDevList[0]); end; end. In Unity, the above mentioned shared memory is accessed using the same Windows API functions and its implementation is available in the UHWComLink.cs file. GetHIDControlData is the function to get all the shared memory data and it's listed below: public bool GetHIDControlData(out UHWComData ComDataSet) { ComDataSet.SignatureCode = 0; ComDataSet.ControlInputs = 0; ComDataSet.SpeedControl = 0; ShMemFileHandler = OpenFileMapping(FileRights.AllAccess, false, COMLINK_NAME); if (ShMemFileHandler == IntPtr.Zero) return false; IPCMapPntr = MapViewOfFile(ShMemFileHandler, FileRights.AllAccess, 0, 0, 0x100); if (IPCMapPntr == IntPtr.Zero) return false; //read values from shared data structure ComDataSet.SignatureCode = Marshal.ReadByte(IPCMapPntr); ComDataSet.ControlInputs = Marshal.ReadByte(IPCMapPntr, 1); ComDataSet.SpeedControl = Marshal.ReadInt16(IPCMapPntr, 2); CloseHandle(ShMemFileHandler); return true; } A schematic of the USB game controller is illustrated in the next figure. This can be constructed using breadboard, stripboard or PCB (Printed Circuit Board). Recommended platform to build this controller is a PCB and the complete PCB design pattern is available at the project repository. The supplied PCB design of this project is based on commonly-available SMD components. Please note that this hardware setup is quite sensitive to external noises, so it is recommended to use some properly-grounded shield with this controller. If the USB connection between the host and the controller is more than 1.5m, it is advisable to use a USB cable with ferrite bead(s). All the source code and design documents of this project are available to download at github.com/dilshan/unityusb. A demonstration video of the prototyped system can be viewed here
  2.   I always accepting criticisim BUT it need to be subjective and point-blank. If you carefully observe the code listsings you never make your initial comment. So in my context I treat your initial comment as "vague & careless" one.
  3. It's purposely done to avoid any hard dependencies with other OSs and game engines. By referring this article anyone can use this hardware piece with other operating systems like Linux and game developmenent kits like UDK, irrlicht, etc.   But in this article we focuse Windows and Unity because both of these technologies are quiet familiar to many developers and easy to understand / explain to any level of developer.   Meantime I wants to know if there are any Unity inbuild custom hardware interface is exists ???