SDL three

xiaoxiao2021-03-05  25

#include #include #include #IF defined (_msc_ver) #include "sdl.h" # else # include "sdl / sdl.h" #ENDIF

// screen surfactsdl_surface * gscreen;

// Screen pitch # define PITCH (gScreen-> pitch / 4) // Screen width # define WIDTH 480 // Screen height # define HEIGHT 320 // Color of player's ball # define BALLCOLOR 0x007fff // Radius of the player's ball # define Radius 12 // Background Color # define bgcolor 0x5f0000 // Wall Color # define wallcolor 0x9f1f1f // definition of pi # define pi 3.141592653589732795f

// Player's thrust value # define thrust 0.1f // Slowdown Due to flan, etc. # define solutiondown 0.99f

// Physics Items Per Second # define Physicsfps 100 // Slowdown Due To Collision # Define CollisionSlowDown 0.99F

// Last Iteration's Tick Valueint GlastTick;

// Player's PositionFloat gxpos; float gypos;

// Player's motion vectorfloat gxmov; float gymov;

// Player's Key Statusint GKEYLEFT; INT GKEYUP; INT GKEYDOWN

Void Drawcircle (INT X, INT Y, INT R, INT C) {INT I, J; for (i = 0; I <2 * R; I ) {// Vertical Clipping: (Top and Bottom) IF ((Y - R I)> = 0 && (Y - R I)

// Left border if (XOFS <0) {len = XOFS; XOFS = 0;}

// Right Border if (XOFS LEN> = Width) {LEN - = (XOFS LEN) - Width;} int OFS = (Y - R I) * Pitch Xofs; // Note That Len May Be 0 At this point, // and no pixels get Drawn! for (j = 0; j pixels) [OFS J] = C;}}} void DrawRect (INT X , int y, int width, int {INT I, J; For (i = 0; i = 0 && (Y I)

// Left border if (XOFS <0) {len = XOFS; XOFS = 0;}

// Right Border if (XOFS LEN> = Width) {LEN - = (XOFS LEN) - width;} int OFS = (i y) * PITCH XOFS;

// Note That Len May Be 0 at this point, // and no pixels get Drawn! for (j = 0; j pixels) [OFS J] = C }}}

Void init () {gxpos = width / 2; gypos = height / 2;

Gxmov = 0; GYMOV = 0;

GKEYLEFT = 0; gkeyright = 0; gkeyup = 0; gkeydown = 0;}

Void render () {// ask SDL for the Time In MilliseConds Int Tick = SDL_GETTICKS ();

IF (Tick <= GlastTick) {SDL_DELAY (1); Return;}

While (GlastTick

GXMOV * = Slowdown; Gymov * = Slowdown;

GXPOS = Gxmov; GYPOS = Gymov; // Collision with the screen borders if (gxpos> width - radius) {gxpos - = gxmov; gxmov = -gxmov * CollisionSlowDown;}}}}}}}}}}}}}}}}}}}} (gxpos HEIGHT - RADIUS) {gYPos - = gYMov; gYMov = -gYMov * COLLISIONSLOWDOWN;} if (gYPos

// Fill Background DrawRect (0, 0, Width, Height, BGColor); // Draw Borders DrawRect (0, 0, Width, 8, Wallcolor); DrawRect (0, 0, 8, Height, Wallcolor); DrawRect (Width - 8, 0, 8, Height, Wallcolor; DrawRect (0, Height - 8, Width, 8, Wallcolor);

// Draw the Player Object DrawCircle ((int) gxpos, (int) GYPOS, RADIUS, BALLCOLOR); // Unlock if Needed IF (SDL_Mustlock (gscreen)) SDL_UNLOCKSURFACE (GScreen);

// Tell SDL to Update The Whole Gscreen SDL_UPDATERECT (Gscreen, 0, 0, Width, Height);

// entry pointint main (int Argc, char * argv []) {// Initialize SDL's Subsystems - in this case, only video. If (SDL_INIT (SDL_INIT_VIDEO) <0) {fprintf (stderr, "unable to init sdl:% S / N ", SDL_GETERROR ()); exit (1);}

// Register SDL_Quit to be called at exit; makes sure things are // cleaned up when we quit atexit (SDL_Quit);.. // Attempt to create a WIDTHxHEIGHT window with 32bit pixels gScreen = SDL_SetVideoMode (WIDTH, HEIGHT, 32, SDL_SWSURFACE Init ();

// if we fail, return error. IF (gscreen == null) {fprintf (stderr, "unable to set up video:% s / n", SDL_GETERROR ()); exit (1);} // main loop: LOOP Forever. While (1) {// Render Stuff Render ();

// Poll for events, and handle the ones we care about SDL_Event event; while (SDL_PollEvent (& event)) {switch (event.type) {case SDL_KEYDOWN:. Switch (event.key.keysym.sym) {case SDLK_LEFT: gKeyLeft = 1; break; case SDLK_RIGHT: gKeyRight = 1; break; case SDLK_UP: gKeyUp = 1; break; case SDLK_DOWN: gKeyDown = 1; break;} break; case SDL_KEYUP: switch (event.key.keysym.sym) {case SDLK_ESCAPE: // If escape is pressed, return (and thus, quit) return 0; case SDLK_LEFT: gKeyLeft = 0; break; case SDLK_RIGHT: gKeyRight = 0; break; case SDLK_UP: gKeyUp = 0; break; case SDLK_DOWN: gKeyDown = 0; Break;} Break; Case SDL_Quit: return (0);}} } Return 0;}

转载请注明原文地址:https://www.9cbs.com/read-36393.html

New Post(0)