Linux game programming An introduction to the use of interval timers and asynchronous input notifications.

Slides:



Advertisements
Similar presentations
Linux Serial Programming for POSIX Operating Systems
Advertisements

CS0007: Introduction to Computer Programming
1 Exceptions, Interrupts & Traps Operating System Hebrew University Spring 2007.
Events Chapter 7. Interactive Real world is interactive User determines order of actions instead of programmer.
Terminal I/O POSIX termios Two terminal I/O modes Canonical and non-canonical Getting and setting terminal attributes Terminal window size: struct winsize.
Out-of-Band Data© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction.
Microsoft® Small Basic
I/O Multiplexing The role of the ‘poll()’ method in Linux device-driver operations.
I/O Multiplexing Road Map: 1. Motivation 2. Description of I/O multiplexing 3. Scenarios to use I/O multiplexing 4. I/O Models  Blocking I/O  Non-blocking.
Controlling concurrency A look at some techniques for process synchronization in the Linux environmemt.
The ‘fasync()’ driver-method Two viewpoints on implementing asynchronous notification: the application’s and the driver’s.
A code-walkthrough Commentary on our ‘wfmodel.cpp’ demo-program – an initial ‘prototype’ for 3D wire-frame animations.
Terminal Control operating systems. Terminal Control may seem like an arcane subject, but … It illustrates the relationship between devices and files.
Concurrent TCP connections A look at design-changes which permit a TCP server to handle multiple clients without delays.
Dynamic visualizations On ‘non-canonical’ keyboard-input and terminal escape-sequences for visualization effects.
A code-walkthrough Commentary on our ‘model3d.cpp’ demo-program – an initial ‘prototype’ for 3D wire-frame animations.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads and Scheduling 6.
Kernel Event-Timers Our first look at the Linux kernel’s mechanism for managing its dynamic timers.
Michener’s Algorithm An efficient scheme for drawing circles (and filling circular disks) on a raster graphics display.
Signal Signal : - is a notification sent to a process to notify it of some event - interrupts whatever the process is doing and force it to handle a signal.
Vertical Retrace Interval An introduction to VGA techniques for smooth graphics animation.
1 Lab Session-VII CSIT-121 Fall Revising Previous Lab and performing ASCII chart printing experiment (DEMO) 4 Visual Studio Features 4 The While.
Signals, Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
Linux game programming An introduction to the use of interval timers and asynchronous input notifications.
Exceptions, Interrupts & Traps
SE320: Introduction to Computer Games Week 8: Game Programming Gazihan Alankus.
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Signals & Timers CS241 Discussion Section Spring 2009 Week 6.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Signal-Driven I/O Concepts and steps for using signal-driven I/O
Visual Basic .NET BASICS
KeyListener and Keyboard Events Just as we can implement listeners to handle mouse events, we can do the same for keyboard events (keypresses) –to implement.
©Robomatter – Distribution or copying without permission is prohibited. 3B STEM Computer Science 1 ©Robomatter – Distribution or copying without permission.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Keyboard and Events. What about the keyboard? Keyboard inputs can be used in many ways---not just for text The boolean variable keyPressed is true if.
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
TELE 402 Lecture 12: Signal-Driven I/O & Raw Socket 1 Overview Last Lecture –Advanced UDP sockets and threads –Source: Chapters 22&26 of Stevens’ book.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Timers and Clocks II.
NCHU System & Network Lab Lab #8 Signals Operating System Lab.
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Implementing ‘noecho’ Programming details regarding the Linux implementation for ‘struct termios’ objects.
Viewing Individual Photos Click on any photo thumbnail to start viewing photos at that position in the album. To move to the NEXT photo: Click anywhere.
7. IRQ and PIC ENGI 3655 Lab Sessions. Richard Khoury2 Textbook Readings  Interrupts ◦ Section
Today’s topics Signals and how to control the program behavior in handling signals. Terminal I/O.
Time Management.  Time management is concerned with OS facilities and services which measure real time.  These services include:  Keeping track of.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
CSCI 330 UNIX and Network Programming Unit XVII: Socket Programming Detail.
1 Run-to-Completion Non-Preemptive Scheduler. 2 In These Notes... What is Scheduling? What is non-preemptive scheduling? Examples Run to completion (cooperative)
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Animate objects and threads
G.Jyostna.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Introduction to Events
Chapter 14 - Advanced C Topics
Sprite Animation An exercise on filling circles and polygons to create the animated sprite used in ‘Pac Man’
Advanced UNIX programming
ECE 352 Digital System Fundamentals
Animation Translation.
On using the mouse A brief introduction to LIBGPM: the General Purpose Mouse programming interface.
Presentation transcript:

