An SDL example (II)-everything starts from main

Source: Internet
Author: User

Since pig uses C code, we can analyze it from Main. The basic steps of the SDL call process are as follows:
1. initialize SDL through sdl_init
2. Build the main surface through sdl_setvideomode
3. Get the keyboard status array through sdl_getkeystate
4. Start message loop
5. Get the message through sdl_pollevent and process it until it is completed.

The following are the main functions.

/*----------------------------------------------------------
Main ()
----------------------------------------------------------*/
Int main (INT argc, char * argv [])
{
Sdl_surface * screen;
Gamestate * GS;
Int I;
Int bpp = 0;
Int last_tick, start_time, end_time;
Int dashframe;
Float logic_fps = 20.0;
Int flags = sdl_doublebuf | sdl_hwsurface;

// SDL video Initialization
Sdl_init (sdl_init_video );

// Press the function sdl_quit into the exit Handler
Atexit (sdl_quit );

// Set the interrupt handle and call breakhandler when Ctrl + break and program abort.
// Reset the interrupt handling handle in breakhandler and set the interrupt flag to 1.
Signal (sigterm, breakhandler );
Signal (SIGINT, breakhandler );

// Processing Parameters
For (I = 1; I <argc; ++ I)
{
If (strncmp (argv [I], "-s", 2) = 0)
Flags & = ~ Sdl_doublebuf;
Else if (strncmp (argv [I], "-F", 2) = 0)
Flags | = sdl_fullscreen;
Else
Bpp = atoi (& argv [I] [1]);
}

// Set the width and height of the video mode. Because BPP is set to 0, the current color number is used.
Screen = sdl_setvideomode (screen_w, screen_h, bpp, flags );
If (! Screen)
{
Fprintf (stderr, "failed to open screen! /N ");
Return 1;
}

// Set the title and Icon name of the window
Sdl_wm_setcaption ("fixed rate pig", "pig ");

// Hide the mouse
Sdl_showcursor (0 );

// Initialize the game status Structure
GS = init_all (screen );
If (! GS)
Return 1;

// Obtain the keyboard status. The keyboard status is an array of uint8 that contains the current status of all buttons.
// Example: If (GS-> keys [sdlk_return])... <return> is pressed.
GS-> keys = sdl_getkeystate (& I );

// Initialize information about the logical Region
GS-> logic_frames = 0;
GS-> rendered_frames = 0;
GS-> pe-> before_objects = before_objects;

// Initialize the genie and original image in pig Engine
Pig_start (GS-> PE, 0 );
GS-> refresh_screen = GS-> pe-> pages;

// Record the current time
Start_time = last_tick = sdl_getticks ();

// Start the message processing cycle
While (GS-> running)
{
Int tick;
Float frames, DT;
Sdl_event EV;

/* Handle input */
// Sdl_pollevent is somewhat similar to peekmessage
While (sdl_pollevent (& eV)> 0)
// Handle keyboard and mouse events
Handle_input (GS, & eV );

// Empty Function
Handle_keys (GS );

// Determine whether the program is interrupted
// Previously set break_received to 1 when Ctrl + break or program end interruption occurs through signal
// Note that the message loop is not directly interrupted, so you can exit after processing the operation of this loop.
If (break_received)
GS-> running = 0;

/* Calculate time since last update */
// The time interval between the calculation and the previous message loop is used here, And the ID of the logical Shard to be displayed is determined by displaying the shard number parameter.
Tick = sdl_getticks ();
Dt = (tick-last_tick) * 0.001;
Frames = DT * logic_fps;

/* Run the game logic */
// Display the logical Region
Pig_animate (GS-> PE, frames );

/*
* Limit the dashboard frame rate to 15 FPS
* When there's no wobbling going on.
*
* The 'dashframework' deal is about keeping
* Pages in sync on a double buffered display.
*/
If (GS-> lives_wobble | GS-> score_wobble |
(GS-> dashboard_time> 1.0/15.0 ))
{
Dashframe = GS-> pe-> pages;
GS-> dashboard_time = 0;
}
If (dashframe)
{
-- Dashframe;
Dashboard (GS );
}

/* Update sprites */
If (GS-> refresh_screen)
{
-- GS-> refresh_screen;
Pig_refresh_all (GS-> PE );
}
Else
Pig_refresh (GS-> PE );

/* Make the new frame visible */
Pig_flip (GS-> PE );

/* Update statistics, timers and stuff */
++ GS-> rendered_frames;
GS-> lives_wobble_time + = DT;
GS-> score_wobble_time + = DT;
GS-> dashboard_time + = DT;

// Record the time to facilitate the final calculation
Last_tick = tick;

// Nice seems to be a sign of some effect, but it is not clear yet
If (GS-> nice)
Sdl_delay (10 );
}

/* Print some statistics */
End_time = sdl_getticks ();
I = end_time-start_time;
Printf ("Total time running: % d MS/N", I );
If (! I)
I = 1;
Printf ("average rendering Frame Rate: %. 2f FPS/N ",
GS-> rendered_frames * 1000.0/I );
Printf ("average logic frame rate: %. 2f FPS/N ",
GS-> logic_frames * 1000.0/I );

// Release pig Engine
Pig_close (GS-> PE );
Return 0;
}

Note: although the title of the form is set in the Code, there is no part generated by the form. Does SDL automatically create the form? Or hidden in unexpanded code? I believe that as the Code continues to expand, it should be clearer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.