7.1 video games and Operating Systems
1. How does a video game work?
(1) Space: The game must draw images at specific locations on the computer screen.
(2) Time: The image moves at different speeds on the screen. Change the location at a specific interval.
(3) Interruption: users can generate input at any time.
(4) do several things at the same time: the game must ensure that several objects are moved and interrupted accordingly.
2. The operating system faces similar problems
(1) Space: The kernel loads the program into the memory space and maintains the location of each program in the memory.
(2) Time: The program runs at intervals of time slices under kernel scheduling;
At the same time, the kernel also runs a specific internal task at a specific time.
(3) interrupt: the kernel must be input at any time within a short period of time.
(4) do several things at the same time.
3. Screen Management, time, signals, and shared resources
(1) four basic topics learned in this chapter: ① screen management;
② Time;
③ Signal (final disconnection );
④ How to do several things safely at the same time.
(2) character-based graphical interface.
7.2 programming task: single player
1. Game Summary(1)BallMoving at a certain speed
(2)BallEncounterWallsOrBaffle PlateWill be bounce back
(3) The user presses the button to controlBaffle PlateMove up/down
Goal: do not let the ball fly out.
2. Three Elements: Wall
Ball: bounce on the screen
Baffle Plate: User Control
3. Understanding how to implement gamesFour basic themes:
① Screen management;
② Time;
③ Signal (final disconnection );
④ How to do several things safely at the same time.
----------- Learn these four basic themes in sequence
Screen Management
7.3 screen programming: Curses Library
1.Curses Library--- Use it to setTerminal screenThe position and character style of the cursor.
2. Most controlsTerminal screenUse the curses library.
7.3.1 introduction to the curses Library
1. Curses regards the screen as a coordinate grid. The upper left corner is the coordinate origin.
2. Nine functions used in this chapter.
7.3.2 virtual and real screen
1. Curses is used to update text Screens without blocking communication lines.
Curses minimizes data traffic through a virtual screen.
2. Real screen: The character array.
Curses retains two internal versions of the screen:
Internal screen: copy the real screen
Working screen: record changes to the screen (changes made by the curses function)
The working screen is like the disk cache, and most curses functions only modify it.
3. How refresh functions work
(1) Refresh compares the differences between a real screen and a working screen,
And then sendEnableThe same character and control code as the working screen.
(2) transfer only the changed content rather than the image itself. This technology is used in video streams.
For example, Smith James is displayed in the upper left corner of the real screen. addstr is displayed in the same position on the Working screen.
Refresh only replaces m and S with N and spaces.
Time
7.4 clock programming: Sleep ()
The game needsSpecific timePlaceSpecific location(1) Use curses to place the image in a specific position.
(2) then add the corresponding time in the program.
Static display:
Hello1.cDisplay a string (Hello, world) on the screen)
Hello2.cIt is displayed from top to bottom, and the background is reversed. Each time X is left and backward, a unit is moved.
Animation display:
Hello3.cOn the basis of 2,Add a row per second.
Hello4.cAdd a row per second on the basis of 2,The previous row is overwritten by a blank line.. (Move slowly along the diagonal line to the bottom right corner)
Hello5.cThe string is in a row, and the left back wall pops up.
7.5 clock programming 1: Alarms -----Clock and advanced signal Programming
The role of the clock:1. Add latency to the execution stream.------------------ Call sleep (7.5.1 and 7.5.2)
2. Schedule a task to be executed at a certain moment in the future .-----Call alarm (7.5.3)
7.5.1sleep function: Add latency
UnsignedSleep(Unsigned seconds );
Function: Step 1: call sleep () and the process is suspended.
Step 2: A process that meets ① ② is awakened:
① Seconds after suspension.
② In secondsWithinThe process captures a signal and returns it from the processing program.
Step 3: continue the process.
Return Value: ① 0 is returned.
② Return (seconds-actual sleep time)
7.5.2 alarm () + pause (): Sleep ()
1. Principle: every process in the system has a privateAlarm:
Set a time for this alarm (set with alarm ).
Once the time arrives, the clock sends a signal to all processes of sigalrm.
Unless sigalrm sets the processing function with signal (), the signal will kill the process.
2.Sleep ()A function consists of three steps:
(1) set a processing function for sigalrm (call signal ()).
(2) call alarm (seconds): SetAlarm---- SecondsAfterSends a sigalrm signal to the process.
(3) Call pause (): suspends the process until the signal reaches.
Implemented: The process is suspended.
Seconds receives the signal (sigalrm) in seconds. After the processing function is executed to return the signal, pause returns the result.
The process is awakened.
Alarm: Alarm = timer (# include <unistd. h>)
1. Unsigned old =Alarm(Unsigned seconds );
Function: Set an alarm time for the process: After seconds, the kernel sends sigalrm to this process.
Return: Successful: If you call this alarm () before:
ProcessAlready setThe alarm time is returnedRemaining Time of the last alarm time.
ProcessNot SetThe alarm time is returned.0.
Error:-1
Explanation:
(1) Signal Processing: IfIgnoreOrNot captured: Terminate the process that calls the alarm function by default.
① Capture: use the signal function.
② No capture: no processing is performed.
③ Ignore: captured but processed by default. No special processing is performed using the processing function.
(2) A process can only have one alarm time.
If you have set the alarm time before calling alarm, and it has not timed out:
Then any previous alarm time is replaced by the new value. (If the previous alarm time is not counted, only the current processing is performed.) Alarm returns the remaining value of the last alarm time.
NOTE: If seconds = 0, the new alarm time is 0, which means the alarm is disabled.
2. Result =Pause();
Function: suspends a process until a signal arrives.
Return: (1) the call process is terminated by this signal. Pause does not return values.
(2) It is captured by signal and returned by the processing function, and then returned by pause. Errno is set to eintr
Bytes ----------------------------------------------------------------------------------------------------------
7.5.3 actions to be scheduled
Another purpose of a Timer: Schedule an action that occurs at a certain moment in the future, and do other things at the same time.
Implementation: ThroughAlarmSet the timer and then do something else.
When the timer time arrives, the signal is sent and the processing function is called.