Linux game programming An introduction to the use of interval timers and asynchronous input notifications

Our prior animation demo We achieved the illusion of “smooth” and “flicker-free” animation, by synchronizing drawing-operations with Vertical Retrace But more is needed in a game that’s “fun” Some deficiencies in our ‘animate1’ demo: –Ball movements tied to Vertical Retrace –The viewer lacked any real-time control How can we overcome these limitations?

Decoupling ‘move’ from ‘draw’ Our program-loop had used logic like this: do { vsync();// delay until start of the next retrace hide_ball(); // “erase” the ball move_ball();// adjust its location show_ball();// “redraw” the ball --count;// decrement a counter } while ( count > 0 );

Linux provides ‘interval timers’ #include struct itimervalitval, itold; itval.it_value.tv_sec = 2; itval.it_value.tv_usec = 0; itval.it_interval.tv_sec = 0; itval.it_interval.tv_usec = 10000; setitimer( ITIMER_REAL, &itval, &itold ); (See the ‘man’ page for additional details)

SIGALRM When timer “expires” our application gets notified, by being sent a signal from Linux Normally an application gets terminated if the SIGALRM signal is delivered to it But we can alter that default behavior, by installing a ‘signal-handler’ that we design We can ‘move-the-ball’ when SIGALRM is received, regardless of Vertical Retrace

Our signal-handler void on_alarm( int signum ) { // modify these global variables ball_xcoordinate += xincrement; ball_ycoordinate += yincrement; } // The ‘signal()’ function “installs” our handler signal( SIGALRM, on_alarm);

Main program-loop “revised” We can now omit ball-movement in main loop: do { vsync();// delay until start of the next retrace hide_ball(); // “erase” the old ball oldx = newx; oldy = newy;// remember new position show_ball();// “redraw” the new ball --count;// decrement a counter } while ( count > 0 ); Ball-movement is managed by ‘signal-handler’

Giving the user control Linux supports “asynchronous” terminal i/o We can reprogram the terminal console so a SIGIO signal will be sent to our program whenever the user decides to press a key And we can install a ‘signal-handler’ of our own design that executes if SIGIO arrives This will allow a user to “control” our game

‘Noncanonical’ terminal i/o We already learned how to reprogram the terminal to allow “raw” keyboard input #include struct termiostty; tcgetattr( 0, &tty );// get tty settings tty.c_lflag &= ~( ICANON | ECHO | ISIG ); tty.c_cc[ VMIN ] = 1; tty.c_cc[ VTIME ] = 0; tcsetattr( 0, TCSAFLUSH, &tty );// install

Handling a key-press Here’s a ‘simple’ signal-handler that lets a user decide to terminate our program (by hitting the -key) instead of the program itself deciding to quit when a counter reaches zero void on_input( int signum ) { intinch = 0; read( 0, &inch, 4 ); if ( inch == ESCAPE_KEY ) done = 1; }

Enabling ‘asynchronous’ I/O Now we need to install our signal-handler, specify which program will receive SIGIO, then enable the delivery of these signals signal( SIGIO, on_input ); fcntl( 0, F_SETOWN, getpid() ); int flagval = fcntl( 0, F_GETFL, NULL ); flagval |= O_ASYNC;// turn on flag-bit fcntl( 0, F_SETFL, flagval );

Program-loop revised again done = 0; do{ oldx = xloc, oldy = yloc;// remember draw_ball();// use current location vsync();// await next retrace hide_ball();// erase previous ball } while ( !done ); // ‘xloc’, ‘yloc’, and ‘done’ get changed by handlers

Enhancment: more user-control In ‘pong’ game the user moves a ‘paddle’ The paddle can only be moved left or right Lower wall of the playing-court is removed Ball will “escape” unless it hits the paddle Keyboard has left and right “arrow keys” Our input signal-handler could “move” the paddle whenever a user hits an arrow-key

In-class exercise #1 Before you remove the game-court’s lower wall, see if you can implement the paddle- movements (left or right) in response to a user’s hitting the left-arrow or right-arrow You will need to investigate the numerical code-sequence that Linux generates when a user hits one of these arrow-keys Our ‘rawtty.cpp’ application may be useful!

In-class exercise #2 For brevity, our ‘animate2’ demo-program removed the capability of users controlling the speed of the ball-movements (with an argument supplied on the command-line) Can you devise a way for users to exert “real-time” control over the ball’s speed by pressing keys while the program is running (for example, the ‘+’ key and the ‘-’ key)?