Guest User

Untitled

a guest
Jan 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.82 KB | None | 0 0
  1. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  2. &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  3.                 R   O   U   T   I   N   E   S   :
  4. &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  5. &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  6.  
  7.  
  8. // frees a buffer
  9. void cleanbuf(void*buffer) {
  10.     SDL_FreeSurface((SDL_Surface*)buffer);
  11.     buffer = NULL;
  12. }
  13.  
  14. // pauses for given time in milliseconds
  15. // THIS IS DEPRECATED, KEPT FOR POSSIBLE
  16. // TESTING USES ONLY
  17. inline void pausems_deprecated(unsigned int ms) {
  18.     SDL_Delay(ms);
  19. }
  20.  
  21. // flips the non-intermediate screen
  22. int updatescreen(void) {
  23.     return (SDL_Flip((SDL_Surface*)screen)==-1)?(ERROR_EXIT):(CLEAN_EXIT);
  24. }
  25.  
  26. // loads an image of pretty much any image format
  27. // and returns the subsequent SDL_Surface made
  28. SDL_Surface*load_image(char*filename) {
  29.     SDL_Surface* img = IMG_Load(filename);
  30.     if(img) { img = SDL_DisplayFormatAlpha(img); }
  31.     return img;
  32. }
  33.  
  34. // simply draws data from one surface to another
  35. // no locking/unlocking done, nothing special
  36. void draw(int x, int y, SDL_Surface* source, SDL_Surface* destination) {
  37.     SDL_Rect offset;
  38.     offset.x = x;
  39.     offset.y = y;
  40.     SDL_BlitSurface(source, NULL, destination, &offset);
  41. }
  42.  
  43. // creates a new surface with a width and height
  44. // creates as an SDL_Surface
  45. SDL_Surface*create_surface(int width, int height) {
  46.     SDL_Surface*img = SDL_CreateRGBSurface(0, width, height, SCREEN_BPP, 0, 0, 0, 0);
  47.     return img;
  48. }
  49.  
  50. // set to fullscreen (really no use to this)
  51. void set_fullscreen(void) {
  52.     unsigned int flags = screen->flags;
  53.     screen = SDL_SetVideoMode(0, 0, 0, screen->flags ^ SDL_FULLSCREEN);
  54.     if(screen == NULL) { screen = SDL_SetVideoMode(0, 0, 0, flags); }
  55.     if(screen == NULL) { exit(ERROR_EXIT); }
  56. }
  57.  
  58. // draws a rectangle on the given buffer
  59. void rectangle(SDL_Rect*rect, SDL_Surface*buffer, Uint32 color) {
  60.     SDL_FillRect(buffer, rect, color);
  61. }
  62.  
  63. // sets buffer to all black
  64. void blackout(SDL_Surface*buffer) {
  65.     SDL_Rect rect = {0, 0, buffer->w, buffer->h};
  66.     rectangle(&rect, buffer, COLOR_BLACK);
  67. }
  68.  
  69. // returns pointer to new rectangle struct
  70. SDL_Rect*rect(int x, int y, int w, int h) {
  71.     static SDL_Rect rect;
  72.     rect.x = x; rect.y = y; rect.w = w; rect.h = h;
  73.     return ▭
  74. }
  75.  
  76. // returns pointer to new rectangle struct
  77. // only with an x and y
  78. SDL_Rect*coord(int x, int y) {
  79.     static SDL_Rect rect;
  80.     rect.x = x; rect.y = y;
  81.     return ▭
  82. }
  83.  
  84.  
  85. // displays the small buffer onto the big buffer and updates and stuff
  86. // good stuff
  87. void updatesmallscreen(void) {
  88.     Uint32 color = 0x00000000;
  89.     Uint32*pixels = (Uint32*)intscreen->pixels;
  90.     if(should_lock) {
  91.         SDL_LockSurface(intscreen);
  92.     }
  93.     blackout(screen);
  94.     for(int j = 0; j < 128; j++) {
  95.         for(int i = 0; i < 192; i++) {
  96.             color = pixels[j * intscreen->w + i];
  97.             rectangle(rect(i*SCREEN_ZOOM_FACTOR, j*SCREEN_ZOOM_FACTOR, SCREEN_ZOOM_FACTOR,
  98.                            SCREEN_ZOOM_FACTOR), screen, color);
  99.         }
  100.     }
  101.     if(should_lock) {
  102.         SDL_UnlockSurface(intscreen);
  103.     }
  104.     if(updatescreen() != CLEAN_EXIT) {
  105.         exit(ERROR_EXIT);
  106.     }
  107. }
  108.  
  109. // draws a line on the given buffer
  110. void line(int startx, int starty, int endx, int endy, SDL_Surface*buffer, Uint32 color) {
  111.     int t, distance;
  112.     int xerr=0, yerr=0, delta_x, delta_y;
  113.     int incx, incy;
  114.     delta_x=endx-startx;
  115.     delta_y=endy-starty;
  116.     if(delta_x>0) incx=1;
  117.     else if(delta_x==0) incx=0;
  118.     else incx=-1;
  119.     if(delta_y>0) incy=1;
  120.     else if(delta_y==0) incy=0;
  121.     else incy=-1;
  122.     delta_x=abs(delta_x);
  123.     delta_y=abs(delta_y);
  124.     if(delta_x>delta_y) distance=delta_x;
  125.     else distance=delta_y;
  126.     for(t=0; t<=distance+1; t++) {
  127.         rectangle(pixel(startx, starty), buffer, color);
  128.         xerr+=delta_x;
  129.         yerr+=delta_y;
  130.         if(xerr>distance) {
  131.             xerr-=distance;
  132.             startx+=incx;
  133.         }
  134.         if(yerr>distance) {
  135.             yerr-=distance;
  136.             starty+=incy;
  137.         }
  138.     }
  139. }
  140.  
  141. // constructor for a pixel (rectangle-based)
  142. // no attached color
  143. SDL_Rect*pixel(int x, int y) {
  144.     static SDL_Rect rect;
  145.     rect.x = x; rect.y = y; rect.w = 1; rect.h = 1;
  146.     return &rect;
  147. }
  148.  
  149. // exits the program quickly and cleanly
  150. void quit_quickly(void) {
  151.     cleanbuf(intscreen);
  152.     musicend();
  153.     Mix_CloseAudio();
  154.     SDL_Quit();
  155.     exit(CLEAN_EXIT);
  156. }
  157.  
  158. // starts playing music in a loop
  159. void musicset(Mix_Music*music) {
  160.     Mix_PlayMusic(music, -1);
  161. }
  162.  
  163. // starts playing music, no loop
  164. void musicsetstart(Mix_Music*music) {
  165.     Mix_PlayMusic(music, 0);
  166. }
  167.  
  168. // stops the music
  169. inline void musicpause(void) {
  170.     Mix_PauseMusic();
  171. }
  172.  
  173. // starts the music
  174. inline void musicresume(void) {
  175.     Mix_ResumeMusic();
  176. }
  177.  
  178. // opens a music file
  179. Mix_Music*open_music(char*filename) {
  180.     return Mix_LoadMUS(filename);
  181. }
  182.  
  183. // stop all music
  184. inline void musicend(void) {
  185.     Mix_HaltMusic();
  186. }
  187.  
  188. // check if music is playing
  189. inline int musicisplaying(void) {
  190.     return Mix_PlayingMusic();
  191. }
  192.  
  193. //pauses without bad effects
  194. void pausems(int ms) {
  195.     SDL_Event*event = malloc(sizeof(SDL_Event));
  196.     int timer = SDL_GetTicks();
  197.     while(SDL_GetTicks() - timer < ms) {
  198.         SDL_PollEvent(event);
  199.         if(event->type == SDL_QUIT) {
  200.             quit_quickly();
  201.         }
  202.     }
  203.  
  204. }
  205.  
  206. // draws sprite to intscreen
  207. void drawsprite(int x, int y, int w, int h, SDL_Surface*surface, int xl, int yl) {
  208.     SDL_Rect*rec = rect(xl, yl, w, h);
  209.     SDL_Rect*cord = coord(x, y);
  210.     SDL_BlitSurface(surface, rec, intscreen, cord);
  211. }
  212.  
  213. // draws a pixel to intscreen
  214. void drawpixel(SDL_Rect*rec, Uint32 color) {
  215.     rectangle(rect(rec->x, rec->y, 1, 1), intscreen, color);
  216. }
Add Comment
Please, Sign In to add comment