SDL four

xiaoxiao2021-03-05  25

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

// Screen SurfaceSDL_SURFACE * GSCreen; // Picture SurfaceSdl_surface * gpicture; // Heightmap SurfaceSDL_SURFACE * GHEIGHTMAP; // Texture SurfaceSDL_SURFACE * GTEXTURE

// Lookup TableShort * Glut;

// screen pitch # define pitch (gscreen-> pitch / 4) // screen width # define width 480 // Screen Height # definition of pi # define pi 3.141592653589732384626433832795f

// Physics Items Per Second # Define Physicsfps 100

// Last Iteration's Tick Valueint GlastTick;

void init () {// load-color SDL_Surface * temp = SDL_LoadBMP ( "picture18.bmp"); gPicture = SDL_ConvertSurface (temp, gScreen-> format, SDL_SWSURFACE); SDL_FreeSurface (temp); // temp = Load grayscale SDL_LoadBMP ( "heightmap18.bmp"); gHeightmap = SDL_ConvertSurface (temp, gScreen-> format, SDL_SWSURFACE); SDL_FreeSurface (temp); // load light temp = SDL_LoadBMP ( "texture18.bmp"); gTexture = SDL_ConvertSurface (temp, gScreen -> Format, SDL_swsurface; SDL_FREESURFACE (TEMP);

INT I, J; GLUT = New Short [width * height]; MEMSET (GLUT, 0, SIZEOF (Short) * Width * Height;

IF (SDL_MustLock (GheightMap)) f (SDL_LOCKSURFACE (GHEIGHTMAP) <0) Return; // Here, the grayscale map is mixed according to the same positional pixel point in the phase difference, and for the render, for (i = 1; i pixels) [J (i - 1) * (GHEIGHTMAP-> PITCH / 4)] & 0xff) - (((unsigned int *) Gheightmap-> pixels) [J (i 1) * (GheightMap-> Pitch / 4)] & 0xFF) ); Int xdiff = (((unsigned int *) gheightmap-> pixels) [J - 1 i * (Gheightmap-> Pitch / 4)] & 0xff) - (((unsigned int *) Gheightmap-> Pixels) [J 1 I * (Gheightmap-> Pitch / 4)] & 0xff)); int TMP = GLUT [i * width j] = ((YDIFF & 0xFF) << 8) | (xdiff & 0xff); // printf ("--- init pixel point -% D / N", J (i - 1) * (Gheightmap-> Pitch / 4)); //printf ("----- initf Pixel point value -----% d / n "(((unsigned int *) Gheightmap-> pixels) [j (i - 1) * (GheightMap-> Pitch / 4)])]); //printf ("----- init pixel point -----% d / n", J (i 1) * (Gheightmap-> Pitch / 4)); //printf ("----- initf Pixel point value -----% D / N "(((unsigned int *) Gheightmap-> Pixels) [J (i 1) * (Gheightmap-> Pitch / 4)])); //printf (" ----- init grays图 特 点 -----% D / N ", TMP); //printf (" ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------ / n ");}}

IF (SDL_MustLock (Gheightmap) SDL_Unlocksurface (Gheightmap);

unsigned int blend_add (unsigned int source, unsigned int target) {unsigned int sourcer = (source >> 0) & 0xff; unsigned int sourceg = (source >> 8) & 0xff; unsigned int sourceb = (source >> 16) & 0xff; unsigned int targetr = (target >> 0) & 0xff; unsigned int targetg = (target >> 8) & 0xff; unsigned int targetb = (target >> 16) & 0xff; targetr = Source; targetg = sourceg ; TargetB = sourceb;

IF (Targetr> 0xFF) Targetr = 0xFF; if (targetg> 0xFF) Targetg = 0xff; if (targetb> 0xff) TargetB = 0xff;

Return (Targetg << 8) | (TargetB << 16);}

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

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

While (GlastTick

GlastTick = 1000 / Physicsfps;

// Lock Surface If Needed IF (SDL_Mustlock (Gscreen)) IF (SDL_LOCKSURFACE (Gscreen <0) Return;

// rendering here if (SDL_MUSTLOCK (gPicture)) if (SDL_LockSurface (gPicture) <0) return; if (SDL_MUSTLOCK (gTexture)) if (SDL_LockSurface (gTexture) <0) return; // movement path int posx = (int) ((SIN (Tick * 0.000645234) 1) * width / 4); int Posy = (int) ((SIN (Tick * 0.000445234) 1) * Height / 4); INT I, J; for (i = 0 ; i = gtexture-> w || u <0 || u> = gtexture-> h) {(unsigned int *) gscreen-> pixels) [(j) (i) * Pitch] = ((unsigned int *) gpicture-> pixels) [(j) (i) * (gpicture-> pitch / 4)];} else // mix the light size and color map This size mixed operation is exchanged Go to the main surface {(unsigned int *) gscreen-> pixels) [(j) (i) * pitch] = blend_add ((unsigned int *) gpicture-> pixels) [(j) (i) * ( GPicture-> Pitch / 4)] ((unsigned int *) gtexture-> pixels) [(u) (v) * (gtexture-> pitch / 4)]);}}} f (sdl_mustlock (gtexture)) SDL_UNLOCKSURFACE (gtexture) ;

IF (SDL_Mustlock (GPICTURE)) SDL_UNLOCKSURFACE (GPICTURE);

// 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 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_KEYUP: switch (event.key.keysym.sym) {case SDLK_ESCAPE: / / If escape ispered, return (and thus, quit) return 0;} Break; Case SDL_Quit: return (0);}}} Return 0;

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

New Post(0